Engauge Digitizer  2
CmdStackShadow.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "CmdAbstract.h"
8 #include "CmdFactory.h"
9 #include "CmdMediator.h"
10 #include "CmdRedoForTest.h"
11 #include "CmdStackShadow.h"
12 #include "CmdUndoForTest.h"
13 #include "Document.h"
14 #include "DocumentSerialize.h"
15 #include "Logger.h"
16 #include "MainWindow.h"
17 #include <QUndoCommand>
18 #include <QXmlStreamReader>
19 #include "Xml.h"
20 
22  m_mainWindow (0)
23 {
24  LOG4CPP_INFO_S ((*mainCat)) << "CmdStackShadow::CmdStackShadow";
25 }
26 
28 {
29  LOG4CPP_INFO_S ((*mainCat)) << "CmdStackShadow::canRedo";
30 
31  bool canRedo = (m_cmdList.count () > 0);
32 
33  return canRedo;
34 }
35 
37  Document &document,
38  QXmlStreamReader &reader)
39 {
40  LOG4CPP_INFO_S ((*mainCat)) << "CmdStackShadow::loadCommands";
41 
42  // Save pointer to MainWindow
43  m_mainWindow = &mainWindow;
44 
45  // Signals for hack that allows script to perform redo/undo
46  connect (this, SIGNAL (signalRedo ()), mainWindow.cmdMediator(), SLOT (redo ()));
47  connect (this, SIGNAL (signalUndo ()), mainWindow.cmdMediator(), SLOT (undo ()));
48 
49  // Load commands
50  CmdFactory factory;
51  while (!reader.atEnd() && !reader.hasError()) {
52 
53  if ((loadNextFromReader (reader) == QXmlStreamReader::StartElement) &&
54  (reader.name() == DOCUMENT_SERIALIZE_CMD)) {
55 
56  // Extract and append new command to command stack
57  m_cmdList.push_back (factory.createCmd (mainWindow,
58  document,
59  reader));
60  }
61  }
62 }
63 
65 {
66  LOG4CPP_INFO_S ((*mainCat)) << "CmdStackShadow::slotRedo";
67 
68  if (m_cmdList.count() > 0) {
69 
70  // Get the next command from the shadow command stack
71  QUndoCommand *cmd = dynamic_cast<QUndoCommand*> (m_cmdList.front());
72 
73  // Remove this command from the shadow command stack
74  m_cmdList.pop_front();
75 
76  if (m_mainWindow != 0) {
77 
78  CmdRedoForTest *cmdRedoForTest = dynamic_cast<CmdRedoForTest*> (cmd);
79  CmdUndoForTest *cmdUndoForTest = dynamic_cast<CmdUndoForTest*> (cmd);
80 
81  if (cmdRedoForTest != 0) {
82 
83  // Redo command is a special case. Redo of this command is equivalent to redo of the last command on the command stack
84  // (which will never be CmdRedoForTest or CmdUndoForTest since they are never passed onto that command stack)
85  emit (signalRedo ());
86 
87  } else if (cmdUndoForTest != 0) {
88 
89  // Undo command is a special case. Redo of this command is equivalent to undo of the last command on the command stack
90  // (which will never be CmdRedoForTest or CmdUndoForTest since they are never passed onto that command stack)
91  emit (signalUndo ());
92 
93  } else {
94 
95  // Normal command is simply pushed onto the primary command stack
96  m_mainWindow->cmdMediator()->push(cmd);
97 
98  }
99  }
100  }
101 }
102 
104 {
105  LOG4CPP_INFO_S ((*mainCat)) << "CmdStackShadow::slotUndo";
106 
107  CmdListInternal::iterator itr;
108  for (itr = m_cmdList.begin(); itr != m_cmdList.end(); itr++) {
109 
110  CmdAbstract *cmd = *itr;
111  delete cmd;
112  }
113 
114  m_cmdList.clear();
115 }
bool canRedo() const
Return true if there is a command available.
Factory for CmdAbstractBase objects.
Definition: CmdFactory.h:16
void signalRedo()
Signal used to emulate a shift-control-z redo command from user during testing.
Wrapper around QUndoCommand. This simplifies the more complicated feature set of QUndoCommand.
Definition: CmdAbstract.h:19
Command for performing Undo during testing.
Command for performing Redo during testing.
void slotRedo()
Move next command from list to CmdMediator. Noop if there are no more commands.
CmdMediator * cmdMediator()
Accessor for commands to process the Document.
Definition: MainWindow.cpp:337
CmdStackShadow()
Single constructor.
void slotUndo()
Throw away every command since trying to reconcile two different command stacks after an undo is too ...
Storage of one imported image and the data attached to that image.
Definition: Document.h:41
void signalUndo()
Signal used to emulate a shift-z undo command from user during testing.
void loadCommands(MainWindow &mainWindow, Document &document, QXmlStreamReader &reader)
Load commands from serialized xml.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:89