diff options
author | Volker Hilsheimer <[email protected]> | 2023-01-09 13:55:09 +0100 |
---|---|---|
committer | Jan Arve Sæther <[email protected]> | 2023-01-31 00:20:31 +0100 |
commit | cb0bf5ad683b72d4ecb0d21dafde3a5acdaac800 (patch) | |
tree | d16f765a9d41bf178c7bcc0bccccf0fe59ac1fee /examples/widgets/tools/undoframework/mainwindow.cpp | |
parent | 26f8ea1224e43bef9ba3f121c6d5d8e19a8756fc (diff) |
Merge "undo" and "undoframework" examples
The "undo" example didn't show anything that the "undoframework"
example doesn't, and the latter is more comprehensive and properly
documented. "undoframework" also uses QGraphicsView instead of
inventing its own diagram widget.
However, the "undo" example created a nicer UI with toolbuttons,
icons, and the undo view in a dock widget, so reuse those elements
in the "undoframework" example instead.
Update the documentation quoting tags accordingly, and clean up
a bit.
Pick-to: 6.5
Change-Id: I3c91feecbd5fe3e5900838b0b51f9fe7bd190280
Reviewed-by: Jan Arve Sæther <[email protected]>
Diffstat (limited to 'examples/widgets/tools/undoframework/mainwindow.cpp')
-rw-r--r-- | examples/widgets/tools/undoframework/mainwindow.cpp | 91 |
1 files changed, 51 insertions, 40 deletions
diff --git a/examples/widgets/tools/undoframework/mainwindow.cpp b/examples/widgets/tools/undoframework/mainwindow.cpp index 6b13d0d328b..c2a6c1cff6f 100644 --- a/examples/widgets/tools/undoframework/mainwindow.cpp +++ b/examples/widgets/tools/undoframework/mainwindow.cpp @@ -7,70 +7,75 @@ #include "commands.h" #include <QAction> +#include <QDockWidget> #include <QGraphicsView> #include <QMenu> #include <QMenuBar> #include <QMessageBox> +#include <QToolBar> #include <QUndoView> //! [0] MainWindow::MainWindow() { undoStack = new QUndoStack(this); + diagramScene = new DiagramScene(); + + const QBrush pixmapBrush(QPixmap(":/icons/cross.png").scaled(30, 30)); + diagramScene->setBackgroundBrush(pixmapBrush); + diagramScene->setSceneRect(QRect(0, 0, 500, 500)); createActions(); createMenus(); + createToolBars(); 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); + connect(diagramScene, &DiagramScene::selectionChanged, + this, &MainWindow::updateActions); setWindowTitle("Undo Framework"); QGraphicsView *view = new QGraphicsView(diagramScene); setCentralWidget(view); - resize(700, 500); + adjustSize(); } //! [0] //! [1] void MainWindow::createUndoView() { - undoView = new QUndoView(undoStack); - undoView->setWindowTitle(tr("Command List")); - undoView->show(); - undoView->setAttribute(Qt::WA_QuitOnClose, false); + QDockWidget *undoDockWidget = new QDockWidget; + undoDockWidget->setWindowTitle(tr("Command List")); + undoDockWidget->setWidget(new QUndoView(undoStack)); + addDockWidget(Qt::RightDockWidgetArea, undoDockWidget); } //! [1] //! [2] void MainWindow::createActions() { - deleteAction = new QAction(tr("&Delete Item"), this); + deleteAction = new QAction(QIcon(":/icons/remove.png"), tr("&Delete Item"), this); deleteAction->setShortcut(tr("Del")); connect(deleteAction, &QAction::triggered, this, &MainWindow::deleteItem); -//! [2] //! [3] +//! [2] -//! [3] //! [4] - addBoxAction = new QAction(tr("Add &Box"), this); -//! [4] + addBoxAction = new QAction(QIcon(":/icons/rectangle.png"), tr("Add &Box"), this); addBoxAction->setShortcut(tr("Ctrl+O")); connect(addBoxAction, &QAction::triggered, this, &MainWindow::addBox); - addTriangleAction = new QAction(tr("Add &Triangle"), this); + addTriangleAction = new QAction(QIcon(":/icons/triangle.png"), tr("Add &Triangle"), this); addTriangleAction->setShortcut(tr("Ctrl+T")); connect(addTriangleAction, &QAction::triggered, this, &MainWindow::addTriangle); //! [5] undoAction = undoStack->createUndoAction(this, tr("&Undo")); + undoAction->setIcon(QIcon(":/icons/undo.png")); undoAction->setShortcuts(QKeySequence::Undo); redoAction = undoStack->createRedoAction(this, tr("&Redo")); + redoAction->setIcon(QIcon(":/icons/redo.png")); redoAction->setShortcuts(QKeySequence::Redo); //! [5] @@ -83,46 +88,66 @@ void MainWindow::createActions() aboutShortcuts << tr("Ctrl+A") << tr("Ctrl+B"); aboutAction->setShortcuts(aboutShortcuts); connect(aboutAction, &QAction::triggered, this, &MainWindow::about); + +//! [6] + updateActions(); } +void MainWindow::updateActions() +{ + deleteAction->setEnabled(!diagramScene->selectedItems().isEmpty()); +} //! [6] + +//! [7] 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); +//! [8] helpMenu = menuBar()->addMenu(tr("&About")); helpMenu->addAction(aboutAction); -//! [8] } + +void MainWindow::createToolBars() +{ + QToolBar *editToolBar = new QToolBar; + editToolBar->addAction(undoAction); + editToolBar->addAction(redoAction); + editToolBar->addSeparator(); + editToolBar->addAction(deleteAction); //! [8] + QToolBar *itemToolBar = new QToolBar; + itemToolBar->addAction(addBoxAction); + itemToolBar->addAction(addTriangleAction); +//! [9] + addToolBar(editToolBar); + addToolBar(itemToolBar); +} //! [9] + +//! [11] void MainWindow::itemMoved(DiagramItem *movedItem, const QPointF &oldPosition) { undoStack->push(new MoveCommand(movedItem, oldPosition)); } -//! [9] +//! [11] -//! [10] +//! [12] void MainWindow::deleteItem() { if (diagramScene->selectedItems().isEmpty()) @@ -131,20 +156,6 @@ void MainWindow::deleteItem() 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] |