Salome HOME
32eb35804e96388f415c5e8720eea92412a0720a
[modules/shaper.git] / src / ModuleBase / ModuleBase_Tools.h
1 // Copyright (C) 2014-2023  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef ModuleBase_Tools_H
21 #define ModuleBase_Tools_H
22
23 #include "ModuleBase.h"
24 #include "ModuleBase_Definitions.h"
25
26 #include <ModelAPI_Attribute.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_Folder.h>
29
30 #include <TopAbs_ShapeEnum.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <Prs3d_Drawer.hxx>
33 #include <AIS_Shape.hxx>
34
35 #include <Quantity_Color.hxx>
36
37 #include <QIcon>
38 #include <QPixmap>
39 #include <QLocale>
40
41 #include <map>
42
43 class QWidget;
44 class QLayout;
45 class QDoubleSpinBox;
46 class QAction;
47 class ModuleBase_IWorkshop;
48 class ModuleBase_ParamIntSpinBox;
49 class ModuleBase_ParamSpinBox;
50 class ModuleBase_ViewerPrs;
51
52 class GeomAPI_Shape;
53
54 class Events_InfoMessage;
55
56 namespace ModuleBase_Tools {
57
58 /// Methods to adjust margins and spacings.
59 MODULEBASE_EXPORT void adjustMargins(QWidget* theWidget);
60 MODULEBASE_EXPORT void adjustMargins(QLayout* theLayout);
61
62 MODULEBASE_EXPORT void zeroMargins(QWidget* theWidget);
63 MODULEBASE_EXPORT void zeroMargins(QLayout* theLayout);
64
65 /// Calls the same-named Qt method for the given widget.
66 /// It sets the top-level widget containing this widget to be the active window.
67 /// An active window is a visible top-level window that has the keyboard input focus.
68 /// \param theWidget a widget to be activated
69 /// \param theIndo a debug information
70 MODULEBASE_EXPORT void activateWindow(QWidget* theWidget, const QString& theInfo = QString());
71
72 /// Calls the same-named Qt method for the given widget.
73 /// Gives the keyboard input focus to this widget (or its focus proxy) if this widget or
74 /// one of its parents is the active window.
75 /// \param theWidget a widget to be activated
76 /// \param theIndo a debug information
77 MODULEBASE_EXPORT void setFocus(QWidget* theWidget, const QString& theInfo = QString());
78
79
80 /// Sets or removes the shadow effect to the widget
81 /// \param theWidget a widget to be styled
82 /// \param isSetEffect if true, the shadow effect is set, otherwise cleared
83 /// \return resulting pixmap
84 MODULEBASE_EXPORT void setShadowEffect(QWidget* theWidget, const bool isSetEffect);
85
86 /// \ingroup GUI
87 /// Methods to modify a resource pixmap
88
89 /// Create composite pixmap.
90 /// Pixmap \a theAdditionalIcon is drawn over pixmap \a dest with coordinates
91 /// specified relatively to the upper left corner of \a theIcon.
92
93 /// \param theAdditionalIcon resource text of the additional pixmap
94 /// \param theIcon resource text of the background pixmap
95 /// \return resulting pixmap
96 MODULEBASE_EXPORT QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon);
97
98 /// Create composite pixmap.
99 /// Pixmap \a theAdditionalIcon is drawn over pixmap \a dest with coordinates
100 /// specified relatively to the upper left corner of \a theIcon.
101
102 /// \param theAdditionalIcon additional pixmap
103 /// \param theIcon background pixmap
104 /// \return resulting pixmap
105 MODULEBASE_EXPORT QPixmap composite(const QImage& theAdditionalIcon, QImage& theIcon);
106
107 /// Generates the pixmap lighter than the resources pixmap.
108 /// Pixmap \a theIcon is lighted according to the given value.
109 /// If the lighter value is greater than 100, this functions returns a lighter pixmap.
110 /// Setting lighter value to 150 returns a color that is 50% brighter.
111 /// If the factor is less than 100,
112 /// the return pixmap is darker. If the factor is 0 or negative, the return pixmap is unspecified.
113
114 /// \param resource text of the pixmap
115 /// \param theLighterValue a lighter factor
116 /// \return resulting pixmap
117 MODULEBASE_EXPORT QPixmap lighter(const QString& theIcon, const int theLighterValue = 200);
118
119 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
120 /// \param theSpin an X or Y coordinate widget
121 /// \param theValue a new value
122 MODULEBASE_EXPORT void setSpinValue(QDoubleSpinBox* theSpin, double theValue);
123
124 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
125 /// \param theSpin an X or Y coordinate widget
126 /// \param theValue a new value
127 MODULEBASE_EXPORT void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue);
128
129 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
130 /// \param theSpin an ModuleBase_ParamSpinBox that accepts text
131 /// \param theText a new value
132 MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText);
133
134 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
135 /// \param theSpin a ModuleBase_ParamIntSpinBox object
136 /// \param theValue a new value
137 MODULEBASE_EXPORT void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue);
138
139 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
140 /// \param theSpin a SpinBox that accepts text
141 /// \param theText a new value
142 MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText);
143
144 /// Creates an action filled with the parameters
145 /// \param theIcon an action icon
146 /// \param theText an icon text
147 /// \param theParent an action object
148 /// \param theReceiver an object to process action triggered signal
149 /// \param theMember a slot to process the action triggered signal
150 /// \param theToolTip an action tool tip information, if empty the text is used
151 /// \param theStatusTip an action status tip information, if empty the text is used
152 MODULEBASE_EXPORT QAction* createAction(const QIcon& theIcon, const QString& theText,
153                                         QObject* theParent, const QObject* theReceiver = 0,
154                                         const char* theMember = "",
155                                         const QString& theToolTip = QString(),
156                                         const QString& theStatusTip = QString());
157
158 #ifdef _DEBUG
159 /// Returns name of data of the object if it is not NULL
160 /// \param theObj an object
161 /// \return a string
162 MODULEBASE_EXPORT QString objectName(const ObjectPtr& theObj);
163
164 /// Converts the object to the feature or a result and generate information string
165 /// \param theObj an object
166 /// \param isUseAttributesInfo a flag whether the attribute values information is used
167 /// \return a string
168 MODULEBASE_EXPORT QString objectInfo(const ObjectPtr& theObj,
169                                      const bool isUseAttributesInfo = false);
170 #endif
171
172 /// Converts string value (name of shape type) to shape enum value
173 /// \param theType - string with shape type name
174 /// \return TopAbs_ShapeEnum value
175 MODULEBASE_EXPORT int shapeType(const QString& theType);
176
177 /// Checks whether the object is a sub result. It gets the feature of the object,
178 /// obtains all results of the feature and check if the object is a sub result
179 /// \return boolean result
180 MODULEBASE_EXPORT bool isSubResult(ObjectPtr theObject);
181
182 /// Check types of objects which are in the given list
183 /// \param theObjects the list of objects
184 /// \param hasResult will be set to true if list contains Result objects
185 /// \param hasFeature will be set to true if list contains Feature objects
186 /// \param hasParameter will be set to true if list contains Parameter objects
187 /// \param hasCompositeOwner will be set to true if list contains Sub-Feature objects
188 /// \param hasResultInHistory will be set to true if one of result is in history
189 /// \param hasFolder will be set to true if one of folder is in the list
190 MODULEBASE_EXPORT void checkObjects(const QObjectPtrList& theObjects, bool& hasResult,
191                            bool& hasFeature, bool& hasParameter, bool& hasCompositeOwner,
192                            bool& hasResultInHistory, bool& hasFolder, bool &hasGroupsOnly);
193
194 /// Sets the default coeffient into the driver calculated accordingly the shape type.
195 /// It provides 1.e-4 for results of construction type
196 /// \param theResult a result object to define the deviation coefficient
197 /// \param theDrawer a drawer
198 //MODULEBASE_EXPORT void setDefaultDeviationCoefficient(
199 //                           const std::shared_ptr<ModelAPI_Result>& theResult,
200 //                           const Handle(Prs3d_Drawer)& theDrawer);
201
202 /// Sets the default coeffient into the driver calculated accordingly the shape type.
203 /// It provides 1.e-4 for a shape withe Edge shape type
204 /// \param theShape a shape to define the deviation coefficient,
205 /// \param theDrawer a drawer
206 MODULEBASE_EXPORT void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
207                                                       const Handle(Prs3d_Drawer)& theDrawer);
208
209 /// Obtains the color from the property manager and converts it to the OCCT color
210 /// \param theSection a property section
211 /// \param theName a property item name
212 /// \return quantity color
213 MODULEBASE_EXPORT Quantity_Color color(const std::string& theSection,
214                                        const std::string& theName);
215
216 /// Returns the object from the attribute
217 /// \param theObj an object
218 MODULEBASE_EXPORT ObjectPtr getObject(const AttributePtr& theAttribute);
219
220 /// Returns the object from the attribute
221 /// \param theObj an object
222 MODULEBASE_EXPORT TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape);
223
224 /// Returns either presentation shape or shape of BREP owner if it is casted to it
225 /// \param thePrs selection presentation
226 /// \return shape
227 MODULEBASE_EXPORT TopoDS_Shape getSelectedShape(
228   const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs);
229
230 /// Returns list of parameters accessible in the active part and partset
231 /// \theParameters a list of parameter names
232 MODULEBASE_EXPORT void getParameters(QStringList& theParameters);
233
234 /// Returns list of parameters accessible in the active part and partset
235 /// \theParameters a list of parameter names
236 MODULEBASE_EXPORT std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
237                                                  const FeaturePtr& theFeature);
238
239 /// Set the object to the attribute depending on the attribute type. If it is a list,
240 /// the values are appended if they are not in the list yet.
241 /// \param theAttribute an attribute where the object and shape are set
242 /// \param theObject an object
243 /// \param theShape a shape
244 /// \param theWorkshop to find an attribute for the given shape for attribute reference
245 /// \param theTemporarily if it is true, do not store and name the added in the data framework
246 ///        It is useful for attribute selection
247 MODULEBASE_EXPORT bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
248                                  const std::shared_ptr<GeomAPI_Shape>& theShape,
249                                  ModuleBase_IWorkshop* theWorkshop,
250                                  const bool theTemporarily);
251
252 /// Set the object to the attribute depending on the attribute type. If it is a list,
253 /// the values are appended if they are not in the list yet.
254 /// \param theAttribute an attribute where the object and shape are set
255 /// \param theObject an object
256 /// \param theShape a shape
257 /// \param theWorkshop to find an attribute for the given shape for attribute reference
258 /// \param theTemporarily if it is true, do not store and name the added in the data framework
259 /// \param theCheckIfAttributeHasObject if it is true, the check isInList is called
260 ///        It is useful for attribute selection
261 /// \return true if the attribute is filled with the given parameters
262 MODULEBASE_EXPORT bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
263                                  const std::shared_ptr<GeomAPI_Shape>& theShape,
264                                  ModuleBase_IWorkshop* theWorkshop,
265                                  const bool theTemporarily,
266                                  const bool theCheckIfAttributeHasObject);
267
268 /// Returns the shape of the attribute. If the attribute is AttributeRefAttrPtr, the shape is found
269 /// using current module of the given workshop.
270 /// \param theAttribute an attribute where the shape is set
271 /// \param theWorkshop to find a shape for the given attribute
272 MODULEBASE_EXPORT std::shared_ptr<GeomAPI_Shape> getShape(const AttributePtr& theAttribute,
273                                                           ModuleBase_IWorkshop* theWorkshop);
274
275 /// Flush updated signal for the object. The viewer update is blocked in the process
276 /// \param theObject parameter of the signal
277 MODULEBASE_EXPORT void flushUpdated(ObjectPtr theObject);
278
279 /// Sends a message about block/unblock viewer updating
280 /// \param theValue a boolean value
281 MODULEBASE_EXPORT void blockUpdateViewer(const bool theValue);
282
283 /// Generates a wrapped string to be less than value with '\n' separators
284 /// \param theValue a boolean value
285 /// \param theWidget a widget to know the font
286 /// \param theMaxLineInPixels a maximum line width in pixels
287 MODULEBASE_EXPORT QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
288                                              int theMaxLineInPixels = 150);
289
290 /// Generates a locale to disable thousands separator for spin box
291 /// (to avoid inconsistency of double-2-string and string-2-double conversion)
292 /// \return locale
293 MODULEBASE_EXPORT QLocale doubleLocale();
294
295 /// Returns a container of referenced feature to the current object in the object document.
296 /// \param theObject an object, which will be casted to a feature type
297 /// \param theRefFeatures an output container
298 void MODULEBASE_EXPORT refsToFeatureInFeatureDocument(const ObjectPtr& theObject,
299                                                       std::set<FeaturePtr>& theRefFeatures);
300
301
302 /// Returns true if the object if a sub child of the feature. The feature is casted to the
303 /// composite one. If it is possible, the sub object check happens. The method is applied
304 /// recursively to the feature subs.
305 /// \param theObject a candidate to be a sub object
306 /// \param theFeature a candidate to be a composite feature
307 /// \return a boolean value
308 //bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature);
309
310
311 /// Returns true if the result is a sub object of some composite object
312 /// \param theObject a result object
313 /// \returns boolean value
314 //bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject);
315
316
317 /// Returns first result of the feature: the object itself if it is a result of
318 /// first result of the object's feature
319 /// \param theObject an object
320 /// \return first result or NULL
321 std::shared_ptr<ModelAPI_Result> MODULEBASE_EXPORT firstResult(const ObjectPtr& theObject);
322
323 /// Returns true if the list contains at least one feature from the module document(PartSet)
324 /// The Part features are skipped.
325 /// \param theFeatures a list of features
326 /// \bool true if it is found
327 bool MODULEBASE_EXPORT hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures);
328
329 /// Shows a dialog box about references. Ask whether they should be also removed.
330 /// \param theFeatures a list of features
331 /// \param theReferences a map of all references to the features
332 /// \param theParent a parent widget for the question message box
333 /// \param theReferencesToDelete an out set for references features to be removed
334 /// \return true if in message box answer is Yes
335 bool MODULEBASE_EXPORT askToDelete(const std::set<FeaturePtr> aFeatures,
336                                    const std::map<FeaturePtr,
337                                    std::set<FeaturePtr> >& theReferences,
338                                    QWidget* theParent,
339                                    std::set<FeaturePtr>& theReferencesToDelete,
340                                    const std::string& thePrefixInfo = "");
341
342 /// Shows a message box about conflicting constraints.
343 /// \param theParent a parent widget for the message box
344 /// \param theWarningText text describing the cause of the conflict
345 bool MODULEBASE_EXPORT warningAboutConflict(QWidget* theParent, const std::string& theWarningText);
346
347 /// Converts a list of objects to set of corresponded features. If object is result, it is ignored
348 /// because the feature only might be removed. But if result is in a parameter group, the feature
349 /// of this parameter is to be removed
350 /// \param theObjects a list of objects
351 /// \param theFeatures an out container of features
352 void MODULEBASE_EXPORT convertToFeatures(const QObjectPtrList& theObjects,
353                                          std::set<FeaturePtr>& theFeatures);
354
355
356 /// Converts a list of objects to set of folders.
357 /// \param theObjects a list of objects
358 /// \param theFeatures an out container of features
359 void MODULEBASE_EXPORT convertToFolders(const QObjectPtrList& theObjects,
360                                          std::set<FolderPtr>& theFolders);
361
362
363 /// Returns translation from the given data.
364 /// If translation is not exists then it returns a string
365 /// from the info data without translation
366 /// \param theMessage a message which have to be translated
367 QString MODULEBASE_EXPORT translate(const Events_InfoMessage& theMessage);
368
369 /// Returns translation from the given data.
370 /// If translation is not exists then it returns a string
371 /// from the info data without translation
372 /// \param theContext context of the message (Feature Id)
373 /// \param theMessage a message which have to be translated
374 QString MODULEBASE_EXPORT translate(const std::string& theContext, const std::string& theMessage);
375
376 /// Set Highlighting of points as a Ball shape
377 /// \param theAIS - the presentation
378 void MODULEBASE_EXPORT setPointBallHighlighting(AIS_InteractiveObject* theAIS);
379
380 /// Creates a parameter from a given string
381 /// \theText a text wit equation
382 FeaturePtr MODULEBASE_EXPORT createParameter(const QString& theText);
383
384 /// Edits parameter replacing its name and expression according to the given string
385 /// \theParam a editing parameter
386 /// \theText a text wit equation
387 void MODULEBASE_EXPORT editParameter(FeaturePtr theParam, const QString& theText);
388
389 /// Returns True if a parameter with the given name already exists
390 /// \theName a name of parameter
391 bool MODULEBASE_EXPORT isNameExist(const QString& theName, FeaturePtr theIgnoreParameter);
392
393 /// Find parameter by its name
394 /// \theName a name of parameter
395 FeaturePtr MODULEBASE_EXPORT findParameter(const QString& theName);
396
397 /// Returns true if both shapes are the same. In case of compounds it
398 /// compares their contents.
399 /// \param theShape1 a first shape to compare
400 /// \param theShape2 a second shape to compare
401 /// \return true if both shapes are the same
402 bool MODULEBASE_EXPORT isSameShape(const TopoDS_Shape& theShape1, const TopoDS_Shape& theShape2);
403
404 //----------- Class members -------------
405 /// Returns a name in the next form: attribute_feature_name/attribute_id
406 /// \param theAttribute a model attribute
407 /// \param theWorkshop a workshop class instance
408 /// \return string value
409 std::wstring MODULEBASE_EXPORT generateName(const AttributePtr& theAttribute,
410   ModuleBase_IWorkshop* theWorkshop);
411
412 /// Returns pixel ratio of a screen where main window is displayed
413 qreal MODULEBASE_EXPORT currentPixelRatio();
414
415 /// Set displaying status for elements from part depending on the settings
416 /// \param thePart a pointer of part
417 void MODULEBASE_EXPORT setDisplaying(std::shared_ptr<ModelAPI_ResultPart> thePart,
418                                      bool theDisplayFromScript = false);
419 }
420
421 #endif