Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
DigitizeStateSelect.cpp
1 #include "CmdMediator.h"
2 #include "CmdMoveBy.h"
3 #include "DataKey.h"
4 #include "DigitizeStateContext.h"
5 #include "DigitizeStateSelect.h"
6 #include "EngaugeAssert.h"
7 #include "GraphicsItemType.h"
8 #include "GraphicsScene.h"
9 #include "GraphicsView.h"
10 #include "Logger.h"
11 #include "MainWindow.h"
12 #include <QCursor>
13 #include <QGraphicsItem>
14 #include <QImage>
15 #include <QtToString.h>
16 
17 const QString MOVE_TEXT_DOWN ("Move down");
18 const QString MOVE_TEXT_LEFT ("Move left");
19 const QString MOVE_TEXT_RIGHT ("Move right");
20 const QString MOVE_TEXT_UP ("Move up");
21 
24 {
25 }
26 
27 DigitizeStateSelect::~DigitizeStateSelect ()
28 {
29 }
30 
32 {
34 }
35 
36 void DigitizeStateSelect::begin (DigitizeState /* previousState */)
37 {
38  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::begin";
39 
40  setCursor();
41  context().setDragMode(QGraphicsView::RubberBandDrag);
42 
43  setCursorForPoints ();
45 }
46 
48 {
49  LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateSelect::cursor";
50 
51  return QCursor (Qt::ArrowCursor);
52 }
53 
55 {
56  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::end";
57 
58  unsetCursorForPoints ();
59 }
60 
62 {
63  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::handleCurveChange";
64 }
65 
67  bool atLeastOneSelectedItem)
68 {
69  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::handleKeyPress"
70  << " key=" << QKeySequence (key).toString ().toLatin1 ().data ();
71 
72  if (atLeastOneSelectedItem) {
73 
74  if (key == Qt::Key_Down ||
75  key == Qt::Key_Up ||
76  key == Qt::Key_Left ||
77  key == Qt::Key_Right) {
78 
79  keyPressArrow (key);
80 
81  }
82  }
83 }
84 
85 void DigitizeStateSelect::handleMouseMove (QPointF /* posScreen */)
86 {
87 // LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateSelect::handleMouseMove";
88 }
89 
90 void DigitizeStateSelect::handleMousePress (QPointF posScreen)
91 {
92  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::handleMousePress"
93  << " posScreen=" << QPointFToString (posScreen).toLatin1 ().data ();
94 
95  // Note that GraphicsView has already called GraphicsPointAbstract::resetPositionHasChanged on all items
96 
97  m_movingStart = posScreen;
98 }
99 
101 {
102  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::handleMouseRelease"
103  << " posScreen=" << QPointFToString (posScreen).toLatin1 ().data ();
104 
105  QPointF deltaScreen = posScreen - m_movingStart;
106  QStringList positionHasChangedIdentifers = context().mainWindow().scene().positionHasChangedPointIdentifiers();
107 
108  bool positionHasChanged = (positionHasChangedIdentifers.count () > 0);
109 
110  if (positionHasChanged && (
111  deltaScreen.x () != 0 ||
112  deltaScreen.y () != 0)) {
113 
114  QString moveText = moveTextFromDeltaScreen (deltaScreen);
115 
116  // Create command to move points
117  CmdMoveBy *cmd = new CmdMoveBy (context().mainWindow(),
118  context().cmdMediator().document(),
119  deltaScreen,
120  moveText,
121  positionHasChangedIdentifers);
122  context().appendNewCmd (cmd);
123 
124  } else {
125 
126  // Selection probably changed so update the MainWindow controls (especially Cut)
128 
129  }
130 }
131 
132 void DigitizeStateSelect::keyPressArrow (Qt::Key key)
133 {
134  QPointF deltaScreen;
135  QString moveText;
136  switch (key) {
137  case Qt::Key_Down:
138  deltaScreen = QPointF (0, zoomedToUnzoomedScreenY ());
139  moveText = MOVE_TEXT_DOWN;
140  break;
141 
142  case Qt::Key_Left:
143  deltaScreen = QPointF (-1 * zoomedToUnzoomedScreenX (), 0);
144  moveText = MOVE_TEXT_LEFT;
145  break;
146 
147  case Qt::Key_Right:
148  deltaScreen = QPointF (zoomedToUnzoomedScreenX (), 0);
149  moveText = MOVE_TEXT_RIGHT;
150  break;
151 
152  case Qt::Key_Up:
153  deltaScreen = QPointF (0, -1 * zoomedToUnzoomedScreenY ());
154  moveText = MOVE_TEXT_UP;
155  break;
156 
157  default:
158  ENGAUGE_ASSERT (false);
159  }
160 
161  // Create command to move points
162  CmdMoveBy *cmd = new CmdMoveBy (context().mainWindow(),
163  context().cmdMediator ().document(),
164  deltaScreen,
165  moveText,
166  context().mainWindow().scene ().selectedPointIdentifiers ());
167  context().appendNewCmd (cmd);
168 }
169 
170 QString DigitizeStateSelect::moveTextFromDeltaScreen (const QPointF &deltaScreen)
171 {
172  QString moveText;
173 
174  // x UP x -----> +x
175  // x x |
176  // LEFT x RIGHT |
177  // x x v
178  // x DOWN x +y
179  bool downOrRight = (deltaScreen.y () > -1.0 * deltaScreen.x ());
180  bool upOrRight = (deltaScreen.y () < deltaScreen.x ());
181  if (downOrRight && upOrRight) {
182  moveText = MOVE_TEXT_RIGHT;
183  } else if (downOrRight && !upOrRight) {
184  moveText = MOVE_TEXT_DOWN;
185  } else if (!downOrRight && upOrRight) {
186  moveText = MOVE_TEXT_UP;
187  } else {
188  moveText = MOVE_TEXT_LEFT;
189  }
190 
191  return moveText;
192 }
193 
194 void DigitizeStateSelect::setCursorForPoints()
195 {
196  QCursor cursor (Qt::OpenHandCursor);
197 
198  QList<QGraphicsItem*> items = context().mainWindow().scene().items();
199  QList<QGraphicsItem*>::iterator itr;
200  for (itr = items.begin (); itr != items.end (); itr++) {
201 
202  QGraphicsItem *item = *itr;
203  if (item->data (DATA_KEY_GRAPHICS_ITEM_TYPE) == GRAPHICS_ITEM_TYPE_POINT) {
204  item->setCursor (cursor);
205  }
206  }
207 }
208 
210 {
211  return "DigitizeStateSelect";
212 }
213 
214 void DigitizeStateSelect::unsetCursorForPoints()
215 {
216  QList<QGraphicsItem*> items = context().mainWindow().scene().items();
217  QList<QGraphicsItem*>::iterator itr;
218  for (itr = items.begin (); itr != items.end (); itr++) {
219 
220  QGraphicsItem *item = *itr;
221  if (item->data (DATA_KEY_GRAPHICS_ITEM_TYPE) == GRAPHICS_ITEM_TYPE_POINT) {
222  item->unsetCursor ();
223  }
224  }
225 }
226 
228 {
229  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::updateModelDigitizeCurve";
230 }
231 
233 {
234  LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::updateModelSegments";
235 }
236 
237 double DigitizeStateSelect::zoomedToUnzoomedScreenX () const
238 {
239  double m11 = context().mainWindow ().view ().transform().m11 ();
240  return 1.0 / m11;
241 }
242 
243 double DigitizeStateSelect::zoomedToUnzoomedScreenY () const
244 {
245  double m22 = context().mainWindow ().view ().transform().m22 ();
246  return 1.0 / m22;
247 }
virtual QCursor cursor() const
Returns the state-specific cursor shape.
virtual void handleMousePress(QPointF posScreen)
Handle a mouse press that was intercepted earlier.
void setDragMode(QGraphicsView::DragMode dragMode)
Set QGraphicsView drag mode (in m_view). Called from DigitizeStateAbstractBase subclasses.
virtual void handleCurveChange()
Handle the selection of a new curve. At a minimum, DigitizeStateSegment will generate a new set of Se...
virtual void updateModelSegments(const DocumentModelSegments &modelSegments)
Update the segments given the new settings.
virtual void handleMouseRelease(QPointF posScreen)
Handle a mouse release that was intercepted earlier.
void updateAfterMouseRelease()
Call MainWindow::updateControls (which is private) after the very specific case - a mouse press/relea...
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.
Command for moving all selected Points by a specified translation.
Definition: CmdMoveBy.h:12
DigitizeStateContext & context()
Reference to the DigitizeStateContext that contains all the DigitizeStateAbstractBase subclasses...
MainWindow & mainWindow()
Reference to the MainWindow, without const.
virtual void handleKeyPress(Qt::Key key, bool atLeastOneSelectedItem)
Handle a key press that was intercepted earlier.
Model for DlgSettingsDigitizeCurve and CmdSettingsDigitizeCurve.
GraphicsView & view()
View for the QImage and QGraphicsItems, without const.
QStringList positionHasChangedPointIdentifiers() const
Return a list of identifiers for the points that have moved since the last call to resetPositionHasCh...
GraphicsScene & scene()
Scene container for the QImage and QGraphicsItems.
virtual void updateModelDigitizeCurve(const DocumentModelDigitizeCurve &modelDigitizeCurve)
Update the digitize curve settings.
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 QString activeCurve() const
Name of the active Curve. This can include AXIS_CURVE_NAME.
virtual void end()
Method that is called at the exact moment a state is exited. Typically called just before begin for t...
virtual void begin(DigitizeState previousState)
Method that is called at the exact moment a state is entered.
DigitizeStateSelect(DigitizeStateContext &context)
Single constructor.
void appendNewCmd(QUndoCommand *cmd)
Append just-created QUndoCommand to command stack. This is called from DigitizeStateAbstractBase subc...
Model for DlgSettingsSegments and CmdSettingsSegments.
Base class for all digitizing states. This serves as an interface to DigitizeStateContext.
virtual QString state() const
State name for debugging.
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...