diff --git a/src/adaptors/UBSvgSubsetAdaptor.cpp b/src/adaptors/UBSvgSubsetAdaptor.cpp index 457d3970..b8f76027 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.cpp +++ b/src/adaptors/UBSvgSubsetAdaptor.cpp @@ -391,8 +391,6 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() UBGraphicsStroke* annotationGroup = 0; UBGraphicsStrokesGroup* strokesGroup = 0; - QMap strokesList; - while (!mXmlReader.atEnd()) { mXmlReader.readNext(); @@ -562,8 +560,11 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() { polygonItem = polygonItemFromLineSvg(mScene->isDarkBackground() ? Qt::white : Qt::black); } + if (polygonItem) { + mScene->addItem(polygonItem); + polygonItem->setUuid(uuidFromSvg); if (annotationGroup) @@ -580,19 +581,7 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() polygonItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Graphic)); - QString parentId = mXmlReader.attributes().value(mNamespaceUri, "parent").toString(); - Q_ASSERT(!parentId.isEmpty()); - UBGraphicsStrokesGroup* group; - if(!strokesList.contains(parentId)){ - group = new UBGraphicsStrokesGroup(); - strokesList.insert(parentId,group); - } - else - group = strokesList.value(parentId); - polygonItem->show(); - group->addToGroup(polygonItem); - } } else if (mXmlReader.name() == "polyline") @@ -615,17 +604,6 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() } polygonItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Graphic)); - - QString parentId = mXmlReader.attributes().value(mNamespaceUri, "parent").toString(); - Q_ASSERT(!parentId.isEmpty()); - UBGraphicsStrokesGroup* group; - if(!strokesList.contains(parentId)){ - group = new UBGraphicsStrokesGroup(); - strokesList.insert(parentId,group); - } - else - group = strokesList.value(parentId); - polygonItem->show(); } } @@ -883,6 +861,7 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() UBGraphicsAppleWidgetItem* appleWidgetItem = graphicsAppleWidgetFromSvg(); if (appleWidgetItem) { + // appleWidgetItem->setFlag(QGraphicsItem::ItemIsMovable, true); appleWidgetItem->setFlag(QGraphicsItem::ItemIsSelectable, true); appleWidgetItem->resize(foreignObjectWidth, foreignObjectHeight); @@ -978,12 +957,6 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() } else if (mXmlReader.name() == tGroups) { //considering groups section at the end of the document - QMapIterator iterator(strokesList); - while (iterator.hasNext()) { - iterator.next(); - mScene->addItem(iterator.value()); - } - readGroupRoot(); } else @@ -997,6 +970,7 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() { if(strokesGroup && mScene){ mScene->addItem(strokesGroup); + //graphicsItemFromSvg(strokesGroup); } if (annotationGroup) @@ -1011,8 +985,6 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() } } - qDebug() << "Number of detected strokes: " << strokesList.count(); - if (mXmlReader.hasError()) { qWarning() << "error parsing Sankore file " << mXmlReader.errorString(); @@ -1036,7 +1008,7 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() UBGraphicsGroupContainerItem* UBSvgSubsetAdaptor::UBSvgSubsetReader::readGroup() { UBGraphicsGroupContainerItem *group = new UBGraphicsGroupContainerItem(); -// QMultiMap strokesGroupsContainer; + QMultiMap strokesGroupsContainer; QList groupContainer; mXmlReader.readNext(); @@ -1045,9 +1017,10 @@ UBGraphicsGroupContainerItem* UBSvgSubsetAdaptor::UBSvgSubsetReader::readGroup() if (mXmlReader.isEndElement()) { mXmlReader.readNext(); break; - } - else if (mXmlReader.isStartElement()) { - if (mXmlReader.name() == tGroup) { + } else if (mXmlReader.isStartElement()) + { + if (mXmlReader.name() == tGroup) + { qDebug() << "came across the group id is" << mXmlReader.attributes().value(aId); UBGraphicsGroupContainerItem *curGroup = readGroup(); if (curGroup) @@ -1055,69 +1028,67 @@ UBGraphicsGroupContainerItem* UBSvgSubsetAdaptor::UBSvgSubsetReader::readGroup() else qDebug() << "this is an error"; } - else if (mXmlReader.name() == tElement) { + else if (mXmlReader.name() == tElement) + { QString id = mXmlReader.attributes().value(aId).toString(); -// QString itemId = id.right(QUuid().toString().size()); -// QString groupId = id.left(QUuid().toString().size()); + QString itemId = id.right(QUuid().toString().size()); + QString groupId = id.left(QUuid().toString().size()); QGraphicsItem *curItem = readElementFromGroup(); -// UBGraphicsPolygonItem *curPolygon = qgraphicsitem_cast(curItem); -// -// if (curPolygon && !groupId.isEmpty() && !itemId.isEmpty() && itemId != groupId) { -// strokesGroupsContainer.insert(groupId, curPolygon); -// } -// else {// item + UBGraphicsPolygonItem *curPolygon = qgraphicsitem_cast(curItem); - // Explanation: the second condition discriminate the old storage version that should - // not be interpreted anymore - if(curItem && id.count("{") < 2) - groupContainer.append(curItem); - else - qDebug() << "this is an error"; -// } - } - else { + if (curPolygon && !groupId.isEmpty() && !itemId.isEmpty() && itemId != groupId) + { + strokesGroupsContainer.insert(groupId, curPolygon); + } + else // item + { + if(curItem) + groupContainer.append(curItem); + else + qDebug() << "this is an error"; + } + }else { mXmlReader.skipCurrentElement(); } - } - else { + } else { mXmlReader.readNext(); } } -// foreach (QString key, strokesGroupsContainer.keys().toSet()) -// { -// UBGraphicsStrokesGroup* pStrokesGroup = new UBGraphicsStrokesGroup(); -// UBGraphicsStroke *currentStroke = new UBGraphicsStroke(); -// foreach(UBGraphicsPolygonItem* poly, strokesGroupsContainer.values(key)) -// { -// if (poly) -// { -// mScene->removeItem(poly); -// mScene->removeItemFromDeletion(poly); -// poly->setStrokesGroup(pStrokesGroup); -// poly->setStroke(currentStroke); -// pStrokesGroup->addToGroup(poly); -// } -// } -// if (currentStroke->polygons().empty()) -// delete currentStroke; - -// if (pStrokesGroup->childItems().count()) -// mScene->addItem(pStrokesGroup); -// else -// delete pStrokesGroup; - -// if (pStrokesGroup) -// { -// QGraphicsItem *strokeGroup = qgraphicsitem_cast(pStrokesGroup); -// if(strokeGroup) -// groupContainer.append(strokeGroup); -// else -// qDebug() << "this is an error"; -// } -// } + foreach (QString key, strokesGroupsContainer.keys().toSet()) + { + UBGraphicsStrokesGroup* pStrokesGroup = new UBGraphicsStrokesGroup(); + UBGraphicsStroke *currentStroke = new UBGraphicsStroke(); + foreach(UBGraphicsPolygonItem* poly, strokesGroupsContainer.values(key)) + { + if (poly) + { + mScene->removeItem(poly); + mScene->removeItemFromDeletion(poly); + poly->setStrokesGroup(pStrokesGroup); + poly->setStroke(currentStroke); + pStrokesGroup->addToGroup(poly); + } + } + if (currentStroke->polygons().empty()) + delete currentStroke; + + if (pStrokesGroup->childItems().count()) + mScene->addItem(pStrokesGroup); + else + delete pStrokesGroup; + + if (pStrokesGroup) + { + QGraphicsItem *strokeGroup = qgraphicsitem_cast(pStrokesGroup); + if(strokeGroup) + groupContainer.append(strokeGroup); + else + qDebug() << "this is an error"; + } + } foreach(QGraphicsItem* item, groupContainer) group->addToGroup(item); @@ -1141,19 +1112,16 @@ void UBSvgSubsetAdaptor::UBSvgSubsetReader::readGroupRoot() if (mXmlReader.isEndElement()) { mXmlReader.readNext(); break; - } - else if (mXmlReader.isStartElement()) { + } else if (mXmlReader.isStartElement()) { if (mXmlReader.name() == tGroup) { - UBGraphicsGroupContainerItem *curGroup = readGroup(); + UBGraphicsGroupContainerItem *curGroup = readGroup(); if (curGroup) { mScene->addGroup(curGroup); } - } - else { + }else { mXmlReader.skipCurrentElement(); } - } - else { + } else { mXmlReader.readNext(); } } @@ -1166,9 +1134,8 @@ QGraphicsItem *UBSvgSubsetAdaptor::UBSvgSubsetReader::readElementFromGroup() QString uuid = id.right(QUuid().toString().size()); result = mScene->itemForUuid(QUuid(uuid)); - Q_ASSERT(result); -// if(!result) -// qDebug() << "uuid " << uuid; + if(!result) + qDebug() << "uuid " << uuid; mXmlReader.skipCurrentElement(); mXmlReader.readNext(); @@ -1272,15 +1239,38 @@ bool UBSvgSubsetAdaptor::UBSvgSubsetWriter::persistScene(int pageIndex) UBGraphicsStrokesGroup* strokesGroupItem = qgraphicsitem_cast(item); if(strokesGroupItem && strokesGroupItem->isVisible()){ + QDomElement newGroupElement; + if (!strokesGroupItem->parentItem() && strokesGroupItem->childItems().count()) { + newGroupElement = groupDomDocument.createElement(tGroup); + newGroupElement.setAttribute(aId, strokesGroupItem->uuid().toString()); + groupRoot.appendChild(newGroupElement); + } + + //disabling g section parsing as a group of elements. Use groups refs instead +// mXmlWriter.writeStartElement("g"); +// mXmlWriter.writeAttribute(UBSettings::uniboardDocumentNamespaceUri, "uuid", UBStringUtils::toCanonicalUuid(strokesGroupItem->uuid())); +// QMatrix matrix = item->sceneMatrix(); +// if (!matrix.isIdentity()){ +// mXmlWriter.writeAttribute("transform", toSvgTransform(matrix)); +// } + // Add the polygons foreach(QGraphicsItem* item, strokesGroupItem->childItems()){ UBGraphicsPolygonItem* poly = qgraphicsitem_cast(item); if(NULL != poly){ polygonItemToSvgPolygon(poly, true); + if (!newGroupElement.isNull()) { + QDomElement curPolygonElement = groupDomDocument.createElement(tElement); + curPolygonElement.setAttribute(aId, strokesGroupItem->uuid().toString() + + poly->uuid().toString()); + newGroupElement.appendChild(curPolygonElement); + } items.removeOne(poly); } } + +// mXmlWriter.writeEndElement(); //g } // Is the item a polygon? @@ -1564,16 +1554,14 @@ void UBSvgSubsetAdaptor::UBSvgSubsetWriter::persistGroupToDom(QGraphicsItem *gro if (!tmpUuid.isNull()) { if (item->type() == UBGraphicsGroupContainerItem::Type && item->childItems().count()) { persistGroupToDom(item, curParent, groupDomDocument); - } -// else if (item->type() == UBGraphicsStrokesGroup::Type) { -// foreach (QGraphicsItem *polygonItem, item->childItems()) { -// QDomElement curPolygonElement = groupDomDocument->createElement(tElement); -// curPolygonElement.setAttribute(aId, tmpUuid.toString() -// + UBGraphicsItem::getOwnUuid(polygonItem).toString()); -// curGroupElement.appendChild(curPolygonElement); -// } -// } - else { + } else if (item->type() == UBGraphicsStrokesGroup::Type) { + foreach (QGraphicsItem *polygonItem, item->childItems()) { + QDomElement curPolygonElement = groupDomDocument->createElement(tElement); + curPolygonElement.setAttribute(aId, tmpUuid.toString() + + UBGraphicsItem::getOwnUuid(polygonItem).toString()); + curGroupElement.appendChild(curPolygonElement); + } + } else { QDomElement curSubElement = groupDomDocument->createElement(tElement); curSubElement.setAttribute(aId, tmpUuid); @@ -1584,6 +1572,27 @@ void UBSvgSubsetAdaptor::UBSvgSubsetWriter::persistGroupToDom(QGraphicsItem *gro } } +void UBSvgSubsetAdaptor::UBSvgSubsetWriter::persistStrokeToDom(QGraphicsItem *strokeItem, QDomElement *curParent, QDomDocument *curDomDocument) +{ + QUuid uuid = UBGraphicsScene::getPersonalUuid(strokeItem); + if (!uuid.isNull()) { + QDomElement curStrokesGroupElement = curDomDocument->createElement(tStrokeGroup); + curStrokesGroupElement.setAttribute(aId, uuid); + curParent->appendChild(curStrokesGroupElement); + + foreach (QGraphicsItem *item, strokeItem->childItems()) { + QUuid tmpUuid = UBGraphicsScene::getPersonalUuid(item); + if (!tmpUuid.isNull()) { + if (item->type() == UBGraphicsPolygonItem::Type && item->childItems().count()) { + QDomElement curSubElement = curDomDocument->createElement(tElement); + curSubElement.setAttribute(aId, tmpUuid); + curStrokesGroupElement.appendChild(curSubElement); + } + } + } + } +} + void UBSvgSubsetAdaptor::UBSvgSubsetWriter::polygonItemToSvgLine(UBGraphicsPolygonItem* polygonItem, bool groupHoldsInfo) { mXmlWriter.writeStartElement("line"); diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 3c351b50..5e4b3130 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -826,14 +826,14 @@ void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) if (!intersectedPolygons[i].empty()) { - // intersected polygons generated as QList QPainterPath::toFillPolygons(), - // so each intersectedPolygonItem has one or couple of QPolygons who should be removed from it. + // intersected polygons generated as QList QPainterPath::toFillPolygons(), + // so each intersectedPolygonItem has one or couple of QPolygons who should be removed from it. for(int j = 0; j < intersectedPolygons[i].size(); j++) { // create small polygon from couple of polygons to replace particular erased polygon UBGraphicsPolygonItem* polygonItem = new UBGraphicsPolygonItem(intersectedPolygons[i][j], intersectedPolygonItem->parentItem()); - intersectedPolygonItem->copyItemParameters(polygonItem); + intersectedPolygonItem->copyItemParameters(polygonItem); polygonItem->setStroke(intersectedPolygonItem->stroke()); polygonItem->setStrokesGroup(intersectedPolygonItem->strokesGroup()); intersectedPolygonItem->strokesGroup()->addToGroup(polygonItem);