This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptgenwindow.cpp
154 lines (121 loc) · 4.89 KB
/
scriptgenwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "scriptgenwindow.h"
#include <QtCore/QVariant>
#include <QJsonObject>
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
#include <QCoreApplication>
#include "mainwindow.h"
ScriptGenWindow::ScriptGenWindow(QString *basePath) : QWizard()
{
this->setupUi();
QFile file(*basePath + "/Scripts/Scripts.json");
file.open(QFile::ReadOnly);
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
file.close();
QVariantMap mainMap = (doc.object()).toVariantMap();
QVariantList localScripts = mainMap["Scripts"].toList();
foreach (QVariant variant, localScripts) {
QVariantMap map = variant.toMap();
Script script;
script.name = map["name"].toString();
script.description = map["description"].toString();
script.path = map["path"].toString();
scripts.append(script);
QListWidgetItem *item = new QListWidgetItem(script.name);
item->setToolTip(script.description);
generatorsList->addItem(item);
}
this->basePath = *basePath;
}
void ScriptGenWindow::setupUi()
{
this->resize(400, 300);
this->setWizardStyle(QWizard::NStyles);
this->setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
this->setWindowTitle("Script Generator");
wizardPage1 = new QWizardPage();
wizardPage1->setObjectName(QString::fromUtf8("wizardPage1"));
generatorsList = new QListWidget(wizardPage1);
generatorsList->setGeometry(QRect(0, 0, 375, 250));
connect(generatorsList, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(itemClicked()));
this->addPage(wizardPage1);
wizardPage2 = new QWizardPage();
wizardPage2->setObjectName(QString::fromUtf8("wizardPage2"));
enabled = new QCheckBox(wizardPage2);
enabled->setObjectName(QString::fromUtf8("enabled"));
enabled->setGeometry(QRect(0, 0, 61, 17));
connect(enabled, SIGNAL(toggled(bool)), this, SLOT(toggleComponent(bool)));
componentList = new QListWidget(wizardPage2);
componentList->setObjectName(QString::fromUtf8("componentList"));
componentList->setGeometry(QRect(100, 0, 271, 251));
connect(componentList, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(itemClicked()));
this->addPage(wizardPage2);
connect(button(WizardButton::NextButton), SIGNAL(clicked()), this, SLOT(nextClicked()));
connect(button(WizardButton::FinishButton), SIGNAL(clicked()), this, SLOT(finishClicked()));
this->button(WizardButton::NextButton)->setEnabled(false);
this->enabled->setEnabled(false);
}
void ScriptGenWindow::toggleComponent(bool enabled)
{
Component selectedComponent = components.at(componentList->selectionModel()->currentIndex().row());
selectedComponent.enabled = enabled;
components.removeAt(componentList->selectionModel()->currentIndex().row());
components.insert(componentList->selectionModel()->currentIndex().row(), selectedComponent);
}
void ScriptGenWindow::itemClicked()
{
if(currentId() == 0) {
this->button(WizardButton::NextButton)->setEnabled(true);
componentList->clear();
} else {
Component selectedComponent = components.at(componentList->selectionModel()->currentIndex().row());
if(!selectedComponent.name.startsWith("#main")) {
enabled->setEnabled(true);
if(selectedComponent.enabled) {
enabled->setCheckState(Qt::CheckState::Checked);
} else {
enabled->setCheckState(Qt::CheckState::Unchecked);
}
} else {
enabled->setEnabled(false);
}
}
}
void ScriptGenWindow::nextClicked()
{
components.clear();
componentList->clear();
QFile file(basePath + "/Scripts/Data/" + scripts.at(generatorsList->selectionModel()->currentIndex().row()).path);
file.open(QFile::ReadOnly);
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
file.close();
QVariantMap mainMap = (doc.object()).toVariantMap();
QVariantList localComponents = mainMap["Components"].toList();
foreach (QVariant variant, localComponents) {
QVariantMap map = variant.toMap();
Component component;
component.name = map["name"].toString();
component.text = map["text"].toString();
components.append(component);
if(!component.name.startsWith("#main")) {
componentList->addItem(map["name"].toString());
} else {
QListWidgetItem *item = new QListWidgetItem();
item->setForeground(QBrush(QColor(160, 160, 160)));
item->setText("Main script");
componentList->addItem(item);
}
}
}
void ScriptGenWindow::finishClicked()
{
QString finalString;
foreach(Component component, components) {
if(component.enabled) {
finalString.append(component.text + '\n');
}
}
MainWindow::getInstance()->insertText(finalString);
qDebug() << finalString;
}