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