File indexing completed on 2024-05-19 08:56:13
0001 /* 0002 * Copyright 2003 Nadeem Hasan <nhasan@kde.org> 0003 * Copyright 2015 Andreas Cord-Landwehr <cordlandwehr@kde.org> 0004 * 0005 * This program is free software; you can redistribute it and/or 0006 * modify it under the terms of the GNU Library General Public 0007 * License as published by the Free Software Foundation; either 0008 * version 2 of the License, or (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, write to 0017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #include "validatorinputdialog.h" 0022 #include "validatorinputdialog_p.h" 0023 0024 #include <QDialogButtonBox> 0025 #include <QLabel> 0026 #include <QLineEdit> 0027 #include <QPushButton> 0028 #include <QValidator> 0029 #include <QVBoxLayout> 0030 0031 using namespace KileDialog; 0032 0033 ValidatorInputDialogHelper::ValidatorInputDialogHelper(const QString &caption, const QString &label, 0034 const QString &value, QWidget *parent, 0035 QValidator *validator, const QString &mask) 0036 : QDialog(parent) 0037 , m_lineEdit(new QLineEdit(this)) 0038 , m_buttonBox(new QDialogButtonBox(this)) 0039 { 0040 setWindowTitle(caption); 0041 setModal(true); 0042 0043 QVBoxLayout *layout = new QVBoxLayout; 0044 setLayout(layout); 0045 0046 QLabel *m_label = new QLabel(label, this); 0047 m_label->setWordWrap(true); 0048 layout->addWidget(m_label); 0049 0050 m_lineEdit->setText(value); 0051 m_lineEdit->setClearButtonEnabled(true); 0052 layout->addWidget(m_lineEdit); 0053 0054 m_lineEdit->setFocus(); 0055 m_label->setBuddy(m_lineEdit); 0056 0057 layout->addStretch(); 0058 0059 m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); 0060 connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0061 connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0062 layout->addWidget(m_buttonBox); 0063 0064 if (validator) { 0065 m_lineEdit->setValidator(validator); 0066 } 0067 0068 if (!mask.isEmpty()) { 0069 m_lineEdit->setInputMask(mask); 0070 } 0071 0072 connect(m_lineEdit, &QLineEdit::textChanged, this, &ValidatorInputDialogHelper::slotEditTextChanged); 0073 0074 slotEditTextChanged(value); 0075 setMinimumWidth(350); 0076 } 0077 0078 QLineEdit * ValidatorInputDialogHelper::lineEdit() const 0079 { 0080 return m_lineEdit; 0081 } 0082 0083 void ValidatorInputDialogHelper::slotEditTextChanged(const QString &text) 0084 { 0085 bool enabled; 0086 0087 if (m_lineEdit->validator()) { 0088 QString str = m_lineEdit->text(); 0089 int index = m_lineEdit->cursorPosition(); 0090 enabled = (m_lineEdit->validator()->validate(str, index) == QValidator::Acceptable); 0091 } else { 0092 enabled = !text.trimmed().isEmpty(); 0093 } 0094 0095 m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enabled); 0096 } 0097 0098 QString KileDialog::getText(const QString &caption, 0099 const QString &label, const QString &value, QWidget *parent, 0100 QValidator *validator, const QString &mask) 0101 { 0102 ValidatorInputDialogHelper dlg(caption, label, value, parent, validator, mask); 0103 0104 bool ok = (dlg.exec() == QDialog::Accepted); 0105 0106 QString result; 0107 if (ok) { 0108 result = dlg.lineEdit()->text(); 0109 } 0110 0111 // validator may explicitly allow leading and trailing whitespace 0112 if (!validator) { 0113 result = result.trimmed(); 0114 } 0115 0116 return result; 0117 } 0118 0119 #include "moc_validatorinputdialog_p.cpp"