Engauge Digitizer  2
CmdAbstract.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 "DataKey.h"
9 #include "Document.h"
10 #include "DocumentHashGenerator.h"
11 #include "EngaugeAssert.h"
12 #include "GraphicsItemType.h"
13 #include "GraphicsScene.h"
14 #include "GraphicsView.h"
15 #include "Logger.h"
16 #include "MainWindow.h"
17 #include "Point.h"
18 #include <QGraphicsItem>
19 
21  Document &document,
22  const QString &cmdDescription) :
23  QUndoCommand (cmdDescription),
24  m_mainWindow (mainWindow),
25  m_document (document),
26  m_isFirstRedo (true)
27 {
28  LOG4CPP_INFO_S ((*mainCat)) << "CmdAbstract::CmdAbstract";
29 }
30 
31 CmdAbstract::~CmdAbstract()
32 {
33 }
34 
36 {
37  return m_document;
38 }
39 
41 {
42  return m_document;
43 }
44 
46 {
47  return m_mainWindow;
48 }
49 
50 void CmdAbstract::redo ()
51 {
52  // Note that m_identifierIndexBeforeRedo and m_identifierIndexAfterRedo are not set until below (at which point they are logged)
53  LOG4CPP_INFO_S ((*mainCat)) << "CmdAbstract::redo";
54 
55  if (m_isFirstRedo) {
56 
57  m_identifierIndexBeforeRedo = Point::identifierIndex ();
58 
59  } else {
60 
61  // Reset state. The first time this is called, this is a noop since m_identifierIndex was just set to
62  // GraphicsPointAbstractBase::identifierIndex in the constructor of this class
63  Point::setIdentifierIndex (m_identifierIndexBeforeRedo);
64 
65  }
66 
67  // Invoke the leaf class redo method
68  cmdRedo ();
69 
70  if (m_isFirstRedo) {
71 
72  m_isFirstRedo = false;
73  m_identifierIndexAfterRedo = Point::identifierIndex();
74 
75  }
76 
77  LOG4CPP_INFO_S ((*mainCat)) << "CmdAbstract::redo identifierIndex=" << m_identifierIndexBeforeRedo << "->"
78  << m_identifierIndexAfterRedo;
79 }
80 
81 void CmdAbstract::resetSelection(const PointIdentifiers &pointIdentifiersToSelect)
82 {
83  LOG4CPP_INFO_S ((*mainCat)) << "CmdAbstract::resetSelection";
84 
85  QList<QGraphicsItem *> items = mainWindow().view().items();
86  QList<QGraphicsItem *>::iterator itrS;
87  for (itrS = items.begin (); itrS != items.end (); itrS++) {
88 
89  QGraphicsItem *item = *itrS;
90  bool selected = false;
91  if (item->data (DATA_KEY_GRAPHICS_ITEM_TYPE).toInt () == GRAPHICS_ITEM_TYPE_POINT) {
92 
93  QString pointIdentifier = item->data (DATA_KEY_IDENTIFIER).toString ();
94 
95  selected = pointIdentifiersToSelect.contains (pointIdentifier);
96  }
97 
98  item->setSelected (selected);
99  }
100 }
101 
103 {
104  // LOG4CPP_INFO_S is below
105 
106  DocumentHashGenerator documentHashGenerator;
107  DocumentHash documentHash = documentHashGenerator.generate (document);
108 
109  if (m_documentHashPost.count() == 0) {
110 
111  // This is the first time through here so save the initial value
112  m_documentHashPost = documentHash;
113 
114  } else {
115 
116  // This is not the first time through here so compare the current value to the initial value
117  ENGAUGE_ASSERT (documentHash == m_documentHashPost);
118 
119  }
120 
121  LOG4CPP_INFO_S ((*mainCat)) << "CmdAbstract::saveOrCheckPostCommandDocumentStateHash stateHash=" << m_documentHashPost.data ();
122 
123 }
124 
126 {
127  // LOG4CPP_INFO_S is below
128 
129  DocumentHashGenerator documentHashGenerator;
130  DocumentHash documentHash = documentHashGenerator.generate (document);
131 
132  if (m_documentHashPre.count() == 0) {
133 
134  // This is the first time through here so save the initial value
135  m_documentHashPre = documentHash;
136 
137  } else {
138 
139  // This is not the first time through here so compare the current value to the initial value
140  ENGAUGE_ASSERT (documentHash == m_documentHashPre);
141 
142  }
143 
144  LOG4CPP_INFO_S ((*mainCat)) << "CmdAbstract::saveOrCheckPreCommandDocumentStateHash stateHash=" << m_documentHashPre.data ();
145 
146 }
147 
148 void CmdAbstract::undo ()
149 {
150  LOG4CPP_INFO_S ((*mainCat)) << "CmdAbstract::undo identifierIndex=" << m_identifierIndexAfterRedo << "->"
151  << m_identifierIndexBeforeRedo;
152 
153  Point::setIdentifierIndex (m_identifierIndexAfterRedo);
154 
155  // Invoke the leaf class undo method
156  cmdUndo ();
157 
158  Point::setIdentifierIndex (m_identifierIndexBeforeRedo);
159 }
Hash table class that tracks point identifiers as the key, with a corresponding boolean value...
static void setIdentifierIndex(unsigned int identifierIndex)
Reset the current index while performing a Redo.
Definition: Point.cpp:466
static unsigned int identifierIndex()
Return the current index for storage in case we need to reset it later while performing a Redo...
Definition: Point.cpp:261
Generates a DocumentHash value representing the state of the entire Document.
virtual void cmdRedo()=0
Redo method that is called when QUndoStack is moved one command forward.
void saveOrCheckPostCommandDocumentStateHash(const Document &document)
Save, when called the first time, a hash value representing the state of the Document.
DocumentHash generate(const Document &document) const
Generate the hash for external storage.
GraphicsView & view()
View for the QImage and QGraphicsItems, without const.
void saveOrCheckPreCommandDocumentStateHash(const Document &document)
Save, when called the first time, a hash value representing the state of the Document.
CmdAbstract(MainWindow &mainWindow, Document &document, const QString &cmdDescription)
Single constructor.
Definition: CmdAbstract.cpp:20
MainWindow & mainWindow()
Return the MainWindow so it can be updated by this command as a last step.
Definition: CmdAbstract.cpp:45
Storage of one imported image and the data attached to that image.
Definition: Document.h:41
virtual void cmdUndo()=0
Undo method that is called when QUndoStack is moved one command backward.
bool contains(const QString &pointIdentifier) const
True if specified entry exists in the table.
Document & document()
Return the Document that this command will modify during redo and undo.
Definition: CmdAbstract.cpp:35
void resetSelection(const PointIdentifiers &pointIdentifiersToSelect)
Since the set of selected points has probably changed, changed that set back to the specified set...
Definition: CmdAbstract.cpp:81
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:89