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