| 1 | // Demo |
| 2 | #include <MainWindow.hpp> |
| 3 | |
| 4 | // QCodeEditor |
| 5 | #include <QCodeEditor> |
| 6 | #include <QGLSLCompleter> |
| 7 | #include <QLuaCompleter> |
| 8 | #include <QPythonCompleter> |
| 9 | #include <QSyntaxStyle> |
| 10 | #include <QCXXHighlighter> |
| 11 | #include <QGLSLHighlighter> |
| 12 | #include <QXMLHighlighter> |
| 13 | #include <QJSONHighlighter> |
| 14 | #include <QLuaHighlighter> |
| 15 | #include <QPythonHighlighter> |
| 16 | |
| 17 | // Qt |
| 18 | #include <QComboBox> |
| 19 | #include <QVBoxLayout> |
| 20 | #include <QHBoxLayout> |
| 21 | #include <QCheckBox> |
| 22 | #include <QSpinBox> |
| 23 | #include <QGroupBox> |
| 24 | #include <QLabel> |
| 25 | |
| 26 | MainWindow::MainWindow(QWidget* parent) : |
| 27 | QMainWindow(parent), |
| 28 | m_setupLayout(nullptr), |
| 29 | m_codeSampleCombobox(nullptr), |
| 30 | m_highlighterCombobox(nullptr), |
| 31 | m_completerCombobox(nullptr), |
| 32 | m_styleCombobox(nullptr), |
| 33 | m_readOnlyCheckBox(nullptr), |
| 34 | m_wordWrapCheckBox(nullptr), |
| 35 | m_parenthesesEnabledCheckbox(nullptr), |
| 36 | m_tabReplaceEnabledCheckbox(nullptr), |
| 37 | m_tabReplaceNumberSpinbox(nullptr), |
| 38 | m_autoIndentationCheckbox(nullptr), |
| 39 | m_codeEditor(nullptr), |
| 40 | m_completers(), |
| 41 | m_highlighters(), |
| 42 | m_styles() |
| 43 | { |
| 44 | initData(); |
| 45 | createWidgets(); |
| 46 | setupWidgets(); |
| 47 | performConnections(); |
| 48 | } |
| 49 | |
| 50 | void MainWindow::initData() |
| 51 | { |
| 52 | m_codeSamples = { |
| 53 | {"C++", loadCode(":/code_samples/cxx.cpp")}, |
| 54 | {"GLSL", loadCode(":/code_samples/shader.glsl")}, |
| 55 | {"XML", loadCode(":/code_samples/xml.xml")}, |
| 56 | {"JSON", loadCode(":/code_samples/json.json")}, |
| 57 | {"LUA", loadCode(":/code_samples/lua.lua")}, |
| 58 | {"Python", loadCode(":/code_samples/python.py")} |
| 59 | }; |
| 60 | |
| 61 | m_completers = { |
| 62 | {"None", nullptr}, |
| 63 | {"GLSL", new QGLSLCompleter(this)}, |
| 64 | {"LUA", new QLuaCompleter(this)}, |
| 65 | {"Python", new QPythonCompleter(this)}, |
| 66 | }; |
| 67 | |
| 68 | m_highlighters = { |
| 69 | {"None", nullptr}, |
| 70 | {"C++", new QCXXHighlighter}, |
| 71 | {"GLSL", new QGLSLHighlighter}, |
| 72 | {"XML", new QXMLHighlighter}, |
| 73 | {"JSON", new QJSONHighlighter}, |
| 74 | {"LUA", new QLuaHighlighter}, |
| 75 | {"Python", new QPythonHighlighter}, |
| 76 | }; |
| 77 | |
| 78 | m_styles = { |
| 79 | {"Default", QSyntaxStyle::defaultStyle()} |
| 80 | }; |
| 81 | |
| 82 | // Loading styles |
| 83 | loadStyle(":/styles/drakula.xml"); |
| 84 | } |
| 85 | |
| 86 | QString MainWindow::loadCode(QString path) |
| 87 | { |
| 88 | QFile fl(path); |
| 89 | |
| 90 | if (!fl.open(QIODevice::ReadOnly)) |
| 91 | { |
| 92 | return QString(); |
| 93 | } |
| 94 | |
| 95 | return fl.readAll(); |
| 96 | } |
| 97 | |
| 98 | void MainWindow::loadStyle(QString path) |
| 99 | { |
| 100 | QFile fl(path); |
| 101 | |
| 102 | if (!fl.open(QIODevice::ReadOnly)) |
| 103 | { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | auto style = new QSyntaxStyle(this); |
| 108 | |
| 109 | if (!style->load(fl.readAll())) |
| 110 | { |
| 111 | delete style; |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | m_styles.append({style->name(), style}); |
| 116 | } |
| 117 | |
| 118 | void MainWindow::createWidgets() |
| 119 | { |
| 120 | // Layout |
| 121 | auto container = new QWidget(this); |
| 122 | setCentralWidget(container); |
| 123 | |
| 124 | auto hBox = new QHBoxLayout(container); |
| 125 | |
| 126 | auto setupGroup = new QGroupBox("Setup", container); |
| 127 | hBox->addWidget(setupGroup); |
| 128 | |
| 129 | m_setupLayout = new QVBoxLayout(setupGroup); |
| 130 | setupGroup->setLayout(m_setupLayout); |
| 131 | setupGroup->setMaximumWidth(300); |
| 132 | |
| 133 | // CodeEditor |
| 134 | m_codeEditor = new QCodeEditor(this); |
| 135 | hBox->addWidget(m_codeEditor); |
| 136 | |
| 137 | m_codeSampleCombobox = new QComboBox(setupGroup); |
| 138 | m_highlighterCombobox = new QComboBox(setupGroup); |
| 139 | m_completerCombobox = new QComboBox(setupGroup); |
| 140 | m_styleCombobox = new QComboBox(setupGroup); |
| 141 | |
| 142 | m_readOnlyCheckBox = new QCheckBox("Read Only", setupGroup); |
| 143 | m_wordWrapCheckBox = new QCheckBox("Word Wrap", setupGroup); |
| 144 | m_parenthesesEnabledCheckbox = new QCheckBox("Auto Parentheses", setupGroup); |
| 145 | m_tabReplaceEnabledCheckbox = new QCheckBox("Tab Replace", setupGroup); |
| 146 | m_tabReplaceNumberSpinbox = new QSpinBox(setupGroup); |
| 147 | m_autoIndentationCheckbox = new QCheckBox("Auto Indentation", setupGroup); |
| 148 | |
| 149 | |
| 150 | // Adding widgets |
| 151 | m_setupLayout->addWidget(new QLabel(tr("Code sample"), setupGroup)); |
| 152 | m_setupLayout->addWidget(m_codeSampleCombobox); |
| 153 | m_setupLayout->addWidget(new QLabel(tr("Completer"), setupGroup)); |
| 154 | m_setupLayout->addWidget(m_completerCombobox); |
| 155 | m_setupLayout->addWidget(new QLabel(tr("Highlighter"), setupGroup)); |
| 156 | m_setupLayout->addWidget(m_highlighterCombobox); |
| 157 | m_setupLayout->addWidget(new QLabel(tr("Style"), setupGroup)); |
| 158 | m_setupLayout->addWidget(m_styleCombobox); |
| 159 | m_setupLayout->addWidget(m_readOnlyCheckBox); |
| 160 | m_setupLayout->addWidget(m_wordWrapCheckBox); |
| 161 | m_setupLayout->addWidget(m_parenthesesEnabledCheckbox); |
| 162 | m_setupLayout->addWidget(m_tabReplaceEnabledCheckbox); |
| 163 | m_setupLayout->addWidget(m_tabReplaceNumberSpinbox); |
| 164 | m_setupLayout->addWidget(m_autoIndentationCheckbox); |
| 165 | m_setupLayout->addSpacerItem(new QSpacerItem(1, 2, QSizePolicy::Minimum, QSizePolicy::Expanding)); |
| 166 | } |
| 167 | |
| 168 | void MainWindow::setupWidgets() |
| 169 | { |
| 170 | setWindowTitle("QCodeEditor Demo"); |
| 171 | |
| 172 | // CodeEditor |
| 173 | m_codeEditor->setPlainText (m_codeSamples[0].second); |
| 174 | m_codeEditor->setSyntaxStyle(m_styles[0].second); |
| 175 | m_codeEditor->setCompleter (m_completers[0].second); |
| 176 | m_codeEditor->setHighlighter(m_highlighters[0].second); |
| 177 | |
| 178 | QStringList list; |
| 179 | // Code samples |
| 180 | for (auto&& el : m_codeSamples) |
| 181 | { |
| 182 | list << el.first; |
| 183 | } |
| 184 | |
| 185 | m_codeSampleCombobox->addItems(list); |
| 186 | list.clear(); |
| 187 | |
| 188 | // Highlighter |
| 189 | for (auto&& el : m_highlighters) |
| 190 | { |
| 191 | list << el.first; |
| 192 | } |
| 193 | |
| 194 | m_highlighterCombobox->addItems(list); |
| 195 | list.clear(); |
| 196 | |
| 197 | // Completer |
| 198 | for (auto&& el : m_completers) |
| 199 | { |
| 200 | list << el.first; |
| 201 | } |
| 202 | |
| 203 | m_completerCombobox->addItems(list); |
| 204 | list.clear(); |
| 205 | |
| 206 | // Styles |
| 207 | for (auto&& el : m_styles) |
| 208 | { |
| 209 | list << el.first; |
| 210 | } |
| 211 | |
| 212 | m_styleCombobox->addItems(list); |
| 213 | list.clear(); |
| 214 | |
| 215 | m_parenthesesEnabledCheckbox->setChecked(m_codeEditor->autoParentheses()); |
| 216 | m_tabReplaceEnabledCheckbox->setChecked(m_codeEditor->tabReplace()); |
| 217 | m_tabReplaceNumberSpinbox->setValue(m_codeEditor->tabReplaceSize()); |
| 218 | m_tabReplaceNumberSpinbox->setSuffix(tr(" spaces")); |
| 219 | m_autoIndentationCheckbox->setChecked(m_codeEditor->autoIndentation()); |
| 220 | |
| 221 | m_wordWrapCheckBox->setChecked(m_codeEditor->wordWrapMode() != QTextOption::NoWrap); |
| 222 | |
| 223 | } |
| 224 | |
| 225 | void MainWindow::performConnections() |
| 226 | { |
| 227 | connect( |
| 228 | m_codeSampleCombobox, |
| 229 | QOverload<int>::of(&QComboBox::currentIndexChanged), |
| 230 | [this](int index) |
| 231 | { m_codeEditor->setPlainText(m_codeSamples[index].second); } |
| 232 | ); |
| 233 | |
| 234 | connect( |
| 235 | m_highlighterCombobox, |
| 236 | QOverload<int>::of(&QComboBox::currentIndexChanged), |
| 237 | [this](int index) |
| 238 | { m_codeEditor->setHighlighter(m_highlighters[index].second); } |
| 239 | ); |
| 240 | |
| 241 | connect( |
| 242 | m_completerCombobox, |
| 243 | QOverload<int>::of(&QComboBox::currentIndexChanged), |
| 244 | [this](int index) |
| 245 | { m_codeEditor->setCompleter(m_completers[index].second); } |
| 246 | ); |
| 247 | |
| 248 | connect( |
| 249 | m_styleCombobox, |
| 250 | QOverload<int>::of(&QComboBox::currentIndexChanged), |
| 251 | [this](int index) |
| 252 | { m_codeEditor->setSyntaxStyle(m_styles[index].second); } |
| 253 | ); |
| 254 | |
| 255 | connect( |
| 256 | m_readOnlyCheckBox, |
| 257 | &QCheckBox::stateChanged, |
| 258 | [this](int state) |
| 259 | { m_codeEditor->setReadOnly(state != 0); } |
| 260 | ); |
| 261 | |
| 262 | connect( |
| 263 | m_wordWrapCheckBox, |
| 264 | &QCheckBox::stateChanged, |
| 265 | [this](int state) |
| 266 | { |
| 267 | if (state != 0) |
| 268 | { |
| 269 | m_codeEditor->setWordWrapMode(QTextOption::WordWrap); |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | m_codeEditor->setWordWrapMode(QTextOption::NoWrap); |
| 274 | } |
| 275 | } |
| 276 | ); |
| 277 | |
| 278 | connect( |
| 279 | m_parenthesesEnabledCheckbox, |
| 280 | &QCheckBox::stateChanged, |
| 281 | [this](int state) |
| 282 | { m_codeEditor->setAutoParentheses(state != 0); } |
| 283 | ); |
| 284 | |
| 285 | connect( |
| 286 | m_tabReplaceEnabledCheckbox, |
| 287 | &QCheckBox::stateChanged, |
| 288 | [this](int state) |
| 289 | { m_codeEditor->setTabReplace(state != 0); } |
| 290 | ); |
| 291 | |
| 292 | connect( |
| 293 | m_tabReplaceNumberSpinbox, |
| 294 | QOverload<int>::of(&QSpinBox::valueChanged), |
| 295 | [this](int value) |
| 296 | { m_codeEditor->setTabReplaceSize(value); } |
| 297 | ); |
| 298 | |
| 299 | connect( |
| 300 | m_autoIndentationCheckbox, |
| 301 | &QCheckBox::stateChanged, |
| 302 | [this](int state) |
| 303 | { m_codeEditor->setAutoIndentation(state != 0); } |
| 304 | ); |
| 305 | } |