Home · All Namespaces · All Classes · Main Classes · Grouped Classes · Modules · Functions

mainwindow.cpp Example File
tools/undoframework/mainwindow.cpp

 /****************************************************************************
 **
 ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
 ** Contact: Qt Software Information ([email protected])
 **
 ** This file is part of the example classes of the Qt Toolkit.
 **
 ** Commercial Usage
 ** Licensees holding valid Qt Commercial licenses may use this file in
 ** accordance with the Qt Commercial License Agreement provided with the
 ** Software or, alternatively, in accordance with the terms contained in
 ** a written agreement between you and Nokia.
 **
 **
 ** GNU General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU
 ** General Public License versions 2.0 or 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 GNU General Public Licensing requirements will be met:
 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
 ** http://www.gnu.org/copyleft/gpl.html.  In addition, as a special
 ** exception, Nokia gives you certain additional rights. These rights
 ** are described in the Nokia Qt GPL Exception version 1.3, included in
 ** the file GPL_EXCEPTION.txt in this package.
 **
 ** Qt for Windows(R) Licensees
 ** As a special exception, Nokia, as the sole copyright holder for Qt
 ** Designer, grants users of the Qt/Eclipse Integration plug-in the
 ** right for the Qt/Eclipse Integration to link to functionality
 ** provided by Qt Designer and its related libraries.
 **
 ** If you are unsure which license is appropriate for your use, please
 ** contact the sales department at [email protected].
 **
 ****************************************************************************/

 #include <QtGui>

 #include "mainwindow.h"
 #include "diagramscene.h"
 #include "diagramitem.h"
 #include "commands.h"

 MainWindow::MainWindow()
 {
     undoStack = new QUndoStack();

     createActions();
     createMenus();

     connect(undoStack, SIGNAL(canRedoChanged(bool)),
             redoAction, SLOT(setEnabled(bool)));
     connect(undoStack, SIGNAL(canUndoChanged(bool)),
             undoAction, SLOT(setEnabled(bool)));
     createUndoView();

     diagramScene = new DiagramScene();
     QBrush pixmapBrush(QPixmap(":/images/cross.png").scaled(30, 30));
     diagramScene->setBackgroundBrush(pixmapBrush);
     diagramScene->setSceneRect(QRect(0, 0, 500, 500));

     connect(diagramScene, SIGNAL(itemMoved(DiagramItem *, const QPointF &)),
             this, SLOT(itemMoved(DiagramItem *, const QPointF &)));

     setWindowTitle("Undo Framework");
     QGraphicsView *view = new QGraphicsView(diagramScene);
     setCentralWidget(view);
     resize(700, 500);
 }

 void MainWindow::createUndoView()
 {
     undoView = new QUndoView(undoStack);
     undoView->setWindowTitle(tr("Command List"));
     undoView->show();
     undoView->setAttribute(Qt::WA_QuitOnClose, false);
 }

 void MainWindow::createActions()
 {
     deleteAction = new QAction(tr("&Delete Item"), this);
     deleteAction->setShortcut(tr("Del"));
     connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));

     addBoxAction = new QAction(tr("Add &Box"), this);
     addBoxAction->setShortcut(tr("Ctrl+O"));
     connect(addBoxAction, SIGNAL(triggered()), this, SLOT(addBox()));

     addTriangleAction = new QAction(tr("Add &Triangle"), this);
     addTriangleAction->setShortcut(tr("Ctrl+T"));
     connect(addTriangleAction, SIGNAL(triggered()), this, SLOT(addTriangle()));

     undoAction = new QAction(tr("&Undo"), this);
     undoAction->setShortcut(tr("Ctrl+Z"));
     undoAction->setEnabled(false);
     connect(undoAction, SIGNAL(triggered()), undoStack, SLOT(undo()));

     redoAction = new QAction(tr("&Redo"), this);
     QList<QKeySequence> redoShortcuts;
     redoShortcuts << tr("Ctrl+Y") << tr("Shift+Ctrl+Z");
     redoAction->setShortcuts(redoShortcuts);
     redoAction->setEnabled(false);
     connect(redoAction, SIGNAL(triggered()), undoStack, SLOT(redo()));

     exitAction = new QAction(tr("E&xit"), this);
     exitAction->setShortcut(tr("Ctrl+Q"));
     connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));

     aboutAction = new QAction(tr("&About"), this);
     QList<QKeySequence> aboutShortcuts;
     aboutShortcuts << tr("Ctrl+A") << tr("Ctrl+B");
     aboutAction->setShortcuts(aboutShortcuts);
     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
 }

 void MainWindow::createMenus()
 {
     fileMenu = menuBar()->addMenu(tr("&File"));
     fileMenu->addAction(exitAction);

     editMenu = menuBar()->addMenu(tr("&Edit"));
     editMenu->addAction(undoAction);
     editMenu->addAction(redoAction);
     editMenu->addSeparator();
     editMenu->addAction(deleteAction);
     connect(editMenu, SIGNAL(aboutToShow()),
             this, SLOT(itemMenuAboutToShow()));
     connect(editMenu, SIGNAL(aboutToHide()),
             this, SLOT(itemMenuAboutToHide()));

     itemMenu = menuBar()->addMenu(tr("&Item"));
     itemMenu->addAction(addBoxAction);
     itemMenu->addAction(addTriangleAction);

     helpMenu = menuBar()->addMenu(tr("&About"));
     helpMenu->addAction(aboutAction);
 }

 void MainWindow::itemMoved(DiagramItem *movedItem,
                            const QPointF &oldPosition)
 {
     undoStack->push(new MoveCommand(movedItem, oldPosition));
 }

 void MainWindow::deleteItem()
 {
     if (diagramScene->selectedItems().isEmpty())
         return;

     QUndoCommand *deleteCommand = new DeleteCommand(diagramScene);
     undoStack->push(deleteCommand);
 }

 void MainWindow::itemMenuAboutToHide()
 {
     deleteAction->setEnabled(true);
 }

 void MainWindow::itemMenuAboutToShow()
 {
     undoAction->setText(tr("Undo ") + undoStack->undoText());
     redoAction->setText(tr("Redo ") + undoStack->redoText());
     deleteAction->setEnabled(!diagramScene->selectedItems().isEmpty());
 }

 void MainWindow::addBox()
 {
     QUndoCommand *addCommand = new AddCommand(DiagramItem::Box, diagramScene);
     undoStack->push(addCommand);
 }

 void MainWindow::addTriangle()
 {
     QUndoCommand *addCommand = new AddCommand(DiagramItem::Triangle,
                                               diagramScene);
     undoStack->push(addCommand);
 }

 void MainWindow::about()
 {
     QMessageBox::about(this, tr("About Undo"),
                        tr("The <b>Undo</b> example demonstrates how to "
                           "use Qt's undo framework."));
 }


Copyright © 2008 Nokia Trademarks
Qt 4.4.3