Engauge Digitizer  2
LineStyle.cpp
1 #include "DocumentSerialize.h"
2 #include "LineStyle.h"
3 #include "Logger.h"
4 #include <QTextStream>
5 #include <QXmlStreamReader>
6 #include <QXmlStreamWriter>
7 #include "Xml.h"
8 
9 const int DEFAULT_LINE_WIDTH = 1;
10 
12  m_width (0),
13  m_paletteColor (COLOR_PALETTE_TRANSPARENT),
14  m_curveConnectAs (CONNECT_AS_FUNCTION_SMOOTH)
15 {
16 }
17 
18 LineStyle::LineStyle (unsigned int width,
19  ColorPalette paletteColor,
20  CurveConnectAs curveConnectAs) :
21  m_width (width),
22  m_paletteColor (paletteColor),
23  m_curveConnectAs (curveConnectAs)
24 {
25 }
26 
28  m_width (other.width ()),
29  m_paletteColor (other.paletteColor()),
30  m_curveConnectAs (other.curveConnectAs())
31 {
32 }
33 
35 {
36  m_width = other.width ();
37  m_paletteColor = other.paletteColor();
38  m_curveConnectAs = other.curveConnectAs();
39 
40  return *this;
41 }
42 
43 CurveConnectAs LineStyle::curveConnectAs () const
44 {
45  return m_curveConnectAs;
46 }
47 
49 {
50  return LineStyle (DEFAULT_LINE_WIDTH,
51  COLOR_PALETTE_TRANSPARENT,
52  CONNECT_SKIP_FOR_AXIS_CURVE); // Same default color as used for PointStyle axes curve default
53 }
54 
56 {
57  return LineStyle (DEFAULT_LINE_WIDTH,
58  COLOR_PALETTE_BLUE,
59  CONNECT_AS_FUNCTION_SMOOTH); // Same default color as used for PointStyle graph curves default
60 }
61 
62 void LineStyle::loadXml(QXmlStreamReader &reader)
63 {
64  LOG4CPP_INFO_S ((*mainCat)) << "LineStyle::loadXml";
65 
66  QXmlStreamAttributes attributes = reader.attributes();
67 
68  if (attributes.hasAttribute(DOCUMENT_SERIALIZE_LINE_STYLE_WIDTH) &&
69  attributes.hasAttribute(DOCUMENT_SERIALIZE_LINE_STYLE_COLOR) &&
70  attributes.hasAttribute(DOCUMENT_SERIALIZE_LINE_STYLE_CONNECT_AS)) {
71 
72  setWidth (attributes.value(DOCUMENT_SERIALIZE_LINE_STYLE_WIDTH).toInt());
73  setPaletteColor ((ColorPalette) attributes.value(DOCUMENT_SERIALIZE_LINE_STYLE_COLOR).toInt());
74  setCurveConnectAs ((CurveConnectAs) attributes.value(DOCUMENT_SERIALIZE_LINE_STYLE_CONNECT_AS).toInt());
75 
76  // Read until end of this subtree
77  while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
78  (reader.name() != DOCUMENT_SERIALIZE_LINE_STYLE)){
79  loadNextFromReader(reader);
80  }
81  } else {
82  reader.raiseError ("Cannot read line style data");
83  }
84 }
85 
86 ColorPalette LineStyle::paletteColor() const
87 {
88  return m_paletteColor;
89 }
90 
91 void LineStyle::printStream(QString indentation,
92  QTextStream &str) const
93 {
94  str << indentation << "LineStyle\n";
95 
96  indentation += INDENTATION_DELTA;
97 
98  str << indentation << "width=" << m_width << "\n";
99  str << indentation << "color=" << colorPaletteToString (m_paletteColor) << "\n";
100  str << indentation << "curveConnectAs=" << curveConnectAsToString (m_curveConnectAs) << "\n";
101 }
102 
103 void LineStyle::saveXml(QXmlStreamWriter &writer) const
104 {
105  LOG4CPP_INFO_S ((*mainCat)) << "LineStyle::saveXml";
106 
107  writer.writeStartElement(DOCUMENT_SERIALIZE_LINE_STYLE);
108  writer.writeAttribute (DOCUMENT_SERIALIZE_LINE_STYLE_WIDTH, QString::number(m_width));
109  writer.writeAttribute (DOCUMENT_SERIALIZE_LINE_STYLE_COLOR, QString::number (m_paletteColor));
110  writer.writeAttribute (DOCUMENT_SERIALIZE_LINE_STYLE_COLOR_STRING, colorPaletteToString (m_paletteColor));
111  writer.writeAttribute (DOCUMENT_SERIALIZE_LINE_STYLE_CONNECT_AS, QString::number (m_curveConnectAs));
112  writer.writeAttribute (DOCUMENT_SERIALIZE_LINE_STYLE_CONNECT_AS_STRING, curveConnectAsToString (m_curveConnectAs));
113  writer.writeEndElement();
114 }
115 
116 void LineStyle::setCurveConnectAs(CurveConnectAs curveConnectAs)
117 {
118  m_curveConnectAs = curveConnectAs;
119 }
120 
121 void LineStyle::setPaletteColor (ColorPalette paletteColor)
122 {
123  m_paletteColor = paletteColor;
124 }
125 
126 void LineStyle::setWidth (int width)
127 {
128  m_width = width;
129 }
130 
131 unsigned int LineStyle::width () const
132 {
133  return m_width;
134 }
void loadXml(QXmlStreamReader &reader)
Load model from serialized xml. Returns the curve name.
Definition: LineStyle.cpp:62
static LineStyle defaultGraphCurve(int index)
Initial default for index'th graph curve.
Definition: LineStyle.cpp:55
unsigned int width() const
Width of line.
Definition: LineStyle.cpp:131
LineStyle & operator=(const LineStyle &other)
Assignment operator.
Definition: LineStyle.cpp:34
static LineStyle defaultAxesCurve()
Initial default for axes curve.
Definition: LineStyle.cpp:48
void printStream(QString indentation, QTextStream &str) const
Debugging method that supports print method of this class and printStream method of some other class(...
Definition: LineStyle.cpp:91
Details for a specific Line.
Definition: LineStyle.h:13
ColorPalette paletteColor() const
Line color.
Definition: LineStyle.cpp:86
void setCurveConnectAs(CurveConnectAs curveConnectAs)
Set connect as.
Definition: LineStyle.cpp:116
CurveConnectAs curveConnectAs() const
Get method for connect type.
Definition: LineStyle.cpp:43
void setPaletteColor(ColorPalette paletteColor)
Set method for line color.
Definition: LineStyle.cpp:121
void saveXml(QXmlStreamWriter &writer) const
Serialize to stream.
Definition: LineStyle.cpp:103
void setWidth(int width)
Set width of line.
Definition: LineStyle.cpp:126
LineStyle()
Default constructor only for use when this class is being stored by a container that requires the def...
Definition: LineStyle.cpp:11