]> cloud.milkyroute.net Git - dolphin.git/blob - src/undomanager.h
commited initial version of Dolphin
[dolphin.git] / src / undomanager.h
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #ifndef UNDOMANAGER_H
22 #define UNDOMANAGER_H
23
24 #include <qobject.h>
25 #include <q3valuelist.h>
26 #include <kurl.h>
27 #include <kio/jobclasses.h>
28
29 class ProgressIndicator;
30
31 /**
32 * @short Represents a file manager command which can be undone and redone.
33 *
34 * A command is specified by a type, a list of source URLs and a
35 * destination URL.
36 *
37 * Due to the fixed set of commands a file manager offers this class is
38 * a very simplified version of the classic command pattern.
39 *
40 * @see UndoManager
41 * @author Peter Penz <peter.penz@gmx.at>
42 */
43 class DolphinCommand
44 {
45 public:
46 enum Type {
47 Copy,
48 Move,
49 Link,
50 Rename,
51 Trash,
52 CreateFolder,
53 CreateFile
54 };
55
56 DolphinCommand();
57 DolphinCommand(Type type, const KUrl::List& source, const KUrl& dest);
58 ~DolphinCommand(); // non-virtual
59
60 DolphinCommand& operator = (const DolphinCommand& command);
61 Type type() const { return m_type; }
62 void setSource(const KUrl::List source) { m_source = source; }
63 const KUrl::List& source() const { return m_source; }
64 const KUrl& destination() const { return m_dest; }
65
66 private:
67 Type m_type;
68 int m_macroIndex;
69 KUrl::List m_source;
70 KUrl m_dest;
71
72 friend class UndoManager; // allow to modify m_macroIndex
73 };
74
75 /**
76 * @short Stores all file manager commands which can be undone and redone.
77 *
78 * During the undo and redo operations a progress information is
79 * shown in the status bar.
80 *
81 * @author Peter Penz <peter.penz@gmx.at>
82 */
83 class UndoManager : public QObject
84 {
85 Q_OBJECT
86
87 public:
88 static UndoManager& instance();
89
90 /**
91 * Adds the command \a command to the undo list. The command
92 * can be undone by invoking UndoManager::undo().
93 */
94 void addCommand(const DolphinCommand& command);
95
96 /**
97 * Allows to summarize several commands into one macro, which
98 * can be undo in one stop by UndoManager::undo(). Example
99 * \code
100 * UndoManager& undoMan = UndoManager.instance();
101 * undoMan.beginMacro();
102 * undoMan.addCommand(...);
103 * undoMan.addCommand(...);
104 * undoMan.addCommand(...);
105 * undoMan.endMacro();
106 * \endcode
107 * It is not allowed to do nested macro recordings.
108 */
109 void beginMacro();
110
111 /**
112 * Marks the end of a macro command. See UndoManager::beginMacro()
113 * for sample code.
114 */
115 void endMacro();
116
117 public slots:
118 /**
119 * Performs an undo operation on the last command which has
120 * been added by UndoManager::addCommand().
121 */
122 void undo();
123
124 /**
125 * Performs a redo operation on the last command where an undo
126 * operation has been applied.
127 */
128 void redo();
129
130 signals:
131 /**
132 * Is emitted if whenever the availability state
133 * of the current undo operation changes.
134 */
135 void undoAvailable(bool available);
136
137 /**
138 * Is emitted whenever the text of the current
139 * undo operation changes
140 * (e. g. from 'Undo: Delete' to 'Undo: Copy')
141 */
142 void undoTextChanged(const QString& text);
143
144 /**
145 * Is emitted if whenever the availability state
146 * of the current redo operation changes.
147 */
148 void redoAvailable(bool available);
149
150 /**
151 * Is emitted whenever the text of the current
152 * redo operation changes
153 * (e. g. from 'Redo: Delete' to 'Redo: Copy')
154 */
155 void redoTextChanged(const QString& text);
156
157 protected:
158 UndoManager();
159 virtual ~UndoManager();
160 QString commandText(const DolphinCommand& command) const;
161
162 private slots:
163 /**
164 * Slot for the percent information of the I/O slaves.
165 * Delegates the updating of the progress information
166 * to UndoManager::updateProgress().
167 */
168 void slotPercent(KIO::Job* job, unsigned long percent);
169
170 /**
171 * Updates the progress information of the statusbar
172 * by accessing the progress indicator information.
173 */
174 void updateProgress();
175
176 private:
177 bool m_recordMacro;
178 int m_historyIndex;
179 int m_macroCounter;
180 Q3ValueList<DolphinCommand> m_history;
181 ProgressIndicator* m_progressIndicator;
182
183 /**
184 * Dependent from the current history index \a m_historyIndex
185 * the number of macro commands is written to the output
186 * parameter \a macroCount. The number of steps for all macro
187 * commands is written to the output parameter \a progressCount.
188 *
189 * Per default \a macroCount is 1 and \a progressCount represents
190 * the number of operations for one command.
191 */
192 void calcStepsCount(int& macroCount,
193 int& progressCount);
194 };
195
196 #endif