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/CChangeGroup.h>
15 #include <istd/TDelPtr.h>
17 #include <i2d/IObject2d.h>
18 
19 // ACF-Solutions includes
20 #include <icmpstr/TShapeBase.h>
21 
22 
23 namespace icmpstr
24 {
25 
26 
27 template <class GraphicsItemClass, class ObjectClass>
29  public TShapeBase<GraphicsItemClass>,
30  public imod::TSingleModelObserverBase<ObjectClass>
31 {
32 public:
35 
36  TObjectShapeBase(bool isEditable = false, const ISceneProvider* providerPtr = NULL);
37 
38 protected:
39  // reimplemented (TShapeBase)
40  virtual void OnSelectionChanged(bool isSelected);
41  virtual void OnPositionChanged(const QPointF& position);
42 
43  // reimplemented (QGraphicsItem)
44  virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value);
45  virtual void mousePressEvent(QGraphicsSceneMouseEvent* eventPtr);
46  virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* eventPtr);
47  virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* eventPtr);
48 
49  // reimplemented (imod::IObserver)
50  virtual void AfterUpdate(imod::IModel* modelPtr, const istd::IChangeable::ChangeSet& changeSet);
51 
52  // abstract methods
53  virtual void UpdateGraphicsItem(const ObjectClass& object) = 0;
54 
55 private:
56  QPointF m_lastPosition;
57 
58  bool m_isShapeUpdateBlocked;
59 
60  istd::TDelPtr<istd::CChangeGroup> m_dragNotifierPtr;
61 
62  static const istd::IChangeable::ChangeSet s_moveObjectChangeSet;
63 };
64 
65 
66 // public methods
67 
68 template <class GraphicsItemClass, class ObjectClass>
70 : BaseClass(isEditable, providerPtr),
71  m_isShapeUpdateBlocked(false)
72 {
73  BaseClass::SetPen(BaseClass::InactiveColor, QPen(Qt::darkGreen, 0));
74  BaseClass::SetPen(BaseClass::EditableColor, QPen(Qt::green, 0));
75  BaseClass::SetPen(BaseClass::SelectedColor, QPen(Qt::yellow, 0));
76 
77  if (isEditable){
78  BaseClass::setFlags(BaseClass::ItemIsMovable | BaseClass::ItemIsSelectable | BaseClass::ItemSendsGeometryChanges);
79  BaseClass::setCursor(QCursor(Qt::ArrowCursor));
80 
82  }
83  else{
85  }
86 }
87 
88 
89 // protected methods
90 
91 // reimplemented (TShapeBase<GraphicsItemClass, ObjectClass>)
92 
93 template <class GraphicsItemClass, class ObjectClass>
95 {
96  if (isSelected){
97  BaseClass::SwitchColorSheme(BaseClass::SelectedColor);
98  }
99  else{
100  if (BaseClass::IsEditable()){
101  BaseClass::SwitchColorSheme(BaseClass::EditableColor);
102  }
103  else{
104  BaseClass::SwitchColorSheme(BaseClass::InactiveColor);
105  }
106  }
107 }
108 
109 
110 template <class GraphicsItemClass, class ObjectClass>
112 {
113  i2d::IObject2d* objectPtr = BaseClass2::GetObservedObject();
114  if (objectPtr != NULL){
115  QPointF offset = position - m_lastPosition;
116 
117  objectPtr->MoveCenterTo(i2d::CVector2d(offset) + objectPtr->GetCenter());
118 
119  m_lastPosition = position;
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 
145  m_dragNotifierPtr.SetPtr(new istd::CChangeGroup(BaseClass2::GetObservedObject(), &s_moveObjectChangeSet));
146  }
147 }
148 
149 
150 template <class GraphicsItemClass, class ObjectClass>
152 {
153  BaseClass::mouseReleaseEvent(eventPtr);
154 
155  m_dragNotifierPtr.Reset();
156 }
157 
158 
159 template <class GraphicsItemClass, class ObjectClass>
161 {
162  BaseClass::mouseDoubleClickEvent(eventPtr);
163 
164  m_dragNotifierPtr.Reset();
165 }
166 
167 
168 // reimplemented (imod::IObserver)
169 
170 template <class GraphicsItemClass, class ObjectClass>
172 {
173  Q_ASSERT(!m_isShapeUpdateBlocked);
174 
175  ObjectClass* objectPtr = BaseClass2::GetObservedObject();
176  if ((objectPtr != NULL) && !changeSet.ContainsExplicit(istd::IChangeable::CF_DESTROYING)){
177  m_isShapeUpdateBlocked = true;
178 
179  UpdateGraphicsItem(*objectPtr);
180 
181  m_isShapeUpdateBlocked = false;
182 
183  BaseClass::update();
184  }
185 }
186 
187 
188 // static attributes
189 
190 template <class GraphicsItemClass, class ObjectClass>
191 const istd::IChangeable::ChangeSet TObjectShapeBase<GraphicsItemClass, ObjectClass>::s_moveObjectChangeSet(i2d::IObject2d::CF_OBJECT_POSITION, QObject::tr("Move component"));
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:48
virtual void mousePressEvent(QGraphicsSceneMouseEvent *eventPtr)
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *eventPtr)
Interface for GUI objects managing scene.
virtual void OnPositionChanged(const QPointF &position)
virtual void AfterUpdate(imod::IModel *modelPtr, const istd::IChangeable::ChangeSet &changeSet)
Color for a selected shape.
Definition: TShapeBase.h:43
virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
TShapeBase< GraphicsItemClass > BaseClass
virtual void UpdateGraphicsItem(const ObjectClass &object)=0
#define NULL
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *eventPtr)
Color for a shape, that can not be edited on the graphics view.
Definition: TShapeBase.h:53
bool ContainsExplicit(int changeId, bool singleOnly=false) const
virtual void SetPen(int colorSheme, const QPen &pen)
Definition: TShapeBase.h:122
imod::TSingleModelObserverBase< ObjectClass > BaseClass2
TObjectShapeBase(bool isEditable=false, const ISceneProvider *providerPtr=NULL)
virtual void SwitchColorSheme(int colorSheme)
Definition: TShapeBase.h:172
virtual void OnSelectionChanged(bool isSelected)

© 2007-2017 Witold Gantzke and Kirill Lepskiy