Examples: move widgets specific "tools" examples to the correct place
[profile/ivi/qtbase.git] / examples / widgets / tools / undoframework / commands.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 **     of its contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <QtWidgets>
42
43 #include "commands.h"
44 #include "diagramitem.h"
45
46 //! [0]
47 MoveCommand::MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
48                  QUndoCommand *parent)
49     : QUndoCommand(parent)
50 {
51     myDiagramItem = diagramItem;
52     newPos = diagramItem->pos();
53     myOldPos = oldPos;
54 }
55 //! [0]
56
57 //! [1]
58 bool MoveCommand::mergeWith(const QUndoCommand *command)
59 {
60     const MoveCommand *moveCommand = static_cast<const MoveCommand *>(command);
61     DiagramItem *item = moveCommand->myDiagramItem;
62
63     if (myDiagramItem != item)
64     return false;
65
66     newPos = item->pos();
67     setText(QObject::tr("Move %1")
68         .arg(createCommandString(myDiagramItem, newPos)));
69
70     return true;
71 }
72 //! [1]
73
74 //! [2]
75 void MoveCommand::undo()
76 {
77     myDiagramItem->setPos(myOldPos);
78     myDiagramItem->scene()->update();
79     setText(QObject::tr("Move %1")
80         .arg(createCommandString(myDiagramItem, newPos)));
81 }
82 //! [2]
83
84 //! [3]
85 void MoveCommand::redo()
86 {
87     myDiagramItem->setPos(newPos);
88     setText(QObject::tr("Move %1")
89         .arg(createCommandString(myDiagramItem, newPos)));
90 }
91 //! [3]
92
93 //! [4]
94 DeleteCommand::DeleteCommand(QGraphicsScene *scene, QUndoCommand *parent)
95     : QUndoCommand(parent)
96 {
97     myGraphicsScene = scene;
98     QList<QGraphicsItem *> list = myGraphicsScene->selectedItems();
99     list.first()->setSelected(false);
100     myDiagramItem = static_cast<DiagramItem *>(list.first());
101     setText(QObject::tr("Delete %1")
102         .arg(createCommandString(myDiagramItem, myDiagramItem->pos())));
103 }
104 //! [4]
105
106 //! [5]
107 void DeleteCommand::undo()
108 {
109     myGraphicsScene->addItem(myDiagramItem);
110     myGraphicsScene->update();
111 }
112 //! [5]
113
114 //! [6]
115 void DeleteCommand::redo()
116 {
117     myGraphicsScene->removeItem(myDiagramItem);
118 }
119 //! [6]
120
121 //! [7]
122 AddCommand::AddCommand(DiagramItem::DiagramType addType,
123                        QGraphicsScene *scene, QUndoCommand *parent)
124     : QUndoCommand(parent)
125 {
126     static int itemCount = 0;
127
128     myGraphicsScene = scene;
129     myDiagramItem = new DiagramItem(addType);
130     initialPosition = QPointF((itemCount * 15) % int(scene->width()),
131                               (itemCount * 15) % int(scene->height()));
132     scene->update();
133     ++itemCount;
134     setText(QObject::tr("Add %1")
135         .arg(createCommandString(myDiagramItem, initialPosition)));
136 }
137 //! [7]
138
139 AddCommand::~AddCommand()
140 {
141     if (!myDiagramItem->scene())
142         delete myDiagramItem;
143 }
144
145 //! [8]
146 void AddCommand::undo()
147 {
148     myGraphicsScene->removeItem(myDiagramItem);
149     myGraphicsScene->update();
150 }
151 //! [8]
152
153 //! [9]
154 void AddCommand::redo()
155 {
156     myGraphicsScene->addItem(myDiagramItem);
157     myDiagramItem->setPos(initialPosition);
158     myGraphicsScene->clearSelection();
159     myGraphicsScene->update();
160 }
161 //! [9]
162
163 QString createCommandString(DiagramItem *item, const QPointF &pos)
164 {
165     return QObject::tr("%1 at (%2, %3)")
166         .arg(item->diagramType() == DiagramItem::Box ? "Box" : "Triangle")
167         .arg(pos.x()).arg(pos.y());
168 }