Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
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 "DigitizeStateSegment.h"
15 #include "DigitizeStateSelect.h"
16 #include "DocumentModelSegments.h"
17 #include "EngaugeAssert.h"
18 #include "GraphicsScene.h"
19 #include "GraphicsView.h"
20 #include "Logger.h"
21 #include "MainWindow.h"
22 #include <QCursor>
23 #include <QGraphicsScene>
24 #include <QGraphicsView>
25 #include "QtToString.h"
26 
28  QGraphicsView &view,
29  bool isGnuplot) :
30  m_mainWindow (mainWindow),
31  m_view (view),
32  m_imageIsLoaded (false),
33  m_isGnuplot (isGnuplot)
34 {
35  // These states follow the same order as the DigitizeState enumeration
36  m_states.insert (DIGITIZE_STATE_AXIS , new DigitizeStateAxis (*this));
37  m_states.insert (DIGITIZE_STATE_COLOR_PICKER, new DigitizeStateColorPicker (*this));
38  m_states.insert (DIGITIZE_STATE_CURVE , new DigitizeStateCurve (*this));
39  m_states.insert (DIGITIZE_STATE_EMPTY , new DigitizeStateEmpty (*this));
40  m_states.insert (DIGITIZE_STATE_POINT_MATCH , new DigitizeStatePointMatch (*this));
41  m_states.insert (DIGITIZE_STATE_SEGMENT , new DigitizeStateSegment (*this));
42  m_states.insert (DIGITIZE_STATE_SELECT , new DigitizeStateSelect (*this));
43  ENGAUGE_ASSERT (m_states.size () == NUM_DIGITIZE_STATES);
44 
45  m_currentState = NUM_DIGITIZE_STATES; // Value that forces a transition right away
47  DIGITIZE_STATE_EMPTY);
48 }
49 
50 DigitizeStateContext::~DigitizeStateContext()
51 {
52 }
53 
55 {
56  return m_states [m_currentState]->activeCurve ();
57 }
58 
60  QUndoCommand *cmd)
61 {
62  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::appendNewCmd";
63 
64  cmdMediator->push (cmd);
65 }
66 
67 void DigitizeStateContext::completeRequestedStateTransitionIfExists (CmdMediator *cmdMediator)
68 {
69  if (m_currentState != m_requestedState) {
70 
71  // A transition is waiting so perform it
72 
73  if (m_currentState != NUM_DIGITIZE_STATES) {
74 
75  // This is not the first state so close the previous state
76  m_states [m_currentState]->end ();
77  }
78 
79  // Start the new state
80  DigitizeState previousState = m_currentState;
81  m_currentState = m_requestedState;
82  m_states [m_requestedState]->begin (cmdMediator,
83  previousState);
84 
85  // If transition was triggered from inside the state machine then MainWindow controls need to be set accordingly
86  // as if user had clicked on a digitize button
88  }
89 }
90 
92  const QString &pointIdentifier)
93 {
94  m_states [m_currentState]->handleContextMenuEventAxis (cmdMediator,
95  pointIdentifier);
96 }
97 
99  const QStringList &pointIdentifiers)
100 {
101  m_states [m_currentState]->handleContextMenuEventGraph (cmdMediator,
102  pointIdentifiers);
103 }
104 
106 {
107  m_states [m_currentState]->handleCurveChange(cmdMediator);
108 }
109 
111  Qt::Key key,
112  bool atLeastOneSelectedItem)
113 {
114  m_states [m_currentState]->handleKeyPress (cmdMediator,
115  key,
116  atLeastOneSelectedItem);
117 
118  completeRequestedStateTransitionIfExists(cmdMediator);
119 
120 }
121 
123  QPointF pos)
124 {
125  m_states [m_currentState]->handleMouseMove (cmdMediator,
126  pos);
127 
128  completeRequestedStateTransitionIfExists(cmdMediator);
129 
130 }
131 
133  QPointF pos)
134 {
135  m_states [m_currentState]->handleMousePress (cmdMediator,
136  pos);
137 
138  completeRequestedStateTransitionIfExists(cmdMediator);
139 
140 }
141 
143  QPointF pos)
144 {
145  m_states [m_currentState]->handleMouseRelease (cmdMediator,
146  pos);
147 
148  completeRequestedStateTransitionIfExists(cmdMediator);
149 }
150 
152 {
153  return m_isGnuplot;
154 }
155 
157 {
158  return m_mainWindow;
159 }
160 
162 {
163  return m_mainWindow;
164 }
165 
166 void DigitizeStateContext::requestDelayedStateTransition (DigitizeState digitizeState)
167 {
168  m_requestedState = digitizeState;
169 }
170 
172  DigitizeState digitizeState)
173 {
174  m_requestedState = digitizeState;
175  completeRequestedStateTransitionIfExists(cmdMediator);
176 }
177 
179 {
180  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::resetOnLoad";
181 
182  // Reset current state. At this point, the current state is DIGITIZE_STATE_EMPTY when opening the first document
183  // so for consistency we always reset it so succeeding documents work the same way
184  if (m_currentState != DIGITIZE_STATE_EMPTY) {
185  m_requestedState = DIGITIZE_STATE_EMPTY;
186  completeRequestedStateTransitionIfExists(cmdMediator);
187  }
188 }
189 
191 {
192  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::setCursor";
193 
194  ENGAUGE_ASSERT(m_currentState < m_states.count());
195 
196  m_states [m_currentState]->setCursor (cmdMediator);
197 }
198 
199 void DigitizeStateContext::setDragMode (QGraphicsView::DragMode dragMode)
200 {
201  LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateContext::setDragMode";
202 
203  if (m_imageIsLoaded) {
204  m_view.setDragMode (dragMode);
205  }
206 }
207 
209  bool imageIsLoaded)
210 {
211  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::setImageIsLoaded";
212 
213  m_imageIsLoaded = imageIsLoaded;
214  setCursor (cmdMediator);
215 }
216 
218 {
219  ENGAUGE_ASSERT (m_currentState != NUM_DIGITIZE_STATES);
220 
221  return m_states [m_currentState]->state();
222 }
223 
225 {
226  ENGAUGE_ASSERT (m_currentState != NUM_DIGITIZE_STATES);
227 
228  m_states [m_currentState]->updateAfterPointAddition ();
229 }
230 
232  const DocumentModelDigitizeCurve &modelDigitizeCurve)
233 {
234  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::updateModelDigitizeCurve";
235 
236  ENGAUGE_ASSERT(m_currentState < m_states.count());
237 
238  m_states [m_currentState]->updateModelDigitizeCurve (cmdMediator,
239  modelDigitizeCurve);
240 }
241 
243 {
244  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::updateModelSegments";
245 
246  ENGAUGE_ASSERT(m_currentState < m_states.count());
247 
248  m_states [m_currentState]->updateModelSegments (modelSegments);
249 }
250 
252 {
253  return m_view;
254 }
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.
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.
QString activeCurve() const
Curve name for active Curve. This can include AXIS_CURVE_NAME, and empty string.
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.
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:313
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.
QString state() const
State name for debugging.
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.
bool isGnuplot() const
Get method for gnuplot flag.
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.