~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to examples/widgets/tools/undo/commands.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 demonstration applications of the Qt Toolkit.
 
7
**
 
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 Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
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 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "commands.h"
 
43
 
 
44
static const int setShapeRectCommandId = 1;
 
45
static const int setShapeColorCommandId = 2;
 
46
 
 
47
/******************************************************************************
 
48
** AddShapeCommand
 
49
*/
 
50
 
 
51
AddShapeCommand::AddShapeCommand(Document *doc, const Shape &shape, QUndoCommand *parent)
 
52
    : QUndoCommand(parent)
 
53
{
 
54
    m_doc = doc;
 
55
    m_shape = shape;
 
56
}
 
57
 
 
58
void AddShapeCommand::undo()
 
59
{
 
60
    m_doc->deleteShape(m_shapeName);
 
61
}
 
62
 
 
63
void 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
 
 
74
RemoveShapeCommand::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
 
 
84
void RemoveShapeCommand::undo()
 
85
{
 
86
    m_shapeName = m_doc->addShape(m_shape);
 
87
}
 
88
 
 
89
void RemoveShapeCommand::redo()
 
90
{
 
91
    m_doc->deleteShape(m_shapeName);
 
92
}
 
93
 
 
94
/******************************************************************************
 
95
** SetShapeColorCommand
 
96
*/
 
97
 
 
98
SetShapeColorCommand::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
 
 
110
void SetShapeColorCommand::undo()
 
111
{
 
112
    m_doc->setShapeColor(m_shapeName, m_oldColor);
 
113
}
 
114
 
 
115
void SetShapeColorCommand::redo()
 
116
{
 
117
    m_doc->setShapeColor(m_shapeName, m_newColor);
 
118
}
 
119
 
 
120
bool 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
 
 
133
int SetShapeColorCommand::id() const
 
134
{
 
135
    return setShapeColorCommandId;
 
136
}
 
137
 
 
138
/******************************************************************************
 
139
** SetShapeRectCommand
 
140
*/
 
141
 
 
142
SetShapeRectCommand::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
 
 
154
void SetShapeRectCommand::undo()
 
155
{
 
156
    m_doc->setShapeRect(m_shapeName, m_oldRect);
 
157
}
 
158
 
 
159
void SetShapeRectCommand::redo()
 
160
{
 
161
    m_doc->setShapeRect(m_shapeName, m_newRect);
 
162
}
 
163
 
 
164
bool 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
 
 
177
int SetShapeRectCommand::id() const
 
178
{
 
179
    return setShapeRectCommandId;
 
180
}