1 /****************************************************************************
2 ** Copyright (C) 2020 Riverbank Computing Limited
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 ****************************************************************************/
49
50 #include <QAction>
51 #include <QApplication>
52 #include <QCloseEvent>
53 #include <QFile>
54 #include <QFileInfo>
55 #include <QFileDialog>
56 #include <QIcon>
57 #include <QMenu>
58 #include <QMenuBar>
59 #include <QMessageBox>
60 #include <QPoint>
61 #include <QSettings>
62 #include <QSize>
63 #include <QStatusBar>
64 #include <QTextStream>
65 #include <QToolBar>
66
67 #include <Qsci/qsciscintilla.h>
68
69 #include "mainwindow.h"
70
MainWindow()71 MainWindow::MainWindow()
72 {
73 textEdit = new QsciScintilla;
74 setCentralWidget(textEdit);
75
76 createActions();
77 createMenus();
78 createToolBars();
79 createStatusBar();
80
81 readSettings();
82
83 connect(textEdit, SIGNAL(textChanged()),
84 this, SLOT(documentWasModified()));
85
86 setCurrentFile("");
87 }
88
closeEvent(QCloseEvent * event)89 void MainWindow::closeEvent(QCloseEvent *event)
90 {
91 if (maybeSave()) {
92 writeSettings();
93 event->accept();
94 } else {
95 event->ignore();
96 }
97 }
98
newFile()99 void MainWindow::newFile()
100 {
101 if (maybeSave()) {
102 textEdit->clear();
103 setCurrentFile("");
104 }
105 }
106
open()107 void MainWindow::open()
108 {
109 if (maybeSave()) {
110 QString fileName = QFileDialog::getOpenFileName(this);
111 if (!fileName.isEmpty())
112 loadFile(fileName);
113 }
114 }
115
save()116 bool MainWindow::save()
117 {
118 if (curFile.isEmpty()) {
119 return saveAs();
120 } else {
121 return saveFile(curFile);
122 }
123 }
124
saveAs()125 bool MainWindow::saveAs()
126 {
127 QString fileName = QFileDialog::getSaveFileName(this);
128 if (fileName.isEmpty())
129 return false;
130
131 return saveFile(fileName);
132 }
133
about()134 void MainWindow::about()
135 {
136 QMessageBox::about(this, tr("About Application"),
137 tr("The <b>Application</b> example demonstrates how to "
138 "write modern GUI applications using Qt, with a menu bar, "
139 "toolbars, and a status bar."));
140 }
141
documentWasModified()142 void MainWindow::documentWasModified()
143 {
144 setWindowModified(textEdit->isModified());
145 }
146
createActions()147 void MainWindow::createActions()
148 {
149 newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
150 newAct->setShortcut(tr("Ctrl+N"));
151 newAct->setStatusTip(tr("Create a new file"));
152 connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
153
154 openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
155 openAct->setShortcut(tr("Ctrl+O"));
156 openAct->setStatusTip(tr("Open an existing file"));
157 connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
158
159 saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
160 saveAct->setShortcut(tr("Ctrl+S"));
161 saveAct->setStatusTip(tr("Save the document to disk"));
162 connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
163
164 saveAsAct = new QAction(tr("Save &As..."), this);
165 saveAsAct->setStatusTip(tr("Save the document under a new name"));
166 connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
167
168 exitAct = new QAction(tr("E&xit"), this);
169 exitAct->setShortcut(tr("Ctrl+Q"));
170 exitAct->setStatusTip(tr("Exit the application"));
171 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
172
173 cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
174 cutAct->setShortcut(tr("Ctrl+X"));
175 cutAct->setStatusTip(tr("Cut the current selection's contents to the "
176 "clipboard"));
177 connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
178
179 copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
180 copyAct->setShortcut(tr("Ctrl+C"));
181 copyAct->setStatusTip(tr("Copy the current selection's contents to the "
182 "clipboard"));
183 connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
184
185 pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
186 pasteAct->setShortcut(tr("Ctrl+V"));
187 pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
188 "selection"));
189 connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
190
191 aboutAct = new QAction(tr("&About"), this);
192 aboutAct->setStatusTip(tr("Show the application's About box"));
193 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
194
195 aboutQtAct = new QAction(tr("About &Qt"), this);
196 aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
197 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
198
199 cutAct->setEnabled(false);
200 copyAct->setEnabled(false);
201 connect(textEdit, SIGNAL(copyAvailable(bool)),
202 cutAct, SLOT(setEnabled(bool)));
203 connect(textEdit, SIGNAL(copyAvailable(bool)),
204 copyAct, SLOT(setEnabled(bool)));
205 }
206
createMenus()207 void MainWindow::createMenus()
208 {
209 fileMenu = menuBar()->addMenu(tr("&File"));
210 fileMenu->addAction(newAct);
211 fileMenu->addAction(openAct);
212 fileMenu->addAction(saveAct);
213 fileMenu->addAction(saveAsAct);
214 fileMenu->addSeparator();
215 fileMenu->addAction(exitAct);
216
217 editMenu = menuBar()->addMenu(tr("&Edit"));
218 editMenu->addAction(cutAct);
219 editMenu->addAction(copyAct);
220 editMenu->addAction(pasteAct);
221
222 menuBar()->addSeparator();
223
224 helpMenu = menuBar()->addMenu(tr("&Help"));
225 helpMenu->addAction(aboutAct);
226 helpMenu->addAction(aboutQtAct);
227 }
228
createToolBars()229 void MainWindow::createToolBars()
230 {
231 fileToolBar = addToolBar(tr("File"));
232 fileToolBar->addAction(newAct);
233 fileToolBar->addAction(openAct);
234 fileToolBar->addAction(saveAct);
235
236 editToolBar = addToolBar(tr("Edit"));
237 editToolBar->addAction(cutAct);
238 editToolBar->addAction(copyAct);
239 editToolBar->addAction(pasteAct);
240 }
241
createStatusBar()242 void MainWindow::createStatusBar()
243 {
244 statusBar()->showMessage(tr("Ready"));
245 }
246
readSettings()247 void MainWindow::readSettings()
248 {
249 QSettings settings("Trolltech", "Application Example");
250 QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
251 QSize size = settings.value("size", QSize(400, 400)).toSize();
252 resize(size);
253 move(pos);
254 }
255
writeSettings()256 void MainWindow::writeSettings()
257 {
258 QSettings settings("Trolltech", "Application Example");
259 settings.setValue("pos", pos());
260 settings.setValue("size", size());
261 }
262
maybeSave()263 bool MainWindow::maybeSave()
264 {
265 if (textEdit->isModified()) {
266 int ret = QMessageBox::warning(this, tr("Application"),
267 tr("The document has been modified.\n"
268 "Do you want to save your changes?"),
269 QMessageBox::Yes | QMessageBox::Default,
270 QMessageBox::No,
271 QMessageBox::Cancel | QMessageBox::Escape);
272 if (ret == QMessageBox::Yes)
273 return save();
274 else if (ret == QMessageBox::Cancel)
275 return false;
276 }
277 return true;
278 }
279
loadFile(const QString & fileName)280 void MainWindow::loadFile(const QString &fileName)
281 {
282 QFile file(fileName);
283 if (!file.open(QFile::ReadOnly)) {
284 QMessageBox::warning(this, tr("Application"),
285 tr("Cannot read file %1:\n%2.")
286 .arg(fileName)
287 .arg(file.errorString()));
288 return;
289 }
290
291 QTextStream in(&file);
292 QApplication::setOverrideCursor(Qt::WaitCursor);
293 textEdit->setText(in.readAll());
294 QApplication::restoreOverrideCursor();
295
296 setCurrentFile(fileName);
297 statusBar()->showMessage(tr("File loaded"), 2000);
298 }
299
saveFile(const QString & fileName)300 bool MainWindow::saveFile(const QString &fileName)
301 {
302 QFile file(fileName);
303 if (!file.open(QFile::WriteOnly)) {
304 QMessageBox::warning(this, tr("Application"),
305 tr("Cannot write file %1:\n%2.")
306 .arg(fileName)
307 .arg(file.errorString()));
308 return false;
309 }
310
311 QTextStream out(&file);
312 QApplication::setOverrideCursor(Qt::WaitCursor);
313 out << textEdit->text();
314 QApplication::restoreOverrideCursor();
315
316 setCurrentFile(fileName);
317 statusBar()->showMessage(tr("File saved"), 2000);
318 return true;
319 }
320
setCurrentFile(const QString & fileName)321 void MainWindow::setCurrentFile(const QString &fileName)
322 {
323 curFile = fileName;
324 textEdit->setModified(false);
325 setWindowModified(false);
326
327 QString shownName;
328 if (curFile.isEmpty())
329 shownName = "untitled.txt";
330 else
331 shownName = strippedName(curFile);
332
333 setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Application")));
334 }
335
strippedName(const QString & fullFileName)336 QString MainWindow::strippedName(const QString &fullFileName)
337 {
338 return QFileInfo(fullFileName).fileName();
339 }
340