File indexing completed on 2024-04-21 05:43:40
0001 /*************************************************************************** 0002 * Copyright (C) 2020 by Friedrich W. H. Kossebau - kossebau@kde.org * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU General Public License as published by * 0006 * the Free Software Foundation; either version 2 of the License, or * 0007 * (at your option) any later version. * 0008 ***************************************************************************/ 0009 0010 #include "inputdialog.h" 0011 0012 #include <QDialog> 0013 #include <QDialogButtonBox> 0014 #include <QHBoxLayout> 0015 #include <QLabel> 0016 #include <QLineEdit> 0017 #include <QPushButton> 0018 #include <QString> 0019 #include <QVBoxLayout> 0020 #include <QValidator> 0021 0022 namespace InputDialog 0023 { 0024 class Dialog : public QDialog 0025 { 0026 Q_OBJECT 0027 0028 public: 0029 Dialog(const QString &windowTitle, const QString &labelText, const QString &value, QWidget *parent, QValidator *validator); 0030 0031 QString text() const; 0032 0033 private Q_SLOTS: 0034 void handleTextChanged(const QString &text); 0035 0036 private: 0037 QValidator *m_validator; 0038 QLineEdit *m_lineEdit; 0039 QDialogButtonBox *m_buttonBox; 0040 }; 0041 0042 Dialog::Dialog(const QString &windowTitle, const QString &labelText, const QString &value, QWidget *parent, QValidator *validator) 0043 : QDialog(parent) 0044 , m_validator(validator) 0045 { 0046 setWindowTitle(windowTitle); 0047 setModal(true); 0048 0049 QVBoxLayout *layout = new QVBoxLayout; 0050 setLayout(layout); 0051 QHBoxLayout *editLayout = new QHBoxLayout; 0052 0053 QLabel *label = new QLabel(labelText, this); 0054 label->setWordWrap(true); 0055 editLayout->addWidget(label); 0056 0057 m_lineEdit = new QLineEdit(this); 0058 m_lineEdit->setText(value); 0059 m_lineEdit->setClearButtonEnabled(true); 0060 m_lineEdit->setFocus(); 0061 if (validator) { 0062 m_lineEdit->setValidator(validator); 0063 } 0064 label->setBuddy(m_lineEdit); 0065 editLayout->addWidget(m_lineEdit); 0066 0067 layout->addLayout(editLayout); 0068 0069 layout->addStretch(); 0070 0071 m_buttonBox = new QDialogButtonBox(this); 0072 m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); 0073 connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0074 connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0075 layout->addWidget(m_buttonBox); 0076 0077 connect(m_lineEdit, &QLineEdit::textChanged, this, &Dialog::handleTextChanged); 0078 0079 handleTextChanged(value); 0080 } 0081 0082 QString Dialog::text() const 0083 { 0084 return m_lineEdit->text(); 0085 } 0086 0087 void Dialog::handleTextChanged(const QString &text) 0088 { 0089 bool acceptable; 0090 0091 if (m_validator) { 0092 QString validatedText(text); 0093 int index = m_lineEdit->cursorPosition(); 0094 acceptable = (m_validator->validate(validatedText, index) == QValidator::Acceptable); 0095 } else { 0096 acceptable = !text.trimmed().isEmpty(); 0097 } 0098 0099 m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable); 0100 } 0101 0102 QString getText(const QString &windowTitle, const QString &labelText, const QString &value, bool *ok, QWidget *parent, QValidator *validator) 0103 { 0104 Dialog *dialog = new Dialog(windowTitle, labelText, value, parent, validator); 0105 0106 const bool accepted = (dialog->exec() == QDialog::Accepted); 0107 0108 const QString result = accepted ? dialog->text() : QString(); 0109 if (ok) { 0110 *ok = accepted; 0111 } 0112 0113 delete dialog; 0114 0115 return result; 0116 } 0117 0118 } 0119 0120 #include "inputdialog.moc"