Engauge Digitizer 2
Loading...
Searching...
No Matches
DocumentModelGuideline.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 "CmdMediator.h"
9#include "DocumentSerialize.h"
10#include "EngaugeAssert.h"
11#include "Logger.h"
12#include <QObject>
13#include <QTextStream>
14#include "QtToString.h"
15#include <QXmlStreamWriter>
16#include "Xml.h"
17
19const ColorPalette DEFAULT_LINE_COLOR (COLOR_PALETTE_MAGENTA); // Should be bright so it gets noticed
20const double DEFAULT_LINE_WIDTH_ACTIVE = 2; // Wider to grab attention
21const double DEFAULT_LINE_WIDTH_INACTIVE = 1; // Narrower for better accuracy
22
24 m_creationCircleRadius (DEFAULT_CREATION_CIRCLE_RADIUS),
25 m_lineColor (DEFAULT_LINE_COLOR),
26 m_lineWidthActive (DEFAULT_LINE_WIDTH_ACTIVE),
27 m_lineWidthInactive (DEFAULT_LINE_WIDTH_INACTIVE)
28{
29}
30
32 m_valuesX (document.modelGuideline().valuesX ()),
33 m_valuesY (document.modelGuideline().valuesY ()),
34 m_creationCircleRadius (document.modelGuideline().creationCircleRadius ()),
35 m_lineColor (document.modelGuideline().lineColor ()),
36 m_lineWidthActive (document.modelGuideline().lineWidthActive ()),
37 m_lineWidthInactive (document.modelGuideline().lineWidthInactive ())
38{
39}
40
42 m_valuesX (other.valuesX ()),
43 m_valuesY (other.valuesY ()),
44 m_creationCircleRadius (other.creationCircleRadius ()),
45 m_lineColor (other.lineColor ()),
46 m_lineWidthActive (other.lineWidthActive ()),
47 m_lineWidthInactive (other.lineWidthInactive ())
48{
49}
50
52{
53 m_valuesX = other.valuesX ();
54 m_valuesY = other.valuesY ();
55 m_creationCircleRadius = other.creationCircleRadius ();
56 m_lineColor = other.lineColor ();
57 m_lineWidthActive = other.lineWidthActive ();
58 m_lineWidthInactive = other.lineWidthInactive ();
59
60 return *this;
61}
62
64{
65 return m_creationCircleRadius;
66}
67
69{
70 return m_lineColor;
71}
72
74{
75 return m_lineWidthActive;
76}
77
79{
80 return m_lineWidthInactive;
81}
82
83void DocumentModelGuideline::loadXml(QXmlStreamReader &reader)
84{
85 LOG4CPP_INFO_S ((*mainCat)) << "DocumentModelGuideline::loadXml";
86
87 bool success = true;
88
89 QXmlStreamAttributes attributes = reader.attributes();
90
91 if (attributes.hasAttribute (DOCUMENT_SERIALIZE_GUIDELINE_CREATION_CIRCLE_RADIUS) &&
92 attributes.hasAttribute (DOCUMENT_SERIALIZE_GUIDELINE_LINE_COLOR) &&
93 attributes.hasAttribute (DOCUMENT_SERIALIZE_GUIDELINE_LINE_WIDTH_ACTIVE) &&
94 attributes.hasAttribute (DOCUMENT_SERIALIZE_GUIDELINE_LINE_WIDTH_INACTIVE)) {
95
97 setLineColor (static_cast<ColorPalette> (attributes.value (DOCUMENT_SERIALIZE_GUIDELINE_LINE_COLOR).toInt ()));
100 }
101
102 // Read until end of this subtree
103 while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
104 (reader.name() != DOCUMENT_SERIALIZE_GUIDELINES)){
105
106 QXmlStreamReader::TokenType tokenType = loadNextFromReader(reader);
107
108 if ((tokenType == QXmlStreamReader::StartElement) &&
109 (reader.name() == DOCUMENT_SERIALIZE_GUIDELINES_X)) {
110
111 loadXmlVector (reader,
113 m_valuesX);
114 }
115
116 if ((tokenType == QXmlStreamReader::StartElement) &&
117 (reader.name() == DOCUMENT_SERIALIZE_GUIDELINES_Y)) {
118
119 loadXmlVector (reader,
121 m_valuesY);
122 }
123
124 if (reader.atEnd()) {
125 success = false;
126 break;
127 }
128 }
129
130 if (!success) {
131 reader.raiseError (QObject::tr ("Cannot read grid display data"));
132 }
133}
134
135void DocumentModelGuideline::loadXmlVector (QXmlStreamReader &reader,
136 const QString &tokenEnd,
137 GuidelineValues &guidelineValues) const
138{
139 LOG4CPP_INFO_S ((*mainCat)) << "DocumentModelGuideline::loadXmlVector";
140
141 while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
142 (reader.name() != tokenEnd)){
143
144 QXmlStreamReader::TokenType tokenType = loadNextFromReader(reader);
145
146 if (reader.atEnd()) {
147 break;
148 }
149
150 if (tokenType == QXmlStreamReader::StartElement) {
151
152 if (reader.attributes().hasAttribute (DOCUMENT_SERIALIZE_GUIDELINE_IDENTIFIER) &&
153 reader.attributes().hasAttribute (DOCUMENT_SERIALIZE_GUIDELINE_VALUE)) {
154
155 QString identifier = reader.attributes ().value (DOCUMENT_SERIALIZE_GUIDELINE_IDENTIFIER).toString ();
156 double value = reader.attributes ().value (DOCUMENT_SERIALIZE_GUIDELINE_VALUE).toDouble ();
157
158 guidelineValues [identifier] = value;
159 }
160 }
161 }
162}
163
164void DocumentModelGuideline::printStream(QString indentation,
165 QTextStream &str) const
166{
167 str << indentation << "DocumentModelGuideline\n";
168
169 indentation += INDENTATION_DELTA;
170
171 QString valuesX, valuesY, delimiterX, delimiterY;
172 QTextStream strX (&valuesX), strY (&valuesY);
173 GuidelineValues::const_iterator itr;
174
175 for (itr = m_valuesX.constBegin(); itr != m_valuesX.constEnd(); itr++) {
176 strX << delimiterX << itr.value();
177 delimiterX = ", ";
178 }
179
180 for (itr = m_valuesY.constBegin(); itr != m_valuesY.constEnd(); itr++) {
181 strY << delimiterY << itr.value();
182 delimiterY = ", ";
183 }
184
185 str << indentation << "valuesX=" << valuesX << "\n";
186 str << indentation << "valuesY=" << valuesY << "\n";
187 str << indentation << "creationCircleRadius=" << m_creationCircleRadius << "\n";
188 str << indentation << "lineColor=" << colorPaletteToString (m_lineColor) << "\n";
189 str << indentation << "lineWidthActive=" << m_lineWidthActive << "\n";
190 str << indentation << "lineWidthInactive=" << m_lineWidthInactive << "\n";
191}
192
193void DocumentModelGuideline::saveXml(QXmlStreamWriter &writer) const
194{
195 LOG4CPP_INFO_S ((*mainCat)) << "DocumentModelGuideline::saveXml";
196
197 writer.writeStartElement(DOCUMENT_SERIALIZE_GUIDELINES);
198 writer.writeAttribute(DOCUMENT_SERIALIZE_GUIDELINE_CREATION_CIRCLE_RADIUS, QString::number (m_creationCircleRadius));
199 writer.writeAttribute(DOCUMENT_SERIALIZE_GUIDELINE_LINE_COLOR, QString::number (m_lineColor));
200 writer.writeAttribute(DOCUMENT_SERIALIZE_GUIDELINE_LINE_COLOR_STRING, colorPaletteToString (m_lineColor));
201 writer.writeAttribute(DOCUMENT_SERIALIZE_GUIDELINE_LINE_WIDTH_ACTIVE, QString::number (m_lineWidthActive));
202 writer.writeAttribute(DOCUMENT_SERIALIZE_GUIDELINE_LINE_WIDTH_INACTIVE, QString::number (m_lineWidthInactive));
203 saveXmlVector (writer,
205 m_valuesX);
206 saveXmlVector (writer,
208 m_valuesY);
209 writer.writeEndElement();
210}
211
212void DocumentModelGuideline::saveXmlVector(QXmlStreamWriter &writer,
213 const QString &tokenAll,
214 const GuidelineValues &values) const
215{
216 writer.writeStartElement(tokenAll);
217
218 // Loop through values
219 GuidelineValues::const_iterator itr;
220 for (itr = values.begin(); itr != values.end(); itr++) {
221 QString identifier = itr.key();
222 double value = itr.value();
223 writer.writeStartElement (DOCUMENT_SERIALIZE_GUIDELINE);
224 writer.writeAttribute (DOCUMENT_SERIALIZE_GUIDELINE_IDENTIFIER, identifier);
225 writer.writeAttribute (DOCUMENT_SERIALIZE_GUIDELINE_VALUE, QString::number (value));
226 writer.writeEndElement ();
227 }
228
229 writer.writeEndElement();
230}
231
233{
234 m_creationCircleRadius = radius;
235}
236
241
243{
244 m_lineWidthActive = lineWidthActive;
245}
246
251
253{
254 m_valuesX = valuesX;
255}
256
258{
259 m_valuesY = valuesY;
260}
261
263{
264 return m_valuesX;
265}
266
268{
269 return m_valuesY;
270}
QString colorPaletteToString(ColorPalette colorPalette)
ColorPalette
@ COLOR_PALETTE_MAGENTA
const ColorPalette DEFAULT_LINE_COLOR
const double DEFAULT_LINE_WIDTH_ACTIVE
const double DEFAULT_LINE_WIDTH_INACTIVE
const double DEFAULT_CREATION_CIRCLE_RADIUS
const QString DOCUMENT_SERIALIZE_GUIDELINE_LINE_WIDTH_INACTIVE
const QString DOCUMENT_SERIALIZE_GUIDELINE_CREATION_CIRCLE_RADIUS
const QString DOCUMENT_SERIALIZE_GUIDELINE_LINE_WIDTH_ACTIVE
const QString DOCUMENT_SERIALIZE_GUIDELINES
const QString DOCUMENT_SERIALIZE_GUIDELINE
const QString DOCUMENT_SERIALIZE_GUIDELINE_LINE_COLOR_STRING
const QString DOCUMENT_SERIALIZE_GUIDELINE_VALUE
const QString DOCUMENT_SERIALIZE_GUIDELINE_LINE_COLOR
const QString DOCUMENT_SERIALIZE_GUIDELINES_Y
const QString DOCUMENT_SERIALIZE_GUIDELINE_IDENTIFIER
const QString DOCUMENT_SERIALIZE_GUIDELINES_X
QMap< QString, double > GuidelineValues
log4cpp::Category * mainCat
Definition Logger.cpp:14
const QString INDENTATION_DELTA
QXmlStreamReader::TokenType loadNextFromReader(QXmlStreamReader &reader)
Load next token from xml reader.
Definition Xml.cpp:14
void setLineColor(ColorPalette lineColor)
Set method for line color.
void setLineWidthInactive(double lineWidth)
Set method for line width when inactive.
virtual void loadXml(QXmlStreamReader &reader)
Load model from serialized xml.
virtual void saveXml(QXmlStreamWriter &writer) const
Save entire model as xml into stream.
void setCreationCircleRadius(double radius)
Set method for creation circle radius in pixels.
DocumentModelGuideline()
Default constructor.
DocumentModelGuideline & operator=(const DocumentModelGuideline &other)
Assignment constructor.
void setLineWidthActive(double lineWidth)
Set method for line width when active.
GuidelineValues valuesY() const
Get method for y/r values.
double lineWidthInactive() const
Get method for line width when inactive.
double creationCircleRadius() const
Get method for creation circle radius in pixels.
GuidelineValues valuesX() const
Get method for x/t values.
void setValuesY(const GuidelineValues &valuesY)
Set method for y/r values.
ColorPalette lineColor() const
Get method for line color.
void printStream(QString indentation, QTextStream &str) const
Debugging method that supports print method of this class and printStream method of some other class(...
void setValuesX(const GuidelineValues &valuesX)
Set method for x/t values.
double lineWidthActive() const
Get method for line width when active.
Storage of one imported image and the data attached to that image.
Definition Document.h:44
#define LOG4CPP_INFO_S(logger)
Definition convenience.h:18