00001
00002
00003
00004
00005
00006
00007 #include "DocumentSerialize.h"
00008 #include "EngaugeAssert.h"
00009 #include "Logger.h"
00010 #include "PointStyle.h"
00011 #include <qmath.h>
00012 #include <QObject>
00013 #include <QSettings>
00014 #include <QTextStream>
00015 #include <QtToString.h>
00016 #include <QXmlStreamWriter>
00017 #include "Settings.h"
00018 #include "SettingsForGraph.h"
00019 #include "Xml.h"
00020
00021 const ColorPalette DEFAULT_POINT_COLOR_AXES = COLOR_PALETTE_RED;
00022 const ColorPalette DEFAULT_POINT_COLOR_GRAPH = COLOR_PALETTE_BLUE;
00023 const int DEFAULT_POINT_LINE_WIDTH = 1;
00024 const int DEFAULT_POINT_RADIUS = 10;
00025 const PointShape DEFAULT_POINT_SHAPE_AXIS = POINT_SHAPE_CROSS;
00026 const double PI = 3.1415926535;
00027 const double TWO_PI = 2.0 * PI;
00028
00029 PointStyle::PointStyle ()
00030 {
00031 }
00032
00033 PointStyle::PointStyle(PointShape shape,
00034 unsigned int radius,
00035 int lineWidth,
00036 ColorPalette paletteColor) :
00037 m_shape (shape),
00038 m_radius (radius),
00039 m_lineWidth (lineWidth),
00040 m_paletteColor (paletteColor)
00041 {
00042 }
00043
00044 PointStyle::PointStyle (const PointStyle &other) :
00045 m_shape (other.shape()),
00046 m_radius (other.radius ()),
00047 m_lineWidth (other.lineWidth ()),
00048 m_paletteColor (other.paletteColor ())
00049 {
00050 }
00051
00052 PointStyle &PointStyle::operator=(const PointStyle &other)
00053 {
00054 m_shape = other.shape ();
00055 m_radius = other.radius ();
00056 m_lineWidth = other.lineWidth ();
00057 m_paletteColor = other.paletteColor ();
00058
00059 return *this;
00060 }
00061
00062 PointStyle PointStyle::defaultAxesCurve ()
00063 {
00064
00065 QSettings settings (SETTINGS_ENGAUGE, SETTINGS_DIGITIZER);
00066 settings.beginGroup (SETTINGS_GROUP_CURVE_AXES);
00067 PointShape shape = (PointShape) settings.value (SETTINGS_CURVE_POINT_SHAPE,
00068 DEFAULT_POINT_SHAPE_AXIS).toInt();
00069 int radius = settings.value (SETTINGS_CURVE_POINT_RADIUS,
00070 DEFAULT_POINT_RADIUS).toInt();
00071 int pointLineWidth = settings.value (SETTINGS_CURVE_POINT_LINE_WIDTH,
00072 DEFAULT_POINT_LINE_WIDTH).toInt();
00073 ColorPalette pointColor = (ColorPalette) settings.value (SETTINGS_CURVE_POINT_COLOR,
00074 DEFAULT_POINT_COLOR_AXES).toInt();
00075 settings.endGroup ();
00076
00077 return PointStyle (shape,
00078 radius,
00079 pointLineWidth,
00080 pointColor);
00081 }
00082
00083 PointStyle PointStyle::defaultGraphCurve (int index)
00084 {
00085
00086 PointShape shape = POINT_SHAPE_CROSS;
00087 static PointShape pointShapes [] = {POINT_SHAPE_CROSS,
00088 POINT_SHAPE_X,
00089 POINT_SHAPE_DIAMOND,
00090 POINT_SHAPE_SQUARE};
00091 shape = pointShapes [index % 4];
00092
00093 SettingsForGraph settingsForGraph;
00094 int indexOneBased = index + 1;
00095 QString groupName = settingsForGraph.groupNameForNthCurve (indexOneBased);
00096
00097
00098 QSettings settings (SETTINGS_ENGAUGE, SETTINGS_DIGITIZER);
00099 settings.beginGroup (groupName);
00100 int radius = settings.value (SETTINGS_CURVE_POINT_RADIUS,
00101 DEFAULT_POINT_RADIUS).toInt();
00102 int pointLineWidth = settings.value (SETTINGS_CURVE_POINT_LINE_WIDTH,
00103 DEFAULT_POINT_LINE_WIDTH).toInt();
00104 ColorPalette pointColor = (ColorPalette) settings.value (SETTINGS_CURVE_POINT_COLOR,
00105 DEFAULT_POINT_COLOR_GRAPH).toInt();
00106 settings.endGroup ();
00107
00108 return PointStyle (shape,
00109 radius,
00110 pointLineWidth,
00111 pointColor);
00112 }
00113
00114 bool PointStyle::isCircle () const
00115 {
00116 return m_shape == POINT_SHAPE_CIRCLE;
00117 }
00118
00119 int PointStyle::lineWidth() const
00120 {
00121 return m_lineWidth;
00122 }
00123
00124 void PointStyle::loadXml(QXmlStreamReader &reader)
00125 {
00126 LOG4CPP_INFO_S ((*mainCat)) << "PointStyle::loadXml";
00127
00128 QXmlStreamAttributes attributes = reader.attributes();
00129
00130 if (attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_STYLE_RADIUS) &&
00131 attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_STYLE_LINE_WIDTH) &&
00132 attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_STYLE_COLOR) &&
00133 attributes.hasAttribute(DOCUMENT_SERIALIZE_POINT_STYLE_SHAPE)) {
00134
00135 setRadius (attributes.value(DOCUMENT_SERIALIZE_POINT_STYLE_RADIUS).toInt());
00136 setLineWidth (attributes.value(DOCUMENT_SERIALIZE_POINT_STYLE_LINE_WIDTH).toInt());
00137 setPaletteColor ((ColorPalette) attributes.value(DOCUMENT_SERIALIZE_POINT_STYLE_COLOR).toInt());
00138 setShape ((PointShape) attributes.value(DOCUMENT_SERIALIZE_POINT_STYLE_SHAPE).toInt());
00139
00140
00141 while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
00142 (reader.name() != DOCUMENT_SERIALIZE_POINT_STYLE)){
00143 loadNextFromReader(reader);
00144 }
00145 } else {
00146 reader.raiseError (QObject::tr ("Cannot read point style data"));
00147 }
00148 }
00149
00150 ColorPalette PointStyle::paletteColor () const
00151 {
00152 return m_paletteColor;
00153 }
00154
00155 QPolygonF PointStyle::polygon () const
00156 {
00157 const int NUM_XY = 60;
00158 QVector<QPointF> points;
00159
00160 switch (m_shape) {
00161
00162 case POINT_SHAPE_CIRCLE:
00163 {
00164 int xyWidth = m_radius;
00165 for (int i = 0; i <= NUM_XY; i++) {
00166 double angle = TWO_PI * (double) i / (double) NUM_XY;
00167 double x = xyWidth * cos (angle);
00168 double y = xyWidth * sin (angle);
00169 points.append (QPointF (x, y));
00170 }
00171 }
00172 break;
00173
00174 case POINT_SHAPE_CROSS:
00175 {
00176 int xyWidth = m_radius;
00177
00178 points.append (QPointF (-1 * xyWidth, 0));
00179 points.append (QPointF (xyWidth, 0));
00180 points.append (QPointF (0, 0));
00181 points.append (QPointF (0, xyWidth));
00182 points.append (QPointF (0, -1 * xyWidth));
00183 points.append (QPointF (0, 0));
00184 }
00185 break;
00186
00187 case POINT_SHAPE_DIAMOND:
00188 {
00189 int xyWidth = m_radius;
00190
00191 points.append (QPointF (0, -1 * xyWidth));
00192 points.append (QPointF (-1 * xyWidth, 0));
00193 points.append (QPointF (0, xyWidth));
00194 points.append (QPointF (xyWidth, 0));
00195 }
00196 break;
00197
00198 case POINT_SHAPE_SQUARE:
00199 {
00200 int xyWidth = m_radius;
00201
00202 points.append (QPointF (-1 * xyWidth, -1 * xyWidth));
00203 points.append (QPointF (-1 * xyWidth, xyWidth));
00204 points.append (QPointF (xyWidth, xyWidth));
00205 points.append (QPointF (xyWidth, -1 * xyWidth));
00206 }
00207 break;
00208
00209 case POINT_SHAPE_TRIANGLE:
00210 {
00211 int xyWidth = m_radius;
00212
00213 points.append (QPointF (-1 * xyWidth, -1 * xyWidth));
00214 points.append (QPointF (0, xyWidth));
00215 points.append (QPointF (xyWidth, -1 * xyWidth));
00216 }
00217 break;
00218
00219 case POINT_SHAPE_X:
00220 {
00221 int xyWidth = m_radius * qSqrt (0.5);
00222
00223 points.append (QPointF (-1 * xyWidth, -1 * xyWidth));
00224 points.append (QPointF (xyWidth, xyWidth));
00225 points.append (QPointF (0, 0));
00226 points.append (QPointF (-1 * xyWidth, xyWidth));
00227 points.append (QPointF (xyWidth, -1 * xyWidth));
00228 points.append (QPointF (0, 0));
00229 }
00230 break;
00231
00232 default:
00233 ENGAUGE_ASSERT (false);
00234 }
00235
00236 QPolygonF polygon (points);
00237 return polygon;
00238 }
00239
00240 void PointStyle::printStream(QString indentation,
00241 QTextStream &str) const
00242 {
00243 str << indentation << "PointStyle\n";
00244
00245 indentation += INDENTATION_DELTA;
00246
00247 str << indentation << pointShapeToString (m_shape) << "\n";
00248 str << indentation << "radius=" << m_radius << "\n";
00249 str << indentation << "lineWidth=" << m_lineWidth << "\n";
00250 str << indentation << "color=" << colorPaletteToString (m_paletteColor) << "\n";
00251 }
00252
00253 int PointStyle::radius () const
00254 {
00255 return m_radius;
00256 }
00257
00258 void PointStyle::saveXml(QXmlStreamWriter &writer) const
00259 {
00260 LOG4CPP_INFO_S ((*mainCat)) << "PointStyle::saveXml";
00261
00262 writer.writeStartElement(DOCUMENT_SERIALIZE_POINT_STYLE);
00263 writer.writeAttribute (DOCUMENT_SERIALIZE_POINT_STYLE_RADIUS, QString::number (m_radius));
00264 writer.writeAttribute (DOCUMENT_SERIALIZE_POINT_STYLE_LINE_WIDTH, QString::number (m_lineWidth));
00265 writer.writeAttribute (DOCUMENT_SERIALIZE_POINT_STYLE_COLOR, QString::number (m_paletteColor));
00266 writer.writeAttribute (DOCUMENT_SERIALIZE_POINT_STYLE_COLOR_STRING, colorPaletteToString (m_paletteColor));
00267 writer.writeAttribute (DOCUMENT_SERIALIZE_POINT_STYLE_SHAPE, QString::number (m_shape));
00268 writer.writeAttribute (DOCUMENT_SERIALIZE_POINT_STYLE_SHAPE_STRING, pointShapeToString (m_shape));
00269 writer.writeEndElement();
00270 }
00271
00272 void PointStyle::setLineWidth(int width)
00273 {
00274 m_lineWidth = width;
00275 }
00276
00277 void PointStyle::setPaletteColor (ColorPalette paletteColor)
00278 {
00279 m_paletteColor = paletteColor;
00280 }
00281
00282 void PointStyle::setRadius (int radius)
00283 {
00284 m_radius = radius;
00285 }
00286
00287 void PointStyle::setShape (PointShape shape)
00288 {
00289 m_shape = shape;
00290 }
00291
00292 PointShape PointStyle::shape () const
00293 {
00294 return m_shape;
00295 }