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