From b0f187b6c2e039b45412ac7261297247787ea3a1 Mon Sep 17 00:00:00 2001 From: thomas_lucky13 Date: Tue, 2 Aug 2022 21:01:40 +0400 Subject: [PATCH] UBGraphicsVectorItem --- src/core/UB.h | 1 + src/domain/UBGraphicsScene.cpp | 50 ++++++- src/domain/UBGraphicsScene.h | 6 + src/domain/UBGraphicsVectorItem.cpp | 206 ++++++++++++++++++++++++++++ src/domain/UBGraphicsVectorItem.h | 107 +++++++++++++++ src/domain/domain.pri | 2 + 6 files changed, 370 insertions(+), 2 deletions(-) create mode 100644 src/domain/UBGraphicsVectorItem.cpp create mode 100644 src/domain/UBGraphicsVectorItem.h diff --git a/src/core/UB.h b/src/core/UB.h index 156aa1aa..0a0b6ecf 100644 --- a/src/core/UB.h +++ b/src/core/UB.h @@ -174,6 +174,7 @@ struct UBGraphicsItemType GraphicsWidgetItemType, //65556 UserTypesCount, //65557 AxesItemType, //65558 + VectorItemType, SelectionFrameType // this line must be the last line in this enum because it is types counter. }; }; diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 74efe918..4b9350e7 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -70,6 +70,7 @@ #include "UBGraphicsPixmapItem.h" #include "UBGraphicsSvgItem.h" #include "UBGraphicsPolygonItem.h" +#include "UBGraphicsVectorItem.h" #include "UBGraphicsMediaItem.h" #include "UBGraphicsWidgetItem.h" #include "UBGraphicsPDFItem.h" @@ -541,6 +542,7 @@ bool UBGraphicsScene::inputDeviceMove(const QPointF& scenePos, const qreal& pres if (currentTool == UBStylusTool::Line || dc->mActiveRuler || currentTool == UBStylusTool::Vector) { + if (UBDrawingController::drawingController()->stylusTool() != UBStylusTool::Vector) if (UBDrawingController::drawingController()->stylusTool() != UBStylusTool::Marker) if(NULL != mpLastPolygon && NULL != mCurrentStroke && mAddedItems.size() > 0){ UBCoreGraphicsScene::removeItemFromDeletion(mpLastPolygon); @@ -922,6 +924,7 @@ void UBGraphicsScene::moveTo(const QPointF &pPoint) } void UBGraphicsScene::drawLineTo(const QPointF &pEndPoint, const qreal &pWidth, bool bLineStyle) { + if(UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Vector) bLineStyle = true; drawLineTo(pEndPoint, pWidth, pWidth, bLineStyle); } @@ -945,8 +948,16 @@ void UBGraphicsScene::drawLineTo(const QPointF &pEndPoint, const qreal &startWid mAddedItems.clear(); } - UBGraphicsPolygonItem *polygonItem = lineToPolygonItem(QLineF(mPreviousPoint, pEndPoint), initialWidth, endWidth); - addPolygonItemToCurrentStroke(polygonItem); + if (UBDrawingController::drawingController()->stylusTool() != UBStylusTool::Vector) + { + UBGraphicsPolygonItem *polygonItem = lineToPolygonItem(QLineF(mPreviousPoint, pEndPoint), initialWidth, endWidth); + addPolygonItemToCurrentStroke(polygonItem); + } else + { + UBGraphicsVectorItem *vectorItem = new UBGraphicsVectorItem(QLineF(mPreviousPoint, pEndPoint), initialWidth, endWidth); + initVectorItem(vectorItem); + addVectorItemToCurrentStroke(vectorItem); + } if (!bLineStyle) { mPreviousPoint = pEndPoint; @@ -1000,6 +1011,19 @@ void UBGraphicsScene::addPolygonItemToCurrentStroke(UBGraphicsPolygonItem* polyg } +void UBGraphicsScene::addVectorItemToCurrentStroke(UBGraphicsVectorItem* vectorItem) +{ + vectorItem->setFlag(QGraphicsItem::ItemIsMovable, true); + vectorItem->setFlag(QGraphicsItem::ItemIsSelectable, true); + vectorItem->SetDelegate(); + + mpLastVector = vectorItem; + mAddedItems.insert(vectorItem); + + // Here we add the item to the scene + addItem(vectorItem); + +} void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) { const QLineF line(mPreviousPoint, pEndPoint); @@ -1273,6 +1297,28 @@ void UBGraphicsScene::initPolygonItem(UBGraphicsPolygonItem* polygonItem) polygonItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Graphic)); } +void UBGraphicsScene::initVectorItem(UBGraphicsVectorItem* vectorItem) +{ + QColor colorOnDarkBG; + QColor colorOnLightBG; + + colorOnDarkBG = UBApplication::boardController->penColorOnDarkBackground(); + colorOnLightBG = UBApplication::boardController->penColorOnLightBackground(); + + if (mDarkBackground) + { + vectorItem->setColor(colorOnDarkBG); + } + else + { + vectorItem->setColor(colorOnLightBG); + } + vectorItem->setColorOnDarkBackground(colorOnDarkBG); + vectorItem->setColorOnLightBackground(colorOnLightBG); + + vectorItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Graphic)); +} + UBGraphicsPolygonItem* UBGraphicsScene::arcToPolygonItem(const QLineF& pStartRadius, qreal pSpanAngle, qreal pWidth) { QPolygonF polygon = UBGeometryUtils::arcToPolygon(pStartRadius, pSpanAngle, pWidth); diff --git a/src/domain/UBGraphicsScene.h b/src/domain/UBGraphicsScene.h index 9e66d662..6d6af1c1 100644 --- a/src/domain/UBGraphicsScene.h +++ b/src/domain/UBGraphicsScene.h @@ -41,6 +41,7 @@ class UBGraphicsPixmapItem; class UBGraphicsProxyWidget; class UBGraphicsSvgItem; class UBGraphicsPolygonItem; +class UBGraphicsVectorItem; class UBGraphicsMediaItem; class UBGraphicsWidgetItem; class UBGraphicsW3CWidgetItem; @@ -400,8 +401,11 @@ public slots: UBGraphicsPolygonItem* curveToPolygonItem(const QList > &points); UBGraphicsPolygonItem* curveToPolygonItem(const QList &points, qreal startWidth, qreal endWidth); void addPolygonItemToCurrentStroke(UBGraphicsPolygonItem* polygonItem); + void addVectorItemToCurrentStroke(UBGraphicsVectorItem* vectorItem); + void initPolygonItem(UBGraphicsPolygonItem*); + void initVectorItem(UBGraphicsVectorItem*); void drawEraser(const QPointF& pEndPoint, bool pressed = true); void redrawEraser(bool pressed); @@ -494,6 +498,8 @@ public slots: UBGraphicsPolygonItem* mpLastPolygon; UBGraphicsPolygonItem* mTempPolygon; + UBGraphicsVectorItem* mpLastVector; + bool mDrawWithCompass; UBGraphicsPolygonItem *mCurrentPolygon; UBSelectionFrame *mSelectionFrame; diff --git a/src/domain/UBGraphicsVectorItem.cpp b/src/domain/UBGraphicsVectorItem.cpp new file mode 100644 index 00000000..22d5a5bb --- /dev/null +++ b/src/domain/UBGraphicsVectorItem.cpp @@ -0,0 +1,206 @@ +#include "UBGraphicsVectorItem.h" + +#include "frameworks/UBGeometryUtils.h" +#include "UBGraphicsScene.h" +#include "core/memcheck.h" +#include + +UBGraphicsVectorItem::UBGraphicsVectorItem (QGraphicsItem * parent) + : QGraphicsLineItem(parent) + , mHasAlpha(false) + , mOriginalWidth(-1) + , mIsNominalLine(false) +{ + // NOOP + initialize(); +} + +UBGraphicsVectorItem::UBGraphicsVectorItem (const QLineF & line, QGraphicsItem * parent) + : QGraphicsLineItem(line, parent) + , mOriginalWidth(-1) + , mIsNominalLine(false) +{ + // NOOP + initialize(); +} + + +UBGraphicsVectorItem::UBGraphicsVectorItem (const QLineF& pLine, qreal pWidth) + : QGraphicsLineItem(pLine) + , mOriginalLine(pLine) + , mOriginalWidth(pWidth) + , mIsNominalLine(true) +{ + // NOOP + initialize(); +} + +UBGraphicsVectorItem::UBGraphicsVectorItem (const QLineF& pLine, qreal pStartWidth, qreal pEndWidth) + : QGraphicsLineItem(pLine) + , mOriginalLine(pLine) + , mOriginalWidth(pEndWidth) + , mIsNominalLine(true) +{ + // NOOP + initialize(); +} + + +void UBGraphicsVectorItem::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); + setSublines(); +} + +void UBGraphicsVectorItem::setSublines() +{ + QLineF thisLine = QLineF(this->line().p2().x(), this->line().p2().y(),this->line().p2().x()+this->line().length()/10, this->line().p2().y()); + thisLine.setAngle(this->line().angle() - 135); + sublines.push_back(new QGraphicsLineItem(thisLine)); + + thisLine = QLineF(this->line().p2().x(), this->line().p2().y(),this->line().p2().x()+this->line().length()/10, this->line().p2().y()); + thisLine.setAngle(this->line().angle() + 135); + sublines.push_back(new QGraphicsLineItem(thisLine)); +} + +void UBGraphicsVectorItem::setUuid(const QUuid &pUuid) +{ + UBItem::setUuid(pUuid); + setData(UBGraphicsItemData::ItemUuid, QVariant(pUuid)); //store item uuid inside the QGraphicsItem to fast operations with Items on the scene +} + +UBGraphicsVectorItem::~UBGraphicsVectorItem() +{ + for (int i = 0; i < sublines.size(); ++i) + delete sublines[i]; + sublines.clear(); +} + +void UBGraphicsVectorItem::setColor(const QColor& pColor) +{ + QPen pen = QPen(pColor); + pen.setStyle(style()); + pen.setCapStyle(Qt::PenCapStyle::RoundCap); + pen.setWidth(mOriginalWidth); + QGraphicsLineItem::setPen(pen); + for (int i = 0; i < sublines.size(); ++i) + sublines[i]->setPen(pen); + mHasAlpha = (pColor.alphaF() < 1.0); +} + + +QColor UBGraphicsVectorItem::color() const +{ + return QGraphicsLineItem::pen().color(); +} + +void UBGraphicsVectorItem::setStyle(const Qt::PenStyle& style) +{ + QPen pen = QPen(color()); + pen.setStyle(style); + pen.setCapStyle(Qt::PenCapStyle::RoundCap); + pen.setWidth(mOriginalWidth); + QGraphicsLineItem::setPen(pen); + for (int i = 0; i < sublines.size(); ++i) + sublines[i]->setPen(pen); +} + +Qt::PenStyle UBGraphicsVectorItem::style() const +{ + return QGraphicsLineItem::pen().style(); +} + +QList UBGraphicsVectorItem::linePoints() +{ + QList points = QList(); + qreal incr = 1/line().length(); + if (incr<0) incr*=-1; + if (incr>0) + { + for (qreal t = 0; t <= 1; t+=incr) + { + points.push_back(line().pointAt(t)); + } + } + return points; +} + +UBItem* UBGraphicsVectorItem::deepCopy() const +{ + UBGraphicsVectorItem* copy = new UBGraphicsVectorItem(line()); + copyItemParameters(copy); + return copy; +} + + +void UBGraphicsVectorItem::copyItemParameters(UBItem *copy) const +{ + UBGraphicsVectorItem *cp = dynamic_cast(copy); + if (cp) + { + cp->mOriginalLine = this->mOriginalLine; + cp->mOriginalWidth = this->mOriginalWidth; + 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 UBGraphicsVectorItem::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); + for (int i = 0; i paint(painter, option, widget); + } + Delegate()->postpaint(painter, &styleOption, widget); +} + +UBGraphicsScene* UBGraphicsVectorItem::scene() +{ + return qobject_cast(QGraphicsLineItem::scene()); +} + +void UBGraphicsVectorItem::SetDelegate() +{ + Delegate()->createControls(); +} + +QVariant UBGraphicsVectorItem::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/UBGraphicsVectorItem.h b/src/domain/UBGraphicsVectorItem.h new file mode 100644 index 00000000..7227065b --- /dev/null +++ b/src/domain/UBGraphicsVectorItem.h @@ -0,0 +1,107 @@ +#ifndef UBGRAPHICSVECTORITEM_H +#define UBGRAPHICSVECTORITEM_H + +#include +#include "core/UB.h" +#include "UBItem.h" + +class UBItem; +class UBGraphicsScene; + +class UBGraphicsVectorItem: public QGraphicsLineItem, public UBItem, public UBGraphicsItem +{ + +public: + UBGraphicsVectorItem(); +public: + + UBGraphicsVectorItem(QGraphicsItem * parent = 0 ); + UBGraphicsVectorItem(const QLineF& line, qreal pWidth); + UBGraphicsVectorItem(const QLineF& pLine, qreal pStartWidth, qreal pEndWidth); + UBGraphicsVectorItem(const QLineF & line, QGraphicsItem * parent = 0); + + ~UBGraphicsVectorItem(); + + void initialize(); + + void setUuid(const QUuid &pUuid); + + void setColor(const QColor& color); + void setStyle(const Qt::PenStyle& style); + + QColor color() const; + Qt::PenStyle style() const; + + virtual UBGraphicsScene* scene(); + + enum { Type = UBGraphicsItemType::VectorItemType }; + + virtual int type() const + { + return Type; + } + + void setLine(const QLineF pLine) + { + mIsNominalLine = false; + QGraphicsLineItem::setLine(pLine); + } + + virtual UBItem* deepCopy() const; + + virtual void copyItemParameters(UBItem *copy) const; + + QLineF originalLine() { return mOriginalLine;} + qreal originalWidth() { return mOriginalWidth;} + bool isNominalLine() {return mIsNominalLine;} + + void setNominalLine(bool isNominalLine) { mIsNominalLine = isNominalLine; } + + QList linePoints(); + + QColor colorOnDarkBackground() const + { + return mColorOnDarkBackground; + } + + void setColorOnDarkBackground(QColor pColorOnDarkBackground) + { + mColorOnDarkBackground = pColorOnDarkBackground; + } + + QColor colorOnLightBackground() const + { + return mColorOnLightBackground; + } + + void setColorOnLightBackground(QColor pColorOnLightBackground) + { + mColorOnLightBackground = pColorOnLightBackground; + } + + void SetDelegate(); + + QList SubLines() + { + return sublines; + } + + protected: + void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget); + virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value); + void setSublines(); + + private: + + bool mHasAlpha; + + QLineF mOriginalLine; + qreal mOriginalWidth; + bool mIsNominalLine; + + QColor mColorOnDarkBackground; + QColor mColorOnLightBackground; + QList sublines; +}; + +#endif // UBGRAPHICSVECTORITEM_H diff --git a/src/domain/domain.pri b/src/domain/domain.pri index bfe9c11e..04e5f4e4 100644 --- a/src/domain/domain.pri +++ b/src/domain/domain.pri @@ -1,4 +1,5 @@ HEADERS += src/domain/UBGraphicsScene.h \ + $$PWD/UBGraphicsVectorItem.h \ src/domain/UBGraphicsItemUndoCommand.h \ src/domain/UBGraphicsTextItemUndoCommand.h \ src/domain/UBGraphicsItemTransformUndoCommand.h \ @@ -28,6 +29,7 @@ HEADERS += src/domain/UBGraphicsScene.h \ src/domain/UBGraphicsItemZLevelUndoCommand.h SOURCES += src/domain/UBGraphicsScene.cpp \ + $$PWD/UBGraphicsVectorItem.cpp \ src/domain/UBGraphicsItemUndoCommand.cpp \ src/domain/UBGraphicsTextItemUndoCommand.cpp \ src/domain/UBGraphicsItemTransformUndoCommand.cpp \