Engauge Digitizer  2
DigitizeStateScale.cpp
1 /******************************************************************************************************
2  * (C) 2017 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 "CmdAddScale.h"
8 #include "CmdMediator.h"
9 #include "CursorFactory.h"
10 #include "DigitizeStateScale.h"
11 #include "DigitizeStateContext.h"
12 #include "DlgEditScale.h"
13 #include "Document.h"
14 #include "EngaugeAssert.h"
15 #include "GraphicsPoint.h"
16 #include "GraphicsScene.h"
17 #include "GraphicsView.h"
18 #include "Logger.h"
19 #include "MainWindow.h"
20 #include "PointStyle.h"
21 #include <QCursor>
22 #include <QGraphicsLineItem>
23 #include <QMessageBox>
24 #include "QtToString.h"
25 #include "ZValues.h"
26 
28  DigitizeStateAbstractBase (context),
29  m_temporaryPoint0 (0),
30  m_temporaryPoint1 (0),
31  m_line (0)
32 {
33 }
34 
35 DigitizeStateScale::~DigitizeStateScale ()
36 {
37 }
38 
40 {
41  return AXIS_CURVE_NAME;
42 }
43 
45  DigitizeState /* previousState */)
46 {
47  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateScale::begin";
48 
49  setCursor(cmdMediator);
50  context().setDragMode(QGraphicsView::NoDrag);
52 }
53 
54 QCursor DigitizeStateScale::cursor(CmdMediator *cmdMediator) const
55 {
56  LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateScale::cursor";
57 
58  CursorFactory cursorFactory;
59  QCursor cursor = cursorFactory.generate (cmdMediator->document().modelDigitizeCurve());
60 
61  return cursor;
62 }
63 
65 {
66  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateScale::end";
67 }
68 
70  const QString &pointIdentifier)
71 {
72  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateScale::handleContextMenuEventAxis "
73  << " point=" << pointIdentifier.toLatin1 ().data ();
74 }
75 
77  const QStringList &pointIdentifiers)
78 {
79  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateScale::handleContextMenuEventGraph "
80  << "points=" << pointIdentifiers.join(",").toLatin1 ().data ();
81 }
82 
84 {
85  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateScale::handleCurveChange";
86 }
87 
89  Qt::Key key,
90  bool /* atLeastOneSelectedItem */)
91 {
92  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateScale::handleKeyPress"
93  << " key=" << QKeySequence (key).toString ().toLatin1 ().data ();
94 }
95 
97  QPointF posScreen)
98 {
99  if (m_temporaryPoint1 != 0) {
100 
101  LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateScale::handleMouseMove"
102  << " oldPos=" << QPointFToString (m_temporaryPoint1->pos ()).toLatin1().data()
103  << " newPos=" << QPointFToString (posScreen).toLatin1().data();
104 
105  m_temporaryPoint1->setPos (posScreen);
106 
107  updateLineGeometry();
108  }
109 }
110 
112  QPointF posScreen)
113 {
114  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateScale::handleMousePress";
115 
116  GeometryWindow *NULL_GEOMETRY_WINDOW = 0;
117 
118  // Create the scale bar to give the user immediate feedback that something was created
119  const Curve &curveAxes = cmdMediator->curveAxes();
120  PointStyle pointStyleAxes = curveAxes.curveStyle().pointStyle();
121  m_pointIdentifier0 = Point::temporaryPointIdentifier();
122  m_pointIdentifier1 = m_pointIdentifier0 + "b";
123  m_temporaryPoint0 = context().mainWindow().scene().createPoint(m_pointIdentifier0,
124  pointStyleAxes,
125  posScreen,
126  NULL_GEOMETRY_WINDOW);
127  m_temporaryPoint1 = context().mainWindow().scene().createPoint(m_pointIdentifier1,
128  pointStyleAxes,
129  posScreen,
130  NULL_GEOMETRY_WINDOW);
131 
132  // Subtle stuff happening here. GraphicsPoints by default can receive focus for clicking/dragging,
133  // but for some reason that was causing the dragged second endpoint to jump to the origin the Nth
134  // time that a scale bar was created, for every N>1. So we make the endpoint passive and update
135  // its position manually in handleMouseMove
136  m_temporaryPoint0->setPassive ();
137  m_temporaryPoint1->setPassive ();
138 
139  context().mainWindow().scene().addTemporaryScaleBar (m_temporaryPoint0,
140  m_temporaryPoint1,
141  m_pointIdentifier0,
142  m_pointIdentifier1);
143 
144  m_line = new QGraphicsLineItem;
145  context().mainWindow().scene().addItem (m_line);
146  m_line->setPen (QColor (Qt::red));
147  m_line->setZValue (Z_VALUE_CURVE);
148  m_line->setVisible (true);
149 
150  updateLineGeometry ();
151 
152  // Attempts to select an endpoint right here, or after an super short timer interval
153  // failed. That would have been nice for having the click create the scale bar and, while
154  // the mouse was still pressed, selecting an endpoint thus allowing a single click-and-drag to
155  // create the scale bar. We fall back to the less elegant solution (which the user will never
156  // notice) of capturing mouse move events and using those to move an endpoint
157 }
158 
160  QPointF /* posScreen */)
161 {
162  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateScale::handleMouseRelease";
163 
164  if (context().mainWindow().transformIsDefined()) {
165 
166  QMessageBox::warning (0,
167  QObject::tr ("Engauge Digitizer"),
168  QObject::tr ("The scale bar has been defined, and another is not needed or allowed."));
169 
170  removeTemporaryPointsAndLine ();
171 
172  } else {
173 
174  // Ask user for coordinates
175  DlgEditScale *dlg = new DlgEditScale (context ().mainWindow (),
176  cmdMediator->document().modelCoords(),
177  cmdMediator->document().modelGeneral(),
179  int rtn = dlg->exec ();
180 
181  double scaleLength = dlg->scaleLength ();
182  QPointF posScreen0 = m_temporaryPoint0->pos ();
183  QPointF posScreen1 = m_temporaryPoint1->pos ();
184  delete dlg;
185 
186  removeTemporaryPointsAndLine ();
187 
188  if (rtn == QDialog::Accepted) {
189 
190  // User wants to add this scale point. There are no additional sanity checks to run
191 
192  int nextOrdinal0 = cmdMediator->document().nextOrdinalForCurve(AXIS_CURVE_NAME);
193  int nextOrdinal1 = nextOrdinal0 + 1;
194 
195  // Create command to add point
196  Document &document = cmdMediator->document ();
197  QUndoCommand *cmd = new CmdAddScale (context ().mainWindow(),
198  document,
199  posScreen0,
200  posScreen1,
201  scaleLength,
202  nextOrdinal0,
203  nextOrdinal1);
204  context().appendNewCmd(cmdMediator,
205  cmd);
206  }
207  }
208 }
209 
210 void DigitizeStateScale::removeTemporaryPointsAndLine ()
211 {
212  context().mainWindow().scene().removePoint (m_pointIdentifier0); // Deallocates GraphicsPoint automatically
213  context().mainWindow().scene().removePoint (m_pointIdentifier1); // Deallocates GraphicsPoint automaticall
214  context().mainWindow().scene().removeItem (m_line);
215  delete m_line;
216  m_temporaryPoint0 = 0;
217  m_temporaryPoint1 = 0;
218  m_line = 0;
219 }
220 
222 {
223  return "DigitizeStateScale";
224 }
225 
227 {
228  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateScale::updateAfterPointAddition";
229 }
230 
231 void DigitizeStateScale::updateLineGeometry ()
232 {
233  m_line->setLine (m_temporaryPoint0->pos ().x (),
234  m_temporaryPoint0->pos ().y (),
235  m_temporaryPoint1->pos ().x (),
236  m_temporaryPoint1->pos ().y ());
237 }
238 
240  const DocumentModelDigitizeCurve & /*modelDigitizeCurve */)
241 {
242  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateScale::updateModelDigitizeCurve";
243 
244  setCursor(cmdMediator);
245 }
246 
248 {
249  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateScale::updateModelSegments";
250 }
const Curve & curveAxes() const
See Document::curveAxes.
Definition: CmdMediator.cpp:57
void setPassive()
Prevent automatic focus on point (=make it passive) for scale bar so drags can be made to work proper...
virtual void handleMousePress(CmdMediator *cmdMediator, QPointF posScreen)
Handle a mouse press that was intercepted earlier.
virtual void handleKeyPress(CmdMediator *cmdMediator, Qt::Key key, bool atLeastOneSelectedItem)
Handle a key press that was intercepted earlier.
virtual void handleContextMenuEventAxis(CmdMediator *cmdMediator, const QString &pointIdentifier)
Handle a right click, on an axis point, that was intercepted earlier.
void removePoint(const QString &identifier)
Remove specified point. This aborts if the point does not exist.
QPointF pos() const
Proxy method for QGraphicsItem::pos.
void setDragMode(QGraphicsView::DragMode dragMode)
Set QGraphicsView drag mode (in m_view). Called from DigitizeStateAbstractBase subclasses.
Create standard cross cursor, or custom cursor, according to settings.
Definition: CursorFactory.h:15
int nextOrdinalForCurve(const QString &curveName) const
Default next ordinal value for specified curve.
Definition: Document.cpp:763
Dialog box for editing the information of the map scale.
Definition: DlgEditScale.h:22
PointStyle pointStyle() const
Get method for PointStyle.
Definition: CurveStyle.cpp:75
virtual void updateModelDigitizeCurve(CmdMediator *cmdMediator, const DocumentModelDigitizeCurve &modelDigitizeCurve)
Update the digitize curve settings.
void updateViewsOfSettings(const QString &activeCurve)
Update curve-specific view of settings. Private version gets active curve name from DigitizeStateCont...
DocumentModelGeneral modelGeneral() const
Get method for DocumentModelGeneral.
Definition: Document.cpp:721
void addTemporaryScaleBar(GraphicsPoint *point0, GraphicsPoint *point1, const QString &pointIdentifier0, const QString &pointIdentifier1)
Add temporary scale bar to scene.
Window that displays the geometry information, as a table, for the current curve. ...
void setPos(const QPointF pos)
Update the position.
virtual void handleMouseRelease(CmdMediator *cmdMediator, QPointF posScreen)
Handle a mouse release that was intercepted earlier.
Document & document()
Provide the Document to commands, primarily for undo/redo processing.
Definition: CmdMediator.cpp:72
DigitizeStateContext & context()
Reference to the DigitizeStateContext that contains all the DigitizeStateAbstractBase subclasses...
virtual void handleCurveChange(CmdMediator *cmdMediator)
Handle the selection of a new curve. At a minimum, DigitizeStateSegment will generate a new set of Se...
MainWindow & mainWindow()
Reference to the MainWindow, without const.
static QString temporaryPointIdentifier()
Point identifier for temporary point that is used by DigitzeStateAxis.
Definition: Point.cpp:507
virtual void handleContextMenuEventGraph(CmdMediator *cmdMediator, const QStringList &pointIdentifiers)
Handle a right click, on a graph point, that was intercepted earlier.
virtual QString state() const
State name for debugging.
GraphicsPoint * createPoint(const QString &identifier, const PointStyle &pointStyle, const QPointF &posScreen, GeometryWindow *geometryWindow)
Create one QGraphicsItem-based object that represents one Point. It is NOT added to m_graphicsLinesFo...
DocumentModelDigitizeCurve modelDigitizeCurve() const
Get method for DocumentModelDigitizeCurve.
Definition: Document.cpp:707
Model for DlgSettingsDigitizeCurve and CmdSettingsDigitizeCurve.
Details for a specific Point.
Definition: PointStyle.h:20
virtual QString activeCurve() const
Name of the active Curve. This can include AXIS_CURVE_NAME.
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 updateAfterPointAddition()
Update graphics attributes after possible new points. This is useful for highlight opacity...
Storage of one imported image and the data attached to that image.
Definition: Document.h:41
Container for one set of digitized Points.
Definition: Curve.h:33
virtual QCursor cursor(CmdMediator *cmdMediator) const
Returns the state-specific cursor shape.
MainWindowModel modelMainWindow() const
Get method for main window model.
Command for adding one scale point.
Definition: CmdAddScale.h:16
virtual void begin(CmdMediator *cmdMediator, DigitizeState previousState)
Method that is called at the exact moment a state is entered.
Command queue stack.
Definition: CmdMediator.h:23
Model for DlgSettingsSegments and CmdSettingsSegments.
CurveStyle curveStyle() const
Return the curve style.
Definition: Curve.cpp:148
Base class for all digitizing states. This serves as an interface to DigitizeStateContext.
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 end()
Method that is called at the exact moment a state is exited. Typically called just before begin for t...
DigitizeStateScale(DigitizeStateContext &context)
Single constructor.
virtual void updateModelSegments(const DocumentModelSegments &modelSegments)
Update the segments given the new settings.
double scaleLength() const
Return the scale bar length specified by the user. Only applies if dialog was accepted.
QCursor generate(const DocumentModelDigitizeCurve &modelDigitizeCurve) const
Factory method to generate standard or custom cursor.
DocumentModelCoords modelCoords() const
Get method for DocumentModelCoords.
Definition: Document.cpp:693