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