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