From 806dda08d685bc5f9ed71dfe8b61f21848d48066 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Fri, 17 Aug 2012 13:23:19 +0200 Subject: Moving .qdoc files under examples/widgets/doc Updated those .qdoc files to refer to the new relative examples emplacement. Images and snippets to be moved later. Also grouped all widgets related examples under widgets. Change-Id: Ib29696e2d8948524537f53e8dda88f9ee26a597f Reviewed-by: J-P Nurmi --- examples/widgets/mainwindows/README | 9 + .../mainwindows/application/application.desktop | 11 + .../mainwindows/application/application.pro | 16 + .../mainwindows/application/application.qrc | 10 + .../mainwindows/application/images/copy.png | Bin 0 -> 1338 bytes .../widgets/mainwindows/application/images/cut.png | Bin 0 -> 1323 bytes .../widgets/mainwindows/application/images/new.png | Bin 0 -> 852 bytes .../mainwindows/application/images/open.png | Bin 0 -> 2073 bytes .../mainwindows/application/images/paste.png | Bin 0 -> 1645 bytes .../mainwindows/application/images/save.png | Bin 0 -> 1187 bytes examples/widgets/mainwindows/application/main.cpp | 57 ++ .../widgets/mainwindows/application/mainwindow.cpp | 392 +++++++++++ .../widgets/mainwindows/application/mainwindow.h | 105 +++ .../mainwindows/dockwidgets/dockwidgets.desktop | 11 + .../mainwindows/dockwidgets/dockwidgets.pro | 15 + .../mainwindows/dockwidgets/dockwidgets.qrc | 8 + .../widgets/mainwindows/dockwidgets/images/new.png | Bin 0 -> 977 bytes .../mainwindows/dockwidgets/images/print.png | Bin 0 -> 1732 bytes .../mainwindows/dockwidgets/images/save.png | Bin 0 -> 1894 bytes .../mainwindows/dockwidgets/images/undo.png | Bin 0 -> 1768 bytes examples/widgets/mainwindows/dockwidgets/main.cpp | 52 ++ .../widgets/mainwindows/dockwidgets/mainwindow.cpp | 345 ++++++++++ .../widgets/mainwindows/dockwidgets/mainwindow.h | 97 +++ .../widgets/mainwindows/mainwindow/colorswatch.cpp | 746 +++++++++++++++++++++ .../widgets/mainwindows/mainwindow/colorswatch.h | 136 ++++ examples/widgets/mainwindows/mainwindow/main.cpp | 164 +++++ .../widgets/mainwindows/mainwindow/mainwindow.cpp | 509 ++++++++++++++ .../widgets/mainwindows/mainwindow/mainwindow.h | 90 +++ .../widgets/mainwindows/mainwindow/mainwindow.pro | 17 + .../widgets/mainwindows/mainwindow/mainwindow.qrc | 8 + examples/widgets/mainwindows/mainwindow/qt.png | Bin 0 -> 2037 bytes .../mainwindows/mainwindow/titlebarCenter.png | Bin 0 -> 146 bytes .../mainwindows/mainwindow/titlebarLeft.png | Bin 0 -> 5148 bytes .../mainwindows/mainwindow/titlebarRight.png | Bin 0 -> 2704 bytes .../widgets/mainwindows/mainwindow/toolbar.cpp | 383 +++++++++++ examples/widgets/mainwindows/mainwindow/toolbar.h | 118 ++++ examples/widgets/mainwindows/mainwindows.pro | 16 + examples/widgets/mainwindows/mdi/images/copy.png | Bin 0 -> 1338 bytes examples/widgets/mainwindows/mdi/images/cut.png | Bin 0 -> 1323 bytes examples/widgets/mainwindows/mdi/images/new.png | Bin 0 -> 852 bytes examples/widgets/mainwindows/mdi/images/open.png | Bin 0 -> 2073 bytes examples/widgets/mainwindows/mdi/images/paste.png | Bin 0 -> 1645 bytes examples/widgets/mainwindows/mdi/images/save.png | Bin 0 -> 1187 bytes examples/widgets/mainwindows/mdi/main.cpp | 53 ++ examples/widgets/mainwindows/mdi/mainwindow.cpp | 411 ++++++++++++ examples/widgets/mainwindows/mdi/mainwindow.h | 122 ++++ examples/widgets/mainwindows/mdi/mdi.desktop | 11 + examples/widgets/mainwindows/mdi/mdi.pro | 16 + examples/widgets/mainwindows/mdi/mdi.qrc | 10 + examples/widgets/mainwindows/mdi/mdichild.cpp | 175 +++++ examples/widgets/mainwindows/mdi/mdichild.h | 76 +++ examples/widgets/mainwindows/menus/main.cpp | 51 ++ examples/widgets/mainwindows/menus/mainwindow.cpp | 370 ++++++++++ examples/widgets/mainwindows/menus/mainwindow.h | 124 ++++ examples/widgets/mainwindows/menus/menus.desktop | 11 + examples/widgets/mainwindows/menus/menus.pro | 11 + examples/widgets/mainwindows/recentfiles/main.cpp | 53 ++ .../widgets/mainwindows/recentfiles/mainwindow.cpp | 251 +++++++ .../widgets/mainwindows/recentfiles/mainwindow.h | 96 +++ .../mainwindows/recentfiles/recentfiles.desktop | 11 + .../mainwindows/recentfiles/recentfiles.pro | 13 + examples/widgets/mainwindows/sdi/images/copy.png | Bin 0 -> 1338 bytes examples/widgets/mainwindows/sdi/images/cut.png | Bin 0 -> 1323 bytes examples/widgets/mainwindows/sdi/images/new.png | Bin 0 -> 852 bytes examples/widgets/mainwindows/sdi/images/open.png | Bin 0 -> 2073 bytes examples/widgets/mainwindows/sdi/images/paste.png | Bin 0 -> 1645 bytes examples/widgets/mainwindows/sdi/images/save.png | Bin 0 -> 1187 bytes examples/widgets/mainwindows/sdi/main.cpp | 54 ++ examples/widgets/mainwindows/sdi/mainwindow.cpp | 372 ++++++++++ examples/widgets/mainwindows/sdi/mainwindow.h | 110 +++ examples/widgets/mainwindows/sdi/sdi.desktop | 11 + examples/widgets/mainwindows/sdi/sdi.pro | 14 + examples/widgets/mainwindows/sdi/sdi.qrc | 10 + 73 files changed, 5751 insertions(+) create mode 100644 examples/widgets/mainwindows/README create mode 100644 examples/widgets/mainwindows/application/application.desktop create mode 100644 examples/widgets/mainwindows/application/application.pro create mode 100644 examples/widgets/mainwindows/application/application.qrc create mode 100644 examples/widgets/mainwindows/application/images/copy.png create mode 100644 examples/widgets/mainwindows/application/images/cut.png create mode 100644 examples/widgets/mainwindows/application/images/new.png create mode 100644 examples/widgets/mainwindows/application/images/open.png create mode 100644 examples/widgets/mainwindows/application/images/paste.png create mode 100644 examples/widgets/mainwindows/application/images/save.png create mode 100644 examples/widgets/mainwindows/application/main.cpp create mode 100644 examples/widgets/mainwindows/application/mainwindow.cpp create mode 100644 examples/widgets/mainwindows/application/mainwindow.h create mode 100644 examples/widgets/mainwindows/dockwidgets/dockwidgets.desktop create mode 100644 examples/widgets/mainwindows/dockwidgets/dockwidgets.pro create mode 100644 examples/widgets/mainwindows/dockwidgets/dockwidgets.qrc create mode 100644 examples/widgets/mainwindows/dockwidgets/images/new.png create mode 100644 examples/widgets/mainwindows/dockwidgets/images/print.png create mode 100644 examples/widgets/mainwindows/dockwidgets/images/save.png create mode 100644 examples/widgets/mainwindows/dockwidgets/images/undo.png create mode 100644 examples/widgets/mainwindows/dockwidgets/main.cpp create mode 100644 examples/widgets/mainwindows/dockwidgets/mainwindow.cpp create mode 100644 examples/widgets/mainwindows/dockwidgets/mainwindow.h create mode 100644 examples/widgets/mainwindows/mainwindow/colorswatch.cpp create mode 100644 examples/widgets/mainwindows/mainwindow/colorswatch.h create mode 100644 examples/widgets/mainwindows/mainwindow/main.cpp create mode 100644 examples/widgets/mainwindows/mainwindow/mainwindow.cpp create mode 100644 examples/widgets/mainwindows/mainwindow/mainwindow.h create mode 100644 examples/widgets/mainwindows/mainwindow/mainwindow.pro create mode 100644 examples/widgets/mainwindows/mainwindow/mainwindow.qrc create mode 100644 examples/widgets/mainwindows/mainwindow/qt.png create mode 100644 examples/widgets/mainwindows/mainwindow/titlebarCenter.png create mode 100644 examples/widgets/mainwindows/mainwindow/titlebarLeft.png create mode 100644 examples/widgets/mainwindows/mainwindow/titlebarRight.png create mode 100644 examples/widgets/mainwindows/mainwindow/toolbar.cpp create mode 100644 examples/widgets/mainwindows/mainwindow/toolbar.h create mode 100644 examples/widgets/mainwindows/mainwindows.pro create mode 100644 examples/widgets/mainwindows/mdi/images/copy.png create mode 100644 examples/widgets/mainwindows/mdi/images/cut.png create mode 100644 examples/widgets/mainwindows/mdi/images/new.png create mode 100644 examples/widgets/mainwindows/mdi/images/open.png create mode 100644 examples/widgets/mainwindows/mdi/images/paste.png create mode 100644 examples/widgets/mainwindows/mdi/images/save.png create mode 100644 examples/widgets/mainwindows/mdi/main.cpp create mode 100644 examples/widgets/mainwindows/mdi/mainwindow.cpp create mode 100644 examples/widgets/mainwindows/mdi/mainwindow.h create mode 100644 examples/widgets/mainwindows/mdi/mdi.desktop create mode 100644 examples/widgets/mainwindows/mdi/mdi.pro create mode 100644 examples/widgets/mainwindows/mdi/mdi.qrc create mode 100644 examples/widgets/mainwindows/mdi/mdichild.cpp create mode 100644 examples/widgets/mainwindows/mdi/mdichild.h create mode 100644 examples/widgets/mainwindows/menus/main.cpp create mode 100644 examples/widgets/mainwindows/menus/mainwindow.cpp create mode 100644 examples/widgets/mainwindows/menus/mainwindow.h create mode 100644 examples/widgets/mainwindows/menus/menus.desktop create mode 100644 examples/widgets/mainwindows/menus/menus.pro create mode 100644 examples/widgets/mainwindows/recentfiles/main.cpp create mode 100644 examples/widgets/mainwindows/recentfiles/mainwindow.cpp create mode 100644 examples/widgets/mainwindows/recentfiles/mainwindow.h create mode 100644 examples/widgets/mainwindows/recentfiles/recentfiles.desktop create mode 100644 examples/widgets/mainwindows/recentfiles/recentfiles.pro create mode 100644 examples/widgets/mainwindows/sdi/images/copy.png create mode 100644 examples/widgets/mainwindows/sdi/images/cut.png create mode 100644 examples/widgets/mainwindows/sdi/images/new.png create mode 100644 examples/widgets/mainwindows/sdi/images/open.png create mode 100644 examples/widgets/mainwindows/sdi/images/paste.png create mode 100644 examples/widgets/mainwindows/sdi/images/save.png create mode 100644 examples/widgets/mainwindows/sdi/main.cpp create mode 100644 examples/widgets/mainwindows/sdi/mainwindow.cpp create mode 100644 examples/widgets/mainwindows/sdi/mainwindow.h create mode 100644 examples/widgets/mainwindows/sdi/sdi.desktop create mode 100644 examples/widgets/mainwindows/sdi/sdi.pro create mode 100644 examples/widgets/mainwindows/sdi/sdi.qrc (limited to 'examples/widgets/mainwindows') diff --git a/examples/widgets/mainwindows/README b/examples/widgets/mainwindows/README new file mode 100644 index 0000000000..b63adee6e3 --- /dev/null +++ b/examples/widgets/mainwindows/README @@ -0,0 +1,9 @@ +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. + + +Documentation for these examples can be found via the Examples +link in the main Qt documentation. diff --git a/examples/widgets/mainwindows/application/application.desktop b/examples/widgets/mainwindows/application/application.desktop new file mode 100644 index 0000000000..b6fab1f1fe --- /dev/null +++ b/examples/widgets/mainwindows/application/application.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Application +Exec=/opt/usr/bin/application +Icon=application +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/mainwindows/application/application.pro b/examples/widgets/mainwindows/application/application.pro new file mode 100644 index 0000000000..ef866a6d5f --- /dev/null +++ b/examples/widgets/mainwindows/application/application.pro @@ -0,0 +1,16 @@ +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 + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/mainwindows/application/application.qrc b/examples/widgets/mainwindows/application/application.qrc new file mode 100644 index 0000000000..0a776fab4d --- /dev/null +++ b/examples/widgets/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/widgets/mainwindows/application/images/copy.png b/examples/widgets/mainwindows/application/images/copy.png new file mode 100644 index 0000000000..2aeb28288f Binary files /dev/null and b/examples/widgets/mainwindows/application/images/copy.png differ diff --git a/examples/widgets/mainwindows/application/images/cut.png b/examples/widgets/mainwindows/application/images/cut.png new file mode 100644 index 0000000000..54638e9386 Binary files /dev/null and b/examples/widgets/mainwindows/application/images/cut.png differ diff --git a/examples/widgets/mainwindows/application/images/new.png b/examples/widgets/mainwindows/application/images/new.png new file mode 100644 index 0000000000..12131b0100 Binary files /dev/null and b/examples/widgets/mainwindows/application/images/new.png differ diff --git a/examples/widgets/mainwindows/application/images/open.png b/examples/widgets/mainwindows/application/images/open.png new file mode 100644 index 0000000000..45fa2883a7 Binary files /dev/null and b/examples/widgets/mainwindows/application/images/open.png differ diff --git a/examples/widgets/mainwindows/application/images/paste.png b/examples/widgets/mainwindows/application/images/paste.png new file mode 100644 index 0000000000..c14425cad1 Binary files /dev/null and b/examples/widgets/mainwindows/application/images/paste.png differ diff --git a/examples/widgets/mainwindows/application/images/save.png b/examples/widgets/mainwindows/application/images/save.png new file mode 100644 index 0000000000..daba865faf Binary files /dev/null and b/examples/widgets/mainwindows/application/images/save.png differ diff --git a/examples/widgets/mainwindows/application/main.cpp b/examples/widgets/mainwindows/application/main.cpp new file mode 100644 index 0000000000..9ff6799574 --- /dev/null +++ b/examples/widgets/mainwindows/application/main.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/application/mainwindow.cpp b/examples/widgets/mainwindows/application/mainwindow.cpp new file mode 100644 index 0000000000..8f60353da9 --- /dev/null +++ b/examples/widgets/mainwindows/application/mainwindow.cpp @@ -0,0 +1,392 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/application/mainwindow.h b/examples/widgets/mainwindows/application/mainwindow.h new file mode 100644 index 0000000000..7f429e0ea3 --- /dev/null +++ b/examples/widgets/mainwindows/application/mainwindow.h @@ -0,0 +1,105 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/dockwidgets/dockwidgets.desktop b/examples/widgets/mainwindows/dockwidgets/dockwidgets.desktop new file mode 100644 index 0000000000..1f57cb613b --- /dev/null +++ b/examples/widgets/mainwindows/dockwidgets/dockwidgets.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Dock Widgets +Exec=/opt/usr/bin/dockwidgets +Icon=dockwidgets +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/mainwindows/dockwidgets/dockwidgets.pro b/examples/widgets/mainwindows/dockwidgets/dockwidgets.pro new file mode 100644 index 0000000000..7e0da2496e --- /dev/null +++ b/examples/widgets/mainwindows/dockwidgets/dockwidgets.pro @@ -0,0 +1,15 @@ +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 + +QT += widgets +!isEmpty(QT.printsupport.name): QT += printsupport + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/mainwindows/dockwidgets/dockwidgets.qrc b/examples/widgets/mainwindows/dockwidgets/dockwidgets.qrc new file mode 100644 index 0000000000..968feac7ea --- /dev/null +++ b/examples/widgets/mainwindows/dockwidgets/dockwidgets.qrc @@ -0,0 +1,8 @@ + + + images/new.png + images/print.png + images/save.png + images/undo.png + + diff --git a/examples/widgets/mainwindows/dockwidgets/images/new.png b/examples/widgets/mainwindows/dockwidgets/images/new.png new file mode 100644 index 0000000000..dd795cfffc Binary files /dev/null and b/examples/widgets/mainwindows/dockwidgets/images/new.png differ diff --git a/examples/widgets/mainwindows/dockwidgets/images/print.png b/examples/widgets/mainwindows/dockwidgets/images/print.png new file mode 100644 index 0000000000..2afb769ee2 Binary files /dev/null and b/examples/widgets/mainwindows/dockwidgets/images/print.png differ diff --git a/examples/widgets/mainwindows/dockwidgets/images/save.png b/examples/widgets/mainwindows/dockwidgets/images/save.png new file mode 100644 index 0000000000..46eac82ad1 Binary files /dev/null and b/examples/widgets/mainwindows/dockwidgets/images/save.png differ diff --git a/examples/widgets/mainwindows/dockwidgets/images/undo.png b/examples/widgets/mainwindows/dockwidgets/images/undo.png new file mode 100644 index 0000000000..eee23d24a3 Binary files /dev/null and b/examples/widgets/mainwindows/dockwidgets/images/undo.png differ diff --git a/examples/widgets/mainwindows/dockwidgets/main.cpp b/examples/widgets/mainwindows/dockwidgets/main.cpp new file mode 100644 index 0000000000..19deafbb86 --- /dev/null +++ b/examples/widgets/mainwindows/dockwidgets/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/dockwidgets/mainwindow.cpp b/examples/widgets/mainwindows/dockwidgets/mainwindow.cpp new file mode 100644 index 0000000000..da01d56502 --- /dev/null +++ b/examples/widgets/mainwindows/dockwidgets/mainwindow.cpp @@ -0,0 +1,345 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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 +#ifndef QT_NO_PRINTDIALOG +#include +#endif + +#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/widgets/mainwindows/dockwidgets/mainwindow.h b/examples/widgets/mainwindows/dockwidgets/mainwindow.h new file mode 100644 index 0000000000..9f3eeb4e6b --- /dev/null +++ b/examples/widgets/mainwindows/dockwidgets/mainwindow.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/mainwindow/colorswatch.cpp b/examples/widgets/mainwindows/mainwindow/colorswatch.cpp new file mode 100644 index 0000000000..c84669d377 --- /dev/null +++ b/examples/widgets/mainwindows/mainwindow/colorswatch.cpp @@ -0,0 +1,746 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "colorswatch.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#undef DEBUG_SIZEHINTS + +QColor bgColorForName(const QString &name) +{ + if (name == "Black") + return QColor("#D8D8D8"); + else if (name == "White") + return QColor("#F1F1F1"); + else if (name == "Red") + return QColor("#F1D8D8"); + else if (name == "Green") + return QColor("#D8E4D8"); + else if (name == "Blue") + return QColor("#D8D8F1"); + else if (name == "Yellow") + return QColor("#F1F0D8"); + return QColor(name).light(110); +} + +QColor fgColorForName(const QString &name) +{ + if (name == "Black") + return QColor("#6C6C6C"); + else if (name == "White") + return QColor("#F8F8F8"); + else if (name == "Red") + return QColor("#F86C6C"); + else if (name == "Green") + return QColor("#6CB26C"); + else if (name == "Blue") + return QColor("#6C6CF8"); + else if (name == "Yellow") + return QColor("#F8F76C"); + return QColor(name); +} + +class ColorDock : public QFrame +{ + Q_OBJECT +public: + ColorDock(const QString &c, QWidget *parent); + + virtual QSize sizeHint() const; + virtual QSize minimumSizeHint() const; + + void setCustomSizeHint(const QSize &size); + +public slots: + void changeSizeHints(); + +protected: + void paintEvent(QPaintEvent *); + QString color; + QSize szHint, minSzHint; +}; + +ColorDock::ColorDock(const QString &c, QWidget *parent) + : QFrame(parent) , color(c) +{ + QFont font = this->font(); + font.setPointSize(8); + setFont(font); + szHint = QSize(-1, -1); + minSzHint = QSize(125, 75); +} + +QSize ColorDock::sizeHint() const +{ + return szHint; +} + +QSize ColorDock::minimumSizeHint() const +{ + return minSzHint; +} + +void ColorDock::paintEvent(QPaintEvent *) +{ + QPainter p(this); + p.setRenderHint(QPainter::Antialiasing); + p.fillRect(rect(), bgColorForName(color)); + + p.save(); + + extern void render_qt_text(QPainter *, int, int, const QColor &); + render_qt_text(&p, width(), height(), fgColorForName(color)); + + p.restore(); + +#ifdef DEBUG_SIZEHINTS + p.setRenderHint(QPainter::Antialiasing, false); + + QSize sz = size(); + QSize szHint = sizeHint(); + QSize minSzHint = minimumSizeHint(); + QSize maxSz = maximumSize(); + QString text = QString::fromLatin1("sz: %1x%2\nszHint: %3x%4\nminSzHint: %5x%6\n" + "maxSz: %8x%9") + .arg(sz.width()).arg(sz.height()) + .arg(szHint.width()).arg(szHint.height()) + .arg(minSzHint.width()).arg(minSzHint.height()) + .arg(maxSz.width()).arg(maxSz.height()); + + QRect r = fontMetrics().boundingRect(rect(), Qt::AlignLeft|Qt::AlignTop, text); + r.adjust(-2, -2, 1, 1); + p.translate(4, 4); + QColor bg = Qt::yellow; + bg.setAlpha(120); + p.setBrush(bg); + p.setPen(Qt::black); + p.drawRect(r); + p.drawText(rect(), Qt::AlignLeft|Qt::AlignTop, text); +#endif // DEBUG_SIZEHINTS +} + +static QSpinBox *createSpinBox(int value, QWidget *parent, int max = 1000) +{ + QSpinBox *result = new QSpinBox(parent); + result->setMinimum(-1); + result->setMaximum(max); + result->setValue(value); + return result; +} + +void ColorDock::changeSizeHints() +{ + QDialog dialog(this); + dialog.setWindowTitle(color); + + QVBoxLayout *topLayout = new QVBoxLayout(&dialog); + + QGridLayout *inputLayout = new QGridLayout(); + topLayout->addLayout(inputLayout); + + inputLayout->addWidget(new QLabel(tr("Size Hint:"), &dialog), 0, 0); + inputLayout->addWidget(new QLabel(tr("Min Size Hint:"), &dialog), 1, 0); + inputLayout->addWidget(new QLabel(tr("Max Size:"), &dialog), 2, 0); + inputLayout->addWidget(new QLabel(tr("Dockwgt Max Size:"), &dialog), 3, 0); + + QSpinBox *szHintW = createSpinBox(szHint.width(), &dialog); + inputLayout->addWidget(szHintW, 0, 1); + QSpinBox *szHintH = createSpinBox(szHint.height(), &dialog); + inputLayout->addWidget(szHintH, 0, 2); + + QSpinBox *minSzHintW = createSpinBox(minSzHint.width(), &dialog); + inputLayout->addWidget(minSzHintW, 1, 1); + QSpinBox *minSzHintH = createSpinBox(minSzHint.height(), &dialog); + inputLayout->addWidget(minSzHintH, 1, 2); + + QSize maxSz = maximumSize(); + QSpinBox *maxSzW = createSpinBox(maxSz.width(), &dialog, QWIDGETSIZE_MAX); + inputLayout->addWidget(maxSzW, 2, 1); + QSpinBox *maxSzH = createSpinBox(maxSz.height(), &dialog, QWIDGETSIZE_MAX); + inputLayout->addWidget(maxSzH, 2, 2); + + QSize dwMaxSz = parentWidget()->maximumSize(); + QSpinBox *dwMaxSzW = createSpinBox(dwMaxSz.width(), &dialog, QWIDGETSIZE_MAX); + inputLayout->addWidget(dwMaxSzW, 3, 1); + QSpinBox *dwMaxSzH = createSpinBox(dwMaxSz.height(), &dialog, QWIDGETSIZE_MAX); + inputLayout->addWidget(dwMaxSzH, 3, 2); + + inputLayout->setColumnStretch(1, 1); + inputLayout->setColumnStretch(2, 1); + + topLayout->addStretch(); + + QHBoxLayout *buttonBox = new QHBoxLayout(); + topLayout->addLayout(buttonBox); + + QPushButton *okButton = new QPushButton(tr("Ok"), &dialog); + QPushButton *cancelButton = new QPushButton(tr("Cancel"), &dialog); + connect(okButton, SIGNAL(clicked()), &dialog, SLOT(accept())); + connect(cancelButton, SIGNAL(clicked()), &dialog, SLOT(reject())); + buttonBox->addStretch(); + buttonBox->addWidget(cancelButton); + buttonBox->addWidget(okButton); + + + if (!dialog.exec()) + return; + + szHint = QSize(szHintW->value(), szHintH->value()); + minSzHint = QSize(minSzHintW->value(), minSzHintH->value()); + maxSz = QSize(maxSzW->value(), maxSzH->value()); + setMaximumSize(maxSz); + dwMaxSz = QSize(dwMaxSzW->value(), dwMaxSzH->value()); + parentWidget()->setMaximumSize(dwMaxSz); + updateGeometry(); + update(); +} + +void ColorDock::setCustomSizeHint(const QSize &size) +{ + szHint = size; + updateGeometry(); +} + +ColorSwatch::ColorSwatch(const QString &colorName, QWidget *parent, Qt::WindowFlags flags) + : QDockWidget(parent, flags) +{ + setObjectName(colorName + QLatin1String(" Dock Widget")); + setWindowTitle(objectName() + QLatin1String(" [*]")); + + QFrame *swatch = new ColorDock(colorName, this); + swatch->setFrameStyle(QFrame::Box | QFrame::Sunken); + + setWidget(swatch); + + changeSizeHintsAction = new QAction(tr("Change Size Hints"), this); + connect(changeSizeHintsAction, SIGNAL(triggered()), swatch, SLOT(changeSizeHints())); + + closableAction = new QAction(tr("Closable"), this); + closableAction->setCheckable(true); + connect(closableAction, SIGNAL(triggered(bool)), SLOT(changeClosable(bool))); + + movableAction = new QAction(tr("Movable"), this); + movableAction->setCheckable(true); + connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool))); + + floatableAction = new QAction(tr("Floatable"), this); + floatableAction->setCheckable(true); + connect(floatableAction, SIGNAL(triggered(bool)), SLOT(changeFloatable(bool))); + + verticalTitleBarAction = new QAction(tr("Vertical title bar"), this); + verticalTitleBarAction->setCheckable(true); + connect(verticalTitleBarAction, SIGNAL(triggered(bool)), + SLOT(changeVerticalTitleBar(bool))); + + floatingAction = new QAction(tr("Floating"), this); + floatingAction->setCheckable(true); + connect(floatingAction, SIGNAL(triggered(bool)), SLOT(changeFloating(bool))); + + allowedAreasActions = new QActionGroup(this); + allowedAreasActions->setExclusive(false); + + allowLeftAction = new QAction(tr("Allow on Left"), this); + allowLeftAction->setCheckable(true); + connect(allowLeftAction, SIGNAL(triggered(bool)), SLOT(allowLeft(bool))); + + allowRightAction = new QAction(tr("Allow on Right"), this); + allowRightAction->setCheckable(true); + connect(allowRightAction, SIGNAL(triggered(bool)), SLOT(allowRight(bool))); + + allowTopAction = new QAction(tr("Allow on Top"), this); + allowTopAction->setCheckable(true); + connect(allowTopAction, SIGNAL(triggered(bool)), SLOT(allowTop(bool))); + + allowBottomAction = new QAction(tr("Allow on Bottom"), this); + allowBottomAction->setCheckable(true); + connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool))); + + allowedAreasActions->addAction(allowLeftAction); + allowedAreasActions->addAction(allowRightAction); + allowedAreasActions->addAction(allowTopAction); + allowedAreasActions->addAction(allowBottomAction); + + areaActions = new QActionGroup(this); + areaActions->setExclusive(true); + + leftAction = new QAction(tr("Place on Left") , this); + leftAction->setCheckable(true); + connect(leftAction, SIGNAL(triggered(bool)), SLOT(placeLeft(bool))); + + rightAction = new QAction(tr("Place on Right") , this); + rightAction->setCheckable(true); + connect(rightAction, SIGNAL(triggered(bool)), SLOT(placeRight(bool))); + + topAction = new QAction(tr("Place on Top") , this); + topAction->setCheckable(true); + connect(topAction, SIGNAL(triggered(bool)), SLOT(placeTop(bool))); + + bottomAction = new QAction(tr("Place on Bottom") , this); + bottomAction->setCheckable(true); + connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool))); + + areaActions->addAction(leftAction); + areaActions->addAction(rightAction); + areaActions->addAction(topAction); + areaActions->addAction(bottomAction); + + connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool))); + + connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool))); + + connect(floatableAction, SIGNAL(triggered(bool)), floatingAction, SLOT(setEnabled(bool))); + + connect(floatingAction, SIGNAL(triggered(bool)), floatableAction, SLOT(setDisabled(bool))); + connect(movableAction, SIGNAL(triggered(bool)), floatableAction, SLOT(setEnabled(bool))); + + tabMenu = new QMenu(this); + tabMenu->setTitle(tr("Tab into")); + connect(tabMenu, SIGNAL(triggered(QAction*)), this, SLOT(tabInto(QAction*))); + + splitHMenu = new QMenu(this); + splitHMenu->setTitle(tr("Split horizontally into")); + connect(splitHMenu, SIGNAL(triggered(QAction*)), this, SLOT(splitInto(QAction*))); + + splitVMenu = new QMenu(this); + splitVMenu->setTitle(tr("Split vertically into")); + connect(splitVMenu, SIGNAL(triggered(QAction*)), this, SLOT(splitInto(QAction*))); + + windowModifiedAction = new QAction(tr("Modified"), this); + windowModifiedAction->setCheckable(true); + windowModifiedAction->setChecked(false); + connect(windowModifiedAction, SIGNAL(toggled(bool)), this, SLOT(setWindowModified(bool))); + + menu = new QMenu(colorName, this); + menu->addAction(toggleViewAction()); + QAction *action = menu->addAction(tr("Raise")); + connect(action, SIGNAL(triggered()), this, SLOT(raise())); + menu->addAction(changeSizeHintsAction); + menu->addSeparator(); + menu->addAction(closableAction); + menu->addAction(movableAction); + menu->addAction(floatableAction); + menu->addAction(floatingAction); + menu->addAction(verticalTitleBarAction); + menu->addSeparator(); + menu->addActions(allowedAreasActions->actions()); + menu->addSeparator(); + menu->addActions(areaActions->actions()); + menu->addSeparator(); + menu->addMenu(splitHMenu); + menu->addMenu(splitVMenu); + menu->addMenu(tabMenu); + menu->addSeparator(); + menu->addAction(windowModifiedAction); + + connect(menu, SIGNAL(aboutToShow()), this, SLOT(updateContextMenu())); + + if(colorName == "Black") { + leftAction->setShortcut(Qt::CTRL|Qt::Key_W); + rightAction->setShortcut(Qt::CTRL|Qt::Key_E); + toggleViewAction()->setShortcut(Qt::CTRL|Qt::Key_R); + } +} + +void ColorSwatch::updateContextMenu() +{ + QMainWindow *mainWindow = qobject_cast(parentWidget()); + const Qt::DockWidgetArea area = mainWindow->dockWidgetArea(this); + const Qt::DockWidgetAreas areas = allowedAreas(); + + closableAction->setChecked(features() & QDockWidget::DockWidgetClosable); + if (windowType() == Qt::Drawer) { + floatableAction->setEnabled(false); + floatingAction->setEnabled(false); + movableAction->setEnabled(false); + verticalTitleBarAction->setChecked(false); + } else { + floatableAction->setChecked(features() & QDockWidget::DockWidgetFloatable); + floatingAction->setChecked(isWindow()); + // done after floating, to get 'floatable' correctly initialized + movableAction->setChecked(features() & QDockWidget::DockWidgetMovable); + verticalTitleBarAction + ->setChecked(features() & QDockWidget::DockWidgetVerticalTitleBar); + } + + allowLeftAction->setChecked(isAreaAllowed(Qt::LeftDockWidgetArea)); + allowRightAction->setChecked(isAreaAllowed(Qt::RightDockWidgetArea)); + allowTopAction->setChecked(isAreaAllowed(Qt::TopDockWidgetArea)); + allowBottomAction->setChecked(isAreaAllowed(Qt::BottomDockWidgetArea)); + + if (allowedAreasActions->isEnabled()) { + allowLeftAction->setEnabled(area != Qt::LeftDockWidgetArea); + allowRightAction->setEnabled(area != Qt::RightDockWidgetArea); + allowTopAction->setEnabled(area != Qt::TopDockWidgetArea); + allowBottomAction->setEnabled(area != Qt::BottomDockWidgetArea); + } + + leftAction->blockSignals(true); + rightAction->blockSignals(true); + topAction->blockSignals(true); + bottomAction->blockSignals(true); + + leftAction->setChecked(area == Qt::LeftDockWidgetArea); + rightAction->setChecked(area == Qt::RightDockWidgetArea); + topAction->setChecked(area == Qt::TopDockWidgetArea); + bottomAction->setChecked(area == Qt::BottomDockWidgetArea); + + leftAction->blockSignals(false); + rightAction->blockSignals(false); + topAction->blockSignals(false); + bottomAction->blockSignals(false); + + if (areaActions->isEnabled()) { + leftAction->setEnabled(areas & Qt::LeftDockWidgetArea); + rightAction->setEnabled(areas & Qt::RightDockWidgetArea); + topAction->setEnabled(areas & Qt::TopDockWidgetArea); + bottomAction->setEnabled(areas & Qt::BottomDockWidgetArea); + } + + tabMenu->clear(); + splitHMenu->clear(); + splitVMenu->clear(); + QList dock_list = mainWindow->findChildren(); + foreach (ColorSwatch *dock, dock_list) { +// if (!dock->isVisible() || dock->isFloating()) +// continue; + tabMenu->addAction(dock->objectName()); + splitHMenu->addAction(dock->objectName()); + splitVMenu->addAction(dock->objectName()); + } +} + +void ColorSwatch::splitInto(QAction *action) +{ + QMainWindow *mainWindow = qobject_cast(parentWidget()); + QList dock_list = mainWindow->findChildren(); + ColorSwatch *target = 0; + foreach (ColorSwatch *dock, dock_list) { + if (action->text() == dock->objectName()) { + target = dock; + break; + } + } + if (target == 0) + return; + + Qt::Orientation o = action->parent() == splitHMenu + ? Qt::Horizontal : Qt::Vertical; + mainWindow->splitDockWidget(target, this, o); +} + +void ColorSwatch::tabInto(QAction *action) +{ + QMainWindow *mainWindow = qobject_cast(parentWidget()); + QList dock_list = mainWindow->findChildren(); + ColorSwatch *target = 0; + foreach (ColorSwatch *dock, dock_list) { + if (action->text() == dock->objectName()) { + target = dock; + break; + } + } + if (target == 0) + return; + + mainWindow->tabifyDockWidget(target, this); +} + +void ColorSwatch::contextMenuEvent(QContextMenuEvent *event) +{ + event->accept(); + menu->exec(event->globalPos()); +} + +void ColorSwatch::resizeEvent(QResizeEvent *e) +{ + if (BlueTitleBar *btb = qobject_cast(titleBarWidget())) + btb->updateMask(); + + QDockWidget::resizeEvent(e); +} + + +void ColorSwatch::allow(Qt::DockWidgetArea area, bool a) +{ + Qt::DockWidgetAreas areas = allowedAreas(); + areas = a ? areas | area : areas & ~area; + setAllowedAreas(areas); + + if (areaActions->isEnabled()) { + leftAction->setEnabled(areas & Qt::LeftDockWidgetArea); + rightAction->setEnabled(areas & Qt::RightDockWidgetArea); + topAction->setEnabled(areas & Qt::TopDockWidgetArea); + bottomAction->setEnabled(areas & Qt::BottomDockWidgetArea); + } +} + +void ColorSwatch::place(Qt::DockWidgetArea area, bool p) +{ + if (!p) return; + + QMainWindow *mainWindow = qobject_cast(parentWidget()); + mainWindow->addDockWidget(area, this); + + if (allowedAreasActions->isEnabled()) { + allowLeftAction->setEnabled(area != Qt::LeftDockWidgetArea); + allowRightAction->setEnabled(area != Qt::RightDockWidgetArea); + allowTopAction->setEnabled(area != Qt::TopDockWidgetArea); + allowBottomAction->setEnabled(area != Qt::BottomDockWidgetArea); + } +} + +void ColorSwatch::setCustomSizeHint(const QSize &size) +{ + if (ColorDock *dock = qobject_cast(widget())) + dock->setCustomSizeHint(size); +} + +void ColorSwatch::changeClosable(bool on) +{ setFeatures(on ? features() | DockWidgetClosable : features() & ~DockWidgetClosable); } + +void ColorSwatch::changeMovable(bool on) +{ setFeatures(on ? features() | DockWidgetMovable : features() & ~DockWidgetMovable); } + +void ColorSwatch::changeFloatable(bool on) +{ setFeatures(on ? features() | DockWidgetFloatable : features() & ~DockWidgetFloatable); } + +void ColorSwatch::changeFloating(bool floating) +{ setFloating(floating); } + +void ColorSwatch::allowLeft(bool a) +{ allow(Qt::LeftDockWidgetArea, a); } + +void ColorSwatch::allowRight(bool a) +{ allow(Qt::RightDockWidgetArea, a); } + +void ColorSwatch::allowTop(bool a) +{ allow(Qt::TopDockWidgetArea, a); } + +void ColorSwatch::allowBottom(bool a) +{ allow(Qt::BottomDockWidgetArea, a); } + +void ColorSwatch::placeLeft(bool p) +{ place(Qt::LeftDockWidgetArea, p); } + +void ColorSwatch::placeRight(bool p) +{ place(Qt::RightDockWidgetArea, p); } + +void ColorSwatch::placeTop(bool p) +{ place(Qt::TopDockWidgetArea, p); } + +void ColorSwatch::placeBottom(bool p) +{ place(Qt::BottomDockWidgetArea, p); } + +void ColorSwatch::changeVerticalTitleBar(bool on) +{ + setFeatures(on ? features() | DockWidgetVerticalTitleBar + : features() & ~DockWidgetVerticalTitleBar); +} + +QSize BlueTitleBar::minimumSizeHint() const +{ + QDockWidget *dw = qobject_cast(parentWidget()); + Q_ASSERT(dw != 0); + QSize result(leftPm.width() + rightPm.width(), centerPm.height()); + if (dw->features() & QDockWidget::DockWidgetVerticalTitleBar) + result.transpose(); + return result; +} + +BlueTitleBar::BlueTitleBar(QWidget *parent) + : QWidget(parent) +{ + leftPm = QPixmap(":/res/titlebarLeft.png"); + centerPm = QPixmap(":/res/titlebarCenter.png"); + rightPm = QPixmap(":/res/titlebarRight.png"); +} + +void BlueTitleBar::paintEvent(QPaintEvent*) +{ + QPainter painter(this); + QRect rect = this->rect(); + + QDockWidget *dw = qobject_cast(parentWidget()); + Q_ASSERT(dw != 0); + + if (dw->features() & QDockWidget::DockWidgetVerticalTitleBar) { + QSize s = rect.size(); + s.transpose(); + rect.setSize(s); + + painter.translate(rect.left(), rect.top() + rect.width()); + painter.rotate(-90); + painter.translate(-rect.left(), -rect.top()); + } + + painter.drawPixmap(rect.topLeft(), leftPm); + painter.drawPixmap(rect.topRight() - QPoint(rightPm.width() - 1, 0), rightPm); + QBrush brush(centerPm); + painter.fillRect(rect.left() + leftPm.width(), rect.top(), + rect.width() - leftPm.width() - rightPm.width(), + centerPm.height(), centerPm); +} + +void BlueTitleBar::mousePressEvent(QMouseEvent *event) +{ + QPoint pos = event->pos(); + + QRect rect = this->rect(); + + QDockWidget *dw = qobject_cast(parentWidget()); + Q_ASSERT(dw != 0); + + if (dw->features() & QDockWidget::DockWidgetVerticalTitleBar) { + QPoint p = pos; + pos.setX(rect.left() + rect.bottom() - p.y()); + pos.setY(rect.top() + p.x() - rect.left()); + + QSize s = rect.size(); + s.transpose(); + rect.setSize(s); + } + + const int buttonRight = 7; + const int buttonWidth = 20; + int right = rect.right() - pos.x(); + int button = (right - buttonRight)/buttonWidth; + switch (button) { + case 0: + event->accept(); + dw->close(); + break; + case 1: + event->accept(); + dw->setFloating(!dw->isFloating()); + break; + case 2: { + event->accept(); + QDockWidget::DockWidgetFeatures features = dw->features(); + if (features & QDockWidget::DockWidgetVerticalTitleBar) + features &= ~QDockWidget::DockWidgetVerticalTitleBar; + else + features |= QDockWidget::DockWidgetVerticalTitleBar; + dw->setFeatures(features); + break; + } + default: + event->ignore(); + break; + } +} + +void BlueTitleBar::updateMask() +{ + QDockWidget *dw = qobject_cast(parent()); + Q_ASSERT(dw != 0); + + QRect rect = dw->rect(); + QPixmap bitmap(dw->size()); + + { + QPainter painter(&bitmap); + + ///initialize to transparent + painter.fillRect(rect, Qt::color0); + + QRect contents = rect; + contents.setTopLeft(geometry().bottomLeft()); + contents.setRight(geometry().right()); + contents.setBottom(contents.bottom()-y()); + painter.fillRect(contents, Qt::color1); + + + + //let's pait the titlebar + + QRect titleRect = this->geometry(); + + if (dw->features() & QDockWidget::DockWidgetVerticalTitleBar) { + QSize s = rect.size(); + s.transpose(); + rect.setSize(s); + + QSize s2 = size(); + s2.transpose(); + titleRect.setSize(s2); + + painter.translate(rect.left(), rect.top() + rect.width()); + painter.rotate(-90); + painter.translate(-rect.left(), -rect.top()); + } + + contents.setTopLeft(titleRect.bottomLeft()); + contents.setRight(titleRect.right()); + contents.setBottom(rect.bottom()-y()); + + QRect rect = titleRect; + + + painter.drawPixmap(rect.topLeft(), leftPm.mask()); + painter.fillRect(rect.left() + leftPm.width(), rect.top(), + rect.width() - leftPm.width() - rightPm.width(), + centerPm.height(), Qt::color1); + painter.drawPixmap(rect.topRight() - QPoint(rightPm.width() - 1, 0), rightPm.mask()); + + painter.fillRect(contents, Qt::color1); + } + + dw->setMask(bitmap); +} + +#include "colorswatch.moc" diff --git a/examples/widgets/mainwindows/mainwindow/colorswatch.h b/examples/widgets/mainwindows/mainwindow/colorswatch.h new file mode 100644 index 0000000000..794a69cd6c --- /dev/null +++ b/examples/widgets/mainwindows/mainwindow/colorswatch.h @@ -0,0 +1,136 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef COLORSWATCH_H +#define COLORSWATCH_H + +#include + +QT_FORWARD_DECLARE_CLASS(QAction) +QT_FORWARD_DECLARE_CLASS(QActionGroup) +QT_FORWARD_DECLARE_CLASS(QMenu) + +class ColorSwatch : public QDockWidget +{ + Q_OBJECT + + QAction *closableAction; + QAction *movableAction; + QAction *floatableAction; + QAction *floatingAction; + QAction *verticalTitleBarAction; + + QActionGroup *allowedAreasActions; + QAction *allowLeftAction; + QAction *allowRightAction; + QAction *allowTopAction; + QAction *allowBottomAction; + + QActionGroup *areaActions; + QAction *leftAction; + QAction *rightAction; + QAction *topAction; + QAction *bottomAction; + + QAction *changeSizeHintsAction; + + QMenu *tabMenu; + QMenu *splitHMenu; + QMenu *splitVMenu; + + QAction *windowModifiedAction; + +public: + ColorSwatch(const QString &colorName, QWidget *parent = 0, Qt::WindowFlags flags = 0); + + QMenu *menu; + void setCustomSizeHint(const QSize &size); + +protected: + virtual void contextMenuEvent(QContextMenuEvent *event); + virtual void resizeEvent(QResizeEvent *e); + +private: + void allow(Qt::DockWidgetArea area, bool allow); + void place(Qt::DockWidgetArea area, bool place); + +private slots: + void changeClosable(bool on); + void changeMovable(bool on); + void changeFloatable(bool on); + void changeFloating(bool on); + void changeVerticalTitleBar(bool on); + void updateContextMenu(); + + void allowLeft(bool a); + void allowRight(bool a); + void allowTop(bool a); + void allowBottom(bool a); + + void placeLeft(bool p); + void placeRight(bool p); + void placeTop(bool p); + void placeBottom(bool p); + + void splitInto(QAction *action); + void tabInto(QAction *action); +}; + +class BlueTitleBar : public QWidget +{ + Q_OBJECT +public: + BlueTitleBar(QWidget *parent = 0); + + QSize sizeHint() const { return minimumSizeHint(); } + QSize minimumSizeHint() const; +protected: + void paintEvent(QPaintEvent *event); + void mousePressEvent(QMouseEvent *event); +public slots: + void updateMask(); + +private: + QPixmap leftPm, centerPm, rightPm; +}; + + +#endif diff --git a/examples/widgets/mainwindows/mainwindow/main.cpp b/examples/widgets/mainwindows/mainwindow/main.cpp new file mode 100644 index 0000000000..2b6342367a --- /dev/null +++ b/examples/widgets/mainwindows/mainwindow/main.cpp @@ -0,0 +1,164 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" + +#include +#include +#include +#include +#include + +void render_qt_text(QPainter *painter, int w, int h, const QColor &color) { + QPainterPath path; + path.moveTo(-0.083695, 0.283849); + path.cubicTo(-0.049581, 0.349613, -0.012720, 0.397969, 0.026886, 0.428917); + path.cubicTo(0.066493, 0.459865, 0.111593, 0.477595, 0.162186, 0.482108); + path.lineTo(0.162186, 0.500000); + path.cubicTo(0.115929, 0.498066, 0.066565, 0.487669, 0.014094, 0.468810); + path.cubicTo(-0.038378, 0.449952, -0.088103, 0.423839, -0.135082, 0.390474); + path.cubicTo(-0.182061, 0.357108, -0.222608, 0.321567, -0.256722, 0.283849); + path.cubicTo(-0.304712, 0.262250, -0.342874, 0.239362, -0.371206, 0.215184); + path.cubicTo(-0.411969, 0.179078, -0.443625, 0.134671, -0.466175, 0.081963); + path.cubicTo(-0.488725, 0.029255, -0.500000, -0.033043, -0.500000, -0.104932); + path.cubicTo(-0.500000, -0.218407, -0.467042, -0.312621, -0.401127, -0.387573); + path.cubicTo(-0.335212, -0.462524, -0.255421, -0.500000, -0.161752, -0.500000); + path.cubicTo(-0.072998, -0.500000, 0.003903, -0.462444, 0.068951, -0.387331); + path.cubicTo(0.133998, -0.312218, 0.166522, -0.217440, 0.166522, -0.102998); + path.cubicTo(0.166522, -0.010155, 0.143394, 0.071325, 0.097138, 0.141441); + path.cubicTo(0.050882, 0.211557, -0.009396, 0.259026, -0.083695, 0.283849); + path.moveTo(-0.167823, -0.456963); + path.cubicTo(-0.228823, -0.456963, -0.277826, -0.432624, -0.314831, -0.383946); + path.cubicTo(-0.361665, -0.323340, -0.385082, -0.230335, -0.385082, -0.104932); + path.cubicTo(-0.385082, 0.017569, -0.361376, 0.112025, -0.313964, 0.178433); + path.cubicTo(-0.277248, 0.229368, -0.228534, 0.254836, -0.167823, 0.254836); + path.cubicTo(-0.105088, 0.254836, -0.054496, 0.229368, -0.016045, 0.178433); + path.cubicTo(0.029055, 0.117827, 0.051605, 0.028691, 0.051605, -0.088975); + path.cubicTo(0.051605, -0.179562, 0.039318, -0.255803, 0.014744, -0.317698); + path.cubicTo(-0.004337, -0.365409, -0.029705, -0.400548, -0.061362, -0.423114); + path.cubicTo(-0.093018, -0.445680, -0.128505, -0.456963, -0.167823, -0.456963); + path.moveTo(0.379011, -0.404739); + path.lineTo(0.379011, -0.236460); + path.lineTo(0.486123, -0.236460); + path.lineTo(0.486123, -0.197292); + path.lineTo(0.379011, -0.197292); + path.lineTo(0.379011, 0.134913); + path.cubicTo(0.379011, 0.168117, 0.383276, 0.190442, 0.391804, 0.201886); + path.cubicTo(0.400332, 0.213330, 0.411246, 0.219052, 0.424545, 0.219052); + path.cubicTo(0.435531, 0.219052, 0.446227, 0.215264, 0.456635, 0.207689); + path.cubicTo(0.467042, 0.200113, 0.474993, 0.188910, 0.480486, 0.174081); + path.lineTo(0.500000, 0.174081); + path.cubicTo(0.488436, 0.210509, 0.471957, 0.237911, 0.450564, 0.256286); + path.cubicTo(0.429170, 0.274662, 0.407054, 0.283849, 0.384215, 0.283849); + path.cubicTo(0.368893, 0.283849, 0.353859, 0.279094, 0.339115, 0.269584); + path.cubicTo(0.324371, 0.260074, 0.313530, 0.246534, 0.306592, 0.228965); + path.cubicTo(0.299653, 0.211396, 0.296184, 0.184075, 0.296184, 0.147002); + path.lineTo(0.296184, -0.197292); + path.lineTo(0.223330, -0.197292); + path.lineTo(0.223330, -0.215667); + path.cubicTo(0.241833, -0.224049, 0.260697, -0.237992, 0.279922, -0.257495); + path.cubicTo(0.299147, -0.276999, 0.316276, -0.300129, 0.331310, -0.326886); + path.cubicTo(0.338826, -0.341070, 0.349523, -0.367021, 0.363400, -0.404739); + path.lineTo(0.379011, -0.404739); + path.moveTo(-0.535993, 0.275629); + + painter->translate(w / 2, h / 2); + double scale = qMin(w, h) * 8 / 10.0; + painter->scale(scale, scale); + + painter->setRenderHint(QPainter::Antialiasing); + + painter->save(); + painter->translate(.1, .1); + painter->fillPath(path, QColor(0, 0, 0, 63)); + painter->restore(); + + painter->setBrush(color); + painter->setPen(QPen(Qt::black, 0.02, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin)); + painter->drawPath(path); +} + +void usage() +{ + qWarning() << "Usage: mainwindow [-SizeHint x] ..."; + exit(1); +} + +QMap parseCustomSizeHints(int argc, char **argv) +{ + QMap result; + + for (int i = 1; i < argc; ++i) { + QString arg = QString::fromLocal8Bit(argv[i]); + + if (arg.startsWith(QLatin1String("-SizeHint"))) { + QString name = arg.mid(9); + if (name.isEmpty()) + usage(); + if (++i == argc) + usage(); + QString sizeStr = QString::fromLocal8Bit(argv[i]); + int idx = sizeStr.indexOf(QLatin1Char('x')); + if (idx == -1) + usage(); + bool ok; + int w = sizeStr.left(idx).toInt(&ok); + if (!ok) + usage(); + int h = sizeStr.mid(idx + 1).toInt(&ok); + if (!ok) + usage(); + result[name] = QSize(w, h); + } + } + + return result; +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + QMap customSizeHints = parseCustomSizeHints(argc, argv); + MainWindow mainWin(customSizeHints); + mainWin.resize(800, 600); + mainWin.show(); + return app.exec(); +} diff --git a/examples/widgets/mainwindows/mainwindow/mainwindow.cpp b/examples/widgets/mainwindows/mainwindow/mainwindow.cpp new file mode 100644 index 0000000000..2ac2b562f2 --- /dev/null +++ b/examples/widgets/mainwindows/mainwindow/mainwindow.cpp @@ -0,0 +1,509 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" +#include "colorswatch.h" +#include "toolbar.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const char * const message = + "

Qt Main Window Example

" + + "

This is a demonstration of the QMainWindow, QToolBar and " + "QDockWidget classes.

" + + "

The tool bar and dock widgets can be dragged around and rearranged " + "using the mouse or via the menu.

" + + "

Each dock widget contains a colored frame and a context " + "(right-click) menu.

" + +#ifdef Q_OS_MAC + "

On Mac OS X, the \"Black\" dock widget has been created as a " + "Drawer, which is a special kind of QDockWidget.

" +#endif + ; + +Q_DECLARE_METATYPE(QDockWidget::DockWidgetFeatures) + +MainWindow::MainWindow(const QMap &customSizeHints, + QWidget *parent, Qt::WindowFlags flags) + : QMainWindow(parent, flags) +{ + setObjectName("MainWindow"); + setWindowTitle("Qt Main Window Example"); + + center = new QTextEdit(this); + center->setReadOnly(true); + center->setMinimumSize(400, 205); + setCentralWidget(center); + + setupToolBar(); + setupMenuBar(); + setupDockWidgets(customSizeHints); + + statusBar()->showMessage(tr("Status Bar")); +} + +void MainWindow::actionTriggered(QAction *action) +{ + qDebug("action '%s' triggered", action->text().toLocal8Bit().data()); +} + +void MainWindow::setupToolBar() +{ + for (int i = 0; i < 3; ++i) { + ToolBar *tb = new ToolBar(QString::fromLatin1("Tool Bar %1").arg(i + 1), this); + toolBars.append(tb); + addToolBar(tb); + } +} + +void MainWindow::setupMenuBar() +{ + QMenu *menu = menuBar()->addMenu(tr("&File")); + + QAction *action = menu->addAction(tr("Save layout...")); + connect(action, SIGNAL(triggered()), this, SLOT(saveLayout())); + + action = menu->addAction(tr("Load layout...")); + connect(action, SIGNAL(triggered()), this, SLOT(loadLayout())); + + action = menu->addAction(tr("Switch layout direction")); + connect(action, SIGNAL(triggered()), this, SLOT(switchLayoutDirection())); + + menu->addSeparator(); + + menu->addAction(tr("&Quit"), this, SLOT(close())); + + mainWindowMenu = menuBar()->addMenu(tr("Main window")); + + action = mainWindowMenu->addAction(tr("Animated docks")); + action->setCheckable(true); + action->setChecked(dockOptions() & AnimatedDocks); + connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); + + action = mainWindowMenu->addAction(tr("Allow nested docks")); + action->setCheckable(true); + action->setChecked(dockOptions() & AllowNestedDocks); + connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); + + action = mainWindowMenu->addAction(tr("Allow tabbed docks")); + action->setCheckable(true); + action->setChecked(dockOptions() & AllowTabbedDocks); + connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); + + action = mainWindowMenu->addAction(tr("Force tabbed docks")); + action->setCheckable(true); + action->setChecked(dockOptions() & ForceTabbedDocks); + connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); + + action = mainWindowMenu->addAction(tr("Vertical tabs")); + action->setCheckable(true); + action->setChecked(dockOptions() & VerticalTabs); + connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); + + QMenu *toolBarMenu = menuBar()->addMenu(tr("Tool bars")); + for (int i = 0; i < toolBars.count(); ++i) + toolBarMenu->addMenu(toolBars.at(i)->menu); + + dockWidgetMenu = menuBar()->addMenu(tr("&Dock Widgets")); +} + +void MainWindow::setDockOptions() +{ + DockOptions opts; + QList actions = mainWindowMenu->actions(); + + if (actions.at(0)->isChecked()) + opts |= AnimatedDocks; + if (actions.at(1)->isChecked()) + opts |= AllowNestedDocks; + if (actions.at(2)->isChecked()) + opts |= AllowTabbedDocks; + if (actions.at(3)->isChecked()) + opts |= ForceTabbedDocks; + if (actions.at(4)->isChecked()) + opts |= VerticalTabs; + + QMainWindow::setDockOptions(opts); +} + +void MainWindow::saveLayout() +{ + QString fileName + = QFileDialog::getSaveFileName(this, tr("Save layout")); + if (fileName.isEmpty()) + return; + QFile file(fileName); + if (!file.open(QFile::WriteOnly)) { + QString msg = tr("Failed to open %1\n%2") + .arg(fileName) + .arg(file.errorString()); + QMessageBox::warning(this, tr("Error"), msg); + return; + } + + QByteArray geo_data = saveGeometry(); + QByteArray layout_data = saveState(); + + bool ok = file.putChar((uchar)geo_data.size()); + if (ok) + ok = file.write(geo_data) == geo_data.size(); + if (ok) + ok = file.write(layout_data) == layout_data.size(); + + if (!ok) { + QString msg = tr("Error writing to %1\n%2") + .arg(fileName) + .arg(file.errorString()); + QMessageBox::warning(this, tr("Error"), msg); + return; + } +} + +void MainWindow::loadLayout() +{ + QString fileName + = QFileDialog::getOpenFileName(this, tr("Load layout")); + if (fileName.isEmpty()) + return; + QFile file(fileName); + if (!file.open(QFile::ReadOnly)) { + QString msg = tr("Failed to open %1\n%2") + .arg(fileName) + .arg(file.errorString()); + QMessageBox::warning(this, tr("Error"), msg); + return; + } + + uchar geo_size; + QByteArray geo_data; + QByteArray layout_data; + + bool ok = file.getChar((char*)&geo_size); + if (ok) { + geo_data = file.read(geo_size); + ok = geo_data.size() == geo_size; + } + if (ok) { + layout_data = file.readAll(); + ok = layout_data.size() > 0; + } + + if (ok) + ok = restoreGeometry(geo_data); + if (ok) + ok = restoreState(layout_data); + + if (!ok) { + QString msg = tr("Error reading %1") + .arg(fileName); + QMessageBox::warning(this, tr("Error"), msg); + return; + } +} + +QAction *addAction(QMenu *menu, const QString &text, QActionGroup *group, QSignalMapper *mapper, + int id) +{ + bool first = group->actions().isEmpty(); + QAction *result = menu->addAction(text); + result->setCheckable(true); + result->setChecked(first); + group->addAction(result); + QObject::connect(result, SIGNAL(triggered()), mapper, SLOT(map())); + mapper->setMapping(result, id); + return result; +} + +void MainWindow::setupDockWidgets(const QMap &customSizeHints) +{ + qRegisterMetaType(); + + mapper = new QSignalMapper(this); + connect(mapper, SIGNAL(mapped(int)), this, SLOT(setCorner(int))); + + QMenu *corner_menu = dockWidgetMenu->addMenu(tr("Top left corner")); + QActionGroup *group = new QActionGroup(this); + group->setExclusive(true); + ::addAction(corner_menu, tr("Top dock area"), group, mapper, 0); + ::addAction(corner_menu, tr("Left dock area"), group, mapper, 1); + + corner_menu = dockWidgetMenu->addMenu(tr("Top right corner")); + group = new QActionGroup(this); + group->setExclusive(true); + ::addAction(corner_menu, tr("Top dock area"), group, mapper, 2); + ::addAction(corner_menu, tr("Right dock area"), group, mapper, 3); + + corner_menu = dockWidgetMenu->addMenu(tr("Bottom left corner")); + group = new QActionGroup(this); + group->setExclusive(true); + ::addAction(corner_menu, tr("Bottom dock area"), group, mapper, 4); + ::addAction(corner_menu, tr("Left dock area"), group, mapper, 5); + + corner_menu = dockWidgetMenu->addMenu(tr("Bottom right corner")); + group = new QActionGroup(this); + group->setExclusive(true); + ::addAction(corner_menu, tr("Bottom dock area"), group, mapper, 6); + ::addAction(corner_menu, tr("Right dock area"), group, mapper, 7); + + dockWidgetMenu->addSeparator(); + + static const struct Set { + const char * name; + uint flags; + Qt::DockWidgetArea area; + } sets [] = { +#ifndef Q_OS_MAC + { "Black", 0, Qt::LeftDockWidgetArea }, +#else + { "Black", Qt::Drawer, Qt::LeftDockWidgetArea }, +#endif + { "White", 0, Qt::RightDockWidgetArea }, + { "Red", 0, Qt::TopDockWidgetArea }, + { "Green", 0, Qt::TopDockWidgetArea }, + { "Blue", 0, Qt::BottomDockWidgetArea }, + { "Yellow", 0, Qt::BottomDockWidgetArea } + }; + const int setCount = sizeof(sets) / sizeof(Set); + + for (int i = 0; i < setCount; ++i) { + ColorSwatch *swatch = new ColorSwatch(tr(sets[i].name), this, Qt::WindowFlags(sets[i].flags)); + if (i%2) + swatch->setWindowIcon(QIcon(QPixmap(":/res/qt.png"))); + if (qstrcmp(sets[i].name, "Blue") == 0) { + BlueTitleBar *titlebar = new BlueTitleBar(swatch); + swatch->setTitleBarWidget(titlebar); + connect(swatch, SIGNAL(topLevelChanged(bool)), titlebar, SLOT(updateMask())); + connect(swatch, SIGNAL(featuresChanged(QDockWidget::DockWidgetFeatures)), titlebar, SLOT(updateMask()), Qt::QueuedConnection); + + } + + QString name = QString::fromLatin1(sets[i].name); + if (customSizeHints.contains(name)) + swatch->setCustomSizeHint(customSizeHints.value(name)); + + addDockWidget(sets[i].area, swatch); + dockWidgetMenu->addMenu(swatch->menu); + } + + createDockWidgetAction = new QAction(tr("Add dock widget..."), this); + connect(createDockWidgetAction, SIGNAL(triggered()), this, SLOT(createDockWidget())); + destroyDockWidgetMenu = new QMenu(tr("Destroy dock widget"), this); + destroyDockWidgetMenu->setEnabled(false); + connect(destroyDockWidgetMenu, SIGNAL(triggered(QAction*)), this, SLOT(destroyDockWidget(QAction*))); + + dockWidgetMenu->addSeparator(); + dockWidgetMenu->addAction(createDockWidgetAction); + dockWidgetMenu->addMenu(destroyDockWidgetMenu); +} + +void MainWindow::setCorner(int id) +{ + switch (id) { + case 0: + QMainWindow::setCorner(Qt::TopLeftCorner, Qt::TopDockWidgetArea); + break; + case 1: + QMainWindow::setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea); + break; + case 2: + QMainWindow::setCorner(Qt::TopRightCorner, Qt::TopDockWidgetArea); + break; + case 3: + QMainWindow::setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea); + break; + case 4: + QMainWindow::setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea); + break; + case 5: + QMainWindow::setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea); + break; + case 6: + QMainWindow::setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea); + break; + case 7: + QMainWindow::setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea); + break; + } +} + +void MainWindow::showEvent(QShowEvent *event) +{ + QMainWindow::showEvent(event); +} + +void MainWindow::switchLayoutDirection() +{ + if (layoutDirection() == Qt::LeftToRight) + qApp->setLayoutDirection(Qt::RightToLeft); + else + qApp->setLayoutDirection(Qt::LeftToRight); +} + +class CreateDockWidgetDialog : public QDialog +{ +public: + CreateDockWidgetDialog(QWidget *parent = 0); + + QString objectName() const; + Qt::DockWidgetArea location() const; + +private: + QLineEdit *m_objectName; + QComboBox *m_location; +}; + +CreateDockWidgetDialog::CreateDockWidgetDialog(QWidget *parent) + : QDialog(parent) +{ + QGridLayout *layout = new QGridLayout(this); + + layout->addWidget(new QLabel(tr("Object name:")), 0, 0); + m_objectName = new QLineEdit; + layout->addWidget(m_objectName, 0, 1); + + layout->addWidget(new QLabel(tr("Location:")), 1, 0); + m_location = new QComboBox; + m_location->setEditable(false); + m_location->addItem(tr("Top")); + m_location->addItem(tr("Left")); + m_location->addItem(tr("Right")); + m_location->addItem(tr("Bottom")); + m_location->addItem(tr("Restore")); + layout->addWidget(m_location, 1, 1); + + QHBoxLayout *buttonLayout = new QHBoxLayout; + layout->addLayout(buttonLayout, 2, 0, 1, 2); + buttonLayout->addStretch(); + + QPushButton *cancelButton = new QPushButton(tr("Cancel")); + connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject())); + buttonLayout->addWidget(cancelButton); + QPushButton *okButton = new QPushButton(tr("Ok")); + connect(okButton, SIGNAL(clicked()), this, SLOT(accept())); + buttonLayout->addWidget(okButton); + + okButton->setDefault(true); +} + +QString CreateDockWidgetDialog::objectName() const +{ + return m_objectName->text(); +} + +Qt::DockWidgetArea CreateDockWidgetDialog::location() const +{ + switch (m_location->currentIndex()) { + case 0: return Qt::TopDockWidgetArea; + case 1: return Qt::LeftDockWidgetArea; + case 2: return Qt::RightDockWidgetArea; + case 3: return Qt::BottomDockWidgetArea; + default: + break; + } + return Qt::NoDockWidgetArea; +} + +void MainWindow::createDockWidget() +{ + CreateDockWidgetDialog dialog(this); + int ret = dialog.exec(); + if (ret == QDialog::Rejected) + return; + + QDockWidget *dw = new QDockWidget; + dw->setObjectName(dialog.objectName()); + dw->setWindowTitle(dialog.objectName()); + dw->setWidget(new QTextEdit); + + Qt::DockWidgetArea area = dialog.location(); + switch (area) { + case Qt::LeftDockWidgetArea: + case Qt::RightDockWidgetArea: + case Qt::TopDockWidgetArea: + case Qt::BottomDockWidgetArea: + addDockWidget(area, dw); + break; + default: + if (!restoreDockWidget(dw)) { + QMessageBox::warning(this, QString(), tr("Failed to restore dock widget")); + delete dw; + return; + } + break; + } + + extraDockWidgets.append(dw); + destroyDockWidgetMenu->setEnabled(true); + destroyDockWidgetMenu->addAction(new QAction(dialog.objectName(), this)); +} + +void MainWindow::destroyDockWidget(QAction *action) +{ + int index = destroyDockWidgetMenu->actions().indexOf(action); + delete extraDockWidgets.takeAt(index); + destroyDockWidgetMenu->removeAction(action); + action->deleteLater(); + + if (destroyDockWidgetMenu->isEmpty()) + destroyDockWidgetMenu->setEnabled(false); +} diff --git a/examples/widgets/mainwindows/mainwindow/mainwindow.h b/examples/widgets/mainwindows/mainwindow/mainwindow.h new file mode 100644 index 0000000000..51969acaa3 --- /dev/null +++ b/examples/widgets/mainwindows/mainwindow/mainwindow.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include + +class ToolBar; +QT_FORWARD_DECLARE_CLASS(QMenu) +QT_FORWARD_DECLARE_CLASS(QSignalMapper) + +class MainWindow : public QMainWindow +{ + Q_OBJECT + + QTextEdit *center; + QList toolBars; + QMenu *dockWidgetMenu; + QMenu *mainWindowMenu; + QSignalMapper *mapper; + QList extraDockWidgets; + QAction *createDockWidgetAction; + QMenu *destroyDockWidgetMenu; + +public: + MainWindow(const QMap &customSizeHints, + QWidget *parent = 0, Qt::WindowFlags flags = 0); + +protected: + void showEvent(QShowEvent *event); + +public slots: + void actionTriggered(QAction *action); + void saveLayout(); + void loadLayout(); + void setCorner(int id); + void switchLayoutDirection(); + void setDockOptions(); + + void createDockWidget(); + void destroyDockWidget(QAction *action); + +private: + void setupToolBar(); + void setupMenuBar(); + void setupDockWidgets(const QMap &customSizeHints); +}; + + +#endif diff --git a/examples/widgets/mainwindows/mainwindow/mainwindow.pro b/examples/widgets/mainwindows/mainwindow/mainwindow.pro new file mode 100644 index 0000000000..1381c460c2 --- /dev/null +++ b/examples/widgets/mainwindows/mainwindow/mainwindow.pro @@ -0,0 +1,17 @@ +TEMPLATE = app +HEADERS += colorswatch.h mainwindow.h toolbar.h +SOURCES += colorswatch.cpp mainwindow.cpp toolbar.cpp main.cpp +build_all:!build_pass { + CONFIG -= build_all + CONFIG += release +} + +RESOURCES += mainwindow.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/mainwindow +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.png *.jpg *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/mainwindows/mainwindow +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/mainwindows/mainwindow/mainwindow.qrc b/examples/widgets/mainwindows/mainwindow/mainwindow.qrc new file mode 100644 index 0000000000..47ff22a3e4 --- /dev/null +++ b/examples/widgets/mainwindows/mainwindow/mainwindow.qrc @@ -0,0 +1,8 @@ + + + qt.png + titlebarLeft.png + titlebarCenter.png + titlebarRight.png + + diff --git a/examples/widgets/mainwindows/mainwindow/qt.png b/examples/widgets/mainwindows/mainwindow/qt.png new file mode 100644 index 0000000000..48fa9fc2ef Binary files /dev/null and b/examples/widgets/mainwindows/mainwindow/qt.png differ diff --git a/examples/widgets/mainwindows/mainwindow/titlebarCenter.png b/examples/widgets/mainwindows/mainwindow/titlebarCenter.png new file mode 100644 index 0000000000..5cc141355c Binary files /dev/null and b/examples/widgets/mainwindows/mainwindow/titlebarCenter.png differ diff --git a/examples/widgets/mainwindows/mainwindow/titlebarLeft.png b/examples/widgets/mainwindows/mainwindow/titlebarLeft.png new file mode 100644 index 0000000000..315166202b Binary files /dev/null and b/examples/widgets/mainwindows/mainwindow/titlebarLeft.png differ diff --git a/examples/widgets/mainwindows/mainwindow/titlebarRight.png b/examples/widgets/mainwindows/mainwindow/titlebarRight.png new file mode 100644 index 0000000000..a4505268ec Binary files /dev/null and b/examples/widgets/mainwindows/mainwindow/titlebarRight.png differ diff --git a/examples/widgets/mainwindows/mainwindow/toolbar.cpp b/examples/widgets/mainwindows/mainwindow/toolbar.cpp new file mode 100644 index 0000000000..d10eccb46d --- /dev/null +++ b/examples/widgets/mainwindows/mainwindow/toolbar.cpp @@ -0,0 +1,383 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "toolbar.h" + +#include +#include +#include +#include +#include +#include +#include + +#include + +static QPixmap genIcon(const QSize &iconSize, const QString &, const QColor &color) +{ + int w = iconSize.width(); + int h = iconSize.height(); + + QImage image(w, h, QImage::Format_ARGB32_Premultiplied); + image.fill(0); + + QPainter p(&image); + + extern void render_qt_text(QPainter *, int, int, const QColor &); + render_qt_text(&p, w, h, color); + + return QPixmap::fromImage(image, Qt::DiffuseDither | Qt::DiffuseAlphaDither); +} + +static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color) +{ return genIcon(iconSize, QString::number(number), color); } + +ToolBar::ToolBar(const QString &title, QWidget *parent) + : QToolBar(parent), spinbox(0), spinboxAction(0) +{ + tip = 0; + setWindowTitle(title); + setObjectName(title); + + setIconSize(QSize(32, 32)); + + QColor bg(palette().background().color()); + menu = new QMenu("One", this); + menu->setIcon(genIcon(iconSize(), 1, Qt::black)); + menu->addAction(genIcon(iconSize(), "A", Qt::blue), "A"); + menu->addAction(genIcon(iconSize(), "B", Qt::blue), "B"); + menu->addAction(genIcon(iconSize(), "C", Qt::blue), "C"); + addAction(menu->menuAction()); + + QAction *two = addAction(genIcon(iconSize(), 2, Qt::white), "Two"); + QFont boldFont; + boldFont.setBold(true); + two->setFont(boldFont); + + addAction(genIcon(iconSize(), 3, Qt::red), "Three"); + addAction(genIcon(iconSize(), 4, Qt::green), "Four"); + addAction(genIcon(iconSize(), 5, Qt::blue), "Five"); + addAction(genIcon(iconSize(), 6, Qt::yellow), "Six"); + orderAction = new QAction(this); + orderAction->setText(tr("Order Items in Tool Bar")); + connect(orderAction, SIGNAL(triggered()), SLOT(order())); + + randomizeAction = new QAction(this); + randomizeAction->setText(tr("Randomize Items in Tool Bar")); + connect(randomizeAction, SIGNAL(triggered()), SLOT(randomize())); + + addSpinBoxAction = new QAction(this); + addSpinBoxAction->setText(tr("Add Spin Box")); + connect(addSpinBoxAction, SIGNAL(triggered()), SLOT(addSpinBox())); + + removeSpinBoxAction = new QAction(this); + removeSpinBoxAction->setText(tr("Remove Spin Box")); + removeSpinBoxAction->setEnabled(false); + connect(removeSpinBoxAction, SIGNAL(triggered()), SLOT(removeSpinBox())); + + movableAction = new QAction(tr("Movable"), this); + movableAction->setCheckable(true); + connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool))); + + allowedAreasActions = new QActionGroup(this); + allowedAreasActions->setExclusive(false); + + allowLeftAction = new QAction(tr("Allow on Left"), this); + allowLeftAction->setCheckable(true); + connect(allowLeftAction, SIGNAL(triggered(bool)), SLOT(allowLeft(bool))); + + allowRightAction = new QAction(tr("Allow on Right"), this); + allowRightAction->setCheckable(true); + connect(allowRightAction, SIGNAL(triggered(bool)), SLOT(allowRight(bool))); + + allowTopAction = new QAction(tr("Allow on Top"), this); + allowTopAction->setCheckable(true); + connect(allowTopAction, SIGNAL(triggered(bool)), SLOT(allowTop(bool))); + + allowBottomAction = new QAction(tr("Allow on Bottom"), this); + allowBottomAction->setCheckable(true); + connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool))); + + allowedAreasActions->addAction(allowLeftAction); + allowedAreasActions->addAction(allowRightAction); + allowedAreasActions->addAction(allowTopAction); + allowedAreasActions->addAction(allowBottomAction); + + areaActions = new QActionGroup(this); + areaActions->setExclusive(true); + + leftAction = new QAction(tr("Place on Left") , this); + leftAction->setCheckable(true); + connect(leftAction, SIGNAL(triggered(bool)), SLOT(placeLeft(bool))); + + rightAction = new QAction(tr("Place on Right") , this); + rightAction->setCheckable(true); + connect(rightAction, SIGNAL(triggered(bool)), SLOT(placeRight(bool))); + + topAction = new QAction(tr("Place on Top") , this); + topAction->setCheckable(true); + connect(topAction, SIGNAL(triggered(bool)), SLOT(placeTop(bool))); + + bottomAction = new QAction(tr("Place on Bottom") , this); + bottomAction->setCheckable(true); + connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool))); + + areaActions->addAction(leftAction); + areaActions->addAction(rightAction); + areaActions->addAction(topAction); + areaActions->addAction(bottomAction); + + toolBarBreakAction = new QAction(tr("Insert break"), this); + connect(toolBarBreakAction, SIGNAL(triggered(bool)), this, SLOT(insertToolBarBreak())); + + connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool))); + + connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool))); + + menu = new QMenu(title, this); + menu->addAction(toggleViewAction()); + menu->addSeparator(); + menu->addAction(orderAction); + menu->addAction(randomizeAction); + menu->addSeparator(); + menu->addAction(addSpinBoxAction); + menu->addAction(removeSpinBoxAction); + menu->addSeparator(); + menu->addAction(movableAction); + menu->addSeparator(); + menu->addActions(allowedAreasActions->actions()); + menu->addSeparator(); + menu->addActions(areaActions->actions()); + menu->addSeparator(); + menu->addAction(toolBarBreakAction); + + connect(menu, SIGNAL(aboutToShow()), this, SLOT(updateMenu())); + + randomize(); +} + +void ToolBar::updateMenu() +{ + QMainWindow *mainWindow = qobject_cast(parentWidget()); + Q_ASSERT(mainWindow != 0); + + const Qt::ToolBarArea area = mainWindow->toolBarArea(this); + const Qt::ToolBarAreas areas = allowedAreas(); + + movableAction->setChecked(isMovable()); + + allowLeftAction->setChecked(isAreaAllowed(Qt::LeftToolBarArea)); + allowRightAction->setChecked(isAreaAllowed(Qt::RightToolBarArea)); + allowTopAction->setChecked(isAreaAllowed(Qt::TopToolBarArea)); + allowBottomAction->setChecked(isAreaAllowed(Qt::BottomToolBarArea)); + + if (allowedAreasActions->isEnabled()) { + allowLeftAction->setEnabled(area != Qt::LeftToolBarArea); + allowRightAction->setEnabled(area != Qt::RightToolBarArea); + allowTopAction->setEnabled(area != Qt::TopToolBarArea); + allowBottomAction->setEnabled(area != Qt::BottomToolBarArea); + } + + leftAction->setChecked(area == Qt::LeftToolBarArea); + rightAction->setChecked(area == Qt::RightToolBarArea); + topAction->setChecked(area == Qt::TopToolBarArea); + bottomAction->setChecked(area == Qt::BottomToolBarArea); + + if (areaActions->isEnabled()) { + leftAction->setEnabled(areas & Qt::LeftToolBarArea); + rightAction->setEnabled(areas & Qt::RightToolBarArea); + topAction->setEnabled(areas & Qt::TopToolBarArea); + bottomAction->setEnabled(areas & Qt::BottomToolBarArea); + } +} + +void ToolBar::order() +{ + QList ordered, actions1 = actions(), + actions2 = findChildren(); + while (!actions2.isEmpty()) { + QAction *action = actions2.takeFirst(); + if (!actions1.contains(action)) + continue; + actions1.removeAll(action); + ordered.append(action); + } + + clear(); + addActions(ordered); + + orderAction->setEnabled(false); +} + +void ToolBar::randomize() +{ + QList randomized, actions = this->actions(); + while (!actions.isEmpty()) { + QAction *action = actions.takeAt(rand() % actions.size()); + randomized.append(action); + } + clear(); + addActions(randomized); + + orderAction->setEnabled(true); +} + +void ToolBar::addSpinBox() +{ + if (!spinbox) { + spinbox = new QSpinBox(this); + } + if (!spinboxAction) + spinboxAction = addWidget(spinbox); + else + addAction(spinboxAction); + + addSpinBoxAction->setEnabled(false); + removeSpinBoxAction->setEnabled(true); +} + +void ToolBar::removeSpinBox() +{ + if (spinboxAction) + removeAction(spinboxAction); + + addSpinBoxAction->setEnabled(true); + removeSpinBoxAction->setEnabled(false); +} + +void ToolBar::allow(Qt::ToolBarArea area, bool a) +{ + Qt::ToolBarAreas areas = allowedAreas(); + areas = a ? areas | area : areas & ~area; + setAllowedAreas(areas); + + if (areaActions->isEnabled()) { + leftAction->setEnabled(areas & Qt::LeftToolBarArea); + rightAction->setEnabled(areas & Qt::RightToolBarArea); + topAction->setEnabled(areas & Qt::TopToolBarArea); + bottomAction->setEnabled(areas & Qt::BottomToolBarArea); + } +} + +void ToolBar::place(Qt::ToolBarArea area, bool p) +{ + if (!p) + return; + + QMainWindow *mainWindow = qobject_cast(parentWidget()); + Q_ASSERT(mainWindow != 0); + + mainWindow->addToolBar(area, this); + + if (allowedAreasActions->isEnabled()) { + allowLeftAction->setEnabled(area != Qt::LeftToolBarArea); + allowRightAction->setEnabled(area != Qt::RightToolBarArea); + allowTopAction->setEnabled(area != Qt::TopToolBarArea); + allowBottomAction->setEnabled(area != Qt::BottomToolBarArea); + } +} + +void ToolBar::changeMovable(bool movable) +{ setMovable(movable); } + +void ToolBar::allowLeft(bool a) +{ allow(Qt::LeftToolBarArea, a); } + +void ToolBar::allowRight(bool a) +{ allow(Qt::RightToolBarArea, a); } + +void ToolBar::allowTop(bool a) +{ allow(Qt::TopToolBarArea, a); } + +void ToolBar::allowBottom(bool a) +{ allow(Qt::BottomToolBarArea, a); } + +void ToolBar::placeLeft(bool p) +{ place(Qt::LeftToolBarArea, p); } + +void ToolBar::placeRight(bool p) +{ place(Qt::RightToolBarArea, p); } + +void ToolBar::placeTop(bool p) +{ place(Qt::TopToolBarArea, p); } + +void ToolBar::placeBottom(bool p) +{ place(Qt::BottomToolBarArea, p); } + +void ToolBar::insertToolBarBreak() +{ + QMainWindow *mainWindow = qobject_cast(parentWidget()); + Q_ASSERT(mainWindow != 0); + + mainWindow->insertToolBarBreak(this); +} + +void ToolBar::enterEvent(QEvent*) +{ +/* + These labels on top of toolbars look darn ugly + + if (tip == 0) { + tip = new QLabel(windowTitle(), this); + QPalette pal = tip->palette(); + QColor c = Qt::black; + c.setAlpha(100); + pal.setColor(QPalette::Window, c); + pal.setColor(QPalette::Foreground, Qt::white); + tip->setPalette(pal); + tip->setAutoFillBackground(true); + tip->setMargin(3); + tip->setText(windowTitle()); + } + QPoint c = rect().center(); + QSize hint = tip->sizeHint(); + tip->setGeometry(c.x() - hint.width()/2, c.y() - hint.height()/2, + hint.width(), hint.height()); + + tip->show(); +*/ +} + +void ToolBar::leaveEvent(QEvent*) +{ + if (tip != 0) + tip->hide(); +} diff --git a/examples/widgets/mainwindows/mainwindow/toolbar.h b/examples/widgets/mainwindows/mainwindow/toolbar.h new file mode 100644 index 0000000000..0ffb1eea5d --- /dev/null +++ b/examples/widgets/mainwindows/mainwindow/toolbar.h @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TOOLBAR_H +#define TOOLBAR_H + +#include + +QT_FORWARD_DECLARE_CLASS(QAction) +QT_FORWARD_DECLARE_CLASS(QActionGroup) +QT_FORWARD_DECLARE_CLASS(QMenu) +QT_FORWARD_DECLARE_CLASS(QSpinBox) +QT_FORWARD_DECLARE_CLASS(QLabel) + +class ToolBar : public QToolBar +{ + Q_OBJECT + + QSpinBox *spinbox; + QAction *spinboxAction; + + QAction *orderAction; + QAction *randomizeAction; + QAction *addSpinBoxAction; + QAction *removeSpinBoxAction; + + QAction *movableAction; + + QActionGroup *allowedAreasActions; + QAction *allowLeftAction; + QAction *allowRightAction; + QAction *allowTopAction; + QAction *allowBottomAction; + + QActionGroup *areaActions; + QAction *leftAction; + QAction *rightAction; + QAction *topAction; + QAction *bottomAction; + + QAction *toolBarBreakAction; + +public: + ToolBar(const QString &title, QWidget *parent); + + QMenu *menu; + +protected: + void enterEvent(QEvent*); + void leaveEvent(QEvent*); + +private: + void allow(Qt::ToolBarArea area, bool allow); + void place(Qt::ToolBarArea area, bool place); + QLabel *tip; + +private slots: + void order(); + void randomize(); + void addSpinBox(); + void removeSpinBox(); + + void changeMovable(bool movable); + + void allowLeft(bool a); + void allowRight(bool a); + void allowTop(bool a); + void allowBottom(bool a); + + void placeLeft(bool p); + void placeRight(bool p); + void placeTop(bool p); + void placeBottom(bool p); + + void updateMenu(); + void insertToolBarBreak(); + +}; + +#endif diff --git a/examples/widgets/mainwindows/mainwindows.pro b/examples/widgets/mainwindows/mainwindows.pro new file mode 100644 index 0000000000..c30d29e95a --- /dev/null +++ b/examples/widgets/mainwindows/mainwindows.pro @@ -0,0 +1,16 @@ +TEMPLATE = subdirs +SUBDIRS = application \ + dockwidgets \ + mainwindow \ + mdi \ + menus \ + recentfiles \ + sdi + +# 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 + +QT += widgets diff --git a/examples/widgets/mainwindows/mdi/images/copy.png b/examples/widgets/mainwindows/mdi/images/copy.png new file mode 100644 index 0000000000..2aeb28288f Binary files /dev/null and b/examples/widgets/mainwindows/mdi/images/copy.png differ diff --git a/examples/widgets/mainwindows/mdi/images/cut.png b/examples/widgets/mainwindows/mdi/images/cut.png new file mode 100644 index 0000000000..54638e9386 Binary files /dev/null and b/examples/widgets/mainwindows/mdi/images/cut.png differ diff --git a/examples/widgets/mainwindows/mdi/images/new.png b/examples/widgets/mainwindows/mdi/images/new.png new file mode 100644 index 0000000000..12131b0100 Binary files /dev/null and b/examples/widgets/mainwindows/mdi/images/new.png differ diff --git a/examples/widgets/mainwindows/mdi/images/open.png b/examples/widgets/mainwindows/mdi/images/open.png new file mode 100644 index 0000000000..45fa2883a7 Binary files /dev/null and b/examples/widgets/mainwindows/mdi/images/open.png differ diff --git a/examples/widgets/mainwindows/mdi/images/paste.png b/examples/widgets/mainwindows/mdi/images/paste.png new file mode 100644 index 0000000000..c14425cad1 Binary files /dev/null and b/examples/widgets/mainwindows/mdi/images/paste.png differ diff --git a/examples/widgets/mainwindows/mdi/images/save.png b/examples/widgets/mainwindows/mdi/images/save.png new file mode 100644 index 0000000000..daba865faf Binary files /dev/null and b/examples/widgets/mainwindows/mdi/images/save.png differ diff --git a/examples/widgets/mainwindows/mdi/main.cpp b/examples/widgets/mainwindows/mdi/main.cpp new file mode 100644 index 0000000000..8beec478a4 --- /dev/null +++ b/examples/widgets/mainwindows/mdi/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/mdi/mainwindow.cpp b/examples/widgets/mainwindows/mdi/mainwindow.cpp new file mode 100644 index 0000000000..6ba15aa224 --- /dev/null +++ b/examples/widgets/mainwindows/mdi/mainwindow.cpp @@ -0,0 +1,411 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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); +} + +#ifndef QT_NO_CLIPBOARD +void MainWindow::cut() +{ + if (activeMdiChild()) + activeMdiChild()->cut(); +} + +void MainWindow::copy() +{ + if (activeMdiChild()) + activeMdiChild()->copy(); +} + +void MainWindow::paste() +{ + if (activeMdiChild()) + activeMdiChild()->paste(); +} +#endif + +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); +#ifndef QT_NO_CLIPBOARD + pasteAct->setEnabled(hasMdiChild); +#endif + closeAct->setEnabled(hasMdiChild); + closeAllAct->setEnabled(hasMdiChild); + tileAct->setEnabled(hasMdiChild); + cascadeAct->setEnabled(hasMdiChild); + nextAct->setEnabled(hasMdiChild); + previousAct->setEnabled(hasMdiChild); + separatorAct->setVisible(hasMdiChild); + +#ifndef QT_NO_CLIPBOARD + bool hasSelection = (activeMdiChild() && + activeMdiChild()->textCursor().hasSelection()); + cutAct->setEnabled(hasSelection); + copyAct->setEnabled(hasSelection); +#endif +} + +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); + +#ifndef QT_NO_CLIPBOARD + connect(child, SIGNAL(copyAvailable(bool)), + cutAct, SLOT(setEnabled(bool))); + connect(child, SIGNAL(copyAvailable(bool)), + copyAct, SLOT(setEnabled(bool))); +#endif + + 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] + +#ifndef QT_NO_CLIPBOARD + 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())); +#endif + + 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")); +#ifndef QT_NO_CLIPBOARD + editMenu->addAction(cutAct); + editMenu->addAction(copyAct); + editMenu->addAction(pasteAct); +#endif + + 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); + +#ifndef QT_NO_CLIPBOARD + editToolBar = addToolBar(tr("Edit")); + editToolBar->addAction(cutAct); + editToolBar->addAction(copyAct); + editToolBar->addAction(pasteAct); +#endif +} + +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/widgets/mainwindows/mdi/mainwindow.h b/examples/widgets/mainwindows/mdi/mainwindow.h new file mode 100644 index 0000000000..1652ca9749 --- /dev/null +++ b/examples/widgets/mainwindows/mdi/mainwindow.h @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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(); +#ifndef QT_NO_CLIPBOARD + void cut(); + void copy(); + void paste(); +#endif + 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; +#ifndef QT_NO_CLIPBOARD + QAction *cutAct; + QAction *copyAct; + QAction *pasteAct; +#endif + QAction *closeAct; + QAction *closeAllAct; + QAction *tileAct; + QAction *cascadeAct; + QAction *nextAct; + QAction *previousAct; + QAction *separatorAct; + QAction *aboutAct; + QAction *aboutQtAct; +}; + +#endif diff --git a/examples/widgets/mainwindows/mdi/mdi.desktop b/examples/widgets/mainwindows/mdi/mdi.desktop new file mode 100644 index 0000000000..267e6f19d0 --- /dev/null +++ b/examples/widgets/mainwindows/mdi/mdi.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=MDI +Exec=/opt/usr/bin/mdi +Icon=mdi +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/mainwindows/mdi/mdi.pro b/examples/widgets/mainwindows/mdi/mdi.pro new file mode 100644 index 0000000000..a4387fecd6 --- /dev/null +++ b/examples/widgets/mainwindows/mdi/mdi.pro @@ -0,0 +1,16 @@ +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 + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/mainwindows/mdi/mdi.qrc b/examples/widgets/mainwindows/mdi/mdi.qrc new file mode 100644 index 0000000000..0a776fab4d --- /dev/null +++ b/examples/widgets/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/widgets/mainwindows/mdi/mdichild.cpp b/examples/widgets/mainwindows/mdi/mdichild.cpp new file mode 100644 index 0000000000..2279fe7044 --- /dev/null +++ b/examples/widgets/mainwindows/mdi/mdichild.cpp @@ -0,0 +1,175 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/mdi/mdichild.h b/examples/widgets/mainwindows/mdi/mdichild.h new file mode 100644 index 0000000000..aacdd0f0f6 --- /dev/null +++ b/examples/widgets/mainwindows/mdi/mdichild.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/menus/main.cpp b/examples/widgets/mainwindows/menus/main.cpp new file mode 100644 index 0000000000..09a923477c --- /dev/null +++ b/examples/widgets/mainwindows/menus/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/menus/mainwindow.cpp b/examples/widgets/mainwindows/menus/mainwindow.cpp new file mode 100644 index 0000000000..e1f3a0c099 --- /dev/null +++ b/examples/widgets/mainwindows/menus/mainwindow.cpp @@ -0,0 +1,370 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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); + setParagraphSpacingAct->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/widgets/mainwindows/menus/mainwindow.h b/examples/widgets/mainwindows/menus/mainwindow.h new file mode 100644 index 0000000000..ebafb529eb --- /dev/null +++ b/examples/widgets/mainwindows/menus/mainwindow.h @@ -0,0 +1,124 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/menus/menus.desktop b/examples/widgets/mainwindows/menus/menus.desktop new file mode 100644 index 0000000000..dc2bda92af --- /dev/null +++ b/examples/widgets/mainwindows/menus/menus.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Menus +Exec=/opt/usr/bin/menus +Icon=menus +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/mainwindows/menus/menus.pro b/examples/widgets/mainwindows/menus/menus.pro new file mode 100644 index 0000000000..5ed1e0dae5 --- /dev/null +++ b/examples/widgets/mainwindows/menus/menus.pro @@ -0,0 +1,11 @@ +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 + +QT += widgets diff --git a/examples/widgets/mainwindows/recentfiles/main.cpp b/examples/widgets/mainwindows/recentfiles/main.cpp new file mode 100644 index 0000000000..023357719e --- /dev/null +++ b/examples/widgets/mainwindows/recentfiles/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/recentfiles/mainwindow.cpp b/examples/widgets/mainwindows/recentfiles/mainwindow.cpp new file mode 100644 index 0000000000..b439ae8950 --- /dev/null +++ b/examples/widgets/mainwindows/recentfiles/mainwindow.cpp @@ -0,0 +1,251 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/recentfiles/mainwindow.h b/examples/widgets/mainwindows/recentfiles/mainwindow.h new file mode 100644 index 0000000000..dd548838fc --- /dev/null +++ b/examples/widgets/mainwindows/recentfiles/mainwindow.h @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/recentfiles/recentfiles.desktop b/examples/widgets/mainwindows/recentfiles/recentfiles.desktop new file mode 100644 index 0000000000..c314cf7f7c --- /dev/null +++ b/examples/widgets/mainwindows/recentfiles/recentfiles.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Recent Files +Exec=/opt/usr/bin/recentfiles +Icon=recentfiles +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/mainwindows/recentfiles/recentfiles.pro b/examples/widgets/mainwindows/recentfiles/recentfiles.pro new file mode 100644 index 0000000000..5425efc68f --- /dev/null +++ b/examples/widgets/mainwindows/recentfiles/recentfiles.pro @@ -0,0 +1,13 @@ +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 + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/mainwindows/sdi/images/copy.png b/examples/widgets/mainwindows/sdi/images/copy.png new file mode 100644 index 0000000000..2aeb28288f Binary files /dev/null and b/examples/widgets/mainwindows/sdi/images/copy.png differ diff --git a/examples/widgets/mainwindows/sdi/images/cut.png b/examples/widgets/mainwindows/sdi/images/cut.png new file mode 100644 index 0000000000..54638e9386 Binary files /dev/null and b/examples/widgets/mainwindows/sdi/images/cut.png differ diff --git a/examples/widgets/mainwindows/sdi/images/new.png b/examples/widgets/mainwindows/sdi/images/new.png new file mode 100644 index 0000000000..12131b0100 Binary files /dev/null and b/examples/widgets/mainwindows/sdi/images/new.png differ diff --git a/examples/widgets/mainwindows/sdi/images/open.png b/examples/widgets/mainwindows/sdi/images/open.png new file mode 100644 index 0000000000..45fa2883a7 Binary files /dev/null and b/examples/widgets/mainwindows/sdi/images/open.png differ diff --git a/examples/widgets/mainwindows/sdi/images/paste.png b/examples/widgets/mainwindows/sdi/images/paste.png new file mode 100644 index 0000000000..c14425cad1 Binary files /dev/null and b/examples/widgets/mainwindows/sdi/images/paste.png differ diff --git a/examples/widgets/mainwindows/sdi/images/save.png b/examples/widgets/mainwindows/sdi/images/save.png new file mode 100644 index 0000000000..daba865faf Binary files /dev/null and b/examples/widgets/mainwindows/sdi/images/save.png differ diff --git a/examples/widgets/mainwindows/sdi/main.cpp b/examples/widgets/mainwindows/sdi/main.cpp new file mode 100644 index 0000000000..fbbedd9f0d --- /dev/null +++ b/examples/widgets/mainwindows/sdi/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/sdi/mainwindow.cpp b/examples/widgets/mainwindows/sdi/mainwindow.cpp new file mode 100644 index 0000000000..bc0685fd77 --- /dev/null +++ b/examples/widgets/mainwindows/sdi/mainwindow.cpp @@ -0,0 +1,372 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/sdi/mainwindow.h b/examples/widgets/mainwindows/sdi/mainwindow.h new file mode 100644 index 0000000000..4bf5ebe9b5 --- /dev/null +++ b/examples/widgets/mainwindows/sdi/mainwindow.h @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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/widgets/mainwindows/sdi/sdi.desktop b/examples/widgets/mainwindows/sdi/sdi.desktop new file mode 100644 index 0000000000..06d7289c8b --- /dev/null +++ b/examples/widgets/mainwindows/sdi/sdi.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=SDI +Exec=/opt/usr/bin/sdi +Icon=sdi +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/mainwindows/sdi/sdi.pro b/examples/widgets/mainwindows/sdi/sdi.pro new file mode 100644 index 0000000000..cd5e0f1616 --- /dev/null +++ b/examples/widgets/mainwindows/sdi/sdi.pro @@ -0,0 +1,14 @@ +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 + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/mainwindows/sdi/sdi.qrc b/examples/widgets/mainwindows/sdi/sdi.qrc new file mode 100644 index 0000000000..0a776fab4d --- /dev/null +++ b/examples/widgets/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