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