File indexing completed on 2024-04-28 17:04:34
0001 /***************************************************************************** 0002 * Copyright 2015 - 2015 Yichao Yu <yyc1992@gmail.com> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU Lesser General Public License as * 0006 * published by the Free Software Foundation; either version 2.1 of the * 0007 * License, or (at your option) version 3, or any later version accepted * 0008 * by the membership of KDE e.V. (or its successor approved by the * 0009 * membership of KDE e.V.), which shall act as a proxy defined in * 0010 * Section 6 of version 3 of the license. * 0011 * * 0012 * This program is distributed in the hope that it will be useful, * 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 0015 * Lesser General Public License for more details. * 0016 * * 0017 * You should have received a copy of the GNU Lesser General Public * 0018 * License along with this library. If not, * 0019 * see <http://www.gnu.org/licenses/>. * 0020 *****************************************************************************/ 0021 0022 #include "kf5_utils.h" 0023 0024 #include <qtcurve-utils/qtutils.h> 0025 0026 #include <QDialog> 0027 #include <QDialogButtonBox> 0028 #include <QVBoxLayout> 0029 #include <QPushButton> 0030 #include <QString> 0031 #include <QValidator> 0032 #include <QLabel> 0033 #include <QLineEdit> 0034 0035 namespace QtCurve { 0036 0037 QDialogButtonBox* 0038 createDialogButtonBox(QDialog *dialog) 0039 { 0040 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | 0041 QDialogButtonBox::Cancel); 0042 auto okButton = buttonBox->button(QDialogButtonBox::Ok); 0043 okButton->setDefault(true); 0044 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0045 QObject::connect(qtcSlot(buttonBox, accepted), qtcSlot(dialog, accept)); 0046 QObject::connect(qtcSlot(buttonBox, rejected), qtcSlot(dialog, reject)); 0047 return buttonBox; 0048 } 0049 0050 InputDialog::InputDialog(QWidget *parent, Qt::WindowFlags flags) 0051 : QDialog(parent), 0052 m_validator(nullptr) 0053 { 0054 if (flags != 0) 0055 setWindowFlags(flags); 0056 auto l = new QVBoxLayout(this); 0057 m_label = new QLabel(this); 0058 0059 m_text = new QLineEdit(this); 0060 connect(m_text, &QLineEdit::textChanged, this, &InputDialog::checkValid); 0061 0062 m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | 0063 QDialogButtonBox::Cancel, 0064 Qt::Horizontal, this); 0065 connect(m_buttonBox, &QDialogButtonBox::accepted, 0066 this, &InputDialog::accept); 0067 connect(m_buttonBox, &QDialogButtonBox::rejected, 0068 this, &InputDialog::reject); 0069 0070 l->addWidget(m_label); 0071 l->addWidget(m_text); 0072 l->addWidget(m_buttonBox); 0073 } 0074 0075 void 0076 InputDialog::setTitle(const QString &title) 0077 { 0078 setWindowTitle(title); 0079 } 0080 0081 void 0082 InputDialog::setLabelText(const QString &label) 0083 { 0084 m_label->setText(label); 0085 } 0086 0087 void 0088 InputDialog::setText(const QString &text) 0089 { 0090 m_text->setText(text); 0091 } 0092 0093 void 0094 InputDialog::setValidator(QValidator *validator) 0095 { 0096 m_validator = validator; 0097 m_text->setValidator(validator); 0098 checkValid(m_text->text()); 0099 } 0100 0101 QString 0102 InputDialog::getLabelText() 0103 { 0104 return m_label->text(); 0105 } 0106 0107 QString 0108 InputDialog::getText() 0109 { 0110 return m_text->text(); 0111 } 0112 0113 void 0114 InputDialog::checkValid(const QString &_text) 0115 { 0116 if (!m_validator) 0117 return; 0118 QString text = QString(_text); 0119 int pos = 0; 0120 bool valid = (m_validator->validate(text, pos) == QValidator::Acceptable); 0121 m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid); 0122 } 0123 0124 QString 0125 InputDialog::getText(QWidget *parent, const QString &title, 0126 const QString &label, const QString &text, 0127 QValidator *validator, bool *ok, Qt::WindowFlags flags) 0128 { 0129 InputDialog *r = new InputDialog(parent, flags); 0130 r->setTitle(title); 0131 r->setLabelText(label); 0132 r->setText(text); 0133 r->setValidator(validator); 0134 bool _ok = r->exec() == QDialog::Accepted; 0135 if (ok) { 0136 *ok = _ok; 0137 } 0138 if (_ok) { 0139 return r->getText(); 0140 } else { 0141 return QString(); 0142 } 0143 } 0144 0145 }