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