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>
30 #include <AkonadiCore/collection.h>
32 #include <QStringList>
35 using namespace KAlarmCal
;
38 class Undo
: public QObject
42 enum Type
{ NONE
, UNDO
, REDO
};
43 // N.B. The Event structure must be constructed before the action for
44 // which the undo is being created is carried out, since the
45 // don't-show-errors status is not contained within the KAEvent itself.
49 Event(const KAEvent
&, const Akonadi::Collection
&);
51 mutable Akonadi::Collection collection
;
52 QStringList dontShowErrors
;
54 class EventList
: public QList
<Event
>
57 void append(const KAEvent
& e
, const Akonadi::Collection
& c
) { QList
<Event
>::append(Event(e
, c
)); }
60 static Undo
* instance();
61 static void saveAdd(const KAEvent
&, const Akonadi::Collection
&, const QString
& name
= QString());
62 static void saveAdds(const EventList
&, const QString
& name
= QString());
63 static void saveEdit(const Event
& oldEvent
, const KAEvent
& newEvent
);
64 static void saveDelete(const Event
&, const QString
& name
= QString());
65 static void saveDeletes(const EventList
&, const QString
& name
= QString());
66 static void saveReactivate(const KAEvent
&, const Akonadi::Collection
&, const QString
& name
= QString());
67 static void saveReactivates(const EventList
&, const QString
& name
= QString());
68 static bool undo(QWidget
* parent
, const QString
& action
)
69 { return undo(0, UNDO
, parent
, action
); }
70 static bool undo(int id
, QWidget
* parent
, const QString
& action
)
71 { return undo(findItem(id
, UNDO
), UNDO
, parent
, action
); }
72 static bool redo(QWidget
* parent
, const QString
& action
)
73 { return undo(0, REDO
, parent
, action
); }
74 static bool redo(int id
, QWidget
* parent
, const QString
& action
)
75 { return undo(findItem(id
, REDO
), REDO
, parent
, action
); }
77 static bool haveUndo() { return !mUndoList
.isEmpty(); }
78 static bool haveRedo() { return !mRedoList
.isEmpty(); }
79 static QString
actionText(Type
);
80 static QString
actionText(Type
, int id
);
81 static QString
description(Type
, int id
);
82 static QList
<int> ids(Type
);
83 static void emitChanged();
85 // Types for use by UndoItem class and its descendants
86 typedef AutoDeleteList
<UndoItem
> List
;
89 void changed(const QString
& undo
, const QString
& redo
);
92 // Methods for use by UndoItem class
93 static void add(UndoItem
*, bool undo
);
94 static void remove(UndoItem
*, bool undo
);
95 static void replace(UndoItem
* old
, UndoItem
* New
);
98 Undo(QObject
* parent
) : QObject(parent
) { }
99 static void removeRedos(const QString
& eventID
);
100 static bool undo(int index
, Type
, QWidget
* parent
, const QString
& action
);
101 static UndoItem
* getItem(int id
, Type
);
102 static int findItem(int id
, Type
);
103 void emitChanged(const QString
& undo
, const QString
& redo
)
104 { Q_EMIT
changed(undo
, redo
); }
106 static Undo
* mInstance
; // the one and only Undo instance
107 static List mUndoList
; // edit history for undo, latest undo first
108 static List mRedoList
; // edit history for redo, latest redo first
110 friend class UndoItem
;