-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
195 lines (163 loc) · 6.32 KB
/
Copy pathmain.cpp
File metadata and controls
195 lines (163 loc) · 6.32 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
#include <QApplication>
#include <QDebug>
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>
#include <QKeySequence>
#include <QCoreApplication>
#include <QDir>
#include <CodeEditor/CodeEditor.h>
#include <CodeEditor/EditorTheme.h>
#include <CodeEditor/PerformanceMonitorWindow.h>
namespace {
QStringList candidateThemeDirectories()
{
QStringList directories;
auto collectFrom = [&directories](const QString& startPath) {
if (startPath.isEmpty())
return;
QDir dir(startPath);
for (int depth = 0; depth < 6; ++depth) {
const QString repoThemes = dir.filePath(QStringLiteral("QCodeEngine_C/themes"));
if (QDir(repoThemes).exists())
directories.append(QDir(repoThemes).absolutePath());
const QString localThemes = dir.filePath(QStringLiteral("themes"));
if (QDir(localThemes).exists())
directories.append(QDir(localThemes).absolutePath());
const QString installedThemes = dir.filePath(QStringLiteral("../share/QCodeEngine_C/themes"));
if (QDir(installedThemes).exists())
directories.append(QDir(installedThemes).absolutePath());
if (!dir.cdUp())
break;
}
};
collectFrom(QCoreApplication::applicationDirPath());
collectFrom(QDir::currentPath());
directories.removeDuplicates();
return directories;
}
QString firstExistingThemeDirectory()
{
const QStringList directories = candidateThemeDirectories();
return directories.isEmpty() ? QDir::currentPath() : directories.first();
}
QString bundledThemePath(const QString& fileName)
{
const QStringList directories = candidateThemeDirectories();
for (const QString& directory : directories) {
const QString path = QDir(directory).filePath(fileName);
if (QFileInfo::exists(path))
return path;
}
return QString();
}
} // namespace
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QMainWindow mainWindow;
mainWindow.resize(1000, 720);
CodeEditor* editor = new CodeEditor(&mainWindow);
// Use Monokai Pro Light (Filter Sun)
editor->setTheme(QEditorTheme::own_theme());
const auto updateWindowTitle = [&mainWindow, editor]() {
const QString themeName = editor->theme().name.isEmpty()
? QStringLiteral("Untitled Theme")
: editor->theme().name;
mainWindow.setWindowTitle(QStringLiteral("QCodeEditor - %1").arg(themeName));
};
updateWindowTitle();
// QObject::connect(editor,&CodeEditor::)
// Configure features
editor->setLineNumbersVisible(true);
editor->setMiniMapVisible(true);
editor->setFoldingEnabled(true);
editor->setAutoCompleteEnabled(true);
editor->setBracketPairGuidesEnabled(true);
editor->setTabWidth(4);
editor->setInsertSpacesOnTab(true);
editor->setText(
"#include <stdio.h>\n"
"#include <stdint.h>\n"
"\n"
"typedef struct {\n"
" const char *name;\n"
" uint32_t count;\n"
"} Item;\n"
"\n"
"static void print_item(const Item *item)\n"
"{\n"
" if (!item) {\n"
" return;\n"
" }\n"
"\n"
" printf(\"%s: %u\\n\", item->name, item->count);\n"
"}\n"
"\n"
"int main(void)\n"
"{\n"
" Item item = { \"widgets\", 42 };\n"
" print_item(&item);\n"
" return 0;\n"
"}\n");
mainWindow.setCentralWidget(editor);
QMenuBar* menuBar = mainWindow.menuBar();
QMenu* fileMenu = menuBar->addMenu("&File");
QMenu* viewMenu = menuBar->addMenu("&View");
QMenu* themeMenu = menuBar->addMenu("&Theme");
QAction* openAction = fileMenu->addAction("&Open...");
openAction->setShortcut(QKeySequence::Open);
QAction* toggleMinimapAction = viewMenu->addAction("Toggle &Minimap");
toggleMinimapAction->setCheckable(true);
toggleMinimapAction->setChecked(true);
toggleMinimapAction->setShortcut(QKeySequence(QStringLiteral("Alt+M")));
QAction* vercelDarkAction = themeMenu->addAction("Use &Vercel Dark");
QAction* loadThemeAction = themeMenu->addAction("&Load Theme JSON...");
QAction* resetThemeAction = themeMenu->addAction("&Reset Demo Theme");
QObject::connect(openAction, &QAction::triggered, [&mainWindow, editor]() {
QString fileName = QFileDialog::getOpenFileName(&mainWindow, "Open File", "", "All Files (*)");
if (!fileName.isEmpty()) {
if (!editor->loadFile(fileName)) {
QMessageBox::warning(&mainWindow, "Error", "Could not open file.");
}
}
});
QObject::connect(toggleMinimapAction, &QAction::toggled, editor, &CodeEditor::setMiniMapVisible);
QObject::connect(vercelDarkAction, &QAction::triggered, [&mainWindow, editor, updateWindowTitle]() {
const QString path = bundledThemePath(QStringLiteral("vercel_dark.json"));
if (path.isEmpty()) {
QMessageBox::warning(&mainWindow, "Theme Not Found",
"Could not locate bundled theme file: vercel_dark.json");
return;
}
editor->setThemeFromFile(path);
updateWindowTitle();
});
QObject::connect(loadThemeAction, &QAction::triggered, [&mainWindow, editor, updateWindowTitle]() {
const QString path = QFileDialog::getOpenFileName(
&mainWindow,
"Load Theme JSON",
firstExistingThemeDirectory(),
"JSON Theme (*.json)");
if (path.isEmpty())
return;
editor->setThemeFromFile(path);
updateWindowTitle();
});
QObject::connect(resetThemeAction, &QAction::triggered, [editor, updateWindowTitle]() {
editor->setTheme(QEditorTheme::own_theme());
updateWindowTitle();
});
// Optional: observe function-jump events from Ctrl+Shift+O popup.
QObject::connect(editor, &CodeEditor::functionSelected, [](int line) {
qDebug() << "Jumped to line" << line;
});
mainWindow.show();
PerformanceMonitorWindow* perfWindow = new PerformanceMonitorWindow();
perfWindow->setGeometry(mainWindow.x() + mainWindow.width() + 10, mainWindow.y(), 900, 650);
perfWindow->show();
return app.exec();
}