qt5base-lts/examples/widgets/tools/undoframework/mainwindow.cpp
Lucie Gérard 05fc3aef53 Use SPDX license identifiers
Replace the current license disclaimer in files by
a SPDX-License-Identifier.
Files that have to be modified by hand are modified.
License files are organized under LICENSES directory.

Task-number: QTBUG-67283
Change-Id: Id880c92784c40f3bbde861c0d93f58151c18b9f1
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Jörg Bornemann <joerg.bornemann@qt.io>
2022-05-16 16:37:38 +02:00

175 lines
4.4 KiB
C++

// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "mainwindow.h"
#include "diagramscene.h"
#include "diagramitem.h"
#include "commands.h"
#include <QAction>
#include <QGraphicsView>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QUndoView>
//! [0]
MainWindow::MainWindow()
{
undoStack = new QUndoStack(this);
createActions();
createMenus();
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, &DiagramScene::itemMoved,
this, &MainWindow::itemMoved);
setWindowTitle("Undo Framework");
QGraphicsView *view = new QGraphicsView(diagramScene);
setCentralWidget(view);
resize(700, 500);
}
//! [0]
//! [1]
void MainWindow::createUndoView()
{
undoView = new QUndoView(undoStack);
undoView->setWindowTitle(tr("Command List"));
undoView->show();
undoView->setAttribute(Qt::WA_QuitOnClose, false);
}
//! [1]
//! [2]
void MainWindow::createActions()
{
deleteAction = new QAction(tr("&Delete Item"), this);
deleteAction->setShortcut(tr("Del"));
connect(deleteAction, &QAction::triggered, this, &MainWindow::deleteItem);
//! [2] //! [3]
//! [3] //! [4]
addBoxAction = new QAction(tr("Add &Box"), this);
//! [4]
addBoxAction->setShortcut(tr("Ctrl+O"));
connect(addBoxAction, &QAction::triggered, this, &MainWindow::addBox);
addTriangleAction = new QAction(tr("Add &Triangle"), this);
addTriangleAction->setShortcut(tr("Ctrl+T"));
connect(addTriangleAction, &QAction::triggered, this, &MainWindow::addTriangle);
//! [5]
undoAction = undoStack->createUndoAction(this, tr("&Undo"));
undoAction->setShortcuts(QKeySequence::Undo);
redoAction = undoStack->createRedoAction(this, tr("&Redo"));
redoAction->setShortcuts(QKeySequence::Redo);
//! [5]
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcuts(QKeySequence::Quit);
connect(exitAction, &QAction::triggered, this, &QWidget::close);
aboutAction = new QAction(tr("&About"), this);
QList<QKeySequence> aboutShortcuts;
aboutShortcuts << tr("Ctrl+A") << tr("Ctrl+B");
aboutAction->setShortcuts(aboutShortcuts);
connect(aboutAction, &QAction::triggered, this, &MainWindow::about);
}
//! [6]
void MainWindow::createMenus()
{
//! [6]
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(exitAction);
//! [7]
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(undoAction);
editMenu->addAction(redoAction);
editMenu->addSeparator();
editMenu->addAction(deleteAction);
connect(editMenu, &QMenu::aboutToShow,
this, &MainWindow::itemMenuAboutToShow);
connect(editMenu, &QMenu::aboutToHide,
this, &MainWindow::itemMenuAboutToHide);
//! [7]
itemMenu = menuBar()->addMenu(tr("&Item"));
itemMenu->addAction(addBoxAction);
itemMenu->addAction(addTriangleAction);
helpMenu = menuBar()->addMenu(tr("&About"));
helpMenu->addAction(aboutAction);
//! [8]
}
//! [8]
//! [9]
void MainWindow::itemMoved(DiagramItem *movedItem,
const QPointF &oldPosition)
{
undoStack->push(new MoveCommand(movedItem, oldPosition));
}
//! [9]
//! [10]
void MainWindow::deleteItem()
{
if (diagramScene->selectedItems().isEmpty())
return;
QUndoCommand *deleteCommand = new DeleteCommand(diagramScene);
undoStack->push(deleteCommand);
}
//! [10]
//! [11]
void MainWindow::itemMenuAboutToHide()
{
deleteAction->setEnabled(true);
}
//! [11]
//! [12]
void MainWindow::itemMenuAboutToShow()
{
deleteAction->setEnabled(!diagramScene->selectedItems().isEmpty());
}
//! [12]
//! [13]
void MainWindow::addBox()
{
QUndoCommand *addCommand = new AddCommand(DiagramItem::Box, diagramScene);
undoStack->push(addCommand);
}
//! [13]
//! [14]
void MainWindow::addTriangle()
{
QUndoCommand *addCommand = new AddCommand(DiagramItem::Triangle,
diagramScene);
undoStack->push(addCommand);
}
//! [14]
//! [15]
void MainWindow::about()
{
QMessageBox::about(this, tr("About Undo"),
tr("The <b>Undo</b> example demonstrates how to "
"use Qt's undo framework."));
}
//! [15]