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