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

Last change on this file since 182 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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