From 38be0d13830efd2d98281c645c3a60afe05ffece Mon Sep 17 00:00:00 2001 From: Qt by Nokia Date: Wed, 27 Apr 2011 12:05:43 +0200 Subject: Initial import from the monolithic Qt. This is the beginning of revision history for this module. If you want to look at revision history older than this, please refer to the Qt Git wiki for how to use Git history grafting. At the time of writing, this wiki is located here: http://qt.gitorious.org/qt/pages/GitIntroductionWithQt If you have already performed the grafting and you don't see any history beyond this commit, try running "git log" with the "--follow" argument. Branched from the monolithic repo, Qt master branch, at commit 896db169ea224deb96c59ce8af800d019de63f12 --- examples/mainwindows/README | 40 +++ examples/mainwindows/application/application.pro | 14 + examples/mainwindows/application/application.qrc | 10 + examples/mainwindows/application/images/copy.png | Bin 0 -> 1338 bytes examples/mainwindows/application/images/cut.png | Bin 0 -> 1323 bytes examples/mainwindows/application/images/new.png | Bin 0 -> 852 bytes examples/mainwindows/application/images/open.png | Bin 0 -> 2073 bytes examples/mainwindows/application/images/paste.png | Bin 0 -> 1645 bytes examples/mainwindows/application/images/save.png | Bin 0 -> 1187 bytes examples/mainwindows/application/main.cpp | 57 ++++ examples/mainwindows/application/mainwindow.cpp | 392 +++++++++++++++++++++ examples/mainwindows/application/mainwindow.h | 105 ++++++ examples/mainwindows/dockwidgets/dockwidgets.pro | 12 + examples/mainwindows/dockwidgets/dockwidgets.qrc | 8 + examples/mainwindows/dockwidgets/images/new.png | Bin 0 -> 977 bytes examples/mainwindows/dockwidgets/images/print.png | Bin 0 -> 1732 bytes examples/mainwindows/dockwidgets/images/save.png | Bin 0 -> 1894 bytes examples/mainwindows/dockwidgets/images/undo.png | Bin 0 -> 1768 bytes examples/mainwindows/dockwidgets/main.cpp | 52 +++ examples/mainwindows/dockwidgets/mainwindow.cpp | 342 +++++++++++++++++++ examples/mainwindows/dockwidgets/mainwindow.h | 97 ++++++ examples/mainwindows/mainwindows.pro | 19 ++ examples/mainwindows/mdi/images/copy.png | Bin 0 -> 1338 bytes examples/mainwindows/mdi/images/cut.png | Bin 0 -> 1323 bytes examples/mainwindows/mdi/images/new.png | Bin 0 -> 852 bytes examples/mainwindows/mdi/images/open.png | Bin 0 -> 2073 bytes examples/mainwindows/mdi/images/paste.png | Bin 0 -> 1645 bytes examples/mainwindows/mdi/images/save.png | Bin 0 -> 1187 bytes examples/mainwindows/mdi/main.cpp | 53 +++ examples/mainwindows/mdi/mainwindow.cpp | 397 ++++++++++++++++++++++ examples/mainwindows/mdi/mainwindow.h | 118 +++++++ examples/mainwindows/mdi/mdi.pro | 14 + examples/mainwindows/mdi/mdi.qrc | 10 + examples/mainwindows/mdi/mdichild.cpp | 175 ++++++++++ examples/mainwindows/mdi/mdichild.h | 76 +++++ examples/mainwindows/menus/main.cpp | 51 +++ examples/mainwindows/menus/mainwindow.cpp | 370 ++++++++++++++++++++ examples/mainwindows/menus/mainwindow.h | 124 +++++++ examples/mainwindows/menus/menus.pro | 14 + examples/mainwindows/recentfiles/main.cpp | 53 +++ examples/mainwindows/recentfiles/mainwindow.cpp | 251 ++++++++++++++ examples/mainwindows/recentfiles/mainwindow.h | 96 ++++++ examples/mainwindows/recentfiles/recentfiles.pro | 11 + examples/mainwindows/sdi/images/copy.png | Bin 0 -> 1338 bytes examples/mainwindows/sdi/images/cut.png | Bin 0 -> 1323 bytes examples/mainwindows/sdi/images/new.png | Bin 0 -> 852 bytes examples/mainwindows/sdi/images/open.png | Bin 0 -> 2073 bytes examples/mainwindows/sdi/images/paste.png | Bin 0 -> 1645 bytes examples/mainwindows/sdi/images/save.png | Bin 0 -> 1187 bytes examples/mainwindows/sdi/main.cpp | 54 +++ examples/mainwindows/sdi/mainwindow.cpp | 372 ++++++++++++++++++++ examples/mainwindows/sdi/mainwindow.h | 110 ++++++ examples/mainwindows/sdi/sdi.pro | 12 + examples/mainwindows/sdi/sdi.qrc | 10 + 54 files changed, 3519 insertions(+) create mode 100644 examples/mainwindows/README create mode 100644 examples/mainwindows/application/application.pro create mode 100644 examples/mainwindows/application/application.qrc create mode 100644 examples/mainwindows/application/images/copy.png create mode 100644 examples/mainwindows/application/images/cut.png create mode 100644 examples/mainwindows/application/images/new.png create mode 100644 examples/mainwindows/application/images/open.png create mode 100644 examples/mainwindows/application/images/paste.png create mode 100644 examples/mainwindows/application/images/save.png create mode 100644 examples/mainwindows/application/main.cpp create mode 100644 examples/mainwindows/application/mainwindow.cpp create mode 100644 examples/mainwindows/application/mainwindow.h create mode 100644 examples/mainwindows/dockwidgets/dockwidgets.pro create mode 100644 examples/mainwindows/dockwidgets/dockwidgets.qrc create mode 100644 examples/mainwindows/dockwidgets/images/new.png create mode 100644 examples/mainwindows/dockwidgets/images/print.png create mode 100644 examples/mainwindows/dockwidgets/images/save.png create mode 100644 examples/mainwindows/dockwidgets/images/undo.png create mode 100644 examples/mainwindows/dockwidgets/main.cpp create mode 100644 examples/mainwindows/dockwidgets/mainwindow.cpp create mode 100644 examples/mainwindows/dockwidgets/mainwindow.h create mode 100644 examples/mainwindows/mainwindows.pro create mode 100644 examples/mainwindows/mdi/images/copy.png create mode 100644 examples/mainwindows/mdi/images/cut.png create mode 100644 examples/mainwindows/mdi/images/new.png create mode 100644 examples/mainwindows/mdi/images/open.png create mode 100644 examples/mainwindows/mdi/images/paste.png create mode 100644 examples/mainwindows/mdi/images/save.png create mode 100644 examples/mainwindows/mdi/main.cpp create mode 100644 examples/mainwindows/mdi/mainwindow.cpp create mode 100644 examples/mainwindows/mdi/mainwindow.h create mode 100644 examples/mainwindows/mdi/mdi.pro create mode 100644 examples/mainwindows/mdi/mdi.qrc create mode 100644 examples/mainwindows/mdi/mdichild.cpp create mode 100644 examples/mainwindows/mdi/mdichild.h create mode 100644 examples/mainwindows/menus/main.cpp create mode 100644 examples/mainwindows/menus/mainwindow.cpp create mode 100644 examples/mainwindows/menus/mainwindow.h create mode 100644 examples/mainwindows/menus/menus.pro create mode 100644 examples/mainwindows/recentfiles/main.cpp create mode 100644 examples/mainwindows/recentfiles/mainwindow.cpp create mode 100644 examples/mainwindows/recentfiles/mainwindow.h create mode 100644 examples/mainwindows/recentfiles/recentfiles.pro create mode 100644 examples/mainwindows/sdi/images/copy.png create mode 100644 examples/mainwindows/sdi/images/cut.png create mode 100644 examples/mainwindows/sdi/images/new.png create mode 100644 examples/mainwindows/sdi/images/open.png create mode 100644 examples/mainwindows/sdi/images/paste.png create mode 100644 examples/mainwindows/sdi/images/save.png create mode 100644 examples/mainwindows/sdi/main.cpp create mode 100644 examples/mainwindows/sdi/mainwindow.cpp create mode 100644 examples/mainwindows/sdi/mainwindow.h create mode 100644 examples/mainwindows/sdi/sdi.pro create mode 100644 examples/mainwindows/sdi/sdi.qrc (limited to 'examples/mainwindows') diff --git a/examples/mainwindows/README b/examples/mainwindows/README new file mode 100644 index 0000000000..15a6c23e4b --- /dev/null +++ b/examples/mainwindows/README @@ -0,0 +1,40 @@ +All the standard features of application main windows are provided by Qt. + +Main windows can have pull down menus, tool bars, and dock windows. These +separate forms of user input are unified in an integrated action system that +also supports keyboard shortcuts and accelerator keys in menu items. + + +The example launcher provided with Qt can be used to explore each of the +examples in this directory. + +Documentation for these examples can be found via the Tutorial and Examples +link in the main Qt documentation. + + +Finding the Qt Examples and Demos launcher +========================================== + +On Windows: + +The launcher can be accessed via the Windows Start menu. Select the menu +entry entitled "Qt Examples and Demos" entry in the submenu containing +the Qt tools. + +On Mac OS X: + +For the binary distribution, the qtdemo executable is installed in the +/Developer/Applications/Qt directory. For the source distribution, it is +installed alongside the other Qt tools on the path specified when Qt is +configured. + +On Unix/Linux: + +The qtdemo executable is installed alongside the other Qt tools on the path +specified when Qt is configured. + +On all platforms: + +The source code for the launcher can be found in the demos/qtdemo directory +in the Qt package. This example is built at the same time as the Qt libraries, +tools, examples, and demonstrations. diff --git a/examples/mainwindows/application/application.pro b/examples/mainwindows/application/application.pro new file mode 100644 index 0000000000..285568da85 --- /dev/null +++ b/examples/mainwindows/application/application.pro @@ -0,0 +1,14 @@ +HEADERS = mainwindow.h +SOURCES = main.cpp \ + mainwindow.cpp +#! [0] +RESOURCES = application.qrc +#! [0] + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/application +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS application.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/application +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/mainwindows/application/application.qrc b/examples/mainwindows/application/application.qrc new file mode 100644 index 0000000000..0a776fab4d --- /dev/null +++ b/examples/mainwindows/application/application.qrc @@ -0,0 +1,10 @@ + + + images/copy.png + images/cut.png + images/new.png + images/open.png + images/paste.png + images/save.png + + diff --git a/examples/mainwindows/application/images/copy.png b/examples/mainwindows/application/images/copy.png new file mode 100644 index 0000000000..2aeb28288f Binary files /dev/null and b/examples/mainwindows/application/images/copy.png differ diff --git a/examples/mainwindows/application/images/cut.png b/examples/mainwindows/application/images/cut.png new file mode 100644 index 0000000000..54638e9386 Binary files /dev/null and b/examples/mainwindows/application/images/cut.png differ diff --git a/examples/mainwindows/application/images/new.png b/examples/mainwindows/application/images/new.png new file mode 100644 index 0000000000..12131b0100 Binary files /dev/null and b/examples/mainwindows/application/images/new.png differ diff --git a/examples/mainwindows/application/images/open.png b/examples/mainwindows/application/images/open.png new file mode 100644 index 0000000000..45fa2883a7 Binary files /dev/null and b/examples/mainwindows/application/images/open.png differ diff --git a/examples/mainwindows/application/images/paste.png b/examples/mainwindows/application/images/paste.png new file mode 100644 index 0000000000..c14425cad1 Binary files /dev/null and b/examples/mainwindows/application/images/paste.png differ diff --git a/examples/mainwindows/application/images/save.png b/examples/mainwindows/application/images/save.png new file mode 100644 index 0000000000..daba865faf Binary files /dev/null and b/examples/mainwindows/application/images/save.png differ diff --git a/examples/mainwindows/application/main.cpp b/examples/mainwindows/application/main.cpp new file mode 100644 index 0000000000..9a9dc9ca76 --- /dev/null +++ b/examples/mainwindows/application/main.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(application); + + QApplication app(argc, argv); + app.setOrganizationName("Trolltech"); + app.setApplicationName("Application Example"); + MainWindow mainWin; + mainWin.show(); + return app.exec(); +} +//! [0] diff --git a/examples/mainwindows/application/mainwindow.cpp b/examples/mainwindows/application/mainwindow.cpp new file mode 100644 index 0000000000..4ebc5c169e --- /dev/null +++ b/examples/mainwindows/application/mainwindow.cpp @@ -0,0 +1,392 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include + +#include "mainwindow.h" +//! [0] + +//! [1] +MainWindow::MainWindow() +//! [1] //! [2] +{ + textEdit = new QPlainTextEdit; + setCentralWidget(textEdit); + + createActions(); + createMenus(); + createToolBars(); + createStatusBar(); + + readSettings(); + + connect(textEdit->document(), SIGNAL(contentsChanged()), + this, SLOT(documentWasModified())); + + setCurrentFile(""); + setUnifiedTitleAndToolBarOnMac(true); +} +//! [2] + +//! [3] +void MainWindow::closeEvent(QCloseEvent *event) +//! [3] //! [4] +{ + if (maybeSave()) { + writeSettings(); + event->accept(); + } else { + event->ignore(); + } +} +//! [4] + +//! [5] +void MainWindow::newFile() +//! [5] //! [6] +{ + if (maybeSave()) { + textEdit->clear(); + setCurrentFile(""); + } +} +//! [6] + +//! [7] +void MainWindow::open() +//! [7] //! [8] +{ + if (maybeSave()) { + QString fileName = QFileDialog::getOpenFileName(this); + if (!fileName.isEmpty()) + loadFile(fileName); + } +} +//! [8] + +//! [9] +bool MainWindow::save() +//! [9] //! [10] +{ + if (curFile.isEmpty()) { + return saveAs(); + } else { + return saveFile(curFile); + } +} +//! [10] + +//! [11] +bool MainWindow::saveAs() +//! [11] //! [12] +{ + QString fileName = QFileDialog::getSaveFileName(this); + if (fileName.isEmpty()) + return false; + + return saveFile(fileName); +} +//! [12] + +//! [13] +void MainWindow::about() +//! [13] //! [14] +{ + QMessageBox::about(this, tr("About Application"), + tr("The Application example demonstrates how to " + "write modern GUI applications using Qt, with a menu bar, " + "toolbars, and a status bar.")); +} +//! [14] + +//! [15] +void MainWindow::documentWasModified() +//! [15] //! [16] +{ + setWindowModified(textEdit->document()->isModified()); +} +//! [16] + +//! [17] +void MainWindow::createActions() +//! [17] //! [18] +{ + newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this); + newAct->setShortcuts(QKeySequence::New); + newAct->setStatusTip(tr("Create a new file")); + connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); + +//! [19] + openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this); + openAct->setShortcuts(QKeySequence::Open); + openAct->setStatusTip(tr("Open an existing file")); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); +//! [18] //! [19] + + saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this); + saveAct->setShortcuts(QKeySequence::Save); + saveAct->setStatusTip(tr("Save the document to disk")); + connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); + + saveAsAct = new QAction(tr("Save &As..."), this); + saveAsAct->setShortcuts(QKeySequence::SaveAs); + saveAsAct->setStatusTip(tr("Save the document under a new name")); + connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); + +//! [20] + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcuts(QKeySequence::Quit); +//! [20] + exitAct->setStatusTip(tr("Exit the application")); + connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); + +//! [21] + cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this); +//! [21] + cutAct->setShortcuts(QKeySequence::Cut); + cutAct->setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")); + connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut())); + + copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this); + copyAct->setShortcuts(QKeySequence::Copy); + copyAct->setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")); + connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy())); + + pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this); + pasteAct->setShortcuts(QKeySequence::Paste); + pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")); + connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste())); + + aboutAct = new QAction(tr("&About"), this); + aboutAct->setStatusTip(tr("Show the application's About box")); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + +//! [22] + aboutQtAct = new QAction(tr("About &Qt"), this); + aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); +//! [22] + +//! [23] + cutAct->setEnabled(false); +//! [23] //! [24] + copyAct->setEnabled(false); + connect(textEdit, SIGNAL(copyAvailable(bool)), + cutAct, SLOT(setEnabled(bool))); + connect(textEdit, SIGNAL(copyAvailable(bool)), + copyAct, SLOT(setEnabled(bool))); +} +//! [24] + +//! [25] //! [26] +void MainWindow::createMenus() +//! [25] //! [27] +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(newAct); +//! [28] + fileMenu->addAction(openAct); +//! [28] + fileMenu->addAction(saveAct); +//! [26] + fileMenu->addAction(saveAsAct); + fileMenu->addSeparator(); + fileMenu->addAction(exitAct); + + editMenu = menuBar()->addMenu(tr("&Edit")); + editMenu->addAction(cutAct); + editMenu->addAction(copyAct); + editMenu->addAction(pasteAct); + + menuBar()->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +} +//! [27] + +//! [29] //! [30] +void MainWindow::createToolBars() +{ + fileToolBar = addToolBar(tr("File")); + fileToolBar->addAction(newAct); +//! [29] //! [31] + fileToolBar->addAction(openAct); +//! [31] + fileToolBar->addAction(saveAct); + + editToolBar = addToolBar(tr("Edit")); + editToolBar->addAction(cutAct); + editToolBar->addAction(copyAct); + editToolBar->addAction(pasteAct); +} +//! [30] + +//! [32] +void MainWindow::createStatusBar() +//! [32] //! [33] +{ + statusBar()->showMessage(tr("Ready")); +} +//! [33] + +//! [34] //! [35] +void MainWindow::readSettings() +//! [34] //! [36] +{ + QSettings settings("Trolltech", "Application Example"); + QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint(); + QSize size = settings.value("size", QSize(400, 400)).toSize(); + resize(size); + move(pos); +} +//! [35] //! [36] + +//! [37] //! [38] +void MainWindow::writeSettings() +//! [37] //! [39] +{ + QSettings settings("Trolltech", "Application Example"); + settings.setValue("pos", pos()); + settings.setValue("size", size()); +} +//! [38] //! [39] + +//! [40] +bool MainWindow::maybeSave() +//! [40] //! [41] +{ + if (textEdit->document()->isModified()) { + QMessageBox::StandardButton ret; + ret = QMessageBox::warning(this, tr("Application"), + tr("The document has been modified.\n" + "Do you want to save your changes?"), + QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); + if (ret == QMessageBox::Save) + return save(); + else if (ret == QMessageBox::Cancel) + return false; + } + return true; +} +//! [41] + +//! [42] +void MainWindow::loadFile(const QString &fileName) +//! [42] //! [43] +{ + QFile file(fileName); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Application"), + tr("Cannot read file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return; + } + + QTextStream in(&file); +#ifndef QT_NO_CURSOR + QApplication::setOverrideCursor(Qt::WaitCursor); +#endif + textEdit->setPlainText(in.readAll()); +#ifndef QT_NO_CURSOR + QApplication::restoreOverrideCursor(); +#endif + + setCurrentFile(fileName); + statusBar()->showMessage(tr("File loaded"), 2000); +} +//! [43] + +//! [44] +bool MainWindow::saveFile(const QString &fileName) +//! [44] //! [45] +{ + QFile file(fileName); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Application"), + tr("Cannot write file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return false; + } + + QTextStream out(&file); +#ifndef QT_NO_CURSOR + QApplication::setOverrideCursor(Qt::WaitCursor); +#endif + out << textEdit->toPlainText(); +#ifndef QT_NO_CURSOR + QApplication::restoreOverrideCursor(); +#endif + + setCurrentFile(fileName); + statusBar()->showMessage(tr("File saved"), 2000); + return true; +} +//! [45] + +//! [46] +void MainWindow::setCurrentFile(const QString &fileName) +//! [46] //! [47] +{ + curFile = fileName; + textEdit->document()->setModified(false); + setWindowModified(false); + + QString shownName = curFile; + if (curFile.isEmpty()) + shownName = "untitled.txt"; + setWindowFilePath(shownName); +} +//! [47] + +//! [48] +QString MainWindow::strippedName(const QString &fullFileName) +//! [48] //! [49] +{ + return QFileInfo(fullFileName).fileName(); +} +//! [49] diff --git a/examples/mainwindows/application/mainwindow.h b/examples/mainwindows/application/mainwindow.h new file mode 100644 index 0000000000..9d40d30026 --- /dev/null +++ b/examples/mainwindows/application/mainwindow.h @@ -0,0 +1,105 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +class QAction; +class QMenu; +class QPlainTextEdit; +QT_END_NAMESPACE + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +protected: + void closeEvent(QCloseEvent *event); + +private slots: + void newFile(); + void open(); + bool save(); + bool saveAs(); + void about(); + void documentWasModified(); + +private: + void createActions(); + void createMenus(); + void createToolBars(); + void createStatusBar(); + void readSettings(); + void writeSettings(); + bool maybeSave(); + void loadFile(const QString &fileName); + bool saveFile(const QString &fileName); + void setCurrentFile(const QString &fileName); + QString strippedName(const QString &fullFileName); + + QPlainTextEdit *textEdit; + QString curFile; + + QMenu *fileMenu; + QMenu *editMenu; + QMenu *helpMenu; + QToolBar *fileToolBar; + QToolBar *editToolBar; + QAction *newAct; + QAction *openAct; + QAction *saveAct; + QAction *saveAsAct; + QAction *exitAct; + QAction *cutAct; + QAction *copyAct; + QAction *pasteAct; + QAction *aboutAct; + QAction *aboutQtAct; +}; +//! [0] + +#endif diff --git a/examples/mainwindows/dockwidgets/dockwidgets.pro b/examples/mainwindows/dockwidgets/dockwidgets.pro new file mode 100644 index 0000000000..3485ca5e02 --- /dev/null +++ b/examples/mainwindows/dockwidgets/dockwidgets.pro @@ -0,0 +1,12 @@ +HEADERS = mainwindow.h +SOURCES = main.cpp \ + mainwindow.cpp +RESOURCES = dockwidgets.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/dockwidgets +sources.files = $$SOURCES $$HEADERS $$RESOURCES dockwidgets.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/dockwidgets +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/mainwindows/dockwidgets/dockwidgets.qrc b/examples/mainwindows/dockwidgets/dockwidgets.qrc new file mode 100644 index 0000000000..968feac7ea --- /dev/null +++ b/examples/mainwindows/dockwidgets/dockwidgets.qrc @@ -0,0 +1,8 @@ + + + images/new.png + images/print.png + images/save.png + images/undo.png + + diff --git a/examples/mainwindows/dockwidgets/images/new.png b/examples/mainwindows/dockwidgets/images/new.png new file mode 100644 index 0000000000..dd795cfffc Binary files /dev/null and b/examples/mainwindows/dockwidgets/images/new.png differ diff --git a/examples/mainwindows/dockwidgets/images/print.png b/examples/mainwindows/dockwidgets/images/print.png new file mode 100644 index 0000000000..2afb769ee2 Binary files /dev/null and b/examples/mainwindows/dockwidgets/images/print.png differ diff --git a/examples/mainwindows/dockwidgets/images/save.png b/examples/mainwindows/dockwidgets/images/save.png new file mode 100644 index 0000000000..46eac82ad1 Binary files /dev/null and b/examples/mainwindows/dockwidgets/images/save.png differ diff --git a/examples/mainwindows/dockwidgets/images/undo.png b/examples/mainwindows/dockwidgets/images/undo.png new file mode 100644 index 0000000000..eee23d24a3 Binary files /dev/null and b/examples/mainwindows/dockwidgets/images/undo.png differ diff --git a/examples/mainwindows/dockwidgets/main.cpp b/examples/mainwindows/dockwidgets/main.cpp new file mode 100644 index 0000000000..5c846a4dad --- /dev/null +++ b/examples/mainwindows/dockwidgets/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Q_INIT_RESOURCE(dockwidgets); + MainWindow mainWin; + mainWin.show(); + return app.exec(); +} diff --git a/examples/mainwindows/dockwidgets/mainwindow.cpp b/examples/mainwindows/dockwidgets/mainwindow.cpp new file mode 100644 index 0000000000..296eec2242 --- /dev/null +++ b/examples/mainwindows/dockwidgets/mainwindow.cpp @@ -0,0 +1,342 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include + +#include "mainwindow.h" +//! [0] + +//! [1] +MainWindow::MainWindow() +{ + textEdit = new QTextEdit; + setCentralWidget(textEdit); + + createActions(); + createMenus(); + createToolBars(); + createStatusBar(); + createDockWindows(); + + setWindowTitle(tr("Dock Widgets")); + + newLetter(); + setUnifiedTitleAndToolBarOnMac(true); +} +//! [1] + +//! [2] +void MainWindow::newLetter() +{ + textEdit->clear(); + + QTextCursor cursor(textEdit->textCursor()); + cursor.movePosition(QTextCursor::Start); + QTextFrame *topFrame = cursor.currentFrame(); + QTextFrameFormat topFrameFormat = topFrame->frameFormat(); + topFrameFormat.setPadding(16); + topFrame->setFrameFormat(topFrameFormat); + + QTextCharFormat textFormat; + QTextCharFormat boldFormat; + boldFormat.setFontWeight(QFont::Bold); + QTextCharFormat italicFormat; + italicFormat.setFontItalic(true); + + QTextTableFormat tableFormat; + tableFormat.setBorder(1); + tableFormat.setCellPadding(16); + tableFormat.setAlignment(Qt::AlignRight); + cursor.insertTable(1, 1, tableFormat); + cursor.insertText("The Firm", boldFormat); + cursor.insertBlock(); + cursor.insertText("321 City Street", textFormat); + cursor.insertBlock(); + cursor.insertText("Industry Park"); + cursor.insertBlock(); + cursor.insertText("Some Country"); + cursor.setPosition(topFrame->lastPosition()); + cursor.insertText(QDate::currentDate().toString("d MMMM yyyy"), textFormat); + cursor.insertBlock(); + cursor.insertBlock(); + cursor.insertText("Dear ", textFormat); + cursor.insertText("NAME", italicFormat); + cursor.insertText(",", textFormat); + for (int i = 0; i < 3; ++i) + cursor.insertBlock(); + cursor.insertText(tr("Yours sincerely,"), textFormat); + for (int i = 0; i < 3; ++i) + cursor.insertBlock(); + cursor.insertText("The Boss", textFormat); + cursor.insertBlock(); + cursor.insertText("ADDRESS", italicFormat); +} +//! [2] + +//! [3] +void MainWindow::print() +{ +#ifndef QT_NO_PRINTDIALOG + QTextDocument *document = textEdit->document(); + QPrinter printer; + + QPrintDialog *dlg = new QPrintDialog(&printer, this); + if (dlg->exec() != QDialog::Accepted) + return; + + document->print(&printer); + + statusBar()->showMessage(tr("Ready"), 2000); +#endif +} +//! [3] + +//! [4] +void MainWindow::save() +{ + QString fileName = QFileDialog::getSaveFileName(this, + tr("Choose a file name"), ".", + tr("HTML (*.html *.htm)")); + if (fileName.isEmpty()) + return; + QFile file(fileName); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Dock Widgets"), + tr("Cannot write file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return; + } + + QTextStream out(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + out << textEdit->toHtml(); + QApplication::restoreOverrideCursor(); + + statusBar()->showMessage(tr("Saved '%1'").arg(fileName), 2000); +} +//! [4] + +//! [5] +void MainWindow::undo() +{ + QTextDocument *document = textEdit->document(); + document->undo(); +} +//! [5] + +//! [6] +void MainWindow::insertCustomer(const QString &customer) +{ + if (customer.isEmpty()) + return; + QStringList customerList = customer.split(", "); + QTextDocument *document = textEdit->document(); + QTextCursor cursor = document->find("NAME"); + if (!cursor.isNull()) { + cursor.beginEditBlock(); + cursor.insertText(customerList.at(0)); + QTextCursor oldcursor = cursor; + cursor = document->find("ADDRESS"); + if (!cursor.isNull()) { + for (int i = 1; i < customerList.size(); ++i) { + cursor.insertBlock(); + cursor.insertText(customerList.at(i)); + } + cursor.endEditBlock(); + } + else + oldcursor.endEditBlock(); + } +} +//! [6] + +//! [7] +void MainWindow::addParagraph(const QString ¶graph) +{ + if (paragraph.isEmpty()) + return; + QTextDocument *document = textEdit->document(); + QTextCursor cursor = document->find(tr("Yours sincerely,")); + if (cursor.isNull()) + return; + cursor.beginEditBlock(); + cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor, 2); + cursor.insertBlock(); + cursor.insertText(paragraph); + cursor.insertBlock(); + cursor.endEditBlock(); + +} +//! [7] + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Dock Widgets"), + tr("The Dock Widgets example demonstrates how to " + "use Qt's dock widgets. You can enter your own text, " + "click a customer to add a customer name and " + "address, and click standard paragraphs to add them.")); +} + +void MainWindow::createActions() +{ + newLetterAct = new QAction(QIcon(":/images/new.png"), tr("&New Letter"), + this); + newLetterAct->setShortcuts(QKeySequence::New); + newLetterAct->setStatusTip(tr("Create a new form letter")); + connect(newLetterAct, SIGNAL(triggered()), this, SLOT(newLetter())); + + saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save..."), this); + saveAct->setShortcuts(QKeySequence::Save); + saveAct->setStatusTip(tr("Save the current form letter")); + connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); + + printAct = new QAction(QIcon(":/images/print.png"), tr("&Print..."), this); + printAct->setShortcuts(QKeySequence::Print); + printAct->setStatusTip(tr("Print the current form letter")); + connect(printAct, SIGNAL(triggered()), this, SLOT(print())); + + undoAct = new QAction(QIcon(":/images/undo.png"), tr("&Undo"), this); + undoAct->setShortcuts(QKeySequence::Undo); + undoAct->setStatusTip(tr("Undo the last editing action")); + connect(undoAct, SIGNAL(triggered()), this, SLOT(undo())); + + quitAct = new QAction(tr("&Quit"), this); + quitAct->setShortcuts(QKeySequence::Quit); + quitAct->setStatusTip(tr("Quit the application")); + connect(quitAct, SIGNAL(triggered()), this, SLOT(close())); + + aboutAct = new QAction(tr("&About"), this); + aboutAct->setStatusTip(tr("Show the application's About box")); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); +} + +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(newLetterAct); + fileMenu->addAction(saveAct); + fileMenu->addAction(printAct); + fileMenu->addSeparator(); + fileMenu->addAction(quitAct); + + editMenu = menuBar()->addMenu(tr("&Edit")); + editMenu->addAction(undoAct); + + viewMenu = menuBar()->addMenu(tr("&View")); + + menuBar()->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +} + +void MainWindow::createToolBars() +{ + fileToolBar = addToolBar(tr("File")); + fileToolBar->addAction(newLetterAct); + fileToolBar->addAction(saveAct); + fileToolBar->addAction(printAct); + + editToolBar = addToolBar(tr("Edit")); + editToolBar->addAction(undoAct); +} + +//! [8] +void MainWindow::createStatusBar() +{ + statusBar()->showMessage(tr("Ready")); +} +//! [8] + +//! [9] +void MainWindow::createDockWindows() +{ + QDockWidget *dock = new QDockWidget(tr("Customers"), this); + dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); + customerList = new QListWidget(dock); + customerList->addItems(QStringList() + << "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton" + << "Jane Doe, Memorabilia, 23 Watersedge, Beaton" + << "Tammy Shea, Tiblanka, 38 Sea Views, Carlton" + << "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal" + << "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston" + << "Sally Hobart, Tiroli Tea, 67 Long River, Fedula"); + dock->setWidget(customerList); + addDockWidget(Qt::RightDockWidgetArea, dock); + viewMenu->addAction(dock->toggleViewAction()); + + dock = new QDockWidget(tr("Paragraphs"), this); + paragraphsList = new QListWidget(dock); + paragraphsList->addItems(QStringList() + << "Thank you for your payment which we have received today." + << "Your order has been dispatched and should be with you " + "within 28 days." + << "We have dispatched those items that were in stock. The " + "rest of your order will be dispatched once all the " + "remaining items have arrived at our warehouse. No " + "additional shipping charges will be made." + << "You made a small overpayment (less than $5) which we " + "will keep on account for you, or return at your request." + << "You made a small underpayment (less than $1), but we have " + "sent your order anyway. We'll add this underpayment to " + "your next bill." + << "Unfortunately you did not send enough money. Please remit " + "an additional $. Your order will be dispatched as soon as " + "the complete amount has been received." + << "You made an overpayment (more than $5). Do you wish to " + "buy more items, or should we return the excess to you?"); + dock->setWidget(paragraphsList); + addDockWidget(Qt::RightDockWidgetArea, dock); + viewMenu->addAction(dock->toggleViewAction()); + + connect(customerList, SIGNAL(currentTextChanged(QString)), + this, SLOT(insertCustomer(QString))); + connect(paragraphsList, SIGNAL(currentTextChanged(QString)), + this, SLOT(addParagraph(QString))); +} +//! [9] diff --git a/examples/mainwindows/dockwidgets/mainwindow.h b/examples/mainwindows/dockwidgets/mainwindow.h new file mode 100644 index 0000000000..6ac8ff3883 --- /dev/null +++ b/examples/mainwindows/dockwidgets/mainwindow.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +class QAction; +class QListWidget; +class QMenu; +class QTextEdit; +QT_END_NAMESPACE + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void newLetter(); + void save(); + void print(); + void undo(); + void about(); + void insertCustomer(const QString &customer); + void addParagraph(const QString ¶graph); + +private: + void createActions(); + void createMenus(); + void createToolBars(); + void createStatusBar(); + void createDockWindows(); + + QTextEdit *textEdit; + QListWidget *customerList; + QListWidget *paragraphsList; + + QMenu *fileMenu; + QMenu *editMenu; + QMenu *viewMenu; + QMenu *helpMenu; + QToolBar *fileToolBar; + QToolBar *editToolBar; + QAction *newLetterAct; + QAction *saveAct; + QAction *printAct; + QAction *undoAct; + QAction *aboutAct; + QAction *aboutQtAct; + QAction *quitAct; +}; +//! [0] + +#endif diff --git a/examples/mainwindows/mainwindows.pro b/examples/mainwindows/mainwindows.pro new file mode 100644 index 0000000000..d44f9f0b49 --- /dev/null +++ b/examples/mainwindows/mainwindows.pro @@ -0,0 +1,19 @@ +TEMPLATE = subdirs +SUBDIRS = application \ + dockwidgets \ + mdi \ + menus \ + recentfiles \ + sdi + +symbian: SUBDIRS = \ + menus + + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS mainwindows.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/mainwindows/mdi/images/copy.png b/examples/mainwindows/mdi/images/copy.png new file mode 100644 index 0000000000..2aeb28288f Binary files /dev/null and b/examples/mainwindows/mdi/images/copy.png differ diff --git a/examples/mainwindows/mdi/images/cut.png b/examples/mainwindows/mdi/images/cut.png new file mode 100644 index 0000000000..54638e9386 Binary files /dev/null and b/examples/mainwindows/mdi/images/cut.png differ diff --git a/examples/mainwindows/mdi/images/new.png b/examples/mainwindows/mdi/images/new.png new file mode 100644 index 0000000000..12131b0100 Binary files /dev/null and b/examples/mainwindows/mdi/images/new.png differ diff --git a/examples/mainwindows/mdi/images/open.png b/examples/mainwindows/mdi/images/open.png new file mode 100644 index 0000000000..45fa2883a7 Binary files /dev/null and b/examples/mainwindows/mdi/images/open.png differ diff --git a/examples/mainwindows/mdi/images/paste.png b/examples/mainwindows/mdi/images/paste.png new file mode 100644 index 0000000000..c14425cad1 Binary files /dev/null and b/examples/mainwindows/mdi/images/paste.png differ diff --git a/examples/mainwindows/mdi/images/save.png b/examples/mainwindows/mdi/images/save.png new file mode 100644 index 0000000000..daba865faf Binary files /dev/null and b/examples/mainwindows/mdi/images/save.png differ diff --git a/examples/mainwindows/mdi/main.cpp b/examples/mainwindows/mdi/main.cpp new file mode 100644 index 0000000000..1a10a19619 --- /dev/null +++ b/examples/mainwindows/mdi/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(mdi); + + QApplication app(argc, argv); + MainWindow mainWin; + mainWin.show(); + return app.exec(); +} diff --git a/examples/mainwindows/mdi/mainwindow.cpp b/examples/mainwindows/mdi/mainwindow.cpp new file mode 100644 index 0000000000..956eabb230 --- /dev/null +++ b/examples/mainwindows/mdi/mainwindow.cpp @@ -0,0 +1,397 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" +#include "mdichild.h" + +MainWindow::MainWindow() +{ + mdiArea = new QMdiArea; + mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); + mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); + setCentralWidget(mdiArea); + connect(mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), + this, SLOT(updateMenus())); + windowMapper = new QSignalMapper(this); + connect(windowMapper, SIGNAL(mapped(QWidget*)), + this, SLOT(setActiveSubWindow(QWidget*))); + + createActions(); + createMenus(); + createToolBars(); + createStatusBar(); + updateMenus(); + + readSettings(); + + setWindowTitle(tr("MDI")); + setUnifiedTitleAndToolBarOnMac(true); +} + +void MainWindow::closeEvent(QCloseEvent *event) +{ + mdiArea->closeAllSubWindows(); + if (mdiArea->currentSubWindow()) { + event->ignore(); + } else { + writeSettings(); + event->accept(); + } +} + +void MainWindow::newFile() +{ + MdiChild *child = createMdiChild(); + child->newFile(); + child->show(); +} + +void MainWindow::open() +{ + QString fileName = QFileDialog::getOpenFileName(this); + if (!fileName.isEmpty()) { + QMdiSubWindow *existing = findMdiChild(fileName); + if (existing) { + mdiArea->setActiveSubWindow(existing); + return; + } + + MdiChild *child = createMdiChild(); + if (child->loadFile(fileName)) { + statusBar()->showMessage(tr("File loaded"), 2000); + child->show(); + } else { + child->close(); + } + } +} + +void MainWindow::save() +{ + if (activeMdiChild() && activeMdiChild()->save()) + statusBar()->showMessage(tr("File saved"), 2000); +} + +void MainWindow::saveAs() +{ + if (activeMdiChild() && activeMdiChild()->saveAs()) + statusBar()->showMessage(tr("File saved"), 2000); +} + +void MainWindow::cut() +{ + if (activeMdiChild()) + activeMdiChild()->cut(); +} + +void MainWindow::copy() +{ + if (activeMdiChild()) + activeMdiChild()->copy(); +} + +void MainWindow::paste() +{ + if (activeMdiChild()) + activeMdiChild()->paste(); +} + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About MDI"), + tr("The MDI example demonstrates how to write multiple " + "document interface applications using Qt.")); +} + +void MainWindow::updateMenus() +{ + bool hasMdiChild = (activeMdiChild() != 0); + saveAct->setEnabled(hasMdiChild); + saveAsAct->setEnabled(hasMdiChild); + pasteAct->setEnabled(hasMdiChild); + closeAct->setEnabled(hasMdiChild); + closeAllAct->setEnabled(hasMdiChild); + tileAct->setEnabled(hasMdiChild); + cascadeAct->setEnabled(hasMdiChild); + nextAct->setEnabled(hasMdiChild); + previousAct->setEnabled(hasMdiChild); + separatorAct->setVisible(hasMdiChild); + + bool hasSelection = (activeMdiChild() && + activeMdiChild()->textCursor().hasSelection()); + cutAct->setEnabled(hasSelection); + copyAct->setEnabled(hasSelection); +} + +void MainWindow::updateWindowMenu() +{ + windowMenu->clear(); + windowMenu->addAction(closeAct); + windowMenu->addAction(closeAllAct); + windowMenu->addSeparator(); + windowMenu->addAction(tileAct); + windowMenu->addAction(cascadeAct); + windowMenu->addSeparator(); + windowMenu->addAction(nextAct); + windowMenu->addAction(previousAct); + windowMenu->addAction(separatorAct); + + QList windows = mdiArea->subWindowList(); + separatorAct->setVisible(!windows.isEmpty()); + + for (int i = 0; i < windows.size(); ++i) { + MdiChild *child = qobject_cast(windows.at(i)->widget()); + + QString text; + if (i < 9) { + text = tr("&%1 %2").arg(i + 1) + .arg(child->userFriendlyCurrentFile()); + } else { + text = tr("%1 %2").arg(i + 1) + .arg(child->userFriendlyCurrentFile()); + } + QAction *action = windowMenu->addAction(text); + action->setCheckable(true); + action ->setChecked(child == activeMdiChild()); + connect(action, SIGNAL(triggered()), windowMapper, SLOT(map())); + windowMapper->setMapping(action, windows.at(i)); + } +} + +MdiChild *MainWindow::createMdiChild() +{ + MdiChild *child = new MdiChild; + mdiArea->addSubWindow(child); + + connect(child, SIGNAL(copyAvailable(bool)), + cutAct, SLOT(setEnabled(bool))); + connect(child, SIGNAL(copyAvailable(bool)), + copyAct, SLOT(setEnabled(bool))); + + return child; +} + +void MainWindow::createActions() +{ + newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this); + newAct->setShortcuts(QKeySequence::New); + newAct->setStatusTip(tr("Create a new file")); + connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); + + openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this); + openAct->setShortcuts(QKeySequence::Open); + openAct->setStatusTip(tr("Open an existing file")); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + + saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this); + saveAct->setShortcuts(QKeySequence::Save); + saveAct->setStatusTip(tr("Save the document to disk")); + connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); + + saveAsAct = new QAction(tr("Save &As..."), this); + saveAsAct->setShortcuts(QKeySequence::SaveAs); + saveAsAct->setStatusTip(tr("Save the document under a new name")); + connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); + +//! [0] + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcuts(QKeySequence::Quit); + exitAct->setStatusTip(tr("Exit the application")); + connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows())); +//! [0] + + cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this); + cutAct->setShortcuts(QKeySequence::Cut); + cutAct->setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")); + connect(cutAct, SIGNAL(triggered()), this, SLOT(cut())); + + copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this); + copyAct->setShortcuts(QKeySequence::Copy); + copyAct->setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")); + connect(copyAct, SIGNAL(triggered()), this, SLOT(copy())); + + pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this); + pasteAct->setShortcuts(QKeySequence::Paste); + pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")); + connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste())); + + closeAct = new QAction(tr("Cl&ose"), this); + closeAct->setStatusTip(tr("Close the active window")); + connect(closeAct, SIGNAL(triggered()), + mdiArea, SLOT(closeActiveSubWindow())); + + closeAllAct = new QAction(tr("Close &All"), this); + closeAllAct->setStatusTip(tr("Close all the windows")); + connect(closeAllAct, SIGNAL(triggered()), + mdiArea, SLOT(closeAllSubWindows())); + + tileAct = new QAction(tr("&Tile"), this); + tileAct->setStatusTip(tr("Tile the windows")); + connect(tileAct, SIGNAL(triggered()), mdiArea, SLOT(tileSubWindows())); + + cascadeAct = new QAction(tr("&Cascade"), this); + cascadeAct->setStatusTip(tr("Cascade the windows")); + connect(cascadeAct, SIGNAL(triggered()), mdiArea, SLOT(cascadeSubWindows())); + + nextAct = new QAction(tr("Ne&xt"), this); + nextAct->setShortcuts(QKeySequence::NextChild); + nextAct->setStatusTip(tr("Move the focus to the next window")); + connect(nextAct, SIGNAL(triggered()), + mdiArea, SLOT(activateNextSubWindow())); + + previousAct = new QAction(tr("Pre&vious"), this); + previousAct->setShortcuts(QKeySequence::PreviousChild); + previousAct->setStatusTip(tr("Move the focus to the previous " + "window")); + connect(previousAct, SIGNAL(triggered()), + mdiArea, SLOT(activatePreviousSubWindow())); + + separatorAct = new QAction(this); + separatorAct->setSeparator(true); + + aboutAct = new QAction(tr("&About"), this); + aboutAct->setStatusTip(tr("Show the application's About box")); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); +} + +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(newAct); + fileMenu->addAction(openAct); + fileMenu->addAction(saveAct); + fileMenu->addAction(saveAsAct); + fileMenu->addSeparator(); + QAction *action = fileMenu->addAction(tr("Switch layout direction")); + connect(action, SIGNAL(triggered()), this, SLOT(switchLayoutDirection())); + fileMenu->addAction(exitAct); + + editMenu = menuBar()->addMenu(tr("&Edit")); + editMenu->addAction(cutAct); + editMenu->addAction(copyAct); + editMenu->addAction(pasteAct); + + windowMenu = menuBar()->addMenu(tr("&Window")); + updateWindowMenu(); + connect(windowMenu, SIGNAL(aboutToShow()), this, SLOT(updateWindowMenu())); + + menuBar()->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +} + +void MainWindow::createToolBars() +{ + fileToolBar = addToolBar(tr("File")); + fileToolBar->addAction(newAct); + fileToolBar->addAction(openAct); + fileToolBar->addAction(saveAct); + + editToolBar = addToolBar(tr("Edit")); + editToolBar->addAction(cutAct); + editToolBar->addAction(copyAct); + editToolBar->addAction(pasteAct); +} + +void MainWindow::createStatusBar() +{ + statusBar()->showMessage(tr("Ready")); +} + +void MainWindow::readSettings() +{ + QSettings settings("Trolltech", "MDI Example"); + QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint(); + QSize size = settings.value("size", QSize(400, 400)).toSize(); + move(pos); + resize(size); +} + +void MainWindow::writeSettings() +{ + QSettings settings("Trolltech", "MDI Example"); + settings.setValue("pos", pos()); + settings.setValue("size", size()); +} + +MdiChild *MainWindow::activeMdiChild() +{ + if (QMdiSubWindow *activeSubWindow = mdiArea->activeSubWindow()) + return qobject_cast(activeSubWindow->widget()); + return 0; +} + +QMdiSubWindow *MainWindow::findMdiChild(const QString &fileName) +{ + QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath(); + + foreach (QMdiSubWindow *window, mdiArea->subWindowList()) { + MdiChild *mdiChild = qobject_cast(window->widget()); + if (mdiChild->currentFile() == canonicalFilePath) + return window; + } + return 0; +} + +void MainWindow::switchLayoutDirection() +{ + if (layoutDirection() == Qt::LeftToRight) + qApp->setLayoutDirection(Qt::RightToLeft); + else + qApp->setLayoutDirection(Qt::LeftToRight); +} + +void MainWindow::setActiveSubWindow(QWidget *window) +{ + if (!window) + return; + mdiArea->setActiveSubWindow(qobject_cast(window)); +} diff --git a/examples/mainwindows/mdi/mainwindow.h b/examples/mainwindows/mdi/mainwindow.h new file mode 100644 index 0000000000..906dee9bca --- /dev/null +++ b/examples/mainwindows/mdi/mainwindow.h @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +class MdiChild; +QT_BEGIN_NAMESPACE +class QAction; +class QMenu; +class QMdiArea; +class QMdiSubWindow; +class QSignalMapper; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +protected: + void closeEvent(QCloseEvent *event); + +private slots: + void newFile(); + void open(); + void save(); + void saveAs(); + void cut(); + void copy(); + void paste(); + void about(); + void updateMenus(); + void updateWindowMenu(); + MdiChild *createMdiChild(); + void switchLayoutDirection(); + void setActiveSubWindow(QWidget *window); + +private: + void createActions(); + void createMenus(); + void createToolBars(); + void createStatusBar(); + void readSettings(); + void writeSettings(); + MdiChild *activeMdiChild(); + QMdiSubWindow *findMdiChild(const QString &fileName); + + QMdiArea *mdiArea; + QSignalMapper *windowMapper; + + QMenu *fileMenu; + QMenu *editMenu; + QMenu *windowMenu; + QMenu *helpMenu; + QToolBar *fileToolBar; + QToolBar *editToolBar; + QAction *newAct; + QAction *openAct; + QAction *saveAct; + QAction *saveAsAct; + QAction *exitAct; + QAction *cutAct; + QAction *copyAct; + QAction *pasteAct; + QAction *closeAct; + QAction *closeAllAct; + QAction *tileAct; + QAction *cascadeAct; + QAction *nextAct; + QAction *previousAct; + QAction *separatorAct; + QAction *aboutAct; + QAction *aboutQtAct; +}; + +#endif diff --git a/examples/mainwindows/mdi/mdi.pro b/examples/mainwindows/mdi/mdi.pro new file mode 100644 index 0000000000..3eedff1949 --- /dev/null +++ b/examples/mainwindows/mdi/mdi.pro @@ -0,0 +1,14 @@ +HEADERS = mainwindow.h \ + mdichild.h +SOURCES = main.cpp \ + mainwindow.cpp \ + mdichild.cpp +RESOURCES = mdi.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/mdi +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS mdi.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/mdi +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/mainwindows/mdi/mdi.qrc b/examples/mainwindows/mdi/mdi.qrc new file mode 100644 index 0000000000..0a776fab4d --- /dev/null +++ b/examples/mainwindows/mdi/mdi.qrc @@ -0,0 +1,10 @@ + + + images/copy.png + images/cut.png + images/new.png + images/open.png + images/paste.png + images/save.png + + diff --git a/examples/mainwindows/mdi/mdichild.cpp b/examples/mainwindows/mdi/mdichild.cpp new file mode 100644 index 0000000000..0382b27e23 --- /dev/null +++ b/examples/mainwindows/mdi/mdichild.cpp @@ -0,0 +1,175 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mdichild.h" + +MdiChild::MdiChild() +{ + setAttribute(Qt::WA_DeleteOnClose); + isUntitled = true; +} + +void MdiChild::newFile() +{ + static int sequenceNumber = 1; + + isUntitled = true; + curFile = tr("document%1.txt").arg(sequenceNumber++); + setWindowTitle(curFile + "[*]"); + + connect(document(), SIGNAL(contentsChanged()), + this, SLOT(documentWasModified())); +} + +bool MdiChild::loadFile(const QString &fileName) +{ + QFile file(fileName); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + QMessageBox::warning(this, tr("MDI"), + tr("Cannot read file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return false; + } + + QTextStream in(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + setPlainText(in.readAll()); + QApplication::restoreOverrideCursor(); + + setCurrentFile(fileName); + + connect(document(), SIGNAL(contentsChanged()), + this, SLOT(documentWasModified())); + + return true; +} + +bool MdiChild::save() +{ + if (isUntitled) { + return saveAs(); + } else { + return saveFile(curFile); + } +} + +bool MdiChild::saveAs() +{ + QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), + curFile); + if (fileName.isEmpty()) + return false; + + return saveFile(fileName); +} + +bool MdiChild::saveFile(const QString &fileName) +{ + QFile file(fileName); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("MDI"), + tr("Cannot write file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return false; + } + + QTextStream out(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + out << toPlainText(); + QApplication::restoreOverrideCursor(); + + setCurrentFile(fileName); + return true; +} + +QString MdiChild::userFriendlyCurrentFile() +{ + return strippedName(curFile); +} + +void MdiChild::closeEvent(QCloseEvent *event) +{ + if (maybeSave()) { + event->accept(); + } else { + event->ignore(); + } +} + +void MdiChild::documentWasModified() +{ + setWindowModified(document()->isModified()); +} + +bool MdiChild::maybeSave() +{ + if (document()->isModified()) { + QMessageBox::StandardButton ret; + ret = QMessageBox::warning(this, tr("MDI"), + tr("'%1' has been modified.\n" + "Do you want to save your changes?") + .arg(userFriendlyCurrentFile()), + QMessageBox::Save | QMessageBox::Discard + | QMessageBox::Cancel); + if (ret == QMessageBox::Save) + return save(); + else if (ret == QMessageBox::Cancel) + return false; + } + return true; +} + +void MdiChild::setCurrentFile(const QString &fileName) +{ + curFile = QFileInfo(fileName).canonicalFilePath(); + isUntitled = false; + document()->setModified(false); + setWindowModified(false); + setWindowTitle(userFriendlyCurrentFile() + "[*]"); +} + +QString MdiChild::strippedName(const QString &fullFileName) +{ + return QFileInfo(fullFileName).fileName(); +} diff --git a/examples/mainwindows/mdi/mdichild.h b/examples/mainwindows/mdi/mdichild.h new file mode 100644 index 0000000000..e136d7f9ae --- /dev/null +++ b/examples/mainwindows/mdi/mdichild.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MDICHILD_H +#define MDICHILD_H + +#include + +class MdiChild : public QTextEdit +{ + Q_OBJECT + +public: + MdiChild(); + + void newFile(); + bool loadFile(const QString &fileName); + bool save(); + bool saveAs(); + bool saveFile(const QString &fileName); + QString userFriendlyCurrentFile(); + QString currentFile() { return curFile; } + +protected: + void closeEvent(QCloseEvent *event); + +private slots: + void documentWasModified(); + +private: + bool maybeSave(); + void setCurrentFile(const QString &fileName); + QString strippedName(const QString &fullFileName); + + QString curFile; + bool isUntitled; +}; + +#endif diff --git a/examples/mainwindows/menus/main.cpp b/examples/mainwindows/menus/main.cpp new file mode 100644 index 0000000000..01c8adae53 --- /dev/null +++ b/examples/mainwindows/menus/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow window; + window.show(); + return app.exec(); +} diff --git a/examples/mainwindows/menus/mainwindow.cpp b/examples/mainwindows/menus/mainwindow.cpp new file mode 100644 index 0000000000..cae81f6b71 --- /dev/null +++ b/examples/mainwindows/menus/mainwindow.cpp @@ -0,0 +1,370 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" + +//! [0] +MainWindow::MainWindow() +{ + QWidget *widget = new QWidget; + setCentralWidget(widget); +//! [0] + +//! [1] + QWidget *topFiller = new QWidget; + topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + + infoLabel = new QLabel(tr("Choose a menu option, or right-click to " + "invoke a context menu")); + infoLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken); + infoLabel->setAlignment(Qt::AlignCenter); + + QWidget *bottomFiller = new QWidget; + bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + + QVBoxLayout *layout = new QVBoxLayout; + layout->setMargin(5); + layout->addWidget(topFiller); + layout->addWidget(infoLabel); + layout->addWidget(bottomFiller); + widget->setLayout(layout); +//! [1] + +//! [2] + createActions(); + createMenus(); + + QString message = tr("A context menu is available by right-clicking"); + statusBar()->showMessage(message); + + setWindowTitle(tr("Menus")); + setMinimumSize(160, 160); + resize(480, 320); +} +//! [2] + +//! [3] +void MainWindow::contextMenuEvent(QContextMenuEvent *event) +{ + QMenu menu(this); + menu.addAction(cutAct); + menu.addAction(copyAct); + menu.addAction(pasteAct); + menu.exec(event->globalPos()); +} +//! [3] + +void MainWindow::newFile() +{ + infoLabel->setText(tr("Invoked File|New")); +} + +void MainWindow::open() +{ + infoLabel->setText(tr("Invoked File|Open")); +} + +void MainWindow::save() +{ + infoLabel->setText(tr("Invoked File|Save")); +} + +void MainWindow::print() +{ + infoLabel->setText(tr("Invoked File|Print")); +} + +void MainWindow::undo() +{ + infoLabel->setText(tr("Invoked Edit|Undo")); +} + +void MainWindow::redo() +{ + infoLabel->setText(tr("Invoked Edit|Redo")); +} + +void MainWindow::cut() +{ + infoLabel->setText(tr("Invoked Edit|Cut")); +} + +void MainWindow::copy() +{ + infoLabel->setText(tr("Invoked Edit|Copy")); +} + +void MainWindow::paste() +{ + infoLabel->setText(tr("Invoked Edit|Paste")); +} + +void MainWindow::bold() +{ + infoLabel->setText(tr("Invoked Edit|Format|Bold")); +} + +void MainWindow::italic() +{ + infoLabel->setText(tr("Invoked Edit|Format|Italic")); +} + +void MainWindow::leftAlign() +{ + infoLabel->setText(tr("Invoked Edit|Format|Left Align")); +} + +void MainWindow::rightAlign() +{ + infoLabel->setText(tr("Invoked Edit|Format|Right Align")); +} + +void MainWindow::justify() +{ + infoLabel->setText(tr("Invoked Edit|Format|Justify")); +} + +void MainWindow::center() +{ + infoLabel->setText(tr("Invoked Edit|Format|Center")); +} + +void MainWindow::setLineSpacing() +{ + infoLabel->setText(tr("Invoked Edit|Format|Set Line Spacing")); +} + +void MainWindow::setParagraphSpacing() +{ + infoLabel->setText(tr("Invoked Edit|Format|Set Paragraph Spacing")); +} + +void MainWindow::about() +{ + infoLabel->setText(tr("Invoked Help|About")); + QMessageBox::about(this, tr("About Menu"), + tr("The Menu example shows how to create " + "menu-bar menus and context menus.")); +} + +void MainWindow::aboutQt() +{ + infoLabel->setText(tr("Invoked Help|About Qt")); +} + +//! [4] +void MainWindow::createActions() +{ +//! [5] + newAct = new QAction(tr("&New"), this); + newAct->setShortcuts(QKeySequence::New); + newAct->setStatusTip(tr("Create a new file")); + connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); +//! [4] + + openAct = new QAction(tr("&Open..."), this); + openAct->setShortcuts(QKeySequence::Open); + openAct->setStatusTip(tr("Open an existing file")); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); +//! [5] + + saveAct = new QAction(tr("&Save"), this); + saveAct->setShortcuts(QKeySequence::Save); + saveAct->setStatusTip(tr("Save the document to disk")); + connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); + + printAct = new QAction(tr("&Print..."), this); + printAct->setShortcuts(QKeySequence::Print); + printAct->setStatusTip(tr("Print the document")); + connect(printAct, SIGNAL(triggered()), this, SLOT(print())); + + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcuts(QKeySequence::Quit); + exitAct->setStatusTip(tr("Exit the application")); + connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); + + undoAct = new QAction(tr("&Undo"), this); + undoAct->setShortcuts(QKeySequence::Undo); + undoAct->setStatusTip(tr("Undo the last operation")); + connect(undoAct, SIGNAL(triggered()), this, SLOT(undo())); + + redoAct = new QAction(tr("&Redo"), this); + redoAct->setShortcuts(QKeySequence::Redo); + redoAct->setStatusTip(tr("Redo the last operation")); + connect(redoAct, SIGNAL(triggered()), this, SLOT(redo())); + + cutAct = new QAction(tr("Cu&t"), this); + cutAct->setShortcuts(QKeySequence::Cut); + cutAct->setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")); + connect(cutAct, SIGNAL(triggered()), this, SLOT(cut())); + + copyAct = new QAction(tr("&Copy"), this); + copyAct->setShortcuts(QKeySequence::Copy); + copyAct->setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")); + connect(copyAct, SIGNAL(triggered()), this, SLOT(copy())); + + pasteAct = new QAction(tr("&Paste"), this); + pasteAct->setShortcuts(QKeySequence::Paste); + pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")); + connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste())); + + boldAct = new QAction(tr("&Bold"), this); + boldAct->setCheckable(true); + boldAct->setShortcut(QKeySequence::Bold); + boldAct->setStatusTip(tr("Make the text bold")); + connect(boldAct, SIGNAL(triggered()), this, SLOT(bold())); + + QFont boldFont = boldAct->font(); + boldFont.setBold(true); + boldAct->setFont(boldFont); + + italicAct = new QAction(tr("&Italic"), this); + italicAct->setCheckable(true); + italicAct->setShortcut(QKeySequence::Italic); + italicAct->setStatusTip(tr("Make the text italic")); + connect(italicAct, SIGNAL(triggered()), this, SLOT(italic())); + + QFont italicFont = italicAct->font(); + italicFont.setItalic(true); + italicAct->setFont(italicFont); + + setLineSpacingAct = new QAction(tr("Set &Line Spacing..."), this); + setLineSpacingAct->setStatusTip(tr("Change the gap between the lines of a " + "paragraph")); + connect(setLineSpacingAct, SIGNAL(triggered()), this, SLOT(setLineSpacing())); + + setParagraphSpacingAct = new QAction(tr("Set &Paragraph Spacing..."), this); + setLineSpacingAct->setStatusTip(tr("Change the gap between paragraphs")); + connect(setParagraphSpacingAct, SIGNAL(triggered()), + this, SLOT(setParagraphSpacing())); + + aboutAct = new QAction(tr("&About"), this); + aboutAct->setStatusTip(tr("Show the application's About box")); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + connect(aboutQtAct, SIGNAL(triggered()), this, SLOT(aboutQt())); + + leftAlignAct = new QAction(tr("&Left Align"), this); + leftAlignAct->setCheckable(true); + leftAlignAct->setShortcut(tr("Ctrl+L")); + leftAlignAct->setStatusTip(tr("Left align the selected text")); + connect(leftAlignAct, SIGNAL(triggered()), this, SLOT(leftAlign())); + + rightAlignAct = new QAction(tr("&Right Align"), this); + rightAlignAct->setCheckable(true); + rightAlignAct->setShortcut(tr("Ctrl+R")); + rightAlignAct->setStatusTip(tr("Right align the selected text")); + connect(rightAlignAct, SIGNAL(triggered()), this, SLOT(rightAlign())); + + justifyAct = new QAction(tr("&Justify"), this); + justifyAct->setCheckable(true); + justifyAct->setShortcut(tr("Ctrl+J")); + justifyAct->setStatusTip(tr("Justify the selected text")); + connect(justifyAct, SIGNAL(triggered()), this, SLOT(justify())); + + centerAct = new QAction(tr("&Center"), this); + centerAct->setCheckable(true); + centerAct->setShortcut(tr("Ctrl+E")); + centerAct->setStatusTip(tr("Center the selected text")); + connect(centerAct, SIGNAL(triggered()), this, SLOT(center())); + +//! [6] //! [7] + alignmentGroup = new QActionGroup(this); + alignmentGroup->addAction(leftAlignAct); + alignmentGroup->addAction(rightAlignAct); + alignmentGroup->addAction(justifyAct); + alignmentGroup->addAction(centerAct); + leftAlignAct->setChecked(true); +//! [6] +} +//! [7] + +//! [8] +void MainWindow::createMenus() +{ +//! [9] //! [10] + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(newAct); +//! [9] + fileMenu->addAction(openAct); +//! [10] + fileMenu->addAction(saveAct); + fileMenu->addAction(printAct); +//! [11] + fileMenu->addSeparator(); +//! [11] + fileMenu->addAction(exitAct); + + editMenu = menuBar()->addMenu(tr("&Edit")); + editMenu->addAction(undoAct); + editMenu->addAction(redoAct); + editMenu->addSeparator(); + editMenu->addAction(cutAct); + editMenu->addAction(copyAct); + editMenu->addAction(pasteAct); + editMenu->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +//! [8] + +//! [12] + formatMenu = editMenu->addMenu(tr("&Format")); + formatMenu->addAction(boldAct); + formatMenu->addAction(italicAct); + formatMenu->addSeparator()->setText(tr("Alignment")); + formatMenu->addAction(leftAlignAct); + formatMenu->addAction(rightAlignAct); + formatMenu->addAction(justifyAct); + formatMenu->addAction(centerAct); + formatMenu->addSeparator(); + formatMenu->addAction(setLineSpacingAct); + formatMenu->addAction(setParagraphSpacingAct); +} +//! [12] diff --git a/examples/mainwindows/menus/mainwindow.h b/examples/mainwindows/menus/mainwindow.h new file mode 100644 index 0000000000..58b33fa257 --- /dev/null +++ b/examples/mainwindows/menus/mainwindow.h @@ -0,0 +1,124 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +class QAction; +class QActionGroup; +class QLabel; +class QMenu; +QT_END_NAMESPACE + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +protected: + void contextMenuEvent(QContextMenuEvent *event); +//! [0] + +//! [1] +private slots: + void newFile(); + void open(); + void save(); + void print(); + void undo(); + void redo(); + void cut(); + void copy(); + void paste(); + void bold(); + void italic(); + void leftAlign(); + void rightAlign(); + void justify(); + void center(); + void setLineSpacing(); + void setParagraphSpacing(); + void about(); + void aboutQt(); +//! [1] + +//! [2] +private: + void createActions(); + void createMenus(); +//! [2] + +//! [3] + QMenu *fileMenu; + QMenu *editMenu; + QMenu *formatMenu; + QMenu *helpMenu; + QActionGroup *alignmentGroup; + QAction *newAct; + QAction *openAct; + QAction *saveAct; + QAction *printAct; + QAction *exitAct; + QAction *undoAct; + QAction *redoAct; + QAction *cutAct; + QAction *copyAct; + QAction *pasteAct; + QAction *boldAct; + QAction *italicAct; + QAction *leftAlignAct; + QAction *rightAlignAct; + QAction *justifyAct; + QAction *centerAct; + QAction *setLineSpacingAct; + QAction *setParagraphSpacingAct; + QAction *aboutAct; + QAction *aboutQtAct; + QLabel *infoLabel; +}; +//! [3] + +#endif diff --git a/examples/mainwindows/menus/menus.pro b/examples/mainwindows/menus/menus.pro new file mode 100644 index 0000000000..dc762ff44d --- /dev/null +++ b/examples/mainwindows/menus/menus.pro @@ -0,0 +1,14 @@ +HEADERS = mainwindow.h +SOURCES = mainwindow.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/menus +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS menus.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/menus +INSTALLS += target sources + +symbian { + TARGET.UID3 = 0xA000CF66 + include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) +} diff --git a/examples/mainwindows/recentfiles/main.cpp b/examples/mainwindows/recentfiles/main.cpp new file mode 100644 index 0000000000..3bbf0137c1 --- /dev/null +++ b/examples/mainwindows/recentfiles/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + app.setOrganizationName("Trolltech"); + app.setApplicationName("Recent Files Example"); + MainWindow *mainWin = new MainWindow; + mainWin->show(); + return app.exec(); +} diff --git a/examples/mainwindows/recentfiles/mainwindow.cpp b/examples/mainwindows/recentfiles/mainwindow.cpp new file mode 100644 index 0000000000..623c4a375c --- /dev/null +++ b/examples/mainwindows/recentfiles/mainwindow.cpp @@ -0,0 +1,251 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" + +MainWindow::MainWindow() +{ + setAttribute(Qt::WA_DeleteOnClose); + + textEdit = new QTextEdit; + setCentralWidget(textEdit); + + createActions(); + createMenus(); + (void)statusBar(); + + setWindowFilePath(QString()); + resize(400, 300); +} + +void MainWindow::newFile() +{ + MainWindow *other = new MainWindow; + other->show(); +} + +void MainWindow::open() +{ + QString fileName = QFileDialog::getOpenFileName(this); + if (!fileName.isEmpty()) + loadFile(fileName); +} + +void MainWindow::save() +{ + if (curFile.isEmpty()) + saveAs(); + else + saveFile(curFile); +} + +void MainWindow::saveAs() +{ + QString fileName = QFileDialog::getSaveFileName(this); + if (fileName.isEmpty()) + return; + + saveFile(fileName); +} + +void MainWindow::openRecentFile() +{ + QAction *action = qobject_cast(sender()); + if (action) + loadFile(action->data().toString()); +} + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Recent Files"), + tr("The Recent Files example demonstrates how to provide a " + "recently used file menu in a Qt application.")); +} + +void MainWindow::createActions() +{ + newAct = new QAction(tr("&New"), this); + newAct->setShortcuts(QKeySequence::New); + newAct->setStatusTip(tr("Create a new file")); + connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); + + openAct = new QAction(tr("&Open..."), this); + openAct->setShortcuts(QKeySequence::Open); + openAct->setStatusTip(tr("Open an existing file")); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + + saveAct = new QAction(tr("&Save"), this); + saveAct->setShortcuts(QKeySequence::Save); + saveAct->setStatusTip(tr("Save the document to disk")); + connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); + + saveAsAct = new QAction(tr("Save &As..."), this); + saveAsAct->setShortcuts(QKeySequence::SaveAs); + saveAsAct->setStatusTip(tr("Save the document under a new name")); + connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); + + for (int i = 0; i < MaxRecentFiles; ++i) { + recentFileActs[i] = new QAction(this); + recentFileActs[i]->setVisible(false); + connect(recentFileActs[i], SIGNAL(triggered()), + this, SLOT(openRecentFile())); + } + + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcuts(QKeySequence::Quit); + exitAct->setStatusTip(tr("Exit the application")); + connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows())); + + aboutAct = new QAction(tr("&About"), this); + aboutAct->setStatusTip(tr("Show the application's About box")); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); +} + +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(newAct); + fileMenu->addAction(openAct); + fileMenu->addAction(saveAct); + fileMenu->addAction(saveAsAct); + separatorAct = fileMenu->addSeparator(); + for (int i = 0; i < MaxRecentFiles; ++i) + fileMenu->addAction(recentFileActs[i]); + fileMenu->addSeparator(); + fileMenu->addAction(exitAct); + updateRecentFileActions(); + + menuBar()->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +} + +void MainWindow::loadFile(const QString &fileName) +{ + QFile file(fileName); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Recent Files"), + tr("Cannot read file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return; + } + + QTextStream in(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + textEdit->setPlainText(in.readAll()); + QApplication::restoreOverrideCursor(); + + setCurrentFile(fileName); + statusBar()->showMessage(tr("File loaded"), 2000); +} + +void MainWindow::saveFile(const QString &fileName) +{ + QFile file(fileName); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Recent Files"), + tr("Cannot write file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return; + } + + QTextStream out(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + out << textEdit->toPlainText(); + QApplication::restoreOverrideCursor(); + + setCurrentFile(fileName); + statusBar()->showMessage(tr("File saved"), 2000); +} + +void MainWindow::setCurrentFile(const QString &fileName) +{ + curFile = fileName; + setWindowFilePath(curFile); + + QSettings settings; + QStringList files = settings.value("recentFileList").toStringList(); + files.removeAll(fileName); + files.prepend(fileName); + while (files.size() > MaxRecentFiles) + files.removeLast(); + + settings.setValue("recentFileList", files); + + foreach (QWidget *widget, QApplication::topLevelWidgets()) { + MainWindow *mainWin = qobject_cast(widget); + if (mainWin) + mainWin->updateRecentFileActions(); + } +} + +void MainWindow::updateRecentFileActions() +{ + QSettings settings; + QStringList files = settings.value("recentFileList").toStringList(); + + int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles); + + for (int i = 0; i < numRecentFiles; ++i) { + QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i])); + recentFileActs[i]->setText(text); + recentFileActs[i]->setData(files[i]); + recentFileActs[i]->setVisible(true); + } + for (int j = numRecentFiles; j < MaxRecentFiles; ++j) + recentFileActs[j]->setVisible(false); + + separatorAct->setVisible(numRecentFiles > 0); +} + +QString MainWindow::strippedName(const QString &fullFileName) +{ + return QFileInfo(fullFileName).fileName(); +} diff --git a/examples/mainwindows/recentfiles/mainwindow.h b/examples/mainwindows/recentfiles/mainwindow.h new file mode 100644 index 0000000000..8c41502bf1 --- /dev/null +++ b/examples/mainwindows/recentfiles/mainwindow.h @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include + +QT_BEGIN_NAMESPACE +class QAction; +class QMenu; +class QTextEdit; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void newFile(); + void open(); + void save(); + void saveAs(); + void openRecentFile(); + void about(); + +private: + void createActions(); + void createMenus(); + void loadFile(const QString &fileName); + void saveFile(const QString &fileName); + void setCurrentFile(const QString &fileName); + void updateRecentFileActions(); + QString strippedName(const QString &fullFileName); + + QString curFile; + + QTextEdit *textEdit; + QMenu *fileMenu; + QMenu *recentFilesMenu; + QMenu *helpMenu; + QAction *newAct; + QAction *openAct; + QAction *saveAct; + QAction *saveAsAct; + QAction *exitAct; + QAction *aboutAct; + QAction *aboutQtAct; + QAction *separatorAct; + + enum { MaxRecentFiles = 5 }; + QAction *recentFileActs[MaxRecentFiles]; +}; + +#endif diff --git a/examples/mainwindows/recentfiles/recentfiles.pro b/examples/mainwindows/recentfiles/recentfiles.pro new file mode 100644 index 0000000000..b4cf79b630 --- /dev/null +++ b/examples/mainwindows/recentfiles/recentfiles.pro @@ -0,0 +1,11 @@ +HEADERS = mainwindow.h +SOURCES = main.cpp \ + mainwindow.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/recentfiles +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS recentfiles.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/recentfiles +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/mainwindows/sdi/images/copy.png b/examples/mainwindows/sdi/images/copy.png new file mode 100644 index 0000000000..2aeb28288f Binary files /dev/null and b/examples/mainwindows/sdi/images/copy.png differ diff --git a/examples/mainwindows/sdi/images/cut.png b/examples/mainwindows/sdi/images/cut.png new file mode 100644 index 0000000000..54638e9386 Binary files /dev/null and b/examples/mainwindows/sdi/images/cut.png differ diff --git a/examples/mainwindows/sdi/images/new.png b/examples/mainwindows/sdi/images/new.png new file mode 100644 index 0000000000..12131b0100 Binary files /dev/null and b/examples/mainwindows/sdi/images/new.png differ diff --git a/examples/mainwindows/sdi/images/open.png b/examples/mainwindows/sdi/images/open.png new file mode 100644 index 0000000000..45fa2883a7 Binary files /dev/null and b/examples/mainwindows/sdi/images/open.png differ diff --git a/examples/mainwindows/sdi/images/paste.png b/examples/mainwindows/sdi/images/paste.png new file mode 100644 index 0000000000..c14425cad1 Binary files /dev/null and b/examples/mainwindows/sdi/images/paste.png differ diff --git a/examples/mainwindows/sdi/images/save.png b/examples/mainwindows/sdi/images/save.png new file mode 100644 index 0000000000..daba865faf Binary files /dev/null and b/examples/mainwindows/sdi/images/save.png differ diff --git a/examples/mainwindows/sdi/main.cpp b/examples/mainwindows/sdi/main.cpp new file mode 100644 index 0000000000..0fb8a0388d --- /dev/null +++ b/examples/mainwindows/sdi/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(sdi); + QApplication app(argc, argv); + app.setApplicationName("SDI Example"); + app.setOrganizationName("Trolltech"); + MainWindow *mainWin = new MainWindow; + mainWin->show(); + return app.exec(); +} diff --git a/examples/mainwindows/sdi/mainwindow.cpp b/examples/mainwindows/sdi/mainwindow.cpp new file mode 100644 index 0000000000..f881118899 --- /dev/null +++ b/examples/mainwindows/sdi/mainwindow.cpp @@ -0,0 +1,372 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" + +MainWindow::MainWindow() +{ + init(); + setCurrentFile(""); +} + +MainWindow::MainWindow(const QString &fileName) +{ + init(); + loadFile(fileName); +} + +void MainWindow::closeEvent(QCloseEvent *event) +{ + if (maybeSave()) { + writeSettings(); + event->accept(); + } else { + event->ignore(); + } +} + +void MainWindow::newFile() +{ + MainWindow *other = new MainWindow; + other->move(x() + 40, y() + 40); + other->show(); +} + +void MainWindow::open() +{ + QString fileName = QFileDialog::getOpenFileName(this); + if (!fileName.isEmpty()) { + MainWindow *existing = findMainWindow(fileName); + if (existing) { + existing->show(); + existing->raise(); + existing->activateWindow(); + return; + } + + if (isUntitled && textEdit->document()->isEmpty() + && !isWindowModified()) { + loadFile(fileName); + } else { + MainWindow *other = new MainWindow(fileName); + if (other->isUntitled) { + delete other; + return; + } + other->move(x() + 40, y() + 40); + other->show(); + } + } +} + +bool MainWindow::save() +{ + if (isUntitled) { + return saveAs(); + } else { + return saveFile(curFile); + } +} + +bool MainWindow::saveAs() +{ + QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), + curFile); + if (fileName.isEmpty()) + return false; + + return saveFile(fileName); +} + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About SDI"), + tr("The SDI example demonstrates how to write single " + "document interface applications using Qt.")); +} + +void MainWindow::documentWasModified() +{ + setWindowModified(true); +} + +void MainWindow::init() +{ + setAttribute(Qt::WA_DeleteOnClose); + + isUntitled = true; + + textEdit = new QTextEdit; + setCentralWidget(textEdit); + + createActions(); + createMenus(); + createToolBars(); + createStatusBar(); + + readSettings(); + + connect(textEdit->document(), SIGNAL(contentsChanged()), + this, SLOT(documentWasModified())); + + setUnifiedTitleAndToolBarOnMac(true); +} + +void MainWindow::createActions() +{ + newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this); + newAct->setShortcuts(QKeySequence::New); + newAct->setStatusTip(tr("Create a new file")); + connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); + + openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this); + openAct->setShortcuts(QKeySequence::Open); + openAct->setStatusTip(tr("Open an existing file")); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + + saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this); + saveAct->setShortcuts(QKeySequence::Save); + saveAct->setStatusTip(tr("Save the document to disk")); + connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); + + saveAsAct = new QAction(tr("Save &As..."), this); + saveAsAct->setShortcuts(QKeySequence::SaveAs); + saveAsAct->setStatusTip(tr("Save the document under a new name")); + connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); + + closeAct = new QAction(tr("&Close"), this); + closeAct->setShortcut(tr("Ctrl+W")); + closeAct->setStatusTip(tr("Close this window")); + connect(closeAct, SIGNAL(triggered()), this, SLOT(close())); + + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcuts(QKeySequence::Quit); + exitAct->setStatusTip(tr("Exit the application")); + connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows())); + + cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this); + cutAct->setShortcuts(QKeySequence::Cut); + cutAct->setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")); + connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut())); + + copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this); + copyAct->setShortcuts(QKeySequence::Copy); + copyAct->setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")); + connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy())); + + pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this); + pasteAct->setShortcuts(QKeySequence::Paste); + pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")); + connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste())); + + aboutAct = new QAction(tr("&About"), this); + aboutAct->setStatusTip(tr("Show the application's About box")); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + + + cutAct->setEnabled(false); + copyAct->setEnabled(false); + connect(textEdit, SIGNAL(copyAvailable(bool)), + cutAct, SLOT(setEnabled(bool))); + connect(textEdit, SIGNAL(copyAvailable(bool)), + copyAct, SLOT(setEnabled(bool))); +} + +//! [implicit tr context] +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); +//! [implicit tr context] + fileMenu->addAction(newAct); + fileMenu->addAction(openAct); + fileMenu->addAction(saveAct); + fileMenu->addAction(saveAsAct); + fileMenu->addSeparator(); + fileMenu->addAction(closeAct); + fileMenu->addAction(exitAct); + + editMenu = menuBar()->addMenu(tr("&Edit")); + editMenu->addAction(cutAct); + editMenu->addAction(copyAct); + editMenu->addAction(pasteAct); + + menuBar()->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +} + +void MainWindow::createToolBars() +{ +//! [0] + fileToolBar = addToolBar(tr("File")); + fileToolBar->addAction(newAct); + fileToolBar->addAction(openAct); +//! [0] + fileToolBar->addAction(saveAct); + + editToolBar = addToolBar(tr("Edit")); + editToolBar->addAction(cutAct); + editToolBar->addAction(copyAct); + editToolBar->addAction(pasteAct); +} + +void MainWindow::createStatusBar() +{ + statusBar()->showMessage(tr("Ready")); +} + +void MainWindow::readSettings() +{ + QSettings settings; + QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint(); + QSize size = settings.value("size", QSize(400, 400)).toSize(); + move(pos); + resize(size); +} + +void MainWindow::writeSettings() +{ + QSettings settings; + settings.setValue("pos", pos()); + settings.setValue("size", size()); +} + +bool MainWindow::maybeSave() +{ + if (textEdit->document()->isModified()) { + QMessageBox::StandardButton ret; + ret = QMessageBox::warning(this, tr("SDI"), + tr("The document has been modified.\n" + "Do you want to save your changes?"), + QMessageBox::Save | QMessageBox::Discard + | QMessageBox::Cancel); + if (ret == QMessageBox::Save) + return save(); + else if (ret == QMessageBox::Cancel) + return false; + } + return true; +} + +void MainWindow::loadFile(const QString &fileName) +{ + + QFile file(fileName); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + QMessageBox::warning(this, tr("SDI"), + tr("Cannot read file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return; + } + + QTextStream in(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + textEdit->setPlainText(in.readAll()); + QApplication::restoreOverrideCursor(); + + setCurrentFile(fileName); + statusBar()->showMessage(tr("File loaded"), 2000); +} + +bool MainWindow::saveFile(const QString &fileName) +{ + QFile file(fileName); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("SDI"), + tr("Cannot write file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return false; + } + + QTextStream out(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + out << textEdit->toPlainText(); + QApplication::restoreOverrideCursor(); + + setCurrentFile(fileName); + statusBar()->showMessage(tr("File saved"), 2000); + return true; +} + +void MainWindow::setCurrentFile(const QString &fileName) +{ + static int sequenceNumber = 1; + + isUntitled = fileName.isEmpty(); + if (isUntitled) { + curFile = tr("document%1.txt").arg(sequenceNumber++); + } else { + curFile = QFileInfo(fileName).canonicalFilePath(); + } + + textEdit->document()->setModified(false); + setWindowModified(false); + setWindowFilePath(curFile); +} + +QString MainWindow::strippedName(const QString &fullFileName) +{ + return QFileInfo(fullFileName).fileName(); +} + +MainWindow *MainWindow::findMainWindow(const QString &fileName) +{ + QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath(); + + foreach (QWidget *widget, qApp->topLevelWidgets()) { + MainWindow *mainWin = qobject_cast(widget); + if (mainWin && mainWin->curFile == canonicalFilePath) + return mainWin; + } + return 0; +} diff --git a/examples/mainwindows/sdi/mainwindow.h b/examples/mainwindows/sdi/mainwindow.h new file mode 100644 index 0000000000..c8ca63f38b --- /dev/null +++ b/examples/mainwindows/sdi/mainwindow.h @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +class QAction; +class QMenu; +class QTextEdit; +QT_END_NAMESPACE + +//! [class definition with macro] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); +//! [class definition with macro] + MainWindow(const QString &fileName); + +protected: + void closeEvent(QCloseEvent *event); + +private slots: + void newFile(); + void open(); + bool save(); + bool saveAs(); + void about(); + void documentWasModified(); + +private: + void init(); + void createActions(); + void createMenus(); + void createToolBars(); + void createStatusBar(); + void readSettings(); + void writeSettings(); + bool maybeSave(); + void loadFile(const QString &fileName); + bool saveFile(const QString &fileName); + void setCurrentFile(const QString &fileName); + QString strippedName(const QString &fullFileName); + MainWindow *findMainWindow(const QString &fileName); + + QTextEdit *textEdit; + QString curFile; + bool isUntitled; + + QMenu *fileMenu; + QMenu *editMenu; + QMenu *helpMenu; + QToolBar *fileToolBar; + QToolBar *editToolBar; + QAction *newAct; + QAction *openAct; + QAction *saveAct; + QAction *saveAsAct; + QAction *closeAct; + QAction *exitAct; + QAction *cutAct; + QAction *copyAct; + QAction *pasteAct; + QAction *aboutAct; + QAction *aboutQtAct; +}; + +#endif diff --git a/examples/mainwindows/sdi/sdi.pro b/examples/mainwindows/sdi/sdi.pro new file mode 100644 index 0000000000..1ca9746c9e --- /dev/null +++ b/examples/mainwindows/sdi/sdi.pro @@ -0,0 +1,12 @@ +HEADERS = mainwindow.h +SOURCES = main.cpp \ + mainwindow.cpp +RESOURCES = sdi.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/sdi +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS sdi.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/sdi +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/mainwindows/sdi/sdi.qrc b/examples/mainwindows/sdi/sdi.qrc new file mode 100644 index 0000000000..0a776fab4d --- /dev/null +++ b/examples/mainwindows/sdi/sdi.qrc @@ -0,0 +1,10 @@ + + + images/copy.png + images/cut.png + images/new.png + images/open.png + images/paste.png + images/save.png + + -- cgit v1.2.3