source: trunk/examples/mainwindows/sdi/mainwindow.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 11.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42
43#include "mainwindow.h"
44
45MainWindow::MainWindow()
46{
47 init();
48 setCurrentFile("");
49}
50
51MainWindow::MainWindow(const QString &fileName)
52{
53 init();
54 loadFile(fileName);
55}
56
57void MainWindow::closeEvent(QCloseEvent *event)
58{
59 if (maybeSave()) {
60 writeSettings();
61 event->accept();
62 } else {
63 event->ignore();
64 }
65}
66
67void MainWindow::newFile()
68{
69 MainWindow *other = new MainWindow;
70 other->move(x() + 40, y() + 40);
71 other->show();
72}
73
74void MainWindow::open()
75{
76 QString fileName = QFileDialog::getOpenFileName(this);
77 if (!fileName.isEmpty()) {
78 MainWindow *existing = findMainWindow(fileName);
79 if (existing) {
80 existing->show();
81 existing->raise();
82 existing->activateWindow();
83 return;
84 }
85
86 if (isUntitled && textEdit->document()->isEmpty()
87 && !isWindowModified()) {
88 loadFile(fileName);
89 } else {
90 MainWindow *other = new MainWindow(fileName);
91 if (other->isUntitled) {
92 delete other;
93 return;
94 }
95 other->move(x() + 40, y() + 40);
96 other->show();
97 }
98 }
99}
100
101bool MainWindow::save()
102{
103 if (isUntitled) {
104 return saveAs();
105 } else {
106 return saveFile(curFile);
107 }
108}
109
110bool MainWindow::saveAs()
111{
112 QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
113 curFile);
114 if (fileName.isEmpty())
115 return false;
116
117 return saveFile(fileName);
118}
119
120void MainWindow::about()
121{
122 QMessageBox::about(this, tr("About SDI"),
123 tr("The <b>SDI</b> example demonstrates how to write single "
124 "document interface applications using Qt."));
125}
126
127void MainWindow::documentWasModified()
128{
129 setWindowModified(true);
130}
131
132void MainWindow::init()
133{
134 setAttribute(Qt::WA_DeleteOnClose);
135
136 isUntitled = true;
137
138 textEdit = new QTextEdit;
139 setCentralWidget(textEdit);
140
141 createActions();
142 createMenus();
143 createToolBars();
144 createStatusBar();
145
146 readSettings();
147
148 connect(textEdit->document(), SIGNAL(contentsChanged()),
149 this, SLOT(documentWasModified()));
150
151 setUnifiedTitleAndToolBarOnMac(true);
152}
153
154void MainWindow::createActions()
155{
156 newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
157 newAct->setShortcuts(QKeySequence::New);
158 newAct->setStatusTip(tr("Create a new file"));
159 connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
160
161 openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
162 openAct->setShortcuts(QKeySequence::Open);
163 openAct->setStatusTip(tr("Open an existing file"));
164 connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
165
166 saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
167 saveAct->setShortcuts(QKeySequence::Save);
168 saveAct->setStatusTip(tr("Save the document to disk"));
169 connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
170
171 saveAsAct = new QAction(tr("Save &As..."), this);
172 saveAsAct->setShortcuts(QKeySequence::SaveAs);
173 saveAsAct->setStatusTip(tr("Save the document under a new name"));
174 connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
175
176 closeAct = new QAction(tr("&Close"), this);
177 closeAct->setShortcut(tr("Ctrl+W"));
178 closeAct->setStatusTip(tr("Close this window"));
179 connect(closeAct, SIGNAL(triggered()), this, SLOT(close()));
180
181 exitAct = new QAction(tr("E&xit"), this);
182 exitAct->setShortcuts(QKeySequence::Quit);
183 exitAct->setStatusTip(tr("Exit the application"));
184 connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
185
186 cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
187 cutAct->setShortcuts(QKeySequence::Cut);
188 cutAct->setStatusTip(tr("Cut the current selection's contents to the "
189 "clipboard"));
190 connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
191
192 copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
193 copyAct->setShortcuts(QKeySequence::Copy);
194 copyAct->setStatusTip(tr("Copy the current selection's contents to the "
195 "clipboard"));
196 connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
197
198 pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
199 pasteAct->setShortcuts(QKeySequence::Paste);
200 pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
201 "selection"));
202 connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
203
204 aboutAct = new QAction(tr("&About"), this);
205 aboutAct->setStatusTip(tr("Show the application's About box"));
206 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
207
208 aboutQtAct = new QAction(tr("About &Qt"), this);
209 aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
210 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
211
212
213 cutAct->setEnabled(false);
214 copyAct->setEnabled(false);
215 connect(textEdit, SIGNAL(copyAvailable(bool)),
216 cutAct, SLOT(setEnabled(bool)));
217 connect(textEdit, SIGNAL(copyAvailable(bool)),
218 copyAct, SLOT(setEnabled(bool)));
219}
220
221//! [implicit tr context]
222void MainWindow::createMenus()
223{
224 fileMenu = menuBar()->addMenu(tr("&File"));
225//! [implicit tr context]
226 fileMenu->addAction(newAct);
227 fileMenu->addAction(openAct);
228 fileMenu->addAction(saveAct);
229 fileMenu->addAction(saveAsAct);
230 fileMenu->addSeparator();
231 fileMenu->addAction(closeAct);
232 fileMenu->addAction(exitAct);
233
234 editMenu = menuBar()->addMenu(tr("&Edit"));
235 editMenu->addAction(cutAct);
236 editMenu->addAction(copyAct);
237 editMenu->addAction(pasteAct);
238
239 menuBar()->addSeparator();
240
241 helpMenu = menuBar()->addMenu(tr("&Help"));
242 helpMenu->addAction(aboutAct);
243 helpMenu->addAction(aboutQtAct);
244}
245
246void MainWindow::createToolBars()
247{
248//! [0]
249 fileToolBar = addToolBar(tr("File"));
250 fileToolBar->addAction(newAct);
251 fileToolBar->addAction(openAct);
252//! [0]
253 fileToolBar->addAction(saveAct);
254
255 editToolBar = addToolBar(tr("Edit"));
256 editToolBar->addAction(cutAct);
257 editToolBar->addAction(copyAct);
258 editToolBar->addAction(pasteAct);
259}
260
261void MainWindow::createStatusBar()
262{
263 statusBar()->showMessage(tr("Ready"));
264}
265
266void MainWindow::readSettings()
267{
268 QSettings settings;
269 QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
270 QSize size = settings.value("size", QSize(400, 400)).toSize();
271 move(pos);
272 resize(size);
273}
274
275void MainWindow::writeSettings()
276{
277 QSettings settings;
278 settings.setValue("pos", pos());
279 settings.setValue("size", size());
280}
281
282bool MainWindow::maybeSave()
283{
284 if (textEdit->document()->isModified()) {
285 QMessageBox::StandardButton ret;
286 ret = QMessageBox::warning(this, tr("SDI"),
287 tr("The document has been modified.\n"
288 "Do you want to save your changes?"),
289 QMessageBox::Save | QMessageBox::Discard
290 | QMessageBox::Cancel);
291 if (ret == QMessageBox::Save)
292 return save();
293 else if (ret == QMessageBox::Cancel)
294 return false;
295 }
296 return true;
297}
298
299void MainWindow::loadFile(const QString &fileName)
300{
301
302 QFile file(fileName);
303 if (!file.open(QFile::ReadOnly | QFile::Text)) {
304 QMessageBox::warning(this, tr("SDI"),
305 tr("Cannot read file %1:\n%2.")
306 .arg(fileName)
307 .arg(file.errorString()));
308 return;
309 }
310
311 QTextStream in(&file);
312 QApplication::setOverrideCursor(Qt::WaitCursor);
313 textEdit->setPlainText(in.readAll());
314 QApplication::restoreOverrideCursor();
315
316 setCurrentFile(fileName);
317 statusBar()->showMessage(tr("File loaded"), 2000);
318}
319
320bool MainWindow::saveFile(const QString &fileName)
321{
322 QFile file(fileName);
323 if (!file.open(QFile::WriteOnly | QFile::Text)) {
324 QMessageBox::warning(this, tr("SDI"),
325 tr("Cannot write file %1:\n%2.")
326 .arg(fileName)
327 .arg(file.errorString()));
328 return false;
329 }
330
331 QTextStream out(&file);
332 QApplication::setOverrideCursor(Qt::WaitCursor);
333 out << textEdit->toPlainText();
334 QApplication::restoreOverrideCursor();
335
336 setCurrentFile(fileName);
337 statusBar()->showMessage(tr("File saved"), 2000);
338 return true;
339}
340
341void MainWindow::setCurrentFile(const QString &fileName)
342{
343 static int sequenceNumber = 1;
344
345 isUntitled = fileName.isEmpty();
346 if (isUntitled) {
347 curFile = tr("document%1.txt").arg(sequenceNumber++);
348 } else {
349 curFile = QFileInfo(fileName).canonicalFilePath();
350 }
351
352 textEdit->document()->setModified(false);
353 setWindowModified(false);
354 setWindowFilePath(curFile);
355}
356
357QString MainWindow::strippedName(const QString &fullFileName)
358{
359 return QFileInfo(fullFileName).fileName();
360}
361
362MainWindow *MainWindow::findMainWindow(const QString &fileName)
363{
364 QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
365
366 foreach (QWidget *widget, qApp->topLevelWidgets()) {
367 MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
368 if (mainWin && mainWin->curFile == canonicalFilePath)
369 return mainWin;
370 }
371 return 0;
372}
Note: See TracBrowser for help on using the repository browser.