0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the examples of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include <QtGui>
|
|
43 |
|
|
44 |
#include "mainwindow.h"
|
|
45 |
|
|
46 |
MainWindow::MainWindow()
|
|
47 |
{
|
|
48 |
init();
|
|
49 |
setCurrentFile("");
|
|
50 |
}
|
|
51 |
|
|
52 |
MainWindow::MainWindow(const QString &fileName)
|
|
53 |
{
|
|
54 |
init();
|
|
55 |
loadFile(fileName);
|
|
56 |
}
|
|
57 |
|
|
58 |
void MainWindow::closeEvent(QCloseEvent *event)
|
|
59 |
{
|
|
60 |
if (maybeSave()) {
|
|
61 |
writeSettings();
|
|
62 |
event->accept();
|
|
63 |
} else {
|
|
64 |
event->ignore();
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
|
68 |
void MainWindow::newFile()
|
|
69 |
{
|
|
70 |
MainWindow *other = new MainWindow;
|
|
71 |
other->move(x() + 40, y() + 40);
|
|
72 |
other->show();
|
|
73 |
}
|
|
74 |
|
|
75 |
void MainWindow::open()
|
|
76 |
{
|
|
77 |
QString fileName = QFileDialog::getOpenFileName(this);
|
|
78 |
if (!fileName.isEmpty()) {
|
|
79 |
MainWindow *existing = findMainWindow(fileName);
|
|
80 |
if (existing) {
|
|
81 |
existing->show();
|
|
82 |
existing->raise();
|
|
83 |
existing->activateWindow();
|
|
84 |
return;
|
|
85 |
}
|
|
86 |
|
|
87 |
if (isUntitled && textEdit->document()->isEmpty()
|
|
88 |
&& !isWindowModified()) {
|
|
89 |
loadFile(fileName);
|
|
90 |
} else {
|
|
91 |
MainWindow *other = new MainWindow(fileName);
|
|
92 |
if (other->isUntitled) {
|
|
93 |
delete other;
|
|
94 |
return;
|
|
95 |
}
|
|
96 |
other->move(x() + 40, y() + 40);
|
|
97 |
other->show();
|
|
98 |
}
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
|
102 |
bool MainWindow::save()
|
|
103 |
{
|
|
104 |
if (isUntitled) {
|
|
105 |
return saveAs();
|
|
106 |
} else {
|
|
107 |
return saveFile(curFile);
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
bool MainWindow::saveAs()
|
|
112 |
{
|
|
113 |
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
|
|
114 |
curFile);
|
|
115 |
if (fileName.isEmpty())
|
|
116 |
return false;
|
|
117 |
|
|
118 |
return saveFile(fileName);
|
|
119 |
}
|
|
120 |
|
|
121 |
void MainWindow::about()
|
|
122 |
{
|
|
123 |
QMessageBox::about(this, tr("About SDI"),
|
|
124 |
tr("The <b>SDI</b> example demonstrates how to write single "
|
|
125 |
"document interface applications using Qt."));
|
|
126 |
}
|
|
127 |
|
|
128 |
void MainWindow::documentWasModified()
|
|
129 |
{
|
|
130 |
setWindowModified(true);
|
|
131 |
}
|
|
132 |
|
|
133 |
void MainWindow::init()
|
|
134 |
{
|
|
135 |
setAttribute(Qt::WA_DeleteOnClose);
|
|
136 |
|
|
137 |
isUntitled = true;
|
|
138 |
|
|
139 |
textEdit = new QTextEdit;
|
|
140 |
setCentralWidget(textEdit);
|
|
141 |
|
|
142 |
createActions();
|
|
143 |
createMenus();
|
|
144 |
createToolBars();
|
|
145 |
createStatusBar();
|
|
146 |
|
|
147 |
readSettings();
|
|
148 |
|
|
149 |
connect(textEdit->document(), SIGNAL(contentsChanged()),
|
|
150 |
this, SLOT(documentWasModified()));
|
|
151 |
|
|
152 |
setUnifiedTitleAndToolBarOnMac(true);
|
|
153 |
}
|
|
154 |
|
|
155 |
void MainWindow::createActions()
|
|
156 |
{
|
|
157 |
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
|
|
158 |
newAct->setShortcuts(QKeySequence::New);
|
|
159 |
newAct->setStatusTip(tr("Create a new file"));
|
|
160 |
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
|
|
161 |
|
|
162 |
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
|
|
163 |
openAct->setShortcuts(QKeySequence::Open);
|
|
164 |
openAct->setStatusTip(tr("Open an existing file"));
|
|
165 |
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
|
|
166 |
|
|
167 |
saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
|
|
168 |
saveAct->setShortcuts(QKeySequence::Save);
|
|
169 |
saveAct->setStatusTip(tr("Save the document to disk"));
|
|
170 |
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
|
|
171 |
|
|
172 |
saveAsAct = new QAction(tr("Save &As..."), this);
|
|
173 |
saveAsAct->setShortcuts(QKeySequence::SaveAs);
|
|
174 |
saveAsAct->setStatusTip(tr("Save the document under a new name"));
|
|
175 |
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
|
|
176 |
|
|
177 |
closeAct = new QAction(tr("&Close"), this);
|
|
178 |
closeAct->setShortcut(tr("Ctrl+W"));
|
|
179 |
closeAct->setStatusTip(tr("Close this window"));
|
|
180 |
connect(closeAct, SIGNAL(triggered()), this, SLOT(close()));
|
|
181 |
|
|
182 |
exitAct = new QAction(tr("E&xit"), this);
|
|
183 |
exitAct->setShortcuts(QKeySequence::Quit);
|
|
184 |
exitAct->setStatusTip(tr("Exit the application"));
|
|
185 |
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
|
|
186 |
|
|
187 |
cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
|
|
188 |
cutAct->setShortcuts(QKeySequence::Cut);
|
|
189 |
cutAct->setStatusTip(tr("Cut the current selection's contents to the "
|
|
190 |
"clipboard"));
|
|
191 |
connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
|
|
192 |
|
|
193 |
copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
|
|
194 |
copyAct->setShortcuts(QKeySequence::Copy);
|
|
195 |
copyAct->setStatusTip(tr("Copy the current selection's contents to the "
|
|
196 |
"clipboard"));
|
|
197 |
connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
|
|
198 |
|
|
199 |
pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
|
|
200 |
pasteAct->setShortcuts(QKeySequence::Paste);
|
|
201 |
pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
|
|
202 |
"selection"));
|
|
203 |
connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
|
|
204 |
|
|
205 |
aboutAct = new QAction(tr("&About"), this);
|
|
206 |
aboutAct->setStatusTip(tr("Show the application's About box"));
|
|
207 |
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
|
|
208 |
|
|
209 |
aboutQtAct = new QAction(tr("About &Qt"), this);
|
|
210 |
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
|
|
211 |
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
|
|
212 |
|
|
213 |
|
|
214 |
cutAct->setEnabled(false);
|
|
215 |
copyAct->setEnabled(false);
|
|
216 |
connect(textEdit, SIGNAL(copyAvailable(bool)),
|
|
217 |
cutAct, SLOT(setEnabled(bool)));
|
|
218 |
connect(textEdit, SIGNAL(copyAvailable(bool)),
|
|
219 |
copyAct, SLOT(setEnabled(bool)));
|
|
220 |
}
|
|
221 |
|
|
222 |
//! [implicit tr context]
|
|
223 |
void MainWindow::createMenus()
|
|
224 |
{
|
|
225 |
fileMenu = menuBar()->addMenu(tr("&File"));
|
|
226 |
//! [implicit tr context]
|
|
227 |
fileMenu->addAction(newAct);
|
|
228 |
fileMenu->addAction(openAct);
|
|
229 |
fileMenu->addAction(saveAct);
|
|
230 |
fileMenu->addAction(saveAsAct);
|
|
231 |
fileMenu->addSeparator();
|
|
232 |
fileMenu->addAction(closeAct);
|
|
233 |
fileMenu->addAction(exitAct);
|
|
234 |
|
|
235 |
editMenu = menuBar()->addMenu(tr("&Edit"));
|
|
236 |
editMenu->addAction(cutAct);
|
|
237 |
editMenu->addAction(copyAct);
|
|
238 |
editMenu->addAction(pasteAct);
|
|
239 |
|
|
240 |
menuBar()->addSeparator();
|
|
241 |
|
|
242 |
helpMenu = menuBar()->addMenu(tr("&Help"));
|
|
243 |
helpMenu->addAction(aboutAct);
|
|
244 |
helpMenu->addAction(aboutQtAct);
|
|
245 |
}
|
|
246 |
|
|
247 |
void MainWindow::createToolBars()
|
|
248 |
{
|
|
249 |
//! [0]
|
|
250 |
fileToolBar = addToolBar(tr("File"));
|
|
251 |
fileToolBar->addAction(newAct);
|
|
252 |
fileToolBar->addAction(openAct);
|
|
253 |
//! [0]
|
|
254 |
fileToolBar->addAction(saveAct);
|
|
255 |
|
|
256 |
editToolBar = addToolBar(tr("Edit"));
|
|
257 |
editToolBar->addAction(cutAct);
|
|
258 |
editToolBar->addAction(copyAct);
|
|
259 |
editToolBar->addAction(pasteAct);
|
|
260 |
}
|
|
261 |
|
|
262 |
void MainWindow::createStatusBar()
|
|
263 |
{
|
|
264 |
statusBar()->showMessage(tr("Ready"));
|
|
265 |
}
|
|
266 |
|
|
267 |
void MainWindow::readSettings()
|
|
268 |
{
|
|
269 |
QSettings settings;
|
|
270 |
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
|
|
271 |
QSize size = settings.value("size", QSize(400, 400)).toSize();
|
|
272 |
move(pos);
|
|
273 |
resize(size);
|
|
274 |
}
|
|
275 |
|
|
276 |
void MainWindow::writeSettings()
|
|
277 |
{
|
|
278 |
QSettings settings;
|
|
279 |
settings.setValue("pos", pos());
|
|
280 |
settings.setValue("size", size());
|
|
281 |
}
|
|
282 |
|
|
283 |
bool MainWindow::maybeSave()
|
|
284 |
{
|
|
285 |
if (textEdit->document()->isModified()) {
|
|
286 |
QMessageBox::StandardButton ret;
|
|
287 |
ret = QMessageBox::warning(this, tr("SDI"),
|
|
288 |
tr("The document has been modified.\n"
|
|
289 |
"Do you want to save your changes?"),
|
|
290 |
QMessageBox::Save | QMessageBox::Discard
|
|
291 |
| QMessageBox::Cancel);
|
|
292 |
if (ret == QMessageBox::Save)
|
|
293 |
return save();
|
|
294 |
else if (ret == QMessageBox::Cancel)
|
|
295 |
return false;
|
|
296 |
}
|
|
297 |
return true;
|
|
298 |
}
|
|
299 |
|
|
300 |
void MainWindow::loadFile(const QString &fileName)
|
|
301 |
{
|
|
302 |
|
|
303 |
QFile file(fileName);
|
|
304 |
if (!file.open(QFile::ReadOnly | QFile::Text)) {
|
|
305 |
QMessageBox::warning(this, tr("SDI"),
|
|
306 |
tr("Cannot read file %1:\n%2.")
|
|
307 |
.arg(fileName)
|
|
308 |
.arg(file.errorString()));
|
|
309 |
return;
|
|
310 |
}
|
|
311 |
|
|
312 |
QTextStream in(&file);
|
|
313 |
QApplication::setOverrideCursor(Qt::WaitCursor);
|
|
314 |
textEdit->setPlainText(in.readAll());
|
|
315 |
QApplication::restoreOverrideCursor();
|
|
316 |
|
|
317 |
setCurrentFile(fileName);
|
|
318 |
statusBar()->showMessage(tr("File loaded"), 2000);
|
|
319 |
}
|
|
320 |
|
|
321 |
bool MainWindow::saveFile(const QString &fileName)
|
|
322 |
{
|
|
323 |
QFile file(fileName);
|
|
324 |
if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
|
325 |
QMessageBox::warning(this, tr("SDI"),
|
|
326 |
tr("Cannot write file %1:\n%2.")
|
|
327 |
.arg(fileName)
|
|
328 |
.arg(file.errorString()));
|
|
329 |
return false;
|
|
330 |
}
|
|
331 |
|
|
332 |
QTextStream out(&file);
|
|
333 |
QApplication::setOverrideCursor(Qt::WaitCursor);
|
|
334 |
out << textEdit->toPlainText();
|
|
335 |
QApplication::restoreOverrideCursor();
|
|
336 |
|
|
337 |
setCurrentFile(fileName);
|
|
338 |
statusBar()->showMessage(tr("File saved"), 2000);
|
|
339 |
return true;
|
|
340 |
}
|
|
341 |
|
|
342 |
void MainWindow::setCurrentFile(const QString &fileName)
|
|
343 |
{
|
|
344 |
static int sequenceNumber = 1;
|
|
345 |
|
|
346 |
isUntitled = fileName.isEmpty();
|
|
347 |
if (isUntitled) {
|
|
348 |
curFile = tr("document%1.txt").arg(sequenceNumber++);
|
|
349 |
} else {
|
|
350 |
curFile = QFileInfo(fileName).canonicalFilePath();
|
|
351 |
}
|
|
352 |
|
|
353 |
textEdit->document()->setModified(false);
|
|
354 |
setWindowModified(false);
|
|
355 |
setWindowFilePath(curFile);
|
|
356 |
}
|
|
357 |
|
|
358 |
QString MainWindow::strippedName(const QString &fullFileName)
|
|
359 |
{
|
|
360 |
return QFileInfo(fullFileName).fileName();
|
|
361 |
}
|
|
362 |
|
|
363 |
MainWindow *MainWindow::findMainWindow(const QString &fileName)
|
|
364 |
{
|
|
365 |
QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
|
|
366 |
|
|
367 |
foreach (QWidget *widget, qApp->topLevelWidgets()) {
|
|
368 |
MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
|
|
369 |
if (mainWin && mainWin->curFile == canonicalFilePath)
|
|
370 |
return mainWin;
|
|
371 |
}
|
|
372 |
return 0;
|
|
373 |
}
|