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