Subversion Repositories Scribus

Rev

Rev 22694 | Rev 23017 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4430 cbradney 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
1111 tsoots 7
/***************************************************************************
8
 *   Copyright (C) 2005 by Riku Leino                                      *
5116 tsoots 9
 *   riku@scribus.info                                                     *
1111 tsoots 10
 *                                                                         *
11
 *   This program is free software; you can redistribute it and/or modify  *
12
 *   it under the terms of the GNU General Public License as published by  *
13
 *   the Free Software Foundation; either version 2 of the License, or     *
14
 *   (at your option) any later version.                                   *
15
 *                                                                         *
16
 *   This program is distributed in the hope that it will be useful,       *
17
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19
 *   GNU General Public License for more details.                          *
20
 *                                                                         *
21
 *   You should have received a copy of the GNU General Public License     *
22
 *   along with this program; if not, write to the                         *
23
 *   Free Software Foundation, Inc.,                                       *
18122 mrdocs 24
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
1111 tsoots 25
 ***************************************************************************/
26
 
27
#include "undostate.h"
5116 tsoots 28
#include "undoobject.h"
1111 tsoots 29
 
5116 tsoots 30
UndoState::UndoState(const QString& name, const QString& description, QPixmap* pixmap) :
22601 craig 31
        transactionCode(0),
22694 jghali 32
        m_actionName(name),
33
        m_actionDescription(description),
34
        m_actionPixmap(pixmap),
35
        m_undoObject(nullptr)
1111 tsoots 36
{
5116 tsoots 37
 
1111 tsoots 38
}
39
 
22694 jghali 40
const QString& UndoState::getName() const
1111 tsoots 41
{
22694 jghali 42
        return m_actionName;
1111 tsoots 43
}
44
 
1213 tsoots 45
void UndoState::setName(const QString &newName)
46
{
22694 jghali 47
        m_actionName = newName;
1213 tsoots 48
}
49
 
22694 jghali 50
const QString& UndoState::getDescription() const
1111 tsoots 51
{
22694 jghali 52
        return m_actionDescription;
1111 tsoots 53
}
54
 
1213 tsoots 55
void UndoState::setDescription(const QString &newDescription)
56
{
22694 jghali 57
        m_actionDescription = newDescription;
1213 tsoots 58
}
59
 
1111 tsoots 60
QPixmap* UndoState::getPixmap()
61
{
22694 jghali 62
        return m_actionPixmap;
1111 tsoots 63
}
64
 
1213 tsoots 65
void UndoState::setPixmap(QPixmap *pixmap)
66
{
22694 jghali 67
        m_actionPixmap = pixmap;
1213 tsoots 68
}
69
 
5116 tsoots 70
void UndoState::undo()
71
{
22694 jghali 72
        if (m_undoObject) // if !m_undoObject there's an error, hmmm
73
                m_undoObject->restore(this, true);
5116 tsoots 74
}
75
 
76
void UndoState::redo()
77
{
22694 jghali 78
        if (m_undoObject)
79
                m_undoObject->restore(this, false);
5116 tsoots 80
}
81
 
82
void UndoState::setUndoObject(UndoObject *object)
83
{
22694 jghali 84
        m_undoObject = object->undoObjectPtr();
5116 tsoots 85
}
86
 
87
UndoObject* UndoState::undoObject()
88
{
22694 jghali 89
        return m_undoObject;
5116 tsoots 90
}
91
 
1111 tsoots 92
UndoState::~UndoState()
93
{
1518 tsoots 94
 
1111 tsoots 95
}
96
 
97
/*** SimpleState **************************************************************/
98
 
99
SimpleState::SimpleState(const QString& name, const QString& description, QPixmap* pixmap)
100
: UndoState(name, description, pixmap)
101
{
102
 
103
}
104
 
105
bool SimpleState::contains(const QString& key)
106
{
20793 jghali 107
        return m_values.contains(key);
1111 tsoots 108
}
109
 
13461 jghali 110
QVariant SimpleState::variant(const QString& key, const QVariant& def)
111
{
20793 jghali 112
        QMap<QString, QVariant>::const_iterator it = m_values.find(key);
113
        if (it != m_values.end())
13461 jghali 114
                return it.value();
115
 
20793 jghali 116
        m_values[key] = def;
13461 jghali 117
        return def;
118
}
119
 
1111 tsoots 120
QString SimpleState::get(const QString& key, const QString& def)
121
{
20793 jghali 122
        QMap<QString, QVariant>::const_iterator it = m_values.find(key);
123
        if (it != m_values.end())
13461 jghali 124
                return it.value().toString();
1111 tsoots 125
 
20793 jghali 126
        m_values[key] = def;
13461 jghali 127
        return def;
1111 tsoots 128
}
129
 
22890 jghali 130
bool SimpleState::getBool(const QString& key, bool def)
131
{
132
        bool ok = false;
133
        QVariant retVar = variant(key, QVariant(def));
134
        int ret = retVar.toInt(&ok);
135
        if (!ok)
136
                ret = def;
137
        return ret;
138
}
139
 
1111 tsoots 140
int SimpleState::getInt(const QString& key, int def)
141
{
142
        bool ok = false;
13461 jghali 143
        QVariant retVar = variant(key, QVariant(def));
144
        int ret = retVar.toInt(&ok);
1111 tsoots 145
        if (!ok)
146
                ret = def;
147
        return ret;
148
}
149
 
1238 tsoots 150
uint SimpleState::getUInt(const QString& key, uint def)
151
{
152
        bool ok = false;
13461 jghali 153
        QVariant retVar = variant(key, QVariant(def));
154
        uint ret = retVar.toUInt(&ok);
1238 tsoots 155
        if (!ok)
156
                ret = def;
157
        return ret;
158
}
159
 
1111 tsoots 160
double SimpleState::getDouble(const QString& key, double def)
161
{
162
        bool ok = false;
13461 jghali 163
        QVariant retVar = variant(key, QVariant(def));
164
        double ret = retVar.toDouble(&ok);
1111 tsoots 165
        if (!ok)
166
                ret = def;
167
        return ret;
168
}
169
 
22890 jghali 170
void* SimpleState::getVoidPtr(const QString& key, void* def)
1179 tsoots 171
{
22890 jghali 172
        void* ret = nullptr;
173
        QVariant retVar = variant(key, QVariant::fromValue(def));
174
        if (!retVar.canConvert<void*>())
175
                ret = retVar.value<void*>();
1179 tsoots 176
        return ret;
177
}
178
 
22890 jghali 179
 
20793 jghali 180
void SimpleState::set(const QString& key)
181
{
182
        m_values[key] = QVariant();
183
}
184
 
1111 tsoots 185
void SimpleState::set(const QString& key, const QString& value)
186
{
20793 jghali 187
        m_values[key] = QVariant(value);
1111 tsoots 188
}
189
 
22890 jghali 190
void SimpleState::set(const QString& key, bool value)
191
{
192
        m_values[key] = QVariant(value);
193
}
194
 
1111 tsoots 195
void SimpleState::set(const QString& key, int value)
196
{
20793 jghali 197
        m_values[key] = QVariant(value);
1111 tsoots 198
}
199
 
1238 tsoots 200
void SimpleState::set(const QString& key, uint value)
201
{
20793 jghali 202
        m_values[key] = QVariant(value);
1238 tsoots 203
}
204
 
1111 tsoots 205
void SimpleState::set(const QString& key, double value)
206
{
20793 jghali 207
        m_values[key] = QVariant(value);
1111 tsoots 208
}
209
 
22890 jghali 210
void SimpleState::set(const QString& key, void* ptr)
1179 tsoots 211
{
22890 jghali 212
        m_values[key] = QVariant::fromValue<void*>(ptr);
1179 tsoots 213
}
1111 tsoots 214
 
1518 tsoots 215
SimpleState::~SimpleState()
1111 tsoots 216
{
217
 
218
}
19416 jghali 219
 
220
/*** TransactionState *****************************************************/
221
 
222
TransactionState::TransactionState() : UndoState("")
223
{
22694 jghali 224
        m_size = 0;
19416 jghali 225
}
226
 
19929 jghali 227
UndoState* TransactionState::at(int index) const
19416 jghali 228
{
229
        if (index >= 0 && static_cast<uint>(index) < sizet())
22694 jghali 230
                return m_states[index];
22601 craig 231
        return nullptr;
19416 jghali 232
}
233
 
19929 jghali 234
UndoState* TransactionState::last() const
235
{
22694 jghali 236
        if (!m_states.empty())
237
                return m_states.at(m_size - 1);
22601 craig 238
        return nullptr;
19929 jghali 239
}
240
 
19416 jghali 241
bool TransactionState::contains(int uid) const
242
{
22694 jghali 243
        for (size_t i = 0; i < m_states.size(); ++i)
19416 jghali 244
        {
22694 jghali 245
                UndoObject* undoObject = m_states[i]->undoObject();
19416 jghali 246
                if (undoObject && undoObject->getUId() == static_cast<uint>(uid))
247
                        return true;
248
        }
249
        return false;
250
}
251
 
252
bool TransactionState::containsOnly(int uid) const
253
{
22694 jghali 254
        for (size_t i = 0; i < m_states.size(); ++i)
19416 jghali 255
        {
22694 jghali 256
                UndoObject* undoObject = m_states[i]->undoObject();
19416 jghali 257
                if (undoObject && undoObject->getUId() != static_cast<uint>(uid))
258
                        return false;
259
        }
260
        return true;
261
}
262
 
263
void TransactionState::pushBack(UndoObject *target, UndoState *state)
264
{
265
        if (target && state)
266
        {
267
                state->setUndoObject(target);
22694 jghali 268
                m_states.push_back(state);
269
                ++m_size;
19416 jghali 270
        }
271
}
272
 
19929 jghali 273
uint TransactionState::sizet() const
19416 jghali 274
{
22694 jghali 275
        return m_size;
19416 jghali 276
}
277
 
278
void TransactionState::useActionName()
279
{
22694 jghali 280
        if (m_size > 0)
281
                setName(m_states[m_size - 1]->getName());
19416 jghali 282
}
283
 
284
UndoObject* TransactionState::replace(ulong uid, UndoObject *newUndoObject)
285
{
22601 craig 286
        UndoObject *tmp = nullptr;
22694 jghali 287
        for (size_t i = 0; i < m_states.size(); ++i)
19416 jghali 288
        {
22694 jghali 289
                TransactionState *ts = dynamic_cast<TransactionState*>(m_states[i]);
19416 jghali 290
                if (ts) // are we having a transaction_inside a transaction
291
                        ts->replace(uid, newUndoObject);
22694 jghali 292
                else if (m_states[i]->undoObject() && m_states[i]->undoObject()->getUId() == uid)
19416 jghali 293
                {
22694 jghali 294
                        tmp = m_states[i]->undoObject();
295
                        m_states[i]->setUndoObject(newUndoObject);
19416 jghali 296
                }
297
        }
298
        return tmp;
299
}
300
 
301
void TransactionState::undo() // undo all attached states
302
{
303
        for (int i = sizet() - 1; i > -1; --i)
304
        {
305
                if ((sizet() - 1) == 0)
306
                        at(i)->transactionCode = 0;
307
                else
308
                {
309
                        if (i == static_cast<int>(sizet() - 1))
310
                                at(i)->transactionCode = 1;
311
                        else if (i == 0)
312
                                at(i)->transactionCode = 2;
313
                        else
314
                                at(i)->transactionCode = 3;
315
                }
316
                if (transactionCode != 0)
317
                        at(i)->transactionCode = transactionCode;
318
                at(i)->undo();
319
        }
320
}
321
 
322
void TransactionState::redo() // redo all attached states
323
{
324
        for (uint i = 0; i < sizet(); ++i)
325
        {
326
                if ((sizet() - 1) == 0)
327
                        at(i)->transactionCode = 0;
328
                else
329
                {
330
                        if (i == 0)
331
                                at(i)->transactionCode = 1;
332
                        else if (i == static_cast<uint>(sizet() - 1))
333
                                at(i)->transactionCode = 2;
334
                        else
335
                                at(i)->transactionCode = 3;
336
                }
337
                if (transactionCode != 0)
338
                        at(i)->transactionCode = transactionCode;
339
                at(i)->redo();
340
        }
341
}
342
 
343
TransactionState::~TransactionState()
344
{
22694 jghali 345
        for (size_t i = 0; i < m_states.size(); ++i)
19416 jghali 346
        {
22694 jghali 347
                if (m_states[i])
19416 jghali 348
                {
22694 jghali 349
                        delete m_states[i];
350
                        m_states[i] = nullptr;
19416 jghali 351
                }
352
        }
353
}