source: trunk/examples/mainwindows/application/mainwindow.cpp@ 1147

Last change on this file since 1147 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: 10.8 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//! [0]
42#include <QtGui>
43
44#include "mainwindow.h"
45//! [0]
46
47//! [1]
48MainWindow::MainWindow()
49//! [1] //! [2]
50{
51 textEdit = new QPlainTextEdit;
52 setCentralWidget(textEdit);
53
54 createActions();
55 createMenus();
56 createToolBars();
57 createStatusBar();
58
59 readSettings();
60
61 connect(textEdit->document(), SIGNAL(contentsChanged()),
62 this, SLOT(documentWasModified()));
63
64 setCurrentFile("");
65 setUnifiedTitleAndToolBarOnMac(true);
66}
67//! [2]
68
69//! [3]
70void MainWindow::closeEvent(QCloseEvent *event)
71//! [3] //! [4]
72{
73 if (maybeSave()) {
74 writeSettings();
75 event->accept();
76 } else {
77 event->ignore();
78 }
79}
80//! [4]
81
82//! [5]
83void MainWindow::newFile()
84//! [5] //! [6]
85{
86 if (maybeSave()) {
87 textEdit->clear();
88 setCurrentFile("");
89 }
90}
91//! [6]
92
93//! [7]
94void MainWindow::open()
95//! [7] //! [8]
96{
97 if (maybeSave()) {
98 QString fileName = QFileDialog::getOpenFileName(this);
99 if (!fileName.isEmpty())
100 loadFile(fileName);
101 }
102}
103//! [8]
104
105//! [9]
106bool MainWindow::save()
107//! [9] //! [10]
108{
109 if (curFile.isEmpty()) {
110 return saveAs();
111 } else {
112 return saveFile(curFile);
113 }
114}
115//! [10]
116
117//! [11]
118bool MainWindow::saveAs()
119//! [11] //! [12]
120{
121 QString fileName = QFileDialog::getSaveFileName(this);
122 if (fileName.isEmpty())
123 return false;
124
125 return saveFile(fileName);
126}
127//! [12]
128
129//! [13]
130void MainWindow::about()
131//! [13] //! [14]
132{
133 QMessageBox::about(this, tr("About Application"),
134 tr("The <b>Application</b> example demonstrates how to "
135 "write modern GUI applications using Qt, with a menu bar, "
136 "toolbars, and a status bar."));
137}
138//! [14]
139
140//! [15]
141void MainWindow::documentWasModified()
142//! [15] //! [16]
143{
144 setWindowModified(textEdit->document()->isModified());
145}
146//! [16]
147
148//! [17]
149void MainWindow::createActions()
150//! [17] //! [18]
151{
152 newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
153 newAct->setShortcuts(QKeySequence::New);
154 newAct->setStatusTip(tr("Create a new file"));
155 connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
156
157//! [19]
158 openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
159 openAct->setShortcuts(QKeySequence::Open);
160 openAct->setStatusTip(tr("Open an existing file"));
161 connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
162//! [18] //! [19]
163
164 saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
165 saveAct->setShortcuts(QKeySequence::Save);
166 saveAct->setStatusTip(tr("Save the document to disk"));
167 connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
168
169 saveAsAct = new QAction(tr("Save &As..."), this);
170 saveAsAct->setShortcuts(QKeySequence::SaveAs);
171 saveAsAct->setStatusTip(tr("Save the document under a new name"));
172 connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
173
174//! [20]
175 exitAct = new QAction(tr("E&xit"), this);
176 exitAct->setShortcuts(QKeySequence::Quit);
177//! [20]
178 exitAct->setStatusTip(tr("Exit the application"));
179 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
180
181//! [21]
182 cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
183//! [21]
184 cutAct->setShortcuts(QKeySequence::Cut);
185 cutAct->setStatusTip(tr("Cut the current selection's contents to the "
186 "clipboard"));
187 connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
188
189 copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
190 copyAct->setShortcuts(QKeySequence::Copy);
191 copyAct->setStatusTip(tr("Copy the current selection's contents to the "
192 "clipboard"));
193 connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
194
195 pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
196 pasteAct->setShortcuts(QKeySequence::Paste);
197 pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
198 "selection"));
199 connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
200
201 aboutAct = new QAction(tr("&About"), this);
202 aboutAct->setStatusTip(tr("Show the application's About box"));
203 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
204
205//! [22]
206 aboutQtAct = new QAction(tr("About &Qt"), this);
207 aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
208 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
209//! [22]
210
211//! [23]
212 cutAct->setEnabled(false);
213//! [23] //! [24]
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//! [24]
221
222//! [25] //! [26]
223void MainWindow::createMenus()
224//! [25] //! [27]
225{
226 fileMenu = menuBar()->addMenu(tr("&File"));
227 fileMenu->addAction(newAct);
228//! [28]
229 fileMenu->addAction(openAct);
230//! [28]
231 fileMenu->addAction(saveAct);
232//! [26]
233 fileMenu->addAction(saveAsAct);
234 fileMenu->addSeparator();
235 fileMenu->addAction(exitAct);
236
237 editMenu = menuBar()->addMenu(tr("&Edit"));
238 editMenu->addAction(cutAct);
239 editMenu->addAction(copyAct);
240 editMenu->addAction(pasteAct);
241
242 menuBar()->addSeparator();
243
244 helpMenu = menuBar()->addMenu(tr("&Help"));
245 helpMenu->addAction(aboutAct);
246 helpMenu->addAction(aboutQtAct);
247}
248//! [27]
249
250//! [29] //! [30]
251void MainWindow::createToolBars()
252{
253 fileToolBar = addToolBar(tr("File"));
254 fileToolBar->addAction(newAct);
255//! [29] //! [31]
256 fileToolBar->addAction(openAct);
257//! [31]
258 fileToolBar->addAction(saveAct);
259
260 editToolBar = addToolBar(tr("Edit"));
261 editToolBar->addAction(cutAct);
262 editToolBar->addAction(copyAct);
263 editToolBar->addAction(pasteAct);
264}
265//! [30]
266
267//! [32]
268void MainWindow::createStatusBar()
269//! [32] //! [33]
270{
271 statusBar()->showMessage(tr("Ready"));
272}
273//! [33]
274
275//! [34] //! [35]
276void MainWindow::readSettings()
277//! [34] //! [36]
278{
279 QSettings settings("Trolltech", "Application Example");
280 QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
281 QSize size = settings.value("size", QSize(400, 400)).toSize();
282 resize(size);
283 move(pos);
284}
285//! [35] //! [36]
286
287//! [37] //! [38]
288void MainWindow::writeSettings()
289//! [37] //! [39]
290{
291 QSettings settings("Trolltech", "Application Example");
292 settings.setValue("pos", pos());
293 settings.setValue("size", size());
294}
295//! [38] //! [39]
296
297//! [40]
298bool MainWindow::maybeSave()
299//! [40] //! [41]
300{
301 if (textEdit->document()->isModified()) {
302 QMessageBox::StandardButton ret;
303 ret = QMessageBox::warning(this, tr("Application"),
304 tr("The document has been modified.\n"
305 "Do you want to save your changes?"),
306 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
307 if (ret == QMessageBox::Save)
308 return save();
309 else if (ret == QMessageBox::Cancel)
310 return false;
311 }
312 return true;
313}
314//! [41]
315
316//! [42]
317void MainWindow::loadFile(const QString &fileName)
318//! [42] //! [43]
319{
320 QFile file(fileName);
321 if (!file.open(QFile::ReadOnly | QFile::Text)) {
322 QMessageBox::warning(this, tr("Application"),
323 tr("Cannot read file %1:\n%2.")
324 .arg(fileName)
325 .arg(file.errorString()));
326 return;
327 }
328
329 QTextStream in(&file);
330#ifndef QT_NO_CURSOR
331 QApplication::setOverrideCursor(Qt::WaitCursor);
332#endif
333 textEdit->setPlainText(in.readAll());
334#ifndef QT_NO_CURSOR
335 QApplication::restoreOverrideCursor();
336#endif
337
338 setCurrentFile(fileName);
339 statusBar()->showMessage(tr("File loaded"), 2000);
340}
341//! [43]
342
343//! [44]
344bool MainWindow::saveFile(const QString &fileName)
345//! [44] //! [45]
346{
347 QFile file(fileName);
348 if (!file.open(QFile::WriteOnly | QFile::Text)) {
349 QMessageBox::warning(this, tr("Application"),
350 tr("Cannot write file %1:\n%2.")
351 .arg(fileName)
352 .arg(file.errorString()));
353 return false;
354 }
355
356 QTextStream out(&file);
357#ifndef QT_NO_CURSOR
358 QApplication::setOverrideCursor(Qt::WaitCursor);
359#endif
360 out << textEdit->toPlainText();
361#ifndef QT_NO_CURSOR
362 QApplication::restoreOverrideCursor();
363#endif
364
365 setCurrentFile(fileName);
366 statusBar()->showMessage(tr("File saved"), 2000);
367 return true;
368}
369//! [45]
370
371//! [46]
372void MainWindow::setCurrentFile(const QString &fileName)
373//! [46] //! [47]
374{
375 curFile = fileName;
376 textEdit->document()->setModified(false);
377 setWindowModified(false);
378
379 QString shownName = curFile;
380 if (curFile.isEmpty())
381 shownName = "untitled.txt";
382 setWindowFilePath(shownName);
383}
384//! [47]
385
386//! [48]
387QString MainWindow::strippedName(const QString &fullFileName)
388//! [48] //! [49]
389{
390 return QFileInfo(fullFileName).fileName();
391}
392//! [49]
Note: See TracBrowser for help on using the repository browser.