Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
GraphicsPoint.cpp
1 #include "CurveStyle.h"
2 #include "DataKey.h"
3 #include "EnumsToQt.h"
4 #include "GraphicsItemType.h"
5 #include "GraphicsPoint.h"
6 #include "GraphicsPointEllipse.h"
7 #include "GraphicsPointPolygon.h"
8 #include "Logger.h"
9 #include "PointStyle.h"
10 #include <QGraphicsEllipseItem>
11 #include <QGraphicsPolygonItem>
12 #include <QGraphicsScene>
13 #include <QGraphicsSceneContextMenuEvent>
14 #include <QPen>
15 #include <QTextStream>
16 #include "QtToString.h"
17 
18 const double ZERO_WIDTH = 0.0;
19 const double Z_VALUE = 100.0; // Put on top of Segments in DlgSettingsSegments
20 
21 GraphicsPoint::GraphicsPoint(QGraphicsScene &scene,
22  const QString &identifier,
23  const QPointF &posScreen,
24  const QColor &color,
25  unsigned int radius,
26  double lineWidth) :
28  m_scene (scene),
29  m_graphicsItemEllipse (0),
30  m_shadowZeroWidthEllipse (0),
31  m_graphicsItemPolygon (0),
32  m_shadowZeroWidthPolygon (0),
33  m_identifier (identifier),
34  m_posScreen (posScreen),
35  m_color (color),
36  m_lineWidth (lineWidth),
37  m_wanted (true)
38 {
39  LOG4CPP_INFO_S ((*mainCat)) << "GraphicsPoint::GraphicsPoint"
40  << " identifier=" << identifier.toLatin1 ().data ();
41 
42  createPointEllipse (radius);
43 }
44 
45 GraphicsPoint::GraphicsPoint(QGraphicsScene &scene,
46  const QString &identifier,
47  const QPointF &posScreen,
48  const QColor &color,
49  const QPolygonF &polygon,
50  double lineWidth) :
52  m_scene (scene),
53  m_graphicsItemEllipse (0),
54  m_shadowZeroWidthEllipse (0),
55  m_graphicsItemPolygon (0),
56  m_shadowZeroWidthPolygon (0),
57  m_identifier (identifier),
58  m_posScreen (posScreen),
59  m_color (color),
60  m_lineWidth (lineWidth),
61  m_wanted (true)
62 {
63  LOG4CPP_INFO_S ((*mainCat)) << "GraphicsPoint::GraphicsPoint "
64  << " identifier=" << identifier.toLatin1 ().data ();
65 
66  createPointPolygon (polygon);
67 }
68 
70 {
71  LOG4CPP_INFO_S ((*mainCat)) << "GraphicsPoint::~GraphicsPoint";
72 
73  if (m_graphicsItemEllipse == 0) {
74 
75  QGraphicsScene *scene = m_graphicsItemPolygon->scene();
76 
77  // Since m_shadowZeroWidthPolygon is a child of m_graphicsItemPolygon, removing the parent removes both
78  scene->removeItem (m_graphicsItemPolygon);
79  delete m_graphicsItemPolygon;
80  m_graphicsItemPolygon = 0;
81  m_shadowZeroWidthPolygon = 0;
82 
83 
84  } else {
85 
86  QGraphicsScene *scene = m_graphicsItemEllipse->scene();
87 
88  // Since m_shadowZeroWidthEllipse is a child of m_graphicsItemEllipse, removing the parent removes both
89  scene->removeItem (m_graphicsItemEllipse);
90  delete m_graphicsItemEllipse;
91  m_graphicsItemEllipse = 0;
92  m_shadowZeroWidthEllipse = 0;
93 
94  }
95 }
96 
97 void GraphicsPoint::createPointEllipse (unsigned int radius)
98 {
99  LOG4CPP_INFO_S ((*mainCat)) << "GraphicsPoint::createPointEllipse";
100 
101  const int radiusSigned = radius; // Radius must be signed before multiplying by -1 below, for Visual Studio
102  m_graphicsItemEllipse = new GraphicsPointEllipse (*this,
103  QRect (- radiusSigned,
104  - radiusSigned,
105  2 * radiusSigned + 1,
106  2 * radiusSigned + 1));
107  m_scene.addItem (m_graphicsItemEllipse);
108 
109  m_graphicsItemEllipse->setZValue (Z_VALUE);
110  m_graphicsItemEllipse->setData (DATA_KEY_IDENTIFIER, m_identifier);
111  m_graphicsItemEllipse->setData (DATA_KEY_GRAPHICS_ITEM_TYPE, GRAPHICS_ITEM_TYPE_POINT);
112  m_graphicsItemEllipse->setPos (m_posScreen.x (),
113  m_posScreen.y ());
114  m_graphicsItemEllipse->setPen (QPen (QBrush (m_color), m_lineWidth));
115  m_graphicsItemEllipse->setEnabled (true);
116  m_graphicsItemEllipse->setFlags (QGraphicsItem::ItemIsSelectable |
117  QGraphicsItem::ItemIsMovable |
118  QGraphicsItem::ItemSendsGeometryChanges);
119 
120  m_graphicsItemEllipse->setToolTip (m_identifier);
121  m_graphicsItemEllipse->setData (DATA_KEY_GRAPHICS_ITEM_TYPE, GRAPHICS_ITEM_TYPE_POINT);
122 
123  // Shadow item is not selectable so it needs no stored data. Do NOT
124  // call QGraphicsScene::addItem since the QGraphicsItem::setParentItem call adds the item
125  m_shadowZeroWidthEllipse = new GraphicsPointEllipse (*this,
126  QRect (- radiusSigned,
127  - radiusSigned,
128  2 * radiusSigned + 1,
129  2 * radiusSigned + 1));
130  m_shadowZeroWidthEllipse->setParentItem(m_graphicsItemPolygon); // Dragging parent also drags child
131 
132  m_shadowZeroWidthEllipse->setPen (QPen (QBrush (m_color), ZERO_WIDTH));
133  m_shadowZeroWidthEllipse->setEnabled (true);
134 }
135 
136 void GraphicsPoint::createPointPolygon (const QPolygonF &polygon)
137 {
138  LOG4CPP_INFO_S ((*mainCat)) << "GraphicsPoint::createPointPolygon";
139 
140  m_graphicsItemPolygon = new GraphicsPointPolygon (*this,
141  polygon);
142  m_scene.addItem (m_graphicsItemPolygon);
143 
144  m_graphicsItemPolygon->setZValue (Z_VALUE);
145  m_graphicsItemPolygon->setData (DATA_KEY_IDENTIFIER, m_identifier);
146  m_graphicsItemPolygon->setData (DATA_KEY_GRAPHICS_ITEM_TYPE, GRAPHICS_ITEM_TYPE_POINT);
147  m_graphicsItemPolygon->setPos (m_posScreen.x (),
148  m_posScreen.y ());
149  m_graphicsItemPolygon->setPen (QPen (QBrush (m_color), m_lineWidth));
150  m_graphicsItemPolygon->setEnabled (true);
151  m_graphicsItemPolygon->setFlags (QGraphicsItem::ItemIsSelectable |
152  QGraphicsItem::ItemIsMovable |
153  QGraphicsItem::ItemSendsGeometryChanges);
154 
155  m_graphicsItemPolygon->setToolTip (m_identifier);
156  m_graphicsItemPolygon->setData (DATA_KEY_GRAPHICS_ITEM_TYPE, GRAPHICS_ITEM_TYPE_POINT);
157 
158  // Shadow item is not selectable so it needs no stored data. Do NOT
159  // call QGraphicsScene::addItem since the QGraphicsItem::setParentItem call adds the item
160  m_shadowZeroWidthPolygon = new GraphicsPointPolygon (*this,
161  polygon);
162  m_shadowZeroWidthPolygon->setParentItem(m_graphicsItemPolygon); // Dragging parent also drags child
163 
164  m_shadowZeroWidthPolygon->setPen (QPen (QBrush (m_color), ZERO_WIDTH));
165  m_shadowZeroWidthPolygon->setEnabled (true);
166 }
167 
168 QVariant GraphicsPoint::data (int key) const
169 {
170  if (m_graphicsItemEllipse == 0) {
171  return m_graphicsItemPolygon->data (key);
172  } else {
173  return m_graphicsItemEllipse->data (key);
174  }
175 }
176 
177 QPointF GraphicsPoint::pos () const
178 {
179  if (m_graphicsItemEllipse == 0) {
180  return m_graphicsItemPolygon->pos ();
181  } else {
182  return m_graphicsItemEllipse->pos ();
183  }
184 }
185 
186 void GraphicsPoint::printStream (QString indentation,
187  QTextStream &str,
188  double ordinalKey) const
189 {
190  str << indentation << "GraphicsPoint\n";
191 
192  indentation += INDENTATION_DELTA;
193 
194  QString identifier;
195  QString pointType;
196  QPointF pos;
197  if (m_graphicsItemEllipse == 0) {
198  identifier = m_graphicsItemPolygon->data (DATA_KEY_IDENTIFIER).toString ();
199  pointType = "polygon";
200  pos = m_graphicsItemPolygon->pos();
201  } else {
202  identifier = m_graphicsItemEllipse->data (DATA_KEY_IDENTIFIER).toString ();
203  pointType = "ellipse";
204  pos = m_graphicsItemEllipse->pos();
205  }
206 
207  DataKey type = (DataKey) data (DATA_KEY_GRAPHICS_ITEM_TYPE).toInt();
208 
209  str << indentation << identifier
210  << " ordinalKey=" << ordinalKey
211  << " dataIdentifier=" << data (DATA_KEY_IDENTIFIER).toString().toLatin1().data()
212  << " dataType=" << dataKeyToString (type).toLatin1().data()
213  << " " << pointType << "Pos=" << QPointFToString (pos) << "\n";
214 }
215 
217 {
218  m_wanted = false;
219 }
220 
221 void GraphicsPoint::setData (int key, const QVariant &data)
222 {
223  LOG4CPP_INFO_S ((*mainCat)) << "GraphicsPoint::setData"
224  << " key=" << dataKeyToString ((DataKey) key).toLatin1().data()
225  << " data=" << data.toString().toLatin1().data();
226 
227  if (m_graphicsItemEllipse == 0) {
228  m_graphicsItemPolygon->setData (key, data);
229  } else {
230  m_graphicsItemEllipse->setData (key, data);
231  }
232 }
233 
235 {
236  // Setting pen and radius of parent graphics items below also affects the child shadows
237  // (m_shadowItemPolygon and m_shadowItemEllipse)
238  if (m_graphicsItemEllipse == 0) {
239  if (pointStyle.shape() == POINT_SHAPE_CIRCLE) {
240 
241  // Transition from non-circle to circle. Deleting parent also deletes child shadow
242  delete m_graphicsItemPolygon;
243  m_graphicsItemPolygon = 0;
244  m_shadowZeroWidthPolygon = 0;
245 
246  createPointEllipse (pointStyle.radius());
247 
248  } else {
249 
250  // Update polygon
251  m_graphicsItemPolygon->setPen (QPen (ColorPaletteToQColor(pointStyle.paletteColor()),
252  pointStyle.lineWidth()));
253  m_shadowZeroWidthPolygon->setPen (QPen (ColorPaletteToQColor(pointStyle.paletteColor()),
254  pointStyle.lineWidth()));
255  m_graphicsItemPolygon->setPolygon (pointStyle.polygon());
256  m_shadowZeroWidthPolygon->setPolygon (pointStyle.polygon());
257 
258  }
259  } else {
260  if (pointStyle.shape() != POINT_SHAPE_CIRCLE) {
261 
262  // Transition from circle to non-circlee. Deleting parent also deletes child shadow
263  delete m_graphicsItemEllipse;
264  m_graphicsItemEllipse = 0;
265  m_shadowZeroWidthEllipse = 0;
266 
267  createPointPolygon (pointStyle.polygon());
268 
269  } else {
270 
271  // Update circle
272  m_graphicsItemEllipse->setPen (QPen (ColorPaletteToQColor(pointStyle.paletteColor()),
273  pointStyle.lineWidth()));
274  m_shadowZeroWidthEllipse->setPen (QPen (ColorPaletteToQColor(pointStyle.paletteColor()),
275  pointStyle.lineWidth()));
276  m_graphicsItemEllipse->setRadius (pointStyle.radius());
277  m_shadowZeroWidthEllipse->setRadius (pointStyle.radius());
278  }
279  }
280 }
281 
282 void GraphicsPoint::setPos (const QPointF pos)
283 {
284  if (m_graphicsItemEllipse == 0) {
285  return m_graphicsItemPolygon->setPos (pos);
286  } else {
287  return m_graphicsItemEllipse->setPos (pos);
288  }
289 }
290 
291 void GraphicsPoint::setToolTip (const QString &toolTip)
292 {
293  if (m_graphicsItemEllipse == 0) {
294  m_graphicsItemPolygon->setToolTip (toolTip);
295  } else {
296  m_graphicsItemEllipse->setToolTip (toolTip);
297  }
298 }
299 
301 {
302  m_wanted = true;
303 }
304 
306 {
307  setPointStyle (curveStyle.pointStyle()); // This point
308 }
309 
311 {
312  return m_wanted;
313 }
void setWanted()
Mark point as wanted. Marking as unwanted is done by the reset function.
int lineWidth() const
Get method for line width.
Definition: PointStyle.cpp:77
void printStream(QString indentation, QTextStream &str, double ordinalKey) const
Debugging method that supports print method of this class and printStream method of some other class(...
QPolygonF polygon() const
Return the polygon for creating a QGraphicsPolygonItem. The size is determined by the radius...
Definition: PointStyle.cpp:113
Base class for adding identifiers to graphics items that represent Points.
PointStyle pointStyle() const
Get method for PointStyle.
Definition: CurveStyle.cpp:69
void setData(int key, const QVariant &data)
Proxy method for QGraphicsItem::setData.
void setPos(const QPointF pos)
Update the position.
bool wanted() const
Identify point as wanted//unwanted.
void setPointStyle(const PointStyle &pointStyle)
Update the point style.
void updateCurveStyle(const CurveStyle &curveStyle)
Update point and line styles that comprise the curve style.
This class add event handling to QGraphicsEllipseItem.
Details for a specific Point.
Definition: PointStyle.h:14
~GraphicsPoint()
Destructor. This remove the graphics item from the scene.
ColorPalette paletteColor() const
Get method for point color.
Definition: PointStyle.cpp:108
Container for LineStyle and PointStyle for one Curve.
Definition: CurveStyle.h:12
int radius() const
Radius of point. For a circle this is all that is needed to draw a circle. For a polygon, the radius determines the size of the polygon.
Definition: PointStyle.cpp:211
This class add event handling to QGraphicsPolygonItem.
QPointF pos() const
Proxy method for QGraphicsItem::pos.
GraphicsPoint(QGraphicsScene &scene, const QString &identifier, const QPointF &posScreen, const QColor &color, unsigned int radius, double lineWidth)
Constructor of circular point.
QVariant data(int key) const
Proxy method for QGraphicsItem::data.
void setToolTip(const QString &toolTip)
Proxy method for QGraphicsItem::setToolTip.
PointShape shape() const
Get method for point shape.
Definition: PointStyle.cpp:250
void setRadius(int radius)
Update the radius.
void reset()
Mark point as unwanted, and unbind any bound lines.