Ok, here is sample of my code:
paint method:
{
if (startPoint == endPoint) return;
Qt::PenStyle lineS;
float lineWidth;
float edgeLineWidth_e = editor->edgeLineWidth*TR_LINE_VIEW_COEF; // better looking on scene
if (dimmed){
lineWidth = edgeLineWidth_e * dimEdgeLineCoef;
lineS = dimEdgeLineStyle;
lineC = dimEdgeLineColor;
} else{
lineWidth = edgeLineWidth_e * edgeLineWidth;
lineS = edgeLineStyle;
lineC = edgeLineColor;
}
if (checked){
painter
->setPen
(QPen(QColor(255,
100,
100), lineWidth, lineS
));
}
else{
painter
->setPen
(QPen(lineC, lineWidth, lineS
));
}
painter->setBrush(Qt::NoBrush);
// p is clasls variable QPainterPath, updated in adjust method, which is called when
// changed (position or size) -> this works correctly
painter->strokePath(p, painter->pen());
// draw arrow
if (checked)
painter
->setBrush
(QColor(255,
100,
100));
else
painter->setBrush(lineC);
painter->drawPolygon(pa); // coumputed in adjust
}
void TransitionArc::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
if (startPoint == endPoint) return;
QColor lineC;
Qt::PenStyle lineS;
float lineWidth;
float edgeLineWidth_e = editor->edgeLineWidth*TR_LINE_VIEW_COEF; // better looking on scene
if (dimmed){
lineWidth = edgeLineWidth_e * dimEdgeLineCoef;
lineS = dimEdgeLineStyle;
lineC = dimEdgeLineColor;
} else{
lineWidth = edgeLineWidth_e * edgeLineWidth;
lineS = edgeLineStyle;
lineC = edgeLineColor;
}
if (checked){
painter->setPen(QPen(QColor(255,100,100), lineWidth, lineS));
}
else{
painter->setPen(QPen(lineC, lineWidth, lineS));
}
painter->setBrush(Qt::NoBrush);
// p is clasls variable QPainterPath, updated in adjust method, which is called when
// changed (position or size) -> this works correctly
painter->strokePath(p, painter->pen());
// draw arrow
if (checked)
painter->setBrush(QColor(255,100,100));
else
painter->setBrush(lineC);
painter->drawPolygon(pa); // coumputed in adjust
}
To copy to clipboard, switch view to plain text mode
boundingRect metohod is implemented in superclass as virtual
QRectF Transition
::boundingRect() const {
QPolygonF areaPol
= makeClickArea
(p
);
// p is clasls variable QPainterPath
QRectF rect
= areaPol.
boundingRect();
if (label)
rect |= (mapFromItem(label, label->boundingRect())).boundingRect(); // rect over pathRect and labelRect
for (TLabelXList::ConstIterator labelIt = nextLabels.begin();
labelIt != nextLabels.end();
++labelIt)
{
rect |= (mapFromItem((*labelIt), (*labelIt)->boundingRect())).boundingRect();
}
rect |= pa.boundingRect();
return rect;
}
QRectF Transition::boundingRect() const
{
QPolygonF areaPol = makeClickArea(p); // p is clasls variable QPainterPath
QRectF rect = areaPol.boundingRect();
if (label)
rect |= (mapFromItem(label, label->boundingRect())).boundingRect(); // rect over pathRect and labelRect
for (TLabelXList::ConstIterator labelIt = nextLabels.begin();
labelIt != nextLabels.end();
++labelIt)
{
rect |= (mapFromItem((*labelIt), (*labelIt)->boundingRect())).boundingRect();
}
rect |= pa.boundingRect();
return rect;
}
To copy to clipboard, switch view to plain text mode
and finally my shape() method:
{
areaPol = areaPol.united(pa);
if (label != NULL)
areaPol = areaPol.united(mapFromItem(label, label->boundingRect()));
for (TLabelXList::ConstIterator labelIt = nextLabels.begin();
labelIt != nextLabels.end();
++labelIt)
{
areaPol = areaPol.united(mapFromItem((*labelIt), (*labelIt)->boundingRect()));
}
path.addPolygon(areaPol);
return path;
}
QPainterPath Transition::shape() const
{
QPainterPath path;
QPolygonF areaPol = makeClickArea(p);
areaPol = areaPol.united(pa);
if (label != NULL)
areaPol = areaPol.united(mapFromItem(label, label->boundingRect()));
for (TLabelXList::ConstIterator labelIt = nextLabels.begin();
labelIt != nextLabels.end();
++labelIt)
{
areaPol = areaPol.united(mapFromItem((*labelIt), (*labelIt)->boundingRect()));
}
path.addPolygon(areaPol);
return path;
}
To copy to clipboard, switch view to plain text mode
In attachements are sample images of my application:
- blue and green polygons are only for my testing, blue == shape polygon and green == bounding rect of item -> green is only for TransitionArc
- note: i create my own shape polygon bigger then line/curve only for make clicking on it easier (is it possible to do it simplier?)
- sample.png is state when I loaded automata
- sample_wrong.png is state after I deleted state 2 and move with state 1. Right Arc is still repainted even if I move with state 1 over it. But in Qt 4.5.3 I have no problem.
Bookmarks