Salome HOME
0023105: [CEA 1535] Be able to active stereo in VTK view and to choose which kind...
[modules/gui.git] / src / Qtx / QtxWorkstack.h
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:      QtxWorkstack.h
24 // Author:    Sergey TELKOV
25 //
26 #ifndef QTXWORKSTACK_H
27 #define QTXWORKSTACK_H
28
29 #include "Qtx.h"
30
31 #include <QMap>
32 #include <QFrame>
33 #include <QEvent>
34 #include <QWidget>
35 #include <QTabBar>
36 #include <QPointer>
37 #include <QSplitter>
38 #include <QByteArray>
39
40 class QAction;
41 class QDataStream;
42 class QRubberBand;
43 class QStackedWidget;
44 class QAbstractButton;
45
46 class QtxWorkstackArea;
47 class QtxWorkstackDrag;
48 class QtxWorkstackChild;
49 class QtxWorkstackTabBar;
50 class QtxWorkstackSplitter;
51
52 #ifdef WIN32
53 #pragma warning( disable:4251 )
54 #endif
55
56 class QTX_EXPORT QtxWorkstack : public QWidget
57 {
58   Q_OBJECT
59
60 public:
61   //! Workstack actions (context menu items)
62   enum { SplitVertical    = 0x01,  //!< "Split vertically" menu item
63          SplitHorizontal  = 0x02,  //!< "Split horizontally" menu item
64          Close            = 0x04,  //!< "Close" menu item
65          Rename           = 0x08,  //!< "Rename" menu item
66          All = SplitVertical | SplitHorizontal |
67                Close | Rename      //!< all menu items
68   };
69
70   enum { VersionMarker = 0x01,
71                SplitMarker   = 0x02,
72                AreaMarker    = 0x04,
73                WidgetMarker  = 0x08
74   };
75
76   enum { Horizontal = 0x01,
77                Visible    = 0x02
78   };
79
80   //! Workstack splitting type
81   enum SplitType
82   {
83     SplitStay,  //!< selected widget stays in current workarea, others widgets are moved into a new workarea
84     SplitAt,    //!< all widgets before selected widget stay in current workarea, other widgess are moved into a new workarea
85     SplitMove   //!< selected widget is moved into a new workarea, all other widgets stay in an old workarea
86   };
87
88 public:
89   QtxWorkstack( QWidget* = 0 );
90   virtual ~QtxWorkstack();
91
92   QWidgetList         windowList( QWidget* = 0 ) const;
93   QWidgetList         splitWindowList() const;
94
95   QWidget*            activeWindow() const;
96   void                setActiveWindow( QWidget* );
97
98   int                 accel( const int ) const;
99   void                setAccel( const int, const int );
100
101   QIcon               icon( const int ) const;
102   void                setIcon( const int, const QIcon& );
103
104   void                setMenuActions( const int );
105   int                 menuActions() const;
106
107   void                stack();
108   void                split( const int );
109   bool                move( QWidget* wid, QWidget* wid_to, const bool before );
110
111   QWidget*            addWindow( QWidget*, Qt::WindowFlags = 0 );
112
113   QByteArray          saveState( int ) const;
114   bool                restoreState( const QByteArray&, int );
115   
116   void                setOpaqueResize( bool = true );
117   bool                opaqueResize() const;
118
119   void                showActiveTabBar( bool = true );
120
121   void Split( QWidget* wid, const Qt::Orientation o, const SplitType type );
122   void Attract( QWidget* wid1, QWidget* wid2, const bool all );
123   void SetRelativePosition( QWidget* wid, const Qt::Orientation o, const double pos );
124   void SetRelativePositionInSplitter( QWidget* wid, const double pos );
125
126 signals:
127   void                windowActivated( QWidget* );
128
129 public slots:
130   void                splitVertical();
131   void                splitHorizontal();
132
133 private slots:
134   void                onRename();
135   void                onCloseWindow();
136   void                onDestroyed( QObject* );
137   void                onWindowActivated( QWidget* );
138   void                onContextMenuRequested( QWidget*, QPoint );
139   void                onDeactivated( QtxWorkstackArea* );
140
141 protected:
142   virtual void        customEvent( QEvent* );
143
144   QAction*            action( const int ) const;
145
146   void                saveState( QDataStream& ) const;
147   bool                restoreState( QDataStream& );
148
149 private:
150   QSplitter*          splitter( QtxWorkstackArea* ) const;
151   void                splitters( QSplitter*, QList<QSplitter*>&, const bool = false ) const;
152   void                areas( QSplitter*, QList<QtxWorkstackArea*>&, const bool = false ) const;
153
154   QSplitter*          wrapSplitter( QtxWorkstackArea* );
155   void                insertWidget( QWidget*, QWidget*, QWidget* );
156
157   QtxWorkstackArea*   areaAt( const QPoint& ) const;
158   QtxWorkstackArea*   wgArea( QWidget* ) const;
159
160   QtxWorkstackArea*   targetArea();
161   QtxWorkstackArea*   activeArea() const;
162   QtxWorkstackArea*   currentArea() const;
163
164   void                setActiveArea( QtxWorkstackArea* );
165   QtxWorkstackArea*   neighbourArea( QtxWorkstackArea* ) const;
166
167   QtxWorkstackArea*   createArea( QWidget* ) const;
168
169   void                updateState();
170   void                updateState( QSplitter* );
171
172   void                distributeSpace( QSplitter* ) const;
173
174   int                 setPosition( QWidget* wid, QSplitter* split, const Qt::Orientation o,
175                                                            const int need_pos, const int splitter_pos );
176
177 private:
178   QPointer<QWidget>          myWin;        //!< active widget
179   QPointer<QtxWorkstackArea> myArea;       //!< active workarea
180   QtxWorkstackSplitter*      mySplit;      //!< tol-level splitter
181   QPointer<QWidget>          myWorkWin;    //!< widget where popup menu is invoked (used internally)
182   QPointer<QtxWorkstackArea> myWorkArea;   //!< workarea where popup menu is invoked (used internally)
183
184   QMap<int, QAction*> myActionsMap; //!< actions map
185
186   friend class QtxWorkstackArea;
187   friend class QtxWorkstackDrag;
188   friend class QtxWorkstackAction;
189   friend class QtxWorkstackSplitter;
190 };
191
192 class QtxWorkstackSplitter : public QSplitter
193 {
194   Q_OBJECT
195
196 public:
197   QtxWorkstackSplitter( QWidget* = 0 );
198   virtual ~QtxWorkstackSplitter();
199
200   QtxWorkstack*       workstack() const;
201
202   void                saveState( QDataStream& ) const;
203   bool                restoreState( QDataStream&, QMap<QString, QtxWorkstackChild*>& );
204 };
205
206 class QTX_EXPORT QtxWorkstackArea : public QFrame
207 {
208   Q_OBJECT
209
210   class WidgetEvent;
211   class RestoreEvent;
212
213 public:
214   QtxWorkstackArea( QWidget* );
215   virtual ~QtxWorkstackArea();
216
217   bool                isNull() const;
218   bool                isEmpty() const;
219
220   QtxWorkstackChild*  insertWidget( QWidget*, const int = -1, Qt::WindowFlags = 0 );
221   void                removeWidget( QWidget*, const bool = true );
222
223   void                insertChild( QtxWorkstackChild*, const int = -1 );
224   void                removeChild( QtxWorkstackChild*, const bool = true );
225
226   QWidget*            activeWidget() const;
227   void                setActiveWidget( QWidget* );
228
229   bool                contains( QWidget* ) const;
230
231   QWidgetList         widgetList() const;
232   QList<QtxWorkstackChild*> childList() const;
233
234   bool                isActive() const;
235   void                updateActiveState();
236
237   QtxWorkstack*       workstack() const;
238
239   virtual bool        eventFilter( QObject*, QEvent* );
240
241   QRect               floatRect() const;
242   QRect               floatTab( const int ) const;
243
244   int                 tabAt( const QPoint& ) const;
245
246   void                saveState( QDataStream& ) const;
247   bool                restoreState( QDataStream&, QMap<QString, QtxWorkstackChild*>& );
248
249   void                showTabBar( bool = true);
250
251 signals:
252   void                activated( QWidget* );
253   void                contextMenuRequested( QWidget*, QPoint );
254   void                deactivated( QtxWorkstackArea* );
255
256 private slots:
257   void                onClose();
258   void                onCurrentChanged( int );
259
260   void                onChildDestroyed( QObject* );
261   void                onChildShown( QtxWorkstackChild* );
262   void                onChildHidden( QtxWorkstackChild* );
263   void                onChildActivated( QtxWorkstackChild* );
264   void                onChildCaptionChanged( QtxWorkstackChild* );
265
266   void                onDragActiveTab();
267   void                onContextMenuRequested( QPoint );
268
269 protected:
270   virtual void        customEvent( QEvent* );
271   virtual void        focusInEvent( QFocusEvent* );
272   virtual void        mousePressEvent( QMouseEvent* );
273
274 private:
275   //! Custom events
276   enum { ActivateWidget = QEvent::User,   //!< activate widget event
277                FocusWidget,                     //!< focus receiving widget event
278          MakeCurrent,
279          RestoreWidget
280   };
281
282 private:
283   void                updateState();
284   void                updateCurrent();
285   void                updateTab( QWidget* );
286
287   QWidget*            widget( const int ) const;
288   int                 widgetId( QWidget* ) const;
289
290   QtxWorkstackChild*  child( QWidget* ) const;
291   QtxWorkstackChild*  child( const int ) const;
292
293   void                setWidgetActive( QWidget* );
294
295   int                 generateId() const;
296
297 private:
298   typedef QList<QtxWorkstackChild*> ChildList;
299
300 private:
301   QWidget*            myTop;     //!< workarea top widget
302   QtxWorkstackTabBar* myBar;     //!< workarea tab bar header
303   ChildList           myList;    //!< child widgets list
304   QAbstractButton*    myClose;   //!< close button
305   QStackedWidget*     myStack;   //!< widget stack
306 };
307
308 class QtxWorkstackChild : public QWidget
309 {
310   Q_OBJECT
311
312 public:
313   QtxWorkstackChild( QWidget*, QWidget* = 0, Qt::WindowFlags = 0 );
314   virtual ~QtxWorkstackChild();
315
316   QWidget*            widget() const
317 ;
318
319   int                 id() const;
320   void                setId( const int );
321
322   bool                visibility();
323
324   QtxWorkstackArea*   area() const;
325
326   virtual bool        eventFilter( QObject*, QEvent* );
327
328 signals:
329   void                shown( QtxWorkstackChild* );
330   void                hidden( QtxWorkstackChild* );
331   void                activated( QtxWorkstackChild* );
332   void                captionChanged( QtxWorkstackChild* );
333
334 private slots:
335   void                onDestroyed( QObject* );
336
337 protected:
338   virtual void        childEvent( QChildEvent* );
339
340 private:
341   int                 myId;       //!< id
342   QPointer<QWidget>   myWidget;   //!< child widget
343 };
344
345 class QtxWorkstackTabBar : public QTabBar
346 {
347   Q_OBJECT
348
349 public:
350   QtxWorkstackTabBar( QWidget* = 0 );
351   virtual ~QtxWorkstackTabBar();
352
353   bool                isActive() const;
354   void                setActive( const bool );
355
356   int                 tabId( const int ) const;
357   int                 indexOf( const int ) const;
358   void                setTabId( const int, const int );
359
360   void                updateActiveState();
361
362 signals:
363   void                dragActiveTab();
364   void                contextMenuRequested( QPoint );
365
366 private slots:
367   void                onCurrentChanged( int );
368
369 protected:
370   virtual void        changeEvent( QEvent* );
371   virtual void        mouseMoveEvent( QMouseEvent* );
372   virtual void        mousePressEvent( QMouseEvent* );
373   virtual void        mouseReleaseEvent( QMouseEvent* );
374   virtual void        contextMenuEvent( QContextMenuEvent* );
375
376 private:
377   int                 myId;         //!< current tab page index
378   bool                myActive;     //!< "active" status
379 };
380
381 class QtxWorkstackDrag : public QObject
382 {
383   Q_OBJECT
384
385 public:
386   QtxWorkstackDrag( QtxWorkstack*, QtxWorkstackChild* );
387   virtual ~QtxWorkstackDrag();
388
389   virtual bool        eventFilter( QObject*, QEvent* );
390
391 private:
392   void                dropWidget();
393
394   void                updateTarget( const QPoint& );
395   QtxWorkstackArea*   detectTarget( const QPoint&, int& ) const;
396   void                setTarget( QtxWorkstackArea*, const int );
397
398   void                drawRect();
399   void                endDrawRect();
400   void                startDrawRect();
401
402 private:
403   QtxWorkstack*       myWS;          //!< parent workstack
404   QtxWorkstackChild*  myChild;       //!< workstack child widget container
405
406   int                 myTab;         //!< workarea tab page index
407   QtxWorkstackArea*   myArea;        //!< workarea
408   QRubberBand*        myTabRect;     //!< tab bar rubber band
409   QRubberBand*        myAreaRect;    //!< workarea rubber band
410 };
411
412 #ifdef WIN32
413 #pragma warning( default:4251 )
414 #endif
415
416 #endif   // QTXWORKSTACK_H