-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor.cpp
More file actions
432 lines (384 loc) · 15.8 KB
/
Copy pathEditor.cpp
File metadata and controls
432 lines (384 loc) · 15.8 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include "Editor.h"
#include <QFileInfo>
#include <QTemporaryFile>
#include <QDir>
Editor::Editor(QWidget *parent)
: QMainWindow(parent), isDarkTheme(true), isModified(false)
{
themeManager = new ThemeManager();
compileProcess = new QProcess(this);
setupUI();
setupMenuBar();
setupToolBar();
setupStatusBar();
setupOutputPanel();
loadSettings();
applyTheme();
autoSaveTimer = new QTimer(this);
connect(autoSaveTimer, &QTimer::timeout, this, [this]() {
CodeEditor *ed = currentEditor();
if (ed && !ed->filePath.isEmpty() && ed->document()->isModified()) {
saveFile();
statusLabel->setText("Auto Saved");
}
});
autoSaveTimer->start(30000);
connect(compileProcess, &QProcess::readyReadStandardOutput, this, [this]() {
outputPanel->appendPlainText(compileProcess->readAllStandardOutput());
});
connect(compileProcess, &QProcess::readyReadStandardError, this, [this]() {
outputPanel->appendPlainText(compileProcess->readAllStandardError());
});
setWindowTitle("Code Editor");
resize(1200, 750);
}
Editor::~Editor() { delete themeManager; }
CodeEditor* Editor::currentEditor() {
return qobject_cast<CodeEditor*>(tabWidget->currentWidget());
}
void Editor::setupUI() {
tabWidget = new QTabWidget(this);
tabWidget->setTabsClosable(true);
tabWidget->setMovable(true);
connect(tabWidget, &QTabWidget::tabCloseRequested, this, &Editor::closeTab);
setCentralWidget(tabWidget);
newTab();
}
void Editor::newTab() {
CodeEditor *ed = new CodeEditor(this);
connect(ed->document(), &QTextDocument::modificationChanged,
this, &Editor::documentModified);
connect(ed, &CodeEditor::cursorPositionChanged,
this, &Editor::updateStatusBar);
int idx = tabWidget->addTab(ed, "Untitled");
tabWidget->setCurrentIndex(idx);
}
void Editor::closeTab(int index) {
CodeEditor *ed = qobject_cast<CodeEditor*>(tabWidget->widget(index));
if (ed && ed->document()->isModified()) {
auto btn = QMessageBox::warning(this, "Unsaved Changes",
"Save before closing this tab?",
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (btn == QMessageBox::Cancel) return;
if (btn == QMessageBox::Save) {
tabWidget->setCurrentIndex(index);
saveFile();
}
}
tabWidget->removeTab(index);
if (tabWidget->count() == 0) newTab();
}
void Editor::setupOutputPanel() {
outputDock = new QDockWidget("Output", this);
outputPanel = new QPlainTextEdit(outputDock);
outputPanel->setReadOnly(true);
outputPanel->setMaximumHeight(180);
QFont f("Cascadia Code", 11);
f.setStyleHint(QFont::Monospace);
outputPanel->setFont(f);
outputDock->setWidget(outputPanel);
outputDock->setAllowedAreas(Qt::BottomDockWidgetArea);
addDockWidget(Qt::BottomDockWidgetArea, outputDock);
outputDock->hide();
}
void Editor::setupMenuBar() {
QMenu *fileMenu = menuBar()->addMenu("&File");
fileMenu->addAction("New Tab", this, &Editor::newTab, QKeySequence::New);
fileMenu->addAction("Open...", this, &Editor::openFile, QKeySequence::Open);
fileMenu->addSeparator();
fileMenu->addAction("Save", this, &Editor::saveFile, QKeySequence::Save);
fileMenu->addAction("Save As...",this, &Editor::saveFileAs, QKeySequence::SaveAs);
fileMenu->addSeparator();
fileMenu->addAction("Exit", this, &QWidget::close, QKeySequence::Quit);
QMenu *editMenu = menuBar()->addMenu("&Edit");
editMenu->addAction("Undo", this, [this](){ if(currentEditor()) currentEditor()->undo(); }, QKeySequence::Undo);
editMenu->addAction("Redo", this, [this](){ if(currentEditor()) currentEditor()->redo(); }, QKeySequence::Redo);
editMenu->addSeparator();
editMenu->addAction("Cut", this, [this](){ if(currentEditor()) currentEditor()->cut(); }, QKeySequence::Cut);
editMenu->addAction("Copy", this, [this](){ if(currentEditor()) currentEditor()->copy(); }, QKeySequence::Copy);
editMenu->addAction("Paste", this, [this](){ if(currentEditor()) currentEditor()->paste(); },QKeySequence::Paste);
editMenu->addSeparator();
editMenu->addAction("Find & Replace...", this, &Editor::showFindReplace, QKeySequence::Find);
QMenu *viewMenu = menuBar()->addMenu("&View");
viewMenu->addAction("Toggle Dark/Light Theme", this, &Editor::toggleTheme,
QKeySequence(Qt::CTRL | Qt::Key_T));
viewMenu->addAction("Change Font...", this, &Editor::changeFont);
viewMenu->addAction("Toggle Output Panel", this, [this](){
outputDock->setVisible(!outputDock->isVisible());
});
QMenu *runMenu = menuBar()->addMenu("&Run");
runMenu->addAction("▶ Run Code", this, &Editor::runCode, QKeySequence(Qt::CTRL | Qt::Key_R));
}
void Editor::setupToolBar() {
toolBar = addToolBar("Main Toolbar");
toolBar->setMovable(false);
toolBar->addAction("New", this, &Editor::newTab);
toolBar->addAction("Open", this, &Editor::openFile);
toolBar->addAction("Save", this, &Editor::saveFile);
toolBar->addSeparator();
toolBar->addAction("Find", this, &Editor::showFindReplace);
toolBar->addSeparator();
toolBar->addAction("▶ Run", this, &Editor::runCode);
toolBar->addSeparator();
toolBar->addAction("Theme", this, &Editor::toggleTheme);
toolBar->addSeparator();
toolBar->addWidget(new QLabel(" Size: "));
fontSizeBox = new QComboBox(this);
for (int sz : {10,11,12,13,14,16,18,20,24,28,32})
fontSizeBox->addItem(QString::number(sz), sz);
fontSizeBox->setCurrentText("14");
connect(fontSizeBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
[this](int idx){ changeFontSize(fontSizeBox->itemData(idx).toInt()); });
toolBar->addWidget(fontSizeBox);
}
void Editor::setupStatusBar() {
statusLabel = new QLabel("Ready");
lineColLabel = new QLabel("Ln 1, Col 1");
fileLabel = new QLabel("Untitled");
statusBar()->addWidget(statusLabel, 1);
statusBar()->addPermanentWidget(fileLabel);
statusBar()->addPermanentWidget(lineColLabel);
}
void Editor::runCode() {
CodeEditor *ed = currentEditor();
if (!ed) return;
// Save first
if (ed->document()->isModified() || ed->filePath.isEmpty()) {
if (!saveFile()) return;
}
QString filePath = ed->filePath;
if (!filePath.endsWith(".cpp") && !filePath.endsWith(".c")) {
outputPanel->setPlainText("⚠ Only C/C++ files can be compiled.");
outputDock->show();
return;
}
outputDock->show();
outputPanel->clear();
outputPanel->appendPlainText("▶ Compiling: " + filePath + "\n");
QString outFile = QFileInfo(filePath).absolutePath() + "/" +
QFileInfo(filePath).baseName();
#ifdef Q_OS_WIN
outFile += ".exe";
#endif
QStringList args = { filePath, "-o", outFile, "-std=c++17", "-Wall" };
compileProcess->disconnect();
connect(compileProcess,
QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this, outFile](int code, QProcess::ExitStatus) {
if (code == 0) {
outputPanel->appendPlainText("\n✅ Compiled! Running...\n─────────────────");
QProcess *run = new QProcess(this);
run->setProcessChannelMode(QProcess::MergedChannels);
connect(run, &QProcess::readyRead, this, [this, run](){
outputPanel->appendPlainText(run->readAll());
});
connect(run, QOverload<int,QProcess::ExitStatus>::of(&QProcess::finished),
this, [this](int exitCode, QProcess::ExitStatus){
outputPanel->appendPlainText(
QString("\n─────────────────\n✔ Exited with code %1").arg(exitCode));
});
run->start(outFile, {});
} else {
outputPanel->appendPlainText("\n❌ Compilation failed.");
}
});
connect(compileProcess, &QProcess::readyReadStandardError, this, [this](){
outputPanel->appendPlainText(compileProcess->readAllStandardError());
});
compileProcess->start("g++", args);
}
void Editor::newFile() { newTab(); }
void Editor::openFile() {
QString path = QFileDialog::getOpenFileName(this, "Open File", {},
"C++ Files (*.cpp *.h *.hpp *.c);;All Files (*)");
if (path.isEmpty()) return;
QFile f(path);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Cannot open:\n" + path);
return;
}
QTextStream in(&f);
newTab();
CodeEditor *ed = currentEditor();
ed->setPlainText(in.readAll());
ed->filePath = path;
ed->document()->setModified(false);
tabWidget->setTabText(tabWidget->currentIndex(), QFileInfo(path).fileName());
setCurrentFile(path);
}
bool Editor::saveFile() {
CodeEditor *ed = currentEditor();
if (!ed) return false;
if (ed->filePath.isEmpty()) return saveFileAs();
QFile f(ed->filePath);
if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Cannot save:\n" + ed->filePath);
return false;
}
QTextStream out(&f);
out << ed->toPlainText();
ed->document()->setModified(false);
statusLabel->setText("Saved.");
return true;
}
bool Editor::saveFileAs() {
QString path = QFileDialog::getSaveFileName(this, "Save As", {},
"C++ Files (*.cpp *.h *.hpp);;All Files (*)");
if (path.isEmpty()) return false;
CodeEditor *ed = currentEditor();
if (!ed) return false;
ed->filePath = path;
tabWidget->setTabText(tabWidget->currentIndex(), QFileInfo(path).fileName());
setCurrentFile(path);
return saveFile();
}
bool Editor::maybeSave() {
CodeEditor *ed = currentEditor();
if (!ed || !ed->document()->isModified()) return true;
auto btn = QMessageBox::warning(this, "Unsaved Changes",
"Document modified. Save?",
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (btn == QMessageBox::Save) return saveFile();
if (btn == QMessageBox::Cancel) return false;
return true;
}
void Editor::setCurrentFile(const QString &path) {
currentFile = path;
QString shown = path.isEmpty() ? "Untitled" : QFileInfo(path).fileName();
setWindowTitle(shown + " - Code Editor");
fileLabel->setText(shown);
}
void Editor::toggleTheme() { isDarkTheme = !isDarkTheme; applyTheme(); }
void Editor::applyTheme() {
Theme theme = isDarkTheme ? themeManager->darkTheme() : themeManager->lightTheme();
QString sheet = isDarkTheme ? themeManager->darkStyleSheet() : themeManager->lightStyleSheet();
setStyleSheet(sheet);
// Output panel style
QString outBg = theme.windowBg.name(), outFg = theme.foreground.name();
outputPanel->setStyleSheet(QString(
"QPlainTextEdit{background:%1;color:%2;font-family:'Cascadia Code',monospace;font-size:12px;border:none;}"
).arg(outBg, outFg));
// Apply to all tabs
for (int i = 0; i < tabWidget->count(); ++i) {
CodeEditor *ed = qobject_cast<CodeEditor*>(tabWidget->widget(i));
if (ed) ed->applyTheme(theme);
}
statusLabel->setText(isDarkTheme ? "Dark theme" : "Light theme");
}
void Editor::showFindReplace() {
CodeEditor *ed = currentEditor();
if (!ed) return;
// Wrap CodeEditor in a QTextEdit-compatible way via a helper
// Since FindReplaceDialog expects QTextEdit, we use a workaround
auto *dlg = new QDialog(this);
dlg->setWindowTitle("Find & Replace");
dlg->setMinimumWidth(400);
dlg->setStyleSheet(styleSheet());
auto *layout = new QGridLayout(dlg);
auto *findEdit = new QLineEdit(dlg);
auto *replaceEdit = new QLineEdit(dlg);
auto *caseCb = new QCheckBox("Case sensitive", dlg);
auto *wholeCb = new QCheckBox("Whole word", dlg);
auto *statusLbl = new QLabel("", dlg);
layout->addWidget(new QLabel("Find:"), 0, 0);
layout->addWidget(findEdit, 0, 1, 1, 2);
layout->addWidget(new QLabel("Replace:"), 1, 0);
layout->addWidget(replaceEdit, 1, 1, 1, 2);
layout->addWidget(caseCb, 2, 0);
layout->addWidget(wholeCb, 2, 1);
auto *btnPrev = new QPushButton("Prev", dlg);
auto *btnNext = new QPushButton("Next", dlg);
auto *btnReplace = new QPushButton("Replace", dlg);
auto *btnAll = new QPushButton("Replace All", dlg);
auto *btnClose = new QPushButton("Close", dlg);
layout->addWidget(btnPrev, 3, 0);
layout->addWidget(btnNext, 3, 1);
layout->addWidget(btnReplace, 3, 2);
layout->addWidget(btnAll, 4, 0);
layout->addWidget(btnClose, 4, 2);
layout->addWidget(statusLbl, 5, 0, 1, 3);
auto getFlags = [&](bool backward = false) {
QTextDocument::FindFlags f;
if (backward) f |= QTextDocument::FindBackward;
if (caseCb->isChecked()) f |= QTextDocument::FindCaseSensitively;
if (wholeCb->isChecked()) f |= QTextDocument::FindWholeWords;
return f;
};
connect(btnNext, &QPushButton::clicked, dlg, [&, ed](){
bool found = ed->find(findEdit->text(), getFlags());
statusLbl->setText(found ? "" : "Not found.");
});
connect(btnPrev, &QPushButton::clicked, dlg, [&, ed](){
bool found = ed->find(findEdit->text(), getFlags(true));
statusLbl->setText(found ? "" : "Not found.");
});
connect(btnReplace, &QPushButton::clicked, dlg, [&, ed](){
QTextCursor c = ed->textCursor();
if (c.hasSelection()) c.insertText(replaceEdit->text());
ed->find(findEdit->text(), getFlags());
});
connect(btnAll, &QPushButton::clicked, dlg, [&, ed](){
QTextCursor c = ed->textCursor();
c.movePosition(QTextCursor::Start);
ed->setTextCursor(c);
int count = 0;
while (ed->find(findEdit->text(), getFlags())) {
ed->textCursor().insertText(replaceEdit->text());
++count;
}
statusLbl->setText(count > 0 ?
QString("Replaced %1 occurrence(s).").arg(count) : "Not found.");
});
connect(btnClose, &QPushButton::clicked, dlg, &QDialog::close);
connect(findEdit, &QLineEdit::returnPressed, btnNext, &QPushButton::click);
dlg->show();
}
void Editor::changeFont() {
CodeEditor *ed = currentEditor();
if (!ed) return;
bool ok;
QFont f = QFontDialog::getFont(&ok, ed->font(), this, "Choose Font");
if (ok) ed->setFont(f);
}
void Editor::changeFontSize(int size) {
for (int i = 0; i < tabWidget->count(); ++i) {
CodeEditor *ed = qobject_cast<CodeEditor*>(tabWidget->widget(i));
if (ed) {
QFont f = ed->font();
f.setPointSize(size);
ed->setFont(f);
}
}
}
void Editor::updateStatusBar() {
CodeEditor *ed = currentEditor();
if (!ed) return;
QTextCursor c = ed->textCursor();
lineColLabel->setText(QString("Ln %1, Col %2")
.arg(c.blockNumber() + 1).arg(c.columnNumber() + 1));
}
void Editor::documentModified() {
CodeEditor *ed = currentEditor();
if (!ed) return;
bool mod = ed->document()->isModified();
QString title = windowTitle();
if (mod && !title.startsWith("*")) setWindowTitle("*" + title);
if (!mod && title.startsWith("*")) setWindowTitle(title.mid(1));
}
void Editor::loadSettings() {
QSettings s("CodeEditor", "Settings");
isDarkTheme = s.value("darkTheme", true).toBool();
resize(s.value("size", QSize(1200, 750)).toSize());
move(s.value("pos", QPoint(100, 100)).toPoint());
applyTheme();
}
void Editor::saveSettings() {
QSettings s("CodeEditor", "Settings");
s.setValue("darkTheme", isDarkTheme);
s.setValue("size", size());
s.setValue("pos", pos());
}
void Editor::closeEvent(QCloseEvent *event) {
if (maybeSave()) { saveSettings(); event->accept(); }
else event->ignore();
}