Engauge Digitizer  2
DigitizeStateSegment.cpp
1 #include "CmdAddPointsGraph.h"
2 #include "DigitizeStateContext.h"
3 #include "DigitizeStateSegment.h"
4 #include "EngaugeAssert.h"
5 #include "Logger.h"
6 #include "MainWindow.h"
7 #include "OrdinalGenerator.h"
8 #include <QGraphicsPixmapItem>
9 #include <QGraphicsScene>
10 #include <QImage>
11 #include "Segment.h"
12 #include "SegmentFactory.h"
13 
16 {
17 }
18 
19 DigitizeStateSegment::~DigitizeStateSegment ()
20 {
21 }
22 
24 {
26 }
27 
28 void DigitizeStateSegment::begin (DigitizeState /* previousState */)
29 {
30  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::begin";
31 
32  setCursor();
33  context().setDragMode(QGraphicsView::NoDrag);
35 
37 }
38 
40 {
41  LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateSegment::cursor";
42 
43  return QCursor (Qt::ArrowCursor);
44 }
45 
47 {
48  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::end";
49 
50  GraphicsScene &scene = context().mainWindow().scene();
51  SegmentFactory segmentFactory ((QGraphicsScene &) scene,
52  context().isGnuplot());
53 
54  segmentFactory.clearSegments(m_segments);
55 }
56 
58 {
59  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleCurveChange";
60 
61  QImage img = context().mainWindow().imageFiltered();
62 
63  GraphicsScene &scene = context().mainWindow().scene();
64  SegmentFactory segmentFactory ((QGraphicsScene &) scene,
65  context().isGnuplot());
66 
67  segmentFactory.clearSegments (m_segments);
68 
69  // Create new segments
70  segmentFactory.makeSegments (img,
71  context().cmdMediator().document().modelSegments(),
72  m_segments);
73 
74  // Connect signals of the new segments
75  QList<Segment*>::iterator itr;
76  for (itr = m_segments.begin(); itr != m_segments.end(); itr++) {
77  Segment *segment = *itr;
78 
79  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleCurveChange"
80  << " lines=" << segment->lineCount();
81 
82  connect (segment, SIGNAL (signalMouseClickOnSegment (QPointF)), this, SLOT (slotMouseClickOnSegment (QPointF)));
83  }
84 }
85 
87  bool /* atLeastOneSelectedItem */)
88 {
89  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleKeyPress"
90  << " key=" << QKeySequence (key).toString ().toLatin1 ().data ();
91 }
92 
93 void DigitizeStateSegment::handleMouseMove (QPointF /* posScreen */)
94 {
95 // LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateSegment::handleMouseMove";
96 }
97 
98 void DigitizeStateSegment::handleMousePress (QPointF /* posScreen */)
99 {
100  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleMousePress";
101 }
102 
103 void DigitizeStateSegment::handleMouseRelease (QPointF /* posScreen */)
104 {
105  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleMouseRelease";
106 }
107 
108 Segment *DigitizeStateSegment::segmentFromSegmentStart (const QPointF &posSegmentStart) const
109 {
110  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::segmentFromSegmentStart"
111  << " segments=" << m_segments.count();
112 
113  QList<Segment*>::const_iterator itr;
114  for (itr = m_segments.begin(); itr != m_segments.end(); itr++) {
115  Segment *segment = *itr;
116 
117  if (segment->firstPoint() == posSegmentStart) {
118 
119  return segment;
120  }
121  }
122 
123  LOG4CPP_ERROR_S ((*mainCat)) << "DigitizeStateSegment::segmentFromSegmentStart";
124  ENGAUGE_ASSERT (false);
125  return 0;
126 }
127 
129 {
130  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::slotMouseClickOnSegment";
131 
132  Segment *segment = segmentFromSegmentStart (posSegmentStart);
133 
134  // Create single-entry list that is expected by SegmentFactory
135  QList<Segment*> segments;
136  segments.push_back (segment);
137 
138  // Generate point coordinates. Nothing is created in the GraphicsScene at this point
139  GraphicsScene &scene = context().mainWindow().scene();
140  SegmentFactory segmentFactory ((QGraphicsScene &) scene,
141  context().isGnuplot());
142 
143  QList<QPoint> points = segmentFactory.fillPoints (context().cmdMediator().document().modelSegments(),
144  segments);
145 
146  // Create one ordinal for each point
147  OrdinalGenerator ordinalGenerator;
148  Document &document = context ().cmdMediator ().document ();
149  const Transformation &transformation = context ().mainWindow ().transformation();
150  QList<double> ordinals;
151  QList<QPoint>::iterator itr;
152  for (itr = points.begin(); itr != points.end(); itr++) {
153 
154  QPoint point = *itr;
155  ordinals << ordinalGenerator.generateCurvePointOrdinal(document,
156  transformation,
157  point,
158  activeCurve ());
159  }
160 
161  // Create command to add points
162  QUndoCommand *cmd = new CmdAddPointsGraph (context ().mainWindow(),
163  document,
164  context ().mainWindow().selectedGraphCurve(),
165  points,
166  ordinals);
167  context().appendNewCmd(cmd);
168 }
169 
171 {
172  return "DigitizeStateSegment";
173 }
174 
176 {
177  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::updateModelDigitizeCurve";
178 }
179 
181 {
182  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::updateModelSegments";
183 
184  QList<Segment*>::const_iterator itr;
185  for (itr = m_segments.begin(); itr != m_segments.end(); itr++) {
186  Segment *segment = *itr;
187 
188  segment->updateModelSegment (modelSegments);
189  }
190 }
void makeSegments(const QImage &imageFiltered, const DocumentModelSegments &modelSegments, QList< Segment * > &segments)
Main entry point for creating all Segments for the filtered image.
virtual QString state() const
State name for debugging.
virtual void handleCurveChange()
Handle the selection of a new curve. At a minimum, DigitizeStateSegment will generate a new set of Se...
virtual void handleMouseRelease(QPointF posScreen)
Handle a mouse release that was intercepted earlier.
virtual void begin(DigitizeState previousState)
Method that is called at the exact moment a state is entered.
CmdMediator & cmdMediator()
Provide CmdMediator for indirect access to the Document.
Transformation transformation() const
Return read-only copy of transformation.
int lineCount() const
Get method for number of lines.
Definition: Segment.cpp:377
void setDragMode(QGraphicsView::DragMode dragMode)
Set QGraphicsView drag mode (in m_view). Called from DigitizeStateAbstractBase subclasses.
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.
Document & document()
Provide the Document to commands, primarily for undo/redo processing.
Definition: CmdMediator.cpp:61
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 handleMousePress(QPointF posScreen)
Handle a mouse press that was intercepted earlier.
Model for DlgSettingsDigitizeCurve and CmdSettingsDigitizeCurve.
Affine transformation between screen and graph coordinates, based on digitized axis points...
virtual void handleMouseMove(QPointF posScreen)
Handle a mouse move. This is part of an experiment to see if augmenting the cursor in Point Match mod...
GraphicsScene & scene()
Scene container for the QImage and QGraphicsItems.
Container for all DigitizeStateAbstractBase subclasses. This functions as the context class in a stan...
void setCursor()
Update the cursor according to the current state.
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.
virtual void updateModelDigitizeCurve(const DocumentModelDigitizeCurve &modelDigitizeCurve)
Update the digitize curve settings.
Storage of one imported image and the data attached to that image.
Definition: Document.h:28
Selectable piecewise-defined line that follows a filtered line in the image.
Definition: Segment.h:15
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.
void appendNewCmd(QUndoCommand *cmd)
Append just-created QUndoCommand to command stack. This is called from DigitizeStateAbstractBase subc...
Utility class for generating ordinal numbers.
void updateModelSegment(const DocumentModelSegments &modelSegments)
Update this segment given the new settings.
Definition: Segment.cpp:531
QPointF firstPoint() const
Coordinates of first point in Segment.
Definition: Segment.cpp:278
Model for DlgSettingsSegments and CmdSettingsSegments.
virtual void handleKeyPress(Qt::Key key, bool atLeastOneSelectedItem)
Handle a key press that was intercepted earlier.
virtual QCursor cursor() const
Returns the state-specific cursor shape.
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:25
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.