From fa9c808b3fee3454f29fe982403e3765f75132d5 Mon Sep 17 00:00:00 2001 From: thomas_lucky13 Date: Sun, 17 Jul 2022 15:08:12 +0400 Subject: [PATCH] LineItem Delegate --- src/board/UBBoardView.cpp | 4 +++- src/domain/UBGraphicsLineItem.cpp | 32 ++++++++++++++++++++++++++++++- src/domain/UBGraphicsLineItem.h | 6 ++++-- src/domain/UBGraphicsScene.cpp | 4 ++++ src/domain/UBItem.cpp | 3 +++ 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/board/UBBoardView.cpp b/src/board/UBBoardView.cpp index 97c8e2af..4142c119 100644 --- a/src/board/UBBoardView.cpp +++ b/src/board/UBBoardView.cpp @@ -67,6 +67,7 @@ #include "domain/UBGraphicsWidgetItem.h" #include "domain/UBGraphicsPDFItem.h" #include "domain/UBGraphicsPolygonItem.h" +#include "domain/UBGraphicsLineItem.h" #include "domain/UBItem.h" #include "domain/UBGraphicsMediaItem.h" #include "domain/UBGraphicsSvgItem.h" @@ -1210,7 +1211,8 @@ void UBBoardView::mouseMoveEvent (QMouseEvent *event) || item->type() == UBGraphicsSvgItem::Type || item->type() == UBGraphicsTextItem::Type || item->type() == UBGraphicsStrokesGroup::Type - || item->type() == UBGraphicsGroupContainerItem::Type) { + || item->type() == UBGraphicsGroupContainerItem::Type + || item->type() == UBGraphicsLineItem::Type) { if (!mJustSelectedItems.contains(item)) { diff --git a/src/domain/UBGraphicsLineItem.cpp b/src/domain/UBGraphicsLineItem.cpp index 881bcb18..1eb3e8ad 100644 --- a/src/domain/UBGraphicsLineItem.cpp +++ b/src/domain/UBGraphicsLineItem.cpp @@ -3,7 +3,6 @@ #include "frameworks/UBGeometryUtils.h" #include "UBGraphicsScene.h" #include "domain/UBGraphicsLineStroke.h" - #include "core/memcheck.h" @@ -59,7 +58,15 @@ UBGraphicsLineItem::UBGraphicsLineItem (const QLineF& pLine, qreal pStartWidth, void UBGraphicsLineItem::initialize() { setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::DrawingItem)); //Necessary to set if we want z value to be assigned correctly + setDelegate(new UBGraphicsItemDelegate(this, 0, GF_COMMON + | GF_RESPECT_RATIO + | GF_REVOLVABLE + | GF_FLIPPABLE_ALL_AXIS)); setUuid(QUuid::createUuid()); + setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::ObjectItem)); //Necessary to set if we want z value to be assigned correctly + setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); + setFlag(QGraphicsItem::ItemIsSelectable, true); + setFlag(QGraphicsItem::ItemIsMovable, true); } void UBGraphicsLineItem::setUuid(const QUuid &pUuid) @@ -175,28 +182,51 @@ void UBGraphicsLineItem::copyItemParameters(UBItem *copy) const cp->mIsNominalLine = this->mIsNominalLine; cp->setTransform(transform()); + cp->setPos(pos()); cp->setPen(this->pen()); cp->mHasAlpha = this->mHasAlpha; cp->setColorOnDarkBackground(this->colorOnDarkBackground()); cp->setColorOnLightBackground(this->colorOnLightBackground()); + cp->setFlag(QGraphicsItem::ItemIsMovable, true); + cp->setFlag(QGraphicsItem::ItemIsSelectable, true); cp->setZValue(this->zValue()); cp->setData(UBGraphicsItemData::ItemLayerType, this->data(UBGraphicsItemData::ItemLayerType)); + cp->setData(UBGraphicsItemData::ItemLocked, this->data(UBGraphicsItemData::ItemLocked)); } } void UBGraphicsLineItem::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) { + QStyleOptionGraphicsItem styleOption = QStyleOptionGraphicsItem(*option); if(mHasAlpha && scene() && scene()->isLightBackground()) painter->setCompositionMode(QPainter::CompositionMode_SourceOver); painter->setRenderHints(QPainter::Antialiasing); QGraphicsLineItem::paint(painter, option, widget); + Delegate()->postpaint(painter, &styleOption, widget); } UBGraphicsScene* UBGraphicsLineItem::scene() { return qobject_cast(QGraphicsLineItem::scene()); } + +void UBGraphicsLineItem::SetDelegate() +{ + Delegate()->createControls(); +} + +QVariant UBGraphicsLineItem::itemChange(GraphicsItemChange change, const QVariant &value) +{ + QVariant newValue = Delegate()->itemChange(change, value); + UBGraphicsItem *item = dynamic_cast(this); + if (item) + { + item->Delegate()->positionHandles(); + } + + return QGraphicsItem::itemChange(change, newValue); +} diff --git a/src/domain/UBGraphicsLineItem.h b/src/domain/UBGraphicsLineItem.h index 535ef143..0e3b4a5f 100644 --- a/src/domain/UBGraphicsLineItem.h +++ b/src/domain/UBGraphicsLineItem.h @@ -11,7 +11,7 @@ class UBItem; class UBGraphicsScene; class UBGraphicsLineStroke; -class UBGraphicsLineItem : public QGraphicsLineItem, public UBItem +class UBGraphicsLineItem : public QGraphicsLineItem, public UBItem, public UBGraphicsItem { public: @@ -109,9 +109,11 @@ class UBGraphicsLineItem : public QGraphicsLineItem, public UBItem void setStroke(UBGraphicsLineStroke* stroke); UBGraphicsLineStroke* stroke() const; + void SetDelegate(); + protected: void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget); - + virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value); private: diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index ac3cbfdc..5b7193bb 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -1020,6 +1020,10 @@ void UBGraphicsScene::addPolygonItemToCurrentStroke(UBGraphicsPolygonItem* polyg void UBGraphicsScene::addLineItemToCurrentStroke(UBGraphicsLineItem* lineItem) { + lineItem->setFlag(QGraphicsItem::ItemIsMovable, true); + lineItem->setFlag(QGraphicsItem::ItemIsSelectable, true); + lineItem->SetDelegate(); + mpLastLine = lineItem; mAddedItems.insert(lineItem); diff --git a/src/domain/UBItem.cpp b/src/domain/UBItem.cpp index 4ebbbeec..14348d4f 100644 --- a/src/domain/UBItem.cpp +++ b/src/domain/UBItem.cpp @@ -41,6 +41,7 @@ #include "domain/UBGraphicsScene.h" #include "tools/UBGraphicsCurtainItem.h" #include "domain/UBGraphicsItemDelegate.h" +#include "domain/UBGraphicsLineItem.h" UBItem::UBItem() : mUuid(QUuid()) @@ -135,6 +136,8 @@ UBGraphicsItemDelegate *UBGraphicsItem::Delegate(QGraphicsItem *pItem) case UBGraphicsCurtainItem::Type : result = (static_cast(pItem))->Delegate(); break; + case UBGraphicsLineItem::Type : + result = (static_cast(pItem))->Delegate(); } return result;