Engauge Digitizer  2
 All Classes Functions Variables Typedefs Enumerations Friends Pages
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 "GraphicsScene.h"
12 #include "Logger.h"
13 #include "MainWindow.h"
14 #include "OrdinalGenerator.h"
15 #include <QGraphicsPixmapItem>
16 #include <QGraphicsScene>
17 #include <QImage>
18 #include <QSize>
19 #include "Segment.h"
20 #include "SegmentFactory.h"
21 #include "Transformation.h"
22 
25 {
26 }
27 
28 DigitizeStateSegment::~DigitizeStateSegment ()
29 {
30 }
31 
33 {
35 }
36 
38  DigitizeState /* previousState */)
39 {
40  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::begin";
41 
42  m_cmdMediator = cmdMediator; // Save for slotMouseClickOnSegment
43 
44  setCursor(cmdMediator);
45  context().setDragMode(QGraphicsView::NoDrag);
47 
48  handleCurveChange(cmdMediator);
49 }
50 
51 bool DigitizeStateSegment::canPaste (const Transformation &transformation,
52  const QSize &viewSize) const
53 {
54  return canPasteProtected (transformation,
55  viewSize);
56 }
57 
58 QCursor DigitizeStateSegment::cursor(CmdMediator * /* cmdMediator */) const
59 {
60  LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateSegment::cursor";
61 
62  return QCursor (Qt::ArrowCursor);
63 }
64 
66 {
67  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::end";
68 
69  GraphicsScene &scene = context().mainWindow().scene();
70  SegmentFactory segmentFactory (dynamic_cast<QGraphicsScene &> (scene),
71  context().isGnuplot());
72 
73  segmentFactory.clearSegments(m_segments);
74 }
75 
77  const QString &pointIdentifier)
78 {
79  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleContextMenuEventAxis "
80  << " point=" << pointIdentifier.toLatin1 ().data ();
81 }
82 
84  const QStringList &pointIdentifiers)
85 {
86  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment ::handleContextMenuEventGraph "
87  << "points=" << pointIdentifiers.join(",").toLatin1 ().data ();
88 }
89 
91 {
92  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleCurveChange";
93 
94  QImage img = context().mainWindow().imageFiltered();
95 
96  GraphicsScene &scene = context().mainWindow().scene();
97  SegmentFactory segmentFactory (dynamic_cast<QGraphicsScene &> (scene),
98  context().isGnuplot());
99 
100  segmentFactory.clearSegments (m_segments);
101 
102  // Create new segments
103  segmentFactory.makeSegments (img,
104  cmdMediator->document().modelSegments(),
105  m_segments);
106 
107  // Connect signals of the new segments
108  QList<Segment*>::iterator itr;
109  for (itr = m_segments.begin(); itr != m_segments.end(); itr++) {
110  Segment *segment = *itr;
111 
112  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleCurveChange"
113  << " lines=" << segment->lineCount();
114 
115  connect (segment, SIGNAL (signalMouseClickOnSegment (QPointF)), this, SLOT (slotMouseClickOnSegment (QPointF)));
116  }
117 }
118 
120  Qt::Key key,
121  bool /* atLeastOneSelectedItem */)
122 {
123  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleKeyPress"
124  << " key=" << QKeySequence (key).toString ().toLatin1 ().data ();
125 }
126 
128  QPointF /* posScreen */)
129 {
130 // LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateSegment::handleMouseMove";
131 }
132 
134  QPointF /* posScreen */)
135 {
136  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleMousePress";
137 }
138 
140  QPointF /* posScreen */)
141 {
142  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::handleMouseRelease";
143 }
144 
145 Segment *DigitizeStateSegment::segmentFromSegmentStart (const QPointF &posSegmentStart) const
146 {
147  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::segmentFromSegmentStart"
148  << " segments=" << m_segments.count();
149 
150  QList<Segment*>::const_iterator itr;
151  for (itr = m_segments.begin(); itr != m_segments.end(); itr++) {
152  Segment *segment = *itr;
153 
154  if (segment->firstPoint() == posSegmentStart) {
155 
156  return segment;
157  }
158  }
159 
160  LOG4CPP_ERROR_S ((*mainCat)) << "DigitizeStateSegment::segmentFromSegmentStart";
161  ENGAUGE_ASSERT (false);
162  return nullptr;
163 }
164 
166 {
167  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::slotMouseClickOnSegment";
168 
169  Segment *segment = segmentFromSegmentStart (posSegmentStart);
170 
171  // Create single-entry list that is expected by SegmentFactory
172  QList<Segment*> segments;
173  segments.push_back (segment);
174 
175  // Generate point coordinates. Nothing is created in the GraphicsScene at this point
176  GraphicsScene &scene = context().mainWindow().scene();
177  SegmentFactory segmentFactory (dynamic_cast<QGraphicsScene &> (scene),
178  context().isGnuplot());
179 
180  QList<QPoint> points = segmentFactory.fillPoints (m_cmdMediator->document().modelSegments(),
181  segments);
182 
183  // Create one ordinal for each point
184  OrdinalGenerator ordinalGenerator;
185  Document &document = m_cmdMediator->document ();
186  const Transformation &transformation = context ().mainWindow ().transformation();
187  QList<double> ordinals;
188  QList<QPoint>::iterator itr;
189  for (itr = points.begin(); itr != points.end(); itr++) {
190 
191  QPoint point = *itr;
192  ordinals << ordinalGenerator.generateCurvePointOrdinal(document,
193  transformation,
194  point,
195  activeCurve ());
196  }
197 
198  // Create command to add points
199  QUndoCommand *cmd = new CmdAddPointsGraph (context ().mainWindow(),
200  document,
201  context ().mainWindow().selectedGraphCurve(),
202  points,
203  ordinals);
204  context().appendNewCmd(m_cmdMediator,
205  cmd);
206 }
207 
209 {
210  return "DigitizeStateSegment";
211 }
212 
214 {
215  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::updateAfterPointAddition";
216 }
217 
219  const DocumentModelDigitizeCurve & /*modelDigitizeCurve */)
220 {
221  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::updateModelDigitizeCurve";
222 }
223 
225 {
226  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSegment::updateModelSegments";
227 
228  QList<Segment*>::const_iterator itr;
229  for (itr = m_segments.begin(); itr != m_segments.end(); itr++) {
230  Segment *segment = *itr;
231 
232  segment->updateModelSegment (modelSegments);
233  }
234 }
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:380
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
virtual bool canPaste(const Transformation &transformation, const QSize &viewSize) const
Return true if there is good data in the clipboard for pasting, and that is compatible with the curre...
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...
bool canPasteProtected(const Transformation &transformation, const QSize &viewSize) const
Protected version of canPaste method. Some, but not all, leaf classes use this method.
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...
Definition: MainWindow.cpp:834
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:540
QPointF firstPoint() const
Coordinates of first point in Segment.
Definition: Segment.cpp:281
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:36
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.