Engauge Digitizer  2
CmdStackShadow.cpp
Go to the documentation of this file.
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 (nullptr)
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 != nullptr) {
77 
78  CmdRedoForTest *cmdRedoForTest = dynamic_cast<CmdRedoForTest*> (cmd);
79  CmdUndoForTest *cmdUndoForTest = dynamic_cast<CmdUndoForTest*> (cmd);
80 
81  if (cmdRedoForTest != nullptr) {
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 != nullptr) {
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 }
Xml.h
MainWindow::cmdMediator
CmdMediator * cmdMediator()
Accessor for commands to process the Document.
Definition: MainWindow.cpp:349
CmdStackShadow::CmdStackShadow
CmdStackShadow()
Single constructor.
Definition: CmdStackShadow.cpp:21
CmdStackShadow::loadCommands
void loadCommands(MainWindow &mainWindow, Document &document, QXmlStreamReader &reader)
Load commands from serialized xml.
Definition: CmdStackShadow.cpp:36
CmdStackShadow::signalUndo
void signalUndo()
Signal used to emulate a shift-z undo command from user during testing.
CmdMediator.h
Document
Storage of one imported image and the data attached to that image.
Definition: Document.h:40
Document.h
DocumentSerialize.h
CmdAbstract.h
MainWindow
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:90
CmdStackShadow::slotRedo
void slotRedo()
Move next command from list to CmdMediator. Noop if there are no more commands.
Definition: CmdStackShadow.cpp:64
Logger.h
CmdRedoForTest.h
CmdUndoForTest
Command for performing Undo during testing.
Definition: CmdUndoForTest.h:19
LOG4CPP_INFO_S
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
CmdRedoForTest
Command for performing Redo during testing.
Definition: CmdRedoForTest.h:19
CmdFactory
Factory for CmdAbstractBase objects.
Definition: CmdFactory.h:15
mainCat
log4cpp::Category * mainCat
Definition: Logger.cpp:14
MainWindow.h
CmdFactory.h
CmdStackShadow::slotUndo
void slotUndo()
Throw away every command since trying to reconcile two different command stacks after an undo is too ...
Definition: CmdStackShadow.cpp:103
CmdStackShadow::signalRedo
void signalRedo()
Signal used to emulate a shift-control-z redo command from user during testing.
CmdStackShadow.h
CmdUndoForTest.h
CmdStackShadow::canRedo
bool canRedo() const
Return true if there is a command available.
Definition: CmdStackShadow.cpp:27
DOCUMENT_SERIALIZE_CMD
const QString DOCUMENT_SERIALIZE_CMD
CmdAbstract
Wrapper around QUndoCommand. This simplifies the more complicated feature set of QUndoCommand.
Definition: CmdAbstract.h:18
loadNextFromReader
QXmlStreamReader::TokenType loadNextFromReader(QXmlStreamReader &reader)
Load next token from xml reader.
Definition: Xml.cpp:14