summaryrefslogtreecommitdiff
path: root/noncore/settings/packagemanager/promptdlg.cpp
blob: 4e82ba9309a52e18de2abd9a6d9dff0a75697912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
                            This file is part of the OPIE Project

               =.            Copyright (c)  2003 Dan Williams <drw@handhelds.org>
             .=l.
           .>+-=
 _;:,     .>    :=|.         This file is free software; you can
.> <`_,   >  .   <=          redistribute it and/or modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s.       This file is distributed in the hope that
     +  .  -:.       =       it will be useful, but WITHOUT ANY WARRANTY;
    : ..    .:,     . . .    without even the implied warranty of
    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU General
..}^=.=       =       ;      Public License for more details.
++=   -.     .`     .:
 :     =  ...= . :.=-        You should have received a copy of the GNU
 -.   .:....=;==+<;          General Public License along with this file;
  -_. . .   )=.  =           see the file COPYING. If not, write to the
    --        :-=`           Free Software Foundation, Inc.,
                             59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.

*/

#include "promptdlg.h"

#include <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qwidgetlist.h>

#include <qpe/qpeapplication.h>

PromptDlg::PromptDlg( const QString &caption, const QString &text, const QString &btn1, const QString &btn2,
                      QWidget *parent )
    : QWidget( parent, QString::null, WType_Modal | WType_TopLevel | WStyle_Dialog )
    , m_btnClicked( -1 )
{
    setCaption( caption );

    QGridLayout *layout = new QGridLayout( this, 2, 2, 4, 2 );
    QLabel *label = new QLabel( text, this );
    label->setAlignment( AlignCenter | WordBreak );
    layout->addMultiCellWidget( label, 0, 0, 0, 1 );

    QPushButton *btn = new QPushButton( btn1, this );
    layout->addWidget( btn, 1, 0 );
    connect( btn, SIGNAL(clicked()), this, SLOT(slotBtn1Clicked()) );

    btn = new QPushButton( btn2, this );
    layout->addWidget( btn, 1, 1 );
    connect( btn, SIGNAL(clicked()), this, SLOT(slotBtn2Clicked()) );
}

int PromptDlg::display()
{
    // Determine position of dialog.  Derived from QT's QDialog::show() method.
    QWidget *w = parentWidget();
    QPoint p( 0, 0 );
    int extraw = 0, extrah = 0;
    QWidget * desk = QApplication::desktop();
    if ( w )
        w = w->topLevelWidget();

    QWidgetList  *list = QApplication::topLevelWidgets();
    QWidgetListIt it( *list );
    while ( (extraw == 0 || extrah == 0) && it.current() != 0 )
    {
        int w, h;
        QWidget * current = it.current();
        ++it;
        w = current->geometry().x() - current->x();
        h = current->geometry().y() - current->y();

        extraw = QMAX( extraw, w );
        extrah = QMAX( extrah, h );
    }
    delete list;

    // sanity check for decoration frames. With embedding, we
    // might get extraordinary values
    if ( extraw >= 10 || extrah >= 40 )
        extraw = extrah = 0;

    if ( w )
    {
        // Use mapToGlobal rather than geometry() in case w might
        // be embedded in another application
        QPoint pp = w->mapToGlobal( QPoint(0,0) );
        p = QPoint( pp.x() + w->width()/2, pp.y() + w->height()/ 2 );
    }
    else
        p = QPoint( desk->width()/2, desk->height()/2 );

    p = QPoint( p.x()-width()/2 - extraw, p.y()-height()/2 - extrah );

    if ( p.x() + extraw + width() > desk->width() )
        p.setX( desk->width() - width() - extraw );
    if ( p.x() < 0 )
        p.setX( 0 );

    if ( p.y() + extrah + height() > desk->height() )
        p.setY( desk->height() - height() - extrah );
    if ( p.y() < 0 )
        p.setY( 0 );

    move( p );
    show();

    // Enter event loop for modality
    qApp->enter_loop();

    return m_btnClicked;
}

int PromptDlg::ask( const QString &caption, const QString &text, const QString &btn1, const QString &btn2,
                    QWidget *parent )
{
    PromptDlg *dlg = new PromptDlg( caption, text, btn1, btn2, parent );
    int rc = dlg->display();
    delete dlg;
    return rc;
}

void PromptDlg::slotBtn1Clicked()
{
    m_btnClicked = 1;
    qApp->exit_loop();
}

void PromptDlg::slotBtn2Clicked()
{
    m_btnClicked = 2;
    qApp->exit_loop();
}