Salome HOME
591d842ad91156746ed3d07174e9a8891e69eb08
[modules/shaper.git] / src / ModuleBase / 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   /// Set the given wrapped value to the current widget
69   /// This value should be processed in the widget according to the needs
70   /// \param theValues the wrapped selection values
71   /// \param theToValidate a validation of the values flag
72   virtual bool setSelection(QList<ModuleBase_ViewerPrs>& theValues,
73                             const bool theToValidate);
74
75   /// Returns values which should be highlighted when the whidget is active
76   /// \param theValues a list of presentations
77   virtual void getHighlighted(QList<ModuleBase_ViewerPrs>& theValues);
78
79   /// Checks the widget validity. By default, it returns true.
80   /// \param thePrs a selected presentation in the view
81   /// \return a boolean value
82   virtual bool isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs);
83
84   /// Returns true if the event is processed. The default implementation is empty, returns false.
85   virtual bool processDelete();
86
87 public slots:
88   /// Slot is called on selection type changed
89   void onSelectionTypeChanged();
90
91 protected slots:
92   /// Slot for copy command in a list pop-up menu
93   void onCopyItem();
94
95   /// Slot for delete command in a list pop-up menu
96   void onDeleteItem();
97
98   /// Slot is called on selection of list of selected items
99   void onListSelection();
100
101 protected:
102   /// Saves the internal parameters to the given feature
103   /// \return True in success
104   virtual bool storeValueCustom() const;
105
106   virtual bool restoreValueCustom();
107
108   /// Creates a backup of the current values of the attribute
109   /// It should be realized in the specific widget because of different
110   /// parameters of the current attribute
111   virtual void storeAttributeValue();
112
113   /// Clear attribute
114   virtual void clearAttribute();
115
116   /// Set the focus on the last item in  the list
117   virtual void updateFocus();
118
119   /// Computes and updates name of selected object in the widget
120   virtual void updateSelectionName();
121
122   /// Retunrs a list of possible shape types
123   /// \return a list of shapes
124   virtual QIntList getShapeTypes() const;
125
126   /// Append the values to the model attribute of the widget. It casts this attribute to
127   /// the specific type and set the given values
128   /// \param theSelectedObject an object
129   /// \param theShape a selected shape, which is used in the selection attribute
130   /// \return true if it is succeed
131   void setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape);
132
133   /// Creates a backup of the current values of the attribute
134   /// It should be realized in the specific widget because of different
135   /// parameters of the current attribute
136   /// \param theValid a boolean flag, if restore happens for valid parameters
137   virtual void restoreAttributeValue(const bool theValid);
138
139   /// Set current shape type for selection
140   void setCurrentShapeType(const TopAbs_ShapeEnum theShapeType);
141
142   /// Return the attribute values wrapped in a list of viewer presentations
143   /// \return a list of viewer presentations, which contains an attribute result and
144   /// a shape. If the attribute do not uses the shape, it is empty
145   virtual QList<ModuleBase_ViewerPrs> getAttributeSelection() const;
146
147   /// Fills the list control by the attribute values
148   void updateSelectionList();
149
150   /// Converts the XML defined type choice to the validator type
151   /// For example, the "Edges" is converted to "edge"
152   std::string validatorType(const QString& theType) const;
153
154 protected:
155   /// Returns attribute indices selected in the widget selection list 
156   /// \param theIndices a list of indices
157   void getSelectedAttributeIndices(std::set<int>& theIndices);
158
159   /// Gets the feature attribute and fill a list of viewer presentation for the attribute
160   /// indices. If the the container of indices is empty, it returns all objects.
161   /// \param theAttributeIds indices in attribute list to be returned
162   /// \param theValues the result presentations, filled with object and shape of an attribute item
163   void convertIndicesToViewerSelection(std::set<int> theAttributeIds,
164                                        QList<ModuleBase_ViewerPrs>& theValues) const;
165 protected:
166   /// List control
167   QListWidget* myListControl;
168
169   /// Combobox of types
170   QComboBox* myTypeCombo;
171
172   /// Provides correspondance between Result object and its shape
173   typedef QPair<ResultPtr, GeomShapePtr> GeomSelection;
174
175   /// An action for pop-up menu in a list control
176   QAction* myCopyAction;
177   QAction* myDeleteAction;
178
179   /// backup parameters of the model attribute. The class processes three types of attribute:
180   /// Reference, RefAttr and Selection. Depending on the attribute type, only the attribute parameter
181   /// values are reserved in the backup
182   /// Variable of selection type
183   std::string mySelectionType;
184
185   /// Variable of GeomSelection
186   int mySelectionCount; // number of elements in the attribute selection list when store
187
188   /// A flag to store use_choice parameter state
189   bool myIsUseChoice;
190 };
191
192 #endif /* MODULEBASE_WIDGETFILESELECTOR_H_ */