Engauge Digitizer  2
PointStyle.cpp
1 #include "DocumentSerialize.h"
2 #include "EngaugeAssert.h"
3 #include "Logger.h"
4 #include "PointStyle.h"
5 #include <qmath.h>
6 #include <QTextStream>
7 #include <QtToString.h>
8 #include <QXmlStreamWriter>
9 #include "Xml.h"
10 
11 const int DEFAULT_POINT_RADIUS = 10;
12 const int DEFAULT_LINE_WIDTH = 1;
13 const double PI = 3.1415926535;
14 const double TWO_PI = 2.0 * PI;
15 
17 {
18 }
19 
20 PointStyle::PointStyle(PointShape shape,
21  unsigned int radius,
22  int lineWidth,
23  ColorPalette paletteColor) :
24  m_shape (shape),
25  m_radius (radius),
26  m_lineWidth (lineWidth),
27  m_paletteColor (paletteColor)
28 {
29 }
30 
32  m_shape (other.shape()),
33  m_radius (other.radius ()),
34  m_lineWidth (other.lineWidth ()),
35  m_paletteColor (other.paletteColor ())
36 {
37 }
38 
40 {
41  m_shape = other.shape ();
42  m_radius = other.radius ();
43  m_lineWidth = other.lineWidth ();
44  m_paletteColor = other.paletteColor ();
45 
46  return *this;
47 }
48 
50 {
51  return PointStyle (POINT_SHAPE_CROSS,
52  DEFAULT_POINT_RADIUS,
53  DEFAULT_LINE_WIDTH,
54  COLOR_PALETTE_RED);
55 }
56 
58 {
59  PointShape shape = POINT_SHAPE_CROSS;
60  static PointShape pointShapes [] = {POINT_SHAPE_CROSS,
61  POINT_SHAPE_X,
62  POINT_SHAPE_DIAMOND,
63  POINT_SHAPE_SQUARE};
64  shape = pointShapes [index % 4];
65 
66  return PointStyle (shape,
67  DEFAULT_POINT_RADIUS,
68  DEFAULT_LINE_WIDTH,
69  COLOR_PALETTE_BLUE);
70 }
71 
72 bool PointStyle::isCircle () const
73 {
74  return m_shape == POINT_SHAPE_CIRCLE;
75 }
76 
78 {
79  return m_lineWidth;
80 }
81 
82 void PointStyle::loadXml(QXmlStreamReader &reader)
83 {
84  LOG4CPP_INFO_S ((*mainCat)) << "PointStyle::loadXml";
85 
86  QXmlStreamAttributes attributes = reader.attributes();
87 
88  if (attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_STYLE_RADIUS) &&
89  attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_STYLE_LINE_WIDTH) &&
90  attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_STYLE_COLOR) &&
91  attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_STYLE_SHAPE)) {
92 
93  setRadius (attributes.value(DOCUMENT_SERIALIZE_POINT_STYLE_RADIUS).toInt());
94  setLineWidth (attributes.value(DOCUMENT_SERIALIZE_POINT_STYLE_LINE_WIDTH).toInt());
95  setPaletteColor ((ColorPalette) attributes.value(DOCUMENT_SERIALIZE_POINT_STYLE_COLOR).toInt());
96  setShape ((PointShape) attributes.value(DOCUMENT_SERIALIZE_POINT_STYLE_SHAPE).toInt());
97 
98  // Read until end of this subtree
99  while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
100  (reader.name() != DOCUMENT_SERIALIZE_POINT_STYLE)){
101  loadNextFromReader(reader);
102  }
103  } else {
104  reader.raiseError ("Cannot read point style data");
105  }
106 }
107 
108 ColorPalette PointStyle::paletteColor () const
109 {
110  return m_paletteColor;
111 }
112 
113 QPolygonF PointStyle::polygon () const
114 {
115  const int NUM_XY = 60;
116  QVector<QPointF> points;
117 
118  switch (m_shape) {
119 
120  case POINT_SHAPE_CIRCLE:
121  {
122  int xyWidth = m_radius;
123  for (int i = 0; i <= NUM_XY; i++) {
124  double angle = TWO_PI * (double) i / (double) NUM_XY;
125  double x = xyWidth * cos (angle);
126  double y = xyWidth * sin (angle);
127  points.append (QPointF (x, y));
128  }
129  }
130  break;
131 
132  case POINT_SHAPE_CROSS:
133  {
134  int xyWidth = m_radius;
135 
136  points.append (QPointF (-1 * xyWidth, 0));
137  points.append (QPointF (xyWidth, 0));
138  points.append (QPointF (0, 0));
139  points.append (QPointF (0, xyWidth));
140  points.append (QPointF (0, -1 * xyWidth));
141  points.append (QPointF (0, 0));
142  }
143  break;
144 
145  case POINT_SHAPE_DIAMOND:
146  {
147  int xyWidth = m_radius;
148 
149  points.append (QPointF (0, -1 * xyWidth));
150  points.append (QPointF (-1 * xyWidth, 0));
151  points.append (QPointF (0, xyWidth));
152  points.append (QPointF (xyWidth, 0));
153  }
154  break;
155 
156  case POINT_SHAPE_SQUARE:
157  {
158  int xyWidth = m_radius;
159 
160  points.append (QPointF (-1 * xyWidth, -1 * xyWidth));
161  points.append (QPointF (-1 * xyWidth, xyWidth));
162  points.append (QPointF (xyWidth, xyWidth));
163  points.append (QPointF (xyWidth, -1 * xyWidth));
164  }
165  break;
166 
167  case POINT_SHAPE_TRIANGLE:
168  {
169  int xyWidth = m_radius;
170 
171  points.append (QPointF (-1 * xyWidth, -1 * xyWidth));
172  points.append (QPointF (0, xyWidth));
173  points.append (QPointF (xyWidth, -1 * xyWidth));
174  }
175  break;
176 
177  case POINT_SHAPE_X:
178  {
179  int xyWidth = m_radius * qSqrt (0.5);
180 
181  points.append (QPointF (-1 * xyWidth, -1 * xyWidth));
182  points.append (QPointF (xyWidth, xyWidth));
183  points.append (QPointF (0, 0));
184  points.append (QPointF (-1 * xyWidth, xyWidth));
185  points.append (QPointF (xyWidth, -1 * xyWidth));
186  points.append (QPointF (0, 0));
187  }
188  break;
189 
190  default:
191  ENGAUGE_ASSERT (false);
192  }
193 
194  QPolygonF polygon (points);
195  return polygon;
196 }
197 
198 void PointStyle::printStream(QString indentation,
199  QTextStream &str) const
200 {
201  str << indentation << "PointStyle\n";
202 
203  indentation += INDENTATION_DELTA;
204 
205  str << indentation << pointShapeToString (m_shape) << "\n";
206  str << indentation << "radius=" << m_radius << "\n";
207  str << indentation << "lineWidth=" << m_lineWidth << "\n";
208  str << indentation << "color=" << colorPaletteToString (m_paletteColor) << "\n";
209 }
210 
211 int PointStyle::radius () const
212 {
213  return m_radius;
214 }
215 
216 void PointStyle::saveXml(QXmlStreamWriter &writer) const
217 {
218  LOG4CPP_INFO_S ((*mainCat)) << "PointStyle::saveXml";
219 
220  writer.writeStartElement(DOCUMENT_SERIALIZE_POINT_STYLE);
221  writer.writeAttribute (DOCUMENT_SERIALIZE_POINT_STYLE_RADIUS, QString::number (m_radius));
222  writer.writeAttribute (DOCUMENT_SERIALIZE_POINT_STYLE_LINE_WIDTH, QString::number (m_lineWidth));
223  writer.writeAttribute (DOCUMENT_SERIALIZE_POINT_STYLE_COLOR, QString::number (m_paletteColor));
224  writer.writeAttribute (DOCUMENT_SERIALIZE_POINT_STYLE_COLOR_STRING, colorPaletteToString (m_paletteColor));
225  writer.writeAttribute (DOCUMENT_SERIALIZE_POINT_STYLE_SHAPE, QString::number (m_shape));
226  writer.writeAttribute (DOCUMENT_SERIALIZE_POINT_STYLE_SHAPE_STRING, pointShapeToString (m_shape));
227  writer.writeEndElement();
228 }
229 
231 {
232  m_lineWidth = width;
233 }
234 
235 void PointStyle::setPaletteColor (ColorPalette paletteColor)
236 {
237  m_paletteColor = paletteColor;
238 }
239 
240 void PointStyle::setRadius (int radius)
241 {
242  m_radius = radius;
243 }
244 
245 void PointStyle::setShape (PointShape shape)
246 {
247  m_shape = shape;
248 }
249 
250 PointShape PointStyle::shape () const
251 {
252  return m_shape;
253 }
static PointStyle defaultAxesCurve()
Initial default for axes curve.
Definition: PointStyle.cpp:49
PointStyle()
Default constructor only for use when this class is being stored by a container that requires the def...
Definition: PointStyle.cpp:16
int lineWidth() const
Get method for line width.
Definition: PointStyle.cpp:77
void printStream(QString indentation, QTextStream &str) const
Debugging method that supports print method of this class and printStream method of some other class(...
Definition: PointStyle.cpp:198
void loadXml(QXmlStreamReader &reader)
Load model from serialized xml. Returns the curve name.
Definition: PointStyle.cpp:82
QPolygonF polygon() const
Return the polygon for creating a QGraphicsPolygonItem. The size is determined by the radius...
Definition: PointStyle.cpp:113
void setShape(PointShape shape)
Set method for point shape.
Definition: PointStyle.cpp:245
void saveXml(QXmlStreamWriter &writer) const
Serialize to stream.
Definition: PointStyle.cpp:216
Details for a specific Point.
Definition: PointStyle.h:14
ColorPalette paletteColor() const
Get method for point color.
Definition: PointStyle.cpp:108
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
void setPaletteColor(ColorPalette paletteColor)
Set method for point color.
Definition: PointStyle.cpp:235
bool isCircle() const
Return true if point is a circle, otherwise it is a polygon. For a circle, the radius is important an...
Definition: PointStyle.cpp:72
PointStyle & operator=(const PointStyle &other)
Assignment constructor.
Definition: PointStyle.cpp:39
static PointStyle defaultGraphCurve(int index)
Initial default for index'th graph curve.
Definition: PointStyle.cpp:57
void setLineWidth(int width)
Set method for line width.
Definition: PointStyle.cpp:230
PointShape shape() const
Get method for point shape.
Definition: PointStyle.cpp:250
void setRadius(int radius)
Set method for point radius.
Definition: PointStyle.cpp:240