summaryrefslogtreecommitdiffstats
path: root/examples/widgets/tools/undoframework
diff options
context:
space:
mode:
authorAlexandru Croitor <alexandru.croitor@qt.io>2019-10-14 17:46:16 +0200
committerAlexandru Croitor <alexandru.croitor@qt.io>2019-10-14 17:46:34 +0200
commit440286655e0ca271506cf7cc02ad0dbf4baef9ca (patch)
tree896fa81adb8b14a69355a3a6cf64d06ec8173c9a /examples/widgets/tools/undoframework
parent1e27ad1697187549151657ba187928e439300db7 (diff)
parente164d61ca8263fc4b46fdd916e1ea77c7dd2b735 (diff)
Merge remote-tracking branch 'origin/dev' into wip/cmake
Diffstat (limited to 'examples/widgets/tools/undoframework')
-rw-r--r--examples/widgets/tools/undoframework/commands.cpp20
-rw-r--r--examples/widgets/tools/undoframework/commands.h6
-rw-r--r--examples/widgets/tools/undoframework/diagramitem.cpp9
-rw-r--r--examples/widgets/tools/undoframework/diagramitem.h5
-rw-r--r--examples/widgets/tools/undoframework/diagramscene.cpp17
-rw-r--r--examples/widgets/tools/undoframework/diagramscene.h4
-rw-r--r--examples/widgets/tools/undoframework/main.cpp2
-rw-r--r--examples/widgets/tools/undoframework/mainwindow.cpp9
-rw-r--r--examples/widgets/tools/undoframework/mainwindow.h28
9 files changed, 51 insertions, 49 deletions
diff --git a/examples/widgets/tools/undoframework/commands.cpp b/examples/widgets/tools/undoframework/commands.cpp
index c3e7383de1..077d7eccaa 100644
--- a/examples/widgets/tools/undoframework/commands.cpp
+++ b/examples/widgets/tools/undoframework/commands.cpp
@@ -48,19 +48,17 @@
**
****************************************************************************/
-#include <QtWidgets>
-
#include "commands.h"
#include "diagramitem.h"
+#include <QGraphicsScene>
+
//! [0]
MoveCommand::MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
- QUndoCommand *parent)
- : QUndoCommand(parent)
+ QUndoCommand *parent)
+ : QUndoCommand(parent), myDiagramItem(diagramItem)
+ , myOldPos(oldPos), newPos(diagramItem->pos())
{
- myDiagramItem = diagramItem;
- newPos = diagramItem->pos();
- myOldPos = oldPos;
}
//! [0]
@@ -71,7 +69,7 @@ bool MoveCommand::mergeWith(const QUndoCommand *command)
DiagramItem *item = moveCommand->myDiagramItem;
if (myDiagramItem != item)
- return false;
+ return false;
newPos = item->pos();
setText(QObject::tr("Move %1")
@@ -102,9 +100,8 @@ void MoveCommand::redo()
//! [4]
DeleteCommand::DeleteCommand(QGraphicsScene *scene, QUndoCommand *parent)
- : QUndoCommand(parent)
+ : QUndoCommand(parent), myGraphicsScene(scene)
{
- myGraphicsScene = scene;
QList<QGraphicsItem *> list = myGraphicsScene->selectedItems();
list.first()->setSelected(false);
myDiagramItem = static_cast<DiagramItem *>(list.first());
@@ -131,11 +128,10 @@ void DeleteCommand::redo()
//! [7]
AddCommand::AddCommand(DiagramItem::DiagramType addType,
QGraphicsScene *scene, QUndoCommand *parent)
- : QUndoCommand(parent)
+ : QUndoCommand(parent), myGraphicsScene(scene)
{
static int itemCount = 0;
- myGraphicsScene = scene;
myDiagramItem = new DiagramItem(addType);
initialPosition = QPointF((itemCount * 15) % int(scene->width()),
(itemCount * 15) % int(scene->height()));
diff --git a/examples/widgets/tools/undoframework/commands.h b/examples/widgets/tools/undoframework/commands.h
index dc53c14557..185d36d668 100644
--- a/examples/widgets/tools/undoframework/commands.h
+++ b/examples/widgets/tools/undoframework/commands.h
@@ -62,7 +62,7 @@ public:
enum { Id = 1234 };
MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
- QUndoCommand *parent = 0);
+ QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
@@ -80,7 +80,7 @@ private:
class DeleteCommand : public QUndoCommand
{
public:
- explicit DeleteCommand(QGraphicsScene *graphicsScene, QUndoCommand *parent = 0);
+ explicit DeleteCommand(QGraphicsScene *graphicsScene, QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
@@ -96,7 +96,7 @@ class AddCommand : public QUndoCommand
{
public:
AddCommand(DiagramItem::DiagramType addType, QGraphicsScene *graphicsScene,
- QUndoCommand *parent = 0);
+ QUndoCommand *parent = nullptr);
~AddCommand();
void undo() override;
diff --git a/examples/widgets/tools/undoframework/diagramitem.cpp b/examples/widgets/tools/undoframework/diagramitem.cpp
index 723645c9b2..91da6538cf 100644
--- a/examples/widgets/tools/undoframework/diagramitem.cpp
+++ b/examples/widgets/tools/undoframework/diagramitem.cpp
@@ -48,10 +48,11 @@
**
****************************************************************************/
-#include <QtWidgets>
-
#include "diagramitem.h"
+#include <QBrush>
+#include <QRandomGenerator>
+
DiagramItem::DiagramItem(DiagramType diagramType, QGraphicsItem *item)
: QGraphicsPolygonItem(item)
{
@@ -65,7 +66,9 @@ DiagramItem::DiagramItem(DiagramType diagramType, QGraphicsItem *item)
setPolygon(trianglePolygon);
}
- QColor color(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256));
+ QColor color(QRandomGenerator::global()->bounded(256),
+ QRandomGenerator::global()->bounded(256),
+ QRandomGenerator::global()->bounded(256));
QBrush brush(color);
setBrush(brush);
setFlag(QGraphicsItem::ItemIsSelectable);
diff --git a/examples/widgets/tools/undoframework/diagramitem.h b/examples/widgets/tools/undoframework/diagramitem.h
index 1638dcbea8..13ff614427 100644
--- a/examples/widgets/tools/undoframework/diagramitem.h
+++ b/examples/widgets/tools/undoframework/diagramitem.h
@@ -66,9 +66,10 @@ public:
enum { Type = UserType + 1 };
enum DiagramType { Box, Triangle };
- explicit DiagramItem(DiagramType diagramType, QGraphicsItem *item = 0);
+ explicit DiagramItem(DiagramType diagramType, QGraphicsItem *item = nullptr);
- DiagramType diagramType() const {
+ DiagramType diagramType() const
+ {
return polygon() == boxPolygon ? Box : Triangle;
}
int type() const override { return Type; }
diff --git a/examples/widgets/tools/undoframework/diagramscene.cpp b/examples/widgets/tools/undoframework/diagramscene.cpp
index 63aad97cb1..65909ab6cb 100644
--- a/examples/widgets/tools/undoframework/diagramscene.cpp
+++ b/examples/widgets/tools/undoframework/diagramscene.cpp
@@ -48,27 +48,24 @@
**
****************************************************************************/
-#include <QtWidgets>
-
#include "diagramscene.h"
#include "diagramitem.h"
+#include <QGraphicsSceneMouseEvent>
+
DiagramScene::DiagramScene(QObject *parent)
: QGraphicsScene(parent)
-{
- movingItem = 0;
-}
+{}
void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QPointF mousePos(event->buttonDownScenePos(Qt::LeftButton).x(),
event->buttonDownScenePos(Qt::LeftButton).y());
const QList<QGraphicsItem *> itemList = items(mousePos);
- movingItem = itemList.isEmpty() ? 0 : itemList.first();
+ movingItem = itemList.isEmpty() ? nullptr : itemList.first();
- if (movingItem != 0 && event->button() == Qt::LeftButton) {
+ if (movingItem != nullptr && event->button() == Qt::LeftButton)
oldPos = movingItem->pos();
- }
clearSelection();
QGraphicsScene::mousePressEvent(event);
@@ -76,11 +73,11 @@ void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
- if (movingItem != 0 && event->button() == Qt::LeftButton) {
+ if (movingItem != nullptr && event->button() == Qt::LeftButton) {
if (oldPos != movingItem->pos())
emit itemMoved(qgraphicsitem_cast<DiagramItem *>(movingItem),
oldPos);
- movingItem = 0;
+ movingItem = nullptr;
}
QGraphicsScene::mouseReleaseEvent(event);
}
diff --git a/examples/widgets/tools/undoframework/diagramscene.h b/examples/widgets/tools/undoframework/diagramscene.h
index b1e2804ba6..205d4162bb 100644
--- a/examples/widgets/tools/undoframework/diagramscene.h
+++ b/examples/widgets/tools/undoframework/diagramscene.h
@@ -66,7 +66,7 @@ class DiagramScene : public QGraphicsScene
Q_OBJECT
public:
- DiagramScene(QObject *parent = 0);
+ DiagramScene(QObject *parent = nullptr);
signals:
void itemMoved(DiagramItem *movedItem, const QPointF &movedFromPosition);
@@ -76,7 +76,7 @@ protected:
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
private:
- QGraphicsItem *movingItem;
+ QGraphicsItem *movingItem = nullptr;
QPointF oldPos;
};
//! [0]
diff --git a/examples/widgets/tools/undoframework/main.cpp b/examples/widgets/tools/undoframework/main.cpp
index 51fb5c53eb..cf090f56eb 100644
--- a/examples/widgets/tools/undoframework/main.cpp
+++ b/examples/widgets/tools/undoframework/main.cpp
@@ -48,7 +48,7 @@
**
****************************************************************************/
-#include <QtWidgets>
+#include <QApplication>
#include "mainwindow.h"
diff --git a/examples/widgets/tools/undoframework/mainwindow.cpp b/examples/widgets/tools/undoframework/mainwindow.cpp
index e95d50d164..583af11a2b 100644
--- a/examples/widgets/tools/undoframework/mainwindow.cpp
+++ b/examples/widgets/tools/undoframework/mainwindow.cpp
@@ -48,13 +48,18 @@
**
****************************************************************************/
-#include <QtWidgets>
-
#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()
{
diff --git a/examples/widgets/tools/undoframework/mainwindow.h b/examples/widgets/tools/undoframework/mainwindow.h
index def2b7d789..c1a56e0770 100644
--- a/examples/widgets/tools/undoframework/mainwindow.h
+++ b/examples/widgets/tools/undoframework/mainwindow.h
@@ -87,22 +87,22 @@ private:
void createMenus();
void createUndoView();
- QAction *deleteAction;
- QAction *addBoxAction;
- QAction *addTriangleAction;
- QAction *undoAction;
- QAction *redoAction;
- QAction *exitAction;
- QAction *aboutAction;
+ QAction *deleteAction = nullptr;
+ QAction *addBoxAction = nullptr;
+ QAction *addTriangleAction = nullptr;
+ QAction *undoAction = nullptr;
+ QAction *redoAction = nullptr;
+ QAction *exitAction = nullptr;
+ QAction *aboutAction = nullptr;
- QMenu *fileMenu;
- QMenu *editMenu;
- QMenu *itemMenu;
- QMenu *helpMenu;
+ QMenu *fileMenu = nullptr;
+ QMenu *editMenu = nullptr;
+ QMenu *itemMenu = nullptr;
+ QMenu *helpMenu = nullptr;
- DiagramScene *diagramScene;
- QUndoStack *undoStack;
- QUndoView *undoView;
+ DiagramScene *diagramScene = nullptr;
+ QUndoStack *undoStack = nullptr;
+ QUndoView *undoView = nullptr;
};
//! [0]