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