diff options
Diffstat (limited to 'examples/mainwindows')
54 files changed, 3512 insertions, 0 deletions
diff --git a/examples/mainwindows/README b/examples/mainwindows/README new file mode 100644 index 0000000..15a6c23 --- /dev/null +++ b/examples/mainwindows/README @@ -0,0 +1,40 @@ +All the standard features of application main windows are provided by Qt. + +Main windows can have pull down menus, tool bars, and dock windows. These +separate forms of user input are unified in an integrated action system that +also supports keyboard shortcuts and accelerator keys in menu items. + + +The example launcher provided with Qt can be used to explore each of the +examples in this directory. + +Documentation for these examples can be found via the Tutorial and Examples +link in the main Qt documentation. + + +Finding the Qt Examples and Demos launcher +========================================== + +On Windows: + +The launcher can be accessed via the Windows Start menu. Select the menu +entry entitled "Qt Examples and Demos" entry in the submenu containing +the Qt tools. + +On Mac OS X: + +For the binary distribution, the qtdemo executable is installed in the +/Developer/Applications/Qt directory. For the source distribution, it is +installed alongside the other Qt tools on the path specified when Qt is +configured. + +On Unix/Linux: + +The qtdemo executable is installed alongside the other Qt tools on the path +specified when Qt is configured. + +On all platforms: + +The source code for the launcher can be found in the demos/qtdemo directory +in the Qt package. This example is built at the same time as the Qt libraries, +tools, examples, and demonstrations. diff --git a/examples/mainwindows/application/application.pro b/examples/mainwindows/application/application.pro new file mode 100644 index 0000000..ad1168a --- /dev/null +++ b/examples/mainwindows/application/application.pro @@ -0,0 +1,12 @@ +HEADERS = mainwindow.h +SOURCES = main.cpp \ + mainwindow.cpp +#! [0] +RESOURCES = application.qrc +#! [0] + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/application +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS application.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/application +INSTALLS += target sources diff --git a/examples/mainwindows/application/application.qrc b/examples/mainwindows/application/application.qrc new file mode 100644 index 0000000..0a776fa --- /dev/null +++ b/examples/mainwindows/application/application.qrc @@ -0,0 +1,10 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/copy.png</file> + <file>images/cut.png</file> + <file>images/new.png</file> + <file>images/open.png</file> + <file>images/paste.png</file> + <file>images/save.png</file> +</qresource> +</RCC> diff --git a/examples/mainwindows/application/images/copy.png b/examples/mainwindows/application/images/copy.png Binary files differnew file mode 100644 index 0000000..2aeb282 --- /dev/null +++ b/examples/mainwindows/application/images/copy.png diff --git a/examples/mainwindows/application/images/cut.png b/examples/mainwindows/application/images/cut.png Binary files differnew file mode 100644 index 0000000..54638e9 --- /dev/null +++ b/examples/mainwindows/application/images/cut.png diff --git a/examples/mainwindows/application/images/new.png b/examples/mainwindows/application/images/new.png Binary files differnew file mode 100644 index 0000000..12131b0 --- /dev/null +++ b/examples/mainwindows/application/images/new.png diff --git a/examples/mainwindows/application/images/open.png b/examples/mainwindows/application/images/open.png Binary files differnew file mode 100644 index 0000000..45fa288 --- /dev/null +++ b/examples/mainwindows/application/images/open.png diff --git a/examples/mainwindows/application/images/paste.png b/examples/mainwindows/application/images/paste.png Binary files differnew file mode 100644 index 0000000..c14425c --- /dev/null +++ b/examples/mainwindows/application/images/paste.png diff --git a/examples/mainwindows/application/images/save.png b/examples/mainwindows/application/images/save.png Binary files differnew file mode 100644 index 0000000..daba865 --- /dev/null +++ b/examples/mainwindows/application/images/save.png diff --git a/examples/mainwindows/application/main.cpp b/examples/mainwindows/application/main.cpp new file mode 100644 index 0000000..0f9d1b2 --- /dev/null +++ b/examples/mainwindows/application/main.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(application); + + QApplication app(argc, argv); + MainWindow mainWin; + mainWin.show(); + return app.exec(); +} +//! [0] diff --git a/examples/mainwindows/application/mainwindow.cpp b/examples/mainwindows/application/mainwindow.cpp new file mode 100644 index 0000000..9a22254 --- /dev/null +++ b/examples/mainwindows/application/mainwindow.cpp @@ -0,0 +1,388 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include <QtGui> + +#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 <b>Application</b> 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->setShortcut(tr("Ctrl+Q")); +//! [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); + QApplication::setOverrideCursor(Qt::WaitCursor); + textEdit->setPlainText(in.readAll()); + QApplication::restoreOverrideCursor(); + + 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); + QApplication::setOverrideCursor(Qt::WaitCursor); + out << textEdit->toPlainText(); + QApplication::restoreOverrideCursor(); + + 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; + if (curFile.isEmpty()) + shownName = "untitled.txt"; + else + shownName = strippedName(curFile); + + setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Application"))); +} +//! [47] + +//! [48] +QString MainWindow::strippedName(const QString &fullFileName) +//! [48] //! [49] +{ + return QFileInfo(fullFileName).fileName(); +} +//! [49] diff --git a/examples/mainwindows/application/mainwindow.h b/examples/mainwindows/application/mainwindow.h new file mode 100644 index 0000000..264be03 --- /dev/null +++ b/examples/mainwindows/application/mainwindow.h @@ -0,0 +1,106 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAction; +class QMenu; +class QPlainTextEdit; +QT_END_NAMESPACE + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +protected: + void closeEvent(QCloseEvent *event); + +private slots: + void newFile(); + void open(); + bool save(); + bool saveAs(); + void about(); + void documentWasModified(); + +private: + void createActions(); + void createMenus(); + void createToolBars(); + void createStatusBar(); + void readSettings(); + void writeSettings(); + bool maybeSave(); + void loadFile(const QString &fileName); + bool saveFile(const QString &fileName); + void setCurrentFile(const QString &fileName); + QString strippedName(const QString &fullFileName); + + QPlainTextEdit *textEdit; + QString curFile; + + QMenu *fileMenu; + QMenu *editMenu; + QMenu *helpMenu; + QToolBar *fileToolBar; + QToolBar *editToolBar; + QAction *newAct; + QAction *openAct; + QAction *saveAct; + QAction *saveAsAct; + QAction *exitAct; + QAction *cutAct; + QAction *copyAct; + QAction *pasteAct; + QAction *aboutAct; + QAction *aboutQtAct; +}; +//! [0] + +#endif diff --git a/examples/mainwindows/dockwidgets/dockwidgets.pro b/examples/mainwindows/dockwidgets/dockwidgets.pro new file mode 100644 index 0000000..afc8aaa --- /dev/null +++ b/examples/mainwindows/dockwidgets/dockwidgets.pro @@ -0,0 +1,10 @@ +HEADERS = mainwindow.h +SOURCES = main.cpp \ + mainwindow.cpp +RESOURCES = dockwidgets.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/dockwidgets +sources.files = $$SOURCES $$HEADERS $$RESOURCES dockwidgets.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/dockwidgets +INSTALLS += target sources diff --git a/examples/mainwindows/dockwidgets/dockwidgets.qrc b/examples/mainwindows/dockwidgets/dockwidgets.qrc new file mode 100644 index 0000000..968feac --- /dev/null +++ b/examples/mainwindows/dockwidgets/dockwidgets.qrc @@ -0,0 +1,8 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/new.png</file> + <file>images/print.png</file> + <file>images/save.png</file> + <file>images/undo.png</file> +</qresource> +</RCC> diff --git a/examples/mainwindows/dockwidgets/images/new.png b/examples/mainwindows/dockwidgets/images/new.png Binary files differnew file mode 100644 index 0000000..dd795cf --- /dev/null +++ b/examples/mainwindows/dockwidgets/images/new.png diff --git a/examples/mainwindows/dockwidgets/images/print.png b/examples/mainwindows/dockwidgets/images/print.png Binary files differnew file mode 100644 index 0000000..2afb769 --- /dev/null +++ b/examples/mainwindows/dockwidgets/images/print.png diff --git a/examples/mainwindows/dockwidgets/images/save.png b/examples/mainwindows/dockwidgets/images/save.png Binary files differnew file mode 100644 index 0000000..46eac82 --- /dev/null +++ b/examples/mainwindows/dockwidgets/images/save.png diff --git a/examples/mainwindows/dockwidgets/images/undo.png b/examples/mainwindows/dockwidgets/images/undo.png Binary files differnew file mode 100644 index 0000000..eee23d2 --- /dev/null +++ b/examples/mainwindows/dockwidgets/images/undo.png diff --git a/examples/mainwindows/dockwidgets/main.cpp b/examples/mainwindows/dockwidgets/main.cpp new file mode 100644 index 0000000..f4bed91 --- /dev/null +++ b/examples/mainwindows/dockwidgets/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Q_INIT_RESOURCE(dockwidgets); + MainWindow mainWin; + mainWin.show(); + return app.exec(); +} diff --git a/examples/mainwindows/dockwidgets/mainwindow.cpp b/examples/mainwindows/dockwidgets/mainwindow.cpp new file mode 100644 index 0000000..1683034 --- /dev/null +++ b/examples/mainwindows/dockwidgets/mainwindow.cpp @@ -0,0 +1,343 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include <QtGui> + +#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 <b>Dock Widgets</b> 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->setShortcut(tr("Ctrl+Q")); + 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(const QString &)), + this, SLOT(insertCustomer(const QString &))); + connect(paragraphsList, SIGNAL(currentTextChanged(const QString &)), + this, SLOT(addParagraph(const QString &))); +} +//! [9] diff --git a/examples/mainwindows/dockwidgets/mainwindow.h b/examples/mainwindows/dockwidgets/mainwindow.h new file mode 100644 index 0000000..f4768d7 --- /dev/null +++ b/examples/mainwindows/dockwidgets/mainwindow.h @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAction; +class QListWidget; +class QMenu; +class QTextEdit; +QT_END_NAMESPACE + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void newLetter(); + void save(); + void print(); + void undo(); + void about(); + void insertCustomer(const QString &customer); + void addParagraph(const QString ¶graph); + +private: + void createActions(); + void createMenus(); + void createToolBars(); + void createStatusBar(); + void createDockWindows(); + + QTextEdit *textEdit; + QListWidget *customerList; + QListWidget *paragraphsList; + + QMenu *fileMenu; + QMenu *editMenu; + QMenu *viewMenu; + QMenu *helpMenu; + QToolBar *fileToolBar; + QToolBar *editToolBar; + QAction *newLetterAct; + QAction *saveAct; + QAction *printAct; + QAction *undoAct; + QAction *aboutAct; + QAction *aboutQtAct; + QAction *quitAct; +}; +//! [0] + +#endif diff --git a/examples/mainwindows/mainwindows.pro b/examples/mainwindows/mainwindows.pro new file mode 100644 index 0000000..90ff3a5 --- /dev/null +++ b/examples/mainwindows/mainwindows.pro @@ -0,0 +1,13 @@ +TEMPLATE = subdirs +SUBDIRS = application \ + dockwidgets \ + mdi \ + menus \ + recentfiles \ + sdi + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/mainwindows +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS mainwindows.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/mainwindows +INSTALLS += target sources diff --git a/examples/mainwindows/mdi/images/copy.png b/examples/mainwindows/mdi/images/copy.png Binary files differnew file mode 100644 index 0000000..2aeb282 --- /dev/null +++ b/examples/mainwindows/mdi/images/copy.png diff --git a/examples/mainwindows/mdi/images/cut.png b/examples/mainwindows/mdi/images/cut.png Binary files differnew file mode 100644 index 0000000..54638e9 --- /dev/null +++ b/examples/mainwindows/mdi/images/cut.png diff --git a/examples/mainwindows/mdi/images/new.png b/examples/mainwindows/mdi/images/new.png Binary files differnew file mode 100644 index 0000000..12131b0 --- /dev/null +++ b/examples/mainwindows/mdi/images/new.png diff --git a/examples/mainwindows/mdi/images/open.png b/examples/mainwindows/mdi/images/open.png Binary files differnew file mode 100644 index 0000000..45fa288 --- /dev/null +++ b/examples/mainwindows/mdi/images/open.png diff --git a/examples/mainwindows/mdi/images/paste.png b/examples/mainwindows/mdi/images/paste.png Binary files differnew file mode 100644 index 0000000..c14425c --- /dev/null +++ b/examples/mainwindows/mdi/images/paste.png diff --git a/examples/mainwindows/mdi/images/save.png b/examples/mainwindows/mdi/images/save.png Binary files differnew file mode 100644 index 0000000..daba865 --- /dev/null +++ b/examples/mainwindows/mdi/images/save.png diff --git a/examples/mainwindows/mdi/main.cpp b/examples/mainwindows/mdi/main.cpp new file mode 100644 index 0000000..8592836 --- /dev/null +++ b/examples/mainwindows/mdi/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(mdi); + + QApplication app(argc, argv); + MainWindow mainWin; + mainWin.show(); + return app.exec(); +} diff --git a/examples/mainwindows/mdi/mainwindow.cpp b/examples/mainwindows/mdi/mainwindow.cpp new file mode 100644 index 0000000..49f9b21 --- /dev/null +++ b/examples/mainwindows/mdi/mainwindow.cpp @@ -0,0 +1,399 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#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 (activeMdiChild()) { + event->ignore(); + } else { + writeSettings(); + event->accept(); + } +} + +void MainWindow::newFile() +{ + MdiChild *child = createMdiChild(); + child->newFile(); + child->show(); +} + +void MainWindow::open() +{ + QString fileName = QFileDialog::getOpenFileName(this); + if (!fileName.isEmpty()) { + QMdiSubWindow *existing = findMdiChild(fileName); + if (existing) { + mdiArea->setActiveSubWindow(existing); + return; + } + + MdiChild *child = createMdiChild(); + if (child->loadFile(fileName)) { + statusBar()->showMessage(tr("File loaded"), 2000); + child->show(); + } else { + child->close(); + } + } +} + +void MainWindow::save() +{ + if (activeMdiChild() && activeMdiChild()->save()) + statusBar()->showMessage(tr("File saved"), 2000); +} + +void MainWindow::saveAs() +{ + if (activeMdiChild() && activeMdiChild()->saveAs()) + statusBar()->showMessage(tr("File saved"), 2000); +} + +void MainWindow::cut() +{ + if (activeMdiChild()) + activeMdiChild()->cut(); +} + +void MainWindow::copy() +{ + if (activeMdiChild()) + activeMdiChild()->copy(); +} + +void MainWindow::paste() +{ + if (activeMdiChild()) + activeMdiChild()->paste(); +} + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About MDI"), + tr("The <b>MDI</b> example demonstrates how to write multiple " + "document interface applications using Qt.")); +} + +void MainWindow::updateMenus() +{ + bool hasMdiChild = (activeMdiChild() != 0); + saveAct->setEnabled(hasMdiChild); + saveAsAct->setEnabled(hasMdiChild); + pasteAct->setEnabled(hasMdiChild); + closeAct->setEnabled(hasMdiChild); + closeAllAct->setEnabled(hasMdiChild); + tileAct->setEnabled(hasMdiChild); + cascadeAct->setEnabled(hasMdiChild); + nextAct->setEnabled(hasMdiChild); + previousAct->setEnabled(hasMdiChild); + separatorAct->setVisible(hasMdiChild); + + bool hasSelection = (activeMdiChild() && + activeMdiChild()->textCursor().hasSelection()); + cutAct->setEnabled(hasSelection); + copyAct->setEnabled(hasSelection); +} + +void MainWindow::updateWindowMenu() +{ + windowMenu->clear(); + windowMenu->addAction(closeAct); + windowMenu->addAction(closeAllAct); + windowMenu->addSeparator(); + windowMenu->addAction(tileAct); + windowMenu->addAction(cascadeAct); + windowMenu->addSeparator(); + windowMenu->addAction(nextAct); + windowMenu->addAction(previousAct); + windowMenu->addAction(separatorAct); + + QList<QMdiSubWindow *> windows = mdiArea->subWindowList(); + separatorAct->setVisible(!windows.isEmpty()); + + for (int i = 0; i < windows.size(); ++i) { + MdiChild *child = qobject_cast<MdiChild *>(windows.at(i)->widget()); + + QString text; + if (i < 9) { + text = tr("&%1 %2").arg(i + 1) + .arg(child->userFriendlyCurrentFile()); + } else { + text = tr("%1 %2").arg(i + 1) + .arg(child->userFriendlyCurrentFile()); + } + QAction *action = windowMenu->addAction(text); + action->setCheckable(true); + action ->setChecked(child == activeMdiChild()); + connect(action, SIGNAL(triggered()), windowMapper, SLOT(map())); + windowMapper->setMapping(action, windows.at(i)); + } +} + +MdiChild *MainWindow::createMdiChild() +{ + MdiChild *child = new MdiChild; + mdiArea->addSubWindow(child); + + connect(child, SIGNAL(copyAvailable(bool)), + cutAct, SLOT(setEnabled(bool))); + connect(child, SIGNAL(copyAvailable(bool)), + copyAct, SLOT(setEnabled(bool))); + + return child; +} + +void MainWindow::createActions() +{ + newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this); + newAct->setShortcuts(QKeySequence::New); + newAct->setStatusTip(tr("Create a new file")); + connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); + + openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this); + openAct->setShortcuts(QKeySequence::Open); + openAct->setStatusTip(tr("Open an existing file")); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + + saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this); + saveAct->setShortcuts(QKeySequence::Save); + saveAct->setStatusTip(tr("Save the document to disk")); + connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); + + saveAsAct = new QAction(tr("Save &As..."), this); + saveAsAct->setShortcuts(QKeySequence::SaveAs); + saveAsAct->setStatusTip(tr("Save the document under a new name")); + connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); + +//! [0] + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcut(tr("Ctrl+Q")); + exitAct->setStatusTip(tr("Exit the application")); + connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows())); +//! [0] + + cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this); + cutAct->setShortcuts(QKeySequence::Cut); + cutAct->setStatusTip(tr("Cut the current selection's contents to the " + "clipboard")); + connect(cutAct, SIGNAL(triggered()), this, SLOT(cut())); + + copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this); + copyAct->setShortcuts(QKeySequence::Copy); + copyAct->setStatusTip(tr("Copy the current selection's contents to the " + "clipboard")); + connect(copyAct, SIGNAL(triggered()), this, SLOT(copy())); + + pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this); + pasteAct->setShortcuts(QKeySequence::Paste); + pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current " + "selection")); + connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste())); + + closeAct = new QAction(tr("Cl&ose"), this); + closeAct->setShortcut(tr("Ctrl+F4")); + closeAct->setStatusTip(tr("Close the active window")); + connect(closeAct, SIGNAL(triggered()), + mdiArea, SLOT(closeActiveSubWindow())); + + closeAllAct = new QAction(tr("Close &All"), this); + closeAllAct->setStatusTip(tr("Close all the windows")); + connect(closeAllAct, SIGNAL(triggered()), + mdiArea, SLOT(closeAllSubWindows())); + + tileAct = new QAction(tr("&Tile"), this); + tileAct->setStatusTip(tr("Tile the windows")); + connect(tileAct, SIGNAL(triggered()), mdiArea, SLOT(tileSubWindows())); + + cascadeAct = new QAction(tr("&Cascade"), this); + cascadeAct->setStatusTip(tr("Cascade the windows")); + connect(cascadeAct, SIGNAL(triggered()), mdiArea, SLOT(cascadeSubWindows())); + + nextAct = new QAction(tr("Ne&xt"), this); + nextAct->setShortcuts(QKeySequence::NextChild); + nextAct->setStatusTip(tr("Move the focus to the next window")); + connect(nextAct, SIGNAL(triggered()), + mdiArea, SLOT(activateNextSubWindow())); + + previousAct = new QAction(tr("Pre&vious"), this); + previousAct->setShortcuts(QKeySequence::PreviousChild); + previousAct->setStatusTip(tr("Move the focus to the previous " + "window")); + connect(previousAct, SIGNAL(triggered()), + mdiArea, SLOT(activatePreviousSubWindow())); + + separatorAct = new QAction(this); + separatorAct->setSeparator(true); + + aboutAct = new QAction(tr("&About"), this); + aboutAct->setStatusTip(tr("Show the application's About box")); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); +} + +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(newAct); + fileMenu->addAction(openAct); + fileMenu->addAction(saveAct); + fileMenu->addAction(saveAsAct); + fileMenu->addSeparator(); + QAction *action = fileMenu->addAction(tr("Switch layout direction")); + connect(action, SIGNAL(triggered()), this, SLOT(switchLayoutDirection())); + fileMenu->addAction(exitAct); + + editMenu = menuBar()->addMenu(tr("&Edit")); + editMenu->addAction(cutAct); + editMenu->addAction(copyAct); + editMenu->addAction(pasteAct); + + windowMenu = menuBar()->addMenu(tr("&Window")); + updateWindowMenu(); + connect(windowMenu, SIGNAL(aboutToShow()), this, SLOT(updateWindowMenu())); + + menuBar()->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +} + +void MainWindow::createToolBars() +{ + fileToolBar = addToolBar(tr("File")); + fileToolBar->addAction(newAct); + fileToolBar->addAction(openAct); + fileToolBar->addAction(saveAct); + + editToolBar = addToolBar(tr("Edit")); + editToolBar->addAction(cutAct); + editToolBar->addAction(copyAct); + editToolBar->addAction(pasteAct); +} + +void MainWindow::createStatusBar() +{ + statusBar()->showMessage(tr("Ready")); +} + +void MainWindow::readSettings() +{ + QSettings settings("Trolltech", "MDI Example"); + QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint(); + QSize size = settings.value("size", QSize(400, 400)).toSize(); + move(pos); + resize(size); +} + +void MainWindow::writeSettings() +{ + QSettings settings("Trolltech", "MDI Example"); + settings.setValue("pos", pos()); + settings.setValue("size", size()); +} + +MdiChild *MainWindow::activeMdiChild() +{ + if (QMdiSubWindow *activeSubWindow = mdiArea->activeSubWindow()) + return qobject_cast<MdiChild *>(activeSubWindow->widget()); + return 0; +} + +QMdiSubWindow *MainWindow::findMdiChild(const QString &fileName) +{ + QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath(); + + foreach (QMdiSubWindow *window, mdiArea->subWindowList()) { + MdiChild *mdiChild = qobject_cast<MdiChild *>(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<QMdiSubWindow *>(window)); +} diff --git a/examples/mainwindows/mdi/mainwindow.h b/examples/mainwindows/mdi/mainwindow.h new file mode 100644 index 0000000..ee91d36 --- /dev/null +++ b/examples/mainwindows/mdi/mainwindow.h @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +class MdiChild; +QT_BEGIN_NAMESPACE +class QAction; +class QMenu; +class QMdiArea; +class QMdiSubWindow; +class QSignalMapper; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +protected: + void closeEvent(QCloseEvent *event); + +private slots: + void newFile(); + void open(); + void save(); + void saveAs(); + void cut(); + void copy(); + void paste(); + void about(); + void updateMenus(); + void updateWindowMenu(); + MdiChild *createMdiChild(); + void switchLayoutDirection(); + void setActiveSubWindow(QWidget *window); + +private: + void createActions(); + void createMenus(); + void createToolBars(); + void createStatusBar(); + void readSettings(); + void writeSettings(); + MdiChild *activeMdiChild(); + QMdiSubWindow *findMdiChild(const QString &fileName); + + QMdiArea *mdiArea; + QSignalMapper *windowMapper; + + QMenu *fileMenu; + QMenu *editMenu; + QMenu *windowMenu; + QMenu *helpMenu; + QToolBar *fileToolBar; + QToolBar *editToolBar; + QAction *newAct; + QAction *openAct; + QAction *saveAct; + QAction *saveAsAct; + QAction *exitAct; + QAction *cutAct; + QAction *copyAct; + QAction *pasteAct; + QAction *closeAct; + QAction *closeAllAct; + QAction *tileAct; + QAction *cascadeAct; + QAction *nextAct; + QAction *previousAct; + QAction *separatorAct; + QAction *aboutAct; + QAction *aboutQtAct; +}; + +#endif diff --git a/examples/mainwindows/mdi/mdi.pro b/examples/mainwindows/mdi/mdi.pro new file mode 100644 index 0000000..4601f1c --- /dev/null +++ b/examples/mainwindows/mdi/mdi.pro @@ -0,0 +1,12 @@ +HEADERS = mainwindow.h \ + mdichild.h +SOURCES = main.cpp \ + mainwindow.cpp \ + mdichild.cpp +RESOURCES = mdi.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/mdi +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS mdi.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/mdi +INSTALLS += target sources diff --git a/examples/mainwindows/mdi/mdi.qrc b/examples/mainwindows/mdi/mdi.qrc new file mode 100644 index 0000000..0a776fa --- /dev/null +++ b/examples/mainwindows/mdi/mdi.qrc @@ -0,0 +1,10 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/copy.png</file> + <file>images/cut.png</file> + <file>images/new.png</file> + <file>images/open.png</file> + <file>images/paste.png</file> + <file>images/save.png</file> +</qresource> +</RCC> diff --git a/examples/mainwindows/mdi/mdichild.cpp b/examples/mainwindows/mdi/mdichild.cpp new file mode 100644 index 0000000..1433450 --- /dev/null +++ b/examples/mainwindows/mdi/mdichild.cpp @@ -0,0 +1,176 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "mdichild.h" + +MdiChild::MdiChild() +{ + setAttribute(Qt::WA_DeleteOnClose); + isUntitled = true; +} + +void MdiChild::newFile() +{ + static int sequenceNumber = 1; + + isUntitled = true; + curFile = tr("document%1.txt").arg(sequenceNumber++); + setWindowTitle(curFile + "[*]"); + + connect(document(), SIGNAL(contentsChanged()), + this, SLOT(documentWasModified())); +} + +bool MdiChild::loadFile(const QString &fileName) +{ + QFile file(fileName); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + QMessageBox::warning(this, tr("MDI"), + tr("Cannot read file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return false; + } + + QTextStream in(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + setPlainText(in.readAll()); + QApplication::restoreOverrideCursor(); + + setCurrentFile(fileName); + + connect(document(), SIGNAL(contentsChanged()), + this, SLOT(documentWasModified())); + + return true; +} + +bool MdiChild::save() +{ + if (isUntitled) { + return saveAs(); + } else { + return saveFile(curFile); + } +} + +bool MdiChild::saveAs() +{ + QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), + curFile); + if (fileName.isEmpty()) + return false; + + return saveFile(fileName); +} + +bool MdiChild::saveFile(const QString &fileName) +{ + QFile file(fileName); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("MDI"), + tr("Cannot write file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return false; + } + + QTextStream out(&file); + QApplication::setOverrideCursor(Qt::WaitCursor); + out << toPlainText(); + QApplication::restoreOverrideCursor(); + + setCurrentFile(fileName); + return true; +} + +QString MdiChild::userFriendlyCurrentFile() +{ + return strippedName(curFile); +} + +void MdiChild::closeEvent(QCloseEvent *event) +{ + if (maybeSave()) { + event->accept(); + } else { + event->ignore(); + } +} + +void MdiChild::documentWasModified() +{ + setWindowModified(document()->isModified()); +} + +bool MdiChild::maybeSave() +{ + if (document()->isModified()) { + QMessageBox::StandardButton ret; + ret = QMessageBox::warning(this, tr("MDI"), + tr("'%1' has been modified.\n" + "Do you want to save your changes?") + .arg(userFriendlyCurrentFile()), + QMessageBox::Save | QMessageBox::Discard + | QMessageBox::Cancel); + if (ret == QMessageBox::Save) + return save(); + else if (ret == QMessageBox::Cancel) + return false; + } + return true; +} + +void MdiChild::setCurrentFile(const QString &fileName) +{ + curFile = QFileInfo(fileName).canonicalFilePath(); + isUntitled = false; + document()->setModified(false); + setWindowModified(false); + setWindowTitle(userFriendlyCurrentFile() + "[*]"); +} + +QString MdiChild::strippedName(const QString &fullFileName) +{ + return QFileInfo(fullFileName).fileName(); +} diff --git a/examples/mainwindows/mdi/mdichild.h b/examples/mainwindows/mdi/mdichild.h new file mode 100644 index 0000000..04084c7 --- /dev/null +++ b/examples/mainwindows/mdi/mdichild.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MDICHILD_H +#define MDICHILD_H + +#include <QTextEdit> + +class MdiChild : public QTextEdit +{ + Q_OBJECT + +public: + MdiChild(); + + void newFile(); + bool loadFile(const QString &fileName); + bool save(); + bool saveAs(); + bool saveFile(const QString &fileName); + QString userFriendlyCurrentFile(); + QString currentFile() { return curFile; } + +protected: + void closeEvent(QCloseEvent *event); + +private slots: + void documentWasModified(); + +private: + bool maybeSave(); + void setCurrentFile(const QString &fileName); + QString strippedName(const QString &fullFileName); + + QString curFile; + bool isUntitled; +}; + +#endif diff --git a/examples/mainwindows/menus/main.cpp b/examples/mainwindows/menus/main.cpp new file mode 100644 index 0000000..1d51376 --- /dev/null +++ b/examples/mainwindows/menus/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow window; + window.show(); + return app.exec(); +} diff --git a/examples/mainwindows/menus/mainwindow.cpp b/examples/mainwindows/menus/mainwindow.cpp new file mode 100644 index 0000000..4c10f73 --- /dev/null +++ b/examples/mainwindows/menus/mainwindow.cpp @@ -0,0 +1,371 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#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("<i>Choose a menu option, or right-click to " + "invoke a context menu</i>")); + 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 <b>File|New</b>")); +} + +void MainWindow::open() +{ + infoLabel->setText(tr("Invoked <b>File|Open</b>")); +} + +void MainWindow::save() +{ + infoLabel->setText(tr("Invoked <b>File|Save</b>")); +} + +void MainWindow::print() +{ + infoLabel->setText(tr("Invoked <b>File|Print</b>")); +} + +void MainWindow::undo() +{ + infoLabel->setText(tr("Invoked <b>Edit|Undo</b>")); +} + +void MainWindow::redo() +{ + infoLabel->setText(tr("Invoked <b>Edit|Redo</b>")); +} + +void MainWindow::cut() +{ + infoLabel->setText(tr("Invoked <b>Edit|Cut</b>")); +} + +void MainWindow::copy() +{ + infoLabel->setText(tr("Invoked <b>Edit|Copy</b>")); +} + +void MainWindow::paste() +{ + infoLabel->setText(tr("Invoked <b>Edit|Paste</b>")); +} + +void MainWindow::bold() +{ + infoLabel->setText(tr("Invoked <b>Edit|Format|Bold</b>")); +} + +void MainWindow::italic() +{ + infoLabel->setText(tr("Invoked <b>Edit|Format|Italic</b>")); +} + +void MainWindow::leftAlign() +{ + infoLabel->setText(tr("Invoked <b>Edit|Format|Left Align</b>")); +} + +void MainWindow::rightAlign() +{ + infoLabel->setText(tr("Invoked <b>Edit|Format|Right Align</b>")); +} + +void MainWindow::justify() +{ + infoLabel->setText(tr("Invoked <b>Edit|Format|Justify</b>")); +} + +void MainWindow::center() +{ + infoLabel->setText(tr("Invoked <b>Edit|Format|Center</b>")); +} + +void MainWindow::setLineSpacing() +{ + infoLabel->setText(tr("Invoked <b>Edit|Format|Set Line Spacing</b>")); +} + +void MainWindow::setParagraphSpacing() +{ + infoLabel->setText(tr("Invoked <b>Edit|Format|Set Paragraph Spacing</b>")); +} + +void MainWindow::about() +{ + infoLabel->setText(tr("Invoked <b>Help|About</b>")); + QMessageBox::about(this, tr("About Menu"), + tr("The <b>Menu</b> example shows how to create " + "menu-bar menus and context menus.")); +} + +void MainWindow::aboutQt() +{ + infoLabel->setText(tr("Invoked <b>Help|About Qt</b>")); +} + +//! [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->setShortcut(tr("Ctrl+Q")); + 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->setShortcut(tr("Ctrl+C")); + 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(tr("Ctrl+B")); + 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(tr("Ctrl+I")); + italicAct->setStatusTip(tr("Make the text italic")); + connect(italicAct, SIGNAL(triggered()), this, SLOT(italic())); + + QFont italicFont = italicAct->font(); + italicFont.setItalic(true); + italicAct->setFont(italicFont); + + setLineSpacingAct = new QAction(tr("Set &Line Spacing..."), this); + setLineSpacingAct->setStatusTip(tr("Change the gap between the lines of a " + "paragraph")); + connect(setLineSpacingAct, SIGNAL(triggered()), this, SLOT(setLineSpacing())); + + setParagraphSpacingAct = new QAction(tr("Set &Paragraph Spacing..."), this); + setLineSpacingAct->setStatusTip(tr("Change the gap between paragraphs")); + connect(setParagraphSpacingAct, SIGNAL(triggered()), + this, SLOT(setParagraphSpacing())); + + aboutAct = new QAction(tr("&About"), this); + aboutAct->setStatusTip(tr("Show the application's About box")); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + connect(aboutQtAct, SIGNAL(triggered()), this, SLOT(aboutQt())); + + leftAlignAct = new QAction(tr("&Left Align"), this); + leftAlignAct->setCheckable(true); + leftAlignAct->setShortcut(tr("Ctrl+L")); + leftAlignAct->setStatusTip(tr("Left align the selected text")); + connect(leftAlignAct, SIGNAL(triggered()), this, SLOT(leftAlign())); + + rightAlignAct = new QAction(tr("&Right Align"), this); + rightAlignAct->setCheckable(true); + rightAlignAct->setShortcut(tr("Ctrl+R")); + rightAlignAct->setStatusTip(tr("Right align the selected text")); + connect(rightAlignAct, SIGNAL(triggered()), this, SLOT(rightAlign())); + + justifyAct = new QAction(tr("&Justify"), this); + justifyAct->setCheckable(true); + justifyAct->setShortcut(tr("Ctrl+J")); + justifyAct->setStatusTip(tr("Justify the selected text")); + connect(justifyAct, SIGNAL(triggered()), this, SLOT(justify())); + + centerAct = new QAction(tr("&Center"), this); + centerAct->setCheckable(true); + centerAct->setShortcut(tr("Ctrl+E")); + centerAct->setStatusTip(tr("Center the selected text")); + connect(centerAct, SIGNAL(triggered()), this, SLOT(center())); + +//! [6] //! [7] + alignmentGroup = new QActionGroup(this); + alignmentGroup->addAction(leftAlignAct); + alignmentGroup->addAction(rightAlignAct); + alignmentGroup->addAction(justifyAct); + alignmentGroup->addAction(centerAct); + leftAlignAct->setChecked(true); +//! [6] +} +//! [7] + +//! [8] +void MainWindow::createMenus() +{ +//! [9] //! [10] + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(newAct); +//! [9] + fileMenu->addAction(openAct); +//! [10] + fileMenu->addAction(saveAct); + fileMenu->addAction(printAct); +//! [11] + fileMenu->addSeparator(); +//! [11] + fileMenu->addAction(exitAct); + + editMenu = menuBar()->addMenu(tr("&Edit")); + editMenu->addAction(undoAct); + editMenu->addAction(redoAct); + editMenu->addSeparator(); + editMenu->addAction(cutAct); + editMenu->addAction(copyAct); + editMenu->addAction(pasteAct); + editMenu->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +//! [8] + +//! [12] + formatMenu = editMenu->addMenu(tr("&Format")); + formatMenu->addAction(boldAct); + formatMenu->addAction(italicAct); + formatMenu->addSeparator()->setText(tr("Alignment")); + formatMenu->addAction(leftAlignAct); + formatMenu->addAction(rightAlignAct); + formatMenu->addAction(justifyAct); + formatMenu->addAction(centerAct); + formatMenu->addSeparator(); + formatMenu->addAction(setLineSpacingAct); + formatMenu->addAction(setParagraphSpacingAct); +} +//! [12] diff --git a/examples/mainwindows/menus/mainwindow.h b/examples/mainwindows/menus/mainwindow.h new file mode 100644 index 0000000..4894a9a --- /dev/null +++ b/examples/mainwindows/menus/mainwindow.h @@ -0,0 +1,125 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAction; +class QActionGroup; +class QLabel; +class QMenu; +QT_END_NAMESPACE + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +protected: + void contextMenuEvent(QContextMenuEvent *event); +//! [0] + +//! [1] +private slots: + void newFile(); + void open(); + void save(); + void print(); + void undo(); + void redo(); + void cut(); + void copy(); + void paste(); + void bold(); + void italic(); + void leftAlign(); + void rightAlign(); + void justify(); + void center(); + void setLineSpacing(); + void setParagraphSpacing(); + void about(); + void aboutQt(); +//! [1] + +//! [2] +private: + void createActions(); + void createMenus(); +//! [2] + +//! [3] + QMenu *fileMenu; + QMenu *editMenu; + QMenu *formatMenu; + QMenu *helpMenu; + QActionGroup *alignmentGroup; + QAction *newAct; + QAction *openAct; + QAction *saveAct; + QAction *printAct; + QAction *exitAct; + QAction *undoAct; + QAction *redoAct; + QAction *cutAct; + QAction *copyAct; + QAction *pasteAct; + QAction *boldAct; + QAction *italicAct; + QAction *leftAlignAct; + QAction *rightAlignAct; + QAction *justifyAct; + QAction *centerAct; + QAction *setLineSpacingAct; + QAction *setParagraphSpacingAct; + QAction *aboutAct; + QAction *aboutQtAct; + QLabel *infoLabel; +}; +//! [3] + +#endif diff --git a/examples/mainwindows/menus/menus.pro b/examples/mainwindows/menus/menus.pro new file mode 100644 index 0000000..7b0a2ec --- /dev/null +++ b/examples/mainwindows/menus/menus.pro @@ -0,0 +1,9 @@ +HEADERS = mainwindow.h +SOURCES = mainwindow.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/menus +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS menus.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/menus +INSTALLS += target sources diff --git a/examples/mainwindows/recentfiles/main.cpp b/examples/mainwindows/recentfiles/main.cpp new file mode 100644 index 0000000..d1c8eb0 --- /dev/null +++ b/examples/mainwindows/recentfiles/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow *mainWin = new MainWindow; + mainWin->show(); + return app.exec(); +} diff --git a/examples/mainwindows/recentfiles/mainwindow.cpp b/examples/mainwindows/recentfiles/mainwindow.cpp new file mode 100644 index 0000000..c8c2f88 --- /dev/null +++ b/examples/mainwindows/recentfiles/mainwindow.cpp @@ -0,0 +1,256 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "mainwindow.h" + +MainWindow::MainWindow() +{ + setAttribute(Qt::WA_DeleteOnClose); + + textEdit = new QTextEdit; + setCentralWidget(textEdit); + + createActions(); + createMenus(); + (void)statusBar(); + + setWindowTitle(tr("Recent Files")); + 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<QAction *>(sender()); + if (action) + loadFile(action->data().toString()); +} + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Recent Files"), + tr("The <b>Recent Files</b> 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->setShortcut(tr("Ctrl+Q")); + 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; + if (curFile.isEmpty()) + setWindowTitle(tr("Recent Files")); + else + setWindowTitle(tr("%1 - %2").arg(strippedName(curFile)) + .arg(tr("Recent Files"))); + + QSettings settings("Trolltech", "Recent Files Example"); + 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<MainWindow *>(widget); + if (mainWin) + mainWin->updateRecentFileActions(); + } +} + +void MainWindow::updateRecentFileActions() +{ + QSettings settings("Trolltech", "Recent Files Example"); + QStringList files = settings.value("recentFileList").toStringList(); + + int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles); + + for (int i = 0; i < numRecentFiles; ++i) { + QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i])); + recentFileActs[i]->setText(text); + recentFileActs[i]->setData(files[i]); + recentFileActs[i]->setVisible(true); + } + for (int j = numRecentFiles; j < MaxRecentFiles; ++j) + recentFileActs[j]->setVisible(false); + + separatorAct->setVisible(numRecentFiles > 0); +} + +QString MainWindow::strippedName(const QString &fullFileName) +{ + return QFileInfo(fullFileName).fileName(); +} diff --git a/examples/mainwindows/recentfiles/mainwindow.h b/examples/mainwindows/recentfiles/mainwindow.h new file mode 100644 index 0000000..9d9d467 --- /dev/null +++ b/examples/mainwindows/recentfiles/mainwindow.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QList> +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAction; +class QMenu; +class QTextEdit; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void newFile(); + void open(); + void save(); + void saveAs(); + void openRecentFile(); + void about(); + +private: + void createActions(); + void createMenus(); + void loadFile(const QString &fileName); + void saveFile(const QString &fileName); + void setCurrentFile(const QString &fileName); + void updateRecentFileActions(); + QString strippedName(const QString &fullFileName); + + QString curFile; + + QTextEdit *textEdit; + QMenu *fileMenu; + QMenu *recentFilesMenu; + QMenu *helpMenu; + QAction *newAct; + QAction *openAct; + QAction *saveAct; + QAction *saveAsAct; + QAction *exitAct; + QAction *aboutAct; + QAction *aboutQtAct; + QAction *separatorAct; + + enum { MaxRecentFiles = 5 }; + QAction *recentFileActs[MaxRecentFiles]; +}; + +#endif diff --git a/examples/mainwindows/recentfiles/recentfiles.pro b/examples/mainwindows/recentfiles/recentfiles.pro new file mode 100644 index 0000000..7c62cdf --- /dev/null +++ b/examples/mainwindows/recentfiles/recentfiles.pro @@ -0,0 +1,9 @@ +HEADERS = mainwindow.h +SOURCES = main.cpp \ + mainwindow.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/recentfiles +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS recentfiles.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/recentfiles +INSTALLS += target sources diff --git a/examples/mainwindows/sdi/images/copy.png b/examples/mainwindows/sdi/images/copy.png Binary files differnew file mode 100644 index 0000000..2aeb282 --- /dev/null +++ b/examples/mainwindows/sdi/images/copy.png diff --git a/examples/mainwindows/sdi/images/cut.png b/examples/mainwindows/sdi/images/cut.png Binary files differnew file mode 100644 index 0000000..54638e9 --- /dev/null +++ b/examples/mainwindows/sdi/images/cut.png diff --git a/examples/mainwindows/sdi/images/new.png b/examples/mainwindows/sdi/images/new.png Binary files differnew file mode 100644 index 0000000..12131b0 --- /dev/null +++ b/examples/mainwindows/sdi/images/new.png diff --git a/examples/mainwindows/sdi/images/open.png b/examples/mainwindows/sdi/images/open.png Binary files differnew file mode 100644 index 0000000..45fa288 --- /dev/null +++ b/examples/mainwindows/sdi/images/open.png diff --git a/examples/mainwindows/sdi/images/paste.png b/examples/mainwindows/sdi/images/paste.png Binary files differnew file mode 100644 index 0000000..c14425c --- /dev/null +++ b/examples/mainwindows/sdi/images/paste.png diff --git a/examples/mainwindows/sdi/images/save.png b/examples/mainwindows/sdi/images/save.png Binary files differnew file mode 100644 index 0000000..daba865 --- /dev/null +++ b/examples/mainwindows/sdi/images/save.png diff --git a/examples/mainwindows/sdi/main.cpp b/examples/mainwindows/sdi/main.cpp new file mode 100644 index 0000000..163ad93 --- /dev/null +++ b/examples/mainwindows/sdi/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(sdi); + QApplication app(argc, argv); + MainWindow *mainWin = new MainWindow; + mainWin->show(); + return app.exec(); +} diff --git a/examples/mainwindows/sdi/mainwindow.cpp b/examples/mainwindows/sdi/mainwindow.cpp new file mode 100644 index 0000000..61d63b3 --- /dev/null +++ b/examples/mainwindows/sdi/mainwindow.cpp @@ -0,0 +1,375 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#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 <b>SDI</b> 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->setShortcut(tr("Ctrl+Q")); + 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("Trolltech", "SDI 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", "SDI Example"); + 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); + + setWindowTitle(tr("%1[*] - %2").arg(strippedName(curFile)) + .arg(tr("SDI"))); +} + +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<MainWindow *>(widget); + if (mainWin && mainWin->curFile == canonicalFilePath) + return mainWin; + } + return 0; +} diff --git a/examples/mainwindows/sdi/mainwindow.h b/examples/mainwindows/sdi/mainwindow.h new file mode 100644 index 0000000..f08cee2 --- /dev/null +++ b/examples/mainwindows/sdi/mainwindow.h @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAction; +class QMenu; +class QTextEdit; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + MainWindow(const QString &fileName); + +protected: + void closeEvent(QCloseEvent *event); + +private slots: + void newFile(); + void open(); + bool save(); + bool saveAs(); + void about(); + void documentWasModified(); + +private: + void init(); + void createActions(); + void createMenus(); + void createToolBars(); + void createStatusBar(); + void readSettings(); + void writeSettings(); + bool maybeSave(); + void loadFile(const QString &fileName); + bool saveFile(const QString &fileName); + void setCurrentFile(const QString &fileName); + QString strippedName(const QString &fullFileName); + MainWindow *findMainWindow(const QString &fileName); + + QTextEdit *textEdit; + QString curFile; + bool isUntitled; + + QMenu *fileMenu; + QMenu *editMenu; + QMenu *helpMenu; + QToolBar *fileToolBar; + QToolBar *editToolBar; + QAction *newAct; + QAction *openAct; + QAction *saveAct; + QAction *saveAsAct; + QAction *closeAct; + QAction *exitAct; + QAction *cutAct; + QAction *copyAct; + QAction *pasteAct; + QAction *aboutAct; + QAction *aboutQtAct; +}; + +#endif diff --git a/examples/mainwindows/sdi/sdi.pro b/examples/mainwindows/sdi/sdi.pro new file mode 100644 index 0000000..3283334 --- /dev/null +++ b/examples/mainwindows/sdi/sdi.pro @@ -0,0 +1,10 @@ +HEADERS = mainwindow.h +SOURCES = main.cpp \ + mainwindow.cpp +RESOURCES = sdi.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/sdi +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS sdi.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/sdi +INSTALLS += target sources diff --git a/examples/mainwindows/sdi/sdi.qrc b/examples/mainwindows/sdi/sdi.qrc new file mode 100644 index 0000000..0a776fa --- /dev/null +++ b/examples/mainwindows/sdi/sdi.qrc @@ -0,0 +1,10 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/copy.png</file> + <file>images/cut.png</file> + <file>images/new.png</file> + <file>images/open.png</file> + <file>images/paste.png</file> + <file>images/save.png</file> +</qresource> +</RCC> |