1 /****************************************************************************
3 ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
5 ** This file is part of the documentation of the Qt Toolkit.
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
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.
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.
22 ****************************************************************************/
26 #include "mainwindow.h"
28 MainWindow::MainWindow()
30 textEdit = new QTextEdit;
31 setCentralWidget(textEdit);
40 connect(textEdit->document(), SIGNAL(contentsChanged()),
41 this, SLOT(documentWasModified()));
46 void MainWindow::closeEvent(QCloseEvent *event)
56 void MainWindow::newFile()
64 void MainWindow::open()
67 QString fileName = QFileDialog::getOpenFileName(this);
68 if (!fileName.isEmpty())
73 bool MainWindow::save()
75 if (curFile.isEmpty()) {
78 return saveFile(curFile);
82 bool MainWindow::saveAs()
84 QString fileName = QFileDialog::getSaveFileName(this);
85 if (fileName.isEmpty())
88 return saveFile(fileName);
91 void MainWindow::about()
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."));
99 void MainWindow::documentWasModified()
101 setWindowModified(true);
104 void MainWindow::createActions()
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()));
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()));
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()));
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()));
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()));
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 "
134 connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
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 "
140 connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
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 "
146 connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
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()));
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()));
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)));
164 void MainWindow::createMenus()
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);
174 editMenu = menuBar()->addMenu(tr("&Edit"));
175 editMenu->addAction(cutAct);
176 editMenu->addAction(copyAct);
177 editMenu->addAction(pasteAct);
179 menuBar()->addSeparator();
181 helpMenu = menuBar()->addMenu(tr("&Help"));
182 helpMenu->addAction(aboutAct);
183 helpMenu->addAction(aboutQtAct);
186 void MainWindow::createToolBars()
188 fileToolBar = addToolBar(tr("File"));
189 fileToolBar->addAction(newAct);
190 fileToolBar->addAction(openAct);
191 fileToolBar->addAction(saveAct);
193 editToolBar = addToolBar(tr("Edit"));
194 editToolBar->addAction(cutAct);
195 editToolBar->addAction(copyAct);
196 editToolBar->addAction(pasteAct);
199 void MainWindow::createStatusBar()
201 statusBar()->showMessage(tr("Ready"));
204 void MainWindow::readSettings()
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();
213 void MainWindow::writeSettings()
215 QSettings settings("Trolltech", "Application Example");
216 settings.setValue("pos", pos());
217 settings.setValue("size", size());
220 bool MainWindow::maybeSave()
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,
228 QMessageBox::Cancel | QMessageBox::Escape);
229 if (ret == QMessageBox::Yes)
231 else if (ret == QMessageBox::Cancel)
237 void MainWindow::loadFile(const QString &fileName)
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.")
244 .arg(file.errorString()));
248 QTextStream in(&file);
249 QApplication::setOverrideCursor(Qt::WaitCursor);
250 textEdit->setPlainText(in.readAll());
251 QApplication::restoreOverrideCursor();
253 setCurrentFile(fileName);
254 statusBar()->showMessage(tr("File loaded"), 2000);
257 bool MainWindow::saveFile(const QString &fileName)
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.")
264 .arg(file.errorString()));
268 QTextStream out(&file);
269 QApplication::setOverrideCursor(Qt::WaitCursor);
270 out << textEdit->toPlainText();
271 QApplication::restoreOverrideCursor();
273 setCurrentFile(fileName);
274 statusBar()->showMessage(tr("File saved"), 2000);
278 void MainWindow::setCurrentFile(const QString &fileName)
281 textEdit->document()->setModified(false);
282 setWindowModified(false);
285 if (curFile.isEmpty())
286 shownName = "untitled.txt";
288 shownName = strippedName(curFile);
290 setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Application")));
293 QString MainWindow::strippedName(const QString &fullFileName)
295 return QFileInfo(fullFileName).fileName();