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

© 2007-2011 Witold Gantzke and Kirill Lepskiy