]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxDockWindow.cxx
Salome HOME
Initial version
[modules/gui.git] / src / Qtx / QtxDockWindow.cxx
1 // File:      QtxDockWindow.cxx
2 // Author:    Sergey TELKOV
3
4 #include "QtxDockWindow.h"
5
6 #include <qlayout.h>
7 #include <qpixmap.h>
8 #include <qdockarea.h>
9 #include <qmainwindow.h>
10 #include <qapplication.h>
11
12 /*!
13     Class: QtxDockWindow::Watcher [Internal]
14     Descr: Internal object with event filter.
15 */
16
17 class QtxDockWindow::Watcher : public QObject
18 {
19 public:
20   Watcher( QtxDockWindow* );
21
22   virtual bool   eventFilter( QObject*, QEvent* );
23
24 protected:
25   virtual void   customEvent( QCustomEvent* );
26
27 private:
28   void           installFilters();
29
30   void           updateIcon();
31   void           updateCaption();
32   void           updateVisibility();
33
34 private:
35   bool           myVis;
36   QtxDockWindow* myCont;
37 };
38
39 QtxDockWindow::Watcher::Watcher( QtxDockWindow* cont )
40 : QObject( cont ), myCont( cont ),
41 myVis( true )
42 {
43   if ( myCont->mainWindow() )
44     myVis = myCont->mainWindow()->appropriate( myCont );
45
46   myCont->installEventFilter( this );
47
48   installFilters();
49 }
50
51 bool QtxDockWindow::Watcher::eventFilter( QObject* o, QEvent* e )
52 {
53   if ( o == myCont &&
54        ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
55          e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ||
56          e->type() == QEvent::ChildInserted ) )
57     QApplication::postEvent( this, new QCustomEvent( QEvent::User ) );
58
59   if ( o != myCont && e->type() == QEvent::IconChange )
60     updateIcon();
61
62   if ( o != myCont && e->type() == QEvent::CaptionChange )
63     updateCaption();
64
65   if ( o != myCont &&
66        ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
67          e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ||
68          e->type() == QEvent::ChildRemoved ) )
69     updateVisibility();
70
71   return false;
72 }
73
74 void QtxDockWindow::Watcher::customEvent( QCustomEvent* e )
75 {
76   installFilters();
77
78   updateIcon();
79   updateCaption();
80   updateVisibility();
81 }
82
83 void QtxDockWindow::Watcher::installFilters()
84 {
85   if ( !myCont )
86     return;
87
88   QBoxLayout* bl = myCont->boxLayout();
89   if ( !bl )
90     return;
91
92   for ( QLayoutIterator it = bl->iterator(); it.current(); ++it )
93   {
94     if ( it.current()->widget() )
95       it.current()->widget()->installEventFilter( this );
96   }
97 }
98
99 void QtxDockWindow::Watcher::updateVisibility()
100 {
101   if ( !myCont )
102     return;
103
104   QBoxLayout* bl = myCont->boxLayout();
105   if ( !bl )
106     return;
107
108   bool vis = false;
109   for ( QLayoutIterator it = bl->iterator(); it.current() && !vis; ++it )
110     vis = it.current()->widget() && it.current()->widget()->isVisibleTo( myCont );
111
112   QMainWindow* mw = myCont->mainWindow();
113   if ( mw )
114   {
115     if ( vis )
116       mw->setAppropriate( myCont, myVis );
117     else
118     {
119       myVis = mw->appropriate( myCont );
120       mw->setAppropriate( myCont, false );
121     }
122   }
123
124   if ( vis )
125     myCont->show();
126   else
127     myCont->hide();
128 }
129
130 void QtxDockWindow::Watcher::updateIcon()
131 {
132   if ( !myCont || !myCont->widget() )
133     return;
134   
135   const QPixmap* ico = myCont->widget()->icon();
136   myCont->setIcon( ico ? *ico : QPixmap() );
137 }
138
139 void QtxDockWindow::Watcher::updateCaption()
140 {
141   if ( myCont && myCont->widget() && !myCont->widget()->caption().isNull() )
142     myCont->setCaption( myCont->widget()->caption() );
143 }
144
145 /*!
146     Class: QtxDockWindow [Public]
147     Descr: 
148 */
149
150 QtxDockWindow::QtxDockWindow( Place p, QWidget* parent, const char* name, WFlags f )
151 : QDockWindow( p, parent, name, f ),
152 myWatcher( 0 ),
153 myStretch( false )
154 {
155 }
156
157 QtxDockWindow::QtxDockWindow( const bool watch, QWidget* parent, const char* name, WFlags f )
158 : QDockWindow( InDock, parent, name, f ),
159 myWatcher( 0 ),
160 myStretch( false )
161 {
162   if ( watch )
163     myWatcher = new Watcher( this );
164 }
165
166 QtxDockWindow::QtxDockWindow( QWidget* parent, const char* name, WFlags f )
167 : QDockWindow( InDock, parent, name, f ),
168 myWatcher( 0 ),
169 myStretch( false )
170 {
171 }
172
173 QtxDockWindow::~QtxDockWindow()
174 {
175 }
176
177 void QtxDockWindow::setWidget( QWidget* wid )
178 {
179   if ( wid )
180     wid->reparent( this, QPoint( 0, 0 ), wid->isVisibleTo( wid->parentWidget() ) );
181
182   QDockWindow::setWidget( wid );
183 }
184
185 bool QtxDockWindow::isStretchable() const
186 {
187   return myStretch;
188 }
189
190 void QtxDockWindow::setStretchable( const bool on )
191 {
192   if ( myStretch == on )
193     return;
194
195   myStretch = on;
196
197   boxLayout()->setStretchFactor( widget(), myStretch ? 1 : 0 );
198
199   if ( myStretch != isHorizontalStretchable() ||
200        myStretch != isVerticalStretchable() )
201   {
202           if ( orientation() == Horizontal )
203             setHorizontalStretchable( myStretch );
204           else
205             setVerticalStretchable( myStretch );
206   }
207 }
208
209 QSize QtxDockWindow::sizeHint() const
210 {
211   QSize sz = QDockWindow::sizeHint();
212
213   if ( place() == InDock && isStretchable() && area() )
214   {
215     if ( orientation() == Horizontal )
216       sz.setWidth( area()->width() );
217     else
218       sz.setHeight( area()->height() );
219   }
220
221   return sz;
222 }
223
224 QSize QtxDockWindow::minimumSizeHint() const
225 {
226   QSize sz = QDockWindow::minimumSizeHint();
227
228   if ( orientation() == Horizontal )
229           sz = QSize( 0, QDockWindow::minimumSizeHint().height() );
230   else
231     sz = QSize( QDockWindow::minimumSizeHint().width(), 0 );
232
233   if ( place() == InDock && isStretchable() && area() )
234   {
235     if ( orientation() == Horizontal )
236       sz.setWidth( area()->width() );
237     else
238       sz.setHeight( area()->height() );
239   }
240
241   return sz;
242 }
243
244 QMainWindow* QtxDockWindow::mainWindow() const
245 {
246   QMainWindow* mw = 0;
247
248   QWidget* wid = parentWidget();
249   while ( !mw && wid )
250   {
251     if ( wid->inherits( "QMainWindow" ) )
252       mw = (QMainWindow*)wid;
253     wid = wid->parentWidget();
254   }
255
256   return mw;
257 }