Debian Codegraph @

src/Gui/Dialogs/DlgInputDialogImp.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 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 *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <QPushButton>
24
25 #include "Dialogs/DlgInputDialogImp.h"
26 #include "ui_DlgInputDialog.h"
27 #include "SpinBox.h"
28
29
30 using namespace Gui::Dialog;
31
32 /* TRANSLATOR Gui::Dialog::DlgInputDialogImp */
33
34 /**
35 * Constructs a Gui::Dialog::DlgInputDialogImp as a child of 'parent', with the
36 * name 'name' and widget flags set to 'f'.
37 *
38 * The dialog will by default be modeless, unless you set 'modal' to
39 * true to construct a modal dialog.
40 */
41 DlgInputDialogImp::DlgInputDialogImp(const QString& labelTxt, QWidget* parent, bool modal, Type type)
42 : QDialog(parent)
43 , ui(new Ui_DlgInputDialog)
44 {
45 this->setModal(modal);
46 ui->setupUi(this);
47 ui->label->setText(labelTxt);
48
49 QSize sh = sizeHint();
50 setType(type);
51 resize(qMax(sh.width(), 400), 1);
52
53 connect(ui->lineEdit, &QLineEdit::returnPressed, this, &DlgInputDialogImp::tryAccept);
54 connect(ui->lineEdit, &QLineEdit::textChanged, this, &DlgInputDialogImp::textChanged);
55 }
56
57 /**
58 * Destroys the object and frees any allocated resources
59 */
60 DlgInputDialogImp::~DlgInputDialogImp() = default;
61
62 void DlgInputDialogImp::textChanged(const QString& s)
63 {
64 bool on = true;
65
66 if (ui->lineEdit->validator()) {
67 QString str = ui->lineEdit->text();
68 int index = ui->lineEdit->cursorPosition();
69 on = (ui->lineEdit->validator()->validate(str, index) == QValidator::Acceptable);
70 }
71 else if (type() != LineEdit) {
72 on = !s.isEmpty();
73 }
74
75 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(on);
76 }
77
78 void DlgInputDialogImp::tryAccept()
79 {
80 if (!ui->lineEdit->text().isEmpty()) {
81 accept();
82 }
83 }
84
85 void DlgInputDialogImp::setType(DlgInputDialogImp::Type t)
86 {
87 inputtype = t;
88
89 QWidget* input = nullptr;
90 switch (inputtype) {
91 case LineEdit:
92 input = ui->lineEdit;
93 break;
94 case SpinBox:
95 input = ui->spinBox;
96 break;
97 case UIntBox:
98 input = ui->uIntSpinBox;
99 break;
100 case FloatSpinBox:
101 input = ui->floatSpinBox;
102 break;
103 case ComboBox:
104 input = ui->comboBox;
105 break;
106 default:
107 break;
108 }
109
110 if (input) {
111 ui->stack->setCurrentWidget(input->parentWidget());
112 ui->stack->setFixedHeight(input->sizeHint().height());
113 input->setFocus();
114 ui->label->setBuddy(input);
115 }
116 }
117
118 DlgInputDialogImp::Type DlgInputDialogImp::type() const
119 {
120 return inputtype;
121 }
122
123 QSpinBox* DlgInputDialogImp::getSpinBox() const
124 {
125 return ui->spinBox;
126 }
127
128 Gui::UIntSpinBox* DlgInputDialogImp::getUIntBox() const
129 {
130 return ui->uIntSpinBox;
131 }
132
133 QDoubleSpinBox* DlgInputDialogImp::getFloatSpinBox() const
134 {
135 return ui->floatSpinBox;
136 }
137
138 QLineEdit* DlgInputDialogImp::getLineEdit() const
139 {
140 return ui->lineEdit;
141 }
142
143 QComboBox* DlgInputDialogImp::getComboBox() const
144 {
145 return ui->comboBox;
146 }
147
148 #include "moc_DlgInputDialogImp.cpp"