Salome HOME
bebe4123b98cf30ddf8078088d33803dca11ce3b
[modules/shaper.git] / ModuleBase_WidgetMultiSelector.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_WidgetMultiSelector.h
5  *
6  *  Created on: Oct 8, 2014
7  *      Author: sbh
8  */
9
10 #ifndef MODULEBASE_WIDGETMULTISELECTOR_H_
11 #define MODULEBASE_WIDGETMULTISELECTOR_H_
12
13 #include <ModuleBase.h>
14 #include <ModuleBase_WidgetSelector.h>
15
16 #include <GeomAPI_Shape.h>
17 #include <ModelAPI_Result.h>
18
19 #include <NCollection_List.hxx>
20 #include <TopoDS_Shape.hxx>
21
22 #include <QList>
23 #include <QString>
24 #include <QStringList>
25 #include <QPair>
26 #include <QMap>
27
28 class QWidget;
29 class QListWidget;
30 class QComboBox;
31 class ModuleBase_IWorkshop;
32 class QAction;
33
34
35 /**
36 * \ingroup GUI
37 * Implementation of widget for shapes selection. This widget provides selection of several shapes.
38 * It can be defined in XML file as following:
39 * \code
40 * <multi_selector id="group_list" 
41 *    tooltip="Select a set of objects" 
42 *    type_choice="Vertices Edges Faces Solids" /> 
43 * \endcode
44 * It uses following parameters:
45 * - id - is a name of corresponded attribute
46 * - tooltip - a tooltip for the widget
47 * - type_choice - list of expected shape types.
48 */
49 class MODULEBASE_EXPORT ModuleBase_WidgetMultiSelector : public ModuleBase_WidgetSelector
50 {
51   Q_OBJECT
52  public:
53   /// Constructor
54   /// \param theParent the parent object
55   /// \param theWorkshop instance of workshop interface
56   /// \param theData the widget configuration. The attribute of the model widget is obtained from
57   /// \param theParentId is Id of a parent of the current attribute
58   ModuleBase_WidgetMultiSelector(QWidget* theParent,
59                                  ModuleBase_IWorkshop* theWorkshop,
60                                  const Config_WidgetAPI* theData,
61                                  const std::string& theParentId);
62   virtual ~ModuleBase_WidgetMultiSelector();
63
64   /// Returns list of widget controls
65   /// \return a control list
66   virtual QList<QWidget*> getControls() const;
67
68   /// The methiod called when widget is deactivated
69   virtual void deactivate();
70
71   /// Set the given wrapped value to the current widget
72   /// This value should be processed in the widget according to the needs
73   /// \param theValues the wrapped selection values
74   /// \param theToValidate a validation of the values flag
75   virtual bool setSelection(QList<ModuleBase_ViewerPrs>& theValues,
76                             const bool theToValidate);
77
78   /// Returns values which should be highlighted when the whidget is active
79   /// \param theValues a list of presentations
80   virtual void getHighlighted(QList<ModuleBase_ViewerPrs>& theValues);
81
82   /// Checks the widget validity. By default, it returns true.
83   /// \param thePrs a selected presentation in the view
84   /// \return a boolean value
85   virtual bool isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs);
86
87   /// Returns true if the event is processed. The default implementation is empty, returns false.
88   virtual bool processDelete();
89
90 public slots:
91   /// Slot is called on selection type changed
92   void onSelectionTypeChanged();
93
94 protected slots:
95   /// Slot for copy command in a list pop-up menu
96   void onCopyItem();
97
98   /// Slot for delete command in a list pop-up menu
99   void onDeleteItem();
100
101   /// Slot is called on selection of list of selected items
102   void onListSelection();
103
104 protected:
105   /// The methiod called when widget is activated
106   virtual void activateCustom();
107
108   /// Saves the internal parameters to the given feature
109   /// \return True in success
110   virtual bool storeValueCustom() const;
111
112   virtual bool restoreValueCustom();
113
114   /// Creates a backup of the current values of the attribute
115   /// It should be realized in the specific widget because of different
116   /// parameters of the current attribute
117   virtual void storeAttributeValue();
118
119   /// Set the focus on the last item in  the list
120   virtual void updateFocus();
121
122   /// Computes and updates name of selected object in the widget
123   virtual void updateSelectionName();
124
125   /// Retunrs a list of possible shape types
126   /// \return a list of shapes
127   virtual QIntList getShapeTypes() const;
128
129   /// Append the values to the model attribute of the widget. It casts this attribute to
130   /// the specific type and set the given values
131   /// \param theSelectedObject an object
132   /// \param theShape a selected shape, which is used in the selection attribute
133   /// \return true if it is succeed
134   void setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape);
135
136   /// Creates a backup of the current values of the attribute
137   /// It should be realized in the specific widget because of different
138   /// parameters of the current attribute
139   /// \param theValid a boolean flag, if restore happens for valid parameters
140   virtual void restoreAttributeValue(const bool theValid);
141
142   /// Set current shape type for selection
143   void setCurrentShapeType(const TopAbs_ShapeEnum theShapeType);
144
145   /// Return the attribute values wrapped in a list of viewer presentations
146   /// \return a list of viewer presentations, which contains an attribute result and
147   /// a shape. If the attribute do not uses the shape, it is empty
148   virtual QList<ModuleBase_ViewerPrs> getAttributeSelection() const;
149
150   /// Fills the list control by the attribute values
151   void updateSelectionList();
152
153   /// Converts the XML defined type choice to the validator type
154   /// For example, the "Edges" is converted to "edge"
155   std::string validatorType(const QString& theType) const;
156
157 protected:
158   /// Returns attribute indices selected in the widget selection list 
159   /// \param theIndices a list of indices
160   void getSelectedAttributeIndices(std::set<int>& theIndices);
161
162   /// Gets the feature attribute and fill a list of viewer presentation for the attribute
163   /// indices. If the the container of indices is empty, it returns all objects.
164   /// \param theAttributeIds indices in attribute list to be returned
165   /// \param theValues the result presentations, filled with object and shape of an attribute item
166   void convertIndicesToViewerSelection(std::set<int> theAttributeIds,
167                                        QList<ModuleBase_ViewerPrs>& theValues) const;
168
169   /// Iterates throgh the model attribute list and remove elements which do not present in the list
170   /// \param theValues the wrapped selection values
171   virtual void removeUnusedAttributeObjects(QList<ModuleBase_ViewerPrs>& theValues);
172
173   /// Converts viewer presentation selection list to objects and shapes map
174   /// \param theValues the wrapped selection values
175   /// \return selection list
176   std::map<ObjectPtr, std::set<GeomShapePtr> > convertSelection
177                                           (QList<ModuleBase_ViewerPrs>& theValues);
178
179   /// Returns true if the object and shape present in the container
180   /// \param theObject a model object, a set of shapes is searched by it
181   /// \param theShape a shape to be in the set of the object shapes
182   /// \param theGeomSelection a map built on selection
183   /// \return boolean result
184   static bool findInSelection(const ObjectPtr& theObject,
185                             const GeomShapePtr& theShape,
186                             const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection);
187
188 protected:
189   /// List control
190   QListWidget* myListControl;
191
192   /// Combobox of types
193   QComboBox* myTypeCombo;
194
195   /// Provides correspondance between Result object and its shape
196   typedef QPair<ResultPtr, GeomShapePtr> GeomSelection;
197
198   /// An action for pop-up menu in a list control
199   QAction* myCopyAction;
200   QAction* myDeleteAction;
201
202   /// backup parameters of the model attribute. The class processes three types of attribute:
203   /// Reference, RefAttr and Selection. Depending on the attribute type, only the attribute parameter
204   /// values are reserved in the backup
205   /// Variable of selection type
206   std::string mySelectionType;
207
208   /// Variable of GeomSelection
209   int mySelectionCount; // number of elements in the attribute selection list when store
210
211   /// A flag to store use_choice parameter state
212   bool myIsUseChoice;
213 };
214
215 #endif /* MODULEBASE_WIDGETFILESELECTOR_H_ */