Salome HOME
Issue #2154 Multiselection field is cleared when trying to add another object
[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   ModuleBase_WidgetMultiSelector(QWidget* theParent,
58                                  ModuleBase_IWorkshop* theWorkshop,
59                                  const Config_WidgetAPI* theData);
60   virtual ~ModuleBase_WidgetMultiSelector();
61
62   /// Returns list of widget controls
63   /// \return a control list
64   virtual QList<QWidget*> getControls() const;
65
66   /// The methiod called when widget is deactivated
67   virtual void deactivate();
68
69   /// Set the given wrapped value to the current widget
70   /// This value should be processed in the widget according to the needs
71   /// \param theValues the wrapped selection values
72   /// \param theToValidate a validation of the values flag
73   virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
74                             const bool theToValidate);
75
76   /// Returns values which should be highlighted when the whidget is active
77   /// \param theValues a list of presentations
78   virtual void getHighlighted(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues);
79
80   /// Checks the widget validity. By default, it returns true.
81   /// \param thePrs a selected presentation in the view
82   /// \return a boolean value
83   virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs);
84
85   /// Returns true if the event is processed. The default implementation is empty, returns false.
86   virtual bool processDelete();
87
88 public slots:
89   /// Slot is called on selection type changed
90   void onSelectionTypeChanged();
91
92   /// Slot which is called on selection event. Redefined to process XML state about
93   /// clear selection in neutral point
94   virtual void onSelectionChanged();
95
96 protected slots:
97   /// Slot for copy command in a list pop-up menu
98   void onCopyItem();
99
100   /// Slot for delete command in a list pop-up menu
101   void onDeleteItem();
102
103   /// Slot is called on selection of list of selected items
104   void onListSelection();
105
106 protected:
107   /// The methiod called when widget is activated
108   virtual void activateCustom();
109
110   /// Saves the internal parameters to the given feature
111   /// \return True in success
112   virtual bool storeValueCustom();
113
114   virtual bool restoreValueCustom();
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   /// Emits model changed info, updates the current control by selection change
123   /// \param theDone a state whether the selection is set
124   virtual void updateOnSelectionChanged(const bool theDone);
125
126   /// Retunrs a list of possible shape types
127   /// \return a list of shapes
128   virtual QIntList shapeTypes() const;
129
130   /// Set current shape type for selection
131   void setCurrentShapeType(const int theShapeType);
132
133   /// Return the attribute values wrapped in a list of viewer presentations
134   /// \return a list of viewer presentations, which contains an attribute result and
135   /// a shape. If the attribute do not uses the shape, it is empty
136   virtual QList<std::shared_ptr<ModuleBase_ViewerPrs>> getAttributeSelection() const;
137
138   /// Fills the list control by the attribute values
139   void updateSelectionList();
140
141   /// Converts the XML defined type choice to the validator type
142   /// For example, the "Edges" is converted to "edge"
143   std::string validatorType(const QString& theType) const;
144
145   /// Clear selection in the viewer. The content of the widget is cleared also
146   void clearSelection();
147
148 protected:
149   /// Returns attribute indices selected in the widget selection list
150   /// \param theIndices a list of indices
151   void getSelectedAttributeIndices(std::set<int>& theIndices);
152
153   /// Gets the feature attribute and fill a list of viewer presentation for the attribute
154   /// indices. If the the container of indices is empty, it returns all objects.
155   /// \param theAttributeIds indices in attribute list to be returned
156   /// \param theValues the result presentations, filled with object and shape of an attribute item
157   void convertIndicesToViewerSelection(std::set<int> theAttributeIds,
158                             QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues) const;
159
160   /// Iterates throgh the model attribute list and remove elements which do not present in the list
161   /// \param theValues the wrapped selection values
162   /// \return true if at least one object is removed
163   virtual bool removeUnusedAttributeObjects(
164     QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues);
165
166   /// Converts viewer presentation selection list to objects and shapes map
167   /// \param theValues the wrapped selection values
168   /// \return selection list
169   std::map<ObjectPtr, std::set<GeomShapePtr> > convertSelection
170                                           (QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues);
171
172   /// Returns true if the object and shape present in the container
173   /// \param theObject a model object, a set of shapes is searched by it
174   /// \param theShape a shape to be in the set of the object shapes
175   /// \param theGeomSelection a map built on selection
176   /// \return boolean result
177   static bool findInSelection(const ObjectPtr& theObject,
178                             const GeomShapePtr& theShape,
179                             const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection);
180
181 protected:
182   /// List control
183   QListWidget* myListControl;
184
185   /// Combobox of types
186   QComboBox* myTypeCombo;
187
188   /// Provides correspondance between Result object and its shape
189   typedef QPair<ResultPtr, GeomShapePtr> GeomSelection;
190
191   /// A copy action for pop-up menu in a list control
192   QAction* myCopyAction;
193
194   /// A delete action for pop-up menu in a list control
195   QAction* myDeleteAction;
196
197   /// A flag to store use_choice parameter state
198   bool myIsUseChoice;
199
200   /// A flag to clear selection by click in empty place in the viewer
201   bool myIsNeutralPointClear;
202
203   /// A flag to block set selection perform if the method is in process
204   bool myIsSetSelectionBlocked;
205 };
206
207 #endif /* MODULEBASE_WIDGETFILESELECTOR_H_ */