Subversion Repositories Scribus

Rev

Rev 24902 | 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) :
22694 jghali 31
        m_actionName(name),
32
        m_actionDescription(description),
33
        m_actionPixmap(pixmap),
34
        m_undoObject(nullptr)
1111 tsoots 35
{
5116 tsoots 36
 
1111 tsoots 37
}
38
 
22694 jghali 39
const QString& UndoState::getName() const
1111 tsoots 40
{
22694 jghali 41
        return m_actionName;
1111 tsoots 42
}
43
 
1213 tsoots 44
void UndoState::setName(const QString &newName)
45
{
22694 jghali 46
        m_actionName = newName;
1213 tsoots 47
}
48
 
22694 jghali 49
const QString& UndoState::getDescription() const
1111 tsoots 50
{
22694 jghali 51
        return m_actionDescription;
1111 tsoots 52
}
53
 
1213 tsoots 54
void UndoState::setDescription(const QString &newDescription)
55
{
22694 jghali 56
        m_actionDescription = newDescription;
1213 tsoots 57
}
58
 
1111 tsoots 59
QPixmap* UndoState::getPixmap()
60
{
22694 jghali 61
        return m_actionPixmap;
1111 tsoots 62
}
63
 
1213 tsoots 64
void UndoState::setPixmap(QPixmap *pixmap)
65
{
22694 jghali 66
        m_actionPixmap = pixmap;
1213 tsoots 67
}
68
 
5116 tsoots 69
void UndoState::undo()
70
{
22694 jghali 71
        if (m_undoObject) // if !m_undoObject there's an error, hmmm
72
                m_undoObject->restore(this, true);
5116 tsoots 73
}
74
 
75
void UndoState::redo()
76
{
22694 jghali 77
        if (m_undoObject)
78
                m_undoObject->restore(this, false);
5116 tsoots 79
}
80
 
81
void UndoState::setUndoObject(UndoObject *object)
82
{
22694 jghali 83
        m_undoObject = object->undoObjectPtr();
5116 tsoots 84
}
85
 
86
UndoObject* UndoState::undoObject()
87
{
22694 jghali 88
        return m_undoObject;
5116 tsoots 89
}
90
 
1111 tsoots 91
/*** SimpleState **************************************************************/
92
 
93
SimpleState::SimpleState(const QString& name, const QString& description, QPixmap* pixmap)
24730 jghali 94
           : UndoState(name, description, pixmap)
1111 tsoots 95
{
96
 
97
}
98
 
24730 jghali 99
bool SimpleState::contains(const QString& key) const
1111 tsoots 100
{
20793 jghali 101
        return m_values.contains(key);
1111 tsoots 102
}
103
 
24730 jghali 104
QVariant SimpleState::variant(const QString& key, const QVariant& def) const
13461 jghali 105
{
23652 craig 106
        QMap<QString, QVariant>::const_iterator it = m_values.constFind(key);
107
        if (it != m_values.constEnd())
13461 jghali 108
                return it.value();
109
 
110
        return def;
111
}
112
 
24730 jghali 113
QString SimpleState::get(const QString& key, const QString& def) const
1111 tsoots 114
{
23652 craig 115
        QMap<QString, QVariant>::const_iterator it = m_values.constFind(key);
116
        if (it != m_values.constEnd())
13461 jghali 117
                return it.value().toString();
1111 tsoots 118
 
13461 jghali 119
        return def;
1111 tsoots 120
}
121
 
24730 jghali 122
bool SimpleState::getBool(const QString& key, bool def) const
22890 jghali 123
{
124
        bool ok = false;
125
        QVariant retVar = variant(key, QVariant(def));
126
        int ret = retVar.toInt(&ok);
127
        if (!ok)
128
                ret = def;
129
        return ret;
130
}
131
 
24730 jghali 132
int SimpleState::getInt(const QString& key, int def) const
1111 tsoots 133
{
134
        bool ok = false;
13461 jghali 135
        QVariant retVar = variant(key, QVariant(def));
136
        int ret = retVar.toInt(&ok);
1111 tsoots 137
        if (!ok)
138
                ret = def;
139
        return ret;
140
}
141
 
24902 jghali 142
qint64 SimpleState::getInt64(const QString& key, qint64 def) const
24837 jghali 143
{
144
        bool ok = false;
145
        QVariant retVar = variant(key, QVariant(def));
24902 jghali 146
        qint64 ret = retVar.toLongLong(&ok);
24837 jghali 147
        if (!ok)
148
                ret = def;
149
        return ret;
150
}
151
 
24730 jghali 152
uint SimpleState::getUInt(const QString& key, uint def) const
1238 tsoots 153
{
154
        bool ok = false;
13461 jghali 155
        QVariant retVar = variant(key, QVariant(def));
156
        uint ret = retVar.toUInt(&ok);
1238 tsoots 157
        if (!ok)
158
                ret = def;
159
        return ret;
160
}
161
 
24909 jghali 162
quint64 SimpleState::getUInt64(const QString& key, quint64 def) const
24837 jghali 163
{
164
        bool ok = false;
165
        QVariant retVar = variant(key, QVariant(def));
24902 jghali 166
        quint64 ret = retVar.toULongLong(&ok);
24837 jghali 167
        if (!ok)
168
                ret = def;
169
        return ret;
170
}
171
 
24730 jghali 172
double SimpleState::getDouble(const QString& key, double def) const
1111 tsoots 173
{
174
        bool ok = false;
13461 jghali 175
        QVariant retVar = variant(key, QVariant(def));
176
        double ret = retVar.toDouble(&ok);
1111 tsoots 177
        if (!ok)
178
                ret = def;
179
        return ret;
180
}
181
 
24730 jghali 182
void* SimpleState::getVoidPtr(const QString& key, void* def) const
1179 tsoots 183
{
22890 jghali 184
        void* ret = nullptr;
185
        QVariant retVar = variant(key, QVariant::fromValue(def));
23243 jghali 186
        if (retVar.canConvert<void*>())
22890 jghali 187
                ret = retVar.value<void*>();
1179 tsoots 188
        return ret;
189
}
190
 
22890 jghali 191
 
20793 jghali 192
void SimpleState::set(const QString& key)
193
{
194
        m_values[key] = QVariant();
195
}
196
 
1111 tsoots 197
void SimpleState::set(const QString& key, const QString& value)
198
{
20793 jghali 199
        m_values[key] = QVariant(value);
1111 tsoots 200
}
201
 
22890 jghali 202
void SimpleState::set(const QString& key, bool value)
203
{
204
        m_values[key] = QVariant(value);
205
}
206
 
1111 tsoots 207
void SimpleState::set(const QString& key, int value)
208
{
20793 jghali 209
        m_values[key] = QVariant(value);
1111 tsoots 210
}
211
 
24902 jghali 212
void SimpleState::set(const QString& key, qlonglong value)
24837 jghali 213
{
214
        m_values[key] = QVariant(value);
215
}
216
 
1238 tsoots 217
void SimpleState::set(const QString& key, uint value)
218
{
20793 jghali 219
        m_values[key] = QVariant(value);
1238 tsoots 220
}
221
 
24902 jghali 222
void SimpleState::set(const QString& key, qulonglong value)
24837 jghali 223
{
224
        m_values[key] = QVariant(value);
225
}
226
 
1111 tsoots 227
void SimpleState::set(const QString& key, double value)
228
{
20793 jghali 229
        m_values[key] = QVariant(value);
1111 tsoots 230
}
231
 
22890 jghali 232
void SimpleState::set(const QString& key, void* ptr)
1179 tsoots 233
{
22890 jghali 234
        m_values[key] = QVariant::fromValue<void*>(ptr);
1179 tsoots 235
}
1111 tsoots 236
 
19416 jghali 237
/*** TransactionState *****************************************************/
238
 
23653 craig 239
TransactionState::TransactionState() : UndoState(QString())
19416 jghali 240
{
24730 jghali 241
 
19416 jghali 242
}
243
 
19929 jghali 244
UndoState* TransactionState::at(int index) const
19416 jghali 245
{
246
        if (index >= 0 && static_cast<uint>(index) < sizet())
22694 jghali 247
                return m_states[index];
22601 craig 248
        return nullptr;
19416 jghali 249
}
250
 
19929 jghali 251
UndoState* TransactionState::last() const
252
{
22694 jghali 253
        if (!m_states.empty())
254
                return m_states.at(m_size - 1);
22601 craig 255
        return nullptr;
19929 jghali 256
}
257
 
19416 jghali 258
bool TransactionState::contains(int uid) const
259
{
22694 jghali 260
        for (size_t i = 0; i < m_states.size(); ++i)
19416 jghali 261
        {
22694 jghali 262
                UndoObject* undoObject = m_states[i]->undoObject();
19416 jghali 263
                if (undoObject && undoObject->getUId() == static_cast<uint>(uid))
264
                        return true;
265
        }
266
        return false;
267
}
268
 
269
bool TransactionState::containsOnly(int uid) const
270
{
22694 jghali 271
        for (size_t i = 0; i < m_states.size(); ++i)
19416 jghali 272
        {
22694 jghali 273
                UndoObject* undoObject = m_states[i]->undoObject();
19416 jghali 274
                if (undoObject && undoObject->getUId() != static_cast<uint>(uid))
275
                        return false;
276
        }
277
        return true;
278
}
279
 
280
void TransactionState::pushBack(UndoObject *target, UndoState *state)
281
{
282
        if (target && state)
283
        {
284
                state->setUndoObject(target);
22694 jghali 285
                m_states.push_back(state);
286
                ++m_size;
19416 jghali 287
        }
288
}
289
 
19929 jghali 290
uint TransactionState::sizet() const
19416 jghali 291
{
22694 jghali 292
        return m_size;
19416 jghali 293
}
294
 
295
void TransactionState::useActionName()
296
{
22694 jghali 297
        if (m_size > 0)
298
                setName(m_states[m_size - 1]->getName());
19416 jghali 299
}
300
 
301
UndoObject* TransactionState::replace(ulong uid, UndoObject *newUndoObject)
302
{
22601 craig 303
        UndoObject *tmp = nullptr;
22694 jghali 304
        for (size_t i = 0; i < m_states.size(); ++i)
19416 jghali 305
        {
22694 jghali 306
                TransactionState *ts = dynamic_cast<TransactionState*>(m_states[i]);
19416 jghali 307
                if (ts) // are we having a transaction_inside a transaction
308
                        ts->replace(uid, newUndoObject);
22694 jghali 309
                else if (m_states[i]->undoObject() && m_states[i]->undoObject()->getUId() == uid)
19416 jghali 310
                {
22694 jghali 311
                        tmp = m_states[i]->undoObject();
312
                        m_states[i]->setUndoObject(newUndoObject);
19416 jghali 313
                }
314
        }
315
        return tmp;
316
}
317
 
318
void TransactionState::undo() // undo all attached states
319
{
320
        for (int i = sizet() - 1; i > -1; --i)
321
        {
322
                if ((sizet() - 1) == 0)
323
                        at(i)->transactionCode = 0;
324
                else
325
                {
326
                        if (i == static_cast<int>(sizet() - 1))
327
                                at(i)->transactionCode = 1;
328
                        else if (i == 0)
329
                                at(i)->transactionCode = 2;
330
                        else
331
                                at(i)->transactionCode = 3;
332
                }
333
                if (transactionCode != 0)
334
                        at(i)->transactionCode = transactionCode;
335
                at(i)->undo();
336
        }
337
}
338
 
339
void TransactionState::redo() // redo all attached states
340
{
341
        for (uint i = 0; i < sizet(); ++i)
342
        {
343
                if ((sizet() - 1) == 0)
344
                        at(i)->transactionCode = 0;
345
                else
346
                {
347
                        if (i == 0)
348
                                at(i)->transactionCode = 1;
349
                        else if (i == static_cast<uint>(sizet() - 1))
350
                                at(i)->transactionCode = 2;
351
                        else
352
                                at(i)->transactionCode = 3;
353
                }
354
                if (transactionCode != 0)
355
                        at(i)->transactionCode = transactionCode;
356
                at(i)->redo();
357
        }
358
}
359
 
360
TransactionState::~TransactionState()
361
{
22694 jghali 362
        for (size_t i = 0; i < m_states.size(); ++i)
19416 jghali 363
        {
22694 jghali 364
                if (m_states[i])
19416 jghali 365
                {
22694 jghali 366
                        delete m_states[i];
367
                        m_states[i] = nullptr;
19416 jghali 368
                }
369
        }
370
}
23017 craig 371
 
372
void* ScItemsState::getItem(const QString& itemname) const
373
{
374
        if (pointerMap.contains(itemname))
375
                return pointerMap.value(itemname, nullptr);
376
        return nullptr;
377
}