-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
306 lines (258 loc) · 8.07 KB
/
MainWindow.cpp
File metadata and controls
306 lines (258 loc) · 8.07 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Demo
#include <MainWindow.hpp>
// QCodeEditor
#include <QCodeEditor>
#include <QGLSLCompleter>
#include <QLuaCompleter>
#include <QPythonCompleter>
#include <QSyntaxStyle>
#include <QCXXHighlighter>
#include <QGLSLHighlighter>
#include <QXMLHighlighter>
#include <QJSONHighlighter>
#include <QLuaHighlighter>
#include <QPythonHighlighter>
// Qt
#include <QCheckBox>
#include <QComboBox>
#include <QFile>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QSpinBox>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
m_setupLayout(nullptr),
m_codeSampleCombobox(nullptr),
m_highlighterCombobox(nullptr),
m_completerCombobox(nullptr),
m_styleCombobox(nullptr),
m_readOnlyCheckBox(nullptr),
m_wordWrapCheckBox(nullptr),
m_parenthesesEnabledCheckbox(nullptr),
m_tabReplaceEnabledCheckbox(nullptr),
m_tabReplaceNumberSpinbox(nullptr),
m_autoIndentationCheckbox(nullptr),
m_codeEditor(nullptr),
m_completers(),
m_highlighters(),
m_styles()
{
initData();
createWidgets();
setupWidgets();
performConnections();
}
void MainWindow::initData()
{
m_codeSamples = {
{"C++", loadCode(":/code_samples/cxx.cpp")},
{"GLSL", loadCode(":/code_samples/shader.glsl")},
{"XML", loadCode(":/code_samples/xml.xml")},
{"JSON", loadCode(":/code_samples/json.json")},
{"LUA", loadCode(":/code_samples/lua.lua")},
{"Python", loadCode(":/code_samples/python.py")}
};
m_completers = {
{"None", nullptr},
{"GLSL", new QGLSLCompleter(this)},
{"LUA", new QLuaCompleter(this)},
{"Python", new QPythonCompleter(this)},
};
m_highlighters = {
{"None", nullptr},
{"C++", new QCXXHighlighter},
{"GLSL", new QGLSLHighlighter},
{"XML", new QXMLHighlighter},
{"JSON", new QJSONHighlighter},
{"LUA", new QLuaHighlighter},
{"Python", new QPythonHighlighter},
};
m_styles = {
{"Default", QSyntaxStyle::defaultStyle()}
};
// Loading styles
loadStyle(":/styles/drakula.xml");
}
QString MainWindow::loadCode(QString path)
{
QFile fl(path);
if (!fl.open(QIODevice::ReadOnly))
{
return QString();
}
return fl.readAll();
}
void MainWindow::loadStyle(QString path)
{
QFile fl(path);
if (!fl.open(QIODevice::ReadOnly))
{
return;
}
auto style = new QSyntaxStyle(this);
if (!style->load(fl.readAll()))
{
delete style;
return;
}
m_styles.append({style->name(), style});
}
void MainWindow::createWidgets()
{
// Layout
auto container = new QWidget(this);
setCentralWidget(container);
auto hBox = new QHBoxLayout(container);
auto setupGroup = new QGroupBox("Setup", container);
hBox->addWidget(setupGroup);
m_setupLayout = new QVBoxLayout(setupGroup);
setupGroup->setLayout(m_setupLayout);
setupGroup->setMaximumWidth(300);
// CodeEditor
m_codeEditor = new QCodeEditor(this);
hBox->addWidget(m_codeEditor);
m_codeSampleCombobox = new QComboBox(setupGroup);
m_highlighterCombobox = new QComboBox(setupGroup);
m_completerCombobox = new QComboBox(setupGroup);
m_styleCombobox = new QComboBox(setupGroup);
m_readOnlyCheckBox = new QCheckBox("Read Only", setupGroup);
m_wordWrapCheckBox = new QCheckBox("Word Wrap", setupGroup);
m_parenthesesEnabledCheckbox = new QCheckBox("Auto Parentheses", setupGroup);
m_tabReplaceEnabledCheckbox = new QCheckBox("Tab Replace", setupGroup);
m_tabReplaceNumberSpinbox = new QSpinBox(setupGroup);
m_autoIndentationCheckbox = new QCheckBox("Auto Indentation", setupGroup);
// Adding widgets
m_setupLayout->addWidget(new QLabel(tr("Code sample"), setupGroup));
m_setupLayout->addWidget(m_codeSampleCombobox);
m_setupLayout->addWidget(new QLabel(tr("Completer"), setupGroup));
m_setupLayout->addWidget(m_completerCombobox);
m_setupLayout->addWidget(new QLabel(tr("Highlighter"), setupGroup));
m_setupLayout->addWidget(m_highlighterCombobox);
m_setupLayout->addWidget(new QLabel(tr("Style"), setupGroup));
m_setupLayout->addWidget(m_styleCombobox);
m_setupLayout->addWidget(m_readOnlyCheckBox);
m_setupLayout->addWidget(m_wordWrapCheckBox);
m_setupLayout->addWidget(m_parenthesesEnabledCheckbox);
m_setupLayout->addWidget(m_tabReplaceEnabledCheckbox);
m_setupLayout->addWidget(m_tabReplaceNumberSpinbox);
m_setupLayout->addWidget(m_autoIndentationCheckbox);
m_setupLayout->addSpacerItem(new QSpacerItem(1, 2, QSizePolicy::Minimum, QSizePolicy::Expanding));
}
void MainWindow::setupWidgets()
{
setWindowTitle("QCodeEditor Demo");
// CodeEditor
m_codeEditor->setPlainText (m_codeSamples[0].second);
m_codeEditor->setSyntaxStyle(m_styles[0].second);
m_codeEditor->setCompleter (m_completers[0].second);
m_codeEditor->setHighlighter(m_highlighters[0].second);
QStringList list;
// Code samples
for (auto&& el : m_codeSamples)
{
list << el.first;
}
m_codeSampleCombobox->addItems(list);
list.clear();
// Highlighter
for (auto&& el : m_highlighters)
{
list << el.first;
}
m_highlighterCombobox->addItems(list);
list.clear();
// Completer
for (auto&& el : m_completers)
{
list << el.first;
}
m_completerCombobox->addItems(list);
list.clear();
// Styles
for (auto&& el : m_styles)
{
list << el.first;
}
m_styleCombobox->addItems(list);
list.clear();
m_parenthesesEnabledCheckbox->setChecked(m_codeEditor->autoParentheses());
m_tabReplaceEnabledCheckbox->setChecked(m_codeEditor->tabReplace());
m_tabReplaceNumberSpinbox->setValue(m_codeEditor->tabReplaceSize());
m_tabReplaceNumberSpinbox->setSuffix(tr(" spaces"));
m_autoIndentationCheckbox->setChecked(m_codeEditor->autoIndentation());
m_wordWrapCheckBox->setChecked(m_codeEditor->wordWrapMode() != QTextOption::NoWrap);
}
void MainWindow::performConnections()
{
connect(
m_codeSampleCombobox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
[this](int index)
{ m_codeEditor->setPlainText(m_codeSamples[index].second); }
);
connect(
m_highlighterCombobox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
[this](int index)
{ m_codeEditor->setHighlighter(m_highlighters[index].second); }
);
connect(
m_completerCombobox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
[this](int index)
{ m_codeEditor->setCompleter(m_completers[index].second); }
);
connect(
m_styleCombobox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
[this](int index)
{ m_codeEditor->setSyntaxStyle(m_styles[index].second); }
);
connect(
m_readOnlyCheckBox,
&QCheckBox::stateChanged,
[this](int state)
{ m_codeEditor->setReadOnly(state != 0); }
);
connect(
m_wordWrapCheckBox,
&QCheckBox::stateChanged,
[this](int state)
{
if (state != 0)
{
m_codeEditor->setWordWrapMode(QTextOption::WordWrap);
}
else
{
m_codeEditor->setWordWrapMode(QTextOption::NoWrap);
}
}
);
connect(
m_parenthesesEnabledCheckbox,
&QCheckBox::stateChanged,
[this](int state)
{ m_codeEditor->setAutoParentheses(state != 0); }
);
connect(
m_tabReplaceEnabledCheckbox,
&QCheckBox::stateChanged,
[this](int state)
{ m_codeEditor->setTabReplace(state != 0); }
);
connect(
m_tabReplaceNumberSpinbox,
QOverload<int>::of(&QSpinBox::valueChanged),
[this](int value)
{ m_codeEditor->setTabReplaceSize(value); }
);
connect(
m_autoIndentationCheckbox,
&QCheckBox::stateChanged,
[this](int state)
{ m_codeEditor->setAutoIndentation(state != 0); }
);
}