1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #ifndef ModuleBase_Tools_H
22 #define ModuleBase_Tools_H
24 #include "ModuleBase.h"
25 #include "ModuleBase_Definitions.h"
27 #include <ModelAPI_Attribute.h>
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_Folder.h>
31 #include <TopAbs_ShapeEnum.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <Prs3d_Drawer.hxx>
34 #include <AIS_Shape.hxx>
36 #include <Quantity_Color.hxx>
48 class ModuleBase_ParamIntSpinBox;
49 class ModuleBase_ParamSpinBox;
50 class ModuleBase_IWorkshop;
54 class Events_InfoMessage;
56 namespace ModuleBase_Tools {
58 /// Methods to adjust margins and spacings.
59 MODULEBASE_EXPORT void adjustMargins(QWidget* theWidget);
60 MODULEBASE_EXPORT void adjustMargins(QLayout* theLayout);
62 MODULEBASE_EXPORT void zeroMargins(QWidget* theWidget);
63 MODULEBASE_EXPORT void zeroMargins(QLayout* theLayout);
65 /// Calls the same-named Qt method for the given widget.
66 /// It sets the top-level widget containing this widget to be the active window.
67 /// An active window is a visible top-level window that has the keyboard input focus.
68 /// \param theWidget a widget to be activated
69 /// \param theIndo a debug information
70 MODULEBASE_EXPORT void activateWindow(QWidget* theWidget, const QString& theInfo = QString());
72 /// Calls the same-named Qt method for the given widget.
73 /// Gives the keyboard input focus to this widget (or its focus proxy) if this widget or
74 /// one of its parents is the active window.
75 /// \param theWidget a widget to be activated
76 /// \param theIndo a debug information
77 MODULEBASE_EXPORT void setFocus(QWidget* theWidget, const QString& theInfo = QString());
80 /// Sets or removes the shadow effect to the widget
81 /// \param theWidget a widget to be styled
82 /// \param isSetEffect if true, the shadow effect is set, overwise cleared
83 /// \return resulting pixmap
84 MODULEBASE_EXPORT void setShadowEffect(QWidget* theWidget, const bool isSetEffect);
87 /// Methods to modify a resource pixmap
89 /// Create composite pixmap.
90 /// Pixmap \a theAdditionalIcon is drawn over pixmap \a dest with coordinates
91 /// specified relatively to the upper left corner of \a theIcon.
93 /// \param theAdditionalIcon resource text of the additional pixmap
94 /// \param theIcon resource text of the background pixmap
95 /// \return resulting pixmap
96 MODULEBASE_EXPORT QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon);
98 /// Generates the pixmap lighter than the resources pixmap.
99 /// Pixmap \a theIcon is lighted according to the given value.
100 /// If the lighter value is greater than 100, this functions returns a lighter pixmap.
101 /// Setting lighter value to 150 returns a color that is 50% brighter.
102 /// If the factor is less than 100,
103 /// the return pixmap is darker. If the factor is 0 or negative, the return pixmap is unspecified.
105 /// \param resource text of the pixmap
106 /// \param theLighterValue a lighter factor
107 /// \return resulting pixmap
108 MODULEBASE_EXPORT QPixmap lighter(const QString& theIcon, const int theLighterValue = 200);
110 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
111 /// \param theSpin an X or Y coordinate widget
112 /// \param theValue a new value
113 MODULEBASE_EXPORT void setSpinValue(QDoubleSpinBox* theSpin, double theValue);
115 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
116 /// \param theSpin an X or Y coordinate widget
117 /// \param theValue a new value
118 MODULEBASE_EXPORT void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue);
120 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
121 /// \param theSpin an ModuleBase_ParamSpinBox that accepts text
122 /// \param theText a new value
123 MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText);
125 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
126 /// \param theSpin a ModuleBase_ParamIntSpinBox object
127 /// \param theValue a new value
128 MODULEBASE_EXPORT void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue);
130 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
131 /// \param theSpin a SpinBox that accepts text
132 /// \param theText a new value
133 MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText);
135 /// Creates an action filled with the parameters
136 /// \param theIcon an action icon
137 /// \param theText an icon text
138 /// \param theParent an action object
139 /// \param theReceiver an object to process action triggered signal
140 /// \param theMember a slot to process the action triggered signal
141 /// \param theToolTip an action tool tip information, if empty the text is used
142 /// \param theStatusTip an action status tip information, if empty the text is used
143 MODULEBASE_EXPORT QAction* createAction(const QIcon& theIcon, const QString& theText,
144 QObject* theParent, const QObject* theReceiver = 0,
145 const char* theMember = "",
146 const QString& theToolTip = QString(),
147 const QString& theStatusTip = QString());
150 /// Returns name of data of the object if it is not NULL
151 /// \param theObj an object
153 MODULEBASE_EXPORT QString objectName(const ObjectPtr& theObj);
155 /// Converts the object to the feature or a result and generate information string
156 /// \param theObj an object
157 /// \param isUseAttributesInfo a flag whether the attribute values information is used
159 MODULEBASE_EXPORT QString objectInfo(const ObjectPtr& theObj,
160 const bool isUseAttributesInfo = false);
163 /// Converts string value (name of shape type) to shape enum value
164 /// \param theType - string with shape type name
165 /// \return TopAbs_ShapeEnum value
166 MODULEBASE_EXPORT int shapeType(const QString& theType);
168 /// Checks whether the object is a sub result. It gets the feature of the object,
169 /// obtains all results of the feature and check if the object is a sub result
170 /// \return boolean result
171 MODULEBASE_EXPORT bool isSubResult(ObjectPtr theObject);
173 /// Check types of objects which are in the given list
174 /// \param theObjects the list of objects
175 /// \param hasResult will be set to true if list contains Result objects
176 /// \param hasFeature will be set to true if list contains Feature objects
177 /// \param hasParameter will be set to true if list contains Parameter objects
178 /// \param hasCompositeOwner will be set to true if list contains Sub-Feature objects
179 /// \param hasResultInHistory will be set to true if one of result is in history
180 /// \param hasFolder will be set to true if one of folder is in the list
181 MODULEBASE_EXPORT void checkObjects(const QObjectPtrList& theObjects, bool& hasResult,
182 bool& hasFeature, bool& hasParameter, bool& hasCompositeOwner,
183 bool& hasResultInHistory, bool& hasFolder);
185 /// Sets the default coeffient into the driver calculated accordingly the shape type.
186 /// It provides 1.e-4 for results of construction type
187 /// \param theResult a result object to define the deviation coeffient
188 /// \param theDrawer a drawer
189 //MODULEBASE_EXPORT void setDefaultDeviationCoefficient(
190 // const std::shared_ptr<ModelAPI_Result>& theResult,
191 // const Handle(Prs3d_Drawer)& theDrawer);
193 /// Sets the default coeffient into the driver calculated accordingly the shape type.
194 /// It provides 1.e-4 for a shape withe Edge shape type
195 /// \param theShape a shape to define the deviation coeffient,
196 /// \param theDrawer a drawer
197 MODULEBASE_EXPORT void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
198 const Handle(Prs3d_Drawer)& theDrawer);
200 /// Obtains the color from the property manager and converts it to the OCCT color
201 /// \param theSection a property section
202 /// \param theName a property item name
203 /// \return quantity color
204 MODULEBASE_EXPORT Quantity_Color color(const std::string& theSection,
205 const std::string& theName);
207 /// Returns the object from the attribute
208 /// \param theObj an object
209 MODULEBASE_EXPORT ObjectPtr getObject(const AttributePtr& theAttribute);
211 /// Returns the object from the attribute
212 /// \param theObj an object
213 MODULEBASE_EXPORT TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape);
215 /// Returns list of parameters accessible in the active part and partset
216 /// \theParameters a list of parameter names
217 MODULEBASE_EXPORT void getParameters(QStringList& theParameters);
219 /// Returns list of parameters accessible in the active part and partset
220 /// \theParameters a list of parameter names
221 MODULEBASE_EXPORT std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
222 const FeaturePtr& theFeature);
224 /// Set the object to the attribute depending on the attribute type. If it is a list,
225 /// the values are appended if they are not in the list yet.
226 /// \param theAttribute an attribute where the object and shape are set
227 /// \param theObject an object
228 /// \param theShape a shape
229 /// \param theWorkshop to find an attribute for the given shape for attribute reference
230 /// \param theTemporarily if it is true, do not store and name the added in the data framework
231 /// It is useful for attribute selection
232 MODULEBASE_EXPORT bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
233 const std::shared_ptr<GeomAPI_Shape>& theShape,
234 ModuleBase_IWorkshop* theWorkshop,
235 const bool theTemporarily);
237 /// Set the object to the attribute depending on the attribute type. If it is a list,
238 /// the values are appended if they are not in the list yet.
239 /// \param theAttribute an attribute where the object and shape are set
240 /// \param theObject an object
241 /// \param theShape a shape
242 /// \param theWorkshop to find an attribute for the given shape for attribute reference
243 /// \param theTemporarily if it is true, do not store and name the added in the data framework
244 /// \param theCheckIfAttributeHasObject if it is true, the check isInList is called
245 /// It is useful for attribute selection
246 /// \return true if the attribute is filled with the given parameters
247 MODULEBASE_EXPORT bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
248 const std::shared_ptr<GeomAPI_Shape>& theShape,
249 ModuleBase_IWorkshop* theWorkshop,
250 const bool theTemporarily,
251 const bool theCheckIfAttributeHasObject);
253 /// Returns the shape of the attribute. If the attribute is AttributeRefAttrPtr, the shape is found
254 /// using current module of the given workshop.
255 /// \param theAttribute an attribute where the shape is set
256 /// \param theWorkshop to find a shape for the given attribute
257 MODULEBASE_EXPORT std::shared_ptr<GeomAPI_Shape> getShape(const AttributePtr& theAttribute,
258 ModuleBase_IWorkshop* theWorkshop);
260 /// Flush updated signal for the object. The viewer update is blocked in the process
261 /// \param theObject parameter of the signal
262 MODULEBASE_EXPORT void flushUpdated(ObjectPtr theObject);
264 /// Sends a message about block/unblock viewer updating
265 /// \param theValue a boolean value
266 MODULEBASE_EXPORT void blockUpdateViewer(const bool theValue);
268 /// Generates a wrapped string to be less than value with '\n' separators
269 /// \param theValue a boolean value
270 /// \param theWidget a widget to know the font
271 /// \param theMaxLineInPixels a maximum line width in pixels
272 MODULEBASE_EXPORT QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
273 int theMaxLineInPixels = 150);
275 /// Generates a locale to disable thousands separator for spin box
276 /// (to avoid inconsistency of double-2-string and string-2-double conversion)
278 MODULEBASE_EXPORT QLocale doubleLocale();
280 /// Returns a container of referenced feature to the current object in the object document.
281 /// \param theObject an object, which will be casted to a feature type
282 /// \param theRefFeatures an output container
283 void MODULEBASE_EXPORT refsToFeatureInFeatureDocument(const ObjectPtr& theObject,
284 std::set<FeaturePtr>& theRefFeatures);
287 /// Returns true if the object if a sub child of the feature. The feature is casted to the
288 /// composite one. If it is possible, the sub object check happens. The method is applyed
289 /// recursively to the feature subs.
290 /// \param theObject a candidate to be a sub object
291 /// \param theFeature a candidate to be a composite feature
292 /// \return a boolean value
293 //bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature);
296 /// Returns true if the result is a sub object of some composite object
297 /// \param theObject a result object
298 /// \returns boolean value
299 //bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject);
302 /// Returns first result of the feature: the object itself if it is a result of
303 /// first result of the object's feature
304 /// \param theObject an object
305 /// \return first result or NULL
306 std::shared_ptr<ModelAPI_Result> MODULEBASE_EXPORT firstResult(const ObjectPtr& theObject);
308 /// Returns true if the list contains at least one feature from the module document(PartSet)
309 /// The Part features are skipped.
310 /// \param theFeatures a list of features
311 /// \bool true if it is found
312 bool MODULEBASE_EXPORT hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures);
314 /// Shows a dialog box about references. Ask whether they should be also removed.
315 /// \param theFeatures a list of features
316 /// \param theReferences a map of all references to the features
317 /// \param theParent a parent widget for the question message box
318 /// \param theReferencesToDelete an out set for references features to be removed
319 /// \return true if in message box answer is Yes
320 bool MODULEBASE_EXPORT askToDelete(const std::set<FeaturePtr> aFeatures,
321 const std::map<FeaturePtr,
322 std::set<FeaturePtr> >& theReferences,
324 std::set<FeaturePtr>& theReferencesToDelete,
325 const std::string& thePrefixInfo = "");
327 /// Converts a list of objects to set of corresponded features. If object is result, it is ingored
328 /// because the feauture only might be removed. But if result is in a parameter group, the feature
329 /// of this parameter is to be removed
330 /// \param theObjects a list of objects
331 /// \param theFeatures an out conteiner of features
332 void MODULEBASE_EXPORT convertToFeatures(const QObjectPtrList& theObjects,
333 std::set<FeaturePtr>& theFeatures);
336 /// Converts a list of objects to set of folders.
337 /// \param theObjects a list of objects
338 /// \param theFeatures an out conteiner of features
339 void MODULEBASE_EXPORT convertToFolders(const QObjectPtrList& theObjects,
340 std::set<FolderPtr>& theFolders);
343 /// Returns translation from the given data.
344 /// If translation is not exists then it returns a string
345 /// from the info data without translation
346 /// \param theMessage a message which dave to be translated
347 QString MODULEBASE_EXPORT translate(const Events_InfoMessage& theMessage);
349 /// Returns translation from the given data.
350 /// If translation is not exists then it returns a string
351 /// from the info data without translation
352 /// \param theContext context of the message (Feature Id)
353 /// \param theMessage a message which dave to be translated
354 QString MODULEBASE_EXPORT translate(const std::string& theContext, const std::string& theMessage);
356 /// Set Highlighting of points as a Ball shape
357 /// \param theAIS - the presentation
358 void MODULEBASE_EXPORT setPointBallHighlighting(AIS_Shape* theAIS);