~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/dialogs/qinputdialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the dialog module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qinputdialog.h"
 
30
 
 
31
#ifndef QT_NO_INPUTDIALOG
 
32
 
 
33
#include "qlayout.h"
 
34
#include "qlabel.h"
 
35
#include "qlineedit.h"
 
36
#include "qpushbutton.h"
 
37
#include "qspinbox.h"
 
38
#include "qcombobox.h"
 
39
#include "qstackedlayout.h"
 
40
#include "qvalidator.h"
 
41
#include "qapplication.h"
 
42
 
 
43
#include "qdialog_p.h"
 
44
 
 
45
class QInputDialogPrivate : public QDialogPrivate
 
46
{
 
47
    Q_DECLARE_PUBLIC(QInputDialog)
 
48
public:
 
49
    QInputDialogPrivate();
 
50
    QLabel *label;
 
51
    QPushButton *ok;
 
52
    QWidget *input;
 
53
 
 
54
    void init(const QString &label, QInputDialog::Type);
 
55
    void tryAccept();
 
56
};
 
57
 
 
58
QInputDialogPrivate::QInputDialogPrivate()
 
59
    : QDialogPrivate(),
 
60
      label(0)
 
61
{
 
62
}
 
63
 
 
64
void QInputDialogPrivate::init(const QString &lbl, QInputDialog::Type type)
 
65
{
 
66
    Q_Q(QInputDialog);
 
67
    QVBoxLayout *vbox = new QVBoxLayout(q);
 
68
    vbox->setMargin(6);
 
69
    vbox->setSpacing(6);
 
70
 
 
71
    label = new QLabel(lbl, q);
 
72
    vbox->addWidget(label);
 
73
    vbox->addStretch(1);
 
74
 
 
75
    switch (type) {
 
76
    case QInputDialog::LineEdit:
 
77
        input = new QLineEdit(q);
 
78
        break;
 
79
    case QInputDialog::SpinBox:
 
80
        input = new QSpinBox(q);
 
81
        break;
 
82
    case QInputDialog::DoubleSpinBox:
 
83
        input = new QDoubleSpinBox(q);
 
84
        break;
 
85
    case QInputDialog::ComboBox:
 
86
    case QInputDialog::EditableComboBox: {
 
87
        QComboBox *combo = new QComboBox(q);
 
88
        if (type == QInputDialog::EditableComboBox)
 
89
            combo->setEditable(true);
 
90
        input = combo;
 
91
    }
 
92
        break;
 
93
    }
 
94
    vbox->addWidget(input);
 
95
    vbox->addStretch(1);
 
96
 
 
97
    QHBoxLayout *hbox = new QHBoxLayout;
 
98
    hbox->setSpacing(6);
 
99
    vbox->addLayout(hbox, Qt::AlignRight);
 
100
 
 
101
    ok = new QPushButton(QInputDialog::tr("OK"), q);
 
102
    ok->setDefault(true);
 
103
    QPushButton *cancel = new QPushButton(QInputDialog::tr("Cancel"), q);
 
104
 
 
105
    QSize bs = ok->sizeHint().expandedTo(cancel->sizeHint());
 
106
    ok->setFixedSize(bs);
 
107
    cancel->setFixedSize(bs);
 
108
 
 
109
    hbox->addStretch();
 
110
    hbox->addWidget(ok);
 
111
    hbox->addWidget(cancel);
 
112
 
 
113
    QObject::connect(ok, SIGNAL(clicked()), q, SLOT(accept()));
 
114
    QObject::connect(cancel, SIGNAL(clicked()), q, SLOT(reject()));
 
115
 
 
116
    q->resize(q->sizeHint());
 
117
}
 
118
 
 
119
 
 
120
/*!
 
121
    \class QInputDialog
 
122
    \brief The QInputDialog class provides a simple convenience dialog to get a single value from the user.
 
123
    \ingroup dialogs
 
124
    \mainclass
 
125
 
 
126
    The input value can be a string, a number or an item from a list. A
 
127
    label must be set to tell the user what they should enter.
 
128
 
 
129
    Four static convenience functions are provided:
 
130
    getText(), getInteger(), getDouble() and getItem(). All the
 
131
    functions can be used in a similar way, for example:
 
132
 
 
133
    \quotefromfile dialogs/standarddialogs/dialog.cpp
 
134
    \skipuntil Dialog::setText
 
135
    \skipline {
 
136
    \printto }
 
137
 
 
138
    The \c ok variable is set to true if the user clicks \gui OK;
 
139
    otherwise it is set to false.
 
140
 
 
141
    \img inputdialogs.png Input Dialogs
 
142
 
 
143
    The \l{dialogs/standarddialogs}{Standard Dialogs} example shows
 
144
    how to use QInputDialog as well as other built-in Qt dialogs.
 
145
 
 
146
    \sa QMessageBox
 
147
*/
 
148
 
 
149
/*!
 
150
  \enum QInputDialog::Type
 
151
 
 
152
  This enum specifies the type of the dialog, i.e. what kind of data you
 
153
  want the user to input:
 
154
 
 
155
  \value LineEdit  A QLineEdit is used for obtaining string or numeric
 
156
  input. The QLineEdit can be accessed using lineEdit().
 
157
 
 
158
  \value SpinBox  A QSpinBox is used for obtaining integer input.
 
159
  Use spinBox() to access the QSpinBox.
 
160
 
 
161
  \value ComboBox  A read-only QComboBox is used to provide a fixed
 
162
  list of choices from which the user can choose.
 
163
  Use comboBox() to access the QComboBox.
 
164
 
 
165
  \value EditableComboBox  An editable QComboBox is used to provide a fixed
 
166
  list of choices from which the user can choose, but which also
 
167
  allows the user to enter their own value instead.
 
168
  Use editableComboBox() to access the QComboBox.
 
169
*/
 
170
 
 
171
 
 
172
/*!
 
173
  Constructs the dialog. The \a label is the text which is shown to
 
174
  the user (it should tell the user what they are expected to enter).
 
175
  The \a parent is the dialog's parent widget. The \a type parameter
 
176
  is used to specify which type of dialog to construct. The \a f
 
177
  parameter is passed on to the QDialog constructor.
 
178
 
 
179
  \sa getText(), getInteger(), getDouble(), getItem()
 
180
*/
 
181
 
 
182
QInputDialog::QInputDialog(const QString &label, QWidget* parent, Type type, Qt::WFlags f)
 
183
    : QDialog(*new QInputDialogPrivate, parent, f)
 
184
{
 
185
    Q_D(QInputDialog);
 
186
    d->init(label, type);
 
187
}
 
188
 
 
189
/*!
 
190
    Destroys the input dialog.
 
191
*/
 
192
 
 
193
QInputDialog::~QInputDialog()
 
194
{
 
195
}
 
196
 
 
197
/*!
 
198
    Static convenience function to get a string from the user. \a
 
199
    title is the text which is displayed in the title bar of the
 
200
    dialog. \a label is the text which is shown to the user (it should
 
201
    say what should be entered). \a text is the default text which is
 
202
    placed in the line edit. The \a mode is the echo mode the line
 
203
    edit will use. If \a ok is non-null \e *\a ok will be set to true
 
204
    if the user pressed \gui OK and to false if the user pressed
 
205
    \gui Cancel. The dialog's parent is \a parent. The dialog will be
 
206
    modal and uses the widget flags \a f.
 
207
 
 
208
    This function returns the text which has been entered in the line
 
209
    edit. It will not return an empty string.
 
210
 
 
211
    Use this static function like this:
 
212
 
 
213
    \quotefromfile dialogs/standarddialogs/dialog.cpp
 
214
    \skipuntil Dialog::setText
 
215
    \skipline {
 
216
    \printto }
 
217
 
 
218
    \sa getInteger(), getDouble(), getItem()
 
219
*/
 
220
 
 
221
QString QInputDialog::getText(QWidget *parent, const QString &title, const QString &label,
 
222
                               QLineEdit::EchoMode mode, const QString &text,
 
223
                               bool *ok, Qt::WFlags f)
 
224
{
 
225
    QInputDialog dlg(label, parent, LineEdit, f);
 
226
    dlg.setObjectName("qt_inputdlg_gettext");
 
227
 
 
228
#ifndef QT_NO_WIDGET_TOPEXTRA
 
229
    dlg.setWindowTitle(title);
 
230
#endif
 
231
    QLineEdit *le = qobject_cast<QLineEdit *>(dlg.d_func()->input);
 
232
    le->setText(text);
 
233
    le->setEchoMode(mode);
 
234
 
 
235
    QString result;
 
236
    bool accepted = (dlg.exec() == QDialog::Accepted);
 
237
    if (ok)
 
238
        *ok = accepted;
 
239
    if (accepted)
 
240
        result = le->text();
 
241
 
 
242
    return result;
 
243
}
 
244
 
 
245
/*!
 
246
    Static convenience function to get an integer input from the
 
247
    user. \a title is the text which is displayed in the title bar
 
248
    of the dialog.  \a label is the text which is shown to the user
 
249
    (it should say what should be entered). \a value is the default
 
250
    integer which the spinbox will be set to.  \a minValue and \a
 
251
    maxValue are the minimum and maximum values the user may choose,
 
252
    and \a step is the amount by which the values change as the user
 
253
    presses the arrow buttons to increment or decrement the value.
 
254
 
 
255
    If \a ok is non-null *\a ok will be set to true if the user
 
256
    pressed \gui OK and to false if the user pressed \gui Cancel. The
 
257
    dialog's parent is \a parent. The dialog will be modal and uses
 
258
    the widget flags \a f.
 
259
 
 
260
    This function returns the integer which has been entered by the user.
 
261
 
 
262
    Use this static function like this:
 
263
 
 
264
    \quotefromfile dialogs/standarddialogs/dialog.cpp
 
265
    \skipuntil Dialog::setInteger
 
266
    \skipline {
 
267
    \printto }
 
268
 
 
269
    \sa getText(), getDouble(), getItem()
 
270
*/
 
271
 
 
272
int QInputDialog::getInteger(QWidget *parent, const QString &title, const QString &label,
 
273
                             int value, int minValue, int maxValue, int step, bool *ok,
 
274
                             Qt::WFlags f)
 
275
{
 
276
    QInputDialog dlg(label, parent, SpinBox, f);
 
277
    dlg.setObjectName("qt_inputdlg_getint");
 
278
 
 
279
#ifndef QT_NO_WIDGET_TOPEXTRA
 
280
    dlg.setWindowTitle(title);
 
281
#endif
 
282
    QSpinBox *sb = qobject_cast<QSpinBox *>(dlg.d_func()->input);
 
283
    sb->setRange(minValue, maxValue);
 
284
    sb->setSingleStep(step);
 
285
    sb->setValue(value);
 
286
 
 
287
    bool accepted = (dlg.exec() == QDialog::Accepted);
 
288
    if (ok)
 
289
        *ok = accepted;
 
290
    return sb->value();
 
291
}
 
292
 
 
293
/*!
 
294
    Static convenience function to get a floating point number from
 
295
    the user. \a title is the text which is displayed in the title
 
296
    bar of the dialog. \a label is the text which is shown to the user
 
297
    (it should say what should be entered). \a value is the default
 
298
    floating point number that the line edit will be set to. \a
 
299
    minValue and \a maxValue are the minimum and maximum values the
 
300
    user may choose, and \a decimals is the maximum number of decimal
 
301
    places the number may have.
 
302
 
 
303
    If \a ok is non-null, *\a ok will be set to true if the user
 
304
    pressed OK and to false if the user pressed \gui Cancel. The
 
305
    dialog's parent is \a parent. The dialog will be modal and uses
 
306
    the widget flags \a f.
 
307
 
 
308
    This function returns the floating point number which has been
 
309
    entered by the user.
 
310
 
 
311
    Use this static function like this:
 
312
 
 
313
    \quotefromfile dialogs/standarddialogs/dialog.cpp
 
314
    \skipuntil Dialog::setDouble
 
315
    \skipline {
 
316
    \printto }
 
317
 
 
318
    \sa getText(), getInteger(), getItem()
 
319
*/
 
320
 
 
321
double QInputDialog::getDouble( QWidget *parent, const QString &title, const QString &label,
 
322
                                double value, double minValue, double maxValue,
 
323
                                int decimals, bool *ok, Qt::WFlags f)
 
324
{
 
325
    QInputDialog dlg(label, parent, DoubleSpinBox, f);
 
326
    dlg.setObjectName("qt_inputdlg_getdbl");
 
327
#ifndef QT_NO_WIDGET_TOPEXTRA
 
328
    dlg.setWindowTitle(title);
 
329
#endif
 
330
    QDoubleSpinBox *sb = qobject_cast<QDoubleSpinBox *>(dlg.d_func()->input);
 
331
    sb->setRange(minValue, maxValue);
 
332
    sb->setDecimals(decimals);
 
333
    sb->setValue(value);
 
334
 
 
335
    bool accepted = (dlg.exec() == QDialog::Accepted);
 
336
    if (ok)
 
337
        *ok = accepted;
 
338
    return sb->value();
 
339
}
 
340
 
 
341
/*!
 
342
    Static convenience function to let the user select an item from a
 
343
    string list. \a title is the text which is displayed in the title
 
344
    bar of the dialog. \a label is the text which is shown to the user (it
 
345
    should say what should be entered). \a list is the
 
346
    string list which is inserted into the combobox, and \a current is the number
 
347
    of the item which should be the current item. If \a editable is true
 
348
    the user can enter their own text; if \a editable is false the user
 
349
    may only select one of the existing items.
 
350
 
 
351
    If \a ok is non-null \e *\a ok will be set to true if the user
 
352
    pressed OK and to false if the user pressed \gui Cancel. The
 
353
    dialog's parent is \a parent. The dialog will be modal and uses
 
354
    the widget flags \a f.
 
355
 
 
356
    This function returns the text of the current item, or if \a
 
357
    editable is true, the current text of the combobox.
 
358
 
 
359
    Use this static function like this:
 
360
 
 
361
    \quotefromfile dialogs/standarddialogs/dialog.cpp
 
362
    \skipuntil Dialog::setItem
 
363
    \skipline {
 
364
    \printto }
 
365
 
 
366
    \sa getText(), getInteger(), getDouble()
 
367
*/
 
368
 
 
369
QString QInputDialog::getItem(QWidget *parent, const QString &title, const QString &label, const QStringList &list,
 
370
                              int current, bool editable, bool *ok, Qt::WFlags f)
 
371
{
 
372
    QInputDialog dlg(label, parent, editable ? EditableComboBox : ComboBox, f);
 
373
    dlg.setObjectName("qt_inputdlg_getitem");
 
374
#ifndef QT_NO_WIDGET_TOPEXTRA
 
375
    dlg.setWindowTitle(title);
 
376
#endif
 
377
 
 
378
    QComboBox *combo = qobject_cast<QComboBox *>(dlg.d_func()->input);
 
379
    combo->addItems(list);
 
380
    combo->setCurrentIndex(current);
 
381
 
 
382
    bool accepted = (dlg.exec() == QDialog::Accepted);
 
383
    if (ok)
 
384
        *ok = accepted;
 
385
    return combo->currentText();
 
386
}
 
387
 
 
388
/*!
 
389
    \fn QString QInputDialog::getText(const QString &title, const QString &label,
 
390
                                      QLineEdit::EchoMode echo = QLineEdit::Normal,
 
391
                                      const QString &text = QString(), bool *ok = 0,
 
392
                                      QWidget *parent = 0, const char *name = 0, Qt::WFlags f = 0)
 
393
 
 
394
    Call getText(\a parent, \a title, \a label, \a echo, \a text, \a
 
395
    ok, \a f) instead.
 
396
 
 
397
    The \a name parameter is ignored.
 
398
*/
 
399
 
 
400
/*!
 
401
    \fn int QInputDialog::getInteger(const QString &title, const QString &label, int value = 0,
 
402
                                     int minValue = -2147483647, int maxValue = 2147483647,
 
403
                                     int step = 1, bool *ok = 0,
 
404
                                     QWidget *parent = 0, const char *name = 0, Qt::WFlags f = 0)
 
405
 
 
406
 
 
407
    Call getInteger(\a parent, \a title, \a label, \a value, \a
 
408
    minValue, \a maxValue, \a step, \a ok, \a f) instead.
 
409
 
 
410
    The \a name parameter is ignored.
 
411
*/
 
412
 
 
413
/*!
 
414
    \fn double QInputDialog::getDouble(const QString &title, const QString &label, double value = 0,
 
415
                                       double minValue = -2147483647, double maxValue = 2147483647,
 
416
                                       int decimals = 1, bool *ok = 0,
 
417
                                       QWidget *parent = 0, const char *name = 0, Qt::WFlags f = 0)
 
418
 
 
419
    Call getDouble(\a parent, \a title, \a label, \a value, \a
 
420
    minValue, \a maxValue, \a decimals, \a ok, \a f).
 
421
 
 
422
    The \a name parameter is ignored.
 
423
*/
 
424
 
 
425
/*!
 
426
    \fn QString QInputDialog::getItem(const QString &title, const QString &label, const QStringList &list,
 
427
                                      int current = 0, bool editable = true, bool *ok = 0,
 
428
                                      QWidget *parent = 0, const char *name = 0, Qt::WFlags f = 0)
 
429
 
 
430
    Call getItem(\a parent, \a title, \a label, \a list, \a current,
 
431
    \a editable, \a ok, \a f) instead.
 
432
 
 
433
    The \a name parameter is ignored.
 
434
*/
 
435
 
 
436
#endif