1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #ifndef ModuleBase_Tools_H
21 #define ModuleBase_Tools_H
23 #include "ModuleBase.h"
24 #include "ModuleBase_Definitions.h"
26 #include <ModelAPI_Attribute.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_Folder.h>
30 #include <TopAbs_ShapeEnum.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <Prs3d_Drawer.hxx>
33 #include <AIS_Shape.hxx>
35 #include <Quantity_Color.hxx>
47 class ModuleBase_IWorkshop;
48 class ModuleBase_ParamIntSpinBox;
49 class ModuleBase_ParamSpinBox;
50 class ModuleBase_ViewerPrs;
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, otherwise 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 /// Create composite pixmap.
99 /// Pixmap \a theAdditionalIcon is drawn over pixmap \a dest with coordinates
100 /// specified relatively to the upper left corner of \a theIcon.
102 /// \param theAdditionalIcon additional pixmap
103 /// \param theIcon background pixmap
104 /// \return resulting pixmap
105 MODULEBASE_EXPORT QPixmap composite(const QImage& theAdditionalIcon, QImage& theIcon);
107 /// Generates the pixmap lighter than the resources pixmap.
108 /// Pixmap \a theIcon is lighted according to the given value.
109 /// If the lighter value is greater than 100, this functions returns a lighter pixmap.
110 /// Setting lighter value to 150 returns a color that is 50% brighter.
111 /// If the factor is less than 100,
112 /// the return pixmap is darker. If the factor is 0 or negative, the return pixmap is unspecified.
114 /// \param resource text of the pixmap
115 /// \param theLighterValue a lighter factor
116 /// \return resulting pixmap
117 MODULEBASE_EXPORT QPixmap lighter(const QString& theIcon, const int theLighterValue = 200);
119 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
120 /// \param theSpin an X or Y coordinate widget
121 /// \param theValue a new value
122 MODULEBASE_EXPORT void setSpinValue(QDoubleSpinBox* theSpin, double theValue);
124 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
125 /// \param theSpin an X or Y coordinate widget
126 /// \param theValue a new value
127 MODULEBASE_EXPORT void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue);
129 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
130 /// \param theSpin an ModuleBase_ParamSpinBox that accepts text
131 /// \param theText a new value
132 MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText);
134 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
135 /// \param theSpin a ModuleBase_ParamIntSpinBox object
136 /// \param theValue a new value
137 MODULEBASE_EXPORT void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue);
139 /// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
140 /// \param theSpin a SpinBox that accepts text
141 /// \param theText a new value
142 MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText);
144 /// Creates an action filled with the parameters
145 /// \param theIcon an action icon
146 /// \param theText an icon text
147 /// \param theParent an action object
148 /// \param theReceiver an object to process action triggered signal
149 /// \param theMember a slot to process the action triggered signal
150 /// \param theToolTip an action tool tip information, if empty the text is used
151 /// \param theStatusTip an action status tip information, if empty the text is used
152 MODULEBASE_EXPORT QAction* createAction(const QIcon& theIcon, const QString& theText,
153 QObject* theParent, const QObject* theReceiver = 0,
154 const char* theMember = "",
155 const QString& theToolTip = QString(),
156 const QString& theStatusTip = QString());
159 /// Returns name of data of the object if it is not NULL
160 /// \param theObj an object
162 MODULEBASE_EXPORT QString objectName(const ObjectPtr& theObj);
164 /// Converts the object to the feature or a result and generate information string
165 /// \param theObj an object
166 /// \param isUseAttributesInfo a flag whether the attribute values information is used
168 MODULEBASE_EXPORT QString objectInfo(const ObjectPtr& theObj,
169 const bool isUseAttributesInfo = false);
172 /// Converts string value (name of shape type) to shape enum value
173 /// \param theType - string with shape type name
174 /// \return TopAbs_ShapeEnum value
175 MODULEBASE_EXPORT int shapeType(const QString& theType);
177 /// Checks whether the object is a sub result. It gets the feature of the object,
178 /// obtains all results of the feature and check if the object is a sub result
179 /// \return boolean result
180 MODULEBASE_EXPORT bool isSubResult(ObjectPtr theObject);
182 /// Check types of objects which are in the given list
183 /// \param theObjects the list of objects
184 /// \param hasResult will be set to true if list contains Result objects
185 /// \param hasFeature will be set to true if list contains Feature objects
186 /// \param hasParameter will be set to true if list contains Parameter objects
187 /// \param hasCompositeOwner will be set to true if list contains Sub-Feature objects
188 /// \param hasResultInHistory will be set to true if one of result is in history
189 /// \param hasFolder will be set to true if one of folder is in the list
190 MODULEBASE_EXPORT void checkObjects(const QObjectPtrList& theObjects, bool& hasResult,
191 bool& hasFeature, bool& hasParameter, bool& hasCompositeOwner,
192 bool& hasResultInHistory, bool& hasFolder);
194 /// Sets the default coeffient into the driver calculated accordingly the shape type.
195 /// It provides 1.e-4 for results of construction type
196 /// \param theResult a result object to define the deviation coefficient
197 /// \param theDrawer a drawer
198 //MODULEBASE_EXPORT void setDefaultDeviationCoefficient(
199 // const std::shared_ptr<ModelAPI_Result>& theResult,
200 // const Handle(Prs3d_Drawer)& theDrawer);
202 /// Sets the default coeffient into the driver calculated accordingly the shape type.
203 /// It provides 1.e-4 for a shape withe Edge shape type
204 /// \param theShape a shape to define the deviation coefficient,
205 /// \param theDrawer a drawer
206 MODULEBASE_EXPORT void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
207 const Handle(Prs3d_Drawer)& theDrawer);
209 /// Obtains the color from the property manager and converts it to the OCCT color
210 /// \param theSection a property section
211 /// \param theName a property item name
212 /// \return quantity color
213 MODULEBASE_EXPORT Quantity_Color color(const std::string& theSection,
214 const std::string& theName);
216 /// Returns the object from the attribute
217 /// \param theObj an object
218 MODULEBASE_EXPORT ObjectPtr getObject(const AttributePtr& theAttribute);
220 /// Returns the object from the attribute
221 /// \param theObj an object
222 MODULEBASE_EXPORT TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape);
224 /// Returns either presentation shape or shape of BREP owner if it is casted to it
225 /// \param thePrs selection presentation
227 MODULEBASE_EXPORT TopoDS_Shape getSelectedShape(
228 const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs);
230 /// Returns list of parameters accessible in the active part and partset
231 /// \theParameters a list of parameter names
232 MODULEBASE_EXPORT void getParameters(QStringList& theParameters);
234 /// Returns list of parameters accessible in the active part and partset
235 /// \theParameters a list of parameter names
236 MODULEBASE_EXPORT std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
237 const FeaturePtr& theFeature);
239 /// Set the object to the attribute depending on the attribute type. If it is a list,
240 /// the values are appended if they are not in the list yet.
241 /// \param theAttribute an attribute where the object and shape are set
242 /// \param theObject an object
243 /// \param theShape a shape
244 /// \param theWorkshop to find an attribute for the given shape for attribute reference
245 /// \param theTemporarily if it is true, do not store and name the added in the data framework
246 /// It is useful for attribute selection
247 MODULEBASE_EXPORT bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
248 const std::shared_ptr<GeomAPI_Shape>& theShape,
249 ModuleBase_IWorkshop* theWorkshop,
250 const bool theTemporarily);
252 /// Set the object to the attribute depending on the attribute type. If it is a list,
253 /// the values are appended if they are not in the list yet.
254 /// \param theAttribute an attribute where the object and shape are set
255 /// \param theObject an object
256 /// \param theShape a shape
257 /// \param theWorkshop to find an attribute for the given shape for attribute reference
258 /// \param theTemporarily if it is true, do not store and name the added in the data framework
259 /// \param theCheckIfAttributeHasObject if it is true, the check isInList is called
260 /// It is useful for attribute selection
261 /// \return true if the attribute is filled with the given parameters
262 MODULEBASE_EXPORT bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
263 const std::shared_ptr<GeomAPI_Shape>& theShape,
264 ModuleBase_IWorkshop* theWorkshop,
265 const bool theTemporarily,
266 const bool theCheckIfAttributeHasObject);
268 /// Returns the shape of the attribute. If the attribute is AttributeRefAttrPtr, the shape is found
269 /// using current module of the given workshop.
270 /// \param theAttribute an attribute where the shape is set
271 /// \param theWorkshop to find a shape for the given attribute
272 MODULEBASE_EXPORT std::shared_ptr<GeomAPI_Shape> getShape(const AttributePtr& theAttribute,
273 ModuleBase_IWorkshop* theWorkshop);
275 /// Flush updated signal for the object. The viewer update is blocked in the process
276 /// \param theObject parameter of the signal
277 MODULEBASE_EXPORT void flushUpdated(ObjectPtr theObject);
279 /// Sends a message about block/unblock viewer updating
280 /// \param theValue a boolean value
281 MODULEBASE_EXPORT void blockUpdateViewer(const bool theValue);
283 /// Generates a wrapped string to be less than value with '\n' separators
284 /// \param theValue a boolean value
285 /// \param theWidget a widget to know the font
286 /// \param theMaxLineInPixels a maximum line width in pixels
287 MODULEBASE_EXPORT QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
288 int theMaxLineInPixels = 150);
290 /// Generates a locale to disable thousands separator for spin box
291 /// (to avoid inconsistency of double-2-string and string-2-double conversion)
293 MODULEBASE_EXPORT QLocale doubleLocale();
295 /// Returns a container of referenced feature to the current object in the object document.
296 /// \param theObject an object, which will be casted to a feature type
297 /// \param theRefFeatures an output container
298 void MODULEBASE_EXPORT refsToFeatureInFeatureDocument(const ObjectPtr& theObject,
299 std::set<FeaturePtr>& theRefFeatures);
302 /// Returns true if the object if a sub child of the feature. The feature is casted to the
303 /// composite one. If it is possible, the sub object check happens. The method is applied
304 /// recursively to the feature subs.
305 /// \param theObject a candidate to be a sub object
306 /// \param theFeature a candidate to be a composite feature
307 /// \return a boolean value
308 //bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature);
311 /// Returns true if the result is a sub object of some composite object
312 /// \param theObject a result object
313 /// \returns boolean value
314 //bool MODULEBASE_EXPORT isSubOfComposite(const ObjectPtr& theObject);
317 /// Returns first result of the feature: the object itself if it is a result of
318 /// first result of the object's feature
319 /// \param theObject an object
320 /// \return first result or NULL
321 std::shared_ptr<ModelAPI_Result> MODULEBASE_EXPORT firstResult(const ObjectPtr& theObject);
323 /// Returns true if the list contains at least one feature from the module document(PartSet)
324 /// The Part features are skipped.
325 /// \param theFeatures a list of features
326 /// \bool true if it is found
327 bool MODULEBASE_EXPORT hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures);
329 /// Shows a dialog box about references. Ask whether they should be also removed.
330 /// \param theFeatures a list of features
331 /// \param theReferences a map of all references to the features
332 /// \param theParent a parent widget for the question message box
333 /// \param theReferencesToDelete an out set for references features to be removed
334 /// \return true if in message box answer is Yes
335 bool MODULEBASE_EXPORT askToDelete(const std::set<FeaturePtr> aFeatures,
336 const std::map<FeaturePtr,
337 std::set<FeaturePtr> >& theReferences,
339 std::set<FeaturePtr>& theReferencesToDelete,
340 const std::string& thePrefixInfo = "");
342 /// Converts a list of objects to set of corresponded features. If object is result, it is ignored
343 /// because the feature only might be removed. But if result is in a parameter group, the feature
344 /// of this parameter is to be removed
345 /// \param theObjects a list of objects
346 /// \param theFeatures an out container of features
347 void MODULEBASE_EXPORT convertToFeatures(const QObjectPtrList& theObjects,
348 std::set<FeaturePtr>& theFeatures);
351 /// Converts a list of objects to set of folders.
352 /// \param theObjects a list of objects
353 /// \param theFeatures an out container of features
354 void MODULEBASE_EXPORT convertToFolders(const QObjectPtrList& theObjects,
355 std::set<FolderPtr>& theFolders);
358 /// Returns translation from the given data.
359 /// If translation is not exists then it returns a string
360 /// from the info data without translation
361 /// \param theMessage a message which have to be translated
362 QString MODULEBASE_EXPORT translate(const Events_InfoMessage& theMessage);
364 /// Returns translation from the given data.
365 /// If translation is not exists then it returns a string
366 /// from the info data without translation
367 /// \param theContext context of the message (Feature Id)
368 /// \param theMessage a message which have to be translated
369 QString MODULEBASE_EXPORT translate(const std::string& theContext, const std::string& theMessage);
371 /// Set Highlighting of points as a Ball shape
372 /// \param theAIS - the presentation
373 void MODULEBASE_EXPORT setPointBallHighlighting(AIS_Shape* theAIS);
375 /// Creates a parameter from a given string
376 /// \theText a text wit equation
377 FeaturePtr MODULEBASE_EXPORT createParameter(const QString& theText);
379 /// Edits parameter replacing its name and expression according to the given string
380 /// \theParam a editing parameter
381 /// \theText a text wit equation
382 void MODULEBASE_EXPORT editParameter(FeaturePtr theParam, const QString& theText);
384 /// Returns True if a parameter with the given name already exists
385 /// \theName a name of parameter
386 bool MODULEBASE_EXPORT isNameExist(const QString& theName, FeaturePtr theIgnoreParameter);
388 /// Find parameter by its name
389 /// \theName a name of parameter
390 FeaturePtr MODULEBASE_EXPORT findParameter(const QString& theName);
393 //----------- Class members -------------
394 /// Returns a name in the next form: attribute_feature_name/attribute_id
395 /// \param theAttribute a model attribute
396 /// \param theWorkshop a workshop class instance
397 /// \return string value
398 std::string MODULEBASE_EXPORT generateName(const AttributePtr& theAttribute,
399 ModuleBase_IWorkshop* theWorkshop);