From 09f3ba234cd6a50ccd78eda0ab1c5c14345e74ee Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sun, 29 Oct 2017 11:06:17 +0200 Subject: [PATCH] Fix bug with item highligh selected. --HG-- branch : develop --- .../drawTools/toolcurve/vabstractspline.cpp | 2 +- src/libs/vtools/tools/drawTools/vtoolline.cpp | 2 +- .../tools/nodeDetails/vtoolpiecepath.cpp | 2 +- src/libs/vtools/tools/vtoolseamallowance.cpp | 2 +- src/libs/vwidgets/global.cpp | 35 +++++++++++++++++++ src/libs/vwidgets/global.h | 25 +++++++++++++ src/libs/vwidgets/scalesceneitems.cpp | 4 +-- src/libs/vwidgets/vcurvepathitem.cpp | 2 +- src/libs/vwidgets/vgraphicssimpletextitem.cpp | 2 +- src/libs/vwidgets/vnobrushscalepathitem.cpp | 3 +- src/libs/vwidgets/vscenepoint.cpp | 5 ++- 11 files changed, 74 insertions(+), 10 deletions(-) diff --git a/src/libs/vtools/tools/drawTools/toolcurve/vabstractspline.cpp b/src/libs/vtools/tools/drawTools/toolcurve/vabstractspline.cpp index 69e75ed73..b355df76e 100644 --- a/src/libs/vtools/tools/drawTools/toolcurve/vabstractspline.cpp +++ b/src/libs/vtools/tools/drawTools/toolcurve/vabstractspline.cpp @@ -118,7 +118,7 @@ void VAbstractSpline::paint(QPainter *painter, const QStyleOptionGraphicsItem *o painter->restore(); } - QGraphicsPathItem::paint(painter, option, widget); + PaintWithFixItemHighlightSelected(this, painter, option, widget); }; if (not m_parentRefresh) diff --git a/src/libs/vtools/tools/drawTools/vtoolline.cpp b/src/libs/vtools/tools/drawTools/vtoolline.cpp index 32bb0f93c..6e4453989 100644 --- a/src/libs/vtools/tools/drawTools/vtoolline.cpp +++ b/src/libs/vtools/tools/drawTools/vtoolline.cpp @@ -185,7 +185,7 @@ void VToolLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, setPen(QPen(CorrectColor(this, lineColor), width, LineStyleToPenStyle(m_lineType))); - QGraphicsLineItem::paint(painter, option, widget); + PaintWithFixItemHighlightSelected(this, painter, option, widget); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vtools/tools/nodeDetails/vtoolpiecepath.cpp b/src/libs/vtools/tools/nodeDetails/vtoolpiecepath.cpp index 75ad2c42a..3d11c6989 100644 --- a/src/libs/vtools/tools/nodeDetails/vtoolpiecepath.cpp +++ b/src/libs/vtools/tools/nodeDetails/vtoolpiecepath.cpp @@ -133,7 +133,7 @@ void VToolPiecePath::paint(QPainter *painter, const QStyleOptionGraphicsItem *op setPen(toolPen); - QGraphicsPathItem::paint(painter, option, widget); + PaintWithFixItemHighlightSelected(this, painter, option, widget); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vtools/tools/vtoolseamallowance.cpp b/src/libs/vtools/tools/vtoolseamallowance.cpp index 67fb54b0d..a03d5e46e 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.cpp +++ b/src/libs/vtools/tools/vtoolseamallowance.cpp @@ -918,7 +918,7 @@ void VToolSeamAllowance::paint(QPainter *painter, const QStyleOptionGraphicsItem { setSelected(true); } - QGraphicsPathItem::paint(painter, option, widget); + PaintWithFixItemHighlightSelected(this, painter, option, widget); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vwidgets/global.cpp b/src/libs/vwidgets/global.cpp index cf41b9a9f..baee2f3c8 100644 --- a/src/libs/vwidgets/global.cpp +++ b/src/libs/vwidgets/global.cpp @@ -133,3 +133,38 @@ QPainterPath ItemShapeFromPath(const QPainterPath &path, const QPen &pen) p.addPath(path); return p; } + +//--------------------------------------------------------------------------------------------------------------------- +void GraphicsItemHighlightSelected(const QRectF &boundingRect, qreal itemPenWidth, QPainter *painter, + const QStyleOptionGraphicsItem *option) +{ + const QRectF murect = painter->transform().mapRect(QRectF(0, 0, 1, 1)); + if (qFuzzyIsNull(qMax(murect.width(), murect.height()))) + { + return; + } + + const QRectF mbrect = painter->transform().mapRect(boundingRect); + if (qMin(mbrect.width(), mbrect.height()) < qreal(1.0)) + { + return; + } + + const qreal pad = itemPenWidth / 2; + + const qreal penWidth = 0; // cosmetic pen + + const QColor fgcolor = option->palette.windowText().color(); + const QColor bgcolor( // ensure good contrast against fgcolor + fgcolor.red() > 127 ? 0 : 255, + fgcolor.green() > 127 ? 0 : 255, + fgcolor.blue() > 127 ? 0 : 255); + + painter->setPen(QPen(bgcolor, penWidth, Qt::SolidLine)); + painter->setBrush(Qt::NoBrush); + painter->drawRect(boundingRect.adjusted(pad, pad, -pad, -pad)); + + painter->setPen(QPen(option->palette.windowText(), 0, Qt::DashLine)); + painter->setBrush(Qt::NoBrush); + painter->drawRect(boundingRect.adjusted(pad, pad, -pad, -pad)); +} diff --git a/src/libs/vwidgets/global.h b/src/libs/vwidgets/global.h index 905b0be82..4de573900 100644 --- a/src/libs/vwidgets/global.h +++ b/src/libs/vwidgets/global.h @@ -30,6 +30,7 @@ #define GLOBAL_H #include +#include extern const qreal widthMainLine; extern const qreal widthHairLine; @@ -55,4 +56,28 @@ qreal ScaleWidth(qreal width, qreal scale); QPainterPath ItemShapeFromPath(const QPainterPath &path, const QPen &pen); +void GraphicsItemHighlightSelected(const QRectF &boundingRect, qreal itemPenWidth, QPainter *painter, + const QStyleOptionGraphicsItem *option); + +/* Helps fix bug with item highlight selected. + * Unfortunatelly qt_graphicsItem_highlightSelected was designed in way that doesn't support custom QGraphicsItem + * classes. Such classes always get pen width 1.0. This make imposible to draw selection rect around very small objects. + */ +template +void PaintWithFixItemHighlightSelected(Item *item, QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget) +{ + QStyleOptionGraphicsItem myoption = (*option); + if (myoption.state & QStyle::State_Selected) + { + myoption.state &= !QStyle::State_Selected; + } + item->Parent::paint(painter, &myoption, widget); + + if (option->state & QStyle::State_Selected) + { + GraphicsItemHighlightSelected(item->boundingRect(), item->pen().widthF(), painter, option); + } +} + #endif // GLOBAL_H diff --git a/src/libs/vwidgets/scalesceneitems.cpp b/src/libs/vwidgets/scalesceneitems.cpp index 262e476fd..9f8eba9fc 100644 --- a/src/libs/vwidgets/scalesceneitems.cpp +++ b/src/libs/vwidgets/scalesceneitems.cpp @@ -52,7 +52,7 @@ void VScaledLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio qRound(width) < 1 ? lPen.setWidthF(width) : lPen.setWidth(qRound(width)); setPen(lPen); - QGraphicsLineItem::paint(painter, option, widget); + PaintWithFixItemHighlightSelected(this, painter, option, widget); } //--------------------------------------------------------------------------------------------------------------------- @@ -84,5 +84,5 @@ void VScaledEllipse::paint(QPainter *painter, const QStyleOptionGraphicsItem *op setPen(visPen); ScaleCircleSize(this, scale); - QGraphicsEllipseItem::paint(painter, option, widget); + PaintWithFixItemHighlightSelected(this, painter, option, widget); } diff --git a/src/libs/vwidgets/vcurvepathitem.cpp b/src/libs/vwidgets/vcurvepathitem.cpp index b6568e31c..20ab950c0 100644 --- a/src/libs/vwidgets/vcurvepathitem.cpp +++ b/src/libs/vwidgets/vcurvepathitem.cpp @@ -93,7 +93,7 @@ void VCurvePathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *op painter->restore(); } - QGraphicsPathItem::paint(painter, option, widget); + PaintWithFixItemHighlightSelected(this, painter, option, widget); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vwidgets/vgraphicssimpletextitem.cpp b/src/libs/vwidgets/vgraphicssimpletextitem.cpp index 4f0412588..b85a71aa6 100644 --- a/src/libs/vwidgets/vgraphicssimpletextitem.cpp +++ b/src/libs/vwidgets/vgraphicssimpletextitem.cpp @@ -111,7 +111,7 @@ void VGraphicsSimpleTextItem::paint(QPainter *painter, const QStyleOptionGraphic { VMainGraphicsView::NewSceneRect(scene, view, this); } - QGraphicsSimpleTextItem::paint(painter, option, widget); + PaintWithFixItemHighlightSelected(this, painter, option, widget); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vwidgets/vnobrushscalepathitem.cpp b/src/libs/vwidgets/vnobrushscalepathitem.cpp index 0457a6803..aa0d8fd85 100644 --- a/src/libs/vwidgets/vnobrushscalepathitem.cpp +++ b/src/libs/vwidgets/vnobrushscalepathitem.cpp @@ -27,6 +27,7 @@ *************************************************************************/ #include "vnobrushscalepathitem.h" +#include "global.h" #include #include @@ -56,5 +57,5 @@ void VNoBrushScalePathItem::paint(QPainter *painter, const QStyleOptionGraphicsI QBrush brush = this->brush(); brush.setMatrix(painter->combinedMatrix().inverted()); this->setBrush(brush); - QGraphicsPathItem::paint(painter, option, widget); + PaintWithFixItemHighlightSelected(this, painter, option, widget); } diff --git a/src/libs/vwidgets/vscenepoint.cpp b/src/libs/vwidgets/vscenepoint.cpp index 4eccd33cd..3e5654ba1 100644 --- a/src/libs/vwidgets/vscenepoint.cpp +++ b/src/libs/vwidgets/vscenepoint.cpp @@ -35,7 +35,10 @@ #include #include +#include #include +#include +#include //--------------------------------------------------------------------------------------------------------------------- VScenePoint::VScenePoint(QGraphicsItem *parent) @@ -87,7 +90,7 @@ void VScenePoint::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio } } - QGraphicsEllipseItem::paint(painter, option, widget); + PaintWithFixItemHighlightSelected(this, painter, option, widget); } //---------------------------------------------------------------------------------------------------------------------