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