Scribus
Open source desktop publishing at your fingertips
undostate.h
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 */
7 /***************************************************************************
8  * Copyright (C) 2005 by Riku Leino *
9  * riku@scribus.info *
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., *
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
25  ***************************************************************************/
26 
27 #ifndef UNDOSTATE_H
28 #define UNDOSTATE_H
29 
30 #include <vector>
31 
32 #include <QMap>
33 #include <QPair>
34 #include <QPixmap>
35 #include <QVariant>
36 #include <QList>
37 
38 #include "scribusapi.h"
39 #include "undoobject.h"
40 
41 class QString;
42 class PageItem;
43 
59 class SCRIBUS_API UndoState
60 {
61 public:
69  UndoState(const QString& name, const QString& description = 0, QPixmap* pixmap = 0);
70 
71  virtual ~UndoState();
72 
77  virtual QString getName();
78 
83  virtual void setName(const QString &newName);
84 
89  virtual QString getDescription();
90 
95  virtual void setDescription(const QString &newDescription);
96 
101  virtual QPixmap* getPixmap();
102 
107  virtual void setPixmap(QPixmap *newPixmap);
108 
111  virtual void undo();
114  virtual void redo();
116  virtual bool isTransaction(){ return false;};
118  virtual void setUndoObject(UndoObject *object);
120  virtual UndoObject* undoObject();
121  int transactionCode;
122 
123 private:
125  QString actionName_;
127  QString actionDescription_;
129  QPixmap *actionPixmap_;
131  UndoObjectPtr undoObject_;
132 };
133 
134 /*** SimpleState **************************************************************************/
135 
145 class SCRIBUS_API SimpleState : public UndoState
146 {
147 public:
155  SimpleState(const QString& name, const QString& description = 0, QPixmap* pixmap = 0);
156 
157  virtual ~SimpleState();
158 
164  bool contains(const QString& key);
165 
178  QString get(const QString& key, const QString& def = "");
179 
195  int getInt(const QString& key, int def = 0);
196 
212  uint getUInt(const QString& key, uint def = 0);
213 
228  double getDouble(const QString& key, double def = 0.0);
229 
244  bool getBool(const QString& key, bool def = false);
245 
251  void set(const QString& key, const QString& value);
252 
258  void set(const QString& key, int value);
259 
265  void set(const QString& key, uint value);
266 
272  void set(const QString& key, double value);
273 
279  void set(const QString& key, bool value);
280 
281 private:
283  QMap<QString, QVariant> values_;
284 
285  QVariant variant(const QString& key, const QVariant& def);
286 };
287 
288 /*** ItemState ***************************************************************************/
289 
290 template<class C>
291 class ScItemState : public SimpleState
292 {
293 public:
294  ScItemState(const QString& name, const QString& description = 0, QPixmap* pixmap = 0)
295  : SimpleState(name, description, pixmap) {}
296  ~ScItemState() {}
297  void setItem(const C &c) { item_ = c; }
298  C getItem() const { return item_; }
299 private:
300  C item_;
301 };
302 
303 /**** ItemsState for list of pointers to items *****/
304 //template<class C>
305 class ScItemsState : public SimpleState
306 {
307 public:
308  ScItemsState(const QString& name, const QString& description = 0, QPixmap* pixmap = 0)
309  : SimpleState(name, description, pixmap) {}
310  ~ScItemsState() {}
311  void insertItem(QString itemname, void * item) { pointerMap.insert(itemname, item); }
312  void* getItem(QString itemname) const { if (pointerMap.contains(itemname)) return pointerMap.value(itemname); else return NULL;}
313  QList< QPair<void*, int> > insertItemPos;
314 private:
315  QMap<QString,void*> pointerMap;
316 };
317 
318 /*** TransactionState ********************************************************************/
319 
327 {
328 public:
334  bool isTransaction(){return true;}
335 
340  void pushBack(UndoObject *target, UndoState *state);
345  uint sizet() const;
349  void useActionName();
357  UndoState* at(int index) const;
362  UndoState* last() const;
369  bool contains(int uid) const;
370 
380  bool containsOnly(int uid) const;
387  UndoObject* replace(ulong uid, UndoObject *newUndoObject);
388 
390  void undo();
392  void redo();
393 private:
395  uint size_;
397  std::vector<UndoState*> states_;
398 };
399 
400 #endif
void pushBack(UndoObject *target, UndoState *state)
Add a new UndoState object to the transaction.
Definition: undostate.cpp:245
bool contains(int uid) const
Returns true if this transaction contains UndoObject with the id uid
Definition: undostate.cpp:223
SimpleState(const QString &name, const QString &description=0, QPixmap *pixmap=0)
Creates a new SimpleState instance.
Definition: undostate.cpp:99
bool isTransaction()
To know if the state is a Transaction.
Definition: undostate.h:334
~TransactionState()
Destroys the TransactionState instance.
Definition: undostate.cpp:325
UndoState describes an undoable state (action).
Definition: undostate.h:59
Definition: undostate.h:305
virtual bool isTransaction()
To know if the state is a transaction.
Definition: undostate.h:116
UndoState * last() const
Returns the last UndoState in transaction.
Definition: undostate.cpp:216
UndoObject * replace(ulong uid, UndoObject *newUndoObject)
Replace object with id uid with new UndoObject newUndoObject.
Definition: undostate.cpp:266
TransactionState provides a container where multiple UndoStates can be stored.
Definition: undostate.h:326
void undo()
undo all UndoStates in this transaction
Definition: undostate.cpp:283
Definition: undostate.h:291
TransactionState()
Creates a new TransactionState instance.
Definition: undostate.cpp:203
Superclass for all objects that are wanted to have undoable actions.
Definition: undoobject.h:59
bool containsOnly(int uid) const
Tells if this transaction contains only UndoObject with ID uid.
Definition: undostate.cpp:234
void redo()
redo all UndoStates in this transaction
Definition: undostate.cpp:304
Definition: pageitem.h:92
UndoState * at(int index) const
Returns an UndoState object at index.
Definition: undostate.cpp:208
uint sizet() const
Returns the count of the UndoState objects in this transaction.
Definition: undostate.cpp:255
void useActionName()
Use the name from last action added to this TransactionState
Definition: undostate.cpp:260
SimpleState provides a simple implementation of the UndoState.
Definition: undostate.h:145