source: trunk/demos/undo/commands.cpp@ 123

Last change on this file since 123 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "commands.h"
43
44static const int setShapeRectCommandId = 1;
45static const int setShapeColorCommandId = 2;
46
47/******************************************************************************
48** AddShapeCommand
49*/
50
51AddShapeCommand::AddShapeCommand(Document *doc, const Shape &shape, QUndoCommand *parent)
52 : QUndoCommand(parent)
53{
54 m_doc = doc;
55 m_shape = shape;
56}
57
58void AddShapeCommand::undo()
59{
60 m_doc->deleteShape(m_shapeName);
61}
62
63void AddShapeCommand::redo()
64{
65 // A shape only gets a name when it is inserted into a document
66 m_shapeName = m_doc->addShape(m_shape);
67 setText(QObject::tr("Add %1").arg(m_shapeName));
68}
69
70/******************************************************************************
71** RemoveShapeCommand
72*/
73
74RemoveShapeCommand::RemoveShapeCommand(Document *doc, const QString &shapeName,
75 QUndoCommand *parent)
76 : QUndoCommand(parent)
77{
78 setText(QObject::tr("Remove %1").arg(shapeName));
79 m_doc = doc;
80 m_shape = doc->shape(shapeName);
81 m_shapeName = shapeName;
82}
83
84void RemoveShapeCommand::undo()
85{
86 m_shapeName = m_doc->addShape(m_shape);
87}
88
89void RemoveShapeCommand::redo()
90{
91 m_doc->deleteShape(m_shapeName);
92}
93
94/******************************************************************************
95** SetShapeColorCommand
96*/
97
98SetShapeColorCommand::SetShapeColorCommand(Document *doc, const QString &shapeName,
99 const QColor &color, QUndoCommand *parent)
100 : QUndoCommand(parent)
101{
102 setText(QObject::tr("Set %1's color").arg(shapeName));
103
104 m_doc = doc;
105 m_shapeName = shapeName;
106 m_oldColor = doc->shape(shapeName).color();
107 m_newColor = color;
108}
109
110void SetShapeColorCommand::undo()
111{
112 m_doc->setShapeColor(m_shapeName, m_oldColor);
113}
114
115void SetShapeColorCommand::redo()
116{
117 m_doc->setShapeColor(m_shapeName, m_newColor);
118}
119
120bool SetShapeColorCommand::mergeWith(const QUndoCommand *command)
121{
122 if (command->id() != setShapeColorCommandId)
123 return false;
124
125 const SetShapeColorCommand *other = static_cast<const SetShapeColorCommand*>(command);
126 if (m_shapeName != other->m_shapeName)
127 return false;
128
129 m_newColor = other->m_newColor;
130 return true;
131}
132
133int SetShapeColorCommand::id() const
134{
135 return setShapeColorCommandId;
136}
137
138/******************************************************************************
139** SetShapeRectCommand
140*/
141
142SetShapeRectCommand::SetShapeRectCommand(Document *doc, const QString &shapeName,
143 const QRect &rect, QUndoCommand *parent)
144 : QUndoCommand(parent)
145{
146 setText(QObject::tr("Change %1's geometry").arg(shapeName));
147
148 m_doc = doc;
149 m_shapeName = shapeName;
150 m_oldRect = doc->shape(shapeName).rect();
151 m_newRect = rect;
152}
153
154void SetShapeRectCommand::undo()
155{
156 m_doc->setShapeRect(m_shapeName, m_oldRect);
157}
158
159void SetShapeRectCommand::redo()
160{
161 m_doc->setShapeRect(m_shapeName, m_newRect);
162}
163
164bool SetShapeRectCommand::mergeWith(const QUndoCommand *command)
165{
166 if (command->id() != setShapeRectCommandId)
167 return false;
168
169 const SetShapeRectCommand *other = static_cast<const SetShapeRectCommand*>(command);
170 if (m_shapeName != other->m_shapeName)
171 return false;
172
173 m_newRect = other->m_newRect;
174 return true;
175}
176
177int SetShapeRectCommand::id() const
178{
179 return setShapeRectCommandId;
180}
Note: See TracBrowser for help on using the repository browser.