Fix bug with item highligh selected.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2017-10-29 11:06:17 +02:00
parent def801a626
commit 09f3ba234c
11 changed files with 74 additions and 10 deletions

View File

@ -118,7 +118,7 @@ void VAbstractSpline::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
painter->restore(); painter->restore();
} }
QGraphicsPathItem::paint(painter, option, widget); PaintWithFixItemHighlightSelected<QGraphicsPathItem>(this, painter, option, widget);
}; };
if (not m_parentRefresh) if (not m_parentRefresh)

View File

@ -185,7 +185,7 @@ void VToolLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
setPen(QPen(CorrectColor(this, lineColor), width, LineStyleToPenStyle(m_lineType))); setPen(QPen(CorrectColor(this, lineColor), width, LineStyleToPenStyle(m_lineType)));
QGraphicsLineItem::paint(painter, option, widget); PaintWithFixItemHighlightSelected<QGraphicsLineItem>(this, painter, option, widget);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -133,7 +133,7 @@ void VToolPiecePath::paint(QPainter *painter, const QStyleOptionGraphicsItem *op
setPen(toolPen); setPen(toolPen);
QGraphicsPathItem::paint(painter, option, widget); PaintWithFixItemHighlightSelected<QGraphicsPathItem>(this, painter, option, widget);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -918,7 +918,7 @@ void VToolSeamAllowance::paint(QPainter *painter, const QStyleOptionGraphicsItem
{ {
setSelected(true); setSelected(true);
} }
QGraphicsPathItem::paint(painter, option, widget); PaintWithFixItemHighlightSelected<QGraphicsPathItem>(this, painter, option, widget);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -133,3 +133,38 @@ QPainterPath ItemShapeFromPath(const QPainterPath &path, const QPen &pen)
p.addPath(path); p.addPath(path);
return p; 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));
}

View File

@ -30,6 +30,7 @@
#define GLOBAL_H #define GLOBAL_H
#include <QtGlobal> #include <QtGlobal>
#include <QStyleOptionGraphicsItem>
extern const qreal widthMainLine; extern const qreal widthMainLine;
extern const qreal widthHairLine; extern const qreal widthHairLine;
@ -55,4 +56,28 @@ qreal ScaleWidth(qreal width, qreal scale);
QPainterPath ItemShapeFromPath(const QPainterPath &path, const QPen &pen); 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<class Parent, class Item>
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 #endif // GLOBAL_H

View File

@ -52,7 +52,7 @@ void VScaledLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio
qRound(width) < 1 ? lPen.setWidthF(width) : lPen.setWidth(qRound(width)); qRound(width) < 1 ? lPen.setWidthF(width) : lPen.setWidth(qRound(width));
setPen(lPen); setPen(lPen);
QGraphicsLineItem::paint(painter, option, widget); PaintWithFixItemHighlightSelected<QGraphicsLineItem>(this, painter, option, widget);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -84,5 +84,5 @@ void VScaledEllipse::paint(QPainter *painter, const QStyleOptionGraphicsItem *op
setPen(visPen); setPen(visPen);
ScaleCircleSize(this, scale); ScaleCircleSize(this, scale);
QGraphicsEllipseItem::paint(painter, option, widget); PaintWithFixItemHighlightSelected<QGraphicsEllipseItem>(this, painter, option, widget);
} }

View File

@ -93,7 +93,7 @@ void VCurvePathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *op
painter->restore(); painter->restore();
} }
QGraphicsPathItem::paint(painter, option, widget); PaintWithFixItemHighlightSelected<QGraphicsPathItem>(this, painter, option, widget);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -111,7 +111,7 @@ void VGraphicsSimpleTextItem::paint(QPainter *painter, const QStyleOptionGraphic
{ {
VMainGraphicsView::NewSceneRect(scene, view, this); VMainGraphicsView::NewSceneRect(scene, view, this);
} }
QGraphicsSimpleTextItem::paint(painter, option, widget); PaintWithFixItemHighlightSelected<QGraphicsSimpleTextItem>(this, painter, option, widget);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -27,6 +27,7 @@
*************************************************************************/ *************************************************************************/
#include "vnobrushscalepathitem.h" #include "vnobrushscalepathitem.h"
#include "global.h"
#include <QBrush> #include <QBrush>
#include <QMatrix> #include <QMatrix>
@ -56,5 +57,5 @@ void VNoBrushScalePathItem::paint(QPainter *painter, const QStyleOptionGraphicsI
QBrush brush = this->brush(); QBrush brush = this->brush();
brush.setMatrix(painter->combinedMatrix().inverted()); brush.setMatrix(painter->combinedMatrix().inverted());
this->setBrush(brush); this->setBrush(brush);
QGraphicsPathItem::paint(painter, option, widget); PaintWithFixItemHighlightSelected<QGraphicsPathItem>(this, painter, option, widget);
} }

View File

@ -35,7 +35,10 @@
#include <QBrush> #include <QBrush>
#include <QFont> #include <QFont>
#include <QPainter>
#include <QPen> #include <QPen>
#include <QStyle>
#include <QStyleOptionGraphicsItem>
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VScenePoint::VScenePoint(QGraphicsItem *parent) VScenePoint::VScenePoint(QGraphicsItem *parent)
@ -87,7 +90,7 @@ void VScenePoint::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio
} }
} }
QGraphicsEllipseItem::paint(painter, option, widget); PaintWithFixItemHighlightSelected<QGraphicsEllipseItem>(this, painter, option, widget);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------