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