-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditorTabWidget.cpp
More file actions
334 lines (277 loc) · 10.3 KB
/
EditorTabWidget.cpp
File metadata and controls
334 lines (277 loc) · 10.3 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
#include "EditorTabWidget.h"
#include <QApplication>
#include <QDesktopServices>
#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QFileIconProvider>
#include <QFileInfo>
#include <QMessageBox>
#include <QStyle>
#include <QTabBar>
#include <QTextStream>
#include <QToolButton>
#include "AppDatabase.h"
EditorTabWidget::EditorTabWidget(QWidget* parent) : QTabWidget(parent) {
setTabsClosable(false);
setMovable(true);
setDocumentMode(true);
setStyleSheet(
"QTabWidget::pane { border: none; }"
"QTabBar::tab {"
" padding: 6px 14px;"
" border: none; border-right: 1px solid;"
" min-width: 80px;"
"}"
"QTabBar::tab:selected { border-top: 2px solid #007acc; }"
"QTabBar::tab:hover:!selected { }");
connect(this, &QTabWidget::tabCloseRequested, this,
&EditorTabWidget::onTabCloseRequested);
connect(this, &QTabWidget::currentChanged, this,
&EditorTabWidget::onCurrentChanged);
}
void EditorTabWidget::applySettingsToEditor(CodeEditor* editor) const {
editor->setTabSize(m_tabSize);
editor->setAutoIndent(m_autoIndent);
}
void EditorTabWidget::setTabSize(int size) {
m_tabSize = qMax(1, qMin(size, 16));
for (int i = 0; i < count(); ++i)
if (auto* ed = editorAt(i)) ed->setTabSize(m_tabSize);
}
bool EditorTabWidget::hasTabOpenWithPath(const QString& filePath) {
return findTabByPath(filePath) >= 0;
}
void EditorTabWidget::setAutoIndent(bool on) {
m_autoIndent = on;
for (int i = 0; i < count(); ++i)
if (auto* ed = editorAt(i)) ed->setAutoIndent(m_autoIndent);
}
CodeEditor* EditorTabWidget::currentEditor() const {
return qobject_cast<CodeEditor*>(currentWidget());
}
CodeEditor* EditorTabWidget::editorAt(int index) const {
return qobject_cast<CodeEditor*>(widget(index));
}
QIcon EditorTabWidget::iconForFile(const QString& filePath) const {
if (filePath.isEmpty())
return QIcon::fromTheme("document-new", QIcon::fromTheme("text-x-generic"));
static QFileIconProvider provider;
return provider.icon(QFileInfo(filePath));
}
int EditorTabWidget::findTabByPath(const QString& filePath) const {
for (int i = 0; i < count(); ++i) {
auto* ed = editorAt(i);
if (ed && ed->filePath() == filePath) return i;
}
return -1;
}
void EditorTabWidget::updateTabTitle(int index) {
auto* ed = editorAt(index);
if (!ed) return;
const QString name = ed->filePath().isEmpty()
? "untitled"
: QFileInfo(ed->filePath()).fileName();
const bool dirty = ed->document()->isModified();
setTabText(index, dirty ? name + "*" : name);
setTabIcon(index, iconForFile(ed->filePath()));
setTabToolTip(index, ed->filePath().isEmpty() ? "New file" : ed->filePath());
}
void EditorTabWidget::attachCloseButton(int index) {
auto* btn = new QToolButton(tabBar());
btn->setText("✕");
btn->setFixedSize(16, 16);
btn->setCursor(Qt::ArrowCursor);
btn->setStyleSheet(
"QToolButton {"
" color: #888; border: none; border-radius: 3px;"
" font-size: 11px; padding: 0;"
" background: transparent;"
"}"
"QToolButton:hover { color: white; background: #c0392b; }"
"QToolButton:pressed { background: #922b21; }");
connect(btn, &QToolButton::clicked, this, [this, btn]() {
for (int i = 0; i < count(); ++i) {
if (tabBar()->tabButton(i, QTabBar::RightSide) == btn) {
closeTab(i);
return;
}
}
});
tabBar()->setTabButton(index, QTabBar::RightSide, btn);
}
void EditorTabWidget::connectEditor(CodeEditor* editor) {
connect(editor->document(), &QTextDocument::modificationChanged, this,
[this, editor](bool) {
const int idx = indexOf(editor);
if (idx >= 0) updateTabTitle(idx);
});
connect(editor, &CodeEditor::cursorPositionChanged, this,
&EditorTabWidget::onCursorMoved);
}
void EditorTabWidget::newFile() {
auto* editor = new CodeEditor(this);
applySettingsToEditor(editor);
connectEditor(editor);
const int idx = addTab(editor, QIcon::fromTheme("document-new"), "untitled");
setCurrentIndex(idx);
updateTabTitle(idx);
attachCloseButton(idx);
editor->setFocus();
}
void EditorTabWidget::openFile(const QString& filePath) {
const int existing = findTabByPath(filePath);
if (existing >= 0) {
setCurrentIndex(existing);
return;
}
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Open Failed", "Could not open:\n" + filePath);
return;
}
const QByteArray sample = file.read(8192);
if (sample.contains('\0')) {
file.close();
auto btn = QMessageBox::question(
this, "Binary File Detected",
QString("'<b>%1</b>' appears to be a binary file.<br>"
"Open with the default application instead?")
.arg(QFileInfo(filePath).fileName()),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);
if (btn == QMessageBox::Yes) {
QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
}
return;
}
file.seek(0);
QTextStream in(&file);
in.setAutoDetectUnicode(true);
const QString content = in.readAll();
auto* editor = new CodeEditor(this);
applySettingsToEditor(editor);
editor->setFilePath(filePath);
editor->setPlainText(content);
editor->document()->setModified(false);
const auto bms = AppDatabase::instance().bookmarksForFile(filePath);
editor->setBookmarks(QSet<int>(bms.begin(), bms.end()));
connectEditor(editor);
const int idx =
addTab(editor, iconForFile(filePath), QFileInfo(filePath).fileName());
setCurrentIndex(idx);
updateTabTitle(idx);
attachCloseButton(idx);
editor->setFocus();
AppDatabase::instance().addRecentFile(filePath);
AppDatabase::instance().save();
}
bool EditorTabWidget::closeTab(int index) {
auto* editor = editorAt(index);
if (!editor) return true;
if (editor->document()->isModified()) {
const QString name = editor->filePath().isEmpty()
? "untitled"
: QFileInfo(editor->filePath()).fileName();
auto btn = QMessageBox::question(
this, "Unsaved Changes",
QString("'<b>%1</b>' has unsaved changes.<br>Save before closing?")
.arg(name),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,
QMessageBox::Save);
if (btn == QMessageBox::Cancel) return false;
if (btn == QMessageBox::Save && !saveTab(index)) return false;
}
if (!editor->filePath().isEmpty()) {
const auto& bms = editor->bookmarks();
AppDatabase::instance().setBookmarks(editor->filePath(),
QList<int>(bms.begin(), bms.end()));
AppDatabase::instance().save();
}
QWidget* closeBtn = tabBar()->tabButton(index, QTabBar::RightSide);
if (closeBtn) {
tabBar()->setTabButton(index, QTabBar::RightSide, nullptr);
delete closeBtn;
}
removeTab(index);
delete editor;
return true;
}
bool EditorTabWidget::closeAllTabs() {
while (count() > 0)
if (!closeTab(0)) return false;
return true;
}
bool EditorTabWidget::saveTab(int index) {
auto* editor = editorAt(index);
if (!editor) return false;
if (editor->filePath().isEmpty()) return saveTabAs(index);
QFile file(editor->filePath());
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Save Failed",
"Could not write to:\n" + editor->filePath());
return false;
}
QTextStream out(&file);
out << editor->toPlainText();
editor->document()->setModified(false);
updateTabTitle(index);
return true;
}
bool EditorTabWidget::saveTabAs(int index) {
auto* editor = editorAt(index);
if (!editor) return false;
const QString workingDir = AppDatabase::instance().recentDirectory();
const QString path = QFileDialog::getSaveFileName(
this, "Save As",
editor->filePath().isEmpty() ? (workingDir.isEmpty() ? QDir::homePath() : workingDir) : editor->filePath(),
"All Files (*)");
if (path.isEmpty()) return false;
const int existing = findTabByPath(path);
if (existing >= 0 && existing != index) {
QMessageBox::warning(this, "File Already Open",
"This file is already open in another tab.");
return false;
}
editor->setFilePath(path);
updateTabTitle(index);
AppDatabase::instance().addRecentFile(path);
AppDatabase::instance().save();
emit currentFileChanged(path, editor->language());
disconnect(editor->document(), &QTextDocument::modificationChanged, nullptr, nullptr);
connect(editor->document(), &QTextDocument::modificationChanged, this,
[this, editor](bool) {
const int idx = indexOf(editor);
if (idx >= 0) updateTabTitle(idx);
});
return saveTab(index);
}
void EditorTabWidget::overrideLanguage(Language lang) {
auto* ed = currentEditor();
if (ed) ed->setLanguage(lang);
emit currentFileChanged(ed ? ed->filePath() : QString(), lang);
}
void EditorTabWidget::updateRecentFiles() {
for (int i = 0; i < count(); ++i) {
auto* ed = editorAt(i);
if (ed && !ed->filePath().isEmpty())
AppDatabase::instance().addRecentFile(ed->filePath());
}
AppDatabase::instance().save();
}
void EditorTabWidget::onTabCloseRequested(int index) { closeTab(index); }
void EditorTabWidget::onCurrentChanged(int index) {
auto* ed = editorAt(index);
emit currentFileChanged(ed ? ed->filePath() : QString(),
ed ? ed->language() : Language::None);
if (ed) {
const QTextCursor c = ed->textCursor();
emit cursorPositionChanged(c.blockNumber() + 1, c.columnNumber() + 1);
}
}
void EditorTabWidget::onCursorMoved() {
auto* ed = qobject_cast<CodeEditor*>(sender());
if (!ed || ed != currentEditor()) return;
const QTextCursor c = ed->textCursor();
emit cursorPositionChanged(c.blockNumber() + 1, c.columnNumber() + 1);
}