-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
342 lines (275 loc) · 11.7 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
setAcceptDrops(true);
setWindowTitle("CIDEPP");
SetupTabWidget();
SetupFileExplorerDock();
SetupOpenedDocsDock();
CreateFile();
SetupMenuBar();
QCompleter *completer = new QCompleter(QStringList("test"), this);
completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setWrapAround(false);
setCentralWidget(tabs);
tabs->currentWidget()->setFocus();
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::dragEnterEvent(QDragEnterEvent* drag_event) {
if (drag_event->mimeData()->hasUrls())
drag_event->acceptProposedAction();
}
void MainWindow::dropEvent(QDropEvent* drop_event) {
QList<QUrl> url_list = drop_event->mimeData()->urls();
foreach (QUrl url, url_list) {
OpenFile(url.url(QUrl::RemoveScheme));
}
}
void MainWindow::SetupTabWidget() {
tabs = new QTabWidget(this);
tabs->setStyleSheet("color:#000000; font-size: 12px; font:sans-serif !important;");
tabs->setMovable(true);
tabs->setTabsClosable(true);
tabs->setUsesScrollButtons(true);
connect(tabs, SIGNAL(tabCloseRequested(int)), this, SLOT(CloseFile(int)));
QToolButton* new_tab_button = new QToolButton;
new_tab_button->setText("+");
connect(new_tab_button, SIGNAL(clicked()), this, SLOT(CreateFile()));
tabs->setCornerWidget(new_tab_button, Qt::TopRightCorner);
}
void MainWindow::SetupMenuBar() {
QMenu* fileMenu = new QMenu("File");
//QMenu* viewMenu = new QMenu("View");
fileMenu->addAction("New File", this, SLOT(CreateFile()), Qt::CTRL + Qt::Key_N);
fileMenu->addAction("Open File", this, SLOT(OpenFile()), Qt::CTRL + Qt::Key_O);
fileMenu->addSeparator();
fileMenu->addAction("Save File", this, SLOT(SaveFile()), Qt::CTRL + Qt::Key_S);
//viewMenu->addAction(file_explorer_dock->toggleViewAction());
file_explorer_dock->show();
//viewMenu->addAction(opened_docs_dock->toggleViewAction());
opened_docs_dock->show();
ui->menubar->addMenu(fileMenu);
fileMenu->hide();
//ui->menubar->addMenu(viewMenu);
}
void MainWindow::SetupFileExplorerDock() {
file_system_model = new QDirModel;
file_system_view = new QTreeView;
file_system_view->setStyleSheet("font-size: 12px !important; font:sans-serif;");
file_system_view->setModel(file_system_model);
file_system_view->setRootIndex(file_system_model->index("/home"));
file_system_view->setColumnHidden(1, true);
file_system_view->setColumnHidden(2, true);
file_system_view->setColumnHidden(3, true);
file_system_view->setHeaderHidden(true);
connect(file_system_view, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(OpenFile(QModelIndex)));
file_explorer_dock = new QDockWidget("File explorer", this);
file_explorer_dock->setStyleSheet("font-size: 12px !important; font:sans-serif !important;");
file_explorer_dock->setWidget(file_system_view);
file_explorer_dock->hide();
addDockWidget(Qt::RightDockWidgetArea, file_explorer_dock);
}
void MainWindow::SetupOpenedDocsDock() {
opened_docs_widget = new QListWidget;
opened_docs_widget->setStyleSheet("font-size: 12px !important; font:sans-serif;");
connect(tabs->tabBar(), SIGNAL(tabMoved(int, int)), this, SLOT(ChangeTabIndexInList(int, int)));
connect(opened_docs_widget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(UpdateCurrentIndex(QListWidgetItem*)));
connect(opened_docs_widget, SIGNAL(currentRowChanged(int)), tabs, SLOT(setCurrentIndex(int)));
connect(tabs->tabBar(), SIGNAL(currentChanged(int)), this, SLOT(UpdateCurrentIndex(int)));
connect(tabs->tabBar(), SIGNAL(tabCloseRequested(int)), this, SLOT(UpdateCurrentIndexOnDelete(int)));
opened_docs_dock = new QDockWidget("Opened files", this);
opened_docs_dock->setStyleSheet("font-size: 12px !important; font:sans-serif !important;");
opened_docs_dock->setWidget(opened_docs_widget);
opened_docs_dock->hide();
addDockWidget(Qt::RightDockWidgetArea, opened_docs_dock);
}
void MainWindow::CreateFile() {
CodeEditor *new_text_edit = new CodeEditor;
QCompleter *completer = new QCompleter(new_text_edit->wordlist, this);
completer->setModelSorting(QCompleter::CaseSensitivelySortedModel);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setWrapAround(false);
new_text_edit->setCompleter(completer);
int index = tabs->addTab(new_text_edit, "untitled.cpp");
tabs->setCurrentIndex(index);
tabs->setTabToolTip(index, "");
tabs->setTabWhatsThis(index, "No changes");
connect(new_text_edit, SIGNAL(textChanged()), this, SLOT(UpdateParameter()));
QListWidgetItem* new_item = new QListWidgetItem;
new_item->setText(tabs->tabText(index));
new_item->setToolTip(tabs->tabToolTip(index));
opened_docs_widget->addItem(new_item);
UpdateCurrentIndex(index);
}
void MainWindow::OpenFile() {
QString filepath = QFileDialog::getOpenFileName(this, "Choose file", "/home");
if (filepath.isEmpty())
return;
OpenFile(filepath);
}
void MainWindow::OpenFile(const QString& filepath) {
for (int i = 0; i < tabs->count(); ++i)
if (tabs->tabToolTip(i) == filepath) {
QMessageBox::warning(this, "Duplicate", "Cannot open one file twice!",
QMessageBox::Ok);
return;
}
QString filename = filepath.section(-1,-1);
QFile file(filepath);
if (file.open(QIODevice::ReadOnly)) {
CodeEditor* temp_text = (CodeEditor*)tabs->currentWidget();
if (temp_text->document()->isEmpty() &&
tabs->tabToolTip(tabs->currentIndex()) == "" &&
tabs->tabText(tabs->currentIndex()) == "untitled") {
DeleteTabFromList(tabs->currentIndex());
delete tabs->widget(tabs->currentIndex());
}
CodeEditor* new_text_edit = new CodeEditor;
QString content = file.readAll();
file.close();
new_text_edit->appendPlainText(content);
int index = tabs->addTab(new_text_edit, filename);
tabs->setCurrentIndex(index);
tabs->setTabToolTip(index, filepath);
tabs->setTabWhatsThis(index, "No changes");
connect(new_text_edit, SIGNAL(textChanged()), this, SLOT(UpdateParameter()));
QListWidgetItem* new_item = new QListWidgetItem;
new_item->setText(tabs->tabText(index));
new_item->setToolTip(tabs->tabToolTip(index));
opened_docs_widget->addItem(new_item);
QString file_extension = QFileInfo(filename).suffix();
tabs->setTabWhatsThis(index, "No changes");
UpdateCurrentIndex(index);
} else {
(new QErrorMessage(this))->showMessage("Cannot open file!");
return;
}
}
void MainWindow::SaveFile() {
if (tabs->tabToolTip(tabs->currentIndex()) == "") {
SaveFileAs();
return;
}
QString filepath = tabs->tabToolTip(tabs->currentIndex());
QFile file(filepath);
if (file.open(QIODevice::WriteOnly)) {
file.write(((CodeEditor*)tabs->currentWidget())->document()->toPlainText().toUtf8());
file.close();
tabs->setTabWhatsThis(tabs->currentIndex(), "No changes");
} else {
(new QErrorMessage(this))->showMessage("Cannot save file!");
return;
}
}
void MainWindow::SaveFileAs() {
QString filename = tabs->tabText(tabs->currentIndex());
QString filepath = QFileDialog::getSaveFileName(this, "Save " + filename, "/home/" + filename);
if (filepath.isEmpty())
return;
if (QFileInfo(filepath).suffix().isEmpty())
filepath.append(".txt");
else {
QFile file(filepath);
if (file.open(QIODevice::WriteOnly)) {
file.write(((CodeEditor*)tabs->currentWidget())->document()->toPlainText().toUtf8());
file.close();
} else {
(new QErrorMessage(this))->showMessage("Cannot save file!");
return;
}
}
filename = filepath.section("/",-1,-1);
tabs->tabBar()->setTabText(tabs->currentIndex(), filename);
tabs->tabBar()->setTabToolTip(tabs->currentIndex(), filepath);
QString file_extension = QFileInfo(filename).suffix();
tabs->setTabWhatsThis(tabs->currentIndex(), "No changes");
}
void MainWindow::SaveAllFiles() {
int current_index = tabs->currentIndex();
for (int i = 0; i < tabs->count(); ++i) {
tabs->setCurrentIndex(i);
if (tabs->tabWhatsThis(tabs->currentIndex()) != "No changes")
SaveFile();
}
tabs->setCurrentIndex(current_index);
}
void MainWindow::CloseFile(int index_) {
if (tabs->tabWhatsThis(tabs->currentIndex()) != "No changes") {
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Saving changes", "Save changes before closing?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
SaveFile();
}
}
delete tabs->widget(index_);
DeleteTabFromList(index_);
if (!tabs->count())
CreateFile();
tabs->currentWidget()->setFocus();
}
void MainWindow::CloseFile() {
CloseFile(tabs->currentIndex());
}
void MainWindow::CloseAllFiles() {
bool checker = false;
for (int32_t i = 0; i < tabs->count(); ++i) {
if (tabs->tabWhatsThis(i) != "No changes") {
checker = true;
break;
}
}
if (checker) {
QMessageBox::StandardButton reply = QMessageBox::question(
this, "Save all changes", "Save all changes before closing?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes)
SaveAllFiles();
}
while (tabs->count() > 0)
delete tabs->widget(0);
opened_docs_widget->clear();
CreateFile();
tabs->currentWidget()->setFocus();
}
void MainWindow::CloseWindow() {
CloseAllFiles();
QApplication::quit();
}
void MainWindow::closeEvent(QCloseEvent*) {
CloseWindow();
}
void::MainWindow::UpdateParameter() {
QString file = tabs->tabBar()->tabText(tabs->currentIndex());
QString file_extension = QFileInfo(file).suffix();
tabs->setTabWhatsThis(tabs->currentIndex(), "Changed");
disconnect(sender(), SIGNAL(textChanged()), this, SLOT(UpdateParameter()));
}
void MainWindow::OpenFile(QModelIndex file_index) {
if (!file_system_model->isDir(file_index))
OpenFile(file_system_model->filePath(file_index));
}
void MainWindow::DeleteTabFromList(int index) {
QListWidgetItem* temp_item = opened_docs_widget->takeItem(index);
delete temp_item;
}
void MainWindow::ChangeTabIndexInList(int old_index, int new_index) {
QListWidgetItem* first_item = opened_docs_widget->takeItem(old_index);
opened_docs_widget->insertItem(new_index, first_item);
}
void MainWindow::UpdateCurrentIndex(QListWidgetItem* current_item) {
int index = current_item->listWidget()->row(current_item);
tabs->setCurrentIndex(index);
}
void MainWindow::UpdateCurrentIndex(int new_selection_index) {
opened_docs_widget->setCurrentRow(new_selection_index);
QString file = tabs->tabBar()->tabText(new_selection_index);
QString file_extension = QFileInfo(file).suffix();
}
void MainWindow::UpdateCurrentIndexOnDelete() {
opened_docs_widget->setCurrentRow(opened_docs_widget->count() - 1);
}