annotate base/CommandHistory.cpp @ 44:701404725897

* Use commands for add/delete pane in main window * Add compound command collection to command history (for add pane, import file etc) * Add hide/show pane and hidden pane list to PaneStack * Various other fixes
author Chris Cannam
date Mon, 13 Mar 2006 17:55:19 +0000
parents 2b6412c1e724
children 5364a9d338a2
rev   line source
Chris@16 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
Chris@16 2
Chris@16 3 /*
Chris@16 4 A waveform viewer and audio annotation editor.
Chris@16 5 Chris Cannam, Queen Mary University of London, 2005-2006
Chris@16 6
Chris@16 7 This is experimental software. Not for distribution.
Chris@16 8 */
Chris@16 9
Chris@16 10 /*
Chris@16 11 This is a modified version of a source file from the Rosegarden
Chris@16 12 MIDI and audio sequencer and notation editor, copyright 2000-2006
Chris@16 13 Chris Cannam, distributed under the GNU General Public License.
Chris@16 14
Chris@16 15 This file contains traces of the KCommandHistory class from the KDE
Chris@16 16 project, copyright 2000 Werner Trobin and David Faure and
Chris@16 17 distributed under the GNU Lesser General Public License.
Chris@16 18 */
Chris@16 19
Chris@17 20 #include "CommandHistory.h"
Chris@16 21
Chris@16 22 #include "Command.h"
Chris@16 23
Chris@16 24 #include <QRegExp>
Chris@16 25 #include <QMenu>
Chris@16 26 #include <QToolBar>
Chris@16 27 #include <QString>
Chris@16 28
Chris@16 29 #include <iostream>
Chris@16 30
Chris@17 31 CommandHistory *CommandHistory::m_instance = 0;
Chris@17 32
Chris@17 33 CommandHistory::CommandHistory() :
Chris@16 34 m_undoLimit(50),
Chris@16 35 m_redoLimit(50),
Chris@44 36 m_savedAt(0),
Chris@44 37 m_currentMacro(0),
Chris@44 38 m_executeMacro(false)
Chris@16 39 {
Chris@16 40 m_undoAction = new QAction(QIcon(":/icons/undo.png"), tr("&Undo"), this);
Chris@16 41 m_undoAction->setShortcut(tr("Ctrl+Z"));
Chris@16 42 connect(m_undoAction, SIGNAL(triggered()), this, SLOT(undo()));
Chris@16 43
Chris@17 44 m_undoMenuAction = new QAction(QIcon(":/icons/undo.png"), tr("&Undo"), this);
Chris@17 45 connect(m_undoMenuAction, SIGNAL(triggered()), this, SLOT(undo()));
Chris@17 46
Chris@16 47 m_undoMenu = new QMenu(tr("&Undo"));
Chris@17 48 m_undoMenuAction->setMenu(m_undoMenu);
Chris@16 49 connect(m_undoMenu, SIGNAL(triggered(QAction *)),
Chris@16 50 this, SLOT(undoActivated(QAction*)));
Chris@16 51
Chris@17 52 m_redoAction = new QAction(QIcon(":/icons/redo.png"), tr("Re&do"), this);
Chris@16 53 m_redoAction->setShortcut(tr("Ctrl+Shift+Z"));
Chris@16 54 connect(m_redoAction, SIGNAL(triggered()), this, SLOT(redo()));
Chris@17 55
Chris@17 56 m_redoMenuAction = new QAction(QIcon(":/icons/redo.png"), tr("Re&do"), this);
Chris@17 57 connect(m_redoMenuAction, SIGNAL(triggered()), this, SLOT(redo()));
Chris@16 58
Chris@16 59 m_redoMenu = new QMenu(tr("Re&do"));
Chris@17 60 m_redoMenuAction->setMenu(m_redoMenu);
Chris@16 61 connect(m_redoMenu, SIGNAL(triggered(QAction *)),
Chris@16 62 this, SLOT(redoActivated(QAction*)));
Chris@16 63 }
Chris@16 64
Chris@17 65 CommandHistory::~CommandHistory()
Chris@16 66 {
Chris@16 67 m_savedAt = -1;
Chris@16 68 clearStack(m_undoStack);
Chris@16 69 clearStack(m_redoStack);
Chris@16 70
Chris@16 71 delete m_undoMenu;
Chris@16 72 delete m_redoMenu;
Chris@16 73 }
Chris@16 74
Chris@17 75 CommandHistory *
Chris@17 76 CommandHistory::getInstance()
Chris@17 77 {
Chris@17 78 if (!m_instance) m_instance = new CommandHistory();
Chris@17 79 return m_instance;
Chris@17 80 }
Chris@17 81
Chris@16 82 void
Chris@17 83 CommandHistory::clear()
Chris@16 84 {
Chris@16 85 m_savedAt = -1;
Chris@16 86 clearStack(m_undoStack);
Chris@16 87 clearStack(m_redoStack);
Chris@16 88 updateActions();
Chris@16 89 }
Chris@16 90
Chris@16 91 void
Chris@17 92 CommandHistory::registerMenu(QMenu *menu)
Chris@16 93 {
Chris@16 94 menu->addAction(m_undoAction);
Chris@16 95 menu->addAction(m_redoAction);
Chris@16 96 }
Chris@16 97
Chris@16 98 void
Chris@17 99 CommandHistory::registerToolbar(QToolBar *toolbar)
Chris@16 100 {
Chris@17 101 toolbar->addAction(m_undoMenuAction);
Chris@17 102 toolbar->addAction(m_redoMenuAction);
Chris@16 103 }
Chris@16 104
Chris@16 105 void
Chris@17 106 CommandHistory::addCommand(Command *command, bool execute)
Chris@16 107 {
Chris@16 108 if (!command) return;
Chris@16 109
Chris@44 110 if (m_currentMacro) {
Chris@44 111 addToMacro(command);
Chris@44 112 return;
Chris@44 113 }
Chris@44 114
Chris@17 115 std::cerr << "MVCH::addCommand: " << command->getName().toLocal8Bit().data() << std::endl;
Chris@16 116
Chris@16 117 // We can't redo after adding a command
Chris@16 118 clearStack(m_redoStack);
Chris@16 119
Chris@16 120 // can we reach savedAt?
Chris@16 121 if ((int)m_undoStack.size() < m_savedAt) m_savedAt = -1; // nope
Chris@16 122
Chris@16 123 m_undoStack.push(command);
Chris@16 124 clipCommands();
Chris@16 125
Chris@16 126 if (execute) {
Chris@16 127 command->execute();
Chris@16 128 }
Chris@16 129
Chris@17 130 // Emit even if we aren't executing the command, because
Chris@17 131 // someone must have executed it for this to make any sense
Chris@17 132 emit commandExecuted();
Chris@17 133 emit commandExecuted(command);
Chris@17 134
Chris@16 135 updateActions();
Chris@16 136 }
Chris@16 137
Chris@16 138 void
Chris@44 139 CommandHistory::addToMacro(Command *command)
Chris@44 140 {
Chris@44 141 std::cerr << "MVCH::addToMacro: " << command->getName().toLocal8Bit().data() << std::endl;
Chris@44 142
Chris@44 143 if (m_executeMacro) command->execute();
Chris@44 144 m_currentMacro->addCommand(command);
Chris@44 145 }
Chris@44 146
Chris@44 147 void
Chris@44 148 CommandHistory::startCompoundOperation(QString name, bool execute)
Chris@44 149 {
Chris@44 150 if (m_currentMacro) {
Chris@44 151 std::cerr << "MVCH::startCompoundOperation: ERROR: compound operation already in progress!" << std::endl;
Chris@44 152 std::cerr << "(name is " << m_currentMacro->getName().toLocal8Bit().data() << ")" << std::endl;
Chris@44 153 }
Chris@44 154
Chris@44 155 m_currentMacro = new MacroCommand(name);
Chris@44 156 m_executeMacro = execute;
Chris@44 157 }
Chris@44 158
Chris@44 159 void
Chris@44 160 CommandHistory::endCompoundOperation()
Chris@44 161 {
Chris@44 162 if (!m_currentMacro) {
Chris@44 163 std::cerr << "MVCH::endCompoundOperation: ERROR: no compound operation in progress!" << std::endl;
Chris@44 164 }
Chris@44 165
Chris@44 166 Command *toAdd = m_currentMacro;
Chris@44 167 m_currentMacro = 0;
Chris@44 168
Chris@44 169 // We don't execute the macro command here, because we have been
Chris@44 170 // executing the individual commands as we went along if
Chris@44 171 // m_executeMacro was true.
Chris@44 172 addCommand(toAdd, false);
Chris@44 173 }
Chris@44 174
Chris@44 175 void
Chris@17 176 CommandHistory::addExecutedCommand(Command *command)
Chris@17 177 {
Chris@17 178 addCommand(command, false);
Chris@17 179 }
Chris@17 180
Chris@17 181 void
Chris@17 182 CommandHistory::addCommandAndExecute(Command *command)
Chris@17 183 {
Chris@17 184 addCommand(command, true);
Chris@17 185 }
Chris@17 186
Chris@17 187 void
Chris@17 188 CommandHistory::undo()
Chris@16 189 {
Chris@16 190 if (m_undoStack.empty()) return;
Chris@16 191
Chris@16 192 Command *command = m_undoStack.top();
Chris@16 193 command->unexecute();
Chris@16 194 emit commandExecuted();
Chris@17 195 emit commandUnexecuted(command);
Chris@16 196
Chris@16 197 m_redoStack.push(command);
Chris@16 198 m_undoStack.pop();
Chris@16 199
Chris@16 200 clipCommands();
Chris@16 201 updateActions();
Chris@16 202
Chris@16 203 if ((int)m_undoStack.size() == m_savedAt) emit documentRestored();
Chris@16 204 }
Chris@16 205
Chris@16 206 void
Chris@17 207 CommandHistory::redo()
Chris@16 208 {
Chris@16 209 if (m_redoStack.empty()) return;
Chris@16 210
Chris@16 211 Command *command = m_redoStack.top();
Chris@16 212 command->execute();
Chris@16 213 emit commandExecuted();
Chris@16 214 emit commandExecuted(command);
Chris@16 215
Chris@16 216 m_undoStack.push(command);
Chris@16 217 m_redoStack.pop();
Chris@16 218 // no need to clip
Chris@16 219
Chris@16 220 updateActions();
Chris@41 221
Chris@41 222 if ((int)m_undoStack.size() == m_savedAt) emit documentRestored();
Chris@16 223 }
Chris@16 224
Chris@16 225 void
Chris@17 226 CommandHistory::setUndoLimit(int limit)
Chris@16 227 {
Chris@16 228 if (limit > 0 && limit != m_undoLimit) {
Chris@16 229 m_undoLimit = limit;
Chris@16 230 clipCommands();
Chris@16 231 }
Chris@16 232 }
Chris@16 233
Chris@16 234 void
Chris@17 235 CommandHistory::setRedoLimit(int limit)
Chris@16 236 {
Chris@16 237 if (limit > 0 && limit != m_redoLimit) {
Chris@16 238 m_redoLimit = limit;
Chris@16 239 clipCommands();
Chris@16 240 }
Chris@16 241 }
Chris@16 242
Chris@16 243 void
Chris@17 244 CommandHistory::documentSaved()
Chris@16 245 {
Chris@16 246 m_savedAt = m_undoStack.size();
Chris@16 247 }
Chris@16 248
Chris@16 249 void
Chris@17 250 CommandHistory::clipCommands()
Chris@16 251 {
Chris@16 252 if ((int)m_undoStack.size() > m_undoLimit) {
Chris@16 253 m_savedAt -= (m_undoStack.size() - m_undoLimit);
Chris@16 254 }
Chris@16 255
Chris@16 256 clipStack(m_undoStack, m_undoLimit);
Chris@16 257 clipStack(m_redoStack, m_redoLimit);
Chris@16 258 }
Chris@16 259
Chris@16 260 void
Chris@17 261 CommandHistory::clipStack(CommandStack &stack, int limit)
Chris@16 262 {
Chris@16 263 int i;
Chris@16 264
Chris@16 265 if ((int)stack.size() > limit) {
Chris@16 266
Chris@16 267 CommandStack tempStack;
Chris@16 268
Chris@16 269 for (i = 0; i < limit; ++i) {
Chris@16 270 Command *command = stack.top();
Chris@17 271 std::cerr << "MVCH::clipStack: Saving recent command: " << command->getName().toLocal8Bit().data() << " at " << command << std::endl;
Chris@16 272 tempStack.push(stack.top());
Chris@16 273 stack.pop();
Chris@16 274 }
Chris@16 275
Chris@16 276 clearStack(stack);
Chris@16 277
Chris@16 278 for (i = 0; i < m_undoLimit; ++i) {
Chris@16 279 stack.push(tempStack.top());
Chris@16 280 tempStack.pop();
Chris@16 281 }
Chris@16 282 }
Chris@16 283 }
Chris@16 284
Chris@16 285 void
Chris@17 286 CommandHistory::clearStack(CommandStack &stack)
Chris@16 287 {
Chris@16 288 while (!stack.empty()) {
Chris@16 289 Command *command = stack.top();
Chris@17 290 std::cerr << "MVCH::clearStack: About to delete command: " << command->getName().toLocal8Bit().data() << " at " << command << std::endl;
Chris@16 291 delete command;
Chris@16 292 stack.pop();
Chris@16 293 }
Chris@16 294 }
Chris@16 295
Chris@16 296 void
Chris@17 297 CommandHistory::undoActivated(QAction *action)
Chris@16 298 {
Chris@16 299 int pos = m_actionCounts[action];
Chris@16 300 for (int i = 0; i <= pos; ++i) {
Chris@16 301 undo();
Chris@16 302 }
Chris@16 303 }
Chris@16 304
Chris@16 305 void
Chris@17 306 CommandHistory::redoActivated(QAction *action)
Chris@16 307 {
Chris@16 308 int pos = m_actionCounts[action];
Chris@16 309 for (int i = 0; i <= pos; ++i) {
Chris@16 310 redo();
Chris@16 311 }
Chris@16 312 }
Chris@16 313
Chris@16 314 void
Chris@17 315 CommandHistory::updateActions()
Chris@16 316 {
Chris@16 317 m_actionCounts.clear();
Chris@16 318
Chris@16 319 for (int undo = 0; undo <= 1; ++undo) {
Chris@16 320
Chris@17 321 QAction *action(undo ? m_undoAction : m_redoAction);
Chris@17 322 QAction *menuAction(undo ? m_undoMenuAction : m_redoMenuAction);
Chris@16 323 QMenu *menu(undo ? m_undoMenu : m_redoMenu);
Chris@16 324 CommandStack &stack(undo ? m_undoStack : m_redoStack);
Chris@16 325
Chris@17 326 if (stack.empty()) {
Chris@17 327
Chris@17 328 QString text(undo ? tr("Nothing to undo") : tr("Nothing to redo"));
Chris@17 329
Chris@17 330 action->setEnabled(false);
Chris@17 331 action->setText(text);
Chris@17 332
Chris@17 333 menuAction->setEnabled(false);
Chris@17 334 menuAction->setText(text);
Chris@17 335
Chris@17 336 } else {
Chris@17 337
Chris@17 338 action->setEnabled(true);
Chris@17 339 menuAction->setEnabled(true);
Chris@17 340
Chris@17 341 QString commandName = stack.top()->getName();
Chris@17 342 commandName.replace(QRegExp("&"), "");
Chris@17 343
Chris@17 344 QString text = (undo ? tr("&Undo %1") : tr("Re&do %1"))
Chris@17 345 .arg(commandName);
Chris@17 346
Chris@17 347 action->setText(text);
Chris@17 348 menuAction->setText(text);
Chris@17 349 }
Chris@17 350
Chris@16 351 menu->clear();
Chris@16 352
Chris@16 353 CommandStack tempStack;
Chris@16 354 int j = 0;
Chris@16 355
Chris@16 356 while (j < 10 && !stack.empty()) {
Chris@16 357
Chris@16 358 Command *command = stack.top();
Chris@16 359 tempStack.push(command);
Chris@16 360 stack.pop();
Chris@16 361
Chris@17 362 QString commandName = command->getName();
Chris@16 363 commandName.replace(QRegExp("&"), "");
Chris@16 364
Chris@16 365 QString text;
Chris@16 366 if (undo) text = tr("&Undo %1").arg(commandName);
Chris@16 367 else text = tr("Re&do %1").arg(commandName);
Chris@16 368
Chris@16 369 QAction *action = menu->addAction(text);
Chris@16 370 m_actionCounts[action] = j++;
Chris@16 371 }
Chris@16 372
Chris@16 373 while (!tempStack.empty()) {
Chris@16 374 stack.push(tempStack.top());
Chris@16 375 tempStack.pop();
Chris@16 376 }
Chris@16 377 }
Chris@16 378 }
Chris@16 379