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