Salome HOME
Merge remote-tracking branch 'remotes/origin/2018_Lot1_Tasks'
[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 /// Generates the pixmap lighter than the resources pixmap.
100 /// Pixmap \a theIcon is lighted according to the given value.
101 /// If the lighter value is greater than 100, this functions returns a lighter pixmap.
102 /// Setting lighter value to 150 returns a color that is 50% brighter.
103 /// If the factor is less than 100,
104 /// the return pixmap is darker. If the factor is 0 or negative, the return pixmap is unspecified.
105
106 /// \param resource text of the pixmap
107 /// \param theLighterValue a lighter factor
108 /// \return resulting pixmap
109 MODULEBASE_EXPORT QPixmap lighter(const QString& theIcon, const int theLighterValue = 200);
110
111 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
112 /// \param theSpin an X or Y coordinate widget
113 /// \param theValue a new value
114 MODULEBASE_EXPORT void setSpinValue(QDoubleSpinBox* theSpin, double theValue);
115
116 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
117 /// \param theSpin an X or Y coordinate widget
118 /// \param theValue a new value
119 MODULEBASE_EXPORT void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue);
120
121 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
122 /// \param theSpin an ModuleBase_ParamSpinBox that accepts text
123 /// \param theText a new value
124 MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText);
125
126 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
127 /// \param theSpin a ModuleBase_ParamIntSpinBox object
128 /// \param theValue a new value
129 MODULEBASE_EXPORT void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue);
130
131 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
132 /// \param theSpin a SpinBox that accepts text
133 /// \param theText a new value
134 MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText);
135
136 /// Creates an action filled with the parameters
137 /// \param theIcon an action icon
138 /// \param theText an icon text
139 /// \param theParent an action object
140 /// \param theReceiver an object to process action triggered signal
141 /// \param theMember a slot to process the action triggered signal
142 /// \param theToolTip an action tool tip information, if empty the text is used
143 /// \param theStatusTip an action status tip information, if empty the text is used
144 MODULEBASE_EXPORT QAction* createAction(const QIcon& theIcon, const QString& theText,
145                                         QObject* theParent, const QObject* theReceiver = 0,
146                                         const char* theMember = "",
147                                         const QString& theToolTip = QString(),
148                                         const QString& theStatusTip = QString());
149
150 #ifdef _DEBUG
151 /// Returns name of data of the object if it is not NULL
152 /// \param theObj an object
153 /// \return a string
154 MODULEBASE_EXPORT QString objectName(const ObjectPtr& theObj);
155
156 /// Converts the object to the feature or a result and generate information string
157 /// \param theObj an object
158 /// \param isUseAttributesInfo a flag whether the attribute values information is used
159 /// \return a string
160 MODULEBASE_EXPORT QString objectInfo(const ObjectPtr& theObj,
161                                      const bool isUseAttributesInfo = false);
162 #endif
163
164 /// Converts string value (name of shape type) to shape enum value
165 /// \param theType - string with shape type name
166 /// \return TopAbs_ShapeEnum value
167 MODULEBASE_EXPORT int shapeType(const QString& theType);
168
169 /// Checks whether the object is a sub result. It gets the feature of the object,
170 /// obtains all results of the feature and check if the object is a sub result
171 /// \return boolean result
172 MODULEBASE_EXPORT bool isSubResult(ObjectPtr theObject);
173
174 /// Check types of objects which are in the given list
175 /// \param theObjects the list of objects
176 /// \param hasResult will be set to true if list contains Result objects
177 /// \param hasFeature will be set to true if list contains Feature objects
178 /// \param hasParameter will be set to true if list contains Parameter objects
179 /// \param hasCompositeOwner will be set to true if list contains Sub-Feature objects
180 /// \param hasResultInHistory will be set to true if one of result is in history
181 /// \param hasFolder will be set to true if one of folder is in the list
182 MODULEBASE_EXPORT void checkObjects(const QObjectPtrList& theObjects, bool& hasResult,
183                            bool& hasFeature, bool& hasParameter, bool& hasCompositeOwner,
184                            bool& hasResultInHistory, bool& hasFolder);
185
186 /// Sets the default coeffient into the driver calculated accordingly the shape type.
187 /// It provides 1.e-4 for results of construction type
188 /// \param theResult a result object to define the deviation coeffient
189 /// \param theDrawer a drawer
190 //MODULEBASE_EXPORT void setDefaultDeviationCoefficient(
191 //                           const std::shared_ptr<ModelAPI_Result>& theResult,
192 //                           const Handle(Prs3d_Drawer)& theDrawer);
193
194 /// Sets the default coeffient into the driver calculated accordingly the shape type.
195 /// It provides 1.e-4 for a shape withe Edge shape type
196 /// \param theShape a shape to define the deviation coeffient,
197 /// \param theDrawer a drawer
198 MODULEBASE_EXPORT void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
199                                                       const Handle(Prs3d_Drawer)& theDrawer);
200
201 /// Obtains the color from the property manager and converts it to the OCCT color
202 /// \param theSection a property section
203 /// \param theName a property item name
204 /// \return quantity color
205 MODULEBASE_EXPORT Quantity_Color color(const std::string& theSection,
206                                        const std::string& theName);
207
208 /// Returns the object from the attribute
209 /// \param theObj an object
210 MODULEBASE_EXPORT ObjectPtr getObject(const AttributePtr& theAttribute);
211
212 /// Returns the object from the attribute
213 /// \param theObj an object
214 MODULEBASE_EXPORT TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape);
215
216 /// Returns either presentation shape or shape of BREP owner if it is casted to it
217 /// \param thePrs selection presentation
218 /// \return shape
219 MODULEBASE_EXPORT TopoDS_Shape getSelectedShape(
220   const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs);
221
222 /// Returns list of parameters accessible in the active part and partset
223 /// \theParameters a list of parameter names
224 MODULEBASE_EXPORT void getParameters(QStringList& theParameters);
225
226 /// Returns list of parameters accessible in the active part and partset
227 /// \theParameters a list of parameter names
228 MODULEBASE_EXPORT std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
229                                                  const FeaturePtr& theFeature);
230
231 /// Set the object to the attribute depending on the attribute type. If it is a list,
232 /// the values are appended if they are not in the list yet.
233 /// \param theAttribute an attribute where the object and shape are set
234 /// \param theObject an object
235 /// \param theShape a shape
236 /// \param theWorkshop to find an attribute for the given shape for attribute reference
237 /// \param theTemporarily if it is true, do not store and name the added in the data framework
238 ///        It is useful for attribute selection
239 MODULEBASE_EXPORT bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
240                                  const std::shared_ptr<GeomAPI_Shape>& theShape,
241                                  ModuleBase_IWorkshop* theWorkshop,
242                                  const bool theTemporarily);
243
244 /// Set the object to the attribute depending on the attribute type. If it is a list,
245 /// the values are appended if they are not in the list yet.
246 /// \param theAttribute an attribute where the object and shape are set
247 /// \param theObject an object
248 /// \param theShape a shape
249 /// \param theWorkshop to find an attribute for the given shape for attribute reference
250 /// \param theTemporarily if it is true, do not store and name the added in the data framework
251 /// \param theCheckIfAttributeHasObject if it is true, the check isInList is called
252 ///        It is useful for attribute selection
253 /// \return true if the attribute is filled with the given parameters
254 MODULEBASE_EXPORT bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
255                                  const std::shared_ptr<GeomAPI_Shape>& theShape,
256                                  ModuleBase_IWorkshop* theWorkshop,
257                                  const bool theTemporarily,
258                                  const bool theCheckIfAttributeHasObject);
259
260 /// Returns the shape of the attribute. If the attribute is AttributeRefAttrPtr, the shape is found
261 /// using current module of the given workshop.
262 /// \param theAttribute an attribute where the shape is set
263 /// \param theWorkshop to find a shape for the given attribute
264 MODULEBASE_EXPORT std::shared_ptr<GeomAPI_Shape> getShape(const AttributePtr& theAttribute,
265                                                           ModuleBase_IWorkshop* theWorkshop);
266
267 /// Flush updated signal for the object. The viewer update is blocked in the process
268 /// \param theObject parameter of the signal
269 MODULEBASE_EXPORT void flushUpdated(ObjectPtr theObject);
270
271 /// Sends a message about block/unblock viewer updating
272 /// \param theValue a boolean value
273 MODULEBASE_EXPORT void blockUpdateViewer(const bool theValue);
274
275 /// Generates a wrapped string to be less than value with '\n' separators
276 /// \param theValue a boolean value
277 /// \param theWidget a widget to know the font
278 /// \param theMaxLineInPixels a maximum line width in pixels
279 MODULEBASE_EXPORT QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
280                                              int theMaxLineInPixels = 150);
281
282 /// Generates a locale to disable thousands separator for spin box
283 /// (to avoid inconsistency of double-2-string and string-2-double conversion)
284 /// \return locale
285 MODULEBASE_EXPORT QLocale doubleLocale();
286
287 /// Returns a container of referenced feature to the current object in the object document.
288 /// \param theObject an object, which will be casted to a feature type
289 /// \param theRefFeatures an output container
290 void MODULEBASE_EXPORT refsToFeatureInFeatureDocument(const ObjectPtr& theObject,
291                                                       std::set<FeaturePtr>& theRefFeatures);
292
293
294 /// Returns true if the object if a sub child of the feature. The feature is casted to the
295 /// composite one. If it is possible, the sub object check happens. The method is applyed
296 /// recursively to the feature subs.
297 /// \param theObject a candidate to be a sub object
298 /// \param theFeature a candidate to be a composite feature
299 /// \return a boolean value
300 //bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature);
301
302
303 /// Returns true if the result is a sub object of some composite object
304 /// \param theObject a result object
305 /// \returns boolean value
306 //bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject);
307
308
309 /// Returns first result of the feature: the object itself if it is a result of
310 /// first result of the object's feature
311 /// \param theObject an object
312 /// \return first result or NULL
313 std::shared_ptr<ModelAPI_Result> MODULEBASE_EXPORT firstResult(const ObjectPtr& theObject);
314
315 /// Returns true if the list contains at least one feature from the module document(PartSet)
316 /// The Part features are skipped.
317 /// \param theFeatures a list of features
318 /// \bool true if it is found
319 bool MODULEBASE_EXPORT hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures);
320
321 /// Shows a dialog box about references. Ask whether they should be also removed.
322 /// \param theFeatures a list of features
323 /// \param theReferences a map of all references to the features
324 /// \param theParent a parent widget for the question message box
325 /// \param theReferencesToDelete an out set for references features to be removed
326 /// \return true if in message box answer is Yes
327 bool MODULEBASE_EXPORT askToDelete(const std::set<FeaturePtr> aFeatures,
328                                    const std::map<FeaturePtr,
329                                    std::set<FeaturePtr> >& theReferences,
330                                    QWidget* theParent,
331                                    std::set<FeaturePtr>& theReferencesToDelete,
332                                    const std::string& thePrefixInfo = "");
333
334 /// Converts a list of objects to set of corresponded features. If object is result, it is ingored
335 /// because the feauture only might be removed. But if result is in a parameter group, the feature
336 /// of this parameter is to be removed
337 /// \param theObjects a list of objects
338 /// \param theFeatures an out conteiner of features
339 void MODULEBASE_EXPORT convertToFeatures(const QObjectPtrList& theObjects,
340                                          std::set<FeaturePtr>& theFeatures);
341
342
343 /// Converts a list of objects to set of folders.
344 /// \param theObjects a list of objects
345 /// \param theFeatures an out conteiner of features
346 void MODULEBASE_EXPORT convertToFolders(const QObjectPtrList& theObjects,
347                                          std::set<FolderPtr>& theFolders);
348
349
350 /// Returns translation from the given data.
351 /// If translation is not exists then it returns a string
352 /// from the info data without translation
353 /// \param theMessage a message which dave to be translated
354 QString MODULEBASE_EXPORT translate(const Events_InfoMessage& theMessage);
355
356 /// Returns translation from the given data.
357 /// If translation is not exists then it returns a string
358 /// from the info data without translation
359 /// \param theContext context of the message (Feature Id)
360 /// \param theMessage a message which dave to be translated
361 QString MODULEBASE_EXPORT translate(const std::string& theContext, const std::string& theMessage);
362
363 /// Set Highlighting of points as a Ball shape
364 /// \param theAIS - the presentation
365 void MODULEBASE_EXPORT setPointBallHighlighting(AIS_Shape* theAIS);
366
367 /// Creates a parameter from a given string
368 /// \theText a text wit equation
369 FeaturePtr MODULEBASE_EXPORT createParameter(const QString& theText);
370
371 /// Edits parameter replacing its name and expression according to the given string
372 /// \theParam a editing parameter
373 /// \theText a text wit equation
374 void MODULEBASE_EXPORT editParameter(FeaturePtr theParam, const QString& theText);
375
376 /// Returns True if a parameter with the given name already exists
377 /// \theName a name of parameter
378 bool MODULEBASE_EXPORT isNameExist(const QString& theName, FeaturePtr theIgnoreParameter);
379
380 /// Find parameter by its name
381 /// \theName a name of parameter
382 FeaturePtr MODULEBASE_EXPORT findParameter(const QString& theName);
383 }
384
385 #endif