Salome HOME
Merge branch 'master' into cgt/devCEA
[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 MODULEBASE_EXPORT void checkObjects(const QObjectPtrList& theObjects, bool& hasResult,
180                            bool& hasFeature, bool& hasParameter, bool& hasCompositeOwner,
181                            bool& hasResultInHistory);
182
183 /// Sets the default coeffient into the driver calculated accordingly the shape type.
184 /// It provides 1.e-4 for results of construction type
185 /// \param theResult a result object to define the deviation coeffient
186 /// \param theDrawer a drawer
187 //MODULEBASE_EXPORT void setDefaultDeviationCoefficient(
188 //                           const std::shared_ptr<ModelAPI_Result>& theResult,
189 //                           const Handle(Prs3d_Drawer)& theDrawer);
190
191 /// Sets the default coeffient into the driver calculated accordingly the shape type.
192 /// It provides 1.e-4 for a shape withe Edge shape type
193 /// \param theShape a shape to define the deviation coeffient,
194 /// \param theDrawer a drawer
195 MODULEBASE_EXPORT void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
196                                                       const Handle(Prs3d_Drawer)& theDrawer);
197
198 /// Obtains the color from the property manager and converts it to the OCCT color
199 /// \param theSection a property section
200 /// \param theName a property item name
201 /// \return quantity color
202 MODULEBASE_EXPORT Quantity_Color color(const std::string& theSection,
203                                        const std::string& theName);
204
205 /// Returns the object from the attribute
206 /// \param theObj an object
207 MODULEBASE_EXPORT ObjectPtr getObject(const AttributePtr& theAttribute);
208
209 /// Returns the object from the attribute
210 /// \param theObj an object
211 MODULEBASE_EXPORT TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape);
212
213 /// Returns list of parameters accessible in the active part and partset
214 /// \theParameters a list of parameter names
215 MODULEBASE_EXPORT void getParameters(QStringList& theParameters);
216
217 /// Returns list of parameters accessible in the active part and partset
218 /// \theParameters a list of parameter names
219 MODULEBASE_EXPORT std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
220                                                  const FeaturePtr& theFeature);
221
222 /// Set the object to the attribute depending on the attribute type. If it is a list,
223 /// the values are appended if they are not in the list yet.
224 /// \param theAttribute an attribute where the object and shape are set
225 /// \param theObject an object
226 /// \param theShape a shape
227 /// \param theWorkshop to find an attribute for the given shape for attribute reference
228 /// \param theTemporarily if it is true, do not store and name the added in the data framework
229 ///        It is useful for attribute selection
230 MODULEBASE_EXPORT bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
231                                  const std::shared_ptr<GeomAPI_Shape>& theShape,
232                                  ModuleBase_IWorkshop* theWorkshop,
233                                  const bool theTemporarily);
234
235 /// Set the object to the attribute depending on the attribute type. If it is a list,
236 /// the values are appended if they are not in the list yet.
237 /// \param theAttribute an attribute where the object and shape are set
238 /// \param theObject an object
239 /// \param theShape a shape
240 /// \param theWorkshop to find an attribute for the given shape for attribute reference
241 /// \param theTemporarily if it is true, do not store and name the added in the data framework
242 /// \param theCheckIfAttributeHasObject if it is true, the check isInList is called
243 ///        It is useful for attribute selection
244 /// \return true if the attribute is filled with the given parameters
245 MODULEBASE_EXPORT bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
246                                  const std::shared_ptr<GeomAPI_Shape>& theShape,
247                                  ModuleBase_IWorkshop* theWorkshop,
248                                  const bool theTemporarily,
249                                  const bool theCheckIfAttributeHasObject);
250
251 /// Returns the shape of the attribute. If the attribute is AttributeRefAttrPtr, the shape is found
252 /// using current module of the given workshop.
253 /// \param theAttribute an attribute where the shape is set
254 /// \param theWorkshop to find a shape for the given attribute
255 MODULEBASE_EXPORT std::shared_ptr<GeomAPI_Shape> getShape(const AttributePtr& theAttribute,
256                                                           ModuleBase_IWorkshop* theWorkshop);
257
258 /// Flush updated signal for the object. The viewer update is blocked in the process
259 /// \param theObject parameter of the signal
260 MODULEBASE_EXPORT void flushUpdated(ObjectPtr theObject);
261
262 /// Sends a message about block/unblock viewer updating
263 /// \param theValue a boolean value
264 MODULEBASE_EXPORT void blockUpdateViewer(const bool theValue);
265
266 /// Generates a wrapped string to be less than value with '\n' separators
267 /// \param theValue a boolean value
268 /// \param theWidget a widget to know the font
269 /// \param theMaxLineInPixels a maximum line width in pixels
270 MODULEBASE_EXPORT QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
271                                              int theMaxLineInPixels = 150);
272
273 /// Generates a locale to disable thousands separator for spin box
274 /// (to avoid inconsistency of double-2-string and string-2-double conversion)
275 /// \return locale
276 MODULEBASE_EXPORT QLocale doubleLocale();
277
278 /// Returns a container of referenced feature to the current object in the object document.
279 /// \param theObject an object, which will be casted to a feature type
280 /// \param theRefFeatures an output container
281 void MODULEBASE_EXPORT refsToFeatureInFeatureDocument(const ObjectPtr& theObject,
282                                                       std::set<FeaturePtr>& theRefFeatures);
283
284
285 /// Returns true if the object if a sub child of the feature. The feature is casted to the
286 /// composite one. If it is possible, the sub object check happens. The method is applyed
287 /// recursively to the feature subs.
288 /// \param theObject a candidate to be a sub object
289 /// \param theFeature a candidate to be a composite feature
290 /// \return a boolean value
291 //bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature);
292
293
294 /// Returns true if the result is a sub object of some composite object
295 /// \param theObject a result object
296 /// \returns boolean value
297 //bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject);
298
299
300 /// Returns first result of the feature: the object itself if it is a result of
301 /// first result of the object's feature
302 /// \param theObject an object
303 /// \return first result or NULL
304 std::shared_ptr<ModelAPI_Result> MODULEBASE_EXPORT firstResult(const ObjectPtr& theObject);
305
306 /// Returns true if the list contains at least one feature from the module document(PartSet)
307 /// The Part features are skipped.
308 /// \param theFeatures a list of features
309 /// \bool true if it is found
310 bool MODULEBASE_EXPORT hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures);
311
312 /// Shows a dialog box about references. Ask whether they should be also removed.
313 /// \param theFeatures a list of features
314 /// \param theReferences a map of all references to the features
315 /// \param theParent a parent widget for the question message box
316 /// \param theReferencesToDelete an out set for references features to be removed
317 /// \return true if in message box answer is Yes
318 bool MODULEBASE_EXPORT askToDelete(const std::set<FeaturePtr> aFeatures,
319                                    const std::map<FeaturePtr,
320                                    std::set<FeaturePtr> >& theReferences,
321                                    QWidget* theParent,
322                                    std::set<FeaturePtr>& theReferencesToDelete,
323                                    const std::string& thePrefixInfo = "");
324
325 /// Converts a list of objects to set of corresponded features. If object is result, it is ingored
326 /// because the feauture only might be removed. But if result is in a parameter group, the feature
327 /// of this parameter is to be removed
328 /// \param theObjects a list of objects
329 /// \param theFeatures an out conteiner of features
330 void MODULEBASE_EXPORT convertToFeatures(const QObjectPtrList& theObjects,
331                                          std::set<FeaturePtr>& theFeatures);
332
333
334 /// Returns translation from the given data.
335 /// If translation is not exists then it returns a string
336 /// from the info data without translation
337 /// \param theMessage a message which dave to be translated
338 QString MODULEBASE_EXPORT translate(const Events_InfoMessage& theMessage);
339
340 /// Returns translation from the given data.
341 /// If translation is not exists then it returns a string
342 /// from the info data without translation
343 /// \param theContext context of the message (Feature Id)
344 /// \param theMessage a message which dave to be translated
345 QString MODULEBASE_EXPORT translate(const std::string& theContext, const std::string& theMessage);
346
347 /// Set Highlighting of points as a Ball shape
348 /// \param theAIS - the presentation
349 void MODULEBASE_EXPORT setPointBallHighlighting(AIS_Shape* theAIS);
350
351 }
352
353 #endif