1 /*
2  * Cantata
3  *
4  * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5  *
6  * ----
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "inputdialog.h"
25 #include "gtkstyle.h"
26 #include "lineedit.h"
27 #include "dialog.h"
28 #include "utils.h"
29 #include <QLabel>
30 #include <QFormLayout>
31 #include <QSpinBox>
32 #include <algorithm>
33 
34 enum InputType {
35     Int,
36     Text,
37     Combo
38 };
39 
InputDialog(const QString & caption,const QString & label,const QString & value,const QStringList & options,QLineEdit::EchoMode echo,QWidget * parent)40 InputDialog::InputDialog(const QString &caption, const QString &label, const QString &value, const QStringList &options, QLineEdit::EchoMode echo, QWidget *parent)
41     : Dialog(parent)
42     , spin(nullptr)
43     , edit(nullptr)
44     , combo(nullptr)
45 {
46     init(options.isEmpty() ? Text : Combo, caption, label);
47     if (options.isEmpty()) {
48         edit->setText(value);
49         edit->setEchoMode(echo);
50         connect(edit, SIGNAL(textChanged(QString)), this, SLOT(enableOkButton()));
51     } else {
52         QStringList items = options;
53         if (!value.isEmpty() && -1==items.indexOf(value)) {
54             items.append(value);
55         }
56         std::sort(items.begin(), items.end());
57         combo->addItems(items);
58         combo->setCurrentText(value.isEmpty() ? QString() : value);
59         connect(combo, SIGNAL(editTextChanged(QString)), this, SLOT(enableOkButton()));
60     }
61     enableOkButton();
62 }
63 
InputDialog(const QString & caption,const QString & label,int value,int minValue,int maxValue,int step,QWidget * parent)64 InputDialog::InputDialog(const QString &caption, const QString &label, int value, int minValue, int maxValue, int step, QWidget *parent)
65     : Dialog(parent)
66     , spin(nullptr)
67     , edit(nullptr)
68     , combo(nullptr)
69 {
70     init(Int, caption, label);
71     spin->setRange(minValue, maxValue);
72     spin->setValue(value);
73     spin->setSingleStep(step);
74 }
75 
addExtraWidget(QWidget * w)76 void InputDialog::addExtraWidget(QWidget *w)
77 {
78     w->setParent(mainWidget());
79     QVBoxLayout *layout=qobject_cast<QVBoxLayout *>(mainWidget()->layout());
80     layout->insertWidget(layout->count()-1, w);
81 }
82 
init(int type,const QString & caption,const QString & labelText)83 void InputDialog::init(int type, const QString &caption, const QString &labelText)
84 {
85     extra = nullptr;
86     setButtons(Ok|Cancel);
87     QWidget *wid=new QWidget(this);
88     QVBoxLayout *layout=new QVBoxLayout(wid);
89     layout->addWidget(new QLabel(labelText, wid));
90 
91     switch (type) {
92     case Int:
93         spin=new QSpinBox(wid);
94         spin->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
95         setMinimumWidth(Utils::scaleForDpi(300));
96         layout->addWidget(spin);
97         break;
98     case Text:
99         edit=new LineEdit(wid);
100         edit->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
101         edit->setMinimumWidth(Utils::scaleForDpi(350));
102         layout->addWidget(edit);
103         break;
104     case Combo:
105         combo=new ComboBox(wid);
106         combo->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
107         combo->setEditable(true);
108         combo->setMinimumWidth(Utils::scaleForDpi(350));
109         layout->addWidget(combo);
110         break;
111     }
112     layout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
113     layout->setMargin(0);
114     setCaption(caption);
115     setMainWidget(wid);
116     setButtons(Ok|Cancel);
117 }
118 
enableOkButton()119 void InputDialog::enableOkButton()
120 {
121     enableButton(Ok, (nullptr==edit || !edit->text().trimmed().isEmpty()) && (nullptr==combo || !combo->currentText().trimmed().isEmpty()));
122 }
123 
getInteger(const QString & caption,const QString & label,int value,int minValue,int maxValue,int step,int base,bool * ok,QWidget * parent)124 int InputDialog::getInteger(const QString &caption, const QString &label, int value, int minValue, int maxValue, int step, int base, bool *ok, QWidget *parent)
125 {
126     Q_UNUSED(base)
127     InputDialog dlg(caption, label, value, minValue, maxValue, step, parent);
128     if (QDialog::Accepted==dlg.exec()) {
129         if (ok) {
130             *ok=true;
131         }
132         return dlg.spin->value();
133     } else {
134         if (ok) {
135             *ok=false;
136         }
137         return value;
138     }
139 }
140 
getText(const QString & caption,const QString & label,QLineEdit::EchoMode echoMode,const QString & value,const QStringList & options,bool * ok,QWidget * parent)141 QString InputDialog::getText(const QString &caption, const QString &label, QLineEdit::EchoMode echoMode, const QString &value, const QStringList &options, bool *ok, QWidget *parent)
142 {
143     InputDialog dlg(caption, label, value, options, echoMode, parent);
144     if (QDialog::Accepted==dlg.exec()) {
145         if (ok) {
146             *ok=true;
147         }
148         return dlg.lineEdit()->text();
149     } else {
150         if (ok) {
151             *ok=false;
152         }
153         return value;
154     }
155 }
156 
157 #include "moc_inputdialog.cpp"
158