From dbfe1747300cdab30454697c7ac5694f3c011f38 Mon Sep 17 00:00:00 2001 From: thomas_lucky13 Date: Wed, 22 Jun 2022 21:26:51 +0400 Subject: [PATCH] UBGraphicsLineItem --- src/core/UB.h | 1 + src/domain/UBGraphicsLineItem.cpp | 191 ++++++++++++++++++++++++++++++ src/domain/UBGraphicsLineItem.h | 134 +++++++++++++++++++++ src/domain/domain.pri | 2 + 4 files changed, 328 insertions(+) create mode 100644 src/domain/UBGraphicsLineItem.cpp create mode 100644 src/domain/UBGraphicsLineItem.h diff --git a/src/core/UB.h b/src/core/UB.h index 8b9b1c7f..388d265f 100644 --- a/src/core/UB.h +++ b/src/core/UB.h @@ -173,6 +173,7 @@ struct UBGraphicsItemType GraphicsWidgetItemType, //65556 UserTypesCount, //65557 AxesItemType, //65558 + LineItemType, //65559 SelectionFrameType // this line must be the last line in this enum because it is types counter. }; }; diff --git a/src/domain/UBGraphicsLineItem.cpp b/src/domain/UBGraphicsLineItem.cpp new file mode 100644 index 00000000..0db07259 --- /dev/null +++ b/src/domain/UBGraphicsLineItem.cpp @@ -0,0 +1,191 @@ +#include "UBGraphicsLineItem.h" + +#include "frameworks/UBGeometryUtils.h" +#include "UBGraphicsScene.h" +#include "domain/UBGraphicsPolygonItem.h" +#include "domain/UBGraphicsStroke.h" + +#include "core/memcheck.h" + +#include + +UBGraphicsLineItem::UBGraphicsLineItem (QGraphicsItem * parent) + : QGraphicsLineItem(parent) + , mHasAlpha(false) + , mOriginalWidth(-1) + , mIsNominalLine(false) + , mStroke(0) + , mpGroup(NULL) +{ + // NOOP + initialize(); +} + + +UBGraphicsLineItem::UBGraphicsLineItem (const QLineF & line, QGraphicsItem * parent) + : QGraphicsLineItem(line, parent) + , mOriginalWidth(-1) + , mIsNominalLine(false) + , mStroke(0) + , mpGroup(NULL) +{ + // NOOP + initialize(); +} + + +UBGraphicsLineItem::UBGraphicsLineItem (const QLineF& pLine, qreal pWidth) + : QGraphicsLineItem(pLine) + , mOriginalLine(pLine) + , mOriginalWidth(pWidth) + , mIsNominalLine(true) + , mStroke(0) + , mpGroup(NULL) +{ + // NOOP + initialize(); +} + +UBGraphicsLineItem::UBGraphicsLineItem (const QLineF& pLine, qreal pStartWidth, qreal pEndWidth) + : QGraphicsLineItem(pLine) + , mOriginalLine(pLine) + , mOriginalWidth(pEndWidth) + , mIsNominalLine(true) + , mStroke(0) + , mpGroup(NULL) +{ + // NOOP + initialize(); +} + + +void UBGraphicsLineItem::initialize() +{ + setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::DrawingItem)); //Necessary to set if we want z value to be assigned correctly + setUuid(QUuid::createUuid()); +} + +void UBGraphicsLineItem::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 +} + +void UBGraphicsLineItem::clearStroke() +{ + if (mStroke!=NULL) + { + //mStroke->remove(this); + if (mStroke->polygons().empty()) + delete mStroke; + mStroke = NULL; + } +} + +UBGraphicsLineItem::~UBGraphicsLineItem() +{ + //clearStroke(); +} + +void UBGraphicsLineItem::setStrokesGroup(UBGraphicsStrokesGroup *group) +{ + mpGroup = group; +} + +void UBGraphicsLineItem::setStroke(UBGraphicsStroke* stroke) +{ + if (stroke) { + clearStroke(); + + mStroke = stroke; + //mStroke->addPolygon(this); + } +} + +UBGraphicsStroke* UBGraphicsLineItem::stroke() const +{ + return mStroke; +} + + +void UBGraphicsLineItem::setColor(const QColor& pColor) +{ + QPen pen = QPen(pColor); + pen.setStyle(style()); + if (style()==Qt::PenStyle::DotLine) + { + pen.setCapStyle(Qt::PenCapStyle::RoundCap); + } + pen.setWidth(mOriginalWidth); + QGraphicsLineItem::setPen(pen); + + mHasAlpha = (pColor.alphaF() < 1.0); +} + + +QColor UBGraphicsLineItem::color() const +{ + return QGraphicsLineItem::pen().color(); +} + +void UBGraphicsLineItem::setStyle(const Qt::PenStyle& style) +{ + QPen pen = QPen(color()); + pen.setStyle(style); + if (style==Qt::PenStyle::DotLine) + { + pen.setCapStyle(Qt::PenCapStyle::RoundCap); + } + pen.setWidth(mOriginalWidth); + QGraphicsLineItem::setPen(pen); +} + +Qt::PenStyle UBGraphicsLineItem::style() const +{ + return QGraphicsLineItem::pen().style(); +} + + +UBItem* UBGraphicsLineItem::deepCopy() const +{ + UBGraphicsLineItem* copy = new UBGraphicsLineItem(line()); + copyItemParameters(copy); + return copy; +} + + +void UBGraphicsLineItem::copyItemParameters(UBItem *copy) const +{ + UBGraphicsLineItem *cp = dynamic_cast(copy); + if (cp) + { + cp->mOriginalLine = this->mOriginalLine; + cp->mOriginalWidth = this->mOriginalWidth; + cp->mIsNominalLine = this->mIsNominalLine; + + cp->setTransform(transform()); + cp->setPen(this->pen()); + cp->mHasAlpha = this->mHasAlpha; + + cp->setColorOnDarkBackground(this->colorOnDarkBackground()); + cp->setColorOnLightBackground(this->colorOnLightBackground()); + + cp->setZValue(this->zValue()); + cp->setData(UBGraphicsItemData::ItemLayerType, this->data(UBGraphicsItemData::ItemLayerType)); + } +} + +void UBGraphicsLineItem::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) +{ + if(mHasAlpha && scene() && scene()->isLightBackground()) + painter->setCompositionMode(QPainter::CompositionMode_SourceOver); + + painter->setRenderHints(QPainter::Antialiasing); + + QGraphicsLineItem::paint(painter, option, widget); +} + +UBGraphicsScene* UBGraphicsLineItem::scene() +{ + return qobject_cast(QGraphicsLineItem::scene()); +} diff --git a/src/domain/UBGraphicsLineItem.h b/src/domain/UBGraphicsLineItem.h new file mode 100644 index 00000000..c5f88999 --- /dev/null +++ b/src/domain/UBGraphicsLineItem.h @@ -0,0 +1,134 @@ +#ifndef UBGRAPHICSLINEITEM_H +#define UBGRAPHICSLINEITEM_H + +#include +#include "core/UB.h" +#include "UBItem.h" +#include "UBGraphicsStrokesGroup.h" +#include "domain/UBGraphicsGroupContainerItem.h" + +class UBItem; +class UBGraphicsScene; +class UBGraphicsStroke; + +class UBGraphicsLineItem : public QGraphicsLineItem, public UBItem +{ + + public: + + UBGraphicsLineItem(QGraphicsItem * parent = 0 ); + UBGraphicsLineItem(const QLineF& line, qreal pWidth); + UBGraphicsLineItem(const QLineF& pLine, qreal pStartWidth, qreal pEndWidth); + UBGraphicsLineItem(const QLineF & line, QGraphicsItem * parent = 0); + + ~UBGraphicsLineItem(); + + void initialize(); + + void setUuid(const QUuid &pUuid); + + void setStrokesGroup(UBGraphicsStrokesGroup* group); + UBGraphicsStrokesGroup* strokesGroup() const{return mpGroup;} + void setColor(const QColor& color); + void setStyle(const Qt::PenStyle& style); + + QColor color() const; + Qt::PenStyle style() const; + + virtual UBGraphicsScene* scene(); + + inline void subtract(UBGraphicsLineItem *pi) + { + if (boundingRect().intersects(pi->boundingRect())) + { + //QLineF subtractedLine = line().substracted(pi->line()); + + /*if (line() != subtractedLine) + { + mIsNominalLine = false; + QGraphicsLineItem::setLine(subtractedLine); + }*/ + } + } + + inline void subtractIntersecting(UBGraphicsLineItem *pi) + { + /*QLineF subtractedLine = line().substracted(pi->line()) + + if (line() != subtractedLine) + { + mIsNominalLine = false; + QGraphicsLineItem::setLine(subtractedLine); + }*/ + } + + enum { Type = UBGraphicsItemType::LineItemType }; + + 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; } + + QColor colorOnDarkBackground() const + { + return mColorOnDarkBackground; + } + + void setColorOnDarkBackground(QColor pColorOnDarkBackground) + { + mColorOnDarkBackground = pColorOnDarkBackground; + } + + QColor colorOnLightBackground() const + { + return mColorOnLightBackground; + } + + void setColorOnLightBackground(QColor pColorOnLightBackground) + { + mColorOnLightBackground = pColorOnLightBackground; + } + + void setStroke(UBGraphicsStroke* stroke); + UBGraphicsStroke* stroke() const; + + protected: + void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget); + + + private: + + void clearStroke(); + + bool mHasAlpha; + + QLineF mOriginalLine; + qreal mOriginalWidth; + bool mIsNominalLine; + + QColor mColorOnDarkBackground; + QColor mColorOnLightBackground; + + UBGraphicsStroke* mStroke; + UBGraphicsStrokesGroup* mpGroup; + +}; + +#endif // UBGRAPHICSLINEITEM_H + diff --git a/src/domain/domain.pri b/src/domain/domain.pri index bfe9c11e..91206704 100644 --- a/src/domain/domain.pri +++ b/src/domain/domain.pri @@ -6,6 +6,7 @@ HEADERS += src/domain/UBGraphicsScene.h \ src/domain/UBPageSizeUndoCommand.h \ src/domain/UBGraphicsProxyWidget.h \ src/domain/UBGraphicsSvgItem.h \ + src/domain/UBGraphicsLineItem.h \ src/domain/UBGraphicsPolygonItem.h \ src/domain/UBItem.h \ src/domain/UBGraphicsWidgetItem.h \ @@ -35,6 +36,7 @@ SOURCES += src/domain/UBGraphicsScene.cpp \ src/domain/UBPageSizeUndoCommand.cpp \ src/domain/UBGraphicsProxyWidget.cpp \ src/domain/UBGraphicsSvgItem.cpp \ + src/domain/UBGraphicsLineItem.cpp \ src/domain/UBGraphicsPolygonItem.cpp \ src/domain/UBItem.cpp \ src/domain/UBGraphicsWidgetItem.cpp \