TShapeParamsGuiCompBase.h
Go to the documentation of this file.
1 #ifndef iqt2d_TShapeParamsGuiCompBase_included
2 #define iqt2d_TShapeParamsGuiCompBase_included
3 
4 
5 // Qt includes
6 #include <QtCore/QtGlobal>
7 #if QT_VERSION >= 0x050000
8 #include <QtWidgets/QMenu>
9 #include <QtWidgets/QPushButton>
10 #include <QtWidgets/QToolButton>
11 #include <QtWidgets/QToolBar>
12 #else
13 #include <QtGui/QMenu>
14 #include <QtGui/QPushButton>
15 #include <QtGui/QToolButton>
16 #include <QtGui/QToolBar>
17 #endif
18 
19 // ACF includes
20 #include <i2d/ICalibration2d.h>
22 #include <iview/IColorSchema.h>
23 #include <iview/IShapeFactory.h>
26 #include <iprm/IEnableableParam.h>
27 
28 
29 namespace iqt2d
30 {
31 
32 
33 template <class Ui, class Shape, class ShapeModel>
36  iqtgui::TDesignerGuiObserverCompBase<Ui, ShapeModel> >,
37  virtual public iview::IShapeFactory
38 {
39 public:
41 
42  I_BEGIN_COMPONENT(TShapeParamsGuiCompBase);
43  I_REGISTER_INTERFACE(iview::IShapeFactory);
44  I_ASSIGN(m_defaultUnitInfoCompPtr, "DefaultUnitInfo", "Provide default information about the logical value units e.g. mm, this will be used if no unit information found in model", false, "DefaultUnitInfo");
45  I_ASSIGN(m_colorSchemaCompPtr, "ShapeColorSchema", "Color schema used by displayed shape", false, "ShapeColorSchema");
46  I_ASSIGN(m_fixedPositionAttrPtr, "FixedPosition", "If enabled, the shape position will be not editable", true, false);
47  I_ASSIGN(m_fixedPositionParamPtr, "FixedPositionParam", "If set, will be used instead of FixedPosition attribute", false, "FixedPositionParam");
48  I_ASSIGN(m_allowToolsMenuAttrPtr, "ShowTools", "Show extended shape edit tools", true, true);
49  I_ASSIGN(m_toolBarGuiCompPtr, "ToolBarGui", "Toolbar GUI object to fill with actions", false, "ToolBarGui");
50  I_ASSIGN(m_shapeToolTipAttrPtr, "ShapeToolTip", "Tool tip will be show on console", false, "");
51  I_ASSIGN(m_hideParameterEditorAttrPtr, "HideParameterEditor", "If set, the parameter editor will be hidden (only shape will be visible)", true, false);
52  I_END_COMPONENT;
53 
55 
56  // reimplemented (imod::IObserver)
57  virtual bool OnModelAttached(imod::IModel* modelPtr, istd::IChangeable::ChangeSet& changeMask);
58  virtual bool OnModelDetached(imod::IModel* modelPtr);
59 
60  // reimplemented (iview::IShapeFactory)
61  virtual iview::IShape* CreateShape(const istd::IChangeable* objectPtr, bool connectToModel = false) const;
62 
63 protected:
64  typedef typename BaseClass::Shapes Shapes;
65  typedef typename BaseClass::ShapesMap ShapesMap;
66 
67  QString GetUnitName() const;
68  bool IsPositionFixed() const;
69  bool IsToolsMenuAllowed() const;
70  QToolBar* GetToolBar() const;
71 
75  virtual void CreateToolsMenu(QAbstractButton* buttonPtr);
76  virtual bool PopulateActions(QWidget& host, imod::IModel* modelPtr);
77  virtual void OnModelAttachedAndGuiShown(imod::IModel* modelPtr);
78  virtual void OnModelDetachedOrGuiHidden(imod::IModel* modelPtr);
79  virtual void OnActionTriggered(QAction* actionPtr);
80 
85 
86  // reimplemented (iqt2d::TViewExtenderCompBase)
87  virtual void CreateShapes(int sceneId, Shapes& result);
88 
89  // reimplemented (iqtgui::CGuiComponentBase)
90  virtual void OnGuiDestroyed();
91  virtual void OnGuiShown();
92  virtual void OnGuiHidden();
93 
94  // reimplemented (iqtgui::TGuiObserverWrap)
95  virtual void OnGuiModelAttached();
96  virtual void OnGuiModelDetached();
97 
98 private:
99  QMenu* m_menuPtr;
100  QAbstractButton* m_menuButtonPtr;
101 
102  typedef QSet<QAction*> Actions;
103  Actions m_toolBarActions;
104 
105  I_REF(imath::IUnitInfo, m_defaultUnitInfoCompPtr);
106  I_REF(iview::IColorSchema, m_colorSchemaCompPtr);
107  I_REF(iprm::IEnableableParam, m_fixedPositionParamPtr);
108  I_ATTR(bool, m_fixedPositionAttrPtr);
109  I_ATTR(bool, m_allowToolsMenuAttrPtr);
110  I_ATTR(bool, m_hideParameterEditorAttrPtr);
111  I_REF(iqtgui::IGuiObject, m_toolBarGuiCompPtr);
112  I_TEXTATTR(m_shapeToolTipAttrPtr);
113 };
114 
115 
116 // public methods
117 
118 template <class Ui, class Shape, class ShapeModel>
120 : m_menuPtr(NULL),
121  m_menuButtonPtr(NULL)
122 {
123 
124 }
125 
126 
127 // reimplemented (imod::IObserver)
128 
129 template <class Ui, class Shape, class ShapeModel>
131 {
132  if (BaseClass::OnModelAttached(modelPtr, changeMask)){
133  const ShapesMap& shapesMap = BaseClass::GetShapesMap();
134  for ( typename ShapesMap::const_iterator iter = shapesMap.begin();
135  iter != shapesMap.end();
136  ++iter){
137  const Shapes& shapes = iter.value();
138  int shapesCount = shapes.GetCount();
139  for (int shapeIndex = 0; shapeIndex < shapesCount; ++shapeIndex){
140  Shape* shapePtr = dynamic_cast<Shape*>(shapes.GetAt(shapeIndex));
141  if (shapePtr != NULL){
142  if (modelPtr->AttachObserver(shapePtr)){
143  shapePtr->SetVisible(true);
144  }
145  }
146  }
147  }
148 
149  return true;
150  }
151  else{
152  return false;
153  }
154 }
155 
156 
157 template <class Ui, class Shape, class ShapeModel>
159 {
160  if (BaseClass::OnModelDetached(modelPtr)){
161  const ShapesMap& shapesMap = BaseClass::GetShapesMap();
162  for ( typename ShapesMap::const_iterator iter = shapesMap.begin();
163  iter != shapesMap.end();
164  ++iter){
165  const Shapes& shapes = iter.value();
166  int shapesCount = shapes.GetCount();
167  for (int shapeIndex = 0; shapeIndex < shapesCount; ++shapeIndex){
168  Shape* shapePtr = dynamic_cast<Shape*>(shapes.GetAt(shapeIndex));
169  if (shapePtr != NULL){
170  if (modelPtr->IsAttached(shapePtr)){
171  modelPtr->DetachObserver(shapePtr);
172  }
173 
174  shapePtr->SetVisible(false);
175  }
176  }
177  }
178 
179  return true;
180  }
181  else{
182  return false;
183  }
184 }
185 
186 
187 // reimplemented (iview::IShapeFactory)
188 
189 template <class Ui, class Shape, class ShapeModel>
191 {
192  iview::CInteractiveShapeBase* shapePtr = CreateShapeInstance();
193  if (shapePtr != NULL){
194  shapePtr->SetEditablePosition(!IsPositionFixed());
195 
196  if (m_colorSchemaCompPtr.IsValid()){
197  shapePtr->SetUserColorSchema(m_colorSchemaCompPtr.GetPtr());
198  }
199 
200  if (m_shapeToolTipAttrPtr.IsValid()){
201  shapePtr->SetDefaultDescription(*m_shapeToolTipAttrPtr);
202  }
203 
204  if (BaseClass::IsReadOnly()){
206  }
207 
208  if (connectToModel){
209  imod::IModel* modelPtr = dynamic_cast<imod::IModel*>(const_cast<istd::IChangeable*>(objectPtr));
210  if (modelPtr != NULL){
211  if (modelPtr->AttachObserver(shapePtr)){
212  shapePtr->SetVisible(true);
213  }
214  }
215  }
216  }
217 
218  return shapePtr;
219 }
220 
221 
222 // protected methods
223 
224 template <class Ui, class Shape, class ShapeModel>
226 {
227  if (m_fixedPositionParamPtr.IsValid()){
228  return m_fixedPositionParamPtr->IsEnabled();
229  }
230 
231  return *m_fixedPositionAttrPtr;
232 }
233 
234 
235 template <class Ui, class Shape, class ShapeModel>
237 {
238  return *m_allowToolsMenuAttrPtr;
239 }
240 
241 
242 template <class Ui, class Shape, class ShapeModel>
244 {
245  return m_toolBarGuiCompPtr.IsValid() ? dynamic_cast<QToolBar*>(m_toolBarGuiCompPtr->GetWidget()) : NULL;
246 }
247 
248 
249 template <class Ui, class Shape, class ShapeModel>
251 {
252  const imath::IUnitInfo* unitInfoPtr = NULL;
253 
254  const ShapeModel* objectPtr = BaseClass::GetObservedObject();
255  if (objectPtr != NULL){
256  const i2d::ICalibrationProvider* calibrationProviderPtr = dynamic_cast<const i2d::ICalibrationProvider*>(objectPtr);
257  if (calibrationProviderPtr != NULL){
258  const i2d::ICalibration2d* calibrationPtr = calibrationProviderPtr->GetCalibration();
259  if (calibrationPtr != NULL){
260  unitInfoPtr = calibrationPtr->GetArgumentUnitInfo();
261  }
262  }
263  }
264 
265  if (unitInfoPtr != NULL){
266  return unitInfoPtr->GetUnitName();
267  }
268 
269  if (m_defaultUnitInfoCompPtr.IsValid()){
270  return m_defaultUnitInfoCompPtr->GetUnitName();
271  }
272 
273  return QString();
274 }
275 
276 
277 template <class Ui, class Shape, class ShapeModel>
279 {
280  return new Shape();
281 }
282 
283 
284 template <class Ui, class Shape, class ShapeModel>
286 {
287  m_menuButtonPtr = buttonPtr;
288 
289  if (buttonPtr == NULL){
290  return;
291  }
292 
293  if (!IsToolsMenuAllowed()){
294  buttonPtr->hide();
295  return;
296  }
297 
298  if (m_menuPtr == NULL){
299  m_menuPtr = new QMenu(buttonPtr);
300  QObject::connect(m_menuPtr, SIGNAL(triggered(QAction*)), this, SLOT(OnActionTriggered(QAction*)));
301 
302  QPushButton* pushButtonPtr = dynamic_cast<QPushButton*>(buttonPtr);
303  if (pushButtonPtr != NULL){
304  pushButtonPtr->setMenu(m_menuPtr);
305  }
306  else{
307  QToolButton* toolButtonPtr = dynamic_cast<QToolButton*>(buttonPtr);
308  if (toolButtonPtr != NULL){
309  toolButtonPtr->setMenu(m_menuPtr);
310  }
311  }
312  }
313 }
314 
315 
316 template <class Ui, class Shape, class ShapeModel>
318 {
319  return true;
320 }
321 
322 
323 template <class Ui, class Shape, class ShapeModel>
325 {
326  if (m_menuPtr != NULL){
327  m_menuPtr->clear();
328 
329  if (PopulateActions(*m_menuPtr, modelPtr)){
330  m_menuButtonPtr->setEnabled(true);
331  }
332  else
333  m_menuButtonPtr->setEnabled(false);
334  }
335 
336  QToolBar* toolBarPtr = GetToolBar();
337  if (toolBarPtr != NULL){
338  // get actual toolbar actions
339  QSet<QAction*> oldActions = toolBarPtr->actions().toSet();
340 
341  if (PopulateActions(*toolBarPtr, modelPtr)){
342  // store added actions
343  QSet<QAction*> newActions = toolBarPtr->actions().toSet();
344  m_toolBarActions = newActions - oldActions;
345 
346  toolBarPtr->setVisible(true);
347 
348  // connect toolbar signals ONLY if there is no menu
349  // because of a Qt issue, an action added to both of menu and toolbar will emit actionTriggered() signal twice :(
350  if (m_menuPtr == NULL){
351  QObject::connect(toolBarPtr, SIGNAL(actionTriggered(QAction*)), this, SLOT(OnActionTriggered(QAction*)));
352  }
353  }
354  else{
355  toolBarPtr->disconnect();
356  }
357  }
358 }
359 
360 
361 template <class Ui, class Shape, class ShapeModel>
363 {
364  if (m_menuButtonPtr != NULL){
365  m_menuButtonPtr->setEnabled(false);
366  }
367 
368  QToolBar* toolBarPtr = GetToolBar();
369  if (toolBarPtr){
370  toolBarPtr->disconnect();
371 
372  // depopulate stored actions
373  for (Actions::ConstIterator iter = m_toolBarActions.constBegin(); iter != m_toolBarActions.constEnd(); ++iter){
374  toolBarPtr->removeAction(*iter);
375  }
376 
377  m_toolBarActions.clear();
378  }
379 }
380 
381 
382 // reimplemented (iqt2d::TShapeParamsGuiCompBase)
383 
384 template <class Ui, class Shape, class ShapeModel>
386 {
387 }
388 
389 
390 // reimplemented (iqt2d::TViewExtenderCompBase)
391 
392 template <class Ui, class Shape, class ShapeModel>
394 {
395  iview::IShape* shapePtr = CreateShape(BaseClass::GetObservedObject(), true);
396  if (shapePtr != NULL){
397  result.PushBack(shapePtr);
398  }
399 }
400 
401 
402 // reimplemented (iqtgui::CGuiComponentBase)
403 
404 template <class Ui, class Shape, class ShapeModel>
406 {
407  m_menuButtonPtr = NULL;
408  if (m_menuPtr != NULL){
409  m_menuPtr->deleteLater();
410 
411  m_menuPtr = NULL;
412  }
413 
414  BaseClass::OnGuiDestroyed();
415 }
416 
417 
418 template <class Ui, class Shape, class ShapeModel>
420 {
421  BaseClass::OnGuiShown();
422 
423  OnModelAttachedAndGuiShown(BaseClass::GetObservedModel());
424 
425  if (*m_hideParameterEditorAttrPtr){
426  GetQtWidget()->hide();
427  }
428 }
429 
430 
431 template <class Ui, class Shape, class ShapeModel>
433 {
434  OnModelDetachedOrGuiHidden(BaseClass::GetObservedModel());
435 
436  BaseClass::OnGuiHidden();
437 }
438 
439 
440 // reimplemented (iqtgui::TGuiObserverWrap)
441 
442 template <class Ui, class Shape, class ShapeModel>
444 {
445  BaseClass::OnGuiModelAttached();
446 
447  if (BaseClass::IsGuiShown()){
448  OnModelAttachedAndGuiShown(BaseClass::GetObservedModel());
449  }
450 
451  m_fixedPositionParamPtr.EnsureInitialized();
452 }
453 
454 
455 template <class Ui, class Shape, class ShapeModel>
457 {
458  OnModelDetachedOrGuiHidden(BaseClass::GetObservedModel());
459 
460  BaseClass::OnGuiModelDetached();
461 }
462 
463 
464 } // namespace iview
465 
466 
467 #endif // !iview_TShapeParamsGuiCompBase_included
468 
469 
virtual void OnModelAttachedAndGuiShown(imod::IModel *modelPtr)
Common interface to get information about a measurement unit.
Definition: IUnitInfo.h:21
Pointer * GetAt(int index) const
Get pointer at specified index.
virtual void SetEditablePosition(bool state=true)
Common interface for an object, which delivers a 2D-calibration.
virtual void SetUserColorSchema(const IColorSchema *schemaPtr)
Set color schema to draw shape.
virtual void OnGuiShown()
Called from widget event filter when slave widget is shown.
virtual bool IsAttached(const IObserver *observerPtr) const =0
Returns true if observer observer is attached to this model object.
virtual void OnGuiHidden()
Called from widget event filter when slave widget is hidden.
virtual void OnActionTriggered(QAction *actionPtr)
virtual bool AssignToLayer(int layerType)
Assign this shape to same layer.
virtual void OnGuiModelAttached()
Called when model is attached and GUI is created.
void PushBack(const ElementType &element)
Add new element at the end of collection.
virtual const imath::IUnitInfo * GetArgumentUnitInfo() const =0
Get unit description of calibration input (transformation function argument).
virtual void SetVisible(bool state=true)
Make shape to be visible or not.
virtual bool OnModelAttached(imod::IModel *modelPtr, istd::IChangeable::ChangeSet &changeMask)
This call back function will be called, if an observable object is about to be attached.
Common interface for data model objects, which can be changed.
Definition: IChangeable.h:24
Interface for objects which can be enabled/disabled.
virtual void OnGuiModelDetached()
Called when model is detached or GUI is destroyed.
int GetCount() const
Get number of stored elements.
virtual iview::CInteractiveShapeBase * CreateShapeInstance() const
Simple creation of shape instance.
virtual void CreateShapes(int sceneId, Shapes &result)
Create shapes for the view.
virtual QString GetUnitName() const =0
Get name of unit.
virtual bool OnModelDetached(imod::IModel *modelPtr)
This call back function will be called, if an observable object is about to be detached.
Common interface for all display console shapes.
Definition: IShape.h:27
virtual iview::IShape * CreateShape(const istd::IChangeable *objectPtr, bool connectToModel=false) const
Create a graphical representation of the given 2D-object object.
virtual void OnGuiDestroyed()
Called just before GUI is released.
Common interface for GUI objects using in component context.
Definition: IGuiObject.h:25
Implementation of a pointer container, which controls the live cycle of the pointer object...
#define NULL
Definition: istd.h:64
Interface for creation of visualization shapes of some geometrical object.
Definition: IShapeFactory.h:18
virtual bool AttachObserver(IObserver *observerPtr)=0
Attaches model object to observer observerPtr.
virtual void CreateToolsMenu(QAbstractButton *buttonPtr)
Maintenance of the tools actions.
Common interface for model objects, that supports Model/Observer design pattern.
Definition: IModel.h:23
Set of change flags (its IDs).
Definition: IChangeable.h:30
virtual bool PopulateActions(QWidget &host, imod::IModel *modelPtr)
Defines set of standard pens, brushes and simple management of unions.
Definition: IColorSchema.h:28
iqt2d::TViewExtenderCompBase< iqtgui::TDesignerGuiObserverCompBase< Ui, ShapeModel > > BaseClass
virtual void OnModelDetachedOrGuiHidden(imod::IModel *modelPtr)
virtual const ICalibration2d * GetCalibration() const =0
Get access to transformation object, which transforms a local logical coordinate system to global one...
virtual void SetDefaultDescription(const QString &description)
Set default description will be used to display on console.
Layer with inactive shapes.
Definition: IViewLayer.h:44
virtual void DetachObserver(IObserver *observerPtr)=0
Detaches model object from observer observerPtr.

© 2007-2017 Witold Gantzke and Kirill Lepskiy