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