TShapeBase.h
Go to the documentation of this file.
1 #ifndef icmpstr_TShapeBase_included
2 #define icmpstr_TShapeBase_included
3 
4 
5 // Qt includes
6 #include <QtCore/QObject>
7 #include <QtCore/QVariant>
8 #include <QtCore/QMap>
9 #include <QtCore/QPointF>
10 #include <QtGui/QBrush>
11 #include <QtGui/QPen>
12 #if QT_VERSION >= 0x050000
13 #include <QtCore/QtMath>
14 #include <QtWidgets/QGraphicsItem>
15 #include <QtWidgets/QStyle>
16 #include <QtWidgets/QStyleOptionGraphicsItem>
17 #else
18 #include <QtCore/qmath.h>
19 #include <QtGui/QGraphicsItem>
20 #include <QtGui/QStyle>
21 #include <QtGui/QStyleOptionGraphicsItem>
22 #endif
23 
24 // ACF-Solutions includes
25 #include <icmpstr/ISceneProvider.h>
26 
27 
28 namespace icmpstr
29 {
30 
31 
32 template <class GraphicsItemClass>
33 class TShapeBase: public QObject, public GraphicsItemClass
34 {
35 public:
36  typedef GraphicsItemClass BaseClass;
37 
39  {
44 
49 
54 
59  };
60 
61  TShapeBase(bool isEditable = false, const ISceneProvider* providerPtr = NULL);
62 
63  virtual void SetPen(int colorSheme, const QPen& pen);
64  virtual QPen GetPen(int colorSheme) const;
65 
66  virtual void SetBrush(int colorSheme, const QBrush& pen);
67  virtual QBrush GetBrush(int colorSheme) const;
68 
69  virtual void SwitchColorSheme(int colorSheme);
70 
71  bool IsEditable() const;
72  void SetEditable(bool isEditable);
73 
74  const ISceneProvider* GetSceneProvider() const;
75 
76 protected:
80  i2d::CVector2d GetPosFromLocal(const QPointF& position) const;
84  QPointF GetLocalFromPos(const i2d::CVector2d& position) const;
85 
86  virtual void OnSelectionChanged(bool isSelected);
87  virtual void OnPositionChanged(const QPointF& position);
88 
89  // reimplemented (QGraphicsItem)
90  virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value);
91  virtual void hoverEnterEvent(QGraphicsSceneHoverEvent* event);
92  virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent* event);
93  virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = NULL);
94 
95 private:
96  typedef QPair<QPen, QBrush> ColorShemeInfo;
97  typedef QMap<int, ColorShemeInfo> ColorShemeMap;
98 
99  bool m_isEditable;
100  const ISceneProvider* m_providerPtr;
101 
102  ColorShemeMap m_colorShemeMap;
103 };
104 
105 
106 // public methods
107 
108 template <class GraphicsItemClass>
109 TShapeBase<GraphicsItemClass>::TShapeBase(bool isEditable, const ISceneProvider* providerPtr)
110 : m_isEditable(isEditable),
111  m_providerPtr(providerPtr)
112 {
113 #if QT_VERSION >= 0x050000
114  BaseClass::setAcceptHoverEvents(true);
115 #else
116  BaseClass::setAcceptsHoverEvents(true);
117 #endif
118 }
119 
120 
121 template <class GraphicsItemClass>
122 void TShapeBase<GraphicsItemClass>::SetPen(int colorSheme, const QPen& pen)
123 {
124  ColorShemeInfo info;
125 
126  if (m_colorShemeMap.contains(colorSheme)){
127  info = m_colorShemeMap.value(colorSheme);
128  }
129 
130  info.first = pen;
131  m_colorShemeMap[colorSheme] = info;
132 }
133 
134 
135 template <class GraphicsItemClass>
136 QPen TShapeBase<GraphicsItemClass>::GetPen(int colorSheme) const
137 {
138  if (m_colorShemeMap.contains(colorSheme)){
139  return m_colorShemeMap.value(colorSheme).first;
140  }
141 
142  return QPen();
143 }
144 
145 
146 template <class GraphicsItemClass>
147 void TShapeBase<GraphicsItemClass>::SetBrush(int colorSheme, const QBrush& brush)
148 {
149  ColorShemeInfo info;
150 
151  if (m_colorShemeMap.contains(colorSheme)){
152  info = m_colorShemeMap.value(colorSheme);
153  }
154 
155  info.second = brush;
156  m_colorShemeMap[colorSheme] = info;
157 }
158 
159 
160 template <class GraphicsItemClass>
161 QBrush TShapeBase<GraphicsItemClass>::GetBrush(int colorSheme) const
162 {
163  if (m_colorShemeMap.contains(colorSheme)){
164  return m_colorShemeMap.value(colorSheme).second;
165  }
166 
167  return QBrush();
168 }
169 
170 
171 template <class GraphicsItemClass>
173 {
174  QAbstractGraphicsShapeItem* itemPtr = dynamic_cast<QAbstractGraphicsShapeItem*>(this);
175  if (itemPtr != NULL){
176  QBrush brush = GetBrush(colorSheme);
177  itemPtr->setBrush(brush);
178 
179  QPen pen = GetPen(colorSheme);
180  itemPtr->setPen(pen);
181  }
182  else{
183  QGraphicsLineItem* linePtr = dynamic_cast<QGraphicsLineItem*>(this);
184  if (linePtr != NULL){
185  QPen pen = GetPen(colorSheme);
186  linePtr->setPen(pen);
187  }
188  }
189 }
190 
191 
192 template <class GraphicsItemClass>
194 {
195  return m_isEditable;
196 }
197 
198 
199 template <class GraphicsItemClass>
201 {
202  m_isEditable = isEditable;
203  if (isEditable){
204  BaseClass::setFlags(BaseClass::flags() | BaseClass::ItemIsMovable | BaseClass::ItemIsSelectable | BaseClass::ItemSendsGeometryChanges);
205  }
206  else{
207  BaseClass::setFlags(BaseClass::flags() & ~(BaseClass::ItemIsMovable | BaseClass::ItemIsSelectable));
208  }
209 }
210 
211 
212 template <class GraphicsItemClass>
214 {
215  return m_providerPtr;
216 }
217 
218 
219 // protected methods
220 
221 template <class GraphicsItemClass>
223 {
224  return BaseClass::mapToScene(position);
225 }
226 
227 
228 template <class GraphicsItemClass>
230 {
231  return BaseClass::mapFromScene(position);
232 }
233 
234 
235 template <class GraphicsItemClass>
237 {
238  if (isSelected){
239  SwitchColorSheme(SelectedColor);
240  }
241  else{
242  SwitchColorSheme(DefaultColor);
243  }
244 }
245 
246 
247 template <class GraphicsItemClass>
248 void TShapeBase<GraphicsItemClass>::OnPositionChanged(const QPointF& /*position*/)
249 {
250 }
251 
252 
253 // reimplemented (QGraphicsItem)
254 
255 template <class GraphicsItemClass>
256 QVariant TShapeBase<GraphicsItemClass>::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value)
257 {
258  if (change == BaseClass::ItemPositionChange){
259  if (m_providerPtr != NULL && ((m_providerPtr->GetSceneFlags() & icmpstr::ISceneProvider::SF_FIXED_SIZE) != 0)){
260  QGraphicsScene* scenePtr = m_providerPtr->GetScene();
261 
262  if (scenePtr != NULL){
263  QRectF sceneRect = scenePtr->sceneRect();
264  QRectF itemRect = BaseClass::boundingRect();
265 
266  i2d::CRectangle innerRect = iqt::GetCRectangle(sceneRect.adjusted(itemRect.width() / 2, itemRect.height() / 2, -itemRect.width() / 2, -itemRect.height() / 2));
267  i2d::CVector2d innerPosition = i2d::CVector2d(value.toPointF());
268 
269  innerPosition = innerRect.GetNearestPointTo(innerPosition);
270 
271  return QPointF(innerPosition.GetX(), innerPosition.GetY());
272  }
273  }
274  }
275 
276  if (change == BaseClass::ItemPositionHasChanged){
277  QPointF position = value.toPointF();
278 
279  if (m_providerPtr != NULL){
280  double distance;
281  if (m_providerPtr->GetSceneAlignment(distance)){
282  position.setX(qRound(position.x() / distance) * distance);
283  position.setY(qRound(position.y() / distance) * distance);
284  }
285  }
286 
287  OnPositionChanged(position);
288 
289  return position;
290  }
291 
292  if (change == BaseClass::ItemSelectedChange){
293  OnSelectionChanged(value.toBool());
294  }
295 
296  return BaseClass::itemChange(change, value);
297 }
298 
299 
300 template <class GraphicsItemClass>
301 void TShapeBase<GraphicsItemClass>::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
302 {
303  if (m_isEditable){
304  OnSelectionChanged(true);
305  }
306 }
307 
308 
309 template <class GraphicsItemClass>
310 void TShapeBase<GraphicsItemClass>::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
311 {
312  if (m_isEditable){
313  OnSelectionChanged(false);
314  }
315 }
316 
317 
318 template <class GraphicsItemClass>
319 void TShapeBase<GraphicsItemClass>::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
320 {
321  // remove selected flag to prevent the drawing of the cruel rubber band:
322 
323  if ((option != NULL) && ((option->state & QStyle::State_Selected) != 0)){
324  QStyleOptionGraphicsItem newOption = *option;
325 
326  newOption.state &= ~QStyle::State_Selected;
327 
328  BaseClass::paint(painter, &newOption, widget);
329  }
330  else{
331  BaseClass::paint(painter, option, widget);
332  }
333 }
334 
335 
336 // specialized implementations
337 
338 template <>
339 inline void TShapeBase<QGraphicsItem>::paint(QPainter* /*painter*/, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
340 {
341 }
342 
343 
344 template <>
345 inline void TShapeBase<QAbstractGraphicsShapeItem>::paint(QPainter* /*painter*/, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
346 {
347 }
348 
349 
350 } // namespace icmpstr
351 
352 
353 #endif // !icmpstr_TShapeBase_included
354 
355 
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=NULL)
Definition: TShapeBase.h:319
Color for a shape, that can be edited on the graphics view.
Definition: TShapeBase.h:48
QPointF GetLocalFromPos(const i2d::CVector2d &position) const
Map position from local graphics item coordinates.
Definition: TShapeBase.h:229
i2d::CRectangle GetCRectangle(const QRectF &rect)
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
Definition: TShapeBase.h:301
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
Definition: TShapeBase.h:310
double GetY() const
virtual void OnPositionChanged(const QPointF &position)
Definition: TShapeBase.h:248
Interface for GUI objects managing scene.
TShapeBase(bool isEditable=false, const ISceneProvider *providerPtr=NULL)
Definition: TShapeBase.h:109
const ISceneProvider * GetSceneProvider() const
Definition: TShapeBase.h:213
bool IsEditable() const
Definition: TShapeBase.h:193
CVector2d GetNearestPointTo(const CVector2d &point) const
GraphicsItemClass BaseClass
Definition: TShapeBase.h:36
virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
Definition: TShapeBase.h:256
void SetEditable(bool isEditable)
Definition: TShapeBase.h:200
Color for a selected shape.
Definition: TShapeBase.h:43
virtual void OnSelectionChanged(bool isSelected)
Definition: TShapeBase.h:236
#define NULL
virtual QBrush GetBrush(int colorSheme) const
Definition: TShapeBase.h:161
Color for a shape, that can not be edited on the graphics view.
Definition: TShapeBase.h:53
Scene has a fixed size and cannot be resized by user.
virtual void SetBrush(int colorSheme, const QBrush &pen)
Definition: TShapeBase.h:147
i2d::CVector2d GetPosFromLocal(const QPointF &position) const
Map position from local graphics item coordinates.
Definition: TShapeBase.h:222
virtual void SetPen(int colorSheme, const QPen &pen)
Definition: TShapeBase.h:122
virtual QPen GetPen(int colorSheme) const
Definition: TShapeBase.h:136
double GetX() const
virtual void SwitchColorSheme(int colorSheme)
Definition: TShapeBase.h:172
Standard shape color.
Definition: TShapeBase.h:58

© 2007-2017 Witold Gantzke and Kirill Lepskiy