1 /***************************************************************************
2 File : LinearColorMap.cpp
3 Project : QtiPlot
4 --------------------------------------------------------------------
5 Copyright : (C) 2011 by Ion Vasilief
6 Email (use @ for *) : ion_vasilief*yahoo.fr
7 Description : A wrapper around QwtLinearColorMap from Qwt
8
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 * This program is distributed in the hope that it will be useful, *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21 * GNU General Public License for more details. *
22 * *
23 * You should have received a copy of the GNU General Public License *
24 * along with this program; if not, write to the Free Software *
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26 * Boston, MA 02110-1301 USA *
27 * *
28 ***************************************************************************/
29 #include "LinearColorMap.h"
30
LinearColorMap()31 LinearColorMap::LinearColorMap() : QwtLinearColorMap(),
32 d_range(QwtDoubleInterval())
33 {}
34
LinearColorMap(const QColor & from,const QColor & to)35 LinearColorMap::LinearColorMap(const QColor &from, const QColor &to) : QwtLinearColorMap(from, to),
36 d_range(QwtDoubleInterval())
37 {}
38
intensityRange() const39 QwtDoubleInterval LinearColorMap::intensityRange() const
40 {
41 return d_range;
42 }
43
toXmlString()44 QString LinearColorMap::toXmlString()
45 {
46 QString s = "<ColorMap>\n";
47 s += "\t<Mode>" + QString::number(mode()) + "</Mode>\n";
48 s += "\t<MinColor>" + color1().name() + "</MinColor>\n";
49 s += "\t<MaxColor>" + color2().name() + "</MaxColor>\n";
50 if (d_range.isValid())
51 s += "\t<Range>" + QString::number(d_range.minValue(), 'g', 15) + "\t" + QString::number(d_range.maxValue(), 'g', 15) + "</Range>\n";
52 QwtArray <double> colors = colorStops();
53 int stops = (int)colors.size();
54 s += "\t<ColorStops>" + QString::number(stops - 2) + "</ColorStops>\n";
55 for (int i = 1; i < stops - 1; i++){
56 s += "\t<Stop>" + QString::number(colors[i], 'g', 15) + "\t";
57 s += color(i).name();
58 s += "</Stop>\n";
59 }
60 return s += "</ColorMap>\n";
61 }
62
fromXmlStringList(const QStringList & lst)63 LinearColorMap LinearColorMap::fromXmlStringList(const QStringList& lst)
64 {
65 QStringList::const_iterator line = lst.begin();
66 QString s = (*line).stripWhiteSpace();
67
68 int mode = s.remove("<Mode>").remove("</Mode>").stripWhiteSpace().toInt();
69 s = *(++line);
70 QColor color1 = QColor(s.remove("<MinColor>").remove("</MinColor>").stripWhiteSpace());
71 s = *(++line);
72 QColor color2 = QColor(s.remove("<MaxColor>").remove("</MaxColor>").stripWhiteSpace());
73
74 LinearColorMap colorMap = LinearColorMap(color1, color2);
75 colorMap.setMode((QwtLinearColorMap::Mode)mode);
76
77 s = *(++line);
78 if (s.contains("<Range>")){
79 QStringList l = QStringList::split("\t", s.remove("<Range>").remove("</Range>"));
80 if (l.size() == 2)
81 colorMap.setIntensityRange(QwtDoubleInterval(l[0].toDouble(), l[1].toDouble()));
82 s = *(++line);
83 }
84
85 int stops = s.remove("<ColorStops>").remove("</ColorStops>").stripWhiteSpace().toInt();
86 for (int i = 0; i < stops; i++){
87 s = (*(++line)).stripWhiteSpace();
88 QStringList l = QStringList::split("\t", s.remove("<Stop>").remove("</Stop>"));
89 colorMap.addColorStop(l[0].toDouble(), QColor(l[1]));
90 }
91
92 return colorMap;
93 }
94