]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxDockWindow.cxx
Salome HOME
no message
[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 != myCont->isVisibleTo( myCont->parentWidget() ) )
125     vis ? myCont->show() : myCont->hide();
126 }
127
128 void QtxDockWindow::Watcher::updateIcon()
129 {
130   if ( !myCont || !myCont->widget() )
131     return;
132   
133   const QPixmap* ico = myCont->widget()->icon();
134   myCont->setIcon( ico ? *ico : QPixmap() );
135 }
136
137 void QtxDockWindow::Watcher::updateCaption()
138 {
139   if ( myCont && myCont->widget() && !myCont->widget()->caption().isNull() )
140     myCont->setCaption( myCont->widget()->caption() );
141 }
142
143 /*!
144     Class: QtxDockWindow [Public]
145     Descr: 
146 */
147
148 QtxDockWindow::QtxDockWindow( Place p, QWidget* parent, const char* name, WFlags f )
149 : QDockWindow( p, parent, name, f ),
150 myWatcher( 0 ),
151 myStretch( false )
152 {
153 }
154
155 QtxDockWindow::QtxDockWindow( const bool watch, QWidget* parent, const char* name, WFlags f )
156 : QDockWindow( InDock, parent, name, f ),
157 myWatcher( 0 ),
158 myStretch( false )
159 {
160   if ( watch )
161     myWatcher = new Watcher( this );
162 }
163
164 QtxDockWindow::QtxDockWindow( QWidget* parent, const char* name, WFlags f )
165 : QDockWindow( InDock, parent, name, f ),
166 myWatcher( 0 ),
167 myStretch( false )
168 {
169 }
170
171 QtxDockWindow::~QtxDockWindow()
172 {
173 }
174
175 void QtxDockWindow::setWidget( QWidget* wid )
176 {
177   if ( wid )
178     wid->reparent( this, QPoint( 0, 0 ), wid->isVisibleTo( wid->parentWidget() ) );
179
180   QDockWindow::setWidget( wid );
181 }
182
183 bool QtxDockWindow::isStretchable() const
184 {
185   return myStretch;
186 }
187
188 void QtxDockWindow::setStretchable( const bool on )
189 {
190   if ( myStretch == on )
191     return;
192
193   myStretch = on;
194
195   boxLayout()->setStretchFactor( widget(), myStretch ? 1 : 0 );
196
197   if ( myStretch != isHorizontalStretchable() ||
198        myStretch != isVerticalStretchable() )
199   {
200           if ( orientation() == Horizontal )
201             setHorizontalStretchable( myStretch );
202           else
203             setVerticalStretchable( myStretch );
204   }
205 }
206
207 QSize QtxDockWindow::sizeHint() const
208 {
209   QSize sz = QDockWindow::sizeHint();
210
211   if ( place() == InDock && isStretchable() && area() )
212   {
213     if ( orientation() == Horizontal )
214       sz.setWidth( area()->width() );
215     else
216       sz.setHeight( area()->height() );
217   }
218
219   return sz;
220 }
221
222 QSize QtxDockWindow::minimumSizeHint() const
223 {
224   QSize sz = QDockWindow::minimumSizeHint();
225
226   if ( orientation() == Horizontal )
227           sz = QSize( 0, QDockWindow::minimumSizeHint().height() );
228   else
229     sz = QSize( QDockWindow::minimumSizeHint().width(), 0 );
230
231   if ( place() == InDock && isStretchable() && area() )
232   {
233     if ( orientation() == Horizontal )
234       sz.setWidth( area()->width() );
235     else
236       sz.setHeight( area()->height() );
237   }
238
239   return sz;
240 }
241
242 QMainWindow* QtxDockWindow::mainWindow() const
243 {
244   QMainWindow* mw = 0;
245
246   QWidget* wid = parentWidget();
247   while ( !mw && wid )
248   {
249     if ( wid->inherits( "QMainWindow" ) )
250       mw = (QMainWindow*)wid;
251     wid = wid->parentWidget();
252   }
253
254   return mw;
255 }