ACF $AcfVersion:0$
TShapeParamsGuiCompBase.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// Qt includes
6#include <QtCore/QtGlobal>
7#include <QtWidgets/QMenu>
8#include <QtWidgets/QPushButton>
9#include <QtWidgets/QToolButton>
10#include <QtWidgets/QComboBox>
11#include <QtWidgets/QToolBar>
12#include <QtWidgets/QAbstractItemView>
13#include <QtWidgets/QScrollArea>
14
15// ACF includes
16#include <i2d/ICalibration2d.h>
19#include <iview/IColorSchema.h>
20#include <iview/IShapeFactory.h>
24
25
26namespace iqt2d
27{
28
29
30template <class Ui, class Shape, class ShapeModel>
33 iqtgui::TWheelFocusEvent<
34 iqtgui::TDesignerGuiObserverCompBase<Ui, ShapeModel> > >,
35 virtual public iview::IShapeFactory
36{
37public:
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_END_COMPONENT;
52
54
55 // reimplemented (imod::IObserver)
56 virtual bool OnModelAttached(imod::IModel* modelPtr, istd::IChangeable::ChangeSet& changeMask) override;
57 virtual bool OnModelDetached(imod::IModel* modelPtr) override;
58
59 // reimplemented (iview::IShapeFactory)
60 virtual iview::IShape* CreateShape(const istd::IChangeable* objectPtr, bool connectToModel = false) const override;
61
62protected:
63 typedef typename BaseClass::Shapes Shapes;
65
66 QString GetUnitName() const;
67 bool IsPositionFixed() const;
68 bool IsToolsMenuAllowed() const;
69 QToolBar* GetToolBar() const;
70
74 virtual void CreateToolsMenu(QAbstractButton* buttonPtr);
75 virtual bool PopulateActions(QWidget& host, imod::IModel* modelPtr);
76 virtual void OnModelAttachedAndGuiShown(imod::IModel* modelPtr);
77 virtual void OnModelDetachedOrGuiHidden(imod::IModel* modelPtr);
78 virtual void OnActionTriggered(QAction* actionPtr);
79
84
85 // reimplemented (iqt2d::TViewExtenderCompBase)
86 virtual void CreateShapes(int sceneId, Shapes& result) override;
87
88 // reimplemented (iqtgui::CGuiComponentBase)
89 virtual void OnGuiDestroyed() override;
90 virtual void OnGuiShown() override;
91 virtual void OnGuiHidden() override;
92
93 // reimplemented (iqtgui::TGuiObserverWrap)
94 virtual void OnGuiModelAttached() override;
95 virtual void OnGuiModelDetached() override;
96
97private:
98 QMenu* m_menuPtr;
99 QAbstractButton* m_menuButtonPtr;
100
101 typedef QSet<QAction*> Actions;
102 Actions m_toolBarActions;
103
104 I_REF(imath::IUnitInfo, m_defaultUnitInfoCompPtr);
105 I_REF(iview::IColorSchema, m_colorSchemaCompPtr);
106 I_REF(iprm::IEnableableParam, m_fixedPositionParamPtr);
107 I_ATTR(bool, m_fixedPositionAttrPtr);
108 I_ATTR(bool, m_allowToolsMenuAttrPtr);
109 I_REF(iqtgui::IGuiObject, m_toolBarGuiCompPtr);
110 I_TEXTATTR(m_shapeToolTipAttrPtr);
111};
112
113
114// public methods
115
116template <class Ui, class Shape, class ShapeModel>
122
123
124// reimplemented (imod::IObserver)
125
126template <class Ui, class Shape, class ShapeModel>
128{
129 if (BaseClass::OnModelAttached(modelPtr, changeMask)){
130 const ShapesMap& shapesMap = BaseClass::GetShapesMap();
131 for ( typename ShapesMap::const_iterator iter = shapesMap.begin();
132 iter != shapesMap.end();
133 ++iter){
134 const Shapes& shapes = iter.value();
135 int shapesCount = shapes.GetCount();
136 for (int shapeIndex = 0; shapeIndex < shapesCount; ++shapeIndex){
137 Shape* shapePtr = dynamic_cast<Shape*>(shapes.GetAt(shapeIndex));
138 if (shapePtr != NULL){
139 if (modelPtr->AttachObserver(shapePtr)){
140 shapePtr->SetVisible(true);
141 }
142 }
143 }
144 }
145
146 return true;
147 }
148 else{
149 return false;
150 }
151}
152
153
154template <class Ui, class Shape, class ShapeModel>
156{
157 if (BaseClass::OnModelDetached(modelPtr)){
158 const ShapesMap& shapesMap = BaseClass::GetShapesMap();
159 for ( typename ShapesMap::const_iterator iter = shapesMap.begin();
160 iter != shapesMap.end();
161 ++iter){
162 const Shapes& shapes = iter.value();
163 int shapesCount = shapes.GetCount();
164 for (int shapeIndex = 0; shapeIndex < shapesCount; ++shapeIndex){
165 Shape* shapePtr = dynamic_cast<Shape*>(shapes.GetAt(shapeIndex));
166 if (shapePtr != NULL){
167 if (modelPtr->IsAttached(shapePtr)){
168 modelPtr->DetachObserver(shapePtr);
169 }
170
171 shapePtr->SetVisible(false);
172 }
173 }
174 }
175
176 return true;
177 }
178 else{
179 return false;
180 }
181}
182
183
184// reimplemented (iview::IShapeFactory)
185
186template <class Ui, class Shape, class ShapeModel>
188{
189 iview::CInteractiveShapeBase* shapePtr = CreateShapeInstance();
190 if (shapePtr != NULL){
191 shapePtr->SetEditablePosition(!IsPositionFixed());
192
193 if (m_colorSchemaCompPtr.IsValid()){
194 shapePtr->SetUserColorSchema(m_colorSchemaCompPtr.GetPtr());
195 }
196
197 if (m_shapeToolTipAttrPtr.IsValid()){
198 shapePtr->SetDefaultDescription(*m_shapeToolTipAttrPtr);
199 }
200
201 if (BaseClass::IsReadOnly()){
203 }
204
205 if (connectToModel){
206 imod::IModel* modelPtr = dynamic_cast<imod::IModel*>(const_cast<istd::IChangeable*>(objectPtr));
207 if (modelPtr != NULL){
208 if (modelPtr->AttachObserver(shapePtr)){
209 shapePtr->SetVisible(true);
210 }
211 }
212 }
213 }
214
215 return shapePtr;
216}
217
218
219// protected methods
220
221template <class Ui, class Shape, class ShapeModel>
223{
224 if (m_fixedPositionParamPtr.IsValid()){
225 return m_fixedPositionParamPtr->IsEnabled();
226 }
227
228 return *m_fixedPositionAttrPtr;
229}
230
231
232template <class Ui, class Shape, class ShapeModel>
234{
235 return *m_allowToolsMenuAttrPtr;
236}
237
238
239template <class Ui, class Shape, class ShapeModel>
241{
242 return m_toolBarGuiCompPtr.IsValid() ? dynamic_cast<QToolBar*>(m_toolBarGuiCompPtr->GetWidget()) : NULL;
243}
244
245
246template <class Ui, class Shape, class ShapeModel>
248{
249 const imath::IUnitInfo* unitInfoPtr = NULL;
250
251 const ShapeModel* objectPtr = BaseClass::GetObservedObject();
252 if (objectPtr != NULL){
253 const i2d::ICalibrationProvider* calibrationProviderPtr = dynamic_cast<const i2d::ICalibrationProvider*>(objectPtr);
254 if (calibrationProviderPtr != NULL){
255 const i2d::ICalibration2d* calibrationPtr = calibrationProviderPtr->GetCalibration();
256 if (calibrationPtr != NULL){
257 unitInfoPtr = calibrationPtr->GetArgumentUnitInfo();
258 }
259 }
260 }
261
262 if (unitInfoPtr != NULL){
263 return unitInfoPtr->GetUnitName();
264 }
265
266 if (m_defaultUnitInfoCompPtr.IsValid()){
267 return m_defaultUnitInfoCompPtr->GetUnitName();
268 }
269
270 return QString();
271}
272
273
274template <class Ui, class Shape, class ShapeModel>
279
280
281template <class Ui, class Shape, class ShapeModel>
283{
284 m_menuButtonPtr = buttonPtr;
285
286 if (buttonPtr == NULL){
287 return;
288 }
289
290 if (!IsToolsMenuAllowed()){
291 buttonPtr->hide();
292 return;
293 }
294
295 if (m_menuPtr == NULL){
296 m_menuPtr = new QMenu(buttonPtr);
297 QObject::connect(m_menuPtr, SIGNAL(triggered(QAction*)), this, SLOT(OnActionTriggered(QAction*)));
298
299 QPushButton* pushButtonPtr = dynamic_cast<QPushButton*>(buttonPtr);
300 if (pushButtonPtr != NULL){
301 pushButtonPtr->setMenu(m_menuPtr);
302 }
303 else{
304 QToolButton* toolButtonPtr = dynamic_cast<QToolButton*>(buttonPtr);
305 if (toolButtonPtr != NULL){
306 toolButtonPtr->setMenu(m_menuPtr);
307 }
308 }
309 }
310}
311
312
313template <class Ui, class Shape, class ShapeModel>
315{
316 return true;
317}
318
319
320template <class Ui, class Shape, class ShapeModel>
322{
323 if (m_menuPtr != NULL){
324 m_menuPtr->clear();
325
326 if (PopulateActions(*m_menuPtr, modelPtr)){
327 m_menuButtonPtr->setEnabled(true);
328 }
329 else
330 m_menuButtonPtr->setEnabled(false);
331 }
332
333 QToolBar* toolBarPtr = GetToolBar();
334 if (toolBarPtr != NULL){
335 // get actual toolbar actions
336 QList<QAction*> actions = toolBarPtr->actions();
337
338#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
339 QSet<QAction*> oldActions(actions.begin(), actions.end());
340#else
341 QSet<QAction*> oldActions = actions.toSet();
342#endif
343
344 if (PopulateActions(*toolBarPtr, modelPtr)){
345 // store added actions
346 actions = toolBarPtr->actions();
347
348#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
349 QSet<QAction*> newActions(actions.begin(), actions.end());
350#else
351 QSet<QAction*> newActions = actions.toSet();
352#endif
353
354 m_toolBarActions = newActions - oldActions;
355
356 toolBarPtr->setVisible(true);
357
358 // connect toolbar signals ONLY if there is no menu
359 // because of a Qt issue, an action added to both of menu and toolbar will emit actionTriggered() signal twice :(
360 if (m_menuPtr == NULL){
361 QObject::connect(toolBarPtr, SIGNAL(actionTriggered(QAction*)), this, SLOT(OnActionTriggered(QAction*)));
362 }
363 }
364 else{
365 toolBarPtr->disconnect();
366 }
367 }
368}
369
370
371template <class Ui, class Shape, class ShapeModel>
373{
374 if (m_menuButtonPtr != NULL){
375 m_menuButtonPtr->setEnabled(false);
376 }
377
378 QToolBar* toolBarPtr = GetToolBar();
379 if (toolBarPtr){
380 toolBarPtr->disconnect();
381
382 // depopulate stored actions
383 for (Actions::ConstIterator iter = m_toolBarActions.constBegin(); iter != m_toolBarActions.constEnd(); ++iter){
384 toolBarPtr->removeAction(*iter);
385 }
386
387 m_toolBarActions.clear();
388 }
389}
390
391
392// reimplemented (iqt2d::TShapeParamsGuiCompBase)
393
394template <class Ui, class Shape, class ShapeModel>
398
399
400// reimplemented (iqt2d::TViewExtenderCompBase)
401
402template <class Ui, class Shape, class ShapeModel>
404{
405 iview::IShape* shapePtr = CreateShape(BaseClass::GetObservedObject(), true);
406 if (shapePtr != NULL){
407 result.PushBack(shapePtr);
408 }
409}
410
411
412// reimplemented (iqtgui::CGuiComponentBase)
413
414template <class Ui, class Shape, class ShapeModel>
416{
417 m_menuButtonPtr = NULL;
418 if (m_menuPtr != NULL){
419 m_menuPtr->deleteLater();
420
421 m_menuPtr = NULL;
422 }
423
424 BaseClass::OnGuiDestroyed();
425}
426
427
428template <class Ui, class Shape, class ShapeModel>
430{
431 BaseClass::OnGuiShown();
432
433 OnModelAttachedAndGuiShown(BaseClass::GetObservedModel());
434}
435
436
437template <class Ui, class Shape, class ShapeModel>
439{
440 OnModelDetachedOrGuiHidden(BaseClass::GetObservedModel());
441
442 BaseClass::OnGuiHidden();
443}
444
445
446// reimplemented (iqtgui::TGuiObserverWrap)
447
448template <class Ui, class Shape, class ShapeModel>
450{
451 BaseClass::OnGuiModelAttached();
452
453 if (BaseClass::IsGuiShown()){
454 OnModelAttachedAndGuiShown(BaseClass::GetObservedModel());
455 }
456
457 m_fixedPositionParamPtr.EnsureInitialized();
458}
459
460
461template <class Ui, class Shape, class ShapeModel>
463{
464 OnModelDetachedOrGuiHidden(BaseClass::GetObservedModel());
465
466 BaseClass::OnGuiModelDetached();
467}
468
469
470} // namespace iview
471
Interface for 2D calibration and coordinate transformation with unit information.
virtual const imath::IUnitInfo * GetArgumentUnitInfo() const =0
Get unit description of calibration input (transformation function argument).
Common interface for an object, which delivers a 2D-calibration.
virtual const ICalibration2d * GetCalibration() const =0
Get access to transformation object, which transforms a local logical coordinate system to global one...
Common interface to get information about a measurement unit.
Definition IUnitInfo.h:22
virtual QString GetUnitName() const =0
Get name of unit.
Common interface for model objects, that supports Model/Observer design pattern.
Definition IModel.h:25
virtual void DetachObserver(IObserver *observerPtr)=0
Detaches model object from observer observerPtr.
virtual bool AttachObserver(IObserver *observerPtr)=0
Attaches model object to observer observerPtr.
virtual bool IsAttached(const IObserver *observerPtr) const =0
Returns true if observer observer is attached to this model object.
Interface for objects which can be enabled/disabled.
virtual void OnModelAttachedAndGuiShown(imod::IModel *modelPtr)
virtual void CreateShapes(int sceneId, Shapes &result) override
virtual void CreateToolsMenu(QAbstractButton *buttonPtr)
Maintenance of the tools actions.
virtual bool PopulateActions(QWidget &host, imod::IModel *modelPtr)
virtual void OnGuiModelAttached() override
virtual void OnGuiModelDetached() override
iqt2d::TViewExtenderCompBase< iqtgui::TWheelFocusEvent< iqtgui::TDesignerGuiObserverCompBase< Ui, ShapeModel > > > BaseClass
virtual iview::CInteractiveShapeBase * CreateShapeInstance() const
Simple creation of shape instance.
virtual iview::IShape * CreateShape(const istd::IChangeable *objectPtr, bool connectToModel=false) const override
Create a graphical representation of the given 2D-object object.
virtual void OnModelDetachedOrGuiHidden(imod::IModel *modelPtr)
virtual bool OnModelAttached(imod::IModel *modelPtr, istd::IChangeable::ChangeSet &changeMask) override
virtual bool OnModelDetached(imod::IModel *modelPtr) override
virtual void OnActionTriggered(QAction *actionPtr)
Common interface for GUI objects used in ACF component context.
Definition IGuiObject.h:133
Base class for all Qt GUI components.
Set of change flags (its IDs).
Definition IChangeable.h:36
Common interface for data model objects, which can be changed.
Definition IChangeable.h:28
virtual void SetEditablePosition(bool state=true)
virtual void SetVisible(bool state=true) override
Make shape to be visible or not.
virtual void SetUserColorSchema(const IColorSchema *schemaPtr) override
Set color schema to draw shape.
virtual void SetDefaultDescription(const QString &description) override
Set default description will be used to display on console.
virtual bool AssignToLayer(int layerType)
Assign this shape to same layer.
Defines set of standard pens, brushes and simple management of unions.
Interface for creation of visualization shapes of some geometrical object.
Common interface for all display console shapes.
Definition IShape.h:32
@ LT_INACTIVE
Layer with inactive shapes.
Definition IViewLayer.h:44
#define I_TEXTATTR(member)
Declare translatable text attribute member.
#define NULL
Definition istd.h:74
This package contains Qt based implementations for 2D graphic objects.