Skip to content

Commit

Permalink
TD: support weld symbol selection highlight and drag move
Browse files Browse the repository at this point in the history
  • Loading branch information
realthunder committed Oct 17, 2023
1 parent 23afd84 commit 41f83c8
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 46 deletions.
37 changes: 34 additions & 3 deletions src/Mod/TechDraw/Gui/QGCustomSvg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# include <QStyleOptionGraphicsItem>
#endif

#include "PreferencesGui.h"
#include "QGCustomSvg.h"


Expand Down Expand Up @@ -83,8 +84,38 @@ void QGCustomSvg::paint ( QPainter * painter, const QStyleOptionGraphicsItem * o
QStyleOptionGraphicsItem myOption(*option);
myOption.state &= ~QStyle::State_Selected;

// painter->setPen(Qt::yellow);
// painter->drawRect(boundingRect()); //good for debugging

if (m_pen.style() != Qt::NoPen) {
painter->setPen(m_pen);
painter->drawRect(boundingRect());
}
QGraphicsSvgItem::paint (painter, &myOption, widget);
}

void QGCustomSvg::setPreselect(bool enable)
{
m_hasHover = enable;
if (enable)
setPrettyPre();
else if (isSelected())
setPrettySel();
else
setPrettyNormal();
update();
}

void QGCustomSvg::setPrettyPre()
{
m_pen.setColor(PreferencesGui::preselectQColor());
m_pen.setStyle(Qt::DashLine);
}

void QGCustomSvg::setPrettySel()
{
m_pen.setStyle(Qt::DashLine);
m_pen.setColor(PreferencesGui::selectQColor());
}

void QGCustomSvg::setPrettyNormal()
{
m_pen.setStyle(Qt::NoPen);
}
7 changes: 7 additions & 0 deletions src/Mod/TechDraw/Gui/QGCustomSvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,15 @@ class TechDrawGuiExport QGCustomSvg : public QGraphicsSvgItem
virtual bool load(QByteArray *svgString);
virtual bool load(QString filename);

void setPrettyNormal();
void setPrettyPre();
void setPrettySel();
void setPreselect(bool enable);

protected:
QSvgRenderer *m_svgRender;
bool m_hasHover = false;
QPen m_pen;
};

} // namespace TechDrawGui
Expand Down
15 changes: 15 additions & 0 deletions src/Mod/TechDraw/Gui/QGCustomText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ void QGCustomText::setTightBounding(bool tight)
tightBounding = tight;
}

void QGCustomText::setTightPicking(bool tight)
{
tightPicking = tight;
}

void QGCustomText::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
QStyleOptionGraphicsItem myOption(*option);
myOption.state &= ~QStyle::State_Selected;
Expand Down Expand Up @@ -211,6 +216,16 @@ QRectF QGCustomText::tightBoundingRect() const
return result;
}

QPainterPath QGCustomText::shape() const
{
QPainterPath path;
if (tightPicking || tightBounding)
path.addRect(tightBoundingRect());
else
path.addRect(relaxedBoundingRect());
return path;
}

// Calculate the amount of difference between tight and relaxed bounding boxes
QPointF QGCustomText::tightBoundingAdjust() const
{
Expand Down
4 changes: 4 additions & 0 deletions src/Mod/TechDraw/Gui/QGCustomText.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ class TechDrawGuiExport QGCustomText : public QGraphicsTextItem
virtual void setColor(QColor c);

virtual void setTightBounding(bool tight);
void setTightPicking(bool tight);

void makeMark(double x, double y);
void makeMark(Base::Vector3d v);

bool hasHover() const {return m_hasHover;}

QPainterPath shape() const override;

protected:
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
Expand All @@ -90,6 +93,7 @@ class TechDrawGuiExport QGCustomText : public QGraphicsTextItem

bool isHighlighted;
bool tightBounding; // Option to use tighter boundingRect(), works only for plaintext QGCustomText
bool tightPicking = false;
QColor m_colCurrent;
QColor m_colNormal;
bool m_hasHover = false;
Expand Down
9 changes: 9 additions & 0 deletions src/Mod/TechDraw/Gui/QGIDecoration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,12 @@ void QGIDecoration::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
inherited::hoverLeaveEvent(event);
}

QPainterPath QGIDecoration::shape() const
{
QPainterPath path;
for (auto child : childItems()) {
if (child->isVisible())
path.addPath(child->shape());
}
return path;
}
1 change: 1 addition & 0 deletions src/Mod/TechDraw/Gui/QGIDecoration.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class TechDrawGuiExport QGIDecoration : public QGraphicsItemGroup
int type() const override { return Type;}

QRectF boundingRect() const override;
QPainterPath shape() const override;
void paint(QPainter * painter,
const QStyleOptionGraphicsItem * option,
QWidget * widget = nullptr ) override;
Expand Down
52 changes: 47 additions & 5 deletions src/Mod/TechDraw/Gui/QGILeaderLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ QGILeaderLine::QGILeaderLine()
m_blockDraw(false)

{
setHandlesChildEvents(false);
setAcceptHoverEvents(true);
setFiltersChildEvents(true);
setAcceptHoverEvents(false);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemIsMovable, false);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, false);
Expand All @@ -73,26 +73,33 @@ QGILeaderLine::QGILeaderLine()
m_line = new QGIPrimPath();
addToGroup(m_line);
m_line->setFlag(QGraphicsItem::ItemIsSelectable, false);
m_line->setAcceptHoverEvents(false);
m_line->setAcceptHoverEvents(true);
m_line->setPos(0.0, 0.0);
m_line->setZValue(ZVALUE::DIMENSION+1);

m_editPath = new QGEPath(this);
addToGroup(m_editPath);
m_editPath->setPos(0.0, 0.0);
m_editPath->setFlag(QGraphicsItem::ItemIsSelectable, false);
m_editPath->setFlag(QGraphicsItem::ItemIsMovable, false);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, false);
m_editPath->setZValue(ZVALUE::DIMENSION);
m_editPath->setZValue(ZVALUE::DIMENSION+2);
m_editPath->hide();

m_arrow1 = new QGIArrow();
m_arrow1->setFlag(QGraphicsItem::ItemIsSelectable, false);
m_arrow1->setAcceptHoverEvents(true);
addToGroup(m_arrow1);
m_arrow1->setPos(0.0, 0.0);
m_arrow1->hide();
m_arrow1->setZValue(ZVALUE::DIMENSION+1);
m_arrow2 = new QGIArrow();
m_arrow2->setFlag(QGraphicsItem::ItemIsSelectable, false);
m_arrow2->setAcceptHoverEvents(true);
addToGroup(m_arrow2);
m_arrow2->setPos(0.0, 0.0);
m_arrow2->hide();
m_arrow2->setZValue(ZVALUE::DIMENSION+1);

setZValue(ZVALUE::DIMENSION);

Expand Down Expand Up @@ -124,6 +131,9 @@ QVariant QGILeaderLine::itemChange(GraphicsItemChange change, const QVariant& va
if (isSelected()) {
setPrettySel();
}
else if (m_hasHover) {
setPrettyPre();
}
else {
setPrettyNormal();
}
Expand Down Expand Up @@ -155,7 +165,7 @@ void QGILeaderLine::setPreselect(bool enable)
if (enable) {
setPrettyPre();
}
else if (!isSelected()) {
else if (isSelected()) {
setPrettySel();
}
else {
Expand Down Expand Up @@ -323,6 +333,18 @@ void QGILeaderLine::updateView(bool update)
draw();
}

QPainterPath QGILeaderLine::shape() const
{
QPainterPath path;
if (m_line && m_line->isVisible())
path.addPath(m_line->shape());
if (m_arrow1 && m_arrow1->isVisible())
path.addPath(m_arrow1->shape());
if (m_arrow2 && m_arrow2->isVisible())
path.addPath(m_arrow2->shape());
return path;
}

void QGILeaderLine::draw()
{
// Base::Console().Message("QGILL::draw()- %s\n", getViewObject()->getNameInDocument());
Expand Down Expand Up @@ -597,4 +619,24 @@ void QGILeaderLine::paint(QPainter* painter, const QStyleOptionGraphicsItem* opt
QGIView::paint(painter, &myOption, widget);
}

bool QGILeaderLine::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
{
if (watched == m_line
|| watched == m_arrow1
|| watched == m_arrow2)
{
switch (event->type()) {
case QEvent::GraphicsSceneHoverEnter:
hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent*>(event));
return true;
case QEvent::GraphicsSceneHoverLeave:
hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent*>(event));
return true;
default:
break;
}
}
return false;
}

#include <Mod/TechDraw/Gui/moc_QGILeaderLine.cpp>
2 changes: 2 additions & 0 deletions src/Mod/TechDraw/Gui/QGILeaderLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ class TechDrawGuiExport QGILeaderLine: public QGIView
}
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
QWidget* widget = nullptr) override;
QPainterPath shape() const override;
QRectF boundingRect() const override;

bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;
void drawBorder() override;
void updateView(bool update = false) override;

Expand Down
24 changes: 17 additions & 7 deletions src/Mod/TechDraw/Gui/QGITile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,27 @@ QGITile::QGITile(TechDraw::DrawTileWeld* dtw) :
m_altWeld(false),
m_tileFeat(dtw)
{
setFeature(dtw);

m_qgSvg = new QGCustomSvg();
m_qgSvg->setAcceptHoverEvents(true);
addToGroup(m_qgSvg);

// m_effect = new QGraphicsColorizeEffect();

m_qgTextL = new QGCustomText();
m_qgTextL->setAcceptHoverEvents(true);
m_qgTextL->setTightPicking(true);
addToGroup(m_qgTextL);

m_qgTextR = new QGCustomText();
m_qgTextR->setAcceptHoverEvents(true);
m_qgTextR->setTightPicking(true);
addToGroup(m_qgTextR);

m_qgTextC = new QGCustomText();
m_qgTextC->setAcceptHoverEvents(true);
m_qgTextC->setTightPicking(true);
addToGroup(m_qgTextC);

m_wide = getSymbolWidth();
Expand All @@ -77,7 +86,7 @@ QGITile::QGITile(TechDraw::DrawTileWeld* dtw) :
m_fontName = prefTextFont();
m_font = QFont(m_fontName);

setFiltersChildEvents(true); //qt5
setHandlesChildEvents(true); //qt5
setAcceptHoverEvents(true);
setFlag(QGraphicsItem::ItemIsSelectable, false);
setFlag(QGraphicsItem::ItemIsMovable, false);
Expand Down Expand Up @@ -133,6 +142,7 @@ void QGITile::draw()
double y = m_origin.y() - (m_row * m_high) - (m_high * 0.5); //inverted y!!
setPos(x, y);
}
inherited::draw();
}

void QGITile::makeSymbol()
Expand Down Expand Up @@ -283,8 +293,8 @@ void QGITile::setPrettyNormal() {
m_qgTextL->setColor(m_colNormal);
m_qgTextR->setColor(m_colNormal);
m_qgTextC->setColor(m_colNormal);

draw();
m_qgSvg->setPrettyNormal();
inherited::setPrettyNormal();
}

void QGITile::setPrettyPre() {
Expand All @@ -294,8 +304,8 @@ void QGITile::setPrettyPre() {
m_qgTextL->setColor(m_colCurrent);
m_qgTextR->setColor(m_colCurrent);
m_qgTextC->setColor(m_colCurrent);

draw();
m_qgSvg->setPrettyPre();
inherited::setPrettyPre();
}

void QGITile::setPrettySel() {
Expand All @@ -305,8 +315,8 @@ void QGITile::setPrettySel() {
m_qgTextL->setColor(m_colCurrent);
m_qgTextR->setColor(m_colCurrent);
m_qgTextC->setColor(m_colCurrent);

draw();
m_qgSvg->setPrettySel();
inherited::setPrettySel();
}

bool QGITile::isTailRight()
Expand Down
16 changes: 9 additions & 7 deletions src/Mod/TechDraw/Gui/QGITile.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ class QGIWeldSymbol;

class TechDrawGuiExport QGITile : public QGIDecoration
{
using inherited = QGIDecoration;
public:
explicit QGITile(TechDraw::DrawTileWeld*);
~QGITile(void);
~QGITile(void) override;

enum {Type = QGraphicsItem::UserType + 325};
int type(void) const { return Type;}
int type(void) const override { return Type;}

virtual QRectF boundingRect() const;
QRectF boundingRect() const override;

void setTileTextLeft(std::string s);
void setTileTextRight(std::string s);
Expand All @@ -65,13 +66,14 @@ class TechDrawGuiExport QGITile : public QGIDecoration
void setTailRight(bool b) { m_tailRight = b; }
void setAltWeld(bool b) { m_altWeld = b; }
bool isTailRight(void);
virtual void draw(void);
void draw(void) override;

void setPrettyNormal() override;
void setPrettyPre() override;
void setPrettySel() override;

protected:
QColor getTileColor(void) const;
void setPrettyNormal();
void setPrettyPre();
void setPrettySel();

double getSymbolWidth(void) const;
double getSymbolHeight(void) const;
Expand Down
Loading

0 comments on commit 41f83c8

Please sign in to comment.