Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

owidgetstack.cpp

Go to the documentation of this file.
00001 /*
00002                =.            This file is part of the OPIE Project
00003              .=l.            Copyright (c)  2003,2004,2005 Holger Hans Peter Freyther <zecke@handhelds.org>
00004            .>+-=
00005  _;:,     .>    :=|.         This library is free software; you can
00006 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00007 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00008 .="- .-=="i,     .._         License as published by the Free Software
00009  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00010      ._= =}       :          or (at your option) any later version.
00011     .%`+i>       _;_.
00012     .i_,=:_.      -<s.       This library is distributed in the hope that
00013      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00014     : ..    .:,     . . .    without even the implied warranty of
00015     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00016   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00017 ..}^=.=       =       ;      Library General Public License for more
00018 ++=   -.     .`     .:       details.
00019  :     =  ...= . :.=-
00020  -.   .:....=;==+<;          You should have received a copy of the GNU
00021   -_. . .   )=.  =           Library General Public License along with
00022     --        :-=`           this library; see the file COPYING.LIB.
00023                              If not, write to the Free Software Foundation,
00024                              Inc., 59 Temple Place - Suite 330,
00025                              Boston, MA 02111-1307, USA.
00026 
00027 */
00028 
00029 #include "owidgetstack.h"
00030 
00031 /* OPIE */
00032 #include <opie2/odebug.h>
00033 
00034 /* QT */
00035 #include <qapplication.h>
00036 #include <qwidgetstack.h>
00037 
00038 namespace Opie {
00039 namespace Ui   {
00040     const int mode_size = 330;
00041 
00042 
00043 
00055 OWidgetStack::OWidgetStack( QWidget* parent, const char* name, WFlags fl)
00056     : QFrame( parent, name, fl )
00057 {
00058     m_last = m_mWidget = 0;
00059     m_forced = false;
00060 
00061     QApplication::desktop()->installEventFilter( this );
00062     setFontPropagation   ( AllChildren );
00063     setPalettePropagation( AllChildren );
00064 
00065     /* sets m_mode and initializes more */
00066     /* if you change this call change switchTop as well */
00067     m_stack = 0;
00068     switchStack();
00069 }
00070 
00075 OWidgetStack::~OWidgetStack() {
00076     if (m_mode == BigScreen && !m_list.isEmpty() ) {
00077         QMap<int, QWidget*>::Iterator it = m_list.begin();
00078         for ( ; it != m_list.end(); ++it )
00079             delete it.data();
00080     }
00081     m_list.clear();
00082 
00083 }
00084 
00092 enum OWidgetStack::Mode OWidgetStack::mode()const {
00093     return m_mode;
00094 }
00095 
00102 void OWidgetStack::forceMode( enum Mode mode) {
00103     m_forced = mode != NoForce;
00104 
00105     /* we need to see which mode we're in */
00106     if (!m_forced ) {
00107         if ( QApplication::desktop()->width() >=
00108              mode_size )
00109             mode = BigScreen;
00110         else
00111             mode = SmallScreen;
00112     }
00113     switch( mode ) {
00114     case NoForce:
00115     case SmallScreen:
00116         switchStack();
00117         break;
00118     case BigScreen:
00119         switchTop();
00120         break;
00121 
00122     }
00123 
00124     m_mode = mode;
00125 }
00126 
00141 void OWidgetStack::addWidget( QWidget* wid, int id) {
00142     if (!wid)
00143         return;
00144 
00145     /* set our main widget */
00146     if (!m_mWidget)
00147         m_mWidget = wid;
00148 
00149     m_list.insert( id, wid );
00150 
00156     if (m_mode == SmallScreen )
00157         m_stack->addWidget( wid,id );
00158     else if ( m_mWidget == wid ) {
00159         wid->reparent(this, 0, contentsRect().topLeft() );
00160         wid->hide();
00161     }else {
00162         wid->reparent(0, WType_TopLevel, QPoint(10, 10) );
00163         wid->hide();
00164     }
00165 }
00166 
00167 
00176 void OWidgetStack::removeWidget( QWidget* wid) {
00177     if (!wid)
00178         return;
00179 
00180     if (m_mode == SmallScreen )
00181         m_stack->removeWidget( wid );
00182 
00183 
00184     wid->reparent(0, 0, QPoint(0, 0) );
00185     m_list.remove( id(wid) );
00186 
00187     if ( wid == m_mWidget )
00188         m_mWidget = 0;
00189 }
00190 
00191 #if 0
00192 
00195 QSizeHint OWidgetStack::sizeHint()const {
00196 
00197 }
00198 
00202 QSizeHint OWidgetStack::minimumSizeHint()const {
00203 
00204 }
00205 #endif
00206 
00217 QWidget* OWidgetStack::widget( int id) const {
00218     return m_list[id];
00219 }
00220 
00226 int OWidgetStack::id( QWidget* wid)const{
00227     if (m_list.isEmpty() )
00228         return -1;
00229 
00230     QMap<int, QWidget*>::ConstIterator it = m_list.begin();
00231     for ( ; it != m_list.end(); ++it )
00232         if ( it.data() == wid )
00233             break;
00234 
00235     /* if not at the end return the key */
00236     return it == m_list.end() ? -1 : it.key();
00237 }
00238 
00239 
00245 QWidget* OWidgetStack::visibleWidget()const {
00246     if (m_mode == SmallScreen )
00247         return m_stack->visibleWidget();
00248     else
00249         return m_mWidget;
00250 
00251 }
00252 
00261 void OWidgetStack::raiseWidget( int id) {
00262     return raiseWidget( widget( id ) );
00263 }
00264 
00269 void OWidgetStack::raiseWidget( QWidget* wid) {
00270     m_last = wid;
00271     if (m_mode == SmallScreen )
00272         m_stack->raiseWidget( wid );
00273     else {
00274         int ide;
00275         emit aboutToShow( wid );
00276         /* if someone is connected and the widget is actually available */
00277         if ( receivers( SIGNAL(aboutToShow(int) ) ) &&
00278              ( (ide = id( wid ) ) != -1 ) )
00279             emit aboutToShow( ide );
00280 
00281         /* ### FIXME PLACE THE WIDGET right */
00282         wid->show();
00283     }
00284 }
00285 
00292 void OWidgetStack::hideWidget( int id) {
00293     /* hiding our main widget wouldn't be smart */
00294     if ( m_mode == BigScreen && m_last != m_mWidget )
00295         m_last->hide();
00296     raiseWidget( id );
00297 }
00298 
00303 void OWidgetStack::hideWidget( QWidget* wid) {
00304     /* still not smart */
00305     if ( m_mode == BigScreen && m_last != m_mWidget )
00306         m_last->hide();
00307 
00308     raiseWidget( wid );
00309 }
00310 
00311 
00312 bool OWidgetStack::eventFilter( QObject*, QEvent* e) {
00313     if ( e->type() == QEvent::Resize && !m_forced ) {
00314         QResizeEvent *res = static_cast<QResizeEvent*>( e );
00315         QSize size = res->size();
00316         if ( size.width() >= mode_size )
00317             switchTop();
00318         else
00319             switchStack();
00320     }
00321     return false;
00322 }
00323 
00324 
00328 void OWidgetStack::resizeEvent( QResizeEvent* ev ) {
00329     QFrame::resizeEvent( ev );
00330     if (m_mode == SmallScreen )
00331         m_stack->setGeometry( frameRect() );
00332     else
00333         if (m_mWidget )
00334             m_mWidget->setGeometry( frameRect() );
00335 
00336 }
00337 
00353 void OWidgetStack::setMainWindow( QWidget* wid ) {
00354     if (m_mode == BigScreen ) {
00355         bool wasVisible = false;
00356         if (m_mWidget ) {
00357             wasVisible = !m_mWidget->isHidden();
00358             /* hidden by default */
00359             m_mWidget->reparent(0, WType_TopLevel, QPoint(10, 10) );
00360         }
00361         wid->reparent(this, 0, frameRect().topLeft() );
00362 
00363         if (wasVisible)
00364             wid->show();
00365     }
00366 
00367     m_mWidget = wid;
00368 }
00369 
00375 void OWidgetStack::setMainWindow( int id) {
00376     setMainWindow( widget( id ) );
00377 }
00378 
00379 
00380 /*
00381  * this function switches to a stack ;)
00382  */
00383 void OWidgetStack::switchStack() {
00384     if (m_stack ) {
00385         m_stack->setGeometry( frameRect() );
00386         return;
00387     }
00388 
00389     m_mode = SmallScreen;
00390     m_stack = new QWidgetStack(this);
00391     m_stack->setGeometry( frameRect() );
00392     m_stack->show();
00393 
00394     connect(m_stack, SIGNAL(aboutToShow(QWidget*) ),
00395             this, SIGNAL(aboutToShow(QWidget*) ) );
00396     connect(m_stack, SIGNAL(aboutToShow(int) ),
00397             this, SIGNAL(aboutToShow(int) ) );
00398 
00399     /* now reparent the widgets... luckily QWidgetSatck does most of the work */
00400     if (m_list.isEmpty() )
00401         return;
00402 
00403     QMap<int, QWidget*>::Iterator it = m_list.begin();
00404     for ( ; it != m_list.end(); ++it )
00405         m_stack->addWidget( it.data(), it.key() );
00406 
00407     if ( m_mWidget )
00408         m_stack->raiseWidget( m_mWidget );
00409 
00410 
00411 }
00412 
00413 /*
00414  * we will switch to top level mode
00415  * reparent the list of widgets and then delete the stack
00416  */
00417 void OWidgetStack::switchTop() {
00418     m_mode = BigScreen;
00419     /* this works because it is guaranteed that switchStack was called at least once*/
00420     if (!m_stack && m_mWidget) {
00421         m_mWidget->setGeometry( frameRect() );
00422         return;
00423     }else if (!m_stack)
00424         return;
00425 
00426     if (!m_list.isEmpty() ) {
00427         QMap<int, QWidget*>::Iterator it = m_list.begin();
00428         for ( ; it != m_list.end(); ++it ) {
00429             /* better than reparenting twice */
00430             if ( it.data() == m_mWidget ) {
00431                 m_mWidget->reparent(this, 0, frameRect().topLeft() );
00432                 m_mWidget->setGeometry( frameRect() );
00433                 m_mWidget->show();
00434             }else
00435                 /* ### FIXME we need to place the widget better */
00436                 it.data()->reparent(0, WType_TopLevel, QPoint(10, 10) );
00437         }
00438     }
00439 
00440     delete m_stack;
00441     m_stack = 0;
00442 }
00443 
00444 }
00445 }

Generated on Sat Nov 5 16:16:24 2005 for OPIE by  doxygen 1.4.2