1 /*
2  * DesktopInputDialog.cpp
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 
16 #include "DesktopInputDialog.hpp"
17 #include "DesktopUtils.hpp"
18 #include "ui_DesktopInputDialog.h"
19 
20 #include <QPushButton>
21 
InputDialog(QWidget * parent)22 InputDialog::InputDialog(QWidget *parent) :
23     QDialog(parent),
24     ui(new Ui::InputDialog()),
25     pOK_(nullptr),
26     required_(true)
27 {
28    ui->setupUi(this);
29    setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
30 
31    pOK_ = new QPushButton(QString::fromUtf8("OK"));
32    ui->buttonBox->addButton(pOK_, QDialogButtonBox::AcceptRole);
33 
34    QPushButton* pCancel = new QPushButton(QString::fromUtf8("Cancel"));
35    ui->buttonBox->addButton(pCancel, QDialogButtonBox::RejectRole);
36 
37    ui->extraOption->setVisible(false);
38 }
39 
~InputDialog()40 InputDialog::~InputDialog()
41 {
42    delete ui;
43 }
44 
caption()45 QString InputDialog::caption()
46 {
47    return ui->label->text();
48 }
49 
setCaption(const QString & caption)50 void InputDialog::setCaption(const QString& caption)
51 {
52    ui->label->setText(caption);
53    ui->lineEdit->setAccessibleName(caption);
54 }
55 
textValue()56 QString InputDialog::textValue()
57 {
58    return ui->lineEdit->text();
59 }
60 
setTextValue(const QString & value)61 void InputDialog::setTextValue(const QString& value)
62 {
63    ui->lineEdit->setText(value);
64 }
65 
setSelection(int offset,int length)66 void InputDialog::setSelection(int offset, int length)
67 {
68    offset = std::min(offset, textValue().size());
69    length = std::min(length,
70                      textValue().size() - offset);
71 
72    ui->lineEdit->setSelection(offset, length);
73 }
74 
setOkButtonLabel(const QString & label)75 void InputDialog::setOkButtonLabel(const QString& label)
76 {
77    pOK_->setText(label);
78 }
79 
setEchoMode(QLineEdit::EchoMode mode)80 void InputDialog::setEchoMode(QLineEdit::EchoMode mode)
81 {
82    ui->lineEdit->setEchoMode(mode);
83 }
84 
setNumbersOnly(bool numbersOnly)85 void InputDialog::setNumbersOnly(bool numbersOnly)
86 {
87    if (numbersOnly)
88       ui->lineEdit->setInputMask(QString::fromUtf8("D99999999"));
89    else
90       ui->lineEdit->setInputMask(QString());
91 }
92 
setRequired(bool required)93 void InputDialog::setRequired(bool required)
94 {
95    required_ = required;
96 }
97 
setExtraOptionPrompt(const QString & prompt)98 void InputDialog::setExtraOptionPrompt(const QString& prompt)
99 {
100    ui->extraOption->setVisible(!prompt.isEmpty());
101    ui->extraOption->setText(prompt);
102 }
103 
setExtraOption(bool extraOption)104 void InputDialog::setExtraOption(bool extraOption)
105 {
106    ui->extraOption->setCheckState(Qt::Checked);
107 }
108 
extraOption()109 bool InputDialog::extraOption()
110 {
111    return ui->extraOption->checkState() == Qt::Checked;
112 }
113 
done(int r)114 void InputDialog::done(int r)
115 {
116    if (QDialog::Accepted == r && required_) // ok was pressed
117    {
118       if (ui->lineEdit->text().size() == 0)
119       {
120          rstudio::desktop::showWarning(this,
121                      QString::fromUtf8("Error"),
122                      QString::fromUtf8("You must enter a value."),
123                      QString());
124          return;
125        }
126    }
127    QDialog::done(r);
128 }
129