Salome HOME
Base class for resource editor and one completed implementation.
[modules/gui.git] / src / Qtx / QtxToolBar.cxx
1 // File:      QtxToolBar.cxx
2 // Author:    Sergey TELKOV
3
4 #include "QtxToolBar.h"
5
6 #include <qlayout.h>
7 #include <qpixmap.h>
8 #include <qdockarea.h>
9 #include <qobjectlist.h>
10 #include <qmainwindow.h>
11 #include <qapplication.h>
12
13 /*!
14     Class: QtxToolBar::Watcher [Internal]
15     Descr: Internal object with event filter.
16 */
17
18 class QtxToolBar::Watcher : public QObject
19 {
20 public:
21   Watcher( QtxToolBar* );
22
23   void         shown( QtxToolBar* );
24   void         hided( QtxToolBar* );
25
26   virtual bool eventFilter( QObject*, QEvent* );
27
28 protected:
29   virtual void customEvent( QCustomEvent* );
30
31 private:
32   enum { Install = QEvent::User, Update };
33
34 private:
35   void         installFilters();
36
37   void         showContainer();
38   void         hideContainer();
39
40   void         updateIcon();
41   void         updateCaption();
42   void         updateVisibility();
43
44 private:
45   QtxToolBar*  myCont;
46   bool         myState;
47   bool         myEmpty;
48   bool         myVisible;
49 };
50
51 QtxToolBar::Watcher::Watcher( QtxToolBar* cont )
52 : QObject( cont ),
53 myCont( cont ),
54 myState( true ),
55 myEmpty( true )
56 {
57   if ( myCont->mainWindow() )
58     myState = myCont->mainWindow()->appropriate( myCont );
59
60   myCont->installEventFilter( this );
61   myVisible = myCont->isVisibleTo( myCont->parentWidget() );
62
63   installFilters();
64 }
65
66 bool QtxToolBar::Watcher::eventFilter( QObject* o, QEvent* e )
67 {
68   if ( o == myCont && e->type() == QEvent::ChildInserted )
69     QApplication::postEvent( this, new QCustomEvent( Install ) );
70
71   if ( o != myCont && e->type() == QEvent::IconChange )
72     updateIcon();
73
74   if ( o != myCont && e->type() == QEvent::CaptionChange )
75     updateCaption();
76
77   bool updVis = ( o != myCont && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
78                                    e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) ) ||
79                 ( o == myCont && ( e->type() == QEvent::ChildRemoved || e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ) );
80
81   if ( updVis )
82   {
83     QtxToolBar* cont = myCont;
84     myCont = 0;
85     QApplication::sendPostedEvents( this, Update );
86     myCont = cont;
87     QApplication::postEvent( this, new QCustomEvent( Update ) );
88   }
89
90   return false;
91 }
92
93 void QtxToolBar::Watcher::shown( QtxToolBar* tb )
94 {
95   if ( tb != myCont )
96     return;
97
98   myVisible = true;
99 }
100
101 void QtxToolBar::Watcher::hided( QtxToolBar* tb )
102 {
103   if ( tb != myCont )
104     return;
105
106   myVisible = false;
107 }
108
109 void QtxToolBar::Watcher::showContainer()
110 {
111   if ( !myCont )
112     return;
113
114   QtxToolBar* cont = myCont;
115   myCont = 0;
116   cont->show();
117   myCont = cont;
118 }
119
120 void QtxToolBar::Watcher::hideContainer()
121 {
122   if ( !myCont )
123     return;
124
125   QtxToolBar* cont = myCont;
126   myCont = 0;
127   cont->hide();
128   myCont = cont;
129 }
130
131 void QtxToolBar::Watcher::customEvent( QCustomEvent* e )
132 {
133   switch ( e->type() )
134   {
135   case Install:
136     installFilters();
137     updateIcon();
138     updateCaption();
139   case Update:
140     updateVisibility();
141   }
142 }
143
144 void QtxToolBar::Watcher::installFilters()
145 {
146   if ( !myCont )
147     return;
148
149   const QObjectList* objList = myCont->children();
150   if ( !objList )
151     return;
152
153   for ( QObjectListIt it( *objList ); it.current(); ++it )
154   {
155     if ( it.current()->isWidgetType() &&
156          qstrcmp( "qt_dockwidget_internal", it.current()->name() ) )
157       it.current()->installEventFilter( this );
158   }
159 }
160
161 void QtxToolBar::Watcher::updateVisibility()
162 {
163   if ( !myCont )
164     return;
165
166   bool vis = false;
167
168   const QObjectList* objList = myCont->children();
169   if ( objList )
170   {
171     for ( QObjectListIt it( *objList ); it.current() && !vis; ++it )
172     {
173       if ( !it.current()->isWidgetType() ||
174            !qstrcmp( "qt_dockwidget_internal", it.current()->name() ) )
175         continue;
176
177       QWidget* wid = (QWidget*)it.current();
178       vis = wid->isVisibleTo( wid->parentWidget() );
179     }
180   }
181
182   QMainWindow* mw = myCont->mainWindow();
183   if ( mw && myEmpty == vis )
184   {
185     myEmpty = !vis;
186     if ( !myEmpty )
187       mw->setAppropriate( myCont, myState );
188     else
189     {
190       myState = mw->appropriate( myCont );
191       mw->setAppropriate( myCont, false );
192     }
193   }
194
195   vis = !myEmpty && myVisible;
196   if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
197     vis ? showContainer() : hideContainer();
198 }
199
200 void QtxToolBar::Watcher::updateIcon()
201 {
202   if ( !myCont || !myCont->widget() )
203     return;
204   
205   const QPixmap* ico = myCont->widget()->icon();
206   myCont->setIcon( ico ? *ico : QPixmap() );
207 }
208
209 void QtxToolBar::Watcher::updateCaption()
210 {
211   if ( myCont && myCont->widget() && !myCont->widget()->caption().isNull() )
212     myCont->setCaption( myCont->widget()->caption() );
213 }
214
215 /*!
216     Class: QtxToolBar [Public]
217     Descr: 
218 */
219
220 QtxToolBar::QtxToolBar( const bool watch, const QString& label, QMainWindow* main,
221                         QWidget* parent, bool newLine, const char* name, WFlags f )
222 : QToolBar( label, main, parent, newLine, name, f ),
223 myWatcher( 0 ),
224 myStretch( false )
225 {
226   if ( watch )
227     myWatcher = new Watcher( this );
228 }
229
230 QtxToolBar::QtxToolBar( const QString& label, QMainWindow* main,
231                         QWidget* parent, bool newLine, const char* name, WFlags f )
232 : QToolBar( label, main, parent, newLine, name, f ),
233 myWatcher( 0 ),
234 myStretch( false )
235 {
236 }
237
238 QtxToolBar::QtxToolBar( const bool watch, QMainWindow* main, const char* name )
239 : QToolBar( main, name ),
240 myWatcher( 0 ),
241 myStretch( false )
242 {
243   if ( watch )
244     myWatcher = new Watcher( this );
245 }
246
247 QtxToolBar::QtxToolBar( QMainWindow* main, const char* name )
248 : QToolBar( main, name ),
249 myWatcher( 0 ),
250 myStretch( false )
251 {
252 }
253
254 QtxToolBar::~QtxToolBar()
255 {
256 }
257
258 void QtxToolBar::setWidget( QWidget* wid )
259 {
260   if ( wid )
261     wid->reparent( this, QPoint( 0, 0 ), wid->isVisibleTo( wid->parentWidget() ) );
262
263   QToolBar::setWidget( wid );
264
265   if ( !boxLayout() )
266     return;
267
268   for ( QLayoutIterator it = boxLayout()->iterator(); it.current(); ++it )
269   {
270     if ( it.current()->widget() == wid )
271     {
272       it.deleteCurrent();
273       break;
274     }
275   }
276 }
277
278 bool QtxToolBar::isStretchable() const
279 {
280   return myStretch;
281 }
282
283 void QtxToolBar::setStretchable( const bool on )
284 {
285   if ( myStretch == on )
286     return;
287
288   myStretch = on;
289
290   boxLayout()->setStretchFactor( widget(), myStretch ? 1 : 0 );
291
292   if ( myStretch != isHorizontalStretchable() ||
293        myStretch != isVerticalStretchable() )
294   {
295           if ( orientation() == Horizontal )
296             setHorizontalStretchable( myStretch );
297           else
298             setVerticalStretchable( myStretch );
299   }
300 }
301
302 QSize QtxToolBar::sizeHint() const
303 {
304   QSize sz = QToolBar::sizeHint();
305
306   if ( place() == InDock && isStretchable() && area() )
307   {
308     if ( orientation() == Horizontal )
309       sz.setWidth( area()->width() );
310     else
311       sz.setHeight( area()->height() );
312   }
313
314   return sz;
315 }
316
317 QSize QtxToolBar::minimumSizeHint() const
318 {
319   QSize sz = QToolBar::minimumSizeHint();
320
321   if ( place() == InDock && isStretchable() && area() )
322   {
323     if ( orientation() == Horizontal )
324       sz.setWidth( area()->width() );
325     else
326       sz.setHeight( area()->height() );
327   }
328
329   return sz;
330 }
331
332 void QtxToolBar::show()
333 {
334   if ( myWatcher )
335     myWatcher->shown( this );
336
337   QToolBar::show();
338 }
339
340 void QtxToolBar::hide()
341 {
342   if ( myWatcher )
343     myWatcher->hided( this );
344
345   QToolBar::hide();
346 }