1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the demonstration applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
44 static const int setShapeRectCommandId = 1;
45 static const int setShapeColorCommandId = 2;
47 /******************************************************************************
51 AddShapeCommand::AddShapeCommand(Document *doc, const Shape &shape, QUndoCommand *parent)
52 : QUndoCommand(parent)
58 void AddShapeCommand::undo()
60 m_doc->deleteShape(m_shapeName);
63 void AddShapeCommand::redo()
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));
70 /******************************************************************************
74 RemoveShapeCommand::RemoveShapeCommand(Document *doc, const QString &shapeName,
76 : QUndoCommand(parent)
78 setText(QObject::tr("Remove %1").arg(shapeName));
80 m_shape = doc->shape(shapeName);
81 m_shapeName = shapeName;
84 void RemoveShapeCommand::undo()
86 m_shapeName = m_doc->addShape(m_shape);
89 void RemoveShapeCommand::redo()
91 m_doc->deleteShape(m_shapeName);
94 /******************************************************************************
95 ** SetShapeColorCommand
98 SetShapeColorCommand::SetShapeColorCommand(Document *doc, const QString &shapeName,
99 const QColor &color, QUndoCommand *parent)
100 : QUndoCommand(parent)
102 setText(QObject::tr("Set %1's color").arg(shapeName));
105 m_shapeName = shapeName;
106 m_oldColor = doc->shape(shapeName).color();
110 void SetShapeColorCommand::undo()
112 m_doc->setShapeColor(m_shapeName, m_oldColor);
115 void SetShapeColorCommand::redo()
117 m_doc->setShapeColor(m_shapeName, m_newColor);
120 bool SetShapeColorCommand::mergeWith(const QUndoCommand *command)
122 if (command->id() != setShapeColorCommandId)
125 const SetShapeColorCommand *other = static_cast<const SetShapeColorCommand*>(command);
126 if (m_shapeName != other->m_shapeName)
129 m_newColor = other->m_newColor;
133 int SetShapeColorCommand::id() const
135 return setShapeColorCommandId;
138 /******************************************************************************
139 ** SetShapeRectCommand
142 SetShapeRectCommand::SetShapeRectCommand(Document *doc, const QString &shapeName,
143 const QRect &rect, QUndoCommand *parent)
144 : QUndoCommand(parent)
146 setText(QObject::tr("Change %1's geometry").arg(shapeName));
149 m_shapeName = shapeName;
150 m_oldRect = doc->shape(shapeName).rect();
154 void SetShapeRectCommand::undo()
156 m_doc->setShapeRect(m_shapeName, m_oldRect);
159 void SetShapeRectCommand::redo()
161 m_doc->setShapeRect(m_shapeName, m_newRect);
164 bool SetShapeRectCommand::mergeWith(const QUndoCommand *command)
166 if (command->id() != setShapeRectCommandId)
169 const SetShapeRectCommand *other = static_cast<const SetShapeRectCommand*>(command);
170 if (m_shapeName != other->m_shapeName)
173 m_newRect = other->m_newRect;
177 int SetShapeRectCommand::id() const
179 return setShapeRectCommandId;