From a60439302cb638ba20a5f6a8485076dd63feb5f4 Mon Sep 17 00:00:00 2001 From: Ivan Ilyin Date: Wed, 2 May 2012 12:00:52 +0200 Subject: [PATCH] Sankore smart button --- src/core/UB.h | 3 +- src/domain/UBGraphicsPolygonItem.cpp | 16 ++--- src/domain/UBGraphicsPolygonItem.h | 2 +- src/domain/UBGraphicsScene.cpp | 66 ++++++++++++++++++--- src/domain/UBGraphicsScene.h | 3 +- src/domain/ubgraphicsgroupcontaineritem.cpp | 9 +++ src/domain/ubgraphicsgroupcontaineritem.h | 8 +++ src/tools/UBGraphicsCompass.cpp | 1 + src/tools/UBGraphicsProtractor.cpp | 1 + src/tools/UBGraphicsProtractor.h | 2 +- src/tools/UBGraphicsRuler.cpp | 3 +- src/tools/UBGraphicsTriangle.cpp | 1 + 12 files changed, 95 insertions(+), 20 deletions(-) diff --git a/src/core/UB.h b/src/core/UB.h index 0ca8d2cd..86eaa5d9 100644 --- a/src/core/UB.h +++ b/src/core/UB.h @@ -123,7 +123,8 @@ struct UBGraphicsItemType StrokeItemType, TriangleItemType, MagnifierItemType, - cacheItemType + cacheItemType, + groupContainerType }; }; diff --git a/src/domain/UBGraphicsPolygonItem.cpp b/src/domain/UBGraphicsPolygonItem.cpp index f05dd1e9..1e5a8c52 100644 --- a/src/domain/UBGraphicsPolygonItem.cpp +++ b/src/domain/UBGraphicsPolygonItem.cpp @@ -184,18 +184,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 2934b66b..4ca98294 100644 --- a/src/domain/UBGraphicsPolygonItem.h +++ b/src/domain/UBGraphicsPolygonItem.h @@ -115,7 +115,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 1494ce85..c7fa1ff8 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -65,6 +65,9 @@ #include "core/memcheck.h" +const QString groupText = "Group items"; +const QString ungroupText = "Ungroup items"; + qreal UBZLayerController::errorNumber = -20000001.0; UBZLayerController::UBZLayerController(QGraphicsScene *scene) : @@ -291,9 +294,9 @@ UBGraphicsScene::UBGraphicsScene(UBDocumentProxy* parent) } connect(this, SIGNAL(selectionChanged()), this, SLOT(selectionChangedProcessing())); - connect(this, SIGNAL(selectionChanged()), this, SLOT(enableGroupingButton())); + connect(this, SIGNAL(selectionChanged()), this, SLOT(groupButtonProcessing())); - connect(UBApplication::mainWindow->actionGroupItems, SIGNAL(triggered()), this, SLOT(processGroupItems())); + connect(UBApplication::mainWindow->actionGroupItems, SIGNAL(triggered()), this, SLOT(groupButtonClicked())); } UBGraphicsScene::~UBGraphicsScene() @@ -312,15 +315,65 @@ 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::enableGroupingButton() +void UBGraphicsScene::groupButtonProcessing() { QAction *groupAction = UBApplication::mainWindow->actionGroupItems; + QList selItems = selectedItems(); + int selCount = selItems.count(); - if (selectedItems().count() > 1) { - groupAction->setEnabled(true); - } else { + if (selCount < 1) { groupAction->setEnabled(false); + groupAction->setText(groupText); + + } else if (selCount == 1) { + if (selItems.first()->type() == UBGraphicsGroupContainerItem::Type) { + groupAction->setEnabled(true); + groupAction->setText(ungroupText); + } else { + groupAction->setEnabled(false); + } + + } else if (selCount > 1) { + groupAction->setEnabled(true); + groupAction->setText(groupText); + } +} +void UBGraphicsScene::groupButtonClicked() +{ + QAction *groupAction = UBApplication::mainWindow->actionGroupItems; + QList selItems = selectedItems(); + if (!selItems.count()) { + qDebug() << "Got grouping request when there is no any selected item on the scene"; + return; } + + if (groupAction->text() == groupText) { //The only way to get information from item, considering using smth else + 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); + } + + addItem(groupItem); + groupItem->setVisible(true); + groupItem->setFocus(); + + } else if (groupAction->text() == ungroupText) { + //Considering one selected item and it's a group + if (selItems.count() > 1) { + qDebug() << "can't make sense of ungrouping more then one item. Grouping action should be performed for that purpose"; + return; + } + UBGraphicsGroupContainerItem *currentGroup = dynamic_cast(selItems.first()); + if (currentGroup) { + currentGroup->destroy(); + } + } + } void UBGraphicsScene::processGroupItems() { @@ -432,7 +485,6 @@ bool UBGraphicsScene::inputDevicePress(const QPointF& scenePos, const qreal& pre return accepted; } - bool UBGraphicsScene::inputDeviceMove(const QPointF& scenePos, const qreal& pressure) { bool accepted = false; diff --git a/src/domain/UBGraphicsScene.h b/src/domain/UBGraphicsScene.h index 4b9d946f..4e531204 100644 --- a/src/domain/UBGraphicsScene.h +++ b/src/domain/UBGraphicsScene.h @@ -305,7 +305,8 @@ public slots: void setToolCursor(int tool); void selectionChangedProcessing(); - void enableGroupingButton(); + void groupButtonProcessing(); + void groupButtonClicked(); void processGroupItems(); void moveMagnifier(QPoint newPos); diff --git a/src/domain/ubgraphicsgroupcontaineritem.cpp b/src/domain/ubgraphicsgroupcontaineritem.cpp index 0a706099..3099e288 100644 --- a/src/domain/ubgraphicsgroupcontaineritem.cpp +++ b/src/domain/ubgraphicsgroupcontaineritem.cpp @@ -53,6 +53,15 @@ void UBGraphicsGroupContainerItem::remove() mDelegate->remove(); } +void UBGraphicsGroupContainerItem::destroy() { + + foreach (QGraphicsItem *item, childItems()) { + removeFromGroup(item); + item->setFlag(QGraphicsItem::ItemIsSelectable, true); + } + + mDelegate->remove(true); +} void UBGraphicsGroupContainerItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { diff --git a/src/domain/ubgraphicsgroupcontaineritem.h b/src/domain/ubgraphicsgroupcontaineritem.h index 193a3ca7..87cc38ab 100644 --- a/src/domain/ubgraphicsgroupcontaineritem.h +++ b/src/domain/ubgraphicsgroupcontaineritem.h @@ -15,6 +15,14 @@ public: virtual UBGraphicsScene* scene(); virtual UBGraphicsGroupContainerItem *deepCopy() const; virtual void remove(); + enum { Type = UBGraphicsItemType::groupContainerType }; + + virtual int type() const + { + return Type; + } + + void destroy(); protected: diff --git a/src/tools/UBGraphicsCompass.cpp b/src/tools/UBGraphicsCompass.cpp index f2bc45df..c0386d1a 100644 --- a/src/tools/UBGraphicsCompass.cpp +++ b/src/tools/UBGraphicsCompass.cpp @@ -71,6 +71,7 @@ UBGraphicsCompass::UBGraphicsCompass() unsetCursor(); setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::CppTool)); //Necessary to set if we want z value to be assigned correctly + setFlag(QGraphicsItem::ItemIsSelectable, false); connect(UBApplication::boardController, SIGNAL(penColorChanged()), this, SLOT(penColorChanged())); connect(UBDrawingController::drawingController(), SIGNAL(lineWidthIndexChanged(int)), this, SLOT(lineWidthChanged())); diff --git a/src/tools/UBGraphicsProtractor.cpp b/src/tools/UBGraphicsProtractor.cpp index e73f0d02..f00bf275 100644 --- a/src/tools/UBGraphicsProtractor.cpp +++ b/src/tools/UBGraphicsProtractor.cpp @@ -64,6 +64,7 @@ UBGraphicsProtractor::UBGraphicsProtractor() mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::CppTool)); //Necessary to set if we want z value to be assigned correctly + setFlag(QGraphicsItem::ItemIsSelectable, false); scale(1.5, 1.5); } diff --git a/src/tools/UBGraphicsProtractor.h b/src/tools/UBGraphicsProtractor.h index 0d704126..7eb79963 100644 --- a/src/tools/UBGraphicsProtractor.h +++ b/src/tools/UBGraphicsProtractor.h @@ -28,7 +28,7 @@ class UBGraphicsScene; class UBGraphicsProtractor : public UBAbstractDrawRuler, public QGraphicsEllipseItem, public UBItem { - Q_OBJECT; + Q_OBJECT public: UBGraphicsProtractor (); diff --git a/src/tools/UBGraphicsRuler.cpp b/src/tools/UBGraphicsRuler.cpp index 27bcc713..720b7b22 100644 --- a/src/tools/UBGraphicsRuler.cpp +++ b/src/tools/UBGraphicsRuler.cpp @@ -46,7 +46,8 @@ UBGraphicsRuler::UBGraphicsRuler() setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::CppTool)); //Necessary to set if we want z value to be assigned correctly - updateResizeCursor(); + setFlag(QGraphicsItem::ItemIsSelectable, false); + updateResizeCursor(); } void UBGraphicsRuler::updateResizeCursor() diff --git a/src/tools/UBGraphicsTriangle.cpp b/src/tools/UBGraphicsTriangle.cpp index bd4156e0..ef946af7 100644 --- a/src/tools/UBGraphicsTriangle.cpp +++ b/src/tools/UBGraphicsTriangle.cpp @@ -55,6 +55,7 @@ UBGraphicsTriangle::UBGraphicsTriangle() mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::CppTool)); //Necessary to set if we want z value to be assigned correctly + setFlag(QGraphicsItem::ItemIsSelectable, false); updateResizeCursor(); }