1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "UBGraphicsPolygonItem.h"
#include "frameworks/UBGeometryUtils.h"
#include "UBGraphicsScene.h"
#include "domain/UBGraphicsPolygonItem.h"
#include "domain/UBGraphicsStroke.h"
#include "core/memcheck.h"
UBGraphicsPolygonItem::UBGraphicsPolygonItem (QGraphicsItem * parent)
: QGraphicsPolygonItem(parent)
, mHasAlpha(false)
, mOriginalWidth(-1)
, mIsNominalLine(false)
, mStroke(0)
{
// NOOP
initialize();
}
UBGraphicsPolygonItem::UBGraphicsPolygonItem (const QPolygonF & polygon, QGraphicsItem * parent)
: QGraphicsPolygonItem(polygon, parent)
, mOriginalWidth(-1)
, mIsNominalLine(false)
, mStroke(0)
, mpGroup(NULL)
{
// NOOP
initialize();
}
UBGraphicsPolygonItem::UBGraphicsPolygonItem (const QLineF& pLine, qreal pWidth)
: QGraphicsPolygonItem(UBGeometryUtils::lineToPolygon(pLine, pWidth))
, mOriginalLine(pLine)
, mOriginalWidth(pWidth)
, mIsNominalLine(true)
, mStroke(0)
{
// NOOP
initialize();
}
void UBGraphicsPolygonItem::initialize()
{
setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::DrawingItem)); //Necessary to set if we want z value to be assigned correctly
}
void UBGraphicsPolygonItem::clearStroke()
{
if (mStroke!=NULL)
{
mStroke->remove(this);
if (mStroke->polygons().empty())
delete mStroke;
mStroke = NULL;
}
}
UBGraphicsPolygonItem::~UBGraphicsPolygonItem()
{
clearStroke();
}
void UBGraphicsPolygonItem::setStrokesGroup(UBGraphicsStrokesGroup *group)
{
mpGroup = group;
}
void UBGraphicsPolygonItem::setStroke(UBGraphicsStroke* stroke)
{
clearStroke();
mStroke = stroke;
mStroke->addPolygon(this);
}
UBGraphicsStroke* UBGraphicsPolygonItem::stroke() const
{
return mStroke;
}
void UBGraphicsPolygonItem::setColor(const QColor& pColor)
{
QGraphicsPolygonItem::setBrush(QBrush(pColor));
if (pColor.alphaF() >= 1.0)
{
mHasAlpha = false;
setPen(Qt::NoPen);
}
else
{
mHasAlpha = true;
QColor penColor = pColor;
// trick QT antialiasing
// TODO UB 4.x see if we can do better ... it does not behave well with 16 bit color depth
qreal trickAlpha = pColor.alphaF();
if (trickAlpha >= 0.2 && trickAlpha < 0.6)
{
trickAlpha /= 12;
}
else if (trickAlpha < 0.8)
{
trickAlpha /= 5;
}
else if (trickAlpha < 1.0)
{
trickAlpha /= 2;
}
penColor.setAlphaF(trickAlpha);
QGraphicsPolygonItem::setPen(QPen(penColor));
}
}
QColor UBGraphicsPolygonItem::color() const
{
return QGraphicsPolygonItem::brush().color();
}
UBItem* UBGraphicsPolygonItem::deepCopy() const
{
UBGraphicsPolygonItem* copy = new UBGraphicsPolygonItem(polygon(), 0);
UBGraphicsStroke *stroke = new UBGraphicsStroke();
copyItemParameters(copy);
copy->setStroke(stroke);
return copy;
}
void UBGraphicsPolygonItem::copyItemParameters(UBItem *copy) const
{
UBGraphicsPolygonItem *cp = dynamic_cast<UBGraphicsPolygonItem*>(copy);
if (cp)
{
cp->mOriginalLine = this->mOriginalLine;
cp->mOriginalWidth = this->mOriginalWidth;
cp->mIsNominalLine = this->mIsNominalLine;
cp->setTransform(transform());
cp->setBrush(this->brush());
cp->setPen(this->pen());
cp->mHasAlpha = this->mHasAlpha;
cp->setColorOnDarkBackground(this->colorOnDarkBackground());
cp->setColorOnLightBackground(this->colorOnLightBackground());
cp->setData(UBGraphicsItemData::ItemLayerType, this->data(UBGraphicsItemData::ItemLayerType));
}
}
void UBGraphicsPolygonItem::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
if(mHasAlpha && scene() && scene()->isLightBackground())
{
painter->setCompositionMode(QPainter::CompositionMode_Darken);
}
QGraphicsPolygonItem::paint(painter, option, widget);
}
QPainterPath UBGraphicsPolygonItem::shape() const
{
QPainterPath path;
path.addRect(boundingRect());
return path;
// static QPainterPath shapePath = QGraphicsPolygonItem::shape();
// return shapePath;
}
UBGraphicsScene* UBGraphicsPolygonItem::scene()
{
return qobject_cast<UBGraphicsScene*>(QGraphicsPolygonItem::scene());
}