Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.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 XGUI_ObjectsBrowser_H
22 #define XGUI_ObjectsBrowser_H
23
24 #include "XGUI.h"
25 #include <ModuleBase_Definitions.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_ResultPart.h>
28 #include <ModelAPI_Events.h>
29 #include <ModelAPI_Folder.h>
30
31 #include <QWidget>
32 #include <QTreeView>
33 #include <QLabel>
34 #include <QMap>
35
36 class ModuleBase_IDocumentDataModel;
37 class XGUI_DataModel;
38 class Config_DataModelReader;
39 class XGUI_Workshop;
40 class ModuleBase_ITreeNode;
41
42 //#define DEBUG_INDXES
43
44 /**
45 * \ingroup GUI
46 * Implementation of root label in Object Browser
47 */
48 class XGUI_ActiveDocLbl: public QLabel
49 {
50 Q_OBJECT
51  public:
52    /// Constructor
53    /// \param theText a text
54    /// \param theParent a parent widget
55    XGUI_ActiveDocLbl(const QString& theText, QWidget* theParent );
56
57    /// Sets tree view
58    /// \param theView a view
59    void setTreeView(QTreeView* theView);
60
61    /// Returns tree view
62    QTreeView* treePalette() const { return myTreeView;}
63
64 #if (!defined HAVE_SALOME) && (defined WIN32)
65    virtual bool event(QEvent* theEvent);
66 #endif
67
68 public slots:
69   /// On unselect
70   void unselect();
71
72 protected:
73   /// On mouse release
74   virtual void mouseReleaseEvent( QMouseEvent* e);
75
76   /// Filter event
77   bool eventFilter(QObject* theObj, QEvent* theEvent);
78
79 private:
80   QString myPreSelectionStyle;
81   QString myNeutralStyle;
82   QString mySelectionStyle;
83
84   QTreeView* myTreeView;
85   bool myIsSelected;
86 };
87
88
89 /**
90 * \ingroup GUI
91 * Implementation of Data Tree object for Object Browser
92 */
93 class XGUI_EXPORT XGUI_DataTree : public QTreeView
94 {
95 Q_OBJECT
96  public:
97    /// Constructor
98    /// \param theParent a parent widget
99   XGUI_DataTree(QWidget* theParent);
100
101   virtual ~XGUI_DataTree();
102
103   /// Returns current data model
104   XGUI_DataModel* dataModel() const;
105
106 signals:
107   //! Emited on context menu request
108   void contextMenuRequested(QContextMenuEvent* theEvent);
109
110 public slots:
111   /// Clear content of data tree
112   virtual void clear();
113
114  protected slots:
115   /// Commit modified data (used for renaming of objects)
116   virtual void commitData(QWidget* theEditor);
117
118   /// A slot which is called on mouse double click
119   void onDoubleClick(const QModelIndex&);
120
121  protected:
122    /// Redefinition of virtual method
123   virtual void contextMenuEvent(QContextMenuEvent* theEvent);
124
125    /// Redefinition of virtual method
126   virtual void resizeEvent(QResizeEvent* theEvent);
127
128    /// Redefinition of virtual method
129   virtual void mouseReleaseEvent(QMouseEvent* theEvent);
130
131 #ifdef DEBUG_INDXES
132   virtual void mousePressEvent(QMouseEvent* theEvent);
133 #endif
134
135 private:
136   /// Process a history change request
137   /// \param theIndex a clicked data index
138   void processHistoryChange(const QModelIndex& theIndex);
139
140   /// Process a visibility change request
141   /// \param theIndex a clicked data index
142   void processEyeClick(const QModelIndex& theIndex);
143 };
144
145 /**\class XGUI_ObjectsBrowser
146  * \ingroup GUI
147  * \brief Object browser window object. Represents data tree of current data structure
148  */
149 class XGUI_EXPORT XGUI_ObjectsBrowser : public QWidget
150 {
151 Q_OBJECT
152  public:
153    /// Constructor
154    /// \param theParent a parent widget
155   XGUI_ObjectsBrowser(QWidget* theParent, XGUI_Workshop* theWorkshop);
156   virtual ~XGUI_ObjectsBrowser();
157
158   //! Returns Model which provides access to data objects
159   XGUI_DataModel* dataModel() const
160   {
161     return myDocModel;
162   }
163
164   //! Returns list of currently selected objects
165   //! \param theIndexes - output list of corresponded indexes (can be NULL)
166   QObjectPtrList selectedObjects(QModelIndexList* theIndexes = 0) const;
167
168   /// Set selected list of objects
169   /// \param theObjects list of objects to select
170   void setObjectsSelected(const QObjectPtrList& theObjects);
171
172   //! Scroll TreeView to make given object visible
173   //! \param theObject object to make it visible
174   void ensureVisible(const ObjectPtr theObject);
175
176   //! Returns currently selected indexes
177   QModelIndexList selectedIndexes() const
178   {
179     if (myTreeView->selectionModel())
180       return myTreeView->selectionModel()->selectedIndexes();
181     else
182       return QModelIndexList();
183   }
184
185   //! Returns TreeView widget
186   XGUI_DataTree* treeView() const
187   {
188     return myTreeView;
189   }
190
191   /// Returns active doc label object
192   QLabel* activeDocLabel() const { return myActiveDocLbl; }
193
194   /// Rebuild data tree
195   void rebuildDataTree();
196
197   /// Resets the object browser into initial state
198   void clearContent();
199
200   /// Initialize the Object browser
201   void initialize(ModuleBase_ITreeNode* theRoot);
202
203   /// Returns list of folders opened state for the given document
204   /// \param theDoc the document
205   /// \return list of booleans with state expanded or not
206   std::list<bool> getStateForDoc(DocumentPtr theDoc) const;
207
208   /// Set folders opened state for the given document
209   /// \param theDoc the document
210   /// \param theStates list of booleans with state expanded or not
211   void setStateForDoc(DocumentPtr theDoc, const std::list<bool>& theStates);
212
213   /// Returns current workshop
214   XGUI_Workshop* workshop() const { return myWorkshop; }
215
216   void onSelectionChanged();
217
218   /// Updates all items of object browser
219   /// \param theColumn - column of items
220   /// \param theParent - a parent item (by default from root)
221   void updateAllIndexes(int theColumn = 0, const QModelIndex& theParent = QModelIndex());
222
223   QMap<ObjectPtr, bool> getFoldersState(DocumentPtr theDoc) const;
224
225   void setFoldersState(const QMap<ObjectPtr, bool>& theStates);
226
227 public slots:
228   //! Called on Edit command request
229   void onEditItem();
230
231 signals:
232   //! Emited when selection is changed
233   void selectionChanged();
234
235   //! Emited on context menu request
236   void contextMenuRequested(QContextMenuEvent* theEvent);
237
238   //! Segnal is emitted when user cliks by mouse in header label of object browser
239   void headerMouseDblClicked(const QModelIndex&);
240
241  private slots:
242   /// Show context menu
243   /// \param theEvent a context menu event
244   void onContextMenuRequested(QContextMenuEvent* theEvent);
245
246   /// Show context menu on upper label
247   /// \param thePnt a position of context menu
248   void onLabelContextMenuRequested(const QPoint& thePnt);
249
250   //! Called when selection in Data Tree is changed
251   void onSelectionChanged(const QItemSelection& theSelected, const QItemSelection& theDeselected);
252
253   void onBeforeReset();
254
255   void onAfterModelReset();
256
257  private:
258   QModelIndexList expandedItems(const QModelIndex& theParent = QModelIndex()) const;
259
260   //! Internal model
261   XGUI_DataModel* myDocModel;
262   XGUI_ActiveDocLbl* myActiveDocLbl;
263   XGUI_DataTree* myTreeView;
264   XGUI_Workshop* myWorkshop;
265
266   /// A field to store expanded items before model reset
267   QModelIndexList myExpandedItems;
268 };
269
270 #endif