Salome HOME
Use choice control for groups
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetMultiSelector.h
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef MODULEBASE_WIDGETMULTISELECTOR_H_
22 #define MODULEBASE_WIDGETMULTISELECTOR_H_
23
24 #include <ModuleBase.h>
25 #include <ModuleBase_WidgetSelector.h>
26
27 #include <GeomAPI_Shape.h>
28 #include <ModelAPI_Result.h>
29
30 #include <NCollection_List.hxx>
31 #include <TopoDS_Shape.hxx>
32
33 #include <QList>
34 #include <QString>
35 #include <QStringList>
36 #include <QPair>
37 #include <QMap>
38
39 class QWidget;
40 class QListWidget;
41 //class QComboBox;
42 class ModuleBase_IWorkshop;
43 class QAction;
44 class ModuleBase_ChoiceCtrl;
45
46
47 /**
48 * \ingroup GUI
49 * Implementation of widget for shapes selection. This widget provides selection of several shapes.
50 * It can be defined in XML file as following:
51 * \code
52 * <multi_selector id="group_list" 
53 *    tooltip="Select a set of objects" 
54 *    type_choice="Vertices Edges Faces Solids" /> 
55 * \endcode
56 * It uses following parameters:
57 * - id - is a name of corresponded attribute
58 * - tooltip - a tooltip for the widget
59 * - type_choice - list of expected shape types.
60 */
61 class MODULEBASE_EXPORT ModuleBase_WidgetMultiSelector : public ModuleBase_WidgetSelector
62 {
63   Q_OBJECT
64  public:
65   /// Constructor
66   /// \param theParent the parent object
67   /// \param theWorkshop instance of workshop interface
68   /// \param theData the widget configuration. The attribute of the model widget is obtained from
69   ModuleBase_WidgetMultiSelector(QWidget* theParent,
70                                  ModuleBase_IWorkshop* theWorkshop,
71                                  const Config_WidgetAPI* theData);
72   virtual ~ModuleBase_WidgetMultiSelector();
73
74   /// Returns list of widget controls
75   /// \return a control list
76   virtual QList<QWidget*> getControls() const;
77
78   /// The methiod called when widget is deactivated
79   virtual void deactivate();
80
81   /// Set the given wrapped value to the current widget
82   /// This value should be processed in the widget according to the needs
83   /// \param theValues the wrapped selection values
84   /// \param theToValidate a validation of the values flag
85   virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
86                             const bool theToValidate);
87
88   /// Returns values which should be highlighted when the whidget is active
89   /// \param theValues a list of presentations
90   virtual void getHighlighted(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues);
91
92   /// Returns true if the action can be processed. By default it is empty and returns false.
93   /// \param theActionType an action type
94   /// \param isActionEnabled if true, the enable state of the action
95   virtual bool canProcessAction(ModuleBase_ActionType theActionType, bool& isActionEnabled);
96
97   /// Returns true if the event is processed. The default implementation is empty, returns false.
98   virtual bool processAction(ModuleBase_ActionType theActionType);
99
100   /// Activate or deactivate selection and selection filters
101   /// \return true if the selection filter of the widget is activated in viewer context
102   virtual bool activateSelectionAndFilters(bool toActivate);
103
104   /// Checks the widget validity. By default, it returns true.
105   /// \param thePrs a selected presentation in the view
106   /// \return a boolean value
107   virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs);
108
109 public slots:
110   /// Slot is called on selection type changed
111   void onSelectionTypeChanged();
112
113   /// Slot which is called on selection event. Redefined to process XML state about
114   /// clear selection in neutral point
115   virtual void onSelectionChanged();
116
117 protected slots:
118   /// Slot for copy command in a list pop-up menu
119   void onCopyItem();
120
121   /// Slot for delete command in a list pop-up menu
122   void onDeleteItem();
123
124   /// Slot is called on selection of list of selected items
125   void onListSelection();
126
127 protected:
128   /// Returns true if the event is processed. The default implementation is empty, returns false.
129   virtual bool processDelete();
130
131   /// The methiod called when widget is activated
132   virtual void activateCustom();
133
134   /// Saves the internal parameters to the given feature
135   /// \return True in success
136   virtual bool storeValueCustom();
137
138   /// restire type of selection by feature attribute
139   virtual bool restoreValueCustom();
140
141   /// Creates an element of the attribute current selection if history is empty
142   virtual void appendFirstSelectionInHistory();
143
144   /// Create an element in the history that stores the current selection,
145   /// position in the history is incremented
146   void appendSelectionInHistory();
147
148   /// Clear list of stored selected items, reset current position into '-1'
149   void clearSelectedHistory();
150
151   /// Set the focus on the last item in  the list
152   virtual void updateFocus();
153
154   /// Computes and updates name of selected object in the widget
155   virtual void updateSelectionName();
156
157   /// Emits model changed info, updates the current control by selection change
158   /// \param theDone a state whether the selection is set
159   virtual void updateOnSelectionChanged(const bool theDone);
160
161   /// Retunrs a list of possible shape types
162   /// \return a list of shapes
163   virtual QIntList shapeTypes() const;
164
165   /// Set current shape type for selection
166   void setCurrentShapeType(const int theShapeType);
167
168   /// Return the attribute values wrapped in a list of viewer presentations
169   /// \return a list of viewer presentations, which contains an attribute result and
170   /// a shape. If the attribute do not uses the shape, it is empty
171   virtual QList<std::shared_ptr<ModuleBase_ViewerPrs>> getAttributeSelection() const;
172
173   /// Fills the list control by the attribute values
174   void updateSelectionList();
175
176   /// Converts the XML defined type choice to the validator type
177   /// For example, the "Edges" is converted to "edge"
178   std::string validatorType(const QString& theType) const;
179
180   /// Clear selection in the viewer. The content of the widget is cleared also
181   void clearSelection();
182
183 protected:
184   /// Returns attribute indices selected in the widget selection list
185   /// \param theIndices a list of indices
186   void getSelectedAttributeIndices(std::set<int>& theIndices);
187
188   /// Gets the feature attribute and fill a list of viewer presentation for the attribute
189   /// indices. If the the container of indices is empty, it returns all objects.
190   /// \param theAttributeIds indices in attribute list to be returned
191   /// \param theValues the result presentations, filled with object and shape of an attribute item
192   void convertIndicesToViewerSelection(std::set<int> theAttributeIds,
193                             QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues) const;
194
195   /// Iterates throgh the model attribute list and remove elements which do not present in the list
196   /// \param theValues the wrapped selection values
197   /// \return true if at least one object is removed
198   virtual bool removeUnusedAttributeObjects(
199     QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues);
200
201   /// Converts viewer presentation selection list to objects and shapes map
202   /// \param theValues the wrapped selection values
203   /// \return selection list
204   std::map<ObjectPtr, std::set<GeomShapePtr> > convertSelection
205                                           (QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues);
206
207   /// Returns true if the object and shape present in the container
208   /// \param theObject a model object, a set of shapes is searched by it
209   /// \param theShape a shape to be in the set of the object shapes
210   /// \param theGeomSelection a map built on selection
211   /// \param theWorkshop a current workshop
212   /// \return boolean result
213   static bool findInSelection(const ObjectPtr& theObject,
214                             GeomShapePtr theShape,
215                             const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection,
216                             ModuleBase_IWorkshop* theWorkshop);
217
218 protected:
219   /// List control
220   QListWidget* myListControl;
221
222   QStringList myShapeTypes;
223
224   /// Control for types
225   ModuleBase_ChoiceCtrl* myTypeCtrl;
226
227   /// Provides correspondance between Result object and its shape
228   typedef QPair<ResultPtr, GeomShapePtr> GeomSelection;
229
230   /// A copy action for pop-up menu in a list control
231   QAction* myCopyAction;
232
233   /// A delete action for pop-up menu in a list control
234   QAction* myDeleteAction;
235
236   /// A flag to store use_choice parameter state
237   bool myIsUseChoice;
238
239   /// A flag to clear selection by click in empty place in the viewer
240   bool myIsNeutralPointClear;
241
242   /// A flag to block set selection perform if the method is in process
243   bool myIsSetSelectionBlocked;
244
245   /// A container of selected objects
246   QList<QList<std::shared_ptr<ModuleBase_ViewerPrs> > > mySelectedHistoryValues;
247   /// Position in a container of selected values
248   int myCurrentHistoryIndex;
249 };
250
251 #endif /* MODULEBASE_WIDGETFILESELECTOR_H_ */