-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
258 lines (207 loc) · 6.74 KB
/
mainwindow.cpp
File metadata and controls
258 lines (207 loc) · 6.74 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
#include "mainwindow.h"
#include <QSettings>
#include "./ui_mainwindow.h"
#include <QFileDialog>
#include <QFile>
#include <QTextEdit>
#include <QTextStream>
#include <QMessageBox>
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrintDialog>
#include <QtPrintSupport/QPrintPreviewDialog>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
findDialog = new FindDialog();
findDialog->setParent(this, Qt::Tool | Qt::MSWindowsFixedSizeDialogHint);
tabbedWindow = ui->tabWidget;
tabbedWindow->setTabsClosable(true);
on_currentTabChanged(0);
connect(tabbedWindow, SIGNAL(currentChanged(int)), this, SLOT(on_currentTabChanged(int)));
connect(tabbedWindow, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
QSettings settings;
this->setGeometry(settings.value("windowGeometry", QRect(100, 100, 800, 600)).toRect());
}
MainWindow::~MainWindow()
{
QSettings settings;
settings.setValue("windowGeometry", this->geometry());
delete ui;
}
/* User lauches find dialog
*/
void MainWindow::launchFindDialog()
{
if (findDialog->isHidden())
{
findDialog->show();
findDialog->activateWindow();
findDialog->raise();
findDialog->setFocus();
}
}
/* Opened/saved file with name -> changing the name on tab
*/
void MainWindow::updateTabAndWindowTitle()
{
QString tabTitle = window->getFileName();
QString windowTitle = tabTitle;
tabbedWindow->setTabText(tabbedWindow->currentIndex(), tabTitle);
}
void MainWindow::disconnectWindowDependentSignals()
{
disconnect(findDialog, SIGNAL(startFinding(QString, bool, bool)), window, SLOT(find(QString, bool, bool)));
disconnect(findDialog, SIGNAL(startReplacing(QString, QString, bool, bool)), window, SLOT(replace(QString, QString, bool, bool)));
disconnect(findDialog, SIGNAL(startReplacingAll(QString, QString, bool, bool)), window, SLOT(replaceAll(QString, QString, bool, bool)));
disconnect(window, SIGNAL(findResultReady(QString)), findDialog, SLOT(onFindResultReady(QString)));
}
void MainWindow::reconnectWindowDependentSignals()
{
connect(findDialog, SIGNAL(startFinding(QString, bool, bool)), window, SLOT(find(QString, bool, bool)));
connect(findDialog, SIGNAL(startReplacing(QString, QString, bool, bool)), window, SLOT(replace(QString, QString, bool, bool)));
connect(findDialog, SIGNAL(startReplacingAll(QString, QString, bool, bool)), window, SLOT(replaceAll(QString, QString, bool, bool)));
connect(window, SIGNAL(findResultReady(QString)), findDialog, SLOT(onFindResultReady(QString)));
}
/* User lauches open dialog
*/
void MainWindow::on_action_Open_file_triggered()
{
bool openInCurrentTab = window->isUntitled();
QString openedFilePath;
openedFilePath = QFileDialog::getOpenFileName(this, tr("Open"));
if (openedFilePath.isNull())
{
return;
}
// Attempt to create a file descriptor for the file at the given path
QFile file(openedFilePath);
if (!file.open(QIODevice::ReadOnly | QFile::Text))
{
QMessageBox::warning(this, tr("Warning"), tr("Cannot save file: ") + file.errorString());
return;
}
QTextStream in(&file);
QString documentContents = in.readAll();
if (!openInCurrentTab)
{
tabbedWindow->add(new Window());
}
window->setCurrentFilePath(openedFilePath);
window->document()->setHtml(documentContents);
file.close();
updateTabAndWindowTitle();
statusBar()->showMessage(tr("File opened successfully"), 15000);
}
/* User lauches save dialog
*/
void MainWindow::on_action_Save_as_triggered()
{
QString currentFilePath = window->getCurrentFilePath();
if (currentFilePath.isEmpty())
{
QString filePath = QFileDialog::getSaveFileName(this, tr("Save As"));
if (filePath.isNull())
{
return;
}
window->setCurrentFilePath(filePath);
}
QFile file(window->getCurrentFilePath());
if (!file.open(QIODevice::WriteOnly | QFile::Text))
{
QMessageBox::warning(this, tr("Warning"), tr("Cannot save file: ") + file.errorString());
return;
}
QTextStream out(&file);
QString windowContents = window->document()->toHtml();
out << windowContents;
file.close();
updateTabAndWindowTitle();
statusBar()->showMessage(tr("File saved successfully"), 15000);
}
/* reload editor window
*/
void MainWindow::on_currentTabChanged(int index)
{
if (index == -1)
{
return;
}
if (window != nullptr)
{
disconnectWindowDependentSignals();
}
window = tabbedWindow->currentTab();
reconnectWindowDependentSignals();
window->setFocus(Qt::FocusReason::TabFocusReason);
}
/* User lauches print dialog
*/
void MainWindow::on_action_Print_triggered()
{
if (!window || window->toPlainText().isEmpty()) {
QMessageBox::warning(this,
tr("Nothing to Print"),
tr("The document is empty."));
return;
}
QPrinter *printer = new QPrinter(QPrinter::HighResolution);
printer->setPageSize(QPageSize::A4);
printer->setPageOrientation(QPageLayout::Portrait);
printer->setFullPage(false);
// Create a print preview dialog
QPrintPreviewDialog previewDialog(printer, this);
previewDialog.setWindowTitle(tr("Print Preview"));
// Connect preview signal to your print function
connect(&previewDialog, &QPrintPreviewDialog::paintRequested,
this, [=](QPrinter *p) {
window->print(p);
});
previewDialog.exec();
statusBar()->showMessage(tr("File send to printer successfully"), 15000);
}
/* User lauches search dialog
*/
void MainWindow::on_action_Search_triggered()
{
launchFindDialog();
}
/* User wants new editor tab
*/
void MainWindow::on_action_New_file_triggered()
{
tabbedWindow->add(new Window());
}
bool MainWindow::closeTab(Window *tabToClose)
{
Window *currentTab = window;
bool closingCurrentTab = (tabToClose == currentTab);
if (!closingCurrentTab)
{
tabbedWindow->setCurrentWidget(tabToClose);
}
int indexOfTabToClose = tabbedWindow->indexOf(tabToClose);
tabbedWindow->removeTab(indexOfTabToClose);
if (tabbedWindow->count() == 0)
{
on_action_New_file_triggered();
}
// And finally, go back to original tab if the user was closing a different one
if (!closingCurrentTab)
{
tabbedWindow->setCurrentWidget(currentTab);
}
return true;
}
/* User lauches font and size dialog
*/
void MainWindow::on_action_Font_triggered()
{
tabbedWindow->promptFontSelection();
}
/* User lauches color dialog
*/
void MainWindow::on_action_Color_triggered()
{
tabbedWindow->promptColorSelection();
}