diff --git a/src/adaptors/UBSvgSubsetAdaptor.cpp b/src/adaptors/UBSvgSubsetAdaptor.cpp index b8f76027..741096d2 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.cpp +++ b/src/adaptors/UBSvgSubsetAdaptor.cpp @@ -552,25 +552,23 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() { UBGraphicsPolygonItem* polygonItem = 0; + QString parentId = mXmlReader.attributes().value(mNamespaceUri, "parent").toString(); if (mXmlReader.name() == "polygon") - { polygonItem = polygonItemFromPolygonSvg(mScene->isDarkBackground() ? Qt::white : Qt::black); - } else if (mXmlReader.name() == "line") - { polygonItem = polygonItemFromLineSvg(mScene->isDarkBackground() ? Qt::white : Qt::black); - } + + if(parentId.isEmpty() && strokesGroup) + parentId = strokesGroup->uuid().toString(); + + Q_ASSERT(!parentId.isEmpty()); if (polygonItem) { - mScene->addItem(polygonItem); - polygonItem->setUuid(uuidFromSvg); if (annotationGroup) - { polygonItem->setStroke(annotationGroup); - } if(strokesGroup){ @@ -581,7 +579,17 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() polygonItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Graphic)); + UBGraphicsStrokesGroup* group; + if(!mStrokesList.contains(parentId)){ + group = new UBGraphicsStrokesGroup(); + mStrokesList.insert(parentId,group); + } + else + group = mStrokesList.value(parentId); + polygonItem->show(); + group->addToGroup(polygonItem); + } } else if (mXmlReader.name() == "polyline") @@ -604,6 +612,17 @@ 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(!mStrokesList.contains(parentId)){ + group = new UBGraphicsStrokesGroup(); + mStrokesList.insert(parentId,group); + } + else + group = mStrokesList.value(parentId); + polygonItem->show(); } } @@ -861,7 +880,6 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() UBGraphicsAppleWidgetItem* appleWidgetItem = graphicsAppleWidgetFromSvg(); if (appleWidgetItem) { - // appleWidgetItem->setFlag(QGraphicsItem::ItemIsMovable, true); appleWidgetItem->setFlag(QGraphicsItem::ItemIsSelectable, true); appleWidgetItem->resize(foreignObjectWidth, foreignObjectHeight); @@ -885,7 +903,6 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() if (w3cWidgetItem) { - // w3cWidgetItem->setFlag(QGraphicsItem::ItemIsMovable, true); w3cWidgetItem->setFlag(QGraphicsItem::ItemIsSelectable, true); w3cWidgetItem->resize(foreignObjectWidth, foreignObjectHeight); @@ -956,7 +973,6 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() currentWidget->setDatastoreEntry(key, value); } else if (mXmlReader.name() == tGroups) { //considering groups section at the end of the document - readGroupRoot(); } else @@ -968,10 +984,9 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() { if (mXmlReader.name() == "g") { - if(strokesGroup && mScene){ - mScene->addItem(strokesGroup); - //graphicsItemFromSvg(strokesGroup); - } +// if(strokesGroup && mScene){ +// mScene->addItem(strokesGroup); +// } if (annotationGroup) { @@ -985,11 +1000,19 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() } } + if (mXmlReader.hasError()) { qWarning() << "error parsing Sankore file " << mXmlReader.errorString(); } + qDebug() << "Number of detected strokes: " << mStrokesList.count(); + QMapIterator iterator(mStrokesList); + while (iterator.hasNext()) { + iterator.next(); + mScene->addItem(iterator.value()); + } + if (mScene) { mScene->setModified(false); } @@ -1008,7 +1031,7 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene() UBGraphicsGroupContainerItem* UBSvgSubsetAdaptor::UBSvgSubsetReader::readGroup() { UBGraphicsGroupContainerItem *group = new UBGraphicsGroupContainerItem(); - QMultiMap strokesGroupsContainer; +// QMultiMap strokesGroupsContainer; QList groupContainer; mXmlReader.readNext(); @@ -1017,10 +1040,9 @@ 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) @@ -1028,67 +1050,69 @@ 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); +// UBGraphicsPolygonItem *curPolygon = qgraphicsitem_cast(curItem); +// +// if (curPolygon && !groupId.isEmpty() && !itemId.isEmpty() && itemId != groupId) { +// strokesGroupsContainer.insert(groupId, curPolygon); +// } +// else {// item - 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 { + // 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 { 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); @@ -1112,16 +1136,19 @@ 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(); } } @@ -1135,7 +1162,9 @@ QGraphicsItem *UBSvgSubsetAdaptor::UBSvgSubsetReader::readElementFromGroup() result = mScene->itemForUuid(QUuid(uuid)); if(!result) - qDebug() << "uuid " << uuid; + result = mStrokesList.take(uuid.replace("}","").replace("{","")); + + Q_ASSERT(result); mXmlReader.skipCurrentElement(); mXmlReader.readNext(); @@ -1239,38 +1268,15 @@ 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? @@ -1554,14 +1560,16 @@ 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); @@ -1572,27 +1580,6 @@ 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/adaptors/UBSvgSubsetAdaptor.h b/src/adaptors/UBSvgSubsetAdaptor.h index 4b351d2a..0647e626 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.h +++ b/src/adaptors/UBSvgSubsetAdaptor.h @@ -50,6 +50,7 @@ class UBGraphicsTriangle; class UBGraphicsCache; class IDataStorage; class UBGraphicsGroupContainerItem; +class UBGraphicsStrokesGroup; class UBSvgSubsetAdaptor { @@ -172,6 +173,8 @@ class UBSvgSubsetAdaptor QString mNamespaceUri; UBGraphicsScene *mScene; + + QMap mStrokesList; }; class UBSvgSubsetWriter diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 5e4b3130..3c351b50 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);