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