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