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