Salome HOME
5b4610878a9583fafece39a7864369c4ccd770fc
[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 #include <AIS_Shape.hxx>
20
21 #include <Quantity_Color.hxx>
22
23 #include <QIcon>
24 #include <QPixmap>
25 #include <QLocale>
26
27 #include <map>
28
29 class Handle_AIS_InteractiveContext;
30
31 class QWidget;
32 class QLayout;
33 class QDoubleSpinBox;
34 class QAction;
35 class ModuleBase_ParamIntSpinBox;
36 class ModuleBase_ParamSpinBox;
37 class ModuleBase_IWorkshop;
38
39 class GeomAPI_Shape;
40
41 class Events_InfoMessage;
42
43 namespace ModuleBase_Tools {
44
45 /// Methods to adjust margins and spacings.
46 MODULEBASE_EXPORT void adjustMargins(QWidget* theWidget);
47 MODULEBASE_EXPORT void adjustMargins(QLayout* theLayout);
48
49 MODULEBASE_EXPORT void zeroMargins(QWidget* theWidget);
50 MODULEBASE_EXPORT void zeroMargins(QLayout* theLayout);
51
52 /// Calls the same-named Qt method for the given widget.
53 /// It sets the top-level widget containing this widget to be the active window.
54 /// An active window is a visible top-level window that has the keyboard input focus.
55 /// \param theWidget a widget to be activated
56 /// \param theIndo a debug information
57 MODULEBASE_EXPORT void activateWindow(QWidget* theWidget, const QString& theInfo = QString());
58
59 /// Calls the same-named Qt method for the given widget.
60 /// Gives the keyboard input focus to this widget (or its focus proxy) if this widget or
61 /// one of its parents is the active window.
62 /// \param theWidget a widget to be activated
63 /// \param theIndo a debug information
64 MODULEBASE_EXPORT void setFocus(QWidget* theWidget, const QString& theInfo = QString());
65
66
67 /// Sets or removes the shadow effect to the widget
68 /// \param theWidget a widget to be styled
69 /// \param isSetEffect if true, the shadow effect is set, overwise cleared
70 /// \return resulting pixmap
71 MODULEBASE_EXPORT void setShadowEffect(QWidget* theWidget, const bool isSetEffect);
72
73 /// \ingroup GUI
74 /// Methods to modify a resource pixmap
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.
89 /// If the factor is less than 100,
90 /// the return pixmap is darker. If the factor is 0 or negative, the return pixmap is unspecified.
91
92 /// \param resource text of the pixmap
93 /// \param theLighterValue a lighter factor
94 /// \return resulting pixmap
95 MODULEBASE_EXPORT QPixmap lighter(const QString& theIcon, const int theLighterValue = 200);
96
97 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
98 /// \param theSpin an X or Y coordinate widget
99 /// \param theValue a new value
100 MODULEBASE_EXPORT void setSpinValue(QDoubleSpinBox* theSpin, double theValue);
101
102 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
103 /// \param theSpin an X or Y coordinate widget
104 /// \param theValue a new value
105 MODULEBASE_EXPORT void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue);
106
107 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
108 /// \param theSpin an ModuleBase_ParamSpinBox that accepts text
109 /// \param theText a new value
110 MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText);
111
112 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
113 /// \param theSpin a ModuleBase_ParamIntSpinBox object
114 /// \param theValue a new value
115 MODULEBASE_EXPORT void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue);
116
117 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
118 /// \param theSpin a SpinBox that accepts text
119 /// \param theText a new value
120 MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText);
121
122 /// Creates an action filled with the parameters
123 /// \param theIcon an action icon
124 /// \param theText an icon text
125 /// \param theParent an action object
126 /// \param theReceiver an object to process action triggered signal
127 /// \param theMember a slot to process the action triggered signal
128 /// \param theToolTip an action tool tip information, if empty the text is used
129 /// \param theStatusTip an action status tip information, if empty the text is used
130 MODULEBASE_EXPORT QAction* createAction(const QIcon& theIcon, const QString& theText,
131                                         QObject* theParent, const QObject* theReceiver = 0,
132                                         const char* theMember = "",
133                                         const QString& theToolTip = QString(),
134                                         const QString& theStatusTip = QString());
135
136 #ifdef _DEBUG
137 /// Converts the object to the feature or a result and generate information string
138 /// \param theObj an object
139 /// \param isUseAttributesInfo a flag whether the attribute values information is used
140 /// \return a string
141 MODULEBASE_EXPORT QString objectInfo(const ObjectPtr& theObj,
142                                      const bool isUseAttributesInfo = false);
143 #endif
144
145 /// Converts string value (name of shape type) to shape enum value
146 /// \param theType - string with shape type name
147 /// \return TopAbs_ShapeEnum value
148 MODULEBASE_EXPORT int shapeType(const QString& theType);
149
150 /// Checks whether the object is a sub result. It gets the feature of the object,
151 /// obtains all results of the feature and check if the object is a sub result
152 /// \return boolean result
153 MODULEBASE_EXPORT bool isSubResult(ObjectPtr theObject);
154
155 /// Check types of objects which are in the given list
156 /// \param theObjects the list of objects
157 /// \param hasResult will be set to true if list contains Result objects
158 /// \param hasFeature will be set to true if list contains Feature objects
159 /// \param hasParameter will be set to true if list contains Parameter objects
160 /// \param hasCompositeOwner will be set to true if list contains Sub-Feature objects
161 MODULEBASE_EXPORT void checkObjects(const QObjectPtrList& theObjects, bool& hasResult,
162                            bool& hasFeature, bool& hasParameter, bool& hasCompositeOwner);
163
164 /// Sets the default coeffient into the driver calculated accordingly the shape type.
165 /// It provides 1.e-4 for results of construction type
166 /// \param theResult a result object to define the deviation coeffient
167 /// \param theDrawer a drawer
168 //MODULEBASE_EXPORT void setDefaultDeviationCoefficient(
169 //                           const std::shared_ptr<ModelAPI_Result>& theResult,
170 //                           const Handle(Prs3d_Drawer)& theDrawer);
171
172 /// Sets the default coeffient into the driver calculated accordingly the shape type.
173 /// It provides 1.e-4 for a shape withe Edge shape type
174 /// \param theShape a shape to define the deviation coeffient,
175 /// \param theDrawer a drawer
176 MODULEBASE_EXPORT void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
177                                                       const Handle(Prs3d_Drawer)& theDrawer);
178
179 /// Obtains the color from the property manager and converts it to the OCCT color
180 /// \param theSection a property section
181 /// \param theName a property item name
182 /// \param theDefault a default color value
183 /// \return quantity color
184 MODULEBASE_EXPORT Quantity_Color color(const std::string& theSection,
185                                        const std::string& theName,
186                                        const std::string& theDefault);
187
188 /// Returns the object from the attribute
189 /// \param theObj an object
190 MODULEBASE_EXPORT ObjectPtr getObject(const AttributePtr& theAttribute);
191
192 /// Returns the object from the attribute
193 /// \param theObj an object
194 MODULEBASE_EXPORT TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape);
195
196 /// Returns list of parameters accessible in the active part and partset
197 /// \theParameters a list of parameter names
198 MODULEBASE_EXPORT void getParameters(QStringList& theParameters);
199
200 /// Returns list of parameters accessible in the active part and partset
201 /// \theParameters a list of parameter names
202 MODULEBASE_EXPORT std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
203                                                  const FeaturePtr& theFeature);
204
205 /// Set the object to the attribute depending on the attribute type. If it is a list,
206 /// the values are appended if they are not in the list yet.
207 /// \param theAttribute an attribute where the object and shape are set
208 /// \param theObject an object
209 /// \param theShape a shape
210 /// \param theWorkshop to find an attribute for the given shape for attribute reference
211 /// \param theTemporarily if it is true, do not store and name the added in the data framework
212 ///        It is useful for attribute selection
213 MODULEBASE_EXPORT bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
214                                  const std::shared_ptr<GeomAPI_Shape>& theShape,
215                                  ModuleBase_IWorkshop* theWorkshop,
216                                  const bool theTemporarily);
217
218 /// Set the object to the attribute depending on the attribute type. If it is a list,
219 /// the values are appended if they are not in the list yet.
220 /// \param theAttribute an attribute where the object and shape are set
221 /// \param theObject an object
222 /// \param theShape a shape
223 /// \param theWorkshop to find an attribute for the given shape for attribute reference
224 /// \param theTemporarily if it is true, do not store and name the added in the data framework
225 /// \param theCheckIfAttributeHasObject if it is true, the check isInList is called
226 ///        It is useful for attribute selection
227 /// \return true if the attribute is filled with the given parameters
228 MODULEBASE_EXPORT bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
229                                  const std::shared_ptr<GeomAPI_Shape>& theShape,
230                                  ModuleBase_IWorkshop* theWorkshop,
231                                  const bool theTemporarily,
232                                  const bool theCheckIfAttributeHasObject);
233
234 /// Returns the shape of the attribute. If the attribute is AttributeRefAttrPtr, the shape is found
235 /// using current module of the given workshop.
236 /// \param theAttribute an attribute where the shape is set
237 /// \param theWorkshop to find a shape for the given attribute
238 MODULEBASE_EXPORT std::shared_ptr<GeomAPI_Shape> getShape(const AttributePtr& theAttribute,
239                                                           ModuleBase_IWorkshop* theWorkshop);
240
241 /// Flush updated signal for the object. The viewer update is blocked in the process
242 /// \param theObject parameter of the signal
243 MODULEBASE_EXPORT void flushUpdated(ObjectPtr theObject);
244
245 /// Sends a message about block/unblock viewer updating
246 /// \param theValue a boolean value
247 MODULEBASE_EXPORT void blockUpdateViewer(const bool theValue);
248
249 /// Generates a wrapped string to be less than value with '\n' separators
250 /// \param theValue a boolean value
251 /// \param theWidget a widget to know the font
252 /// \param theMaxLineInPixels a maximum line width in pixels
253 MODULEBASE_EXPORT QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
254                                              int theMaxLineInPixels = 150);
255
256 /// Generates a locale to disable thousands separator for spin box
257 /// (to avoid inconsistency of double-2-string and string-2-double conversion)
258 /// \return locale
259 MODULEBASE_EXPORT QLocale doubleLocale();
260
261 /// Returns a container of referenced feature to the current object in the object document.
262 /// \param theObject an object, which will be casted to a feature type
263 /// \param theRefFeatures an output container
264 void MODULEBASE_EXPORT refsToFeatureInFeatureDocument(const ObjectPtr& theObject,
265                                                       std::set<FeaturePtr>& theRefFeatures);
266
267
268 /// Returns true if the object if a sub child of the feature. The feature is casted to the
269 /// composite one. If it is possible, the sub object check happens. The method is applyed
270 /// recursively to the feature subs.
271 /// \param theObject a candidate to be a sub object
272 /// \param theFeature a candidate to be a composite feature
273 /// \return a boolean value
274 //bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature);
275
276
277 /// Returns true if the result is a sub object of some composite object
278 /// \param theObject a result object
279 /// \returns boolean value
280 //bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject);
281
282
283 /// Returns first result of the feature: the object itself if it is a result of
284 /// first result of the object's feature
285 /// \param theObject an object
286 /// \return first result or NULL
287 std::shared_ptr<ModelAPI_Result> MODULEBASE_EXPORT firstResult(const ObjectPtr& theObject);
288
289 /// Returns true if the list contains at least one feature from the module document(PartSet)
290 /// The Part features are skipped.
291 /// \param theFeatures a list of features
292 /// \bool true if it is found
293 bool MODULEBASE_EXPORT hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures);
294
295 /// Shows a dialog box about references. Ask whether they should be also removed.
296 /// \param theFeatures a list of features
297 /// \param theReferences a map of all references to the features
298 /// \param theParent a parent widget for the question message box
299 /// \param theReferencesToDelete an out set for references features to be removed
300 /// \return true if in message box answer is Yes
301 bool MODULEBASE_EXPORT askToDelete(const std::set<FeaturePtr> aFeatures,
302                                    const std::map<FeaturePtr,
303                                    std::set<FeaturePtr> >& theReferences,
304                                    QWidget* theParent,
305                                    std::set<FeaturePtr>& theReferencesToDelete,
306                                    const std::string& thePrefixInfo = "");
307
308 /// Converts a list of objects to set of corresponded features. If object is result, it is ingored
309 /// because the feauture only might be removed. But if result is in a parameter group, the feature
310 /// of this parameter is to be removed
311 /// \param theObjects a list of objects
312 /// \param theFeatures an out conteiner of features
313 void MODULEBASE_EXPORT convertToFeatures(const QObjectPtrList& theObjects,
314                                          std::set<FeaturePtr>& theFeatures);
315
316
317 /// Returns translation from the given data.
318 /// If translation is not exists then it returns a string
319 /// from the info data without translation
320 /// \param theMessage a message which dave to be translated
321 QString MODULEBASE_EXPORT translate(const Events_InfoMessage& theMessage);
322
323 /// Returns translation from the given data.
324 /// If translation is not exists then it returns a string
325 /// from the info data without translation
326 /// \param theContext context of the message (Feature Id)
327 /// \param theMessage a message which dave to be translated
328 QString MODULEBASE_EXPORT translate(const std::string& theContext, const std::string& theMessage);
329
330 /// Set Highlighting of points as a Ball shape
331 /// \param theAIS - the presentation
332 void MODULEBASE_EXPORT setPointBallHighlighting(AIS_Shape* theAIS);
333
334 }
335
336 #endif