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