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