File indexing completed on 2024-04-21 05:45:48

0001 /*******************************************************************************
0002  * Copyright (C) 2018 Alexander Volkov <a.volkov@rusbitech.ru>                 *
0003  *                                                                             *
0004  * This program is free software: you can redistribute it and/or modify it     *
0005  * under the terms of the GNU General Public License as published by the Free  *
0006  * Software Foundation, either version 3 of the License, or (at your option)   *
0007  * any later version.                                                          *
0008  *                                                                             *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT *
0010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
0011  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    *
0012  * more details.                                                               *
0013  *                                                                             *
0014  * You should have received a copy of the GNU General Public License along     *
0015  * with this program. If not, see <http://www.gnu.org/licenses/>.              *
0016  *******************************************************************************/
0017 
0018 #include "textinputdialog.h"
0019 
0020 #include <QVBoxLayout>
0021 #include <QLabel>
0022 #include <QLineEdit>
0023 #include <QRegExpValidator>
0024 #include <QDialogButtonBox>
0025 #include <QPushButton>
0026 #include <QStyle>
0027 
0028 TextInputDialog::TextInputDialog(QWidget *parent)
0029     : QDialog(parent)
0030 {
0031     m_label = new QLabel;
0032     m_lineEdit = new QLineEdit;
0033 
0034     auto *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0035     connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0036     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0037     auto *okButon = buttonBox->button(QDialogButtonBox::Ok);
0038     okButon->setEnabled(false);
0039 
0040     connect(m_lineEdit, &QLineEdit::textEdited, this, [this, okButon]() {
0041         okButon->setEnabled(m_lineEdit->hasAcceptableInput());
0042     });
0043 
0044     auto *layout = new QVBoxLayout;
0045     layout->addWidget(m_label);
0046     layout->addWidget(m_lineEdit);
0047     layout->addSpacing(style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing));
0048     layout->addWidget(buttonBox);
0049     setLayout(layout);
0050 }
0051 
0052 void TextInputDialog::setLabelText(const QString &text)
0053 {
0054     m_label->setText(text);
0055 }
0056 
0057 QString TextInputDialog::textValue() const
0058 {
0059     return m_lineEdit->text();
0060 }
0061 
0062 void TextInputDialog::setTextValue(const QString &resolution)
0063 {
0064     m_lineEdit->setText(resolution);
0065 }
0066 
0067 void TextInputDialog::setValidator(const QValidator *v)
0068 {
0069     m_lineEdit->setValidator(v);
0070 }
0071 
0072 QString TextInputDialog::getText(QWidget *parent, const QString &title, const QString &label, const QString &text, const QValidator *v, bool *ok)
0073 {
0074     TextInputDialog dlg(parent);
0075     dlg.setWindowTitle(title);
0076     dlg.setLabelText(label);
0077     dlg.setTextValue(text);
0078     dlg.setValidator(v);
0079     *ok = dlg.exec();
0080     return dlg.textValue();
0081 }