Engauge Digitizer  2
DigitizeStateSegment.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 "CmdAddPointsGraph.h"
8 #include "DigitizeStateContext.h"
9 #include "DigitizeStateSegment.h"
10 #include "EngaugeAssert.h"
11 #include "Logger.h"
12 #include "MainWindow.h"
13 #include "OrdinalGenerator.h"
14 #include <QGraphicsPixmapItem>
15 #include <QGraphicsScene>
16 #include <QImage>
17 #include "Segment.h"
18 #include "SegmentFactory.h"
19 
22 {
23 }
24 
25 DigitizeStateSegment::~DigitizeStateSegment ()
26 {
27 }
28 
30 {
32 }
33 
35  DigitizeState /* previousState */)
36 {
37  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::begin";
38 
39  m_cmdMediator = cmdMediator; // Save for slotMouseClickOnSegment
40 
41  setCursor(cmdMediator);
42  context().setDragMode(QGraphicsView::NoDrag);
44 
45  handleCurveChange(cmdMediator);
46 }
47 
48 QCursor DigitizeStateSegment::cursor(CmdMediator * /* cmdMediator */) const
49 {
50  LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateSegment::cursor";
51 
52  return QCursor (Qt::ArrowCursor);
53 }
54 
56 {
57  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::end";
58 
59  GraphicsScene &scene = context().mainWindow().scene();
60  SegmentFactory segmentFactory ((QGraphicsScene &) scene,
61  context().isGnuplot());
62 
63  segmentFactory.clearSegments(m_segments);
64 }
65 
67 {
68  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleCurveChange";
69 
70  QImage img = context().mainWindow().imageFiltered();
71 
72  GraphicsScene &scene = context().mainWindow().scene();
73  SegmentFactory segmentFactory ((QGraphicsScene &) scene,
74  context().isGnuplot());
75 
76  segmentFactory.clearSegments (m_segments);
77 
78  // Create new segments
79  segmentFactory.makeSegments (img,
80  cmdMediator->document().modelSegments(),
81  m_segments);
82 
83  // Connect signals of the new segments
84  QList<Segment*>::iterator itr;
85  for (itr = m_segments.begin(); itr != m_segments.end(); itr++) {
86  Segment *segment = *itr;
87 
88  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleCurveChange"
89  << " lines=" << segment->lineCount();
90 
91  connect (segment, SIGNAL (signalMouseClickOnSegment (QPointF)), this, SLOT (slotMouseClickOnSegment (QPointF)));
92  }
93 }
94 
96  Qt::Key key,
97  bool /* atLeastOneSelectedItem */)
98 {
99  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleKeyPress"
100  << " key=" << QKeySequence (key).toString ().toLatin1 ().data ();
101 }
102 
104  QPointF /* posScreen */)
105 {
106 // LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateSegment::handleMouseMove";
107 }
108 
110  QPointF /* posScreen */)
111 {
112  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleMousePress";
113 }
114 
116  QPointF /* posScreen */)
117 {
118  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleMouseRelease";
119 }
120 
121 Segment *DigitizeStateSegment::segmentFromSegmentStart (const QPointF &posSegmentStart) const
122 {
123  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::segmentFromSegmentStart"
124  << " segments=" << m_segments.count();
125 
126  QList<Segment*>::const_iterator itr;
127  for (itr = m_segments.begin(); itr != m_segments.end(); itr++) {
128  Segment *segment = *itr;
129 
130  if (segment->firstPoint() == posSegmentStart) {
131 
132  return segment;
133  }
134  }
135 
136  LOG4CPP_ERROR_S ((*mainCat)) << "DigitizeStateSegment::segmentFromSegmentStart";
137  ENGAUGE_ASSERT (false);
138  return 0;
139 }
140 
142 {
143  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::slotMouseClickOnSegment";
144 
145  Segment *segment = segmentFromSegmentStart (posSegmentStart);
146 
147  // Create single-entry list that is expected by SegmentFactory
148  QList<Segment*> segments;
149  segments.push_back (segment);
150 
151  // Generate point coordinates. Nothing is created in the GraphicsScene at this point
152  GraphicsScene &scene = context().mainWindow().scene();
153  SegmentFactory segmentFactory ((QGraphicsScene &) scene,
154  context().isGnuplot());
155 
156  QList<QPoint> points = segmentFactory.fillPoints (m_cmdMediator->document().modelSegments(),
157  segments);
158 
159  // Create one ordinal for each point
160  OrdinalGenerator ordinalGenerator;
161  Document &document = m_cmdMediator->document ();
162  const Transformation &transformation = context ().mainWindow ().transformation();
163  QList<double> ordinals;
164  QList<QPoint>::iterator itr;
165  for (itr = points.begin(); itr != points.end(); itr++) {
166 
167  QPoint point = *itr;
168  ordinals << ordinalGenerator.generateCurvePointOrdinal(document,
169  transformation,
170  point,
171  activeCurve ());
172  }
173 
174  // Create command to add points
175  QUndoCommand *cmd = new CmdAddPointsGraph (context ().mainWindow(),
176  document,
177  context ().mainWindow().selectedGraphCurve(),
178  points,
179  ordinals);
180  context().appendNewCmd(m_cmdMediator,
181  cmd);
182 }
183 
185 {
186  return "DigitizeStateSegment";
187 }
188 
190  const DocumentModelDigitizeCurve & /*modelDigitizeCurve */)
191 {
192  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::updateModelDigitizeCurve";
193 }
194 
196 {
197  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::updateModelSegments";
198 
199  QList<Segment*>::const_iterator itr;
200  for (itr = m_segments.begin(); itr != m_segments.end(); itr++) {
201  Segment *segment = *itr;
202 
203  segment->updateModelSegment (modelSegments);
204  }
205 }
virtual QString state() const
State name for debugging.
Transformation transformation() const
Return read-only copy of transformation.
int lineCount() const
Get method for number of lines.
Definition: Segment.cpp:383
void setDragMode(QGraphicsView::DragMode dragMode)
Set QGraphicsView drag mode (in m_view). Called from DigitizeStateAbstractBase subclasses.
virtual void handleMousePress(CmdMediator *cmdMediator, QPointF posScreen)
Handle a mouse press that was intercepted earlier.
QList< QPoint > fillPoints(const DocumentModelSegments &modelSegments, QList< Segment * > segments)
Return segment fill points for all segments, for previewing.
virtual void updateModelSegments(const DocumentModelSegments &modelSegments)
Update the segments given the new settings.
void updateViewsOfSettings(const QString &activeCurve)
Update curve-specific view of settings. Private version gets active curve name from DigitizeStateCont...
QString selectedGraphCurve() const
Curve name that is currently selected in m_cmbCurve.
virtual void handleMouseMove(CmdMediator *cmdMediator, QPointF posScreen)
Handle a mouse move. This is part of an experiment to see if augmenting the cursor in Point Match mod...
virtual void updateModelDigitizeCurve(CmdMediator *cmdMediator, const DocumentModelDigitizeCurve &modelDigitizeCurve)
Update the digitize curve settings.
Document & document()
Provide the Document to commands, primarily for undo/redo processing.
Definition: CmdMediator.cpp:72
void clearSegments(QList< Segment * > &segments)
Remove the segments created by makeSegments.
DigitizeStateContext & context()
Reference to the DigitizeStateContext that contains all the DigitizeStateAbstractBase subclasses...
MainWindow & mainWindow()
Reference to the MainWindow, without const.
Factory class for Segment objects.
virtual void handleCurveChange(CmdMediator *cmdMediator)
Handle the selection of a new curve. At a minimum, DigitizeStateSegment will generate a new set of Se...
Model for DlgSettingsDigitizeCurve and CmdSettingsDigitizeCurve.
Affine transformation between screen and graph coordinates, based on digitized axis points...
GraphicsScene & scene()
Scene container for the QImage and QGraphicsItems.
void setCursor(CmdMediator *cmdMediator)
Update the cursor according to the current state.
Container for all DigitizeStateAbstractBase subclasses. This functions as the context class in a stan...
void appendNewCmd(CmdMediator *cmdMediator, QUndoCommand *cmd)
Append just-created QUndoCommand to command stack. This is called from DigitizeStateAbstractBase subc...
virtual void end()
Method that is called at the exact moment a state is exited. Typically called just before begin for t...
Command for adding one or more graph points. This is for Segment Fill mode.
void makeSegments(const QImage &imageFiltered, const DocumentModelSegments &modelSegments, QList< Segment * > &segments, bool useDlg=true)
Main entry point for creating all Segments for the filtered image.
Storage of one imported image and the data attached to that image.
Definition: Document.h:40
Selectable piecewise-defined line that follows a filtered line in the image.
Definition: Segment.h:21
QImage imageFiltered() const
Background image that has been filtered for the current curve. This asserts if a curve-specific image...
void slotMouseClickOnSegment(QPointF)
Receive signal from Segment that has been clicked on. The CmdMediator from the begin method will be u...
virtual void begin(CmdMediator *cmdMediator, DigitizeState previousState)
Method that is called at the exact moment a state is entered.
Utility class for generating ordinal numbers.
Command queue stack.
Definition: CmdMediator.h:23
DocumentModelSegments modelSegments() const
Get method for DocumentModelSegments.
Definition: Document.cpp:660
void updateModelSegment(const DocumentModelSegments &modelSegments)
Update this segment given the new settings.
Definition: Segment.cpp:537
QPointF firstPoint() const
Coordinates of first point in Segment.
Definition: Segment.cpp:284
Model for DlgSettingsSegments and CmdSettingsSegments.
Base class for all digitizing states. This serves as an interface to DigitizeStateContext.
virtual QString activeCurve() const
Name of the active Curve. This can include AXIS_CURVE_NAME.
Add point and line handling to generic QGraphicsScene.
Definition: GraphicsScene.h:31
DigitizeStateSegment(DigitizeStateContext &context)
Single constructor.
double generateCurvePointOrdinal(const Document &document, const Transformation &transformation, const QPointF &posScreen, const QString &curveName)
Select ordinal so new point curve passes smoothly through existing points.
virtual void handleMouseRelease(CmdMediator *cmdMediator, QPointF posScreen)
Handle a mouse release that was intercepted earlier.
virtual void handleKeyPress(CmdMediator *cmdMediator, Qt::Key key, bool atLeastOneSelectedItem)
Handle a key press that was intercepted earlier.
virtual QCursor cursor(CmdMediator *cmdMediator) const
Returns the state-specific cursor shape.