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

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

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