2 * undo.h - undo/redo facility
4 * Copyright © 2005-2011 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 /** @file undo.h - undo/redo facility */
26 #include "autodeletelist.h"
28 #include <kalarmcal/kaevent.h>
31 #include <akonadi/collection.h>
34 #include <QStringList>
40 using namespace KAlarmCal
;
43 class Undo
: public QObject
47 enum Type
{ NONE
, UNDO
, REDO
};
48 // N.B. The Event structure must be constructed before the action for
49 // which the undo is being created is carried out, since the
50 // don't-show-errors status is not contained within the KAEvent itself.
55 Event(const KAEvent
&, Akonadi::Collection
&);
57 Event(const KAEvent
&, AlarmResource
*);
61 mutable Akonadi::Collection collection
;
63 AlarmResource
* resource
;
65 QStringList dontShowErrors
;
67 class EventList
: public QList
<Event
>
71 void append(const KAEvent
& e
, Akonadi::Collection
& c
) { QList
<Event
>::append(Event(e
, c
)); }
73 void append(const KAEvent
& e
, AlarmResource
* r
) { QList
<Event
>::append(Event(e
, r
)); }
77 static Undo
* instance();
79 static void saveAdd(const KAEvent
&, Akonadi::Collection
&, const QString
& name
= QString());
81 static void saveAdd(const KAEvent
&, AlarmResource
*, const QString
& name
= QString());
83 static void saveAdds(const EventList
&, const QString
& name
= QString());
84 static void saveEdit(const Event
& oldEvent
, const KAEvent
& newEvent
);
85 static void saveDelete(const Event
&, const QString
& name
= QString());
86 static void saveDeletes(const EventList
&, const QString
& name
= QString());
88 static void saveReactivate(const KAEvent
&, Akonadi::Collection
&, const QString
& name
= QString());
90 static void saveReactivate(const KAEvent
&, AlarmResource
*, const QString
& name
= QString());
92 static void saveReactivates(const EventList
&, const QString
& name
= QString());
93 static bool undo(QWidget
* parent
, const QString
& action
)
94 { return undo(0, UNDO
, parent
, action
); }
95 static bool undo(int id
, QWidget
* parent
, const QString
& action
)
96 { return undo(findItem(id
, UNDO
), UNDO
, parent
, action
); }
97 static bool redo(QWidget
* parent
, const QString
& action
)
98 { return undo(0, REDO
, parent
, action
); }
99 static bool redo(int id
, QWidget
* parent
, const QString
& action
)
100 { return undo(findItem(id
, REDO
), REDO
, parent
, action
); }
102 static bool haveUndo() { return !mUndoList
.isEmpty(); }
103 static bool haveRedo() { return !mRedoList
.isEmpty(); }
104 static QString
actionText(Type
);
105 static QString
actionText(Type
, int id
);
106 static QString
description(Type
, int id
);
107 static QList
<int> ids(Type
);
108 static void emitChanged();
110 // Types for use by UndoItem class and its descendants
111 typedef AutoDeleteList
<UndoItem
> List
;
114 void changed(const QString
& undo
, const QString
& redo
);
117 // Methods for use by UndoItem class
118 static void add(UndoItem
*, bool undo
);
119 static void remove(UndoItem
*, bool undo
);
120 static void replace(UndoItem
* old
, UndoItem
* New
);
123 Undo(QObject
* parent
) : QObject(parent
) { }
124 static void removeRedos(const QString
& eventID
);
125 static bool undo(int index
, Type
, QWidget
* parent
, const QString
& action
);
126 static UndoItem
* getItem(int id
, Type
);
127 static int findItem(int id
, Type
);
128 void emitChanged(const QString
& undo
, const QString
& redo
)
129 { emit
changed(undo
, redo
); }
131 static Undo
* mInstance
; // the one and only Undo instance
132 static List mUndoList
; // edit history for undo, latest undo first
133 static List mRedoList
; // edit history for redo, latest redo first
135 friend class UndoItem
;