Salome HOME
Issue #1489 Multi-rotation problem if there is a reference to copied objects
[modules/shaper.git] / src / ModuleBase / ModuleBase_Tools.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_Tools.h
4 // Created:     11 July 2014
5 // Author:      Vitaly Smetannikov
6
7 #ifndef ModuleBase_Tools_H
8 #define ModuleBase_Tools_H
9
10 #include "ModuleBase.h"
11 #include "ModuleBase_Definitions.h"
12
13 #include <ModelAPI_Attribute.h>
14 #include <ModelAPI_Feature.h>
15
16 #include <TopAbs_ShapeEnum.hxx>
17 #include <TopoDS_Shape.hxx>
18 #include <Prs3d_Drawer.hxx>
19
20 #include <Quantity_Color.hxx>
21
22 #include <QPixmap>
23
24 #include <map>
25
26 class QWidget;
27 class QLayout;
28 class QDoubleSpinBox;
29 class ModuleBase_ParamIntSpinBox;
30 class ModuleBase_ParamSpinBox;
31 class ModuleBase_IWorkshop;
32
33 class GeomAPI_Shape;
34
35 namespace ModuleBase_Tools {
36
37 /*
38  * Methods to adjust margins and spacings.
39  */
40 MODULEBASE_EXPORT void adjustMargins(QWidget* theWidget);
41 MODULEBASE_EXPORT void adjustMargins(QLayout* theLayout);
42
43 MODULEBASE_EXPORT void zeroMargins(QWidget* theWidget);
44 MODULEBASE_EXPORT void zeroMargins(QLayout* theLayout);
45
46 /*
47  * Calls the same-named Qt method for the given widget.
48  * It sets the top-level widget containing this widget to be the active window.
49  * An active window is a visible top-level window that has the keyboard input focus.
50  * \param theWidget a widget to be activated
51  * \param theIndo a debug information
52  */
53 MODULEBASE_EXPORT void activateWindow(QWidget* theWidget, const QString& theInfo = QString());
54
55 /*
56  * Calls the same-named Qt method for the given widget.
57  * Gives the keyboard input focus to this widget (or its focus proxy) if this widget or
58  * one of its parents is the active window.
59  * \param theWidget a widget to be activated
60  * \param theIndo a debug information
61  */
62 MODULEBASE_EXPORT void setFocus(QWidget* theWidget, const QString& theInfo = QString());
63
64
65 //! Sets or removes the shadow effect to the widget
66 //! \param theWidget a widget to be styled
67 //! \param isSetEffect if true, the shadow effect is set, overwise cleared
68 //! \return resulting pixmap
69 MODULEBASE_EXPORT void setShadowEffect(QWidget* theWidget, const bool isSetEffect);
70
71 /**
72  * \ingroup GUI
73  * Methods to modify a resource pixmap
74  */
75
76 //! Create composite pixmap. 
77 //! Pixmap \a theAdditionalIcon is drawn over pixmap \a dest with coordinates
78 //! specified relatively to the upper left corner of \a theIcon.
79
80 //! \param theAdditionalIcon resource text of the additional pixmap
81 //! \param theIcon resource text of the background pixmap
82 //! \return resulting pixmap
83 MODULEBASE_EXPORT QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon);
84
85 //! Generates the pixmap lighter than the resources pixmap. 
86 //! Pixmap \a theIcon is lighted according to the given value.
87 //! If the lighter value is greater than 100, this functions returns a lighter pixmap.
88 //! Setting lighter value to 150 returns a color that is 50% brighter. If the factor is less than 100,
89 //! the return pixmap is darker. If the factor is 0 or negative, the return pixmap is unspecified.
90
91 //! \param resource text of the pixmap
92 //! \param theLighterValue a lighter factor
93 //! \return resulting pixmap
94 MODULEBASE_EXPORT QPixmap lighter(const QString& theIcon, const int theLighterValue = 200);
95
96 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
97 /// \param theSpin an X or Y coordinate widget
98 /// \param theValue a new value
99 MODULEBASE_EXPORT void setSpinValue(QDoubleSpinBox* theSpin, double theValue);
100
101 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
102 /// \param theSpin an X or Y coordinate widget
103 /// \param theValue a new value
104 MODULEBASE_EXPORT void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue);
105
106 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
107 /// \param theSpin an ModuleBase_ParamSpinBox that accepts text
108 /// \param theText a new value
109 MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText);
110
111 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
112 /// \param theSpin a ModuleBase_ParamIntSpinBox object
113 /// \param theValue a new value
114 MODULEBASE_EXPORT void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue);
115
116 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
117 /// \param theSpin a SpinBox that accepts text
118 /// \param theText a new value
119 MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText);
120
121 /// Converts the object to the feature or a result and generate information string
122 /// \param theObj an object
123 /// \param isUseAttributesInfo a flag whether the attribute values information is used
124 /// \return a string
125 MODULEBASE_EXPORT QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo = false);
126
127 /// Converts string value (name of shape type) to shape enum value
128 /// \param theType - string with shape type name
129 /// \return TopAbs_ShapeEnum value
130 MODULEBASE_EXPORT TopAbs_ShapeEnum shapeType(const QString& theType);
131
132 /// Checks whether the object is a sub result. It gets the feature of the object,
133 /// obtains all results of the feature and check if the object is a sub result
134 /// \return boolean result
135 MODULEBASE_EXPORT bool isSubResult(ObjectPtr theObject);
136
137 /*!
138 Check types of objects which are in the given list
139 \param theObjects the list of objects
140 \param hasResult will be set to true if list contains Result objects
141 \param hasFeature will be set to true if list contains Feature objects
142 \param hasParameter will be set to true if list contains Parameter objects
143 \param hasCompositeOwner will be set to true if list contains Sub-Feature objects
144 */
145 MODULEBASE_EXPORT void checkObjects(const QObjectPtrList& theObjects, bool& hasResult,
146                            bool& hasFeature, bool& hasParameter, bool& hasCompositeOwner);
147
148 /*! Sets the default coeffient into the driver calculated accordingly the shape type.
149 It provides 1.e-4 for a shape withe Edge shape type
150 \param theShape a shape to define the deviation coeffient, 
151 \param theDrawer a drawer
152 */
153 MODULEBASE_EXPORT void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
154                                                       const Handle(Prs3d_Drawer)& theDrawer);
155
156 /*! Obtains the color from the property manager and converts it to the OCCT color
157 \param theSection a property section
158 \param theName a property item name
159 \param theDefault a default color value
160 \return quantity color
161 */
162 MODULEBASE_EXPORT Quantity_Color color(const std::string& theSection,
163                                        const std::string& theName,
164                                        const std::string& theDefault);
165
166
167 /// Returns the object from the attribute
168 /// \param theObj an object
169 MODULEBASE_EXPORT ObjectPtr getObject(const AttributePtr& theAttribute);
170
171 /// Returns the object from the attribute
172 /// \param theObj an object
173 MODULEBASE_EXPORT TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape);
174
175 /// Returns list of parameters accessible in the active part and partset
176 /// \theParameters a list of parameter names
177 MODULEBASE_EXPORT void getParameters(QStringList& theParameters);
178
179 /// Returns list of parameters accessible in the active part and partset
180 /// \theParameters a list of parameter names
181 MODULEBASE_EXPORT std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
182                                                  const FeaturePtr& theFeature);
183
184 /// Set the object to the attribute depending on the attribute type. If it is a list,
185 /// the values are appended if they are not in the list yet.
186 /// \param theAttribute an attribute where the object and shape are set
187 /// \param theObject an object
188 /// \param theShape a shape
189 /// \param theWorkshop to find an attribute for the given shape for attribute reference
190 /// \param theTemporarily if it is true, do not store and name the added in the data framework
191 ///        It is useful for attribute selection
192 MODULEBASE_EXPORT bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
193                                  const std::shared_ptr<GeomAPI_Shape>& theShape,
194                                  ModuleBase_IWorkshop* theWorkshop,
195                                  const bool theTemporarily);
196
197 /// Set the object to the attribute depending on the attribute type. If it is a list,
198 /// the values are appended if they are not in the list yet.
199 /// \param theAttribute an attribute where the object and shape are set
200 /// \param theObject an object
201 /// \param theShape a shape
202 /// \param theWorkshop to find an attribute for the given shape for attribute reference
203 /// \param theTemporarily if it is true, do not store and name the added in the data framework
204 /// \param theCheckIfAttributeHasObject if it is true, the check isInList is called
205 ///        It is useful for attribute selection
206 MODULEBASE_EXPORT void setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
207                                  const std::shared_ptr<GeomAPI_Shape>& theShape,
208                                  ModuleBase_IWorkshop* theWorkshop,
209                                  const bool theTemporarily,
210                                  const bool theCheckIfAttributeHasObject);
211
212 /// Returns the shape of the attribute. If the attribute is AttributeRefAttrPtr, the shape is found
213 /// using current module of the given workshop.
214 /// \param theAttribute an attribute where the shape is set
215 /// \param theWorkshop to find a shape for the given attribute
216 MODULEBASE_EXPORT std::shared_ptr<GeomAPI_Shape> getShape(const AttributePtr& theAttribute,
217                                                           ModuleBase_IWorkshop* theWorkshop);
218
219 /// Flush updated signal for the object. The viewer update is blocked in the process
220 /// \param theObject parameter of the signal
221 MODULEBASE_EXPORT void flushUpdated(ObjectPtr theObject);
222
223 /// Sends a message about block/unblock viewer updating
224 /// \param theValue a boolean value
225 MODULEBASE_EXPORT void blockUpdateViewer(const bool theValue);
226
227 /// Generates a wrapped string to be less than value with '\n' separators
228 /// \param theValue a boolean value
229 /// \param theWidget a widget to know the font
230 /// \param theMaxLineInPixels a maximum line width in pixels
231 MODULEBASE_EXPORT QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
232                                              int theMaxLineInPixels = 150);
233
234 /*!
235   Returns a container of referenced feature to the current object in the object document.
236   \param theObject an object, which will be casted to a feature type
237   \param theRefFeatures an output container
238  */
239 void MODULEBASE_EXPORT refsToFeatureInFeatureDocument(const ObjectPtr& theObject,
240                                                       std::set<FeaturePtr>& theRefFeatures);
241
242
243 /*!
244  Returns true if the object if a sub child of the feature. The feature is casted to the
245  composite one. If it is possible, the sub object check happens. The method is applyed
246  recursively to the feature subs.
247  \param theObject a candidate to be a sub object
248  \param theFeature a candidate to be a composite feature
249  \return a boolean value
250  */
251 bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature);
252
253
254 /*!
255 * Returns true if the result is a sub object of some composite object
256 * \param theObject a result object
257 * \returns boolean value
258 */
259 bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject);
260
261
262 /*!
263 * Shows a dialog box about references. Ask whether they should be also removed.
264 * \param theFeatures a list of features
265 * \param theReferences a map of all references to the features
266 * \param theParent a parent widget for the question message box
267 * \param theReferencesToDelete an out set for references features to be removed
268 * \return true if in message box answer is Yes
269 */
270 bool MODULEBASE_EXPORT askToDelete(const std::set<FeaturePtr> aFeatures,
271                                    const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
272                                    QWidget* theParent,
273                                    std::set<FeaturePtr>& theReferencesToDelete);
274
275 /*!
276 * Converts a list of objects to set of corresponded features. If object is result, it is ingored
277 * because the feauture only might be removed. But if result is in a parameter group, the feature
278 * of this parameter is to be removed
279 * \param theObjects a list of objects
280 * \param theFeatures an out conteiner of features
281 */
282 void MODULEBASE_EXPORT convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures);
283
284 }
285
286 #endif