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  const QString &pointIdentifier)
68 {
69  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleContextMenuEventAxis "
70  << " point=" << pointIdentifier.toLatin1 ().data ();
71 }
72 
74  const QStringList &pointIdentifiers)
75 {
76  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment ::handleContextMenuEventGraph "
77  << "points=" << pointIdentifiers.join(",").toLatin1 ().data ();
78 }
79 
81 {
82  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleCurveChange";
83 
84  QImage img = context().mainWindow().imageFiltered();
85 
86  GraphicsScene &scene = context().mainWindow().scene();
87  SegmentFactory segmentFactory ((QGraphicsScene &) scene,
88  context().isGnuplot());
89 
90  segmentFactory.clearSegments (m_segments);
91 
92  // Create new segments
93  segmentFactory.makeSegments (img,
94  cmdMediator->document().modelSegments(),
95  m_segments);
96 
97  // Connect signals of the new segments
98  QList<Segment*>::iterator itr;
99  for (itr = m_segments.begin(); itr != m_segments.end(); itr++) {
100  Segment *segment = *itr;
101 
102  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleCurveChange"
103  << " lines=" << segment->lineCount();
104 
105  connect (segment, SIGNAL (signalMouseClickOnSegment (QPointF)), this, SLOT (slotMouseClickOnSegment (QPointF)));
106  }
107 }
108 
110  Qt::Key key,
111  bool /* atLeastOneSelectedItem */)
112 {
113  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleKeyPress"
114  << " key=" << QKeySequence (key).toString ().toLatin1 ().data ();
115 }
116 
118  QPointF /* posScreen */)
119 {
120 // LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateSegment::handleMouseMove";
121 }
122 
124  QPointF /* posScreen */)
125 {
126  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleMousePress";
127 }
128 
130  QPointF /* posScreen */)
131 {
132  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleMouseRelease";
133 }
134 
135 Segment *DigitizeStateSegment::segmentFromSegmentStart (const QPointF &posSegmentStart) const
136 {
137  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::segmentFromSegmentStart"
138  << " segments=" << m_segments.count();
139 
140  QList<Segment*>::const_iterator itr;
141  for (itr = m_segments.begin(); itr != m_segments.end(); itr++) {
142  Segment *segment = *itr;
143 
144  if (segment->firstPoint() == posSegmentStart) {
145 
146  return segment;
147  }
148  }
149 
150  LOG4CPP_ERROR_S ((*mainCat)) << "DigitizeStateSegment::segmentFromSegmentStart";
151  ENGAUGE_ASSERT (false);
152  return 0;
153 }
154 
156 {
157  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::slotMouseClickOnSegment";
158 
159  Segment *segment = segmentFromSegmentStart (posSegmentStart);
160 
161  // Create single-entry list that is expected by SegmentFactory
162  QList<Segment*> segments;
163  segments.push_back (segment);
164 
165  // Generate point coordinates. Nothing is created in the GraphicsScene at this point
166  GraphicsScene &scene = context().mainWindow().scene();
167  SegmentFactory segmentFactory ((QGraphicsScene &) scene,
168  context().isGnuplot());
169 
170  QList<QPoint> points = segmentFactory.fillPoints (m_cmdMediator->document().modelSegments(),
171  segments);
172 
173  // Create one ordinal for each point
174  OrdinalGenerator ordinalGenerator;
175  Document &document = m_cmdMediator->document ();
176  const Transformation &transformation = context ().mainWindow ().transformation();
177  QList<double> ordinals;
178  QList<QPoint>::iterator itr;
179  for (itr = points.begin(); itr != points.end(); itr++) {
180 
181  QPoint point = *itr;
182  ordinals << ordinalGenerator.generateCurvePointOrdinal(document,
183  transformation,
184  point,
185  activeCurve ());
186  }
187 
188  // Create command to add points
189  QUndoCommand *cmd = new CmdAddPointsGraph (context ().mainWindow(),
190  document,
191  context ().mainWindow().selectedGraphCurve(),
192  points,
193  ordinals);
194  context().appendNewCmd(m_cmdMediator,
195  cmd);
196 }
197 
199 {
200  return "DigitizeStateSegment";
201 }
202 
204 {
205  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::updateAfterPointAddition";
206 }
207 
209  const DocumentModelDigitizeCurve & /*modelDigitizeCurve */)
210 {
211  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::updateModelDigitizeCurve";
212 }
213 
215 {
216  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::updateModelSegments";
217 
218  QList<Segment*>::const_iterator itr;
219  for (itr = m_segments.begin(); itr != m_segments.end(); itr++) {
220  Segment *segment = *itr;
221 
222  segment->updateModelSegment (modelSegments);
223  }
224 }
virtual void updateAfterPointAddition()
Update graphics attributes after possible new points. This is useful for highlight opacity...
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:384
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.
virtual void handleContextMenuEventAxis(CmdMediator *cmdMediator, const QString &pointIdentifier)
Handle a right click, on an axis point, that was intercepted earlier.
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:41
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.
virtual void handleContextMenuEventGraph(CmdMediator *cmdMediator, const QStringList &pointIdentifiers)
Handle a right click, on a graph point, that was intercepted earlier.
Utility class for generating ordinal numbers.
Command queue stack.
Definition: CmdMediator.h:23
DocumentModelSegments modelSegments() const
Get method for DocumentModelSegments.
Definition: Document.cpp:749
void updateModelSegment(const DocumentModelSegments &modelSegments)
Update this segment given the new settings.
Definition: Segment.cpp:538
QPointF firstPoint() const
Coordinates of first point in Segment.
Definition: Segment.cpp:285
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:33
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.