comparison widgets/CommandHistory.cpp @ 377:0bcb449d15f4

* Fix #1628781 changes to layer visibility and mute should use a command * Also use a command for changes to layer playback pan, gain, plugin settings * Refactor PlayParameterRepository to remove dependency on audioio from base * Fix failure to save play parameters for main model in session file
author Chris Cannam
date Thu, 13 Mar 2008 14:06:03 +0000
parents e1a9e478b7f2
children 2c59b0cd176b
comparison
equal deleted inserted replaced
376:e1a9e478b7f2 377:0bcb449d15f4
32 #include <QString> 32 #include <QString>
33 #include <QTimer> 33 #include <QTimer>
34 #include <QAction> 34 #include <QAction>
35 35
36 #include <iostream> 36 #include <iostream>
37
38 //#define DEBUG_COMMAND_HISTORY 1
37 39
38 CommandHistory *CommandHistory::m_instance = 0; 40 CommandHistory *CommandHistory::m_instance = 0;
39 41
40 CommandHistory::CommandHistory() : 42 CommandHistory::CommandHistory() :
41 m_undoLimit(50), 43 m_undoLimit(50),
93 } 95 }
94 96
95 void 97 void
96 CommandHistory::clear() 98 CommandHistory::clear()
97 { 99 {
98 // std::cerr << "CommandHistory::clear()" << std::endl; 100 #ifdef DEBUG_COMMAND_HISTORY
101 std::cerr << "CommandHistory::clear()" << std::endl;
102 #endif
99 closeBundle(); 103 closeBundle();
100 m_savedAt = -1; 104 m_savedAt = -1;
101 clearStack(m_undoStack); 105 clearStack(m_undoStack);
102 clearStack(m_redoStack); 106 clearStack(m_redoStack);
103 updateActions(); 107 updateActions();
145 return; 149 return;
146 } else if (m_currentBundle) { 150 } else if (m_currentBundle) {
147 closeBundle(); 151 closeBundle();
148 } 152 }
149 153
150 // std::cerr << "CommandHistory::addCommand: " << command->getName().toLocal8Bit().data() << " at " << command << std::endl; 154 #ifdef DEBUG_COMMAND_HISTORY
155 std::cerr << "CommandHistory::addCommand: " << command->getName().toLocal8Bit().data() << " at " << command << std::endl;
156 if (!m_redoStack.empty()) {
157 std::cerr << "CommandHistory::clearing redo stack" << std::endl;
158 }
159 #endif
151 160
152 // We can't redo after adding a command 161 // We can't redo after adding a command
153 // std::cerr << "CommandHistory::clearing redo stack" << std::endl;
154 clearStack(m_redoStack); 162 clearStack(m_redoStack);
155 163
156 // can we reach savedAt? 164 // can we reach savedAt?
157 if ((int)m_undoStack.size() < m_savedAt) m_savedAt = -1; // nope 165 if ((int)m_undoStack.size() < m_savedAt) m_savedAt = -1; // nope
158 166
174 void 182 void
175 CommandHistory::addToBundle(Command *command, bool execute) 183 CommandHistory::addToBundle(Command *command, bool execute)
176 { 184 {
177 if (m_currentBundle) { 185 if (m_currentBundle) {
178 if (!command || (command->getName() != m_currentBundleName)) { 186 if (!command || (command->getName() != m_currentBundleName)) {
187 #ifdef DEBUG_COMMAND_HISTORY
188 std::cerr << "CommandHistory::addToBundle: "
189 << command->getName().toStdString()
190 << ": closing current bundle" << std::endl;
191 #endif
179 closeBundle(); 192 closeBundle();
180 } 193 }
181 } 194 }
182 195
183 if (!command) return; 196 if (!command) return;
184 197
185 if (!m_currentBundle) { 198 if (!m_currentBundle) {
199
200 #ifdef DEBUG_COMMAND_HISTORY
201 std::cerr << "CommandHistory::addToBundle: "
202 << command->getName().toStdString()
203 << ": creating new bundle" << std::endl;
204 #endif
205
186 // need to addCommand before setting m_currentBundle, as addCommand 206 // need to addCommand before setting m_currentBundle, as addCommand
187 // with bundle false will reset m_currentBundle to 0 207 // with bundle false will reset m_currentBundle to 0
188 MacroCommand *mc = new MacroCommand(command->getName()); 208 MacroCommand *mc = new MacroCommand(command->getName());
189 addCommand(mc, false); 209 addCommand(mc, false);
190 m_currentBundle = mc; 210 m_currentBundle = mc;
191 m_currentBundleName = command->getName(); 211 m_currentBundleName = command->getName();
192 } 212 }
193 213
214 #ifdef DEBUG_COMMAND_HISTORY
215 std::cerr << "CommandHistory::addToBundle: "
216 << command->getName().toStdString()
217 << ": adding to bundle" << std::endl;
218 #endif
219
194 if (execute) command->execute(); 220 if (execute) command->execute();
195 m_currentBundle->addCommand(command); 221 m_currentBundle->addCommand(command);
222
223 // Emit even if we aren't executing the command, because
224 // someone must have executed it for this to make any sense
225 emit commandExecuted();
226 emit commandExecuted(command);
227
228 updateActions();
196 229
197 delete m_bundleTimer; 230 delete m_bundleTimer;
198 m_bundleTimer = new QTimer(this); 231 m_bundleTimer = new QTimer(this);
199 connect(m_bundleTimer, SIGNAL(timeout()), this, SLOT(bundleTimerTimeout())); 232 connect(m_bundleTimer, SIGNAL(timeout()), this, SLOT(bundleTimerTimeout()));
200 m_bundleTimer->start(m_bundleTimeout); 233 m_bundleTimer->start(m_bundleTimeout);
201 } 234 }
202 235
203 void 236 void
204 CommandHistory::closeBundle() 237 CommandHistory::closeBundle()
205 { 238 {
239 #ifdef DEBUG_COMMAND_HISTORY
240 std::cerr << "CommandHistory::closeBundle" << std::endl;
241 #endif
242
206 m_currentBundle = 0; 243 m_currentBundle = 0;
207 m_currentBundleName = ""; 244 m_currentBundleName = "";
208 } 245 }
209 246
210 void 247 void
211 CommandHistory::bundleTimerTimeout() 248 CommandHistory::bundleTimerTimeout()
212 { 249 {
250 #ifdef DEBUG_COMMAND_HISTORY
251 std::cerr << "CommandHistory::bundleTimerTimeout: bundle is " << m_currentBundle << std::endl;
252 #endif
253
213 closeBundle(); 254 closeBundle();
214 } 255 }
215 256
216 void 257 void
217 CommandHistory::addToCompound(Command *command, bool execute) 258 CommandHistory::addToCompound(Command *command, bool execute)
218 { 259 {
219 // std::cerr << "CommandHistory::addToCompound: " << command->getName().toLocal8Bit().data() << std::endl; 260 #ifdef DEBUG_COMMAND_HISTORY
261 std::cerr << "CommandHistory::addToCompound: " << command->getName().toLocal8Bit().data() << std::endl;
262 #endif
220 if (!m_currentCompound) { 263 if (!m_currentCompound) {
221 std::cerr << "CommandHistory::addToCompound: ERROR: no compound operation in progress!" << std::endl; 264 std::cerr << "CommandHistory::addToCompound: ERROR: no compound operation in progress!" << std::endl;
222 return; 265 return;
223 } 266 }
224 267
276 void 319 void
277 CommandHistory::undo() 320 CommandHistory::undo()
278 { 321 {
279 if (m_undoStack.empty()) return; 322 if (m_undoStack.empty()) return;
280 323
324 #ifdef DEBUG_COMMAND_HISTORY
325 std::cerr << "CommandHistory::undo()" << std::endl;
326 #endif
327
281 closeBundle(); 328 closeBundle();
282 329
283 Command *command = m_undoStack.top(); 330 Command *command = m_undoStack.top();
284 command->unexecute(); 331 command->unexecute();
285 emit commandExecuted(); 332 emit commandExecuted();
297 void 344 void
298 CommandHistory::redo() 345 CommandHistory::redo()
299 { 346 {
300 if (m_redoStack.empty()) return; 347 if (m_redoStack.empty()) return;
301 348
349 #ifdef DEBUG_COMMAND_HISTORY
350 std::cerr << "CommandHistory::redo()" << std::endl;
351 #endif
352
302 closeBundle(); 353 closeBundle();
303 354
304 Command *command = m_redoStack.top(); 355 Command *command = m_redoStack.top();
305 command->execute(); 356 command->execute();
306 emit commandExecuted(); 357 emit commandExecuted();
372 if ((int)stack.size() > limit) { 423 if ((int)stack.size() > limit) {
373 424
374 CommandStack tempStack; 425 CommandStack tempStack;
375 426
376 for (i = 0; i < limit; ++i) { 427 for (i = 0; i < limit; ++i) {
377 // Command *command = stack.top(); 428 #ifdef DEBUG_COMMAND_HISTORY
378 // std::cerr << "CommandHistory::clipStack: Saving recent command: " << command->getName().toLocal8Bit().data() << " at " << command << std::endl; 429 Command *command = stack.top();
430 std::cerr << "CommandHistory::clipStack: Saving recent command: " << command->getName().toLocal8Bit().data() << " at " << command << std::endl;
431 #endif
379 tempStack.push(stack.top()); 432 tempStack.push(stack.top());
380 stack.pop(); 433 stack.pop();
381 } 434 }
382 435
383 clearStack(stack); 436 clearStack(stack);
393 CommandHistory::clearStack(CommandStack &stack) 446 CommandHistory::clearStack(CommandStack &stack)
394 { 447 {
395 while (!stack.empty()) { 448 while (!stack.empty()) {
396 Command *command = stack.top(); 449 Command *command = stack.top();
397 // Not safe to call getName() on a command about to be deleted 450 // Not safe to call getName() on a command about to be deleted
398 // std::cerr << "CommandHistory::clearStack: About to delete command " << command << std::endl; 451 #ifdef DEBUG_COMMAND_HISTORY
452 std::cerr << "CommandHistory::clearStack: About to delete command " << command << std::endl;
453 #endif
399 delete command; 454 delete command;
400 stack.pop(); 455 stack.pop();
401 } 456 }
402 } 457 }
403 458