Salome HOME
no message
[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   virtual bool eventFilter( QObject*, QEvent* );
24
25 protected:
26   virtual void customEvent( QCustomEvent* );
27
28 private:
29   enum { Install = QEvent::User, Update };
30
31 private:
32   void         installFilters();
33
34   void         updateIcon();
35   void         updateCaption();
36   void         updateVisibility();
37
38 private:
39   bool         myVis;
40   QtxToolBar*  myCont;
41 };
42
43 QtxToolBar::Watcher::Watcher( QtxToolBar* cont )
44 : QObject( cont ),
45 myCont( cont ),
46 myVis( true )
47 {
48   if ( myCont->mainWindow() )
49     myVis = myCont->mainWindow()->appropriate( myCont );
50
51   myCont->installEventFilter( this );
52
53   installFilters();
54 }
55
56 bool QtxToolBar::Watcher::eventFilter( QObject* o, QEvent* e )
57 {
58   if ( o == myCont && e->type() == QEvent::ChildInserted )
59     QApplication::postEvent( this, new QCustomEvent( Install ) );
60
61   if ( o != myCont && e->type() == QEvent::IconChange )
62     updateIcon();
63
64   if ( o != myCont && e->type() == QEvent::CaptionChange )
65     updateCaption();
66
67   bool updVis = ( o != myCont && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
68                                    e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) ) ||
69                 ( o == myCont && e->type() == QEvent::ChildRemoved );
70
71   if ( updVis )
72   {
73     QtxToolBar* cont = myCont;
74     myCont = 0;
75     QApplication::sendPostedEvents( this, Update );
76     myCont = cont;
77     QApplication::postEvent( this, new QCustomEvent( Update ) );
78   }
79
80   return false;
81 }
82
83 void QtxToolBar::Watcher::customEvent( QCustomEvent* e )
84 {
85   switch ( e->type() )
86   {
87   case Install:
88     installFilters();
89     updateIcon();
90     updateCaption();
91   case Update:
92     updateVisibility();
93   }
94 }
95
96 void QtxToolBar::Watcher::installFilters()
97 {
98   if ( !myCont )
99     return;
100
101   const QObjectList* objList = myCont->children();
102   if ( !objList )
103     return;
104
105   for ( QObjectListIt it( *objList ); it.current(); ++it )
106   {
107     if ( it.current()->isWidgetType() &&
108          qstrcmp( "qt_dockwidget_internal", it.current()->name() ) )
109       it.current()->installEventFilter( this );
110   }
111 }
112
113 void QtxToolBar::Watcher::updateVisibility()
114 {
115   if ( !myCont )
116     return;
117
118   bool vis = false;
119
120   const QObjectList* objList = myCont->children();
121   if ( objList )
122   {
123     for ( QObjectListIt it( *objList ); it.current() && !vis; ++it )
124     {
125       if ( !it.current()->isWidgetType() ||
126            !qstrcmp( "qt_dockwidget_internal", it.current()->name() ) )
127         continue;
128
129       QWidget* wid = (QWidget*)it.current();
130       vis = wid->isVisibleTo( wid->parentWidget() );
131     }
132   }
133
134   QMainWindow* mw = myCont->mainWindow();
135   if ( mw )
136   {
137     if ( vis )
138       mw->setAppropriate( myCont, myVis );
139     else
140     {
141       myVis = mw->appropriate( myCont );
142       mw->setAppropriate( myCont, false );
143     }
144   }
145
146   if ( vis != myCont->isVisibleTo( myCont->parentWidget() ) )
147     vis ? myCont->show() : myCont->hide();
148 }
149
150 void QtxToolBar::Watcher::updateIcon()
151 {
152   if ( !myCont || !myCont->widget() )
153     return;
154   
155   const QPixmap* ico = myCont->widget()->icon();
156   myCont->setIcon( ico ? *ico : QPixmap() );
157 }
158
159 void QtxToolBar::Watcher::updateCaption()
160 {
161   if ( myCont && myCont->widget() && !myCont->widget()->caption().isNull() )
162     myCont->setCaption( myCont->widget()->caption() );
163 }
164
165 /*!
166     Class: QtxToolBar [Public]
167     Descr: 
168 */
169
170 QtxToolBar::QtxToolBar( const bool watch, const QString& label, QMainWindow* main,
171                         QWidget* parent, bool newLine, const char* name, WFlags f )
172 : QToolBar( label, main, parent, newLine, name, f ),
173 myWatcher( 0 ),
174 myStretch( false )
175 {
176   if ( watch )
177     myWatcher = new Watcher( this );
178 }
179
180 QtxToolBar::QtxToolBar( const QString& label, QMainWindow* main,
181                         QWidget* parent, bool newLine, const char* name, WFlags f )
182 : QToolBar( label, main, parent, newLine, name, f ),
183 myWatcher( 0 ),
184 myStretch( false )
185 {
186 }
187
188 QtxToolBar::QtxToolBar( const bool watch, QMainWindow* main, const char* name )
189 : QToolBar( main, name ),
190 myWatcher( 0 ),
191 myStretch( false )
192 {
193   if ( watch )
194     myWatcher = new Watcher( this );
195 }
196
197 QtxToolBar::QtxToolBar( QMainWindow* main, const char* name )
198 : QToolBar( main, name ),
199 myWatcher( 0 ),
200 myStretch( false )
201 {
202 }
203
204 QtxToolBar::~QtxToolBar()
205 {
206 }
207
208 void QtxToolBar::setWidget( QWidget* wid )
209 {
210   if ( wid )
211     wid->reparent( this, QPoint( 0, 0 ), wid->isVisibleTo( wid->parentWidget() ) );
212
213   QToolBar::setWidget( wid );
214
215   if ( !boxLayout() )
216     return;
217
218   for ( QLayoutIterator it = boxLayout()->iterator(); it.current(); ++it )
219   {
220     if ( it.current()->widget() == wid )
221     {
222       it.deleteCurrent();
223       break;
224     }
225   }
226 }
227
228 bool QtxToolBar::isStretchable() const
229 {
230   return myStretch;
231 }
232
233 void QtxToolBar::setStretchable( const bool on )
234 {
235   if ( myStretch == on )
236     return;
237
238   myStretch = on;
239
240   boxLayout()->setStretchFactor( widget(), myStretch ? 1 : 0 );
241
242   if ( myStretch != isHorizontalStretchable() ||
243        myStretch != isVerticalStretchable() )
244   {
245           if ( orientation() == Horizontal )
246             setHorizontalStretchable( myStretch );
247           else
248             setVerticalStretchable( myStretch );
249   }
250 }
251
252 QSize QtxToolBar::sizeHint() const
253 {
254   QSize sz = QToolBar::sizeHint();
255
256   if ( place() == InDock && isStretchable() && area() )
257   {
258     if ( orientation() == Horizontal )
259       sz.setWidth( area()->width() );
260     else
261       sz.setHeight( area()->height() );
262   }
263
264   return sz;
265 }
266
267 QSize QtxToolBar::minimumSizeHint() const
268 {
269   QSize sz = QToolBar::minimumSizeHint();
270
271   if ( place() == InDock && isStretchable() && area() )
272   {
273     if ( orientation() == Horizontal )
274       sz.setWidth( area()->width() );
275     else
276       sz.setHeight( area()->height() );
277   }
278
279   return sz;
280 }