1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Rosegarden
A MIDI and audio sequencer and musical notation editor.
This program is Copyright 2000-2008
Guillaume Laurent <glaurent@telegraph-road.org>,
Chris Cannam <cannam@all-day-breakfast.com>,
Richard Bown <richard.bown@ferventsoftware.com>
The moral rights of Guillaume Laurent, Chris Cannam, and Richard
Bown to claim authorship of this work have been asserted.
Other copyrights also apply to some parts of this work. Please
see the AUTHORS file and individual file headers for details.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "MultiViewCommandHistory.h"
#include <klocale.h>
#include "misc/Debug.h"
#include <kactioncollection.h>
#include <kaction.h>
#include <kcommand.h>
#include <kstdaction.h>
#include <tqobject.h>
#include <tqpopupmenu.h>
#include <tqregexp.h>
#include <tqstring.h>
#include <kpopupmenu.h>
namespace Rosegarden
{
MultiViewCommandHistory::MultiViewCommandHistory() :
m_undoLimit(50),
m_redoLimit(50),
m_savedAt(0)
{
// nothing
}
MultiViewCommandHistory::~MultiViewCommandHistory()
{
m_savedAt = -1;
clearStack(m_undoStack);
clearStack(m_redoStack);
}
void
MultiViewCommandHistory::clear()
{
m_savedAt = -1;
clearStack(m_undoStack);
clearStack(m_redoStack);
}
void
MultiViewCommandHistory::attachView(KActionCollection *collection)
{
if (m_views.find(collection) != m_views.end())
return ;
RG_DEBUG << "MultiViewCommandHistory::attachView() : setting up undo/redo actions\n";
KToolBarPopupAction *undo = dynamic_cast<KToolBarPopupAction*>(collection->action(KStdAction::stdName(KStdAction::Undo)));
if (undo) {
connect(undo, TQT_SIGNAL(activated()),
this, TQT_SLOT(slotUndo()));
connect
(undo->popupMenu(),
TQT_SIGNAL(aboutToShow()),
this,
TQT_SLOT(slotUndoAboutToShow()));
connect
(undo->popupMenu(),
TQT_SIGNAL(activated(int)),
this,
TQT_SLOT(slotUndoActivated(int)));
}
KToolBarPopupAction *redo = dynamic_cast<KToolBarPopupAction*>(collection->action(KStdAction::stdName(KStdAction::Redo)));
if (redo) {
connect(redo, TQT_SIGNAL(activated()),
this, TQT_SLOT(slotRedo()));
connect
(redo->popupMenu(),
TQT_SIGNAL(aboutToShow()),
this,
TQT_SLOT(slotRedoAboutToShow()));
connect
(redo->popupMenu(),
TQT_SIGNAL(activated(int)),
this,
TQT_SLOT(slotRedoActivated(int)));
}
m_views.insert(collection);
updateButtons();
}
void
MultiViewCommandHistory::detachView(KActionCollection *collection)
{
ViewSet::iterator i = m_views.find(collection);
if (i != m_views.end())
m_views.erase(collection);
}
void
MultiViewCommandHistory::addCommand(KCommand *command, bool execute)
{
if (!command)
return ;
RG_DEBUG << "MultiViewCommandHistory::addCommand: " << command->name() << endl;
// We can't redo after adding a command
clearStack(m_redoStack);
// can we reach savedAt?
if ((int)m_undoStack.size() < m_savedAt)
m_savedAt = -1; // nope
m_undoStack.push(command);
clipCommands();
if (execute) {
command->execute();
emit commandExecuted();
emit commandExecuted(command);
}
updateButtons();
}
void
MultiViewCommandHistory::slotUndo()
{
if (m_undoStack.empty())
return ;
KCommand *command = m_undoStack.top();
command->unexecute();
emit commandExecuted();
emit commandExecuted(command);
m_redoStack.push(command);
m_undoStack.pop();
clipCommands();
updateButtons();
if ((int)m_undoStack.size() == m_savedAt)
emit documentRestored();
}
void
MultiViewCommandHistory::slotRedo()
{
if (m_redoStack.empty())
return ;
KCommand *command = m_redoStack.top();
command->execute();
emit commandExecuted();
emit commandExecuted(command);
m_undoStack.push(command);
m_redoStack.pop();
// no need to clip
updateButtons();
}
void
MultiViewCommandHistory::setUndoLimit(int limit)
{
if (limit > 0 && limit != m_undoLimit) {
m_undoLimit = limit;
clipCommands();
}
}
void
MultiViewCommandHistory::setRedoLimit(int limit)
{
if (limit > 0 && limit != m_redoLimit) {
m_redoLimit = limit;
clipCommands();
}
}
void
MultiViewCommandHistory::documentSaved()
{
m_savedAt = m_undoStack.size();
}
void
MultiViewCommandHistory::clipCommands()
{
if ((int)m_undoStack.size() > m_undoLimit) {
m_savedAt -= (m_undoStack.size() - m_undoLimit);
}
clipStack(m_undoStack, m_undoLimit);
clipStack(m_redoStack, m_redoLimit);
}
void
MultiViewCommandHistory::clipStack(CommandStack &stack, int limit)
{
int i;
if ((int)stack.size() > limit) {
CommandStack tempStack;
for (i = 0; i < limit; ++i) {
KCommand *togo = stack.top();
KNamedCommand *named = dynamic_cast<KNamedCommand *>(togo);
if (named) {
RG_DEBUG << "MVCH::clipStack: Saving recent command: " << named->name() << " at " << togo << endl;
} else {
RG_DEBUG << "MVCH::clipStack: Saving recent unnamed command" << " at " << togo << endl;
}
tempStack.push(stack.top());
stack.pop();
}
clearStack(stack);
for (i = 0; i < m_undoLimit; ++i) {
stack.push(tempStack.top());
tempStack.pop();
}
}
}
void
MultiViewCommandHistory::clearStack(CommandStack &stack)
{
while (!stack.empty()) {
KCommand *togo = stack.top();
KNamedCommand *named = dynamic_cast<KNamedCommand *>(togo);
if (named) {
RG_DEBUG << "MVCH::clearStack: About to delete command: " << named->name() << " at " << togo << endl;
} else {
RG_DEBUG << "MVCH::clearStack: About to delete unnamed command" << " at " << togo << endl;
}
delete togo;
stack.pop();
}
}
void
MultiViewCommandHistory::slotUndoActivated(int pos)
{
for (int i = 0 ; i <= pos; ++i)
slotUndo();
}
void
MultiViewCommandHistory::slotRedoActivated(int pos)
{
for (int i = 0 ; i <= pos; ++i)
slotRedo();
}
void
MultiViewCommandHistory::slotUndoAboutToShow()
{
updateMenu(true, KStdAction::stdName(KStdAction::Undo), m_undoStack);
}
void
MultiViewCommandHistory::slotRedoAboutToShow()
{
updateMenu(false, KStdAction::stdName(KStdAction::Redo), m_redoStack);
}
void
MultiViewCommandHistory::updateButtons()
{
updateButton(true, KStdAction::stdName(KStdAction::Undo), m_undoStack);
updateButton(false, KStdAction::stdName(KStdAction::Redo), m_redoStack);
}
void
MultiViewCommandHistory::updateButton(bool undo,
const TQString &name,
CommandStack &stack)
{
for (ViewSet::iterator i = m_views.begin(); i != m_views.end(); ++i) {
KAction *action = (*i)->action(name);
if (!action)
continue;
TQString text;
if (stack.empty()) {
action->setEnabled(false);
if (undo)
text = i18n("Nothing to undo");
else
text = i18n("Nothing to redo");
action->setText(text);
} else {
action->setEnabled(true);
TQString commandName = stack.top()->name();
commandName.replace(TQRegExp("&"), "");
commandName.replace(TQRegExp("\\.\\.\\.$"), "");
if (undo)
text = i18n("Und&o %1").tqarg(commandName);
else
text = i18n("Re&do %1").tqarg(commandName);
action->setText(text);
}
}
}
void
MultiViewCommandHistory::updateMenu(bool undo,
const TQString &name,
CommandStack &stack)
{
for (ViewSet::iterator i = m_views.begin(); i != m_views.end(); ++i) {
KAction *action = (*i)->action(name);
if (!action)
continue;
KToolBarPopupAction *popupAction =
dynamic_cast<KToolBarPopupAction *>(action);
if (!popupAction)
continue;
TQPopupMenu *menu = popupAction->popupMenu();
if (!menu)
continue;
menu->clear();
CommandStack tempStack;
int j = 0;
while (j < 10 && !stack.empty()) {
KCommand *command = stack.top();
tempStack.push(command);
stack.pop();
TQString commandName = command->name();
commandName.replace(TQRegExp("&"), "");
commandName.replace(TQRegExp("\\.\\.\\.$"), "");
TQString text;
if (undo)
text = i18n("Und&o %1").tqarg(commandName);
else
text = i18n("Re&do %1").tqarg(commandName);
menu->insertItem(text, j++);
}
while (!tempStack.empty()) {
stack.push(tempStack.top());
tempStack.pop();
}
}
}
}
#include "MultiViewCommandHistory.moc"
|