Engauge Digitizer 2
Loading...
Searching...
No Matches
DigitizeStateContext.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 "CmdMediator.h"
8#include "DigitizeStateAxis.h"
11#include "DigitizeStateCurve.h"
12#include "DigitizeStateEmpty.h"
14#include "DigitizeStateScale.h"
16#include "DigitizeStateSelect.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 <QSize>
27#include "QtToString.h"
28#include "Transformation.h"
29
31 QGraphicsView &view,
32 bool isGnuplot) :
33 m_mainWindow (mainWindow),
34 m_view (view),
35 m_imageIsLoaded (false),
36 m_isGnuplot (isGnuplot)
37{
38 // These states follow the same order as the DigitizeState enumeration
39 m_states.insert (DIGITIZE_STATE_AXIS , new DigitizeStateAxis (*this));
40 m_states.insert (DIGITIZE_STATE_COLOR_PICKER, new DigitizeStateColorPicker (*this));
41 m_states.insert (DIGITIZE_STATE_CURVE , new DigitizeStateCurve (*this));
42 m_states.insert (DIGITIZE_STATE_EMPTY , new DigitizeStateEmpty (*this));
43 m_states.insert (DIGITIZE_STATE_POINT_MATCH , new DigitizeStatePointMatch (*this));
44 m_states.insert (DIGITIZE_STATE_SEGMENT , new DigitizeStateSegment (*this));
45 m_states.insert (DIGITIZE_STATE_SELECT , new DigitizeStateSelect (*this));
46 m_states.insert (DIGITIZE_STATE_SCALE , new DigitizeStateScale (*this)); // Out of order since added later
47 ENGAUGE_ASSERT (m_states.size () == NUM_DIGITIZE_STATES);
48
49 m_currentState = NUM_DIGITIZE_STATES; // Value that forces a transition right away
52}
53
55{
56 qDeleteAll (m_states);
57}
58
60{
61 return m_states [m_currentState]->activeCurve ();
62}
63
65 QUndoCommand *cmd)
66{
67 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::appendNewCmd";
68
69 cmdMediator->push (cmd);
70}
71
73 const QSize &size) const
74{
75 return m_states [m_currentState]->canPaste (transformation,
76 size);
77}
78
79void DigitizeStateContext::completeRequestedStateTransitionIfExists (CmdMediator *cmdMediator)
80{
81 if (m_currentState != m_requestedState) {
82
83 // A transition is waiting so perform it
84
85 if (m_currentState != NUM_DIGITIZE_STATES) {
86
87 // This is not the first state so close the previous state
88 m_states [m_currentState]->end ();
89 }
90
91 // Start the new state
92 DigitizeState previousState = m_currentState;
93 m_currentState = m_requestedState;
94 m_states [m_requestedState]->begin (cmdMediator,
95 previousState);
96
97 // If transition was triggered from inside the state machine then MainWindow controls need to be set accordingly
98 // as if user had clicked on a digitize button
100 }
101}
102
104{
105 return m_states [m_currentState]->guidelinesAreSelectable();
106}
107
109 const QString &pointIdentifier)
110{
111 m_states [m_currentState]->handleContextMenuEventAxis (cmdMediator,
112 pointIdentifier);
113}
114
116 const QStringList &pointIdentifiers)
117{
118 m_states [m_currentState]->handleContextMenuEventGraph (cmdMediator,
119 pointIdentifiers);
120}
121
123{
124 m_states [m_currentState]->handleCurveChange(cmdMediator);
125}
126
128 Qt::Key key,
129 bool atLeastOneSelectedItem)
130{
131 m_states [m_currentState]->handleKeyPress (cmdMediator,
132 key,
133 atLeastOneSelectedItem);
134
135 completeRequestedStateTransitionIfExists(cmdMediator);
136
137}
138
140 QPointF pos)
141{
142 m_states [m_currentState]->handleMouseMove (cmdMediator,
143 pos);
144
145 completeRequestedStateTransitionIfExists(cmdMediator);
146
147}
148
150 QPointF pos)
151{
152 m_states [m_currentState]->handleMousePress (cmdMediator,
153 pos);
154
155 completeRequestedStateTransitionIfExists(cmdMediator);
156
157}
158
160 QPointF pos)
161{
162 m_states [m_currentState]->handleMouseRelease (cmdMediator,
163 pos);
164
165 completeRequestedStateTransitionIfExists(cmdMediator);
166}
167
169{
170 return m_isGnuplot;
171}
172
174{
175 return m_mainWindow;
176}
177
179{
180 return m_mainWindow;
181}
182
184{
185 m_requestedState = digitizeState;
186}
187
189 DigitizeState digitizeState)
190{
191 m_requestedState = digitizeState;
192 completeRequestedStateTransitionIfExists(cmdMediator);
193}
194
196{
197 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::resetOnLoad";
198
199 // Reset current state. At this point, the current state is DIGITIZE_STATE_EMPTY when opening the first document
200 // so for consistency we always reset it so succeeding documents work the same way
201 if (m_currentState != DIGITIZE_STATE_EMPTY) {
202 m_requestedState = DIGITIZE_STATE_EMPTY;
203 completeRequestedStateTransitionIfExists(cmdMediator);
204 }
205}
206
208{
209 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::setCursor";
210
211 ENGAUGE_ASSERT(m_currentState < m_states.count());
212
213 m_states [m_currentState]->setCursor (cmdMediator);
214}
215
216void DigitizeStateContext::setDragMode (QGraphicsView::DragMode dragMode)
217{
218 LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateContext::setDragMode";
219
220 if (m_imageIsLoaded) {
221 m_view.setDragMode (dragMode);
222 }
223}
224
226 bool imageIsLoaded)
227{
228 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::setImageIsLoaded";
229
230 m_imageIsLoaded = imageIsLoaded;
231 setCursor (cmdMediator);
232}
233
235{
236 ENGAUGE_ASSERT (m_currentState != NUM_DIGITIZE_STATES);
237
238 return m_states [m_currentState]->state();
239}
240
242{
243 ENGAUGE_ASSERT (m_currentState != NUM_DIGITIZE_STATES);
244
245 m_states [m_currentState]->updateAfterPointAddition ();
246}
247
249 const DocumentModelDigitizeCurve &modelDigitizeCurve)
250{
251 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::updateModelDigitizeCurve";
252
253 ENGAUGE_ASSERT(m_currentState < m_states.count());
254
255 m_states [m_currentState]->updateModelDigitizeCurve (cmdMediator,
256 modelDigitizeCurve);
257}
258
260{
261 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateContext::updateModelSegments";
262
263 ENGAUGE_ASSERT(m_currentState < m_states.count());
264
265 m_states [m_currentState]->updateModelSegments (modelSegments);
266}
267
269{
270 return m_view;
271}
DigitizeState
Set of possible states of Digitize toolbar.
@ DIGITIZE_STATE_POINT_MATCH
@ DIGITIZE_STATE_SELECT
@ DIGITIZE_STATE_COLOR_PICKER
@ DIGITIZE_STATE_SEGMENT
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT.
log4cpp::Category * mainCat
Definition Logger.cpp:14
Command queue stack.
Definition CmdMediator.h:24
Digitizing state for digitizing one axis point at a time.
Digitizing state for selecting a color for DigitizeStateSegment.
QString state() const
State name for debugging.
bool isGnuplot() const
Get method for gnuplot flag.
void resetOnLoad(CmdMediator *cmdMediator)
Resetting makes re-initializes for documents after the first.
void handleMouseRelease(CmdMediator *cmdMediator, QPointF pos)
See DigitizeStateAbstractBase::handleMouseRelease.
bool canPaste(const Transformation &transformation, const QSize &viewSize) const
Return true if there is good data in the clipboard for pasting, and that operation is compatible with...
DigitizeStateContext(MainWindow &mainWindow, QGraphicsView &view, bool isGnuplot)
Single constructor.
void handleMouseMove(CmdMediator *cmdMediator, QPointF pos)
See DigitizeStateAbstractBase::handleMouseMove.
void requestImmediateStateTransition(CmdMediator *cmdMediator, DigitizeState digitizeState)
Perform immediate state transition. Called from outside state machine.
void updateModelDigitizeCurve(CmdMediator *cmdMediator, const DocumentModelDigitizeCurve &modelDigitizeCurve)
Update the digitize curve settings.
void handleContextMenuEventGraph(CmdMediator *cmdMediator, const QStringList &pointIdentifiers)
See DigitizeStateAbstractBase::handleContextMenuEventGraph.
void setDragMode(QGraphicsView::DragMode dragMode)
Set QGraphicsView drag mode (in m_view). Called from DigitizeStateAbstractBase subclasses.
void handleMousePress(CmdMediator *cmdMediator, QPointF pos)
See DigitizeStateAbstractBase::handleMousePress.
void appendNewCmd(CmdMediator *cmdMediator, QUndoCommand *cmd)
Append just-created QUndoCommand to command stack. This is called from DigitizeStateAbstractBase subc...
void setCursor(CmdMediator *cmdMediator)
Set cursor after asking state for the new cursor shape.
void setImageIsLoaded(CmdMediator *cmdMediator, 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 handleKeyPress(CmdMediator *cmdMediator, Qt::Key key, bool atLeastOneSelectedItem)
See DigitizeStateAbstractBase::handleKeyPress.
void updateModelSegments(const DocumentModelSegments &modelSegments)
Update the segments given the new settings.
bool guidelinesAreSelectable() const
Enable/disable guidelines according to state.
void requestDelayedStateTransition(DigitizeState digitizeState)
Initiate state transition to be performed later, when DigitizeState is off the stack.
void updateAfterPointAddition()
Update the graphics attributes.
QGraphicsView & view()
QGraphicsView for use by DigitizeStateAbstractBase subclasses.
void handleCurveChange(CmdMediator *cmdMediator)
See DigitizeStateAbstractBase::handleCurveChange.
MainWindow & mainWindow()
Reference to the MainWindow, without const.
void handleContextMenuEventAxis(CmdMediator *cmdMediator, const QString &pointIdentifier)
See DigitizeStateAbstractBase::handleContextMenuEventAxis.
Digitizing state for creating Curve Points, one at a time.
Digitizing state before a Document has been created. In this state, the cursor is Qt::ArrowCursor.
Digitizing state for matching Curve Points, one at a time.
Digitizing state for creating the scale bar.
Digitizing state for creating multiple Points along a highlighted segment.
Digitizing state for selecting one or more Points in the Document.
Model for DlgSettingsDigitizeCurve and CmdSettingsDigitizeCurve.
Model for DlgSettingsSegments and CmdSettingsSegments.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition MainWindow.h:96
CmdMediator * cmdMediator()
Accessor for commands to process the Document.
void updateDigitizeStateIfSoftwareTriggered(DigitizeState digitizeState)
After software-triggered state transition, this method manually triggers the action as if user had cl...
Affine transformation between screen and graph coordinates, based on digitized axis points.
#define LOG4CPP_INFO_S(logger)
Definition convenience.h:18
#define LOG4CPP_DEBUG_S(logger)
Definition convenience.h:20