Debian Codegraph @

kysdk-qtwidgets/src/kinputdialog.cpp

raw
1 /*
2 * libkysdk-qtwidgets's Library
3 *
4 * Copyright (C) 2023, KylinSoft Co., Ltd.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this library. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Authors: Zhen Sun <[email protected]>
20 *
21 */
22
23 #include <QLabel>
24 #include <QLineEdit>
25 #include <QPushButton>
26 #include <QWidget>
27
28 #include "kinputdialog.h"
29 #include "qevent.h"
30 #include "qvalidator.h"
31 #include <QComboBox>
32 #include <QDoubleSpinBox>
33 #include <QListView>
34 #include <QObject>
35 #include <QPlainTextEdit>
36 #include <QPointer>
37 #include <QVBoxLayout>
38 #include "accessinfohelper.h"
39
40 namespace kdk
41 {
42 enum CandidateSignal {
43 TextValueSelectedSignal,
44 IntValueSelectedSignal,
45 DoubleValueSelectedSignal,
46
47 NumCandidateSignals
48 };
49
50 static const char *candidateSignal(int which)
51 {
52 switch (CandidateSignal(which)) {
53 case TextValueSelectedSignal:
54 return SIGNAL(textValueSelected(QString));
55 case IntValueSelectedSignal:
56 return SIGNAL(intValueSelected(int));
57 case DoubleValueSelectedSignal:
58 return SIGNAL(doubleValueSelected(double));
59
60 case NumCandidateSignals:
61 break;
62 };
63 Q_UNREACHABLE();
64 return nullptr;
65 }
66
67 static const char *signalForMember(const char *member)
68 {
69 QByteArray normalizedMember(QMetaObject::normalizedSignature(member));
70
71 for (int i = 0; i < NumCandidateSignals; ++i)
72 if (QMetaObject::checkConnectArgs(candidateSignal(i), normalizedMember))
73 return candidateSignal(i);
74
75 return SIGNAL(accepted());
76 }
77
78 QT_BEGIN_NAMESPACE
79
80 class KInputDialogSpinBox : public QSpinBox
81 {
82 Q_OBJECT
83
84 public:
85 KInputDialogSpinBox(QWidget *parent)
86 : QSpinBox(parent)
87 {
88 connect(lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(notifyTextChanged()));
89 connect(this, SIGNAL(editingFinished()), this, SLOT(notifyTextChanged()));
90 }
91
92 protected:
93 QSize sizeHint() const;
94
95 Q_SIGNALS:
96 void textChanged(int);
97
98 private Q_SLOTS:
99 void notifyTextChanged()
100 {
101 Q_EMIT textChanged(hasAcceptableInput() ? 1 : 0);
102 }
103
104 private:
105 void keyPressEvent(QKeyEvent *event) override
106 {
107 if ((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && !hasAcceptableInput()) {
108 #ifndef QT_NO_PROPERTIES
109 setProperty("value", property("value"));
110 #endif
111 } else {
112 QSpinBox::keyPressEvent(event);
113 }
114 notifyTextChanged();
115 }
116
117 void mousePressEvent(QMouseEvent *event) override
118 {
119 QSpinBox::mousePressEvent(event);
120 notifyTextChanged();
121 }
122 };
123
124 class KInputDialogDoubleSpinBox : public QDoubleSpinBox
125 {
126 Q_OBJECT
127
128 public:
129 KInputDialogDoubleSpinBox(QWidget *parent = nullptr)
130 : QDoubleSpinBox(parent)
131 {
132 connect(lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(notifyTextChanged()));
133 connect(this, SIGNAL(editingFinished()), this, SLOT(notifyTextChanged()));
134 }
135
136 protected:
137 QSize sizeHint() const;
138
139 Q_SIGNALS:
140 void textChanged(int);
141
142 private Q_SLOTS:
143 void notifyTextChanged()
144 {
145 Q_EMIT textChanged(hasAcceptableInput() ? 1 : 0);
146 }
147
148 private:
149 void keyPressEvent(QKeyEvent *event) override
150 {
151 if ((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && !hasAcceptableInput()) {
152 #ifndef QT_NO_PROPERTIES
153 setProperty("value", property("value"));
154 #endif
155 } else {
156 QDoubleSpinBox::keyPressEvent(event);
157 }
158 notifyTextChanged();
159 }
160
161 void mousePressEvent(QMouseEvent *event) override
162 {
163 QDoubleSpinBox::mousePressEvent(event);
164 notifyTextChanged();
165 }
166 };
167
168 class KInputDialogLineEdit : public QLineEdit
169 {
170 public:
171 KInputDialogLineEdit(QWidget *parent = nullptr)
172 : QLineEdit(parent)
173 {
174 }
175
176 protected:
177 QSize sizeHint() const;
178 };
179
180 class KInputDialogListView : public QListView
181 {
182 public:
183 KInputDialogListView(QWidget *parent = nullptr)
184 : QListView(parent)
185 {
186 }
187 QVariant inputMethodQuery(Qt::InputMethodQuery query) const override
188 {
189 if (query == Qt::ImEnabled)
190 return false;
191 return QListView::inputMethodQuery(query);
192 }
193 };
194
195 class KInputDialogPrivate : public QObject
196 {
197 Q_DECLARE_PUBLIC(KInputDialog)
198
199 public:
200 KInputDialogPrivate(KInputDialog *parent);
201
202 void ensureLayout();
203 void ensureLineEdit();
204 void ensurePlainTextEdit();
205 void ensureComboBox();
206 void ensureListView();
207 void ensureIntSpinBox();
208 void ensureDoubleSpinBox();
209 void ensureEnabledConnection(QAbstractSpinBox *spinBox);
210 void setInputWidget(QWidget *widget);
211 void chooseRightTextInputWidget();
212 void setComboBoxText(const QString &text);
213 void setListViewText(const QString &text);
214 QString listViewText() const;
215 void ensureLayout() const
216 {
217 const_cast<KInputDialogPrivate *>(this)->ensureLayout();
218 }
219 bool useComboBoxOrListView() const
220 {
221 return comboBox && comboBox->count() > 0;
222 }
223 void _q_textChanged(const QString &text);
224 void _q_plainTextEditTextChanged();
225 void _q_currentRowChanged(const QModelIndex &newIndex, const QModelIndex &oldIndex);
226 void setAccessInfo();
227
228 mutable QLabel *label;
229 mutable QPushButton *okButton;
230 mutable QPushButton *cancelButton;
231 mutable KInputDialogLineEdit *lineEdit;
232 mutable QPlainTextEdit *plainTextEdit;
233 mutable QSpinBox *intSpinBox;
234 mutable QDoubleSpinBox *doubleSpinBox;
235 mutable QComboBox *comboBox;
236 mutable KInputDialogListView *listView;
237 mutable QWidget *inputWidget;
238 mutable QVBoxLayout *mainLayout;
239 KInputDialog::InputDialogOptions opts;
240 QString textValue;
241 QPointer<QObject> receiverToDisconnectOnClose;
242 QByteArray memberToDisconnectOnClose;
243
244 private:
245 KInputDialog *q_ptr;
246 };
247
248 KInputDialogPrivate::KInputDialogPrivate(KInputDialog *parent)
249 : q_ptr(parent)
250 , label(nullptr)
251 , okButton(nullptr)
252 , cancelButton(nullptr)
253 , lineEdit(nullptr)
254 , plainTextEdit(nullptr)
255 , intSpinBox(nullptr)
256 , doubleSpinBox(nullptr)
257 , comboBox(nullptr)
258 , listView(nullptr)
259 , inputWidget(nullptr)
260 , mainLayout(nullptr)
261 {
262 Q_Q(KInputDialog);
263 q->setMinimumHeight(198);
264 q->setMinimumWidth(336);
265 connect(q->m_gsetting,&QGSettings::changed,this,[=](QString key){
266 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
267 {
268 q->changeTheme();
269 }
270 });
271 }
272
273 void KInputDialogPrivate::ensureLayout()
274 {
275 Q_Q(KInputDialog);
276 if (mainLayout)
277 return;
278 if (!inputWidget) {
279 ensureLineEdit();
280 inputWidget = lineEdit;
281 }
282
283 if (!label)
284 {
285 label = new QLabel(KInputDialog::tr("Enter a value:"), q);
286 setAccessInfo();
287 }
288 q->mainLayout()->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
289 #ifndef QT_NO_SHORTCUT
290 label->setBuddy(inputWidget);
291 #endif
292 label->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
293 label->setWordWrap(true);
294 QHBoxLayout *hLayout = new QHBoxLayout();
295 okButton = new QPushButton(tr("ok"), q);
296 okButton->setFixedSize(96, 36);
297 cancelButton = new QPushButton(tr("cancel"), q);
298 cancelButton->setFixedSize(96, 36);
299 hLayout->setContentsMargins(0, 0, 0, 0);
300 hLayout->setSpacing(0);
301 hLayout->addStretch();
302 hLayout->addWidget(cancelButton);
303 hLayout->addSpacing(10);
304 hLayout->addWidget(okButton);
305 QObject::connect(okButton, SIGNAL(clicked(bool)), q, SLOT(accept()));
306 QObject::connect(cancelButton, SIGNAL(clicked(bool)), q, SLOT(reject()));
307
308 mainLayout = new QVBoxLayout();
309 mainLayout->setSpacing(0);
310 mainLayout->setContentsMargins(24, 0, 24, 24);
311 mainLayout->addWidget(label);
312 mainLayout->addSpacing(8);
313 mainLayout->addWidget(inputWidget);
314 mainLayout->addSpacing(32);
315 // mainLayout->addStretch();
316 mainLayout->addLayout(hLayout);
317 q->mainWidget()->setLayout(mainLayout);
318 q->changeTheme();
319 ensureEnabledConnection(qobject_cast<QAbstractSpinBox *>(inputWidget));
320 inputWidget->show();
321
322 KDK_ALL_INFO_FORMAT(okButton,"");
323 KDK_ALL_INFO_FORMAT(cancelButton,"");
324 }
325
326 void KInputDialogPrivate::ensureLineEdit()
327 {
328 Q_Q(KInputDialog);
329 if (!lineEdit) {
330 lineEdit = new KInputDialogLineEdit(q);
331 lineEdit->setFixedHeight(36);
332 lineEdit->hide();
333
334 KDK_ALL_INFO_FORMAT(lineEdit,"");
335
336 QObject::connect(lineEdit, SIGNAL(textChanged(QString)),
337 q, SLOT(_q_textChanged(QString)));
338 }
339 }
340
341 void KInputDialogPrivate::ensurePlainTextEdit()
342 {
343 Q_Q(KInputDialog);
344 if (!plainTextEdit) {
345 plainTextEdit = new QPlainTextEdit(q);
346 plainTextEdit->setLineWrapMode(QPlainTextEdit::NoWrap);
347 plainTextEdit->hide();
348
349 KDK_ALL_INFO_FORMAT(plainTextEdit,"");
350
351 QObject::connect(plainTextEdit, SIGNAL(textChanged()),
352 q, SLOT(_q_plainTextEditTextChanged()));
353 }
354 }
355
356 void KInputDialogPrivate::ensureComboBox()
357 {
358 Q_Q(KInputDialog);
359 if (!comboBox) {
360 comboBox = new QComboBox(q);
361 comboBox->hide();
362
363 KDK_ALL_INFO_FORMAT(comboBox,"");
364
365 QObject::connect(comboBox, SIGNAL(editTextChanged(QString)),
366 q, SLOT(_q_textChanged(QString)));
367 QObject::connect(comboBox, SIGNAL(currentIndexChanged(QString)),
368 q, SLOT(_q_textChanged(QString)));
369 }
370 }
371
372 void KInputDialogPrivate::ensureListView()
373 {
374 Q_Q(KInputDialog);
375 if (!listView) {
376 ensureComboBox();
377 listView = new KInputDialogListView(q);
378 listView->hide();
379
380 KDK_ALL_INFO_FORMAT(listView,"");
381
382 listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
383 listView->setSelectionMode(QAbstractItemView::SingleSelection);
384 listView->setModel(comboBox->model());
385 listView->setCurrentIndex(QModelIndex());
386 QObject::connect(listView->selectionModel(),
387 SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),
388 q, SLOT(_q_currentRowChanged(QModelIndex, QModelIndex)));
389 }
390 }
391
392 void KInputDialogPrivate::ensureIntSpinBox()
393 {
394 Q_Q(KInputDialog);
395 if (!intSpinBox) {
396 intSpinBox = new KInputDialogSpinBox(q);
397 intSpinBox->hide();
398
399 KDK_ALL_INFO_FORMAT(intSpinBox,"");
400
401 QObject::connect(intSpinBox, SIGNAL(valueChanged(int)),
402 q, SIGNAL(intValueChanged(int)));
403 }
404 }
405
406 void KInputDialogPrivate::ensureDoubleSpinBox()
407 {
408 Q_Q(KInputDialog);
409 if (!doubleSpinBox) {
410 doubleSpinBox = new KInputDialogDoubleSpinBox(q);
411 doubleSpinBox->hide();
412
413 KDK_ALL_INFO_FORMAT(doubleSpinBox,"");
414
415 QObject::connect(doubleSpinBox, SIGNAL(valueChanged(double)),
416 q, SIGNAL(doubleValueChanged(double)));
417 }
418 }
419
420 void KInputDialogPrivate::ensureEnabledConnection(QAbstractSpinBox *spinBox)
421 {
422 if (spinBox) {
423 QAbstractButton *okBtn = okButton;
424 QObject::connect(spinBox, SIGNAL(textChanged(int)), okBtn, SLOT(setEnabled(bool)), Qt::UniqueConnection);
425 }
426 }
427
428 void KInputDialogPrivate::setInputWidget(QWidget *widget)
429 {
430 Q_Q(KInputDialog);
431 Q_ASSERT(widget);
432 if (inputWidget == widget)
433 return;
434
435 if (mainLayout) {
436 Q_ASSERT(inputWidget);
437 mainLayout->removeWidget(inputWidget);
438 inputWidget->hide();
439 mainLayout->insertWidget(1, widget);
440 widget->show();
441
442 QAbstractButton *okBtn = okButton;
443 if (QAbstractSpinBox *spinBox = qobject_cast<QAbstractSpinBox *>(inputWidget))
444 QObject::disconnect(spinBox, SIGNAL(textChanged(int)), okBtn, SLOT(setEnabled(bool)));
445
446 QAbstractSpinBox *spinBox = qobject_cast<QAbstractSpinBox *>(widget);
447 ensureEnabledConnection(spinBox);
448 okButton->setEnabled(!spinBox || spinBox->hasAcceptableInput());
449 q->changeTheme();
450 }
451
452 inputWidget = widget;
453
454 if (widget == lineEdit) {
455 lineEdit->setText(textValue);
456 } else if (widget == plainTextEdit) {
457 plainTextEdit->setPlainText(textValue);
458 } else if (widget == comboBox) {
459 setComboBoxText(textValue);
460 } else if (widget == listView) {
461 setListViewText(textValue);
462 ensureLayout();
463 okButton->setEnabled(listView->selectionModel()->hasSelection());
464 }
465 }
466
467 void KInputDialogPrivate::chooseRightTextInputWidget()
468 {
469 QWidget *widget;
470
471 if (useComboBoxOrListView()) {
472 if ((opts & KInputDialog::UseListViewForComboBoxItems) && !comboBox->isEditable()) {
473 ensureListView();
474 widget = listView;
475 } else {
476 widget = comboBox;
477 }
478 } else if (opts & KInputDialog::UsePlainTextEditForTextInput) {
479 ensurePlainTextEdit();
480 widget = plainTextEdit;
481 } else {
482 ensureLineEdit();
483 widget = lineEdit;
484 }
485
486 setInputWidget(widget);
487
488 if (inputWidget == comboBox) {
489 _q_textChanged(comboBox->currentText());
490 } else if (inputWidget == listView) {
491 _q_textChanged(listViewText());
492 }
493 }
494
495 void KInputDialogPrivate::setComboBoxText(const QString &text)
496 {
497 int index = comboBox->findText(text);
498 if (index != -1) {
499 comboBox->setCurrentIndex(index);
500 } else if (comboBox->isEditable()) {
501 comboBox->setEditText(text);
502 }
503 }
504
505 void KInputDialogPrivate::setListViewText(const QString &text)
506 {
507 int row = comboBox->findText(text);
508 if (row != -1) {
509 QModelIndex index(comboBox->model()->index(row, 0));
510 listView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Clear | QItemSelectionModel::SelectCurrent);
511 }
512 }
513
514 QString KInputDialogPrivate::listViewText() const
515 {
516 if (listView->selectionModel()->hasSelection()) {
517 int row = listView->selectionModel()->selectedRows().value(0).row();
518 return comboBox->itemText(row);
519 } else {
520 return QString();
521 }
522 }
523
524 void KInputDialogPrivate::_q_textChanged(const QString &text)
525 {
526 Q_Q(KInputDialog);
527 if (textValue != text) {
528 textValue = text;
529 Q_EMIT q->textValueChanged(text);
530 }
531 }
532
533 void KInputDialogPrivate::_q_plainTextEditTextChanged()
534 {
535 Q_Q(KInputDialog);
536 QString text = plainTextEdit->toPlainText();
537 if (textValue != text) {
538 textValue = text;
539 Q_EMIT q->textValueChanged(text);
540 }
541 }
542
543 void KInputDialogPrivate::_q_currentRowChanged(const QModelIndex &newIndex,
544 const QModelIndex & /* oldIndex */)
545 {
546 _q_textChanged(comboBox->model()->data(newIndex).toString());
547 okButton->setEnabled(true);
548 }
549
550 void KInputDialogPrivate::setAccessInfo()
551 {
552 Q_Q(KInputDialog);
553 KDK_ALL_INFO_FORMAT(label,"");
554 }
555
556 KInputDialog::KInputDialog(QWidget *parent)
557 : KDialog(parent)
558 , d_ptr(new KInputDialogPrivate(this))
559 {
560 }
561
562 KInputDialog::~KInputDialog()
563 {
564 }
565
566 void KInputDialog::setInputMode(InputMode mode)
567 {
568 Q_D(KInputDialog);
569
570 QWidget *widget;
571
572 /*
573 Warning: Some functions in KInputDialog rely on implementation details
574 of the code below. Look for the comments that accompany the calls to
575 setInputMode() throughout this file before you change the code below.
576 */
577
578 switch (mode) {
579 case IntInput:
580 d->ensureIntSpinBox();
581 widget = d->intSpinBox;
582 break;
583 case DoubleInput:
584 d->ensureDoubleSpinBox();
585 widget = d->doubleSpinBox;
586 break;
587 default:
588 Q_ASSERT(mode == TextInput);
589 d->chooseRightTextInputWidget();
590 return;
591 }
592
593 d->setInputWidget(widget);
594 }
595
596 KInputDialog::InputMode KInputDialog::inputMode() const
597 {
598 Q_D(const KInputDialog);
599
600 if (d->inputWidget) {
601 if (d->inputWidget == d->intSpinBox) {
602 return IntInput;
603 } else if (d->inputWidget == d->doubleSpinBox) {
604 return DoubleInput;
605 }
606 }
607
608 return TextInput;
609 }
610
611 void KInputDialog::setLabelText(const QString &text)
612 {
613 Q_D(KInputDialog);
614 if (!d->label) {
615 d->label = new QLabel(text, this);
616 d->setAccessInfo();
617 } else {
618 d->label->setText(text);
619 }
620 }
621
622 QString KInputDialog::labelText() const
623 {
624 Q_D(const KInputDialog);
625 d->ensureLayout();
626 return d->label->text();
627 }
628
629 void KInputDialog::setOption(InputDialogOption option, bool on)
630 {
631 Q_D(KInputDialog);
632 if (!(d->opts & option) != !on)
633 setOptions(d->opts ^ option);
634 }
635
636 bool KInputDialog::testOption(InputDialogOption option) const
637 {
638 Q_D(const KInputDialog);
639 return (d->opts & option) != 0;
640 }
641
642 void KInputDialog::setOptions(InputDialogOptions options)
643 {
644 Q_D(KInputDialog);
645
646 InputDialogOptions changed = (options ^ d->opts);
647 if (!changed)
648 return;
649
650 d->opts = options;
651 d->ensureLayout();
652
653 if (changed & NoButtons) {
654 d->okButton->setVisible(!(options & NoButtons));
655 d->cancelButton->setVisible(!(options & NoButtons));
656 }
657 if ((changed & UseListViewForComboBoxItems) && inputMode() == TextInput)
658 d->chooseRightTextInputWidget();
659 if ((changed & UsePlainTextEditForTextInput) && inputMode() == TextInput)
660 d->chooseRightTextInputWidget();
661 }
662
663 KInputDialog::InputDialogOptions KInputDialog::options() const
664 {
665 Q_D(const KInputDialog);
666 return d->opts;
667 }
668
669 void KInputDialog::setTextValue(const QString &text)
670 {
671 Q_D(KInputDialog);
672
673 setInputMode(TextInput);
674 if (d->inputWidget == d->lineEdit) {
675 d->lineEdit->setText(text);
676 } else if (d->inputWidget == d->plainTextEdit) {
677 d->plainTextEdit->setPlainText(text);
678 } else if (d->inputWidget == d->comboBox) {
679 d->setComboBoxText(text);
680 } else {
681 d->setListViewText(text);
682 }
683 }
684
685 QString KInputDialog::textValue() const
686 {
687 Q_D(const KInputDialog);
688 return d->textValue;
689 }
690
691 void KInputDialog::setTextEchoMode(QLineEdit::EchoMode mode)
692 {
693 Q_D(KInputDialog);
694 d->ensureLineEdit();
695 d->lineEdit->setEchoMode(mode);
696 }
697
698 QLineEdit::EchoMode KInputDialog::textEchoMode() const
699 {
700 Q_D(const KInputDialog);
701 if (d->lineEdit) {
702 return d->lineEdit->echoMode();
703 } else {
704 return QLineEdit::Normal;
705 }
706 }
707
708 void KInputDialog::setComboBoxEditable(bool editable)
709 {
710 Q_D(KInputDialog);
711 d->ensureComboBox();
712 d->comboBox->setEditable(editable);
713 if (inputMode() == TextInput)
714 d->chooseRightTextInputWidget();
715 }
716
717 bool KInputDialog::isComboBoxEditable() const
718 {
719 Q_D(const KInputDialog);
720 if (d->comboBox) {
721 return d->comboBox->isEditable();
722 } else {
723 return false;
724 }
725 }
726
727 void KInputDialog::setComboBoxItems(const QStringList &items)
728 {
729 Q_D(KInputDialog);
730
731 d->ensureComboBox();
732 {
733 const QSignalBlocker blocker(d->comboBox);
734 d->comboBox->clear();
735 d->comboBox->addItems(items);
736 }
737
738 if (inputMode() == TextInput)
739 d->chooseRightTextInputWidget();
740 }
741
742 QStringList KInputDialog::comboBoxItems() const
743 {
744 Q_D(const KInputDialog);
745 QStringList result;
746 if (d->comboBox) {
747 const int count = d->comboBox->count();
748 result.reserve(count);
749 for (int i = 0; i < count; ++i)
750 result.append(d->comboBox->itemText(i));
751 }
752 return result;
753 }
754
755 void KInputDialog::setIntValue(int value)
756 {
757 Q_D(KInputDialog);
758 setInputMode(IntInput);
759 d->intSpinBox->setValue(value);
760 }
761
762 int KInputDialog::intValue() const
763 {
764 Q_D(const KInputDialog);
765 if (d->intSpinBox) {
766 return d->intSpinBox->value();
767 } else {
768 return 0;
769 }
770 }
771
772 void KInputDialog::setIntMinimum(int min)
773 {
774 Q_D(KInputDialog);
775 d->ensureIntSpinBox();
776 d->intSpinBox->setMinimum(min);
777 }
778
779 int KInputDialog::intMinimum() const
780 {
781 Q_D(const KInputDialog);
782 if (d->intSpinBox) {
783 return d->intSpinBox->minimum();
784 } else {
785 return 0;
786 }
787 }
788
789 void KInputDialog::setIntMaximum(int max)
790 {
791 Q_D(KInputDialog);
792 d->ensureIntSpinBox();
793 d->intSpinBox->setMaximum(max);
794 }
795
796 int KInputDialog::intMaximum() const
797 {
798 Q_D(const KInputDialog);
799 if (d->intSpinBox) {
800 return d->intSpinBox->maximum();
801 } else {
802 return 99;
803 }
804 }
805
806 void KInputDialog::setIntRange(int min, int max)
807 {
808 Q_D(KInputDialog);
809 d->ensureIntSpinBox();
810 d->intSpinBox->setRange(min, max);
811 }
812
813 void KInputDialog::setIntStep(int step)
814 {
815 Q_D(KInputDialog);
816 d->ensureIntSpinBox();
817 d->intSpinBox->setSingleStep(step);
818 }
819
820 int KInputDialog::intStep() const
821 {
822 Q_D(const KInputDialog);
823 if (d->intSpinBox) {
824 return d->intSpinBox->singleStep();
825 } else {
826 return 1;
827 }
828 }
829
830 void KInputDialog::setDoubleValue(double value)
831 {
832 Q_D(KInputDialog);
833 setInputMode(DoubleInput);
834 d->doubleSpinBox->setValue(value);
835 }
836
837 double KInputDialog::doubleValue() const
838 {
839 Q_D(const KInputDialog);
840 if (d->doubleSpinBox) {
841 return d->doubleSpinBox->value();
842 } else {
843 return 0.0;
844 }
845 }
846
847 void KInputDialog::setDoubleMinimum(double min)
848 {
849 Q_D(KInputDialog);
850 d->ensureDoubleSpinBox();
851 d->doubleSpinBox->setMinimum(min);
852 }
853
854 double KInputDialog::doubleMinimum() const
855 {
856 Q_D(const KInputDialog);
857 if (d->doubleSpinBox) {
858 return d->doubleSpinBox->minimum();
859 } else {
860 return 0.0;
861 }
862 }
863
864 void KInputDialog::setDoubleMaximum(double max)
865 {
866 Q_D(KInputDialog);
867 d->ensureDoubleSpinBox();
868 d->doubleSpinBox->setMaximum(max);
869 }
870
871 double KInputDialog::doubleMaximum() const
872 {
873 Q_D(const KInputDialog);
874 if (d->doubleSpinBox) {
875 return d->doubleSpinBox->maximum();
876 } else {
877 return 99.99;
878 }
879 }
880
881 void KInputDialog::setDoubleRange(double min, double max)
882 {
883 Q_D(KInputDialog);
884 d->ensureDoubleSpinBox();
885 d->doubleSpinBox->setRange(min, max);
886 }
887
888 void KInputDialog::setDoubleDecimals(int decimals)
889 {
890 Q_D(KInputDialog);
891 d->ensureDoubleSpinBox();
892 d->doubleSpinBox->setDecimals(decimals);
893 }
894
895 int KInputDialog::doubleDecimals() const
896 {
897 Q_D(const KInputDialog);
898 if (d->doubleSpinBox) {
899 return d->doubleSpinBox->decimals();
900 } else {
901 return 2;
902 }
903 }
904
905 void KInputDialog::setOkButtonText(const QString &text)
906 {
907 Q_D(const KInputDialog);
908 d->ensureLayout();
909 d->okButton->setText(text);
910 }
911
912 QString KInputDialog::okButtonText() const
913 {
914 Q_D(const KInputDialog);
915 d->ensureLayout();
916 return d->okButton->text();
917 }
918
919 void KInputDialog::setCancelButtonText(const QString &text)
920 {
921 Q_D(const KInputDialog);
922 d->ensureLayout();
923 d->cancelButton->setText(text);
924 }
925
926 QString KInputDialog::cancelButtonText() const
927 {
928 Q_D(const KInputDialog);
929 d->ensureLayout();
930 return d->cancelButton->text();
931 }
932
933 void KInputDialog::open(QObject *receiver, const char *member)
934 {
935 Q_D(KInputDialog);
936 connect(this, signalForMember(member), receiver, member);
937 d->receiverToDisconnectOnClose = receiver;
938 d->memberToDisconnectOnClose = member;
939 QDialog::open();
940 }
941
942 QSize KInputDialog::minimumSizeHint() const
943 {
944 Q_D(const KInputDialog);
945 d->ensureLayout();
946 return QDialog::minimumSizeHint();
947 }
948
949 QSize KInputDialog::sizeHint() const
950 {
951 Q_D(const KInputDialog);
952 d->ensureLayout();
953 return QDialog::sizeHint();
954 }
955
956 void KInputDialog::setVisible(bool visible)
957 {
958 Q_D(const KInputDialog);
959 if (visible) {
960 d->ensureLayout();
961 d->inputWidget->setFocus();
962 if (d->inputWidget == d->lineEdit) {
963 d->lineEdit->selectAll();
964 } else if (d->inputWidget == d->plainTextEdit) {
965 d->plainTextEdit->selectAll();
966 } else if (d->inputWidget == d->intSpinBox) {
967 d->intSpinBox->selectAll();
968 } else if (d->inputWidget == d->doubleSpinBox) {
969 d->doubleSpinBox->selectAll();
970 }
971 }
972 QDialog::setVisible(visible);
973 }
974
975 QString KInputDialog::placeholderText() const
976 {
977 Q_D(const KInputDialog);
978 if (d->lineEdit)
979 return d->lineEdit->placeholderText();
980 else
981 return QString();
982 }
983
984 void KInputDialog::setPlaceholderText(const QString &str)
985 {
986 Q_D(const KInputDialog);
987 if (d->lineEdit)
988 d->lineEdit->setPlaceholderText(str);
989 }
990
991 void KInputDialog::done(int result)
992 {
993 Q_D(KInputDialog);
994 QDialog::done(result);
995 if (result) {
996 InputMode mode = inputMode();
997 switch (mode) {
998 case DoubleInput:
999 Q_EMIT doubleValueSelected(doubleValue());
1000 break;
1001 case IntInput:
1002 Q_EMIT intValueSelected(intValue());
1003 break;
1004 default:
1005 Q_ASSERT(mode == TextInput);
1006 Q_EMIT textValueSelected(textValue());
1007 }
1008 }
1009 if (d->receiverToDisconnectOnClose) {
1010 disconnect(this, signalForMember(d->memberToDisconnectOnClose),
1011 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
1012 d->receiverToDisconnectOnClose = nullptr;
1013 }
1014 d->memberToDisconnectOnClose.clear();
1015 }
1016
1017 void KInputDialog::changeTheme()
1018 {
1019 Q_D(KInputDialog);
1020 KDialog::changeTheme();
1021 if (d->okButton)
1022 d->okButton->setProperty("isImportant", true);
1023 }
1024
1025 QString KInputDialog::getText(QWidget *parent, const QString &label,
1026 QLineEdit::EchoMode mode, const QString &text, bool *ok,
1027 Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
1028 {
1029 KInputDialog *dialog = new KInputDialog(parent);
1030 dialog->setLabelText(label);
1031 dialog->setTextValue(text);
1032 dialog->setTextEchoMode(mode);
1033 dialog->setInputMethodHints(inputMethodHints);
1034
1035 const int ret = dialog->exec();
1036 if (ok)
1037 *ok = !!ret;
1038 if (ret) {
1039 return dialog->textValue();
1040 } else {
1041 return QString();
1042 }
1043 }
1044
1045 QString KInputDialog::getMultiLineText(QWidget *parent, const QString &label,
1046 const QString &text, bool *ok, Qt::WindowFlags flags,
1047 Qt::InputMethodHints inputMethodHints)
1048 {
1049 KInputDialog *dialog = new KInputDialog(parent);
1050 dialog->setOptions(KInputDialog::UsePlainTextEditForTextInput);
1051 dialog->setLabelText(label);
1052 dialog->setTextValue(text);
1053 dialog->setInputMethodHints(inputMethodHints);
1054
1055 const int ret = dialog->exec();
1056 if (ok)
1057 *ok = !!ret;
1058 if (ret) {
1059 return dialog->textValue();
1060 } else {
1061 return QString();
1062 }
1063 }
1064
1065 int KInputDialog::getInt(QWidget *parent, const QString &label, int value,
1066 int min, int max, int step, bool *ok, Qt::WindowFlags flags)
1067 {
1068 KInputDialog *dialog = new KInputDialog(parent);
1069 dialog->setLabelText(label);
1070 dialog->setIntRange(min, max);
1071 dialog->setIntValue(value);
1072 dialog->setIntStep(step);
1073
1074 const int ret = dialog->exec();
1075 if (ok)
1076 *ok = !!ret;
1077 if (ret) {
1078 return dialog->intValue();
1079 } else {
1080 return value;
1081 }
1082 }
1083
1084 double KInputDialog::getDouble(QWidget *parent, const QString &label, double value, double minValue, double maxValue, int decimals, bool *ok, Qt::WindowFlags flags)
1085 {
1086 KInputDialog *dialog = new KInputDialog(parent);
1087 dialog->setLabelText(label);
1088 dialog->setDoubleDecimals(decimals);
1089 dialog->setDoubleRange(minValue, maxValue);
1090 dialog->setDoubleValue(value);
1091
1092 const int ret = dialog->exec();
1093 if (ok)
1094 *ok = !!ret;
1095 if (ret) {
1096 return dialog->doubleValue();
1097 } else {
1098 return value;
1099 }
1100 }
1101
1102 QString KInputDialog::getItem(QWidget *parent, const QString &label,
1103 const QStringList &items, int current, bool editable, bool *ok,
1104 Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
1105 {
1106 QString text(items.value(current));
1107
1108 KInputDialog *dialog = new KInputDialog(parent);
1109 dialog->setLabelText(label);
1110 dialog->setComboBoxItems(items);
1111 dialog->setTextValue(text);
1112 dialog->setComboBoxEditable(editable);
1113 dialog->setInputMethodHints(inputMethodHints);
1114
1115 const int ret = dialog->exec();
1116 if (ok)
1117 *ok = !!ret;
1118 if (ret) {
1119 return dialog->textValue();
1120 } else {
1121 return text;
1122 }
1123 }
1124
1125 void KInputDialog::setDoubleStep(double step)
1126 {
1127 Q_D(KInputDialog);
1128 d->ensureDoubleSpinBox();
1129 d->doubleSpinBox->setSingleStep(step);
1130 }
1131
1132 double KInputDialog::doubleStep() const
1133 {
1134 Q_D(const KInputDialog);
1135 if (d->doubleSpinBox)
1136 return d->doubleSpinBox->singleStep();
1137 else
1138 return 1.0;
1139 }
1140
1141 QSize KInputDialogSpinBox::sizeHint() const
1142 {
1143 QSize size;
1144 size.setWidth(288);
1145 size.setHeight(36);
1146 return size;
1147 }
1148
1149 QSize KInputDialogLineEdit::sizeHint() const
1150 {
1151 QSize size;
1152 size.setWidth(288);
1153 size.setHeight(36);
1154 return size;
1155 }
1156
1157 QSize KInputDialogDoubleSpinBox::sizeHint() const
1158 {
1159 QSize size;
1160 size.setWidth(288);
1161 size.setHeight(36);
1162 return size;
1163 }
1164
1165 }
1166
1167 #include "kinputdialog.moc"
1168 #include "moc_kinputdialog.cpp"