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/ or email : webmaster.salome@opencascade.com
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* );
53 QtxMainWindow::Filter::Filter( QWidget* wid, QtxMainWindow* mw, QObject* parent )
58 myMain->installEventFilter( this );
64 QtxMainWindow::Filter::~Filter()
71 bool QtxMainWindow::Filter::eventFilter( QObject* o, QEvent* e )
73 if ( myMain == o && e->type() == QEvent::ChildRemoved &&
74 myWidget == ((QChildEvent*)e)->child() )
77 return QObject::eventFilter( o, e );
81 Class: QtxMainWindow [Public]
82 Descr: Main window with support of dockable menubar/status bar
83 and geometry store/retrieve.
85 QtxMainWindow::QtxMainWindow( QWidget* parent, const char* name, WFlags f )
86 : QMainWindow( parent, name, f ),
96 QtxMainWindow::~QtxMainWindow()
98 setDockableMenuBar( false );
99 setDockableStatusBar( false );
103 \return true if menu bar exists
105 bool QtxMainWindow::isDockableMenuBar() const
111 Creates or deletes menu bar
112 \param on - if it is true, then to create, otherwise - to delete
114 void QtxMainWindow::setDockableMenuBar( const bool on )
116 if ( isDockableMenuBar() == on )
119 QMenuBar* mb = menuBar();
123 if ( on && !myMenuBar )
125 mb->setCaption( tr( "Menu bar" ) );
126 QtxToolBar* dockMb = new QtxToolBar( true, this, "menu bar container" );
128 new Filter( mb, this, myMenuBar );
129 dockMb->setWidget( mb );
130 dockMb->setNewLine( true );
131 dockMb->setStretchable( true );
132 dockMb->setResizeEnabled( false );
134 moveDockWindow( dockMb, DockTop );
135 setDockEnabled( dockMb, Left, false );
136 setDockEnabled( dockMb, Right, false );
138 setAppropriate( dockMb, false );
140 connect( dockMb, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
142 else if ( !on && myMenuBar )
144 mb->reparent( this, QPoint( 0, 0 ), mb->isVisibleTo( mb->parentWidget() ) );
145 disconnect( myMenuBar, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
148 QChildEvent ce( QEvent::ChildRemoved, mb );
149 QApplication::sendEvent( this, &ce );
156 \return true if status bar exists
158 bool QtxMainWindow::isDockableStatusBar() const
164 Creates or deletes status bar
165 \param on - if it is true, then to create, otherwise - to delete
167 void QtxMainWindow::setDockableStatusBar( const bool on )
169 if ( isDockableStatusBar() == on )
172 QStatusBar* sb = statusBar();
176 if ( on && !myStatusBar )
178 sb->setCaption( tr( "Status bar" ) );
179 QtxToolBar* dockSb = new QtxToolBar( true, this, "status bar container" );
180 myStatusBar = dockSb;
181 new Filter( sb, this, myStatusBar );
182 dockSb->setWidget( sb );
183 dockSb->setNewLine( true );
184 dockSb->setStretchable( true );
185 dockSb->setResizeEnabled( false );
186 sb->setMinimumWidth( 250 );
188 sb->setSizeGripEnabled( false );
190 moveDockWindow( dockSb, DockBottom );
191 setDockEnabled( dockSb, Left, false );
192 setDockEnabled( dockSb, Right, false );
194 setAppropriate( dockSb, false );
196 connect( dockSb, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
198 else if ( !on && myStatusBar )
200 sb->reparent( this, QPoint( 0, 0 ), sb->isVisibleTo( sb->parentWidget() ) );
201 disconnect( myStatusBar, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
204 QChildEvent ce( QEvent::ChildRemoved, sb );
205 QApplication::sendEvent( this, &ce );
207 sb->setSizeGripEnabled( true );
214 Retrieve the geometry information from the specified resource manager section.
215 \param resMgr - instance of ersource manager
216 \param section - section name
218 void QtxMainWindow::loadGeometry( QtxResourceMgr* resMgr, const QString& section )
220 QString sec = section.stripWhiteSpace();
221 if ( !resMgr || sec.isEmpty() )
225 if ( !resMgr->value( sec, "state", winState ) )
228 if ( resMgr->value( sec, "state", stateStr ) )
229 winState = windowState( stateStr );
232 int win_w = resMgr->integerValue( sec, "width", width() );
233 int win_h = resMgr->integerValue( sec, "height", height() );
235 int winPosX = windowPosition( resMgr->stringValue( sec, QString( "pos_x" ), QString::null ) );
236 int winPosY = windowPosition( resMgr->stringValue( sec, QString( "pos_y" ), QString::null ) );
238 QWidget* desk = QApplication::desktop();
241 if ( winPosX == WP_Absolute )
242 win_x = resMgr->integerValue( sec, "pos_x", x() );
244 win_x = relativeCoordinate( winPosX, desk->width(), win_w );
247 if ( winPosX == WP_Absolute )
248 win_y = resMgr->integerValue( sec, "pos_y", y() );
250 win_y = relativeCoordinate( winPosY, desk->height(), win_h );
252 bool vis = isVisibleTo( parentWidget() );
254 resize( win_w, win_h );
255 move( win_x, win_y );
260 QApplication::postEvent( this, new QCustomEvent( QEvent::User, (void*)winState ) );
268 void QtxMainWindow::show()
271 QApplication::postEvent( this, new QCustomEvent( QEvent::User, (void*)myMode ) );
279 Handler of custom events
281 void QtxMainWindow::customEvent( QCustomEvent* e )
283 QMainWindow::customEvent( e );
285 size_t mode = size_t(e->data());
301 \return relative co-ordinate by two points
302 \param type - type of result: WP_Center (center), WP_Left (left), WP_Right (right)
303 \param wh - left point
304 \param WH - right point
306 int QtxMainWindow::relativeCoordinate( const int type, const int WH, const int wh ) const
312 res = ( WH - wh ) / 2;
325 Store the geometry information into the specified resource manager section.
326 \param resMgr - instance of ersource manager
327 \param section - section name
329 void QtxMainWindow::saveGeometry( QtxResourceMgr* resMgr, const QString& section ) const
331 QString sec = section.stripWhiteSpace();
332 if ( !resMgr || sec.isEmpty() )
335 resMgr->setValue( sec, "pos_x", pos().x() );
336 resMgr->setValue( sec, "pos_y", pos().y() );
337 resMgr->setValue( sec, "width", width() );
338 resMgr->setValue( sec, "height", height() );
340 int winState = WS_Normal;
342 winState = WS_Minimized;
343 else if ( isMaximized() )
344 winState = WS_Maximized;
346 resMgr->setValue( sec, "state", winState );
352 bool QtxMainWindow::eventFilter( QObject* o, QEvent* e )
354 return QMainWindow::eventFilter( o, e );
358 Controls whether or not the dw dock window's caption should appear
359 as a menu item on the dock window menu that lists the dock windows.
361 \param a - if it is true, then it appears in menu
363 void QtxMainWindow::setAppropriate( QDockWindow* dw, bool a )
365 QMainWindow::setAppropriate( dw, myStatusBar != dw && myMenuBar != dw && a );
371 void QtxMainWindow::setUpLayout()
373 QMainWindow::setUpLayout();
375 if ( myMenuBar && layout() )
376 layout()->setMenuBar( 0 );
380 SLOT: called on object destroyed, clears internal fields in case of deletion of menu bar or status bar
382 void QtxMainWindow::onDestroyed( QObject* obj )
385 if ( obj == myMenuBar )
390 else if ( obj == myStatusBar )
398 QChildEvent ce( QEvent::ChildRemoved, o );
399 QApplication::sendEvent( this, &ce );
404 \return flag of window state by it's name
405 \param str - name of flag
407 int QtxMainWindow::windowState( const QString& str ) const
409 static QMap<QString, int> winStateMap;
410 if ( winStateMap.isEmpty() )
412 winStateMap["normal"] = WS_Normal;
413 winStateMap["min"] = WS_Minimized;
414 winStateMap["mini"] = WS_Minimized;
415 winStateMap["minimized"] = WS_Minimized;
416 winStateMap["max"] = WS_Maximized;
417 winStateMap["maxi"] = WS_Maximized;
418 winStateMap["maximized"] = WS_Maximized;
419 winStateMap["hidden"] = WS_Hidden;
420 winStateMap["hided"] = WS_Hidden;
421 winStateMap["hide"] = WS_Hidden;
422 winStateMap["invisible"] = WS_Hidden;
426 QString stateStr = str.stripWhiteSpace().lower();
427 if ( winStateMap.contains( stateStr ) )
428 res = winStateMap[stateStr];
433 \return flag of position by it's name
434 \param str - name of position
436 int QtxMainWindow::windowPosition( const QString& str ) const
438 static QMap<QString, int> winPosMap;
439 if ( winPosMap.isEmpty() )
441 winPosMap["center"] = WP_Center;
442 winPosMap["left"] = WP_Left;
443 winPosMap["right"] = WP_Right;
444 winPosMap["top"] = WP_Top;
445 winPosMap["bottom"] = WP_Bottom;
448 int res = WP_Absolute;
449 QString posStr = str.stripWhiteSpace().lower();
450 if ( winPosMap.contains( posStr ) )
451 res = winPosMap[posStr];