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