Engauge Digitizer  2
DigitizeStateContext.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 "CmdMediator.h"
8 #include "DigitizeStateAxis.h"
9 #include "DigitizeStateColorPicker.h"
10 #include "DigitizeStateContext.h"
11 #include "DigitizeStateCurve.h"
12 #include "DigitizeStateEmpty.h"
13 #include "DigitizeStatePointMatch.h"
14 #include "DigitizeStateScale.h"
15 #include "DigitizeStateSegment.h"
16 #include "DigitizeStateSelect.h"
17 #include "DocumentModelSegments.h"
18 #include "EngaugeAssert.h"
19 #include "GraphicsScene.h"
20 #include "GraphicsView.h"
21 #include "Logger.h"
22 #include "MainWindow.h"
23 #include <QCursor>
24 #include <QGraphicsScene>
25 #include <QGraphicsView>
26 #include "QtToString.h"
27 
29  QGraphicsView &view,
30  bool isGnuplot) :
31  m_mainWindow (mainWindow),
32  m_view (view),
33  m_imageIsLoaded (false),
34  m_isGnuplot (isGnuplot)
35 {
36  // These states follow the same order as the DigitizeState enumeration
37  m_states.insert (DIGITIZE_STATE_AXIS , new DigitizeStateAxis (*this));
38  m_states.insert (DIGITIZE_STATE_COLOR_PICKER, new DigitizeStateColorPicker (*this));
39  m_states.insert (DIGITIZE_STATE_CURVE , new DigitizeStateCurve (*this));
40  m_states.insert (DIGITIZE_STATE_EMPTY , new DigitizeStateEmpty (*this));
41  m_states.insert (DIGITIZE_STATE_POINT_MATCH , new DigitizeStatePointMatch (*this));
42  m_states.insert (DIGITIZE_STATE_SEGMENT , new DigitizeStateSegment (*this));
43  m_states.insert (DIGITIZE_STATE_SELECT , new DigitizeStateSelect (*this));
44  m_states.insert (DIGITIZE_STATE_SCALE , new DigitizeStateScale (*this)); // Out of order since added later
45  ENGAUGE_ASSERT (m_states.size () == NUM_DIGITIZE_STATES);
46 
47  m_currentState = NUM_DIGITIZE_STATES; // Value that forces a transition right away
49  DIGITIZE_STATE_EMPTY);
50 }
51 
52 DigitizeStateContext::~DigitizeStateContext()
53 {
54 }
55 
57 {
58  return m_states [m_currentState]->activeCurve ();
59 }
60 
62  QUndoCommand *cmd)
63 {
64  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::appendNewCmd";
65 
66  cmdMediator->push (cmd);
67 }
68 
69 void DigitizeStateContext::completeRequestedStateTransitionIfExists (CmdMediator *cmdMediator)
70 {
71  if (m_currentState != m_requestedState) {
72 
73  // A transition is waiting so perform it
74 
75  if (m_currentState != NUM_DIGITIZE_STATES) {
76 
77  // This is not the first state so close the previous state
78  m_states [m_currentState]->end ();
79  }
80 
81  // Start the new state
82  DigitizeState previousState = m_currentState;
83  m_currentState = m_requestedState;
84  m_states [m_requestedState]->begin (cmdMediator,
85  previousState);
86 
87  // If transition was triggered from inside the state machine then MainWindow controls need to be set accordingly
88  // as if user had clicked on a digitize button
90  }
91 }
92 
94  const QString &pointIdentifier)
95 {
96  m_states [m_currentState]->handleContextMenuEventAxis (cmdMediator,
97  pointIdentifier);
98 }
99 
101  const QStringList &pointIdentifiers)
102 {
103  m_states [m_currentState]->handleContextMenuEventGraph (cmdMediator,
104  pointIdentifiers);
105 }
106 
108 {
109  m_states [m_currentState]->handleCurveChange(cmdMediator);
110 }
111 
113  Qt::Key key,
114  bool atLeastOneSelectedItem)
115 {
116  m_states [m_currentState]->handleKeyPress (cmdMediator,
117  key,
118  atLeastOneSelectedItem);
119 
120  completeRequestedStateTransitionIfExists(cmdMediator);
121 
122 }
123 
125  QPointF pos)
126 {
127  m_states [m_currentState]->handleMouseMove (cmdMediator,
128  pos);
129 
130  completeRequestedStateTransitionIfExists(cmdMediator);
131 
132 }
133 
135  QPointF pos)
136 {
137  m_states [m_currentState]->handleMousePress (cmdMediator,
138  pos);
139 
140  completeRequestedStateTransitionIfExists(cmdMediator);
141 
142 }
143 
145  QPointF pos)
146 {
147  m_states [m_currentState]->handleMouseRelease (cmdMediator,
148  pos);
149 
150  completeRequestedStateTransitionIfExists(cmdMediator);
151 }
152 
154 {
155  return m_isGnuplot;
156 }
157 
159 {
160  return m_mainWindow;
161 }
162 
164 {
165  return m_mainWindow;
166 }
167 
168 void DigitizeStateContext::requestDelayedStateTransition (DigitizeState digitizeState)
169 {
170  m_requestedState = digitizeState;
171 }
172 
174  DigitizeState digitizeState)
175 {
176  m_requestedState = digitizeState;
177  completeRequestedStateTransitionIfExists(cmdMediator);
178 }
179 
181 {
182  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::resetOnLoad";
183 
184  // Reset current state. At this point, the current state is DIGITIZE_STATE_EMPTY when opening the first document
185  // so for consistency we always reset it so succeeding documents work the same way
186  if (m_currentState != DIGITIZE_STATE_EMPTY) {
187  m_requestedState = DIGITIZE_STATE_EMPTY;
188  completeRequestedStateTransitionIfExists(cmdMediator);
189  }
190 }
191 
193 {
194  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::setCursor";
195 
196  ENGAUGE_ASSERT(m_currentState < m_states.count());
197 
198  m_states [m_currentState]->setCursor (cmdMediator);
199 }
200 
201 void DigitizeStateContext::setDragMode (QGraphicsView::DragMode dragMode)
202 {
203  LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateContext::setDragMode";
204 
205  if (m_imageIsLoaded) {
206  m_view.setDragMode (dragMode);
207  }
208 }
209 
211  bool imageIsLoaded)
212 {
213  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::setImageIsLoaded";
214 
215  m_imageIsLoaded = imageIsLoaded;
216  setCursor (cmdMediator);
217 }
218 
220 {
221  ENGAUGE_ASSERT (m_currentState != NUM_DIGITIZE_STATES);
222 
223  return m_states [m_currentState]->state();
224 }
225 
227 {
228  ENGAUGE_ASSERT (m_currentState != NUM_DIGITIZE_STATES);
229 
230  m_states [m_currentState]->updateAfterPointAddition ();
231 }
232 
234  const DocumentModelDigitizeCurve &modelDigitizeCurve)
235 {
236  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::updateModelDigitizeCurve";
237 
238  ENGAUGE_ASSERT(m_currentState < m_states.count());
239 
240  m_states [m_currentState]->updateModelDigitizeCurve (cmdMediator,
241  modelDigitizeCurve);
242 }
243 
245 {
246  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::updateModelSegments";
247 
248  ENGAUGE_ASSERT(m_currentState < m_states.count());
249 
250  m_states [m_currentState]->updateModelSegments (modelSegments);
251 }
252 
254 {
255  return m_view;
256 }
void requestDelayedStateTransition(DigitizeState digitizeState)
Initiate state transition to be performed later, when DigitizeState is off the stack.
void updateAfterPointAddition()
Update the graphics attributes.
void resetOnLoad(CmdMediator *cmdMediator)
Resetting makes re-initializes for documents after the first.
Digitizing state for creating the scale bar.
void updateDigitizeStateIfSoftwareTriggered(DigitizeState digitizeState)
After software-triggered state transition, this method manually triggers the action as if user had cl...
void setDragMode(QGraphicsView::DragMode dragMode)
Set QGraphicsView drag mode (in m_view). Called from DigitizeStateAbstractBase subclasses.
void updateModelDigitizeCurve(CmdMediator *cmdMediator, const DocumentModelDigitizeCurve &modelDigitizeCurve)
Update the digitize curve settings.
void handleContextMenuEventAxis(CmdMediator *cmdMediator, const QString &pointIdentifier)
See DigitizeStateAbstractBase::handleContextMenuEventAxis.
void setCursor(CmdMediator *cmdMediator)
Set cursor after asking state for the new cursor shape.
Digitizing state for selecting a color for DigitizeStateSegment.
void handleCurveChange(CmdMediator *cmdMediator)
See DigitizeStateAbstractBase::handleCurveChange.
void handleContextMenuEventGraph(CmdMediator *cmdMediator, const QStringList &pointIdentifiers)
See DigitizeStateAbstractBase::handleContextMenuEventGraph.
QString state() const
State name for debugging.
bool isGnuplot() const
Get method for gnuplot flag.
QString activeCurve() const
Curve name for active Curve. This can include AXIS_CURVE_NAME, and empty string.
Digitizing state before a Document has been created. In this state, the cursor is Qt::ArrowCursor...
CmdMediator * cmdMediator()
Accessor for commands to process the Document.
Definition: MainWindow.cpp:322
void handleKeyPress(CmdMediator *cmdMediator, Qt::Key key, bool atLeastOneSelectedItem)
See DigitizeStateAbstractBase::handleKeyPress.
MainWindow & mainWindow()
Reference to the MainWindow, without const.
void setImageIsLoaded(CmdMediator *cmdMediator, bool imageIsLoaded)
Set the image so QGraphicsView cursor and drag mode are accessible.
Digitizing state for matching Curve Points, one at a time.
Model for DlgSettingsDigitizeCurve and CmdSettingsDigitizeCurve.
Digitizing state for selecting one or more Points in the Document.
void appendNewCmd(CmdMediator *cmdMediator, QUndoCommand *cmd)
Append just-created QUndoCommand to command stack. This is called from DigitizeStateAbstractBase subc...
DigitizeStateContext(MainWindow &mainWindow, QGraphicsView &view, bool isGnuplot)
Single constructor.
void updateModelSegments(const DocumentModelSegments &modelSegments)
Update the segments given the new settings.
void handleMouseMove(CmdMediator *cmdMediator, QPointF pos)
See DigitizeStateAbstractBase::handleMouseMove.
void handleMouseRelease(CmdMediator *cmdMediator, QPointF pos)
See DigitizeStateAbstractBase::handleMouseRelease.
Command queue stack.
Definition: CmdMediator.h:23
Model for DlgSettingsSegments and CmdSettingsSegments.
Digitizing state for creating Curve Points, one at a time.
QGraphicsView & view()
QGraphicsView for use by DigitizeStateAbstractBase subclasses.
void handleMousePress(CmdMediator *cmdMediator, QPointF pos)
See DigitizeStateAbstractBase::handleMousePress.
Digitizing state for creating multiple Points along a highlighted segment.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:86
Digitizing state for digitizing one axis point at a time.
void requestImmediateStateTransition(CmdMediator *cmdMediator, DigitizeState digitizeState)
Perform immediate state transition. Called from outside state machine.