New patches from Fedora.
[fedora-mingw.git] / qt-win / mainwindow.cpp
1 /****************************************************************************
2     **
3     ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
4     **
5     ** This file is part of the documentation of the Qt Toolkit.
6     **
7     ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file.  Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
13 **
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
18     **
19     ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20     ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21     **
22     ****************************************************************************/
23
24     #include <QtGui>
25
26     #include "mainwindow.h"
27
28     MainWindow::MainWindow()
29     {
30         textEdit = new QTextEdit;
31         setCentralWidget(textEdit);
32
33         createActions();
34         createMenus();
35         createToolBars();
36         createStatusBar();
37
38         readSettings();
39
40         connect(textEdit->document(), SIGNAL(contentsChanged()),
41                 this, SLOT(documentWasModified()));
42
43         setCurrentFile("");
44     }
45
46     void MainWindow::closeEvent(QCloseEvent *event)
47     {
48         if (maybeSave()) {
49             writeSettings();
50             event->accept();
51         } else {
52             event->ignore();
53         }
54     }
55
56     void MainWindow::newFile()
57     {
58         if (maybeSave()) {
59             textEdit->clear();
60             setCurrentFile("");
61         }
62     }
63
64     void MainWindow::open()
65     {
66         if (maybeSave()) {
67             QString fileName = QFileDialog::getOpenFileName(this);
68             if (!fileName.isEmpty())
69                 loadFile(fileName);
70         }
71     }
72
73     bool MainWindow::save()
74     {
75         if (curFile.isEmpty()) {
76             return saveAs();
77         } else {
78             return saveFile(curFile);
79         }
80     }
81
82     bool MainWindow::saveAs()
83     {
84         QString fileName = QFileDialog::getSaveFileName(this);
85         if (fileName.isEmpty())
86             return false;
87
88         return saveFile(fileName);
89     }
90
91     void MainWindow::about()
92     {
93        QMessageBox::about(this, tr("About Application"),
94                 tr("The <b>Application</b> example demonstrates how to "
95                    "write modern GUI applications using Qt, with a menu bar, "
96                    "toolbars, and a status bar."));
97     }
98
99     void MainWindow::documentWasModified()
100     {
101         setWindowModified(true);
102     }
103
104     void MainWindow::createActions()
105     {
106         newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
107         newAct->setShortcut(tr("Ctrl+N"));
108         newAct->setStatusTip(tr("Create a new file"));
109         connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
110
111         openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
112         openAct->setShortcut(tr("Ctrl+O"));
113         openAct->setStatusTip(tr("Open an existing file"));
114         connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
115
116         saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
117         saveAct->setShortcut(tr("Ctrl+S"));
118         saveAct->setStatusTip(tr("Save the document to disk"));
119         connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
120
121         saveAsAct = new QAction(tr("Save &As..."), this);
122         saveAsAct->setStatusTip(tr("Save the document under a new name"));
123         connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
124
125         exitAct = new QAction(tr("E&xit"), this);
126         exitAct->setShortcut(tr("Ctrl+Q"));
127         exitAct->setStatusTip(tr("Exit the application"));
128         connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
129
130         cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
131         cutAct->setShortcut(tr("Ctrl+X"));
132         cutAct->setStatusTip(tr("Cut the current selection's contents to the "
133                                 "clipboard"));
134         connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
135
136         copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
137         copyAct->setShortcut(tr("Ctrl+C"));
138         copyAct->setStatusTip(tr("Copy the current selection's contents to the "
139                                  "clipboard"));
140         connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
141
142         pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
143         pasteAct->setShortcut(tr("Ctrl+V"));
144         pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
145                                   "selection"));
146         connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
147
148         aboutAct = new QAction(tr("&About"), this);
149         aboutAct->setStatusTip(tr("Show the application's About box"));
150         connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
151
152         aboutQtAct = new QAction(tr("About &Qt"), this);
153         aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
154         connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
155
156         cutAct->setEnabled(false);
157         copyAct->setEnabled(false);
158         connect(textEdit, SIGNAL(copyAvailable(bool)),
159                 cutAct, SLOT(setEnabled(bool)));
160         connect(textEdit, SIGNAL(copyAvailable(bool)),
161                 copyAct, SLOT(setEnabled(bool)));
162     }
163
164     void MainWindow::createMenus()
165     {
166         fileMenu = menuBar()->addMenu(tr("&File"));
167         fileMenu->addAction(newAct);
168         fileMenu->addAction(openAct);
169         fileMenu->addAction(saveAct);
170         fileMenu->addAction(saveAsAct);
171         fileMenu->addSeparator();
172         fileMenu->addAction(exitAct);
173
174         editMenu = menuBar()->addMenu(tr("&Edit"));
175         editMenu->addAction(cutAct);
176         editMenu->addAction(copyAct);
177         editMenu->addAction(pasteAct);
178
179         menuBar()->addSeparator();
180
181         helpMenu = menuBar()->addMenu(tr("&Help"));
182         helpMenu->addAction(aboutAct);
183         helpMenu->addAction(aboutQtAct);
184     }
185
186     void MainWindow::createToolBars()
187     {
188         fileToolBar = addToolBar(tr("File"));
189         fileToolBar->addAction(newAct);
190         fileToolBar->addAction(openAct);
191         fileToolBar->addAction(saveAct);
192
193         editToolBar = addToolBar(tr("Edit"));
194         editToolBar->addAction(cutAct);
195         editToolBar->addAction(copyAct);
196         editToolBar->addAction(pasteAct);
197     }
198
199     void MainWindow::createStatusBar()
200     {
201         statusBar()->showMessage(tr("Ready"));
202     }
203
204     void MainWindow::readSettings()
205     {
206         QSettings settings("Trolltech", "Application Example");
207         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
208         QSize size = settings.value("size", QSize(400, 400)).toSize();
209         resize(size);
210         move(pos);
211     }
212
213     void MainWindow::writeSettings()
214     {
215         QSettings settings("Trolltech", "Application Example");
216         settings.setValue("pos", pos());
217         settings.setValue("size", size());
218     }
219
220     bool MainWindow::maybeSave()
221     {
222         if (textEdit->document()->isModified()) {
223             int ret = QMessageBox::warning(this, tr("Application"),
224                          tr("The document has been modified.\n"
225                             "Do you want to save your changes?"),
226                          QMessageBox::Yes | QMessageBox::Default,
227                          QMessageBox::No,
228                          QMessageBox::Cancel | QMessageBox::Escape);
229             if (ret == QMessageBox::Yes)
230                 return save();
231             else if (ret == QMessageBox::Cancel)
232                 return false;
233         }
234         return true;
235     }
236
237     void MainWindow::loadFile(const QString &fileName)
238     {
239         QFile file(fileName);
240         if (!file.open(QFile::ReadOnly | QFile::Text)) {
241             QMessageBox::warning(this, tr("Application"),
242                                  tr("Cannot read file %1:\n%2.")
243                                  .arg(fileName)
244                                  .arg(file.errorString()));
245             return;
246         }
247
248         QTextStream in(&file);
249         QApplication::setOverrideCursor(Qt::WaitCursor);
250         textEdit->setPlainText(in.readAll());
251         QApplication::restoreOverrideCursor();
252
253         setCurrentFile(fileName);
254         statusBar()->showMessage(tr("File loaded"), 2000);
255     }
256
257     bool MainWindow::saveFile(const QString &fileName)
258     {
259         QFile file(fileName);
260         if (!file.open(QFile::WriteOnly | QFile::Text)) {
261             QMessageBox::warning(this, tr("Application"),
262                                  tr("Cannot write file %1:\n%2.")
263                                  .arg(fileName)
264                                  .arg(file.errorString()));
265             return false;
266         }
267
268         QTextStream out(&file);
269         QApplication::setOverrideCursor(Qt::WaitCursor);
270         out << textEdit->toPlainText();
271         QApplication::restoreOverrideCursor();
272
273         setCurrentFile(fileName);
274         statusBar()->showMessage(tr("File saved"), 2000);
275         return true;
276     }
277
278     void MainWindow::setCurrentFile(const QString &fileName)
279     {
280         curFile = fileName;
281         textEdit->document()->setModified(false);
282         setWindowModified(false);
283
284         QString shownName;
285         if (curFile.isEmpty())
286             shownName = "untitled.txt";
287         else
288             shownName = strippedName(curFile);
289
290         setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Application")));
291     }
292
293     QString MainWindow::strippedName(const QString &fullFileName)
294     {
295         return QFileInfo(fullFileName).fileName();
296     }