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"
8 #include "DocumentModelGridDisplay.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 
18 const ColorPalette DEFAULT_COLOR = COLOR_PALETTE_BLACK;
19 
DocumentModelGridDisplay()20 DocumentModelGridDisplay::DocumentModelGridDisplay() :
21   m_stable (false),
22   m_disableX (GRID_COORD_DISABLE_COUNT),
23   m_countX (2),
24   m_startX (0.0),
25   m_stepX (1.0),
26   m_stopX (1.0),
27   m_disableY (GRID_COORD_DISABLE_COUNT),
28   m_countY (2),
29   m_startY (0.0),
30   m_stepY (1.0),
31   m_stopY (1.0),
32   m_paletteColor (DEFAULT_COLOR)
33 {
34 }
35 
DocumentModelGridDisplay(const Document & document)36 DocumentModelGridDisplay::DocumentModelGridDisplay(const Document &document) :
37   m_stable (document.modelGridDisplay().stable()),
38   m_disableX (document.modelGridDisplay().disableX()),
39   m_countX (document.modelGridDisplay().countX()),
40   m_startX (document.modelGridDisplay().startX()),
41   m_stepX (document.modelGridDisplay().stepX()),
42   m_stopX (document.modelGridDisplay().stopX()),
43   m_disableY (document.modelGridDisplay().disableY()),
44   m_countY (document.modelGridDisplay().countY()),
45   m_startY (document.modelGridDisplay().startY()),
46   m_stepY (document.modelGridDisplay().stepY()),
47   m_stopY (document.modelGridDisplay().stopY()),
48   m_paletteColor (document.modelGridDisplay().paletteColor())
49 {
50 }
51 
DocumentModelGridDisplay(const DocumentModelGridDisplay & other)52 DocumentModelGridDisplay::DocumentModelGridDisplay(const DocumentModelGridDisplay &other) :
53   m_stable(other.stable()),
54   m_disableX (other.disableX()),
55   m_countX (other.countX()),
56   m_startX (other.startX()),
57   m_stepX (other.stepX()),
58   m_stopX (other.stopX()),
59   m_disableY (other.disableY()),
60   m_countY (other.countY()),
61   m_startY (other.startY()),
62   m_stepY (other.stepY()),
63   m_stopY (other.stopY()),
64   m_paletteColor (other.paletteColor())
65 {
66 }
67 
operator =(const DocumentModelGridDisplay & other)68 DocumentModelGridDisplay &DocumentModelGridDisplay::operator=(const DocumentModelGridDisplay &other)
69 {
70   m_stable = other.stable();
71   m_disableX = other.disableX();
72   m_countX = other.countX();
73   m_startX = other.startX();
74   m_stepX = other.stepX();
75   m_stopX = other.stopX();
76   m_disableY = other.disableY();
77   m_countY = other.countY();
78   m_startY = other.startY();
79   m_stepY = other.stepY();
80   m_stopY = other.stopY();
81   m_paletteColor = other.paletteColor();
82 
83   return *this;
84 }
85 
countX() const86 unsigned int DocumentModelGridDisplay::countX () const
87 {
88   return m_countX;
89 }
90 
countY() const91 unsigned int DocumentModelGridDisplay::countY () const
92 {
93   return m_countY;
94 }
95 
disableX() const96 GridCoordDisable DocumentModelGridDisplay::disableX () const
97 {
98   return m_disableX;
99 }
100 
disableY() const101 GridCoordDisable DocumentModelGridDisplay::disableY () const
102 {
103   return m_disableY;
104 }
105 
loadXml(QXmlStreamReader & reader)106 void DocumentModelGridDisplay::loadXml(QXmlStreamReader &reader)
107 {
108   LOG4CPP_INFO_S ((*mainCat)) << "DocumentModelGridDisplay::loadXml";
109 
110   bool success = true;
111 
112   QXmlStreamAttributes attributes = reader.attributes();
113 
114   if (attributes.hasAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_STABLE) &&
115       attributes.hasAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_DISABLE_X) &&
116       attributes.hasAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_COUNT_X) &&
117       attributes.hasAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_START_X) &&
118       attributes.hasAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_STEP_X) &&
119       attributes.hasAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_STOP_X) &&
120       attributes.hasAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_DISABLE_Y) &&
121       attributes.hasAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_COUNT_Y) &&
122       attributes.hasAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_START_Y) &&
123       attributes.hasAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_STEP_Y) &&
124       attributes.hasAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_STOP_Y) &&
125       attributes.hasAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_COLOR)) {
126 
127     // Boolean values
128     QString stableValue = attributes.value(DOCUMENT_SERIALIZE_GRID_DISPLAY_STABLE).toString();
129 
130     setStable (stableValue == DOCUMENT_SERIALIZE_BOOL_TRUE);
131     setDisableX (static_cast<GridCoordDisable> (attributes.value(DOCUMENT_SERIALIZE_GRID_DISPLAY_DISABLE_X).toInt()));
132     setCountX (attributes.value(DOCUMENT_SERIALIZE_GRID_DISPLAY_COUNT_X).toUInt());
133     setStartX (attributes.value(DOCUMENT_SERIALIZE_GRID_DISPLAY_START_X).toDouble());
134     setStepX (attributes.value(DOCUMENT_SERIALIZE_GRID_DISPLAY_STEP_X).toDouble());
135     setStopX (attributes.value(DOCUMENT_SERIALIZE_GRID_DISPLAY_STOP_X).toDouble());
136     setDisableY (static_cast<GridCoordDisable> (attributes.value(DOCUMENT_SERIALIZE_GRID_DISPLAY_DISABLE_Y).toUInt()));
137     setCountY (attributes.value(DOCUMENT_SERIALIZE_GRID_DISPLAY_COUNT_Y).toUInt());
138     setStartY (attributes.value(DOCUMENT_SERIALIZE_GRID_DISPLAY_START_Y).toDouble());
139     setStepY (attributes.value(DOCUMENT_SERIALIZE_GRID_DISPLAY_STEP_Y).toDouble());
140     setStopY (attributes.value(DOCUMENT_SERIALIZE_GRID_DISPLAY_STOP_Y).toDouble());
141     setPaletteColor (static_cast<ColorPalette> (attributes.value(DOCUMENT_SERIALIZE_GRID_DISPLAY_COLOR).toInt()));
142 
143     // Read until end of this subtree
144     while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
145     (reader.name() != DOCUMENT_SERIALIZE_GRID_DISPLAY)){
146       loadNextFromReader(reader);
147       if (reader.atEnd()) {
148         success = false;
149         break;
150       }
151     }
152   }
153 
154   if (!success) {
155     reader.raiseError (QObject::tr ("Cannot read grid display data"));
156   }
157 }
158 
paletteColor() const159 ColorPalette DocumentModelGridDisplay::paletteColor() const
160 {
161   return m_paletteColor;
162 }
163 
printStream(QString indentation,QTextStream & str) const164 void DocumentModelGridDisplay::printStream(QString indentation,
165                                            QTextStream &str) const
166 {
167   str << indentation << "DocumentModelGridDisplay\n";
168 
169   indentation += INDENTATION_DELTA;
170 
171   str << indentation << "stable=" << (m_stable ? "true" : "false") << "\n";
172   str << indentation << "disableX=" << m_disableX << "\n";
173   str << indentation << "countX=" << m_countX << "\n";
174   str << indentation << "startX=" << m_startX << "\n";
175   str << indentation << "stepX=" << m_stepX << "\n";
176   str << indentation << "stopX=" << m_stopX << "\n";
177   str << indentation << "disableY=" << m_disableY << "\n";
178   str << indentation << "countY=" << m_countY << "\n";
179   str << indentation << "startY=" << m_startY << "\n";
180   str << indentation << "stepY=" << m_stepY << "\n";
181   str << indentation << "stopY=" << m_stopY << "\n";
182   str << indentation << "color=" << colorPaletteToString (m_paletteColor) << "\n";
183 }
184 
saveXml(QXmlStreamWriter & writer) const185 void DocumentModelGridDisplay::saveXml(QXmlStreamWriter &writer) const
186 {
187   LOG4CPP_INFO_S ((*mainCat)) << "DocumentModelGridDisplay::saveXml";
188 
189   writer.writeStartElement(DOCUMENT_SERIALIZE_GRID_DISPLAY);
190   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_STABLE, m_stable ?
191                             DOCUMENT_SERIALIZE_BOOL_TRUE :
192                             DOCUMENT_SERIALIZE_BOOL_FALSE);
193   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_DISABLE_X, QString::number (m_disableX));
194   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_COUNT_X, QString::number (m_countX));
195   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_START_X, QString::number  (m_startX));
196   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_STEP_X, QString::number (m_stepX));
197   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_STOP_X, QString::number (m_stopX));
198   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_DISABLE_Y, QString::number (m_disableY));
199   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_COUNT_Y, QString::number (m_countY));
200   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_START_Y, QString::number  (m_startY));
201   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_STEP_Y, QString::number (m_stepY));
202   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_STOP_Y, QString::number (m_stopY));
203   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_COLOR, QString::number (m_paletteColor));
204   writer.writeAttribute(DOCUMENT_SERIALIZE_GRID_DISPLAY_COLOR_STRING, colorPaletteToString (m_paletteColor));
205   writer.writeEndElement();
206 }
207 
setCountX(unsigned int countX)208 void DocumentModelGridDisplay::setCountX (unsigned int countX)
209 {
210   m_countX = countX;
211 }
212 
setCountY(unsigned int countY)213 void DocumentModelGridDisplay::setCountY (unsigned int countY)
214 {
215   m_countY = countY;
216 }
217 
setDisableX(GridCoordDisable disableX)218 void DocumentModelGridDisplay::setDisableX (GridCoordDisable disableX)
219 {
220   m_disableX = disableX;
221 }
222 
setDisableY(GridCoordDisable disableY)223 void DocumentModelGridDisplay::setDisableY (GridCoordDisable disableY)
224 {
225   m_disableY = disableY;
226 }
227 
setPaletteColor(ColorPalette paletteColor)228 void DocumentModelGridDisplay::setPaletteColor(ColorPalette paletteColor)
229 {
230   m_paletteColor = paletteColor;
231 }
232 
setStable(bool stable)233 void DocumentModelGridDisplay::setStable(bool stable)
234 {
235   m_stable = stable;
236 }
237 
setStartX(double startX)238 void DocumentModelGridDisplay::setStartX (double startX)
239 {
240   m_startX = startX;
241 }
242 
setStartY(double startY)243 void DocumentModelGridDisplay::setStartY (double startY)
244 {
245   m_startY = startY;
246 }
247 
setStepX(double stepX)248 void DocumentModelGridDisplay::setStepX (double stepX)
249 {
250   m_stepX = stepX;
251 }
252 
setStepY(double stepY)253 void DocumentModelGridDisplay::setStepY (double stepY)
254 {
255   m_stepY = stepY;
256 }
257 
setStopX(double stopX)258 void DocumentModelGridDisplay::setStopX (double stopX)
259 {
260   m_stopX = stopX;
261 }
262 
setStopY(double stopY)263 void DocumentModelGridDisplay::setStopY (double stopY)
264 {
265   m_stopY = stopY;
266 }
267 
stable() const268 bool DocumentModelGridDisplay::stable() const
269 {
270   return m_stable;
271 }
272 
startX() const273 double DocumentModelGridDisplay::startX() const
274 {
275   return m_startX;
276 }
277 
startY() const278 double DocumentModelGridDisplay::startY() const
279 {
280   return m_startY;
281 }
282 
stepX() const283 double DocumentModelGridDisplay::stepX() const
284 {
285   return m_stepX;
286 }
287 
stepY() const288 double DocumentModelGridDisplay::stepY() const
289 {
290   return m_stepY;
291 }
292 
stopX() const293 double DocumentModelGridDisplay::stopX() const
294 {
295   return m_stopX;
296 }
297 
stopY() const298 double DocumentModelGridDisplay::stopY() const
299 {
300   return m_stopY;
301 }
302