1 /*
2     Actiona
3     Copyright (C) 2005 Jonathan Mercier-Ganady
4 
5     Actiona is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     Actiona is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18     Contact : jmgr@jmgr.info
19 */
20 
21 #include "resourcenamedialog.h"
22 #include "ui_resourcenamedialog.h"
23 #include "actioninstance.h"
24 #include "script.h"
25 
26 #include <QRegExpValidator>
27 #include <QPushButton>
28 
29 namespace ActionTools
30 {
ResourceNameDialog(ActionTools::Script * script,QWidget * parent)31     ResourceNameDialog::ResourceNameDialog(ActionTools::Script *script, QWidget *parent)
32       : QDialog(parent),
33         ui(new Ui::ResourceNameDialog),
34         mScript(script)
35     {
36         ui->setupUi(this);
37 
38         connect(ui->resourceNameLineEdit, &QLineEdit::textChanged, this, &ResourceNameDialog::onTextChanged);
39 
40         ui->resourceNameLineEdit->setValidator(new QRegExpValidator(ActionTools::ActionInstance::NameRegExp, ui->resourceNameLineEdit));
41         ui->resourceNameLineEdit->setFocus();
42 
43         ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
44     }
45 
~ResourceNameDialog()46     ResourceNameDialog::~ResourceNameDialog()
47     {
48         delete ui;
49     }
50 
resourceName() const51     QString ResourceNameDialog::resourceName() const
52     {
53         return ui->resourceNameLineEdit->text();
54     }
55 
accept()56     void ResourceNameDialog::accept()
57     {
58         if(!acceptable())
59             return;
60 
61         QDialog::accept();
62     }
63 
onTextChanged(const QString & text)64     void ResourceNameDialog::onTextChanged(const QString &text)
65     {
66         Q_UNUSED(text)
67 
68         ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable());
69     }
70 
acceptable() const71     bool ResourceNameDialog::acceptable() const
72     {
73         QString resultResourceName = resourceName();
74 
75         return (!resultResourceName.isEmpty() && !mScript->hasResource(resourceName()));
76     }
77 }
78