diff --git a/src/adaptors/UBSvgSubsetAdaptor.cpp b/src/adaptors/UBSvgSubsetAdaptor.cpp index e300bb3a..417e835d 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.cpp +++ b/src/adaptors/UBSvgSubsetAdaptor.cpp @@ -30,6 +30,7 @@ #include "domain/UBAbstractWidget.h" #include "domain/UBGraphicsStroke.h" #include "domain/UBGraphicsStrokesGroup.h" +#include "domain/ubgraphicsgroupcontaineritem.h" #include "domain/UBItem.h" #include "tools/UBGraphicsRuler.h" @@ -929,8 +930,13 @@ void UBSvgSubsetAdaptor::UBSvgSubsetWriter::writeSvgElement() bool UBSvgSubsetAdaptor::UBSvgSubsetWriter::persistScene() { + + if (mScene->isModified()) { + static int i = 0; + qDebug() << "persist call no is " << ++i; + QBuffer buffer; buffer.open(QBuffer::WriteOnly); mXmlWriter.setDevice(&buffer); @@ -1035,146 +1041,162 @@ bool UBSvgSubsetAdaptor::UBSvgSubsetWriter::persistScene() openStroke = 0; } - UBGraphicsPixmapItem *pixmapItem = qgraphicsitem_cast (item); + UBGraphicsGroupContainerItem *groupItem = qgraphicsitem_cast(item); - if (pixmapItem && pixmapItem->isVisible()) + if (groupItem && groupItem->isVisible()) { - pixmapItemToLinkedImage(pixmapItem); + qDebug() << "came across the group during the parsing"; continue; } - UBGraphicsSvgItem *svgItem = qgraphicsitem_cast (item); + parseCommonItems(item); + } - if (svgItem && svgItem->isVisible()) - { - svgItemToLinkedSvg(svgItem); - continue; - } + if (openStroke) + { + mXmlWriter.writeEndElement(); + groupHoldsInfo = false; + openStroke = 0; + } - UBGraphicsVideoItem *videoItem = qgraphicsitem_cast (item); + mXmlWriter.writeEndDocument(); + QString fileName = mDocumentPath + UBFileSystemUtils::digitFileFormat("/page%1.svg", mPageIndex + 1); + QFile file(fileName); - if (videoItem && videoItem->isVisible()) - { - videoItemToLinkedVideo(videoItem); - continue; - } + if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) + { + qCritical() << "cannot open " << fileName << " for writing ..."; + return false; + } + file.write(buffer.data()); + file.flush(); + file.close(); - UBGraphicsAudioItem* audioItem = qgraphicsitem_cast (item); - if (audioItem && audioItem->isVisible()) { - audioItemToLinkedAudio(audioItem); - continue; - } + } + else + { + qDebug() << "ignoring unmodified page" << mPageIndex + 1; + } - UBGraphicsAppleWidgetItem *appleWidgetItem = qgraphicsitem_cast (item); + return true; +} - if (appleWidgetItem && appleWidgetItem->isVisible()) - { - graphicsAppleWidgetToSvg(appleWidgetItem); - continue; - } +bool UBSvgSubsetAdaptor::UBSvgSubsetWriter::parseCommonItems(QGraphicsItem *item) +{ + UBGraphicsPixmapItem *pixmapItem = qgraphicsitem_cast (item); - UBGraphicsW3CWidgetItem *w3cWidgetItem = qgraphicsitem_cast (item); + if (pixmapItem && pixmapItem->isVisible()) + { + pixmapItemToLinkedImage(pixmapItem); + return true; + } - if (w3cWidgetItem && w3cWidgetItem->isVisible()) - { - graphicsW3CWidgetToSvg(w3cWidgetItem); - continue; - } + UBGraphicsSvgItem *svgItem = qgraphicsitem_cast (item); - UBGraphicsPDFItem *pdfItem = qgraphicsitem_cast (item); + if (svgItem && svgItem->isVisible()) + { + svgItemToLinkedSvg(svgItem); + return true; + } - if (pdfItem && pdfItem->isVisible()) - { - pdfItemToLinkedPDF(pdfItem); - continue; - } + UBGraphicsVideoItem *videoItem = qgraphicsitem_cast (item); - UBGraphicsTextItem *textItem = qgraphicsitem_cast (item); + if (videoItem && videoItem->isVisible()) + { + videoItemToLinkedVideo(videoItem); + return true; + } - if (textItem && textItem->isVisible()) - { - textItemToSvg(textItem); - continue; - } + UBGraphicsAudioItem* audioItem = qgraphicsitem_cast (item); + if (audioItem && audioItem->isVisible()) { + audioItemToLinkedAudio(audioItem); + return true; + } - UBGraphicsCurtainItem *curtainItem = qgraphicsitem_cast (item); + UBGraphicsAppleWidgetItem *appleWidgetItem = qgraphicsitem_cast (item); - if (curtainItem && curtainItem->isVisible()) - { - curtainItemToSvg(curtainItem); - continue; - } + if (appleWidgetItem && appleWidgetItem->isVisible()) + { + graphicsAppleWidgetToSvg(appleWidgetItem); + return true; + } - UBGraphicsRuler *ruler = qgraphicsitem_cast (item); + UBGraphicsW3CWidgetItem *w3cWidgetItem = qgraphicsitem_cast (item); - if (ruler && ruler->isVisible()) - { - rulerToSvg(ruler); - continue; - } + if (w3cWidgetItem && w3cWidgetItem->isVisible()) + { + graphicsW3CWidgetToSvg(w3cWidgetItem); + return true; + } - UBGraphicsCache* cache = qgraphicsitem_cast(item); - if(cache && cache->isVisible()) - { - cacheToSvg(cache); - continue; - } + UBGraphicsPDFItem *pdfItem = qgraphicsitem_cast (item); - UBGraphicsCompass *compass = qgraphicsitem_cast (item); + if (pdfItem && pdfItem->isVisible()) + { + pdfItemToLinkedPDF(pdfItem); + return true; + } - if (compass && compass->isVisible()) - { - compassToSvg(compass); - continue; - } + UBGraphicsTextItem *textItem = qgraphicsitem_cast (item); - UBGraphicsProtractor *protractor = qgraphicsitem_cast (item); + if (textItem && textItem->isVisible()) + { + textItemToSvg(textItem); + return true; + } - if (protractor && protractor->isVisible()) - { - protractorToSvg(protractor); - continue; - } + UBGraphicsCurtainItem *curtainItem = qgraphicsitem_cast (item); - UBGraphicsTriangle *triangle = qgraphicsitem_cast (item); + if (curtainItem && curtainItem->isVisible()) + { + curtainItemToSvg(curtainItem); + return true; + } - if (triangle && triangle->isVisible()) - { - triangleToSvg(triangle); - continue; - } - } + UBGraphicsRuler *ruler = qgraphicsitem_cast (item); - if (openStroke) - { - mXmlWriter.writeEndElement(); - groupHoldsInfo = false; - openStroke = 0; - } + if (ruler && ruler->isVisible()) + { + rulerToSvg(ruler); + return true; + } - mXmlWriter.writeEndDocument(); - QString fileName = mDocumentPath + UBFileSystemUtils::digitFileFormat("/page%1.svg", mPageIndex + 1); - QFile file(fileName); + UBGraphicsCache* cache = qgraphicsitem_cast(item); + if(cache && cache->isVisible()) + { + cacheToSvg(cache); + return true; + } - if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) - { - qCritical() << "cannot open " << fileName << " for writing ..."; - return false; - } - file.write(buffer.data()); - file.flush(); - file.close(); + UBGraphicsCompass *compass = qgraphicsitem_cast (item); + if (compass && compass->isVisible()) + { + compassToSvg(compass); + return true; } - else + + UBGraphicsProtractor *protractor = qgraphicsitem_cast (item); + + if (protractor && protractor->isVisible()) { - qDebug() << "ignoring unmodified page" << mPageIndex + 1; + protractorToSvg(protractor); + return true; + } + + UBGraphicsTriangle *triangle = qgraphicsitem_cast (item); + + if (triangle && triangle->isVisible()) + { + triangleToSvg(triangle); + return true; } return true; } + void UBSvgSubsetAdaptor::UBSvgSubsetWriter::polygonItemToSvgLine(UBGraphicsPolygonItem* polygonItem, bool groupHoldsInfo) { mXmlWriter.writeStartElement("line"); diff --git a/src/adaptors/UBSvgSubsetAdaptor.h b/src/adaptors/UBSvgSubsetAdaptor.h index 188bba2f..6514cf20 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.h +++ b/src/adaptors/UBSvgSubsetAdaptor.h @@ -156,6 +156,7 @@ class UBSvgSubsetAdaptor UBSvgSubsetWriter(UBDocumentProxy* proxy, UBGraphicsScene* pScene, const int pageIndex); bool persistScene(); + bool parseCommonItems(QGraphicsItem *item); virtual ~UBSvgSubsetWriter(){} diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index d3faba2b..512c4446 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -1235,10 +1235,12 @@ void UBBoardController::ClearUndoStack() while (itUniq.hasNext()) { QGraphicsItem* item = itUniq.next(); - UBGraphicsScene *scene = dynamic_cast(item->scene()); - if(!scene) - { - mActiveScene->deleteItem(item); + if (item->scene()) { + UBGraphicsScene *scene = dynamic_cast(item->scene()); + if(!scene) + { + mActiveScene->deleteItem(item); + } } } diff --git a/src/domain/UBGraphicsItemDelegate.cpp b/src/domain/UBGraphicsItemDelegate.cpp index db37d684..ad352753 100644 --- a/src/domain/UBGraphicsItemDelegate.cpp +++ b/src/domain/UBGraphicsItemDelegate.cpp @@ -236,27 +236,25 @@ void UBGraphicsItemDelegate::setMimeData(QMimeData *mimeData) bool UBGraphicsItemDelegate::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - if((NULL != mMimeData) && ((event->pos() - mDragStartPosition).manhattanLength() < QApplication::startDragDistance())) - { - QDrag* mDrag = new QDrag(event->widget()); - mDrag->setMimeData(mMimeData); - if (!mDragPixmap.isNull()) { - mDrag->setPixmap(mDragPixmap); - mDrag->setHotSpot(mDragPixmap.rect().center()); - } - mDrag->exec(); - mDragPixmap = QPixmap(); - - return true; + if(mMimeData) { + QDrag* mDrag = new QDrag(event->widget()); + mDrag->setMimeData(mMimeData); + if (!mDragPixmap.isNull()) { + mDrag->setPixmap(mDragPixmap); + mDrag->setHotSpot(mDragPixmap.rect().center()); } + mDrag->exec(); + mDragPixmap = QPixmap(); - if(isLocked()) - { - event->accept(); return true; } - return true; + if(isLocked()) { + event->accept(); + return true; + } else { + return false; + } } bool UBGraphicsItemDelegate::weelEvent(QGraphicsSceneWheelEvent *event) diff --git a/src/domain/UBGraphicsPolygonItem.cpp b/src/domain/UBGraphicsPolygonItem.cpp index 1fc87f6a..9c6dee10 100644 --- a/src/domain/UBGraphicsPolygonItem.cpp +++ b/src/domain/UBGraphicsPolygonItem.cpp @@ -189,18 +189,18 @@ void UBGraphicsPolygonItem::paint ( QPainter * painter, const QStyleOptionGraphi QGraphicsPolygonItem::paint(painter, option, widget); } -//QPainterPath UBGraphicsPolygonItem::shape() const -//{ +QPainterPath UBGraphicsPolygonItem::shape() const +{ -// QPainterPath path; -// path.addRect(boundingRect()); + QPainterPath path; + path.addRect(boundingRect()); -// return path; + return path; -//// static QPainterPath shapePath = QGraphicsPolygonItem::shape(); +// static QPainterPath shapePath = QGraphicsPolygonItem::shape(); -//// return shapePath; -//} +// return shapePath; +} UBGraphicsScene* UBGraphicsPolygonItem::scene() diff --git a/src/domain/UBGraphicsPolygonItem.h b/src/domain/UBGraphicsPolygonItem.h index 663658f2..851092b2 100644 --- a/src/domain/UBGraphicsPolygonItem.h +++ b/src/domain/UBGraphicsPolygonItem.h @@ -118,7 +118,7 @@ class UBGraphicsPolygonItem : public QGraphicsPolygonItem, public UBItem protected: void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget); -// QPainterPath shape () const; + QPainterPath shape () const; private: diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 0b19870c..da622bda 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -294,9 +294,15 @@ UBGraphicsScene::UBGraphicsScene(UBDocumentProxy* parent) } connect(this, SIGNAL(selectionChanged()), this, SLOT(selectionChangedProcessing())); - connect(this, SIGNAL(selectionChanged()), this, SLOT(groupButtonProcessing())); + connect(this, SIGNAL(selectionChanged()), this, SLOT(updateGroupButtonState())); - connect(UBApplication::mainWindow->actionGroupItems, SIGNAL(triggered()), this, SLOT(groupButtonClicked())); +// just a stub don't treat as a result code +// static int i = 0; +// i++; +// if (i == 1) { + connect(UBApplication::mainWindow->actionGroupItems, SIGNAL(triggered()), this, SLOT(groupButtonClicked())); +// qDebug() << "the connect is accepted"; +// } } UBGraphicsScene::~UBGraphicsScene() @@ -315,7 +321,7 @@ void UBGraphicsScene::selectionChangedProcessing() UBApplication::showMessage("ZValue is " + QString::number(selectedItems().first()->zValue(), 'f') + "own z value is " + QString::number(selectedItems().first()->data(UBGraphicsItemData::ItemOwnZValue).toReal(), 'f')); } -void UBGraphicsScene::groupButtonProcessing() +void UBGraphicsScene::updateGroupButtonState() { QAction *groupAction = UBApplication::mainWindow->actionGroupItems; QList selItems = selectedItems(); @@ -351,11 +357,18 @@ void UBGraphicsScene::groupButtonClicked() UBGraphicsGroupContainerItem *groupItem = new UBGraphicsGroupContainerItem(); foreach (QGraphicsItem *item, selItems) { - item->setSelected(false); - item->setFlag(QGraphicsItem::ItemIsSelectable, false); - item->setFlag( QGraphicsItem::ItemIsMovable, false); - item->setFlag(QGraphicsItem::ItemIsFocusable); - groupItem->addToGroup(item); + if (item->type() == UBGraphicsGroupContainerItem::Type) { + QList childItems = item->childItems(); + UBGraphicsGroupContainerItem *currentGroup = dynamic_cast(item); + if (currentGroup) { + currentGroup->destroy(); + } + foreach (QGraphicsItem *chItem, childItems) { + groupItem->addToGroup(chItem); + } + } else { + groupItem->addToGroup(item); + } } addItem(groupItem); @@ -375,26 +388,6 @@ void UBGraphicsScene::groupButtonClicked() } } -void UBGraphicsScene::processGroupItems() -{ - qDebug() << "processing grouping items"; - - UBGraphicsGroupContainerItem *groupItem = new UBGraphicsGroupContainerItem(); - - foreach (QGraphicsItem *item, selectedItems()) { - item->setSelected(false); - item->setFlag(QGraphicsItem::ItemIsSelectable, true); - item->setFlag( QGraphicsItem::ItemIsMovable, false); - item->setFlag(QGraphicsItem::ItemIsFocusable, true); - groupItem->addToGroup(item); - } - - addItem(groupItem); -// groupItem->setPos(50, 50); - groupItem->setVisible(true); - groupItem->setFocus(); - qDebug() << groupItem->boundingRect(); -} // MARK: - // MARK: Mouse/Tablet events handling diff --git a/src/domain/UBGraphicsScene.h b/src/domain/UBGraphicsScene.h index 4e531204..c26c76a1 100644 --- a/src/domain/UBGraphicsScene.h +++ b/src/domain/UBGraphicsScene.h @@ -305,9 +305,8 @@ public slots: void setToolCursor(int tool); void selectionChangedProcessing(); - void groupButtonProcessing(); + void updateGroupButtonState(); void groupButtonClicked(); - void processGroupItems(); void moveMagnifier(QPoint newPos); void closeMagnifier(); diff --git a/src/domain/ubgraphicsgroupcontaineritem.cpp b/src/domain/ubgraphicsgroupcontaineritem.cpp index 41cc19d6..64f68eee 100644 --- a/src/domain/ubgraphicsgroupcontaineritem.cpp +++ b/src/domain/ubgraphicsgroupcontaineritem.cpp @@ -46,6 +46,12 @@ void UBGraphicsGroupContainerItem::addToGroup(QGraphicsItem *item) return; } + //setting item flags to given item + item->setSelected(false); + item->setFlag(QGraphicsItem::ItemIsSelectable, false); + item->setFlag( QGraphicsItem::ItemIsMovable, false); + item->setFlag(QGraphicsItem::ItemIsFocusable, true); + QTransform newItemTransform(itemTransform); item->setPos(mapFromItem(item, 0, 0)); item->setParentItem(this); @@ -130,20 +136,24 @@ void UBGraphicsGroupContainerItem::paint(QPainter *painter, const QStyleOptionGr QWidget *widget) { Q_UNUSED(widget); - if (option->state & QStyle::State_Selected) { - painter->setBrush(Qt::NoBrush); - QPen tmpPen; - qreal tmpPenWidth = 1.0; - tmpPen.setWidth(tmpPenWidth); - tmpPen.setColor(Qt::lightGray); - painter->setPen(tmpPen); - painter->drawRect(itemsBoundingRect.adjusted(tmpPenWidth / 2, tmpPenWidth / 2, -tmpPenWidth / 2, -tmpPenWidth / 2)); - } + Q_UNUSED(painter); + Q_UNUSED(option); + +// we would not use paint smth for the moment +// if (option->state & QStyle::State_Selected) { +// painter->setBrush(Qt::NoBrush); +// QPen tmpPen; +// qreal tmpPenWidth = 1.0; +// tmpPen.setWidth(tmpPenWidth); +// tmpPen.setColor(Qt::lightGray); +// painter->setPen(tmpPen); +// painter->drawRect(itemsBoundingRect.adjusted(tmpPenWidth / 2, tmpPenWidth / 2, -tmpPenWidth / 2, -tmpPenWidth / 2)); +// } } UBGraphicsScene *UBGraphicsGroupContainerItem::scene() { - UBGraphicsScene *castScene = dynamic_cast(scene()); + UBGraphicsScene *castScene = dynamic_cast(QGraphicsItem::scene()); return castScene; } @@ -178,7 +188,12 @@ void UBGraphicsGroupContainerItem::destroy() { item->setFlag(QGraphicsItem::ItemIsFocusable, true); } - mDelegate->remove(true); + remove(); + + if (scene()) { + qDebug() << "scene is well casted"; + scene()->removeItem(this); + } } void UBGraphicsGroupContainerItem::mousePressEvent(QGraphicsSceneMouseEvent *event) @@ -186,9 +201,12 @@ void UBGraphicsGroupContainerItem::mousePressEvent(QGraphicsSceneMouseEvent *eve if (mDelegate->mousePressEvent(event)) { //NOOP } else { - QGraphicsItem::mousePressEvent(event); + + QGraphicsItem::mousePressEvent(event); setSelected(true); } + + } void UBGraphicsGroupContainerItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) @@ -198,11 +216,12 @@ void UBGraphicsGroupContainerItem::mouseMoveEvent(QGraphicsSceneMouseEvent *even } else { QGraphicsItem::mouseMoveEvent(event); } + } void UBGraphicsGroupContainerItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - mDelegate->mouseReleaseEvent(event); +// mDelegate->mouseReleaseEvent(event); QGraphicsItem::mouseReleaseEvent(event); } diff --git a/src/domain/ubgraphicsgroupcontaineritemdelegate.cpp b/src/domain/ubgraphicsgroupcontaineritemdelegate.cpp index 3fcf7b3f..db50b095 100644 --- a/src/domain/ubgraphicsgroupcontaineritemdelegate.cpp +++ b/src/domain/ubgraphicsgroupcontaineritemdelegate.cpp @@ -46,3 +46,24 @@ void UBGraphicsGroupContainerItemDelegate::buildButtons() { UBGraphicsItemDelegate::buildButtons(); } + +bool UBGraphicsGroupContainerItemDelegate::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + Q_UNUSED(event) + + return false; +} + +bool UBGraphicsGroupContainerItemDelegate::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + Q_UNUSED(event) + + return false; +} + +bool UBGraphicsGroupContainerItemDelegate::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + Q_UNUSED(event) + + return false; +} diff --git a/src/domain/ubgraphicsgroupcontaineritemdelegate.h b/src/domain/ubgraphicsgroupcontaineritemdelegate.h index 56a525f8..2396b10c 100644 --- a/src/domain/ubgraphicsgroupcontaineritemdelegate.h +++ b/src/domain/ubgraphicsgroupcontaineritemdelegate.h @@ -17,6 +17,10 @@ protected: virtual void decorateMenu(QMenu *menu); virtual void buildButtons(); + virtual bool mousePressEvent(QGraphicsSceneMouseEvent *event); + virtual bool mouseMoveEvent(QGraphicsSceneMouseEvent *event); + virtual bool mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + private: DelegateButton *mDestroyGroupButton; };