TObjectShapeBase.h
Go to the documentation of this file.
1 #ifndef icmpstr_TObjectShapeBase_included
2 #define icmpstr_TObjectShapeBase_included
3 
4 
5 // Qt includes
6 #include <QtCore/QtGlobal>
7 #if QT_VERSION >= 0x050000
8 #include <QtWidgets/QGraphicsSceneMouseEvent>
9 #else
10 #include <QtGui/QGraphicsSceneMouseEvent>
11 #endif
12 
13 // ACF includes
14 #include "istd/TChangeNotifier.h"
16 #include "i2d/IObject2d.h"
17 
18 // ACF-Solutions includes
19 #include "icmpstr/TShapeBase.h"
20 
21 
22 namespace icmpstr
23 {
24 
25 
26 template <class GraphicsItemClass, class ObjectClass>
28  public TShapeBase<GraphicsItemClass>,
29  public imod::TSingleModelObserverBase<ObjectClass>
30 {
31 public:
34 
35  TObjectShapeBase(bool isEditable = false, const ISceneProvider* providerPtr = NULL);
36 
37 protected:
38  // reimplemented (TShapeBase)
39  virtual void OnSelectionChanged(bool isSelected);
40  virtual void OnPositionChanged(const QPointF& position);
41 
42  // reimplemented (QGraphicsItem)
43  virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value);
44  virtual void mousePressEvent(QGraphicsSceneMouseEvent* eventPtr);
45  virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* eventPtr);
46  virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* eventPtr);
47 
48  // reimplemented (imod::IObserver)
49  virtual void AfterUpdate(imod::IModel* modelPtr, int updateFlags, istd::IPolymorphic* updateParamsPtr);
50 
51  // abstract methods
52  virtual void UpdateGraphicsItem(const ObjectClass& object) = 0;
53 
54 private:
55  QPointF m_lastPosition;
56  bool m_isObjectMoved;
57 
58  bool m_isShapeUpdateBlocked;
59 
60  istd::CChangeNotifier m_mousePressingNotifier;
61 };
62 
63 
64 // public methods
65 
66 template <class GraphicsItemClass, class ObjectClass>
68 : BaseClass(isEditable, providerPtr),
69  m_isShapeUpdateBlocked(false),
70  m_mousePressingNotifier(NULL, i2d::IObject2d::CF_OBJECT_POSITION | istd::IChangeable::CF_MODEL)
71 {
72  BaseClass::SetPen(BaseClass::InactiveColor, QPen(Qt::darkGreen, 0));
73  BaseClass::SetPen(BaseClass::EditableColor, QPen(Qt::green, 0));
74  BaseClass::SetPen(BaseClass::SelectedColor, QPen(Qt::yellow, 0));
75 
76  if (isEditable){
77  BaseClass::setFlags(BaseClass::ItemIsMovable | BaseClass::ItemIsSelectable | BaseClass::ItemSendsGeometryChanges);
78  BaseClass::setCursor(QCursor(Qt::ArrowCursor));
79 
81  }
82  else{
84  }
85 }
86 
87 
88 // protected methods
89 
90 // reimplemented (TShapeBase<GraphicsItemClass, ObjectClass>)
91 
92 template <class GraphicsItemClass, class ObjectClass>
94 {
95  if (isSelected){
96  BaseClass::SwitchColorSheme(BaseClass::SelectedColor);
97  }
98  else{
99  if (BaseClass::IsEditable()){
100  BaseClass::SwitchColorSheme(BaseClass::EditableColor);
101  }
102  else{
103  BaseClass::SwitchColorSheme(BaseClass::InactiveColor);
104  }
105  }
106 }
107 
108 
109 template <class GraphicsItemClass, class ObjectClass>
111 {
112  i2d::IObject2d* objectPtr = BaseClass2::GetObjectPtr();
113  if (objectPtr != NULL){
114  QPointF offset = position - m_lastPosition;
115 
116  objectPtr->MoveCenterTo(i2d::CVector2d(offset) + objectPtr->GetCenter());
117 
118  m_lastPosition = position;
119  m_isObjectMoved = true;
120  }
121 
122  BaseClass::OnPositionChanged(position);
123 }
124 
125 
126 template <class GraphicsItemClass, class ObjectClass>
127 QVariant TObjectShapeBase<GraphicsItemClass, ObjectClass>::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value)
128 {
129  if (!m_isShapeUpdateBlocked){
130  return BaseClass::itemChange(change, value);
131  }
132 
133  return value;
134 }
135 
136 
137 template <class GraphicsItemClass, class ObjectClass>
139 {
140  BaseClass::mousePressEvent(eventPtr);
141 
142  if (BaseClass::IsEditable() && (eventPtr->button() == Qt::LeftButton)){
143  m_lastPosition = BaseClass::pos();
144  m_isObjectMoved = false;
145 
146  m_mousePressingNotifier.SetPtr(BaseClass2::GetObjectPtr());
147  }
148 }
149 
150 
151 template <class GraphicsItemClass, class ObjectClass>
153 {
154  BaseClass::mouseReleaseEvent(eventPtr);
155 
156  if (m_isObjectMoved){
157  m_mousePressingNotifier.Reset();
158  }
159  else{
160  m_mousePressingNotifier.Abort();
161  }
162 }
163 
164 
165 template <class GraphicsItemClass, class ObjectClass>
167 {
168  BaseClass::mouseDoubleClickEvent(eventPtr);
169 
170  m_mousePressingNotifier.Reset();
171 }
172 
173 
174 // reimplemented (imod::IObserver)
175 
176 template <class GraphicsItemClass, class ObjectClass>
177 void TObjectShapeBase<GraphicsItemClass, ObjectClass>::AfterUpdate(imod::IModel* /*modelPtr*/, int /*updateFlags*/, istd::IPolymorphic* /*updateParamsPtr*/)
178 {
179  Q_ASSERT(!m_isShapeUpdateBlocked);
180 
181  ObjectClass* objectPtr = BaseClass2::GetObjectPtr();
182  if (objectPtr != NULL){
183  m_isShapeUpdateBlocked = true;
184 
185  UpdateGraphicsItem(*objectPtr);
186 
187  m_isShapeUpdateBlocked = false;
188 
189  BaseClass::update();
190  }
191 }
192 
193 
194 } // namespace icmpstr
195 
196 
197 #endif // !icmpstr_TObjectShapeBase_included
198 
199 
Color for a shape, that can be edited on the graphics view.
Definition: TShapeBase.h:47
virtual void mousePressEvent(QGraphicsSceneMouseEvent *eventPtr)
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *eventPtr)
Interface for GUI objects managing scene.
virtual void OnPositionChanged(const QPointF &position)
Color for a selected shape.
Definition: TShapeBase.h:42
virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
TShapeBase< GraphicsItemClass > BaseClass
virtual void UpdateGraphicsItem(const ObjectClass &object)=0
virtual void AfterUpdate(imod::IModel *modelPtr, int updateFlags, istd::IPolymorphic *updateParamsPtr)
#define NULL
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *eventPtr)
Color for a shape, that can not be edited on the graphics view.
Definition: TShapeBase.h:52
virtual void SetPen(int colorSheme, const QPen &pen)
Definition: TShapeBase.h:121
imod::TSingleModelObserverBase< ObjectClass > BaseClass2
TObjectShapeBase(bool isEditable=false, const ISceneProvider *providerPtr=NULL)
virtual void SwitchColorSheme(int colorSheme)
Definition: TShapeBase.h:171
virtual void OnSelectionChanged(bool isSelected)

© 2007-2011 Witold Gantzke and Kirill Lepskiy