1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
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 https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "commands.h"
52
53static constexpr int setShapeRectCommandId = 1;
54static constexpr int setShapeColorCommandId = 2;
55
56/******************************************************************************
57** AddShapeCommand
58*/
59
60AddShapeCommand::AddShapeCommand(Document *doc, const Shape &shape, QUndoCommand *parent)
61 : QUndoCommand(parent), m_doc(doc), m_shape(shape)
62{
63}
64
65void AddShapeCommand::undo()
66{
67 m_doc->deleteShape(m_shapeName);
68}
69
70void AddShapeCommand::redo()
71{
72 // A shape only gets a name when it is inserted into a document
73 m_shapeName = m_doc->addShape(m_shape);
74 setText(QObject::tr("Add %1").arg(m_shapeName));
75}
76
77/******************************************************************************
78** RemoveShapeCommand
79*/
80
81RemoveShapeCommand::RemoveShapeCommand(Document *doc, const QString &shapeName,
82 QUndoCommand *parent)
83 : QUndoCommand(parent), m_doc(doc), m_shape(doc->shape(shapeName))
84 , m_shapeName(shapeName)
85{
86 setText(QObject::tr("Remove %1").arg(shapeName));
87}
88
89void RemoveShapeCommand::undo()
90{
91 m_shapeName = m_doc->addShape(m_shape);
92}
93
94void RemoveShapeCommand::redo()
95{
96 m_doc->deleteShape(m_shapeName);
97}
98
99/******************************************************************************
100** SetShapeColorCommand
101*/
102
103SetShapeColorCommand::SetShapeColorCommand(Document *doc, const QString &shapeName,
104 const QColor &color, QUndoCommand *parent)
105 : QUndoCommand(parent), m_doc(doc), m_shapeName(shapeName)
106 , m_oldColor(doc->shape(shapeName).color()), m_newColor(color)
107{
108 setText(QObject::tr("Set %1's color").arg(shapeName));
109}
110
111void SetShapeColorCommand::undo()
112{
113 m_doc->setShapeColor(m_shapeName, m_oldColor);
114}
115
116void SetShapeColorCommand::redo()
117{
118 m_doc->setShapeColor(m_shapeName, m_newColor);
119}
120
121bool SetShapeColorCommand::mergeWith(const QUndoCommand *command)
122{
123 if (command->id() != setShapeColorCommandId)
124 return false;
125
126 const SetShapeColorCommand *other = static_cast<const SetShapeColorCommand*>(command);
127 if (m_shapeName != other->m_shapeName)
128 return false;
129
130 m_newColor = other->m_newColor;
131 return true;
132}
133
134int SetShapeColorCommand::id() const
135{
136 return setShapeColorCommandId;
137}
138
139/******************************************************************************
140** SetShapeRectCommand
141*/
142
143SetShapeRectCommand::SetShapeRectCommand(Document *doc, const QString &shapeName,
144 const QRect &rect, QUndoCommand *parent)
145 : QUndoCommand(parent), m_doc(doc), m_shapeName(shapeName)
146 , m_oldRect(doc->shape(shapeName).rect()), m_newRect(rect)
147{
148 setText(QObject::tr("Change %1's geometry").arg(shapeName));
149}
150
151void SetShapeRectCommand::undo()
152{
153 m_doc->setShapeRect(m_shapeName, m_oldRect);
154}
155
156void SetShapeRectCommand::redo()
157{
158 m_doc->setShapeRect(m_shapeName, m_newRect);
159}
160
161bool SetShapeRectCommand::mergeWith(const QUndoCommand *command)
162{
163 if (command->id() != setShapeRectCommandId)
164 return false;
165
166 const SetShapeRectCommand *other = static_cast<const SetShapeRectCommand*>(command);
167 if (m_shapeName != other->m_shapeName)
168 return false;
169
170 m_newRect = other->m_newRect;
171 return true;
172}
173
174int SetShapeRectCommand::id() const
175{
176 return setShapeRectCommandId;
177}
178