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