1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 // File: QtxMainWindow.cxx
20 // Author: Sergey TELKOV
22 #include "QtxMainWindow.h"
24 #include "QtxToolBar.h"
25 #include "QtxResourceMgr.h"
29 #include <qstatusbar.h>
30 #include <qapplication.h>
33 Class: QtxMainWindow::Filter [Internal]
34 Descr: Internal object with event filter for QtxMainWindow.
37 class QtxMainWindow::Filter : public QObject
40 Filter( QWidget*, QtxMainWindow*, QObject* = 0 );
43 virtual bool eventFilter( QObject*, QEvent* );
50 QtxMainWindow::Filter::Filter( QWidget* wid, QtxMainWindow* mw, QObject* parent )
55 myMain->installEventFilter( this );
58 QtxMainWindow::Filter::~Filter()
62 bool QtxMainWindow::Filter::eventFilter( QObject* o, QEvent* e )
64 if ( myMain == o && e->type() == QEvent::ChildRemoved &&
65 myWidget == ((QChildEvent*)e)->child() )
68 return QObject::eventFilter( o, e );
72 Class: QtxMainWindow [Public]
73 Descr: Main window with support of dockable menubar/status bar
74 and geometry store/retrieve.
77 QtxMainWindow::QtxMainWindow( QWidget* parent, const char* name, WFlags f )
78 : QMainWindow( parent, name, f ),
85 QtxMainWindow::~QtxMainWindow()
87 setDockableMenuBar( false );
88 setDockableStatusBar( false );
91 bool QtxMainWindow::isDockableMenuBar() const
96 void QtxMainWindow::setDockableMenuBar( const bool on )
98 if ( isDockableMenuBar() == on )
101 QMenuBar* mb = menuBar();
105 if ( on && !myMenuBar )
107 mb->setCaption( tr( "Menu bar" ) );
108 QtxToolBar* dockMb = new QtxToolBar( true, this, "menu bar container" );
110 new Filter( mb, this, myMenuBar );
111 dockMb->setWidget( mb );
112 dockMb->setNewLine( true );
113 dockMb->setStretchable( true );
114 dockMb->setResizeEnabled( false );
116 moveDockWindow( dockMb, DockTop );
117 setDockEnabled( dockMb, Left, false );
118 setDockEnabled( dockMb, Right, false );
120 setAppropriate( dockMb, false );
122 connect( dockMb, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
124 else if ( !on && myMenuBar )
126 mb->reparent( this, QPoint( 0, 0 ), mb->isVisibleTo( mb->parentWidget() ) );
127 disconnect( myMenuBar, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
130 QChildEvent ce( QEvent::ChildRemoved, mb );
131 QApplication::sendEvent( this, &ce );
137 bool QtxMainWindow::isDockableStatusBar() const
142 void QtxMainWindow::setDockableStatusBar( const bool on )
144 if ( isDockableStatusBar() == on )
147 QStatusBar* sb = statusBar();
151 if ( on && !myStatusBar )
153 sb->setCaption( tr( "Status bar" ) );
154 QtxToolBar* dockSb = new QtxToolBar( true, this, "status bar container" );
155 myStatusBar = dockSb;
156 new Filter( sb, this, myStatusBar );
157 dockSb->setWidget( sb );
158 dockSb->setNewLine( true );
159 dockSb->setStretchable( true );
160 dockSb->setResizeEnabled( false );
161 sb->setMinimumWidth( 250 );
163 sb->setSizeGripEnabled( false );
165 moveDockWindow( dockSb, DockBottom );
166 setDockEnabled( dockSb, Left, false );
167 setDockEnabled( dockSb, Right, false );
169 setAppropriate( dockSb, false );
171 connect( dockSb, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
173 else if ( !on && myStatusBar )
175 sb->reparent( this, QPoint( 0, 0 ), sb->isVisibleTo( sb->parentWidget() ) );
176 disconnect( myStatusBar, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
179 QChildEvent ce( QEvent::ChildRemoved, sb );
180 QApplication::sendEvent( this, &ce );
182 sb->setSizeGripEnabled( true );
188 void QtxMainWindow::loadGeometry( QtxResourceMgr* resMgr, const QString& section )
190 QString sec = section.stripWhiteSpace();
191 if ( !resMgr || sec.isEmpty() )
195 if ( !resMgr->value( sec, "state", winState ) )
198 if ( resMgr->value( sec, "state", stateStr ) )
199 winState = windowState( stateStr );
202 int win_w = resMgr->integerValue( sec, "width", width() );
203 int win_h = resMgr->integerValue( sec, "height", height() );
205 int winPosX = windowPosition( resMgr->stringValue( sec, QString( "pos_x" ), QString::null ) );
206 int winPosY = windowPosition( resMgr->stringValue( sec, QString( "pos_y" ), QString::null ) );
208 QWidget* desk = QApplication::desktop();
211 if ( winPosX == WP_Absolute )
212 win_x = resMgr->integerValue( sec, "pos_x", x() );
214 win_x = relativeCoordinate( winPosX, desk->width(), win_w );
217 if ( winPosX == WP_Absolute )
218 win_y = resMgr->integerValue( sec, "pos_y", y() );
220 win_y = relativeCoordinate( winPosY, desk->height(), win_h );
222 bool vis = isVisibleTo( parentWidget() );
224 resize( win_w, win_h );
225 move( win_x, win_y );
230 QApplication::postEvent( this, new QCustomEvent( QEvent::User, (void*)winState ) );
235 void QtxMainWindow::show()
238 QApplication::postEvent( this, new QCustomEvent( QEvent::User, (void*)myMode ) );
245 void QtxMainWindow::customEvent( QCustomEvent* e )
247 QMainWindow::customEvent( e );
249 int mode = (int)e->data();
264 int QtxMainWindow::relativeCoordinate( const int type, const int WH, const int wh ) const
270 res = ( WH - wh ) / 2;
282 void QtxMainWindow::saveGeometry( QtxResourceMgr* resMgr, const QString& section ) const
284 QString sec = section.stripWhiteSpace();
285 if ( !resMgr || sec.isEmpty() )
288 resMgr->setValue( sec, "pos_x", pos().x() );
289 resMgr->setValue( sec, "pos_y", pos().y() );
290 resMgr->setValue( sec, "width", width() );
291 resMgr->setValue( sec, "height", height() );
293 int winState = WS_Normal;
295 winState = WS_Minimized;
296 else if ( isMaximized() )
297 winState = WS_Maximized;
299 resMgr->setValue( sec, "state", winState );
302 bool QtxMainWindow::eventFilter( QObject* o, QEvent* e )
304 return QMainWindow::eventFilter( o, e );
307 void QtxMainWindow::setAppropriate( QDockWindow* dw, bool a )
309 QMainWindow::setAppropriate( dw, myStatusBar != dw && myMenuBar != dw && a );
312 void QtxMainWindow::setUpLayout()
314 QMainWindow::setUpLayout();
316 if ( myMenuBar && layout() )
317 layout()->setMenuBar( 0 );
320 void QtxMainWindow::onDestroyed( QObject* obj )
323 if ( obj == myMenuBar )
328 else if ( obj == myStatusBar )
336 QChildEvent ce( QEvent::ChildRemoved, o );
337 QApplication::sendEvent( this, &ce );
341 int QtxMainWindow::windowState( const QString& str ) const
343 static QMap<QString, int> winStateMap;
344 if ( winStateMap.isEmpty() )
346 winStateMap["normal"] = WS_Normal;
347 winStateMap["min"] = WS_Minimized;
348 winStateMap["mini"] = WS_Minimized;
349 winStateMap["minimized"] = WS_Minimized;
350 winStateMap["max"] = WS_Maximized;
351 winStateMap["maxi"] = WS_Maximized;
352 winStateMap["maximized"] = WS_Maximized;
353 winStateMap["hidden"] = WS_Hidden;
354 winStateMap["hided"] = WS_Hidden;
355 winStateMap["hide"] = WS_Hidden;
356 winStateMap["invisible"] = WS_Hidden;
360 QString stateStr = str.stripWhiteSpace().lower();
361 if ( winStateMap.contains( stateStr ) )
362 res = winStateMap[stateStr];
366 int QtxMainWindow::windowPosition( const QString& str ) const
368 static QMap<QString, int> winPosMap;
369 if ( winPosMap.isEmpty() )
371 winPosMap["center"] = WP_Center;
372 winPosMap["left"] = WP_Left;
373 winPosMap["right"] = WP_Right;
374 winPosMap["top"] = WP_Top;
375 winPosMap["bottom"] = WP_Bottom;
378 int res = WP_Absolute;
379 QString posStr = str.stripWhiteSpace().lower();
380 if ( winPosMap.contains( posStr ) )
381 res = winPosMap[posStr];