Salome HOME
It is recommended to use standard Qt QInputDialog instead QtxNameDlg
[modules/gui.git] / src / Qtx / QtxWorkstack.h
1 // File:      QtxWorkstack.h
2 // Author:    Sergey TELKOV
3
4 #ifndef QTXWORKSTACK_H
5 #define QTXWORKSTACK_H
6
7 #include "Qtx.h"
8
9 #include <qhbox.h>
10 #include <qwidget.h>
11 #include <qtabbar.h>
12 #include <qwidgetlist.h>
13
14 class QAction;
15 class QTabBar;
16 class QPainter;
17 class QSplitter;
18 class QPushButton;
19 class QWidgetStack;
20
21 class QtxWorkstackArea;
22 class QtxWorkstackDrag;
23 class QtxWorkstackChild;
24 class QtxWorkstackTabBar;
25
26 #ifdef WIN32
27 #pragma warning( disable:4251 )
28 #endif
29
30 class QTX_EXPORT QtxWorkstack : public QWidget
31 {
32   Q_OBJECT
33
34 public:
35   enum { SplitVertical, SplitHorizontal, Close, Rename };
36     
37   enum SplitType
38   {
39     SPLIT_STAY, //!< given widget stays in its workarea, others are moved into a new one
40     SPLIT_AT,   //!< widgets before a given widget stays in they workarea, others are moved into a new one
41     SPLIT_MOVE  //!< given widget is moved into a new workarea, others stay in an old one
42   };
43
44 public:
45   QtxWorkstack( QWidget* = 0 );
46   virtual ~QtxWorkstack();
47
48   QWidgetList         windowList() const;
49   QWidgetList         splitWindowList() const;
50
51   QWidget*            activeWindow() const;
52
53   int                 accel( const int ) const;
54   void                setAccel( const int, const int );
55
56   void                split( const int );
57
58   // STV: Useless function. wid->setFocus() should be used instead.
59   // void OnTop( QWidget* wid);
60
61   void Split( QWidget* wid, const Qt::Orientation o, const SplitType type );
62   void Attract( QWidget* wid1, QWidget* wid2, const bool all );
63   void SetRelativePosition( QWidget* wid, const Qt::Orientation o, const double pos );
64   void SetRelativePositionInSplitter( QWidget* wid, const double pos );
65
66 signals:
67   void                windowActivated( QWidget* );
68
69 public slots:
70   void                splitVertical();
71   void                splitHorizontal();
72   
73 private slots:
74   void                onRename();
75   void                onCloseWindow();
76   void                onDestroyed( QObject* );
77   void                onWindowActivated( QWidget* );
78   void                onContextMenuRequested( QWidget*, QPoint );
79   void                onDeactivated( QtxWorkstackArea* );
80
81 protected:
82   virtual void        childEvent( QChildEvent* );
83   virtual void        customEvent( QCustomEvent* );
84
85 private:
86   QSplitter*          splitter( QtxWorkstackArea* ) const;
87   void                splitters( QSplitter*, QPtrList<QSplitter>&, const bool = false ) const;
88   void                areas( QSplitter*, QPtrList<QtxWorkstackArea>&, const bool = false ) const;
89
90   QSplitter*          wrapSplitter( QtxWorkstackArea* );
91   void                insertWidget( QWidget*, QWidget*, QWidget* );
92
93   QtxWorkstackArea*   areaAt( const QPoint& ) const;
94
95   QtxWorkstackArea*   targetArea();
96   QtxWorkstackArea*   activeArea() const;
97   QtxWorkstackArea*   currentArea() const;
98
99   void                setActiveArea( QtxWorkstackArea* );
100   QtxWorkstackArea*   neighbourArea( QtxWorkstackArea* ) const;
101
102   QtxWorkstackArea*   createArea( QWidget* ) const;
103
104   void                updateState();
105   void                updateState( QSplitter* );
106
107   void                distributeSpace( QSplitter* ) const;
108   int                 setPosition( QWidget* wid, QSplitter* split, const Qt::Orientation o,
109                                                            const int need_pos, const int splitter_pos );
110
111 private:
112   QWidget*            myWin;
113   QtxWorkstackArea*   myArea;
114   QSplitter*          mySplit;
115   QWidget*            myWorkWin;
116
117   QMap<int, QAction*> myActionsMap; //!< The map of the actions. Allows to get the QAction object by the key.
118
119   friend class QtxWorkstackArea;
120   friend class QtxWorkstackDrag;
121 };
122
123 class QtxWorkstackArea : public QWidget
124 {
125   Q_OBJECT
126
127 public:
128   QtxWorkstackArea( QWidget* );
129   virtual ~QtxWorkstackArea();
130
131   bool                isEmpty() const;
132
133   void                insertWidget( QWidget*, const int = -1 );
134   void                removeWidget( QWidget*, const bool = true );
135
136   QWidget*            activeWidget() const;
137   void                setActiveWidget( QWidget* );
138
139   bool                contains( QWidget* ) const;
140
141   QWidgetList         widgetList() const;
142
143   bool                isActive() const;
144   void                updateActiveState();
145
146   QtxWorkstack*       workstack() const;
147
148   virtual bool        eventFilter( QObject*, QEvent* );
149
150   QRect               floatRect() const;
151   QRect               floatTab( const int ) const;
152
153   int                 tabAt( const QPoint& ) const;
154
155 signals:
156   void                activated( QWidget* );
157   void                contextMenuRequested( QWidget*, QPoint );
158   void                deactivated( QtxWorkstackArea* );
159
160 public slots:
161   virtual void        show();
162   virtual void        hide();
163
164 private slots:
165   void                onClose();
166   void                onSelected( int );
167
168   void                onWidgetDestroyed();
169
170   void                onChildDestroyed( QObject* );
171   void                onChildShown( QtxWorkstackChild* );
172   void                onChildHided( QtxWorkstackChild* );
173   void                onChildActivated( QtxWorkstackChild* );
174   void                onChildCaptionChanged( QtxWorkstackChild* );
175
176   void                onDragActiveTab();
177   void                onContextMenuRequested( QPoint );
178
179 protected:
180   virtual void        customEvent( QCustomEvent* );
181   virtual void        focusInEvent( QFocusEvent* );
182   virtual void        mousePressEvent( QMouseEvent* );
183
184 private:
185   enum { ActivateWidget = QEvent::User, FocusWidget, RemoveWidget };
186
187 private:
188   void                updateState();
189   void                updateCurrent();
190   void                updateTab( QWidget* );
191
192   QWidget*            widget( const int ) const;
193   int                 widgetId( QWidget* ) const;
194   bool                widgetVisibility( QWidget* ) const;
195
196   void                setWidgetActive( QWidget* );
197   void                setWidgetShown( QWidget*, const bool );
198
199   int                 generateId() const;
200
201   bool                isBlocked( QWidget* ) const;
202   void                setBlocked( QWidget*, const bool );
203
204   QtxWorkstackChild*  child( QWidget* ) const;
205
206 private:
207   struct WidgetInfo
208   {
209     WidgetInfo() : id( 0 ), vis( false ) {}
210     int id; bool vis;
211   };
212
213   typedef QMap<QWidget*, bool>               BlockMap;
214   typedef QMap<QWidget*, QtxWorkstackChild*> ChildMap;
215   typedef QMap<QWidget*, WidgetInfo>         WidgetInfoMap;
216
217 private:
218   QtxWorkstackTabBar* myBar;
219   QPushButton*        myClose;
220   QWidgetStack*       myStack;
221
222   QWidgetList         myList;
223   WidgetInfoMap       myInfo;
224   ChildMap            myChild;
225   BlockMap            myBlock;
226 };
227
228 class QtxWorkstackChild : public QHBox
229 {
230   Q_OBJECT
231
232 public:
233   QtxWorkstackChild( QWidget*, QWidget* = 0 );
234   virtual ~QtxWorkstackChild();
235
236   QWidget*            widget() const;
237
238   virtual bool        eventFilter( QObject*, QEvent* );
239
240 signals:
241   void                shown( QtxWorkstackChild* );
242   void                hided( QtxWorkstackChild* );
243   void                activated( QtxWorkstackChild* );
244   void                captionChanged( QtxWorkstackChild* );
245
246 private slots:
247   void                onDestroyed( QObject* );
248
249 protected:
250   virtual void        childEvent( QChildEvent* );
251
252 private:
253   QWidget*            myWidget;
254 };
255
256 class QtxWorkstackTabBar : public QTabBar
257 {
258   Q_OBJECT
259
260 public:
261   QtxWorkstackTabBar( QWidget* = 0 );
262   virtual ~QtxWorkstackTabBar();
263
264   QRect               tabRect( const int ) const;
265
266   void                setActive( const bool );
267
268 signals:
269   void                dragActiveTab();
270   void                contextMenuRequested( QPoint );
271
272 protected:
273   virtual void        mouseMoveEvent( QMouseEvent* );
274   virtual void        mousePressEvent( QMouseEvent* );
275   virtual void        mouseReleaseEvent( QMouseEvent* );
276   virtual void        contextMenuEvent( QContextMenuEvent* );
277
278   virtual void        paintLabel( QPainter*, const QRect&, QTab*, bool ) const;
279
280 private:
281   int                 myId;
282 };
283
284 class QtxWorkstackDrag : public QObject
285 {
286   Q_OBJECT
287
288 public:
289   QtxWorkstackDrag( QtxWorkstack*, QtxWorkstackChild* );
290   virtual ~QtxWorkstackDrag();
291
292   virtual bool        eventFilter( QObject*, QEvent* );
293
294 private:
295   void                dropWidget();
296
297   void                updateTarget( const QPoint& );
298   QtxWorkstackArea*   detectTarget( const QPoint&, int& ) const;
299   void                setTarget( QtxWorkstackArea*, const int );
300
301   void                drawRect();
302   void                endDrawRect();
303   void                startDrawRect();
304
305 private:
306   QtxWorkstack*       myWS;
307   QtxWorkstackChild*  myChild;
308
309   int                 myTab;
310   QtxWorkstackArea*   myArea;
311   QPainter*           myPainter;
312   
313 };
314
315 #ifdef WIN32
316 #pragma warning( default:4251 )
317 #endif
318
319 #endif