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

promptdlg.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003 
00004                              Copyright (C)2004, 2005 Dan Williams <drw@handhelds.org>
00005               =.
00006             .=l.
00007            .>+-=
00008  _;:,     .>    :=|.         This program is free software; you can
00009 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00010 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00011 .="- .-=="i,     .._         License as published by the Free Software
00012  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00013      ._= =}       :          or (at your option) any later version.
00014     .%`+i>       _;_.
00015     .i_,=:_.      -<s.       This program is distributed in the hope that
00016      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00017     : ..    .:,     . . .    without even the implied warranty of
00018     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00019   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00020 ..}^=.=       =       ;      Library General Public License for more
00021 ++=   -.     .`     .:       details.
00022 :     =  ...= . :.=-
00023  -.   .:....=;==+<;          You should have received a copy of the GNU
00024   -_. . .   )=.  =           Library General Public License along with
00025     --        :-=`           this library; see the file COPYING.LIB.
00026                              If not, write to the Free Software Foundation,
00027                              Inc., 59 Temple Place - Suite 330,
00028                              Boston, MA 02111-1307, USA.
00029 */
00030 
00031 #include "promptdlg.h"
00032 
00033 #include <qpe/qpeapplication.h>
00034 
00035 #include <qlabel.h>
00036 #include <qlayout.h>
00037 #include <qpushbutton.h>
00038 #include <qwidgetlist.h>
00039 
00040 PromptDlg::PromptDlg( const QString &caption, const QString &text, const QString &btn1, const QString &btn2,
00041                       QWidget *parent )
00042     : QWidget( parent, QString::null, WType_Modal | WType_TopLevel | WStyle_Dialog )
00043     , m_btnClicked( -1 )
00044 {
00045     setCaption( caption );
00046 
00047     QGridLayout *layout = new QGridLayout( this, 2, 2, 4, 2 );
00048     QLabel *label = new QLabel( text, this );
00049     label->setAlignment( AlignCenter | WordBreak );
00050     layout->addMultiCellWidget( label, 0, 0, 0, 1 );
00051 
00052     QPushButton *btn = new QPushButton( btn1, this );
00053     layout->addWidget( btn, 1, 0 );
00054     connect( btn, SIGNAL(clicked()), this, SLOT(slotBtn1Clicked()) );
00055 
00056     btn = new QPushButton( btn2, this );
00057     layout->addWidget( btn, 1, 1 );
00058     connect( btn, SIGNAL(clicked()), this, SLOT(slotBtn2Clicked()) );
00059 }
00060 
00061 int PromptDlg::display()
00062 {
00063     // Determine position of dialog.  Derived from QT's QDialog::show() method.
00064     QWidget *w = parentWidget();
00065     QPoint p( 0, 0 );
00066     int extraw = 0, extrah = 0;
00067     QWidget * desk = QApplication::desktop();
00068     if ( w )
00069         w = w->topLevelWidget();
00070 
00071     QWidgetList  *list = QApplication::topLevelWidgets();
00072     QWidgetListIt it( *list );
00073     while ( (extraw == 0 || extrah == 0) && it.current() != 0 )
00074     {
00075         int w, h;
00076         QWidget * current = it.current();
00077         ++it;
00078         w = current->geometry().x() - current->x();
00079         h = current->geometry().y() - current->y();
00080 
00081         extraw = QMAX( extraw, w );
00082         extrah = QMAX( extrah, h );
00083     }
00084     delete list;
00085 
00086     // sanity check for decoration frames. With embedding, we
00087     // might get extraordinary values
00088     if ( extraw >= 10 || extrah >= 40 )
00089         extraw = extrah = 0;
00090 
00091     if ( w )
00092     {
00093         // Use mapToGlobal rather than geometry() in case w might
00094         // be embedded in another application
00095         QPoint pp = w->mapToGlobal( QPoint(0,0) );
00096         p = QPoint( pp.x() + w->width()/2, pp.y() + w->height()/ 2 );
00097     }
00098     else
00099         p = QPoint( desk->width()/2, desk->height()/2 );
00100 
00101     p = QPoint( p.x()-width()/2 - extraw, p.y()-height()/2 - extrah );
00102 
00103     if ( p.x() + extraw + width() > desk->width() )
00104         p.setX( desk->width() - width() - extraw );
00105     if ( p.x() < 0 )
00106         p.setX( 0 );
00107 
00108     if ( p.y() + extrah + height() > desk->height() )
00109         p.setY( desk->height() - height() - extrah );
00110     if ( p.y() < 0 )
00111         p.setY( 0 );
00112 
00113     move( p );
00114     show();
00115 
00116     // Enter event loop for modality
00117     qApp->enter_loop();
00118 
00119     return m_btnClicked;
00120 }
00121 
00122 int PromptDlg::ask( const QString &caption, const QString &text, const QString &btn1, const QString &btn2,
00123                     QWidget *parent )
00124 {
00125     PromptDlg *dlg = new PromptDlg( caption, text, btn1, btn2, parent );
00126     int rc = dlg->display();
00127     delete dlg;
00128     return rc;
00129 }
00130 
00131 void PromptDlg::slotBtn1Clicked()
00132 {
00133     m_btnClicked = 1;
00134     qApp->exit_loop();
00135 }
00136 
00137 void PromptDlg::slotBtn2Clicked()
00138 {
00139     m_btnClicked = 2;
00140     qApp->exit_loop();
00141 }

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