source: trunk/src/gui/dialogs/qinputdialog.cpp@ 1056

Last change on this file since 1056 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 40.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qinputdialog.h"
43
44#ifndef QT_NO_INPUTDIALOG
45
46#include "qapplication.h"
47#include "qcombobox.h"
48#include "qdialogbuttonbox.h"
49#include "qlabel.h"
50#include "qlayout.h"
51#include "qlineedit.h"
52#include "qlistwidget.h"
53#include "qpushbutton.h"
54#include "qspinbox.h"
55#include "qstackedlayout.h"
56#include "qvalidator.h"
57#include "qevent.h"
58#include "qdialog_p.h"
59
60QT_USE_NAMESPACE
61
62static const char *signalForMember(const char *member)
63{
64 static const int NumCandidates = 4;
65 static const char * const candidateSignals[NumCandidates] = {
66 SIGNAL(textValueSelected(QString)),
67 SIGNAL(intValueSelected(int)),
68 SIGNAL(doubleValueSelected(double)),
69 SIGNAL(accepted())
70 };
71
72 QByteArray normalizedMember(QMetaObject::normalizedSignature(member));
73
74 int i = 0;
75 while (i < NumCandidates - 1) { // sic
76 if (QMetaObject::checkConnectArgs(candidateSignals[i], normalizedMember))
77 break;
78 ++i;
79 }
80 return candidateSignals[i];
81}
82
83QT_BEGIN_NAMESPACE
84
85/*
86 These internal classes add extra validation to QSpinBox and QDoubleSpinBox by emitting
87 textChanged(bool) after events that may potentially change the visible text. Return or
88 Enter key presses are not propagated if the visible text is invalid. Instead, the visible
89 text is modified to the last valid value.
90*/
91class QInputDialogSpinBox : public QSpinBox
92{
93 Q_OBJECT
94
95public:
96 QInputDialogSpinBox(QWidget *parent)
97 : QSpinBox(parent) {
98 connect(lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(notifyTextChanged()));
99 connect(this, SIGNAL(editingFinished()), this, SLOT(notifyTextChanged()));
100 }
101
102signals:
103 void textChanged(bool);
104
105private slots:
106 void notifyTextChanged() { emit textChanged(hasAcceptableInput()); }
107
108private:
109 void keyPressEvent(QKeyEvent *event) {
110 if ((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && !hasAcceptableInput()) {
111#ifndef QT_NO_PROPERTIES
112 setProperty("value", property("value"));
113#endif
114 } else {
115 QSpinBox::keyPressEvent(event);
116 }
117 notifyTextChanged();
118 }
119
120 void mousePressEvent(QMouseEvent *event) {
121 QSpinBox::mousePressEvent(event);
122 notifyTextChanged();
123 }
124};
125
126class QInputDialogDoubleSpinBox : public QDoubleSpinBox
127{
128 Q_OBJECT
129
130public:
131 QInputDialogDoubleSpinBox(QWidget *parent = 0)
132 : QDoubleSpinBox(parent) {
133 connect(lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(notifyTextChanged()));
134 connect(this, SIGNAL(editingFinished()), this, SLOT(notifyTextChanged()));
135 }
136
137signals:
138 void textChanged(bool);
139
140private slots:
141 void notifyTextChanged() { emit textChanged(hasAcceptableInput()); }
142
143private:
144 void keyPressEvent(QKeyEvent *event) {
145 if ((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && !hasAcceptableInput()) {
146#ifndef QT_NO_PROPERTIES
147 setProperty("value", property("value"));
148#endif
149 } else {
150 QDoubleSpinBox::keyPressEvent(event);
151 }
152 notifyTextChanged();
153 }
154
155 void mousePressEvent(QMouseEvent *event) {
156 QDoubleSpinBox::mousePressEvent(event);
157 notifyTextChanged();
158 }
159};
160
161QT_BEGIN_INCLUDE_NAMESPACE
162#include "qinputdialog.moc"
163QT_END_INCLUDE_NAMESPACE
164
165class QInputDialogPrivate : public QDialogPrivate
166{
167 Q_DECLARE_PUBLIC(QInputDialog)
168
169public:
170 QInputDialogPrivate();
171
172 void ensureLayout();
173 void ensureLineEdit();
174 void ensureComboBox();
175 void ensureListView();
176 void ensureIntSpinBox();
177 void ensureDoubleSpinBox();
178 void ensureEnabledConnection(QAbstractSpinBox *spinBox);
179 void setInputWidget(QWidget *widget);
180 void chooseRightTextInputWidget();
181 void setComboBoxText(const QString &text);
182 void setListViewText(const QString &text);
183 QString listViewText() const;
184 void ensureLayout() const { const_cast<QInputDialogPrivate *>(this)->ensureLayout(); }
185 bool useComboBoxOrListView() const { return comboBox && comboBox->count() > 0; }
186 void _q_textChanged(const QString &text);
187 void _q_currentRowChanged(const QModelIndex &newIndex, const QModelIndex &oldIndex);
188
189 mutable QLabel *label;
190 mutable QDialogButtonBox *buttonBox;
191 mutable QLineEdit *lineEdit;
192 mutable QSpinBox *intSpinBox;
193 mutable QDoubleSpinBox *doubleSpinBox;
194 mutable QComboBox *comboBox;
195 mutable QListView *listView;
196 mutable QWidget *inputWidget;
197 mutable QVBoxLayout *mainLayout;
198 QInputDialog::InputDialogOptions opts;
199 QString textValue;
200 QPointer<QObject> receiverToDisconnectOnClose;
201 QByteArray memberToDisconnectOnClose;
202};
203
204QInputDialogPrivate::QInputDialogPrivate()
205 : label(0), buttonBox(0), lineEdit(0), intSpinBox(0), doubleSpinBox(0), comboBox(0), listView(0),
206 inputWidget(0), mainLayout(0)
207{
208}
209
210void QInputDialogPrivate::ensureLayout()
211{
212 Q_Q(QInputDialog);
213
214 if (mainLayout)
215 return;
216
217 if (!inputWidget) {
218 ensureLineEdit();
219 inputWidget = lineEdit;
220 }
221
222 if (!label)
223 label = new QLabel(QInputDialog::tr("Enter a value:"), q);
224#ifndef QT_NO_SHORTCUT
225 label->setBuddy(inputWidget);
226#endif
227 label->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
228
229 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, q);
230 QObject::connect(buttonBox, SIGNAL(accepted()), q, SLOT(accept()));
231 QObject::connect(buttonBox, SIGNAL(rejected()), q, SLOT(reject()));
232
233 mainLayout = new QVBoxLayout(q);
234 //we want to let the input dialog grow to available size on Symbian.
235#ifndef Q_OS_SYMBIAN
236 mainLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
237#else
238 label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
239#endif
240 mainLayout->addWidget(label);
241 mainLayout->addWidget(inputWidget);
242 mainLayout->addWidget(buttonBox);
243 ensureEnabledConnection(qobject_cast<QAbstractSpinBox *>(inputWidget));
244 inputWidget->show();
245}
246
247void QInputDialogPrivate::ensureLineEdit()
248{
249 Q_Q(QInputDialog);
250 if (!lineEdit) {
251 lineEdit = new QLineEdit(q);
252#ifndef QT_NO_IM
253 qt_widget_private(lineEdit)->inheritsInputMethodHints = 1;
254#endif
255 lineEdit->hide();
256 QObject::connect(lineEdit, SIGNAL(textChanged(QString)),
257 q, SLOT(_q_textChanged(QString)));
258 }
259}
260
261void QInputDialogPrivate::ensureComboBox()
262{
263 Q_Q(QInputDialog);
264 if (!comboBox) {
265 comboBox = new QComboBox(q);
266#ifndef QT_NO_IM
267 qt_widget_private(comboBox)->inheritsInputMethodHints = 1;
268#endif
269 comboBox->hide();
270 QObject::connect(comboBox, SIGNAL(editTextChanged(QString)),
271 q, SLOT(_q_textChanged(QString)));
272 QObject::connect(comboBox, SIGNAL(currentIndexChanged(QString)),
273 q, SLOT(_q_textChanged(QString)));
274 }
275}
276
277void QInputDialogPrivate::ensureListView()
278{
279 Q_Q(QInputDialog);
280 if (!listView) {
281 ensureComboBox();
282
283 listView = new QListView(q);
284 listView->hide();
285 listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
286 listView->setSelectionMode(QAbstractItemView::SingleSelection);
287 listView->setModel(comboBox->model());
288 listView->setCurrentIndex(QModelIndex()); // ###
289 QObject::connect(listView->selectionModel(),
290 SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
291 q, SLOT(_q_currentRowChanged(QModelIndex,QModelIndex)));
292 }
293}
294
295void QInputDialogPrivate::ensureIntSpinBox()
296{
297 Q_Q(QInputDialog);
298 if (!intSpinBox) {
299 intSpinBox = new QInputDialogSpinBox(q);
300 intSpinBox->hide();
301 QObject::connect(intSpinBox, SIGNAL(valueChanged(int)),
302 q, SIGNAL(intValueChanged(int)));
303 }
304}
305
306void QInputDialogPrivate::ensureDoubleSpinBox()
307{
308 Q_Q(QInputDialog);
309 if (!doubleSpinBox) {
310 doubleSpinBox = new QInputDialogDoubleSpinBox(q);
311 doubleSpinBox->hide();
312 QObject::connect(doubleSpinBox, SIGNAL(valueChanged(double)),
313 q, SIGNAL(doubleValueChanged(double)));
314 }
315}
316
317void QInputDialogPrivate::ensureEnabledConnection(QAbstractSpinBox *spinBox)
318{
319 if (spinBox) {
320 QAbstractButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
321 QObject::connect(spinBox, SIGNAL(textChanged(bool)), okButton, SLOT(setEnabled(bool)), Qt::UniqueConnection);
322 }
323}
324
325void QInputDialogPrivate::setInputWidget(QWidget *widget)
326{
327 Q_ASSERT(widget);
328 if (inputWidget == widget)
329 return;
330
331 if (mainLayout) {
332 Q_ASSERT(inputWidget);
333 mainLayout->removeWidget(inputWidget);
334 inputWidget->hide();
335 mainLayout->insertWidget(1, widget);
336 widget->show();
337
338 // disconnect old input widget
339 QAbstractButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
340 if (QAbstractSpinBox *spinBox = qobject_cast<QAbstractSpinBox *>(inputWidget))
341 QObject::disconnect(spinBox, SIGNAL(textChanged(bool)), okButton, SLOT(setEnabled(bool)));
342
343 // connect new input widget and update enabled state of OK button
344 QAbstractSpinBox *spinBox = qobject_cast<QAbstractSpinBox *>(widget);
345 ensureEnabledConnection(spinBox);
346 okButton->setEnabled(!spinBox || spinBox->hasAcceptableInput());
347 }
348
349 inputWidget = widget;
350
351 // synchronize the text shown in the new text editor with the current
352 // textValue
353 if (widget == lineEdit) {
354 lineEdit->setText(textValue);
355 } else if (widget == comboBox) {
356 setComboBoxText(textValue);
357 } else if (widget == listView) {
358 setListViewText(textValue);
359 ensureLayout();
360 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(listView->selectionModel()->hasSelection());
361 }
362}
363
364void QInputDialogPrivate::chooseRightTextInputWidget()
365{
366 QWidget *widget;
367
368 if (useComboBoxOrListView()) {
369 if ((opts & QInputDialog::UseListViewForComboBoxItems) && !comboBox->isEditable()) {
370 ensureListView();
371 widget = listView;
372 } else {
373 widget = comboBox;
374 }
375 } else {
376 ensureLineEdit();
377 widget = lineEdit;
378 }
379
380 setInputWidget(widget);
381
382 if (inputWidget == comboBox) {
383 _q_textChanged(comboBox->currentText());
384 } else if (inputWidget == listView) {
385 _q_textChanged(listViewText());
386 }
387}
388
389void QInputDialogPrivate::setComboBoxText(const QString &text)
390{
391 int index = comboBox->findText(text);
392 if (index != -1) {
393 comboBox->setCurrentIndex(index);
394 } else if (comboBox->isEditable()) {
395 comboBox->setEditText(text);
396 }
397}
398
399void QInputDialogPrivate::setListViewText(const QString &text)
400{
401 int row = comboBox->findText(text);
402 if (row != -1) {
403 QModelIndex index(comboBox->model()->index(row, 0));
404 listView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Clear
405 | QItemSelectionModel::SelectCurrent);
406 }
407}
408
409QString QInputDialogPrivate::listViewText() const
410{
411 if (listView->selectionModel()->hasSelection()) {
412 int row = listView->selectionModel()->selectedRows().value(0).row();
413 return comboBox->itemText(row);
414 } else {
415 return QString();
416 }
417}
418
419void QInputDialogPrivate::_q_textChanged(const QString &text)
420{
421 Q_Q(QInputDialog);
422 if (textValue != text) {
423 textValue = text;
424 emit q->textValueChanged(text);
425 }
426}
427
428void QInputDialogPrivate::_q_currentRowChanged(const QModelIndex &newIndex,
429 const QModelIndex & /* oldIndex */)
430{
431 _q_textChanged(comboBox->model()->data(newIndex).toString());
432 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
433}
434
435/*!
436 \class QInputDialog
437 \brief The QInputDialog class provides a simple convenience dialog to get a
438 single value from the user.
439 \ingroup standard-dialogs
440
441
442 The input value can be a string, a number or an item from a list. A label
443 must be set to tell the user what they should enter.
444
445 Four static convenience functions are provided: getText(), getInt(),
446 getDouble(), and getItem(). All the functions can be used in a similar way,
447 for example:
448
449 \snippet examples/dialogs/standarddialogs/dialog.cpp 3
450
451 The \c ok variable is set to true if the user clicks \gui OK; otherwise it
452 is set to false.
453
454 \img inputdialogs.png Input Dialogs
455
456 The \l{dialogs/standarddialogs}{Standard Dialogs} example shows how to use
457 QInputDialog as well as other built-in Qt dialogs.
458
459 \sa QMessageBox, {Standard Dialogs Example}
460*/
461
462/*!
463 \enum QInputDialog::InputMode
464 \since 4.5
465
466 This enum describes the different modes of input that can be selected for
467 the dialog.
468
469 \value TextInput Used to input text strings.
470 \value IntInput Used to input integers.
471 \value DoubleInput Used to input floating point numbers with double
472 precision accuracy.
473
474 \sa inputMode
475*/
476
477/*!
478 \since 4.5
479
480 Constructs a new input dialog with the given \a parent and window \a flags.
481*/
482QInputDialog::QInputDialog(QWidget *parent, Qt::WindowFlags flags)
483 : QDialog(*new QInputDialogPrivate, parent, flags)
484{
485}
486
487/*!
488 \since 4.5
489
490 Destroys the input dialog.
491*/
492QInputDialog::~QInputDialog()
493{
494}
495
496/*!
497 \since 4.5
498
499 \property QInputDialog::inputMode
500
501 \brief the mode used for input
502
503 This property help determines which widget is used for entering input into
504 the dialog.
505*/
506void QInputDialog::setInputMode(InputMode mode)
507{
508 Q_D(QInputDialog);
509
510 QWidget *widget;
511
512 /*
513 Warning: Some functions in QInputDialog rely on implementation details
514 of the code below. Look for the comments that accompany the calls to
515 setInputMode() throughout this file before you change the code below.
516 */
517
518 switch (mode) {
519 case IntInput:
520 d->ensureIntSpinBox();
521 widget = d->intSpinBox;
522 break;
523 case DoubleInput:
524 d->ensureDoubleSpinBox();
525 widget = d->doubleSpinBox;
526 break;
527 default:
528 Q_ASSERT(mode == TextInput);
529 d->chooseRightTextInputWidget();
530 return;
531 }
532
533 d->setInputWidget(widget);
534}
535
536QInputDialog::InputMode QInputDialog::inputMode() const
537{
538 Q_D(const QInputDialog);
539
540 if (d->inputWidget) {
541 if (d->inputWidget == d->intSpinBox) {
542 return IntInput;
543 } else if (d->inputWidget == d->doubleSpinBox) {
544 return DoubleInput;
545 }
546 }
547
548 return TextInput;
549}
550
551/*!
552 \since 4.5
553
554 \property QInputDialog::labelText
555
556 \brief the text to for the label to describe what needs to be input
557*/
558void QInputDialog::setLabelText(const QString &text)
559{
560 Q_D(QInputDialog);
561 if (!d->label) {
562 d->label = new QLabel(text, this);
563 } else {
564 d->label->setText(text);
565 }
566#ifdef Q_OS_SYMBIAN
567 d->label->setWordWrap(true);
568#endif
569}
570
571QString QInputDialog::labelText() const
572{
573 Q_D(const QInputDialog);
574 d->ensureLayout();
575 return d->label->text();
576}
577
578/*!
579 \enum QInputDialog::InputDialogOption
580
581 \since 4.5
582
583 This enum specifies various options that affect the look and feel
584 of an input dialog.
585
586 \value NoButtons Don't display \gui{OK} and \gui{Cancel} buttons. (Useful for "live dialogs".)
587 \value UseListViewForComboBoxItems Use a QListView rather than a non-editable QComboBox for
588 displaying the items set with setComboBoxItems().
589
590 \sa options, setOption(), testOption()
591*/
592
593/*!
594 Sets the given \a option to be enabled if \a on is true;
595 otherwise, clears the given \a option.
596
597 \sa options, testOption()
598*/
599void QInputDialog::setOption(InputDialogOption option, bool on)
600{
601 Q_D(QInputDialog);
602 if (!(d->opts & option) != !on)
603 setOptions(d->opts ^ option);
604}
605
606/*!
607 Returns true if the given \a option is enabled; otherwise, returns
608 false.
609
610 \sa options, setOption()
611*/
612bool QInputDialog::testOption(InputDialogOption option) const
613{
614 Q_D(const QInputDialog);
615 return (d->opts & option) != 0;
616}
617
618/*!
619 \property QInputDialog::options
620 \brief the various options that affect the look and feel of the dialog
621 \since 4.5
622
623 By default, all options are disabled.
624
625 \sa setOption(), testOption()
626*/
627void QInputDialog::setOptions(InputDialogOptions options)
628{
629 Q_D(QInputDialog);
630
631 InputDialogOptions changed = (options ^ d->opts);
632 if (!changed)
633 return;
634
635 d->opts = options;
636 d->ensureLayout();
637
638 if (changed & NoButtons)
639 d->buttonBox->setVisible(!(options & NoButtons));
640 if ((changed & UseListViewForComboBoxItems) && inputMode() == TextInput)
641 d->chooseRightTextInputWidget();
642}
643
644QInputDialog::InputDialogOptions QInputDialog::options() const
645{
646 Q_D(const QInputDialog);
647 return d->opts;
648}
649
650/*!
651 \since 4.5
652
653 \property QInputDialog::textValue
654
655 \brief the text value for the input dialog
656
657 This property is only relevant when the input dialog is used in
658 TextInput mode.
659*/
660void QInputDialog::setTextValue(const QString &text)
661{
662 Q_D(QInputDialog);
663
664 setInputMode(TextInput);
665 if (d->inputWidget == d->lineEdit) {
666 d->lineEdit->setText(text);
667 } else if (d->inputWidget == d->comboBox) {
668 d->setComboBoxText(text);
669 } else {
670 d->setListViewText(text);
671 }
672}
673
674QString QInputDialog::textValue() const
675{
676 Q_D(const QInputDialog);
677 return d->textValue;
678}
679
680/*!
681 \since 4.5
682
683 \property QInputDialog::textEchoMode
684
685 \brief the echo mode for the text value
686
687 This property is only relevant when the input dialog is used in
688 TextInput mode.
689*/
690void QInputDialog::setTextEchoMode(QLineEdit::EchoMode mode)
691{
692 Q_D(QInputDialog);
693 d->ensureLineEdit();
694 d->lineEdit->setEchoMode(mode);
695}
696
697QLineEdit::EchoMode QInputDialog::textEchoMode() const
698{
699 Q_D(const QInputDialog);
700 if (d->lineEdit) {
701 return d->lineEdit->echoMode();
702 } else {
703 return QLineEdit::Normal;
704 }
705}
706
707/*!
708 \since 4.5
709
710 \property QInputDialog::comboBoxEditable
711
712 \brief whether or not the combo box is used in the input dialog is editable
713*/
714void QInputDialog::setComboBoxEditable(bool editable)
715{
716 Q_D(QInputDialog);
717 d->ensureComboBox();
718 d->comboBox->setEditable(editable);
719 if (inputMode() == TextInput)
720 d->chooseRightTextInputWidget();
721}
722
723bool QInputDialog::isComboBoxEditable() const
724{
725 Q_D(const QInputDialog);
726 if (d->comboBox) {
727 return d->comboBox->isEditable();
728 } else {
729 return false;
730 }
731}
732
733/*!
734 \since 4.5
735
736 \property QInputDialog::comboBoxItems
737
738 \brief the items used in the combobox for the input dialog
739*/
740void QInputDialog::setComboBoxItems(const QStringList &items)
741{
742 Q_D(QInputDialog);
743
744 d->ensureComboBox();
745 d->comboBox->blockSignals(true);
746 d->comboBox->clear();
747 d->comboBox->addItems(items);
748 d->comboBox->blockSignals(false);
749
750 if (inputMode() == TextInput)
751 d->chooseRightTextInputWidget();
752}
753
754QStringList QInputDialog::comboBoxItems() const
755{
756 Q_D(const QInputDialog);
757 QStringList result;
758 if (d->comboBox) {
759 const int count = d->comboBox->count();
760 for (int i = 0; i < count; ++i)
761 result.append(d->comboBox->itemText(i));
762 }
763 return result;
764}
765
766/*!
767 \property QInputDialog::intValue
768 \since 4.5
769 \brief the current integer value accepted as input
770
771 This property is only relevant when the input dialog is used in
772 IntInput mode.
773*/
774void QInputDialog::setIntValue(int value)
775{
776 Q_D(QInputDialog);
777 setInputMode(IntInput);
778 d->intSpinBox->setValue(value);
779}
780
781int QInputDialog::intValue() const
782{
783 Q_D(const QInputDialog);
784 if (d->intSpinBox) {
785 return d->intSpinBox->value();
786 } else {
787 return 0;
788 }
789}
790
791/*!
792 \property QInputDialog::intMinimum
793 \since 4.5
794 \brief the minimum integer value accepted as input
795
796 This property is only relevant when the input dialog is used in
797 IntInput mode.
798*/
799void QInputDialog::setIntMinimum(int min)
800{
801 Q_D(QInputDialog);
802 d->ensureIntSpinBox();
803 d->intSpinBox->setMinimum(min);
804}
805
806int QInputDialog::intMinimum() const
807{
808 Q_D(const QInputDialog);
809 if (d->intSpinBox) {
810 return d->intSpinBox->minimum();
811 } else {
812 return 0;
813 }
814}
815
816/*!
817 \property QInputDialog::intMaximum
818 \since 4.5
819 \brief the maximum integer value accepted as input
820
821 This property is only relevant when the input dialog is used in
822 IntInput mode.
823*/
824void QInputDialog::setIntMaximum(int max)
825{
826 Q_D(QInputDialog);
827 d->ensureIntSpinBox();
828 d->intSpinBox->setMaximum(max);
829}
830
831int QInputDialog::intMaximum() const
832{
833 Q_D(const QInputDialog);
834 if (d->intSpinBox) {
835 return d->intSpinBox->maximum();
836 } else {
837 return 99;
838 }
839}
840
841/*!
842 Sets the range of integer values accepted by the dialog when used in
843 IntInput mode, with minimum and maximum values specified by \a min and
844 \a max respectively.
845*/
846void QInputDialog::setIntRange(int min, int max)
847{
848 Q_D(QInputDialog);
849 d->ensureIntSpinBox();
850 d->intSpinBox->setRange(min, max);
851}
852
853/*!
854 \property QInputDialog::intStep
855 \since 4.5
856 \brief the step by which the integer value is increased and decreased
857
858 This property is only relevant when the input dialog is used in
859 IntInput mode.
860*/
861void QInputDialog::setIntStep(int step)
862{
863 Q_D(QInputDialog);
864 d->ensureIntSpinBox();
865 d->intSpinBox->setSingleStep(step);
866}
867
868int QInputDialog::intStep() const
869{
870 Q_D(const QInputDialog);
871 if (d->intSpinBox) {
872 return d->intSpinBox->singleStep();
873 } else {
874 return 1;
875 }
876}
877
878/*!
879 \property QInputDialog::doubleValue
880 \since 4.5
881 \brief the current double precision floating point value accepted as input
882
883 This property is only relevant when the input dialog is used in
884 DoubleInput mode.
885*/
886void QInputDialog::setDoubleValue(double value)
887{
888 Q_D(QInputDialog);
889 setInputMode(DoubleInput);
890 d->doubleSpinBox->setValue(value);
891}
892
893double QInputDialog::doubleValue() const
894{
895 Q_D(const QInputDialog);
896 if (d->doubleSpinBox) {
897 return d->doubleSpinBox->value();
898 } else {
899 return 0.0;
900 }
901}
902
903/*!
904 \property QInputDialog::doubleMinimum
905 \since 4.5
906 \brief the minimum double precision floating point value accepted as input
907
908 This property is only relevant when the input dialog is used in
909 DoubleInput mode.
910*/
911void QInputDialog::setDoubleMinimum(double min)
912{
913 Q_D(QInputDialog);
914 d->ensureDoubleSpinBox();
915 d->doubleSpinBox->setMinimum(min);
916}
917
918double QInputDialog::doubleMinimum() const
919{
920 Q_D(const QInputDialog);
921 if (d->doubleSpinBox) {
922 return d->doubleSpinBox->minimum();
923 } else {
924 return 0.0;
925 }
926}
927
928/*!
929 \property QInputDialog::doubleMaximum
930 \since 4.5
931 \brief the maximum double precision floating point value accepted as input
932
933 This property is only relevant when the input dialog is used in
934 DoubleInput mode.
935*/
936void QInputDialog::setDoubleMaximum(double max)
937{
938 Q_D(QInputDialog);
939 d->ensureDoubleSpinBox();
940 d->doubleSpinBox->setMaximum(max);
941}
942
943double QInputDialog::doubleMaximum() const
944{
945 Q_D(const QInputDialog);
946 if (d->doubleSpinBox) {
947 return d->doubleSpinBox->maximum();
948 } else {
949 return 99.99;
950 }
951}
952
953/*!
954 Sets the range of double precision floating point values accepted by the
955 dialog when used in DoubleInput mode, with minimum and maximum values
956 specified by \a min and \a max respectively.
957*/
958void QInputDialog::setDoubleRange(double min, double max)
959{
960 Q_D(QInputDialog);
961 d->ensureDoubleSpinBox();
962 d->doubleSpinBox->setRange(min, max);
963}
964
965/*!
966 \since 4.5
967
968 \property QInputDialog::doubleDecimals
969
970 \brief sets the percision of the double spinbox in decimals
971
972 \sa QDoubleSpinBox::setDecimals()
973*/
974void QInputDialog::setDoubleDecimals(int decimals)
975{
976 Q_D(QInputDialog);
977 d->ensureDoubleSpinBox();
978 d->doubleSpinBox->setDecimals(decimals);
979}
980
981int QInputDialog::doubleDecimals() const
982{
983 Q_D(const QInputDialog);
984 if (d->doubleSpinBox) {
985 return d->doubleSpinBox->decimals();
986 } else {
987 return 2;
988 }
989}
990
991/*!
992 \since 4.5
993
994 \property QInputDialog::okButtonText
995
996 \brief the text for the button used to accept the entry in the dialog
997*/
998void QInputDialog::setOkButtonText(const QString &text)
999{
1000 Q_D(const QInputDialog);
1001 d->ensureLayout();
1002 d->buttonBox->button(QDialogButtonBox::Ok)->setText(text);
1003}
1004
1005QString QInputDialog::okButtonText() const
1006{
1007 Q_D(const QInputDialog);
1008 d->ensureLayout();
1009 return d->buttonBox->button(QDialogButtonBox::Ok)->text();
1010}
1011
1012/*!
1013 \since 4.5
1014
1015 \property QInputDialog::cancelButtonText
1016 \brief the text for the button used to cancel the dialog
1017*/
1018void QInputDialog::setCancelButtonText(const QString &text)
1019{
1020 Q_D(const QInputDialog);
1021 d->ensureLayout();
1022 d->buttonBox->button(QDialogButtonBox::Cancel)->setText(text);
1023}
1024
1025QString QInputDialog::cancelButtonText() const
1026{
1027 Q_D(const QInputDialog);
1028 d->ensureLayout();
1029 return d->buttonBox->button(QDialogButtonBox::Cancel)->text();
1030}
1031
1032/*!
1033 \since 4.5
1034 \overload
1035
1036 This function connects one of its signals to the slot specified by \a receiver
1037 and \a member. The specific signal depends on the arguments that are specified
1038 in \a member. These are:
1039
1040 \list
1041 \o textValueSelected() if \a member has a QString for its first argument.
1042 \o intValueSelected() if \a member has an int for its first argument.
1043 \o doubleValueSelected() if \a member has a double for its first argument.
1044 \o accepted() if \a member has NO arguments.
1045 \endlist
1046
1047 The signal will be disconnected from the slot when the dialog is closed.
1048*/
1049void QInputDialog::open(QObject *receiver, const char *member)
1050{
1051 Q_D(QInputDialog);
1052 connect(this, signalForMember(member), receiver, member);
1053 d->receiverToDisconnectOnClose = receiver;
1054 d->memberToDisconnectOnClose = member;
1055 QDialog::open();
1056}
1057
1058/*!
1059 \reimp
1060*/
1061QSize QInputDialog::minimumSizeHint() const
1062{
1063 Q_D(const QInputDialog);
1064 d->ensureLayout();
1065 return QDialog::minimumSizeHint();
1066}
1067
1068/*!
1069 \reimp
1070*/
1071QSize QInputDialog::sizeHint() const
1072{
1073 Q_D(const QInputDialog);
1074 d->ensureLayout();
1075 return QDialog::sizeHint();
1076}
1077
1078/*!
1079 \reimp
1080*/
1081void QInputDialog::setVisible(bool visible)
1082{
1083 Q_D(const QInputDialog);
1084 if (visible) {
1085 d->ensureLayout();
1086 d->inputWidget->setFocus();
1087 if (d->inputWidget == d->lineEdit) {
1088 d->lineEdit->selectAll();
1089 } else if (d->inputWidget == d->intSpinBox) {
1090 d->intSpinBox->selectAll();
1091 } else if (d->inputWidget == d->doubleSpinBox) {
1092 d->doubleSpinBox->selectAll();
1093 }
1094 }
1095 QDialog::setVisible(visible);
1096}
1097
1098/*!
1099 Closes the dialog and sets its result code to \a result. If this dialog
1100 is shown with exec(), done() causes the local event loop to finish,
1101 and exec() to return \a result.
1102
1103 \sa QDialog::done()
1104*/
1105void QInputDialog::done(int result)
1106{
1107 Q_D(QInputDialog);
1108 QDialog::done(result);
1109 if (result) {
1110 InputMode mode = inputMode();
1111 switch (mode) {
1112 case DoubleInput:
1113 emit doubleValueSelected(doubleValue());
1114 break;
1115 case IntInput:
1116 emit intValueSelected(intValue());
1117 break;
1118 default:
1119 Q_ASSERT(mode == TextInput);
1120 emit textValueSelected(textValue());
1121 }
1122 }
1123 if (d->receiverToDisconnectOnClose) {
1124 disconnect(this, signalForMember(d->memberToDisconnectOnClose),
1125 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
1126 d->receiverToDisconnectOnClose = 0;
1127 }
1128 d->memberToDisconnectOnClose.clear();
1129}
1130
1131/*!
1132 Static convenience function to get a string from the user.
1133
1134 \a title is the text which is displayed in the title bar of the dialog.
1135 \a label is the text which is shown to the user (it should say what should
1136 be entered).
1137 \a text is the default text which is placed in the line edit.
1138 \a mode is the echo mode the line edit will use.
1139
1140 If \a ok is nonnull \e *\a ok will be set to true if the user pressed
1141 \gui OK and to false if the user pressed \gui Cancel. The dialog's parent
1142 is \a parent. The dialog will be modal and uses the specified widget
1143 \a flags.
1144
1145 If the dialog is accepted, this function returns the text in the dialog's
1146 line edit. If the dialog is rejected, a null QString is returned.
1147
1148 Use this static function like this:
1149
1150 \snippet examples/dialogs/standarddialogs/dialog.cpp 3
1151
1152 \warning Do not delete \a parent during the execution of the dialog. If you
1153 want to do this, you should create the dialog yourself using one of the
1154 QInputDialog constructors.
1155
1156 \sa getInt(), getDouble(), getItem()
1157*/
1158
1159QString QInputDialog::getText(QWidget *parent, const QString &title, const QString &label,
1160 QLineEdit::EchoMode mode, const QString &text, bool *ok,
1161 Qt::WindowFlags flags)
1162{
1163 QInputDialog dialog(parent, flags);
1164 dialog.setWindowTitle(title);
1165 dialog.setLabelText(label);
1166 dialog.setTextValue(text);
1167 dialog.setTextEchoMode(mode);
1168
1169 int ret = dialog.exec();
1170 if (ok)
1171 *ok = !!ret;
1172 if (ret) {
1173 return dialog.textValue();
1174 } else {
1175 return QString();
1176 }
1177}
1178
1179/*!
1180 \since 4.5
1181
1182 Static convenience function to get an integer input from the user.
1183
1184 \a title is the text which is displayed in the title bar of the dialog.
1185 \a label is the text which is shown to the user (it should say what should
1186 be entered).
1187 \a value is the default integer which the spinbox will be set to.
1188 \a min and \a max are the minimum and maximum values the user may choose.
1189 \a step is the amount by which the values change as the user presses the
1190 arrow buttons to increment or decrement the value.
1191
1192 If \a ok is nonnull *\a ok will be set to true if the user pressed \gui OK
1193 and to false if the user pressed \gui Cancel. The dialog's parent is
1194 \a parent. The dialog will be modal and uses the widget \a flags.
1195
1196 On success, this function returns the integer which has been entered by the
1197 user; on failure, it returns the initial \a value.
1198
1199 Use this static function like this:
1200
1201 \snippet examples/dialogs/standarddialogs/dialog.cpp 0
1202
1203 \warning Do not delete \a parent during the execution of the dialog. If you
1204 want to do this, you should create the dialog yourself using one of the
1205 QInputDialog constructors.
1206
1207 \sa getText(), getDouble(), getItem()
1208*/
1209
1210int QInputDialog::getInt(QWidget *parent, const QString &title, const QString &label, int value,
1211 int min, int max, int step, bool *ok, Qt::WindowFlags flags)
1212{
1213 QInputDialog dialog(parent, flags);
1214 dialog.setWindowTitle(title);
1215 dialog.setLabelText(label);
1216 dialog.setIntRange(min, max);
1217 dialog.setIntValue(value);
1218 dialog.setIntStep(step);
1219
1220 int ret = dialog.exec();
1221 if (ok)
1222 *ok = !!ret;
1223 if (ret) {
1224 return dialog.intValue();
1225 } else {
1226 return value;
1227 }
1228}
1229
1230/*!
1231 Static convenience function to get a floating point number from the user.
1232
1233 \a title is the text which is displayed in the title bar of the dialog.
1234 \a label is the text which is shown to the user (it should say what should
1235 be entered).
1236 \a value is the default floating point number that the line edit will be
1237 set to.
1238 \a min and \a max are the minimum and maximum values the user may choose.
1239 \a decimals is the maximum number of decimal places the number may have.
1240
1241 If \a ok is nonnull, *\a ok will be set to true if the user pressed \gui OK
1242 and to false if the user pressed \gui Cancel. The dialog's parent is
1243 \a parent. The dialog will be modal and uses the widget \a flags.
1244
1245 This function returns the floating point number which has been entered by
1246 the user.
1247
1248 Use this static function like this:
1249
1250 \snippet examples/dialogs/standarddialogs/dialog.cpp 1
1251
1252 \warning Do not delete \a parent during the execution of the dialog. If you
1253 want to do this, you should create the dialog yourself using one of the
1254 QInputDialog constructors.
1255
1256 \sa getText(), getInt(), getItem()
1257*/
1258
1259double QInputDialog::getDouble(QWidget *parent, const QString &title, const QString &label,
1260 double value, double min, double max, int decimals, bool *ok,
1261 Qt::WindowFlags flags)
1262{
1263 QInputDialog dialog(parent, flags);
1264 dialog.setWindowTitle(title);
1265 dialog.setLabelText(label);
1266 dialog.setDoubleDecimals(decimals);
1267 dialog.setDoubleRange(min, max);
1268 dialog.setDoubleValue(value);
1269
1270 int ret = dialog.exec();
1271 if (ok)
1272 *ok = !!ret;
1273 if (ret) {
1274 return dialog.doubleValue();
1275 } else {
1276 return value;
1277 }
1278}
1279
1280/*!
1281 Static convenience function to let the user select an item from a string
1282 list.
1283
1284 \a title is the text which is displayed in the title bar of the dialog.
1285 \a label is the text which is shown to the user (it should say what should
1286 be entered).
1287 \a items is the string list which is inserted into the combobox.
1288 \a current is the number of the item which should be the current item.
1289
1290 If \a editable is true the user can enter their own text; otherwise the
1291 user may only select one of the existing items.
1292
1293 If \a ok is nonnull \e *\a ok will be set to true if the user pressed
1294 \gui OK and to false if the user pressed \gui Cancel. The dialog's parent
1295 is \a parent. The dialog will be modal and uses the widget \a flags.
1296
1297 This function returns the text of the current item, or if \a editable is
1298 true, the current text of the combobox.
1299
1300 Use this static function like this:
1301
1302 \snippet examples/dialogs/standarddialogs/dialog.cpp 2
1303
1304 \warning Do not delete \a parent during the execution of the dialog. If you
1305 want to do this, you should create the dialog yourself using one of the
1306 QInputDialog constructors.
1307
1308 \sa getText(), getInt(), getDouble()
1309*/
1310
1311QString QInputDialog::getItem(QWidget *parent, const QString &title, const QString &label,
1312 const QStringList &items, int current, bool editable, bool *ok,
1313 Qt::WindowFlags flags)
1314{
1315 QString text(items.value(current));
1316
1317 QInputDialog dialog(parent, flags);
1318 dialog.setWindowTitle(title);
1319 dialog.setLabelText(label);
1320 dialog.setComboBoxItems(items);
1321 dialog.setTextValue(text);
1322 dialog.setComboBoxEditable(editable);
1323
1324 int ret = dialog.exec();
1325 if (ok)
1326 *ok = !!ret;
1327 if (ret) {
1328 return dialog.textValue();
1329 } else {
1330 return text;
1331 }
1332}
1333
1334/*!
1335 \obsolete
1336
1337 Use getInt() instead.
1338*/
1339int QInputDialog::getInteger(QWidget *parent, const QString &title, const QString &label,
1340 int value, int min, int max, int step, bool *ok,
1341 Qt::WindowFlags flags)
1342{
1343 return getInt(parent, title, label, value, min, max, step, ok, flags);
1344}
1345
1346/*!
1347 \fn QString QInputDialog::getText(const QString &title, const QString &label,
1348 QLineEdit::EchoMode echo = QLineEdit::Normal,
1349 const QString &text = QString(), bool *ok = 0,
1350 QWidget *parent = 0, const char *name = 0, Qt::WindowFlags flags = 0)
1351
1352 Call getText(\a parent, \a title, \a label, \a echo, \a text, \a
1353 ok, \a flags) instead.
1354
1355 The \a name parameter is ignored.
1356*/
1357
1358/*!
1359 \fn int QInputDialog::getInteger(const QString &title, const QString &label, int value = 0,
1360 int min = -2147483647, int max = 2147483647,
1361 int step = 1, bool *ok = 0,
1362 QWidget *parent = 0, const char *name = 0, Qt::WindowFlags flags = 0)
1363
1364
1365 Call getInteger(\a parent, \a title, \a label, \a value, \a
1366 min, \a max, \a step, \a ok, \a flags) instead.
1367
1368 The \a name parameter is ignored.
1369*/
1370
1371/*!
1372 \fn double QInputDialog::getDouble(const QString &title, const QString &label, double value = 0,
1373 double min = -2147483647, double max = 2147483647,
1374 int decimals = 1, bool *ok = 0,
1375 QWidget *parent = 0, const char *name = 0, Qt::WindowFlags flags = 0)
1376
1377 Call getDouble(\a parent, \a title, \a label, \a value, \a
1378 min, \a max, \a decimals, \a ok, \a flags).
1379
1380 The \a name parameter is ignored.
1381*/
1382
1383/*!
1384 \fn QString QInputDialog::getItem(const QString &title, const QString &label, const QStringList &list,
1385 int current = 0, bool editable = true, bool *ok = 0,
1386 QWidget *parent = 0, const char *name = 0, Qt::WindowFlags flags = 0)
1387
1388 Call getItem(\a parent, \a title, \a label, \a list, \a current,
1389 \a editable, \a ok, \a flags) instead.
1390
1391 The \a name parameter is ignored.
1392*/
1393
1394/*!
1395 \fn void QInputDialog::doubleValueChanged(double value)
1396
1397 This signal is emitted whenever the double value changes in the dialog.
1398 The current value is specified by \a value.
1399
1400 This signal is only relevant when the input dialog is used in
1401 DoubleInput mode.
1402*/
1403
1404/*!
1405 \fn void QInputDialog::doubleValueSelected(double value)
1406
1407 This signal is emitted whenever the user selects a double value by
1408 accepting the dialog; for example, by clicking the \gui{OK} button.
1409 The selected value is specified by \a value.
1410
1411 This signal is only relevant when the input dialog is used in
1412 DoubleInput mode.
1413*/
1414
1415/*!
1416 \fn void QInputDialog::intValueChanged(int value)
1417
1418 This signal is emitted whenever the integer value changes in the dialog.
1419 The current value is specified by \a value.
1420
1421 This signal is only relevant when the input dialog is used in
1422 IntInput mode.
1423*/
1424
1425/*!
1426 \fn void QInputDialog::intValueSelected(int value)
1427
1428 This signal is emitted whenever the user selects a integer value by
1429 accepting the dialog; for example, by clicking the \gui{OK} button.
1430 The selected value is specified by \a value.
1431
1432 This signal is only relevant when the input dialog is used in
1433 IntInput mode.
1434*/
1435
1436/*!
1437 \fn void QInputDialog::textValueChanged(const QString &text)
1438
1439 This signal is emitted whenever the text string changes in the dialog.
1440 The current string is specified by \a text.
1441
1442 This signal is only relevant when the input dialog is used in
1443 TextInput mode.
1444*/
1445
1446/*!
1447 \fn void QInputDialog::textValueSelected(const QString &text)
1448
1449 This signal is emitted whenever the user selects a text string by
1450 accepting the dialog; for example, by clicking the \gui{OK} button.
1451 The selected string is specified by \a text.
1452
1453 This signal is only relevant when the input dialog is used in
1454 TextInput mode.
1455*/
1456
1457QT_END_NAMESPACE
1458
1459#include "moc_qinputdialog.cpp"
1460
1461#endif // QT_NO_INPUTDIALOG
Note: See TracBrowser for help on using the repository browser.