##// END OF EJS Templates
Initial ColorMap Chart implementation....
jeandet -
r2899:905c34fe48ad
parent child
Show More
@@ -0,0 +1,179
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #include "chartcolorbaraxisy_p.h"
24 #include <QtCharts/QAbstractAxis>
25 #include <private/chartpresenter_p.h>
26 //#include <QtCharts/QColorBarAxis>
27 #include "colorbaraxis/qcolorbaraxis.h"
28 #include <private/abstractchartlayout_p.h>
29 #include <QtWidgets/QGraphicsLayout>
30 #include <QtCore/QtMath>
31 #include <QtCore/QDebug>
32
33 QT_CHARTS_BEGIN_NAMESPACE
34
35 ChartColorBarAxisY::ChartColorBarAxisY(QColorBarAxis *axis, QPoint pos, qreal height, QLinearGradient gradient, QGraphicsItem *item)
36 : VerticalAxis(axis, item),
37 m_axis(axis),
38 m_gradient(gradient),
39 m_position(pos),
40 m_height(height)
41 {
42 QObject::connect(m_axis, SIGNAL(tickCountChanged(int)), this, SLOT(handleTickCountChanged(int)));
43 QObject::connect(m_axis, SIGNAL(minorTickCountChanged(int)),
44 this, SLOT(handleMinorTickCountChanged(int)));
45 QObject::connect(m_axis, SIGNAL(labelFormatChanged(QString)), this, SLOT(handleLabelFormatChanged(QString)));
46
47 createColorBar();
48
49 }
50
51 ChartColorBarAxisY::~ChartColorBarAxisY()
52 {
53 }
54
55 QVector<qreal> ChartColorBarAxisY::calculateLayout() const
56 {
57 int tickCount = m_axis->tickCount();
58
59 Q_ASSERT(tickCount >= 2);
60
61 QVector<qreal> points;
62 points.resize(tickCount);
63
64 const QRectF &gridRect = gridGeometry();
65
66 const qreal deltaY = gridRect.height() / (qreal(tickCount) - 1.0);
67 for (int i = 0; i < tickCount; ++i)
68 points[i] = qreal(i) * -deltaY + gridRect.bottom();
69
70 return points;
71 }
72
73 void ChartColorBarAxisY::updateGeometry()
74 {
75 const QVector<qreal> &layout = ChartAxisElement::layout();
76 if (layout.isEmpty())
77 return;
78 setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
79 VerticalAxis::updateGeometry();
80 }
81
82 void ChartColorBarAxisY::handleTickCountChanged(int tick)
83 {
84 Q_UNUSED(tick);
85 QGraphicsLayoutItem::updateGeometry();
86 if (presenter()) presenter()->layout()->invalidate();
87 }
88
89 void ChartColorBarAxisY::handleMinorTickCountChanged(int tick)
90 {
91 Q_UNUSED(tick);
92 QGraphicsLayoutItem::updateGeometry();
93 if (presenter())
94 presenter()->layout()->invalidate();
95 }
96
97 void ChartColorBarAxisY::handleLabelFormatChanged(const QString &format)
98 {
99 Q_UNUSED(format);
100 QGraphicsLayoutItem::updateGeometry();
101 if(presenter()) presenter()->layout()->invalidate();
102 }
103
104 QSizeF ChartColorBarAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
105 {
106 Q_UNUSED(constraint)
107
108 QSizeF sh;
109 QSizeF base = VerticalAxis::sizeHint(which, constraint);
110 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
111 qreal width = 0;
112 // Height of vertical axis sizeHint indicates the maximum distance labels can extend past
113 // first and last ticks. Base height is irrelevant.
114 qreal height = 0;
115
116 switch (which)
117 {
118 case Qt::MinimumSize: {
119 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(),
120 QStringLiteral("..."),
121 axis()->labelsAngle());
122 width = boundingRect.width() + labelPadding() + base.width() + 61.0; //TODO : hint changed 1.0 to 61.0
123 height = boundingRect.height() / 2.0;
124 sh = QSizeF(width, height);
125 break;
126 }
127 case Qt::PreferredSize: {
128 qreal labelWidth = 0.0;
129 qreal firstHeight = -1.0;
130 foreach (const QString& s, ticksList) {
131 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
132 labelWidth = qMax(rect.width(), labelWidth);
133 height = rect.height();
134 if (firstHeight < 0.0)
135 firstHeight = height;
136 }
137 width = labelWidth + labelPadding() + base.width() + 62.0; //two pixels of tolerance //TODO : hint changed 2.0 to 62.0
138 height = qMax(height, firstHeight) / 2.0;
139 sh = QSizeF(width, height);
140 break;
141 }
142 default:
143 break;
144 }
145 return sh;
146 }
147
148 void ChartColorBarAxisY::createColorBar()
149 {
150 m_axis->setLabelsColor(QColor(0,0,255,0)); //make automatic labels disappear without changing layout (as it would be the case with setlabelVisible(false))
151
152 //gradient
153 QGradientStops stops = m_gradient.stops();
154 QLinearGradient gradient(0,m_position.y(),1,m_height);
155 foreach(QGradientStop stop, stops)
156 {
157 gradient.setColorAt(1-stop.first,stop.second);
158 }
159
160 //colorbar
161 QRectF rect(m_position.x()+10,m_position.y(),m_height/20,m_height); //remove +10 if needed
162
163 QGraphicsRectItem *colorbar = new QGraphicsRectItem(rect, this);
164 colorbar->setZValue(ChartPresenter::AxisZValue);
165 colorbar->setBrush(gradient);
166
167 //colorbar labels
168 for(int i=0;i<m_axis->tickCount();i++)
169 {
170 qreal value = m_axis->max() - (i * (m_axis->max() - m_axis->min()) / (m_axis->tickCount() - 1));
171 QGraphicsTextItem *label = new QGraphicsTextItem(this);
172 label->setPlainText(QString::number(value));
173 label->setPos((m_position.x()+10+m_height/20+1),(m_position.y()*0.85+i*(m_height/(m_axis->tickCount()-1)))); //remove +10 if needed
174 }
175 }
176
177 #include "moc_chartcolorbaraxisy_p.cpp"
178
179 QT_CHARTS_END_NAMESPACE
@@ -0,0 +1,63
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #ifndef CHARTCOLORBARAXISY_H
24 #define CHARTCOLORBARAXISY_H
25
26 #include <private/verticalaxis_p.h>
27 #include <QtGui>
28
29 QT_CHARTS_BEGIN_NAMESPACE
30
31 class QColorBarAxis;
32
33 class ChartColorBarAxisY : public VerticalAxis
34 {
35 Q_OBJECT
36 public:
37 ChartColorBarAxisY(QColorBarAxis *axis, QPoint pos, qreal height, QLinearGradient gradient, QGraphicsItem *item = 0);
38 ~ChartColorBarAxisY();
39
40 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
41
42
43
44 protected:
45 QVector<qreal> calculateLayout() const;
46 void updateGeometry();
47 private Q_SLOTS:
48 void handleTickCountChanged(int tick);
49 void handleMinorTickCountChanged(int tick);
50 void handleLabelFormatChanged(const QString &format);
51 void createColorBar();
52
53
54 private:
55 QColorBarAxis *m_axis;
56 QLinearGradient m_gradient;
57 QPoint m_position;
58 qreal m_height;
59 };
60
61 QT_CHARTS_END_NAMESPACE
62
63 #endif // CHARTCOLORBARAXISY_H
@@ -0,0 +1,277
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 //#include <QtCharts/QColorBarAxis> // TODO : fix this
24 #include "colorbaraxis/qcolorbaraxis.h"
25 //#include <private/qcolorbaraxis_p.h>
26 #include "colorbaraxis/qcolorbaraxis_p.h"
27 #include <private/abstractdomain_p.h>
28 #include <private/chartdataset_p.h>
29 #include <private/chartpresenter_p.h>
30 #include <private/charttheme_p.h>
31
32 //#include <private/chartcolorbaraxisy_p.h>
33 #include <chartcolorbaraxisy_p.h>
34
35 #include <QtGui>
36
37 QT_CHARTS_BEGIN_NAMESPACE
38
39
40 /*!
41 Constructs an axis object which is a child of \a parent.
42 */
43 QColorBarAxis::QColorBarAxis(QRectF plotArea, QLinearGradient gradient, qreal min, qreal max, QObject *parent) :
44 QAbstractAxis(*new QColorBarAxisPrivate(plotArea, gradient, min, max, this), parent)
45 {
46 setGridLineVisible(false);
47 }
48
49 /*!
50 \internal
51 */
52 QColorBarAxis::QColorBarAxis(QColorBarAxisPrivate &d, QObject *parent)
53 : QAbstractAxis(d, parent)
54 {
55
56 }
57
58 /*!
59 Destroys the object
60 */
61 QColorBarAxis::~QColorBarAxis()
62 {
63 Q_D(QColorBarAxis);
64 if (d->m_chart)
65 d->m_chart->removeAxis(this);
66 }
67
68 void QColorBarAxis::setMin(qreal min)
69 {
70 Q_D(QColorBarAxis);
71 setRange(min, qMax(d->m_max, min));
72 }
73
74 qreal QColorBarAxis::min() const
75 {
76 Q_D(const QColorBarAxis);
77 return d->m_min;
78 }
79
80 void QColorBarAxis::setMax(qreal max)
81 {
82 Q_D(QColorBarAxis);
83 setRange(qMin(d->m_min, max), max);
84 }
85
86 qreal QColorBarAxis::max() const
87 {
88 Q_D(const QColorBarAxis);
89 return d->m_max;
90 }
91
92 void QColorBarAxis::setRange(qreal min, qreal max)
93 {
94 Q_D(QColorBarAxis);
95 d->setRange(min,max);
96 }
97
98
99 void QColorBarAxis::setTickCount(int count)
100 {
101 Q_D(QColorBarAxis);
102 if (d->m_tickCount != count && count >= 2) {
103 d->m_tickCount = count;
104 emit tickCountChanged(count);
105 }
106 }
107
108 int QColorBarAxis::tickCount() const
109 {
110 Q_D(const QColorBarAxis);
111 return d->m_tickCount;
112 }
113
114 void QColorBarAxis::setMinorTickCount(int count)
115 {
116 Q_D(QColorBarAxis);
117 if (d->m_minorTickCount != count && count >= 0) {
118 d->m_minorTickCount = count;
119 emit minorTickCountChanged(count);
120 }
121 }
122
123 int QColorBarAxis::minorTickCount() const
124 {
125 Q_D(const QColorBarAxis);
126 return d->m_minorTickCount;
127 }
128
129 void QColorBarAxis::setLabelFormat(const QString &format)
130 {
131 Q_D(QColorBarAxis);
132 d->m_format = format;
133 emit labelFormatChanged(format);
134 }
135
136 QString QColorBarAxis::labelFormat() const
137 {
138 Q_D(const QColorBarAxis);
139 return d->m_format;
140 }
141
142 /*!
143 Returns the type of the axis
144 */
145 QAbstractAxis::AxisType QColorBarAxis::type() const
146 {
147 return AxisTypeValue;
148 //TODO : AxisTypeColorBar
149 }
150
151
152
153 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
154
155 QColorBarAxisPrivate::QColorBarAxisPrivate(QRectF plotArea, QLinearGradient gradient, qreal min, qreal max, QColorBarAxis *q)
156 : QAbstractAxisPrivate(q),
157 m_plotArea(plotArea),
158 m_min(min),
159 m_max(max),
160 m_tickCount(5),
161 m_minorTickCount(0),
162 m_format(QString::null),
163 // m_applying(false)
164 m_gradient(gradient)
165 {
166
167 }
168
169 QColorBarAxisPrivate::~QColorBarAxisPrivate()
170 {
171
172 }
173
174 void QColorBarAxisPrivate::initializeGraphics(QGraphicsItem *parent)
175 {
176 Q_Q(QColorBarAxis);
177 ChartAxisElement *axis(0);
178
179 setAlignment(Qt::AlignRight); //also set orientation (Vertical in this case)
180
181 //TODO : change this position
182 qreal x1;
183 qreal y1;
184 qreal x2;
185 qreal y2;
186 m_plotArea.getCoords(&x1,&y1,&x2,&y2);
187 QPoint pos(x2+5,y1);
188 axis = new ChartColorBarAxisY(q, pos, m_plotArea.height(),m_gradient, parent);
189
190 m_item.reset(axis);
191 QAbstractAxisPrivate::initializeGraphics(parent);
192 }
193
194 void QColorBarAxisPrivate::initializeDomain(AbstractDomain *domain)
195 {
196 //domain is not supposed to have a rangeZ
197
198 // if (orientation() == Qt::Vertical) {
199 // if (!qFuzzyIsNull(m_max - m_min))
200 // domain->setRangeY(m_min, m_max);
201 // else
202 // setRange(domain->minY(), domain->maxY());
203 // }
204 }
205
206 void QColorBarAxisPrivate::setMin(const QVariant &min)
207 {
208 Q_Q(QColorBarAxis);
209 bool ok;
210 qreal value = min.toReal(&ok);
211 if (ok)
212 q->setMin(value);
213 }
214
215 void QColorBarAxisPrivate::setMax(const QVariant &max)
216 {
217 Q_Q(QColorBarAxis);
218 bool ok;
219 qreal value = max.toReal(&ok);
220 if (ok)
221 q->setMax(value);
222 }
223
224 void QColorBarAxisPrivate::setRange(const QVariant &min, const QVariant &max)
225 {
226 Q_Q(QColorBarAxis);
227 bool ok1;
228 bool ok2;
229 qreal value1 = min.toReal(&ok1);
230 qreal value2 = max.toReal(&ok2);
231 if (ok1 && ok2)
232 q->setRange(value1, value2);
233 }
234
235 void QColorBarAxisPrivate::setRange(qreal min,qreal max)
236 {
237 Q_Q(QColorBarAxis);
238 bool changed = false;
239
240 if (min > max)
241 return;
242
243 bool changeMin = false;
244 if (m_min == 0 || min == 0)
245 changeMin = !qFuzzyCompare(1 + m_min, 1 + min);
246 else
247 changeMin = !qFuzzyCompare(m_min, min);
248
249 bool changeMax = false;
250 if (m_max == 0 || max == 0)
251 changeMax = !qFuzzyCompare(1 + m_max, 1 + max);
252 else
253 changeMax = !qFuzzyCompare(m_max, max);
254
255 if (changeMin) {
256 m_min = min;
257 changed = true;
258 emit q->minChanged(min);
259 }
260
261 if (changeMax) {
262 m_max = max;
263 changed = true;
264 emit q->maxChanged(max);
265 }
266
267 if (changed) {
268 emit rangeChanged(min,max);
269 emit q->rangeChanged(min, max);
270 }
271 }
272
273
274 #include "moc_qcolorbaraxis.cpp"
275 #include "moc_qcolorbaraxis_p.cpp"
276
277 QT_CHARTS_END_NAMESPACE
@@ -0,0 +1,76
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #ifndef QCOLORBARAXIS_H
24 #define QCOLORBARAXIS_H
25
26 #include <QtCharts/QAbstractAxis>
27
28 QT_CHARTS_BEGIN_NAMESPACE
29
30 class QColorBarAxisPrivate;
31
32 class QT_CHARTS_EXPORT QColorBarAxis : public QAbstractAxis
33 {
34 Q_OBJECT
35 public:
36 explicit QColorBarAxis(QRectF plotArea,QLinearGradient gradient, qreal min, qreal max,QObject *parent = 0);
37 ~QColorBarAxis();
38
39 protected:
40 QColorBarAxis(QColorBarAxisPrivate &d, QObject *parent = 0);
41
42 public:
43 AxisType type() const;
44
45 //range handling
46 void setMin(qreal min);
47 qreal min() const;
48 void setMax(qreal max);
49 qreal max() const;
50 void setRange(qreal min, qreal max);
51
52 //ticks handling
53 void setTickCount(int count);
54 int tickCount() const;
55 void setMinorTickCount(int count);
56 int minorTickCount() const;
57
58 void setLabelFormat(const QString &format);
59 QString labelFormat() const;
60
61 Q_SIGNALS:
62 void minChanged(qreal min);
63 void maxChanged(qreal max);
64 void rangeChanged(qreal min, qreal max);
65 void tickCountChanged(int tickCount);
66 void minorTickCountChanged(int tickCount);
67 void labelFormatChanged(const QString &format);
68
69 private:
70 Q_DECLARE_PRIVATE(QColorBarAxis)
71 Q_DISABLE_COPY(QColorBarAxis)
72 };
73
74 QT_CHARTS_END_NAMESPACE
75
76 #endif // QCOLORBARAXIS_H
@@ -0,0 +1,66
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #ifndef QCOLORBARAXIS_P_H
24 #define QCOLORBARAXIS_P_H
25
26 //#include <QtCharts/QColorBarAxis> //TODO : fix this
27 #include "colorbaraxis/qcolorbaraxis.h"
28 #include <private/qabstractaxis_p.h>
29
30 QT_CHARTS_BEGIN_NAMESPACE
31
32 class QColorBarAxisPrivate : public QAbstractAxisPrivate
33 {
34 Q_OBJECT
35 public:
36 QColorBarAxisPrivate(QRectF plotArea, QLinearGradient gradient, qreal min, qreal max,QColorBarAxis *q);
37 ~QColorBarAxisPrivate();
38
39 public:
40 void initializeGraphics(QGraphicsItem* parent);
41 void initializeDomain(AbstractDomain *domain);
42
43 qreal min() { return m_min; }
44 qreal max() { return m_max; }
45 void setRange(qreal min,qreal max);
46
47 protected:
48 void setMin(const QVariant &min);
49 void setMax(const QVariant &max);
50 void setRange(const QVariant &min, const QVariant &max);
51
52 private:
53 qreal m_min;
54 qreal m_max;
55 int m_tickCount;
56 int m_minorTickCount;
57 QString m_format;
58 // bool m_applying;
59 QLinearGradient m_gradient;
60 QRectF m_plotArea;
61 Q_DECLARE_PUBLIC(QColorBarAxis)
62 };
63
64 QT_CHARTS_END_NAMESPACE
65
66 #endif // QCOLORBARAXIS_P_H
@@ -0,0 +1,299
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #include <private/colormapchart_p.h>
24 #include <QtCharts/QColorMapSeries>
25 #include <private/qcolormapseries_p.h>
26 #include <private/chartpresenter_p.h>
27 #include <private/abstractdomain_p.h>
28 #include <private/chartdataset_p.h>
29 #include <private/qabstractaxis_p.h>
30 #include <QtGui/QPainter>
31 #include <QColorBarAxis>
32
33 #include <QRgb>
34
35 #define nbOfColors 65000
36
37 QT_CHARTS_BEGIN_NAMESPACE
38
39 ColorMapChart::ColorMapChart(QColorMapSeries *series, QGraphicsItem *item)
40 : ChartItem(series->d_func(), item),
41 m_series(series),
42 m_dirty(true),
43 m_gradientType(Rainbow),
44 m_colorbar(Q_NULLPTR),
45 m_isColorBarDrawn(false),
46 m_currentClipRect(QRectF())
47 {
48 // QObject::connect(series, SIGNAL(pointReplaced(int)), this, SLOT(handlePointReplaced(int)));
49 // QObject::connect(series, SIGNAL(pointsReplaced()), this, SLOT(handlePointsReplaced()));
50 // QObject::connect(series, SIGNAL(pointAdded(int)), this, SLOT(handlePointAdded(int)));
51 // QObject::connect(series, SIGNAL(pointRemoved(int)), this, SLOT(handlePointRemoved(int)));
52 // QObject::connect(series, SIGNAL(pointsRemoved(int, int)), this, SLOT(handlePointsRemoved(int, int)));
53 // QObject::connect(this, SIGNAL(clicked(Point3D)), series, SIGNAL(clicked(Point3D)));
54 // QObject::connect(this, SIGNAL(hovered(Point3D,bool)), series, SIGNAL(hovered(Point3D,bool)));
55 // QObject::connect(this, SIGNAL(pressed(Point3D)), series, SIGNAL(pressed(Point3D)));
56 // QObject::connect(this, SIGNAL(released(Point3D)), series, SIGNAL(released(Point3D)));
57 // QObject::connect(this, SIGNAL(doubleClicked(Point3D)), series, SIGNAL(doubleClicked(Point3D)));
58
59 connect(this,SIGNAL(gradientTypeChanged()), this, SLOT(populateColorTable()));
60
61 m_colorTable = new QVector<QRgb>();
62 m_colorTable->reserve(nbOfColors);
63 populateColorTable();
64 }
65
66 ColorMapChart::~ColorMapChart()
67 {
68 delete m_colorTable;
69 delete m_colorbar;
70 }
71
72 void ColorMapChart::setDirty(bool dirty)
73 {
74 m_dirty = dirty;
75 }
76
77 QRectF ColorMapChart::boundingRect() const
78 {
79 return m_rect;
80 }
81
82 void ColorMapChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
83 {
84 Q_UNUSED(widget)
85 Q_UNUSED(option)
86
87 painter->save();
88 //m_series->setUseOpenGL();
89
90 QRectF plotAreaRect = m_series->chart()->plotArea();
91 QRectF clipRect = mapColorMapToPlotArea();
92 painter->setClipRect(clipRect);
93
94 if(m_currentClipRect !=clipRect)
95 {
96 m_currentClipRect = clipRect;
97 m_grid.reserve(clipRect.width()*clipRect.height());
98 m_grid.resize(clipRect.width()*clipRect.height());
99
100 m_series->getUniformGrid(qMax(m_series->minX(),domain()->minX()),qMin(m_series->maxX(),domain()->maxX()),qMax(m_series->minY(),domain()->minY()),qMin(m_series->maxY(),domain()->maxY()), clipRect.width(),clipRect.height(),m_grid, QColorMapSeries::LastPixel);
101 addColorBar(plotAreaRect);
102 }
103
104 QImage colorMapImage(clipRect.width(),clipRect.height(),QImage::Format_RGB32);
105 //http://doc.qt.io/qt-4.8/qimage.html#details :Warning: This will create a QImage with uninitialized data. Call fill() to fill the image with an appropriate pixel value before drawing onto it with QPainter.
106 colorMapImage.fill(QColor(Qt::white).rgb());
107
108 fillColorMapImage(colorMapImage);
109
110 painter->drawImage(clipRect,colorMapImage);
111 painter->restore();
112 }
113
114 void ColorMapChart::addColorBar(QRectF plotAreaRect)
115 {
116 double maxZ = m_series->maxZ();
117 double minZ = m_series->minZ();
118
119 if(m_isColorBarDrawn)
120 m_series->chart()->removeAxis(m_colorbar);
121
122 m_colorbar = new QColorBarAxis(plotAreaRect,createColorMapGradient(m_gradientType),minZ, maxZ,this);
123 m_series->chart()->addAxis(m_colorbar, Qt::AlignRight);
124 m_isColorBarDrawn = true;
125 }
126
127 QRectF ColorMapChart::mapColorMapToPlotArea()
128 {
129 QRectF plotAreaRect = m_series->chart()->plotArea();
130
131 double seriesMinX = m_series->minX();
132 double seriesMaxX = m_series->maxX();
133 double seriesMinY = m_series->minY();
134 double seriesMaxY = m_series->maxY();
135
136 double domainMinX = domain()->minX();
137 double domainMaxX = domain()->maxX();
138 double domainMinY = domain()->minY();
139 double domainMaxY = domain()->maxY();
140
141 double widthToPaint = (qMin(seriesMaxX, domainMaxX)-qMax(seriesMinX, domainMinX))*plotAreaRect.width()/(domainMaxX-domainMinX);
142 double heightTopaint= (qMin(seriesMaxY, domainMaxY)-qMax(seriesMinY, domainMinY))*plotAreaRect.height()/(domainMaxY-domainMinY);
143
144 QSizeF size = QSize(widthToPaint,heightTopaint);
145
146 double pointX = (qMax(seriesMinX,domainMinX)-domainMinX)*plotAreaRect.width()/(domainMaxX-domainMinX);
147 double pointY = (domainMaxY-qMin(seriesMaxY,domainMaxY))*plotAreaRect.height()/(domainMaxY-domainMinY);
148
149 return QRectF(QPointF(pointX,pointY) ,size);
150 }
151
152 void ColorMapChart::fillColorMapImage(QImage &colorMapImage)
153 {
154 double maxZ = m_series->maxZ();
155 double minZ = m_series->minZ();
156 double rangeZ = maxZ - minZ;
157
158 int imageWidth = colorMapImage.width();
159 int imageHeight = colorMapImage.height();
160
161 for(int i=0;i<imageWidth;i++)
162 {
163 for(int j=0;j<imageHeight;j++)
164 {
165 double value = m_grid.at(i+j*(imageWidth));
166 double pix=((value-minZ)/rangeZ);
167 int indexInColorTable = pix*(nbOfColors-1);
168 colorMapImage.setPixel(i,j,m_colorTable->at(indexInColorTable));
169 }
170 }
171 }
172
173 /*!
174 Returns the predefined QLinearGradient corresponding to the \a gradientType passed as argument.
175 */
176 QLinearGradient ColorMapChart::createColorMapGradient(GradientType gradientType)
177 {
178 QLinearGradient gradient(0,0,1,100);
179 switch(gradientType)
180 {
181 case Rainbow :
182 gradient.setColorAt(1.0,Qt::darkRed);
183 gradient.setColorAt(0.8,Qt::red);
184 gradient.setColorAt(0.6,Qt::yellow);
185 gradient.setColorAt(0.4,Qt::green);
186 gradient.setColorAt(0.2,Qt::cyan);
187 gradient.setColorAt(0.0,Qt::blue);
188 break;
189 case CyclingRainbow :
190 gradient.setColorAt(1.0,Qt::red);
191 gradient.setColorAt(0.8,Qt::yellow);
192 gradient.setColorAt(0.6,Qt::green);
193 gradient.setColorAt(0.4,Qt::cyan);
194 gradient.setColorAt(0.2,Qt::blue);
195 gradient.setColorAt(0.0,Qt::magenta);
196 break;
197 case BlackAndWhite :
198 gradient.setColorAt(1.0, Qt::black);
199 gradient.setColorAt(0.0, Qt::white);
200 break;
201 case ReverseBlackAndWhite :
202 gradient.setColorAt(1.0, Qt::white);
203 gradient.setColorAt(0.0, Qt::black);
204 break;
205 default:
206 break;
207 }
208 return gradient;
209 }
210
211 /*!
212 Changes the type of gradient used to paint the ColorMap.
213 */
214 void ColorMapChart::changeGradient(GradientType gradientType)
215 {
216 if(m_gradientType == gradientType)
217 return;
218 else
219 m_gradientType = gradientType;
220 emit gradientTypeChanged();
221 }
222
223 /*!
224 Creates a color table corresponding to the gradient type currently selected.
225 */
226 void ColorMapChart::populateColorTable()
227 {
228 QLinearGradient gradient = createColorMapGradient(m_gradientType);
229 QGradientStops colorStops = gradient.stops();
230
231 for(int i=0;i<nbOfColors;i++)
232 {
233 double colorIndex = (double)i/nbOfColors;
234 for(int k =0;k<colorStops.size()-1;k++)
235 {
236 QGradientStop lowerBound = colorStops.at(k);
237 QGradientStop upperBound = colorStops.at(k+1);
238 if(colorIndex >= lowerBound.first && colorIndex < upperBound.first)
239 {
240 double ratio = (colorIndex-lowerBound.first)/(upperBound.first - lowerBound.first);
241 int red = (int)((1-ratio)*lowerBound.second.red() + ratio*upperBound.second.red());
242 int green = (int)((1-ratio)*lowerBound.second.green() + ratio*upperBound.second.green());
243 int blue = (int)((1-ratio)*lowerBound.second.blue() + ratio*upperBound.second.blue());
244 m_colorTable->append(qRgb(red, green, blue));
245 break;
246 }
247 else
248 {
249 if(k==colorStops.size()-2)
250 {
251 m_colorTable->append(qRgb(colorStops.at(colorStops.size()-1).second.red(), colorStops.at(colorStops.size()-1).second.green(), colorStops.at(colorStops.size()-1).second.blue()));
252 }
253 }
254 }
255 }
256 }
257
258
259 //handlers
260
261 void ColorMapChart::handlePointAdded(int index)
262 {
263
264 }
265
266 void ColorMapChart::handlePointRemoved(int index)
267 {
268
269 }
270
271 void ColorMapChart::handlePointsRemoved(int index, int count)
272 {
273
274 }
275
276 void ColorMapChart::handlePointReplaced(int index)
277 {
278
279 }
280
281 void ColorMapChart::handlePointsReplaced()
282 {
283
284 }
285
286 void ColorMapChart::handleDomainUpdated()
287 {
288
289 }
290
291 bool ColorMapChart::isEmpty()
292 {
293
294 }
295
296
297 #include "moc_colormapchart_p.cpp"
298
299 QT_CHARTS_END_NAMESPACE
@@ -0,0 +1,21
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
3 QT+=concurrent
4 #QMAKE_CXXFLAGS+= -fopenmp
5 #QMAKE_LFLAGS += -fopenmp
6
7 SOURCES += \
8 $$PWD/qcolormapseries.cpp \
9 $$PWD/colormapchart.cpp \
10 $$PWD/colormapdatapart.cpp \
11 $$PWD/point3d.cpp
12
13 PRIVATE_HEADERS += \
14 $$PWD/qcolormapseries_p.h \
15 $$PWD/colormapchart_p.h
16
17 PUBLIC_HEADERS += \
18 $$PWD/qcolormapseries.h \
19 $$PWD/point3d.h \
20 $$PWD/colormapdatapart.h
21
@@ -0,0 +1,154
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #ifndef COLORMAPCHART_H
24 #define COLORMAPCHART_H
25
26 #include "point3d.h"
27 #include <QtCharts/QChartGlobal>
28 #include <private/chartitem_p.h>
29 #include <QtCharts/QValueAxis>
30 #include <QtGui/QPen>
31
32 //#include <QtCharts/QColorBarAxis> TODO : fix this
33 #include "qcolorbaraxis.h"
34
35 QT_CHARTS_BEGIN_NAMESPACE
36
37 class ChartPresenter;
38 class QColorMapSeries;
39
40 class ColorMapChart : public ChartItem
41 {
42 Q_OBJECT
43 public:
44
45 enum GradientType
46 {
47 Rainbow,
48 CyclingRainbow,
49 BlackAndWhite,
50 ReverseBlackAndWhite
51 };
52
53 explicit ColorMapChart(QColorMapSeries *series,QGraphicsItem *item = 0);
54 ~ColorMapChart();
55
56 bool isDirty() const { return m_dirty; }
57 void setDirty(bool dirty);
58
59 // from QGraphicsItem
60 QRectF boundingRect() const;
61 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
62
63 public Q_SLOTS:
64 void handlePointAdded(int index);
65 void handlePointRemoved(int index);
66 void handlePointsRemoved(int index, int count);
67 void handlePointReplaced(int index);
68 void handlePointsReplaced();
69 void handleDomainUpdated();
70
71 private slots :
72 void populateColorTable();
73
74 Q_SIGNALS:
75 void clicked(const Point3D &point);
76 void hovered(const Point3D &point, bool state);
77 void pressed(const Point3D &point);
78 void released(const Point3D &point);
79 void doubleClicked(const Point3D &point);
80 void gradientTypeChanged();
81
82 private:
83 inline bool isEmpty();
84 void addColorBar(QRectF plotAreaRect);
85
86 QRectF mapColorMapToPlotArea();
87 void fillColorMapImage(QImage &colorMapImage);
88 QLinearGradient createColorMapGradient(GradientType gradientType);
89 void changeGradient(GradientType gradientType);
90
91 protected:
92 QColorMapSeries *m_series;
93 QVector<Point3D> m_points;
94 QRectF m_rect;
95 bool m_dirty;
96 QVector<QRgb> *m_colorTable;
97 GradientType m_gradientType;
98 bool m_isColorBarDrawn;
99 QColorBarAxis *m_colorbar;
100 QRectF m_currentClipRect;
101 QVector<double> m_grid;
102 };
103
104 //class PixmapMarker: public QGraphicsRectItem
105 //{
106
107 //public:
108 // PixmapMarker(qreal x, qreal y, qreal w, qreal h, ColorMapChart *parent)
109 // : QGraphicsRectItem(x, y, w, h, parent),
110 // m_parent(parent)
111 // {
112 // setAcceptHoverEvents(true);
113 // setFlag(QGraphicsItem::ItemIsSelectable);
114 // }
115
116 //protected:
117 // void mousePressEvent(QGraphicsSceneMouseEvent *event)
118 // {
119 // QGraphicsRectItem::mousePressEvent(event);
120 // m_parent->markerPressed(this);
121 // m_parent->setMousePressed();
122 // }
123 // void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
124 // {
125 // QGraphicsRectItem::hoverEnterEvent(event);
126 // m_parent->markerHovered(this, true);
127 // }
128 // void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
129 // {
130 // QGraphicsRectItem::hoverLeaveEvent(event);
131 // m_parent->markerHovered(this, false);
132 // }
133 // void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
134 // {
135 // QGraphicsRectItem::mouseReleaseEvent(event);
136 // m_parent->markerReleased(this);
137 // if (m_parent->mousePressed())
138 // m_parent->markerSelected(this);
139 // m_parent->setMousePressed(false);
140 // }
141 // void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
142 // {
143 // QGraphicsRectItem::mouseDoubleClickEvent(event);
144 // m_parent->markerDoubleClicked(this);
145 // }
146
147 //private:
148 // ColorMapChart *m_parent;
149 //};
150
151
152 QT_CHARTS_END_NAMESPACE
153
154 #endif // COLORMAPCHART_H
@@ -0,0 +1,78
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #include "colormapdatapart.h"
24
25 QT_CHARTS_BEGIN_NAMESPACE
26
27 ColorMapDataPart::ColorMapDataPart()
28 {
29
30 }
31
32 ColorMapDataPart::ColorMapDataPart(ColorMapDataPart *dataPart)
33 {/*
34 QVector<double> *m_timeSeries;
35 QVector<double> *m_ySeries;
36 QVector<double> *m_dataSeries;*/
37 this->m_dataSeries = new QVector<double>(dataPart->dataSeries());
38 this->m_ySeries = new QVector<double>(dataPart->ySeries());
39 this->m_timeSeries = new QVector<double>(dataPart->timesSeries());
40 }
41
42 ColorMapDataPart::ColorMapDataPart(QVector<double> *timeSeries, QVector<double> *ySeries, QVector<double> *dataSeries)
43 : m_timeSeries(timeSeries),m_ySeries(ySeries),m_dataSeries(dataSeries)
44 {
45
46 }
47
48 ColorMapDataPart::~ColorMapDataPart()
49 {
50 delete m_dataSeries;
51 delete m_ySeries;
52 delete m_timeSeries;
53 }
54
55 int ColorMapDataPart::find(double val, const QVector<double> &vect, bool more)
56 {
57 if(val >=vect.last() && more)
58 return vect.count();
59 if(val <= vect.first() && !more)
60 return -1;
61
62 int guessedIndex = (val-vect.first())*vect.count()/(vect.last()-vect.first());
63 if(guessedIndex>(vect.count()-1))guessedIndex=vect.count()-1;
64
65 if(more)
66 while(vect[guessedIndex]<val){guessedIndex++;}
67 else
68 while(vect[guessedIndex--]>val){}
69 return guessedIndex;
70 }
71
72 QPair<int, int> ColorMapDataPart::getRange(const QVector<double> &vect, double start, double stop)
73 {
74 return QPair<int, int>(find(start,vect, false),find(stop,vect, true));
75 }
76
77
78 QT_CHARTS_END_NAMESPACE
@@ -0,0 +1,55
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #ifndef COLORMAPDATAPART_H
24 #define COLORMAPDATAPART_H
25
26 #include <QtCharts/QChartGlobal>
27 #include <QtCore>
28
29 QT_CHARTS_BEGIN_NAMESPACE
30
31 class QT_CHARTS_EXPORT ColorMapDataPart
32 {
33 public:
34 explicit ColorMapDataPart();
35 ColorMapDataPart(ColorMapDataPart* dataPart);
36 explicit ColorMapDataPart(QVector<double> *timeSeries, QVector<double> *ySeries, QVector<double> *dataSeries);
37 ~ColorMapDataPart();
38 inline const QVector<double>& timesSeries(){return *this->m_timeSeries;}
39 inline const QVector<double>& ySeries(){return *this->m_ySeries;}
40 inline const QVector<double>& dataSeries(){return *this->m_dataSeries;}
41 inline void setTimeSeries(QVector<double> *timeSeries){this->m_timeSeries =timeSeries;}
42 inline void setYSeries(QVector<double> *ySeries){this->m_ySeries =ySeries;}
43 inline void setDataSeries(QVector<double> *dataSeries){this->m_dataSeries =dataSeries;}
44 QPair<int,int> getRange(const QVector<double> &vect, double start, double stop);
45 protected:
46 QVector<double> *m_timeSeries;
47 QVector<double> *m_ySeries;
48 QVector<double> *m_dataSeries;
49 private:
50 int find(double val, const QVector<double> &vect, bool more);
51 };
52
53 QT_CHARTS_END_NAMESPACE
54
55 #endif // COLORMAPDATAPART_H
@@ -0,0 +1,35
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #include "point3d.h"
24
25
26 Point3D::Point3D()
27 : QPointF()
28 {
29 }
30
31 Point3D::Point3D(qreal x, qreal y, qreal value)
32 : QPointF(x,y),
33 m_value(value)
34 {
35 }
@@ -0,0 +1,38
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #ifndef POINT3D_H
24 #define POINT3D_H
25
26 #include <QPointF>
27
28 class Point3D : public QPointF
29 {
30 public:
31 Point3D();
32 Point3D(qreal x, qreal y, qreal value);
33 inline void setValue(qreal value){this->m_value = value;}
34 inline qreal value(){return this->m_value;}
35 private:
36 qreal m_value;
37 };
38 #endif // POINT3D_H
@@ -0,0 +1,1
1 #include "../../src/charts/legend/qcolormaplegendmarker.h"
@@ -0,0 +1,1
1 #include "../../../../../src/charts/legend/qcolormaplegendmarker_p.h"
This diff has been collapsed as it changes many lines, (658 lines changed) Show them Hide them
@@ -0,0 +1,658
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #include "qcolormapseries.h"
24 #include <private/qcolormapseries_p.h>
25 #include <private/abstractdomain_p.h>
26 #include <QtCharts/QValueAxis>
27 #include <private/colormapchart_p.h>
28 #include <QtCharts/QColorMapLegendMarker>
29 #include <private/charthelpers_p.h>
30 #include <private/qchart_p.h>
31 #include <QtGui/QPainter>
32 #include <cmath>
33
34 #include <cmath>
35 #include <QtConcurrent>
36
37 #include "qcolorbaraxis.h"
38
39 QT_CHARTS_BEGIN_NAMESPACE
40
41 /*!
42 \internal
43
44 Constructs empty series object which is a child of \a parent.
45 When series object is added to QChart instance ownerships is transferred.
46 */
47 QColorMapSeries::QColorMapSeries(QObject *parent)
48 : QAbstractSeries(*new QColorMapSeriesPrivate(this), parent)
49 {
50 }
51
52 /*!
53 \internal
54
55 Constructs empty series object which is a child of \a parent.
56 When series object is added to QChart instance ownerships is transferred.
57 */
58 QColorMapSeries::QColorMapSeries(QColorMapSeriesPrivate &d, QObject *parent)
59 : QAbstractSeries(d, parent)
60 {
61 }
62
63
64 /*!
65 Destroys the object. Series added to QChart instances are owned by those,
66 and are destroyed when QChart instances are destroyed.
67 */
68 QColorMapSeries::~QColorMapSeries()
69 {
70 }
71
72 /*!
73 \fn virtual SeriesType QBoxPlotSeries::type() const
74 \brief Returns type of series.
75 \sa QAbstractSeries, SeriesType
76 */
77 QAbstractSeries::SeriesType QColorMapSeries::type() const
78 {
79 return QAbstractSeries::SeriesTypeColorMap;
80 }
81
82
83 /*!
84 Adds data part \a dataPart to the series.\n
85 If \a copy is true, adds a copy of the data part instead.
86 */
87 void QColorMapSeries::append(ColorMapDataPart* dataPart, bool copy)
88 {
89 Q_D(QColorMapSeries);
90 if(copy)
91 d->m_dataParts << new ColorMapDataPart(dataPart);
92 else
93 d->m_dataParts << dataPart;
94 d->recomputeDataRange();
95 emit dataPartAdded(d->m_dataParts.count() - 1);
96 }
97
98 /*!
99 This is an overloaded function.\n
100 Adds data parts \a dataParts to the series.\n
101 If \a copy is true, adds a copy of the data part instead.
102 */
103 void QColorMapSeries::append(const QList<ColorMapDataPart*> &dataParts, bool copy)
104 {
105 foreach (ColorMapDataPart* dataPart , dataParts)
106 append(dataPart,copy);
107 }
108
109
110 /*!
111 Returns number of data parts within series.
112 */
113 int QColorMapSeries::count() const
114 {
115 Q_D(const QColorMapSeries);
116 return d->m_dataParts.count();
117 }
118
119
120 /*!
121 Stream operator for adding a data part \a point to the series.
122 \sa append()
123 */
124 QColorMapSeries &QColorMapSeries::operator <<(const ColorMapDataPart &dataPart)
125 {
126 append(new ColorMapDataPart(dataPart));
127 return *this;
128 }
129
130 /*!
131 Stream operator for adding a vector of data parts \a dataParts to the series.
132 \sa append()
133 */
134 QColorMapSeries &QColorMapSeries::operator <<(const QList<ColorMapDataPart*> &dataParts)
135 {
136 append(dataParts);
137 return *this;
138 }
139
140 /*!
141 Returns a ColorMapData part containing a uniform grid of data points to be mapped in a ColorMapChart.\n
142 The rectangle of data returned is determined by \a width and \height,\n
143 from the position \a xpos , \a ypos (starting at the top left corner of the plot area).\n
144 When there are more points than pixels, \a strategy is applied to determine which to choose.
145 */
146 void QColorMapSeries::getUniformGrid(double minX, double maxX, double minY, double maxY, int width, int height, QVector<double> &grid, QColorMapSeries::Strategy strategy)
147 {
148 Q_D(QColorMapSeries);
149 d->getUniformGrid(minX, maxX,minY,maxY,width,height, grid, strategy);
150 }
151
152 //void QColorMapSeries::attachAxis(QAbstractAxis *axis)
153 //{
154 // axis = static_cast<QColorBarAxis*>();
155 // if(axis)
156 // {
157
158 // }
159 // else
160 // {
161 // QAbstractSeries::attachAxis(axis);
162 // }
163 //}
164
165 /*!
166 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
167 pen from chart theme is used.
168 \sa QChart::setTheme()
169 */
170 void QColorMapSeries::setPen(const QPen &pen)
171 {
172 Q_D(QColorMapSeries);
173 if (d->m_pen != pen)
174 {
175 //bool emitColorChanged = d->m_pen.color() != pen.color();
176 d->m_pen = pen;
177 emit d->updated();
178 // if (emitColorChanged)
179 // emit colorChanged(pen.color());
180 emit penChanged(pen);
181 }
182 }
183
184 QPen QColorMapSeries::pen() const
185 {
186 Q_D(const QColorMapSeries);
187 if (d->m_pen == QChartPrivate::defaultPen())
188 return QPen();
189 else
190 return d->m_pen;
191 }
192
193 /*!
194 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
195 from chart theme setting is used.
196 \sa QChart::setTheme()
197 */
198 void QColorMapSeries::setBrush(const QBrush &brush)
199 {
200 Q_D(QColorMapSeries);
201 if (d->m_brush != brush)
202 {
203 d->m_brush = brush;
204 emit d->updated();
205 }
206 }
207
208 QBrush QColorMapSeries::brush() const
209 {
210 Q_D(const QColorMapSeries);
211 if (d->m_brush == QChartPrivate::defaultBrush())
212 return QBrush();
213 else
214 return d->m_brush;
215 }
216
217 //void QColorMapSeries::setColor(const QColor &color)
218 //{
219 // QPen p = pen();
220 // if (p.color() != color)
221 // {
222 // p.setColor(color);
223 // setPen(p);
224 // }
225 //}
226
227 //QColor QColorMapSeries::color() const
228 //{
229 // return pen().color();
230 //}
231
232 //void QColorMapSeries::setPointsVisible(bool visible)
233 //{
234 // Q_D(QColorMapSeries);
235 // if (d->m_pointsVisible != visible)
236 // {
237 // d->m_pointsVisible = visible;
238 // emit d->updated();
239 // }
240 //}
241
242 //bool QColorMapSeries::pointsVisible() const
243 //{
244 // Q_D(const QColorMapSeries);
245 // return d->m_pointsVisible;
246 //}
247
248 //void QColorMapSeries::setPointLabelsFormat(const QString &format)
249 //{
250 // Q_D(QColorMapSeries);
251 // if (d->m_pointLabelsFormat != format)
252 // {
253 // d->m_pointLabelsFormat = format;
254 // emit pointLabelsFormatChanged(format);
255 // }
256 //}
257
258 //QString QColorMapSeries::pointLabelsFormat() const
259 //{
260 // Q_D(const QColorMapSeries);
261 // return d->m_pointLabelsFormat;
262 //}
263
264 //void QColorMapSeries::setPointLabelsVisible(bool visible)
265 //{
266 // Q_D(QColorMapSeries);
267 // if (d->m_pointLabelsVisible != visible)
268 // {
269 // d->m_pointLabelsVisible = visible;
270 // emit pointLabelsVisibilityChanged(visible);
271 // }
272 //}
273
274 //bool QColorMapSeries::pointLabelsVisible() const
275 //{
276 // Q_D(const QColorMapSeries);
277 // return d->m_pointLabelsVisible;
278 //}
279
280 //void QColorMapSeries::setPointLabelsFont(const QFont &font)
281 //{
282 // Q_D(QColorMapSeries);
283 // if (d->m_pointLabelsFont != font) {
284 // d->m_pointLabelsFont = font;
285 // emit pointLabelsFontChanged(font);
286 // }
287 //}
288
289 //QFont QColorMapSeries::pointLabelsFont() const
290 //{
291 // Q_D(const QColorMapSeries);
292 // return d->m_pointLabelsFont;
293 //}
294
295 //void QColorMapSeries::setPointLabelsColor(const QColor &color)
296 //{
297 // Q_D(QColorMapSeries);
298 // if (d->m_pointLabelsColor != color) {
299 // d->m_pointLabelsColor = color;
300 // emit pointLabelsColorChanged(color);
301 // }
302 //}
303
304 //QColor QColorMapSeries::pointLabelsColor() const
305 //{
306 // Q_D(const QColorMapSeries);
307 // if (d->m_pointLabelsColor == QChartPrivate::defaultPen().color())
308 // return QPen().color();
309 // else
310 // return d->m_pointLabelsColor;
311 //}
312
313 //void QColorMapSeries::setPointLabelsClipping(bool enabled)
314 //{
315 // Q_D(QColorMapSeries);
316 // if (d->m_pointLabelsClipping != enabled) {
317 // d->m_pointLabelsClipping = enabled;
318 // emit pointLabelsClippingChanged(enabled);
319 // }
320 //}
321
322 //bool QColorMapSeries::pointLabelsClipping() const
323 //{
324 // Q_D(const QColorMapSeries);
325 // return d->m_pointLabelsClipping;
326 //}
327 /*!
328 Returns the minimum value of the series on the X axis.
329 */
330 double QColorMapSeries::minX()
331 {
332 Q_D(QColorMapSeries);
333 return d->m_minX;
334 }
335
336 /*!
337 Returns the minimum value of the series on the Y axis.
338 */
339 double QColorMapSeries::minY()
340 {
341 Q_D(QColorMapSeries);
342 return d->m_minY;
343 }
344
345 /*!
346 Returns the minimum value of the series on the Z axis.
347 */
348 double QColorMapSeries::minZ()
349 {
350 Q_D(QColorMapSeries);
351 return d->m_minZ;
352 }
353
354 /*!
355 Returns the maximum value of the series on the X axis.
356 */
357 double QColorMapSeries::maxX()
358 {
359 Q_D(QColorMapSeries);
360 return d->m_maxX;
361 }
362
363 /*!
364 Returns the maximum value of the series on the Y axis.
365 */
366 double QColorMapSeries::maxY()
367 {
368 Q_D(QColorMapSeries);
369 return d->m_maxY;
370 }
371
372 /*!
373 Returns the maximum value of the series on the Z axis.
374 */
375 double QColorMapSeries::maxZ()
376 {
377 Q_D(QColorMapSeries);
378 return d->m_maxZ;
379 }
380
381 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
382
383 QColorMapSeriesPrivate::QColorMapSeriesPrivate(QColorMapSeries *q)
384 : QAbstractSeriesPrivate(q),
385 m_pen(QChartPrivate::defaultPen()),
386 m_brush(QChartPrivate::defaultBrush()),
387 m_pointsVisible(false),
388 m_pointLabelsFormat(QLatin1String("@xPoint, @yPoint")), //TODO : change
389 m_pointLabelsVisible(false),
390 m_pointLabelsFont(QChartPrivate::defaultFont()),
391 m_pointLabelsColor(QChartPrivate::defaultPen().color()),
392 m_pointLabelsClipping(true)
393 {
394 }
395
396 void QColorMapSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
397 {
398 Q_Q(QColorMapSeries);
399 ColorMapChart *colormap = new ColorMapChart(q,parent);
400 m_item.reset(colormap);
401 QAbstractSeriesPrivate::initializeGraphics(parent);
402 }
403
404 void QColorMapSeriesPrivate::initializeDomain()
405 {
406 domain()->setRange(m_minX, m_maxX, m_minY, m_maxY);
407 }
408
409 void QColorMapSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
410 {
411 // Q_Q(QColorMapSeries);
412
413 // const QList<QGradient> gradients = theme->seriesGradients();
414 // const QList<QColor> colors = theme->seriesColors();
415
416 // if (forced || QChartPrivate::defaultPen() == m_pen) {
417 // QPen pen;
418 // pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0));
419 // pen.setWidthF(2);
420 // q->setPen(pen);
421 // }
422
423 // if (forced || QChartPrivate::defaultBrush() == m_brush) {
424 // QBrush brush(colors.at(index % colors.size()));
425 // q->setBrush(brush);
426 // }
427
428 // if (forced || QChartPrivate::defaultPen().color() == m_pointLabelsColor) {
429 // QColor color = theme->labelBrush().color();
430 // q->setPointLabelsColor(color);
431 // }
432 }
433
434 QList<QLegendMarker*> QColorMapSeriesPrivate::createLegendMarkers(QLegend* legend)
435 {
436 Q_Q(QColorMapSeries);
437 QList<QLegendMarker*> list;
438 return list << new QColorMapLegendMarker(q,legend);
439 }
440
441 void QColorMapSeriesPrivate::initializeAxes()
442 {
443
444 }
445
446 QAbstractAxis::AxisType QColorMapSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
447 {
448 Q_UNUSED(orientation);
449 return QAbstractAxis::AxisTypeValue;
450 }
451
452 QAbstractAxis* QColorMapSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
453 {
454 Q_UNUSED(orientation);
455 return new QValueAxis;
456 }
457
458
459 void QColorMapSeriesPrivate::getUniformGrid(double minX, double maxX, double minY, double maxY, int width, int height, QVector<double> &grid, QColorMapSeries::Strategy strategy)
460 {
461 m_imageHeight = height;
462 m_imageWidth = width;
463 m_strategy = strategy;
464
465 m_requestedMinX = minX;
466 m_requestedMaxX = maxX;
467 m_requestedMinY = minY;
468 m_requestedMaxY = maxY;
469
470 int index=0;
471 std::generate(grid.begin(),grid.end(),[&index]{return index++;});
472
473 QFuture<void> futureWatcher = QtConcurrent::map(grid,[this] (double &index) { return computePixelValue(index); });
474
475 futureWatcher.waitForFinished();
476 }
477
478 double QColorMapSeriesPrivate::computePixelValue(double &index)
479 {
480 QVector<Point3D> cluster;
481 cluster.reserve(m_imageHeight*m_imageWidth/1000);
482 cluster.resize(0);
483
484 double dx = (m_requestedMaxX - m_requestedMinX)/m_imageWidth;
485 double dy = (m_requestedMaxY - m_requestedMinY)/m_imageHeight;
486
487 int x = (int)index%m_imageWidth;
488 int y = (int)index/m_imageWidth;
489 this->buildCluster(x,y,dx,dy,cluster);
490 if(m_strategy == QColorMapSeries::LastPixel)
491 index = this->clusterStrategyLast(cluster);
492 else if(m_strategy == QColorMapSeries::MeanPixel)
493 index = this->clusterStrategyMean(cluster);
494 else if(m_strategy == QColorMapSeries::MedianPixel)
495 index = this->clusterStrategyMedian(cluster);
496
497 return index;//not used
498 }
499
500 /*!
501 Recompute the data range on X, Y and Z axes each time a ColorMapDataPart is added to the series.\n
502 The minimum distance between 2 adjacent values on X and Y axis is added to the maximum on X and Y respectively\n
503 in order to take account of the width of the last element.
504 */
505 void QColorMapSeriesPrivate::recomputeDataRange()
506 {
507 //TODO : if non empty
508 m_minX = m_dataParts.first()->timesSeries().first();
509 m_maxX = m_dataParts.last()->timesSeries().last();
510
511 m_minY = m_dataParts.last()->ySeries().last();
512 m_maxY = m_dataParts.last()->ySeries().first();
513 m_minZ = m_dataParts.first()->dataSeries().first();
514 m_maxZ = m_dataParts.last()->dataSeries().last();
515 double minDeltaX=m_maxX-m_minX;
516 double minDeltaY=m_minY-m_maxY;
517 foreach(ColorMapDataPart* datapart, m_dataParts)
518 {
519 m_minY = qMin(datapart->ySeries().first(),m_minY);
520 m_maxY = qMax(datapart->ySeries().last(),m_maxY);
521 for(int i=1;i<datapart->timesSeries().size();i++)
522 {
523 double newDeltaX=datapart->timesSeries()[i]-datapart->timesSeries()[i-1];
524 minDeltaX=qMin(minDeltaX,newDeltaX);
525 }
526 for(int i=1;i<datapart->ySeries().size();i++)
527 {
528 double newDeltaY=datapart->ySeries()[i]-datapart->ySeries()[i-1];
529 minDeltaY=qMin(minDeltaY,newDeltaY);
530 }
531
532 for (int i = 0; i < datapart->dataSeries().count(); i++)
533 {
534 double z = datapart->dataSeries().at(i);
535 m_minZ = qMin(m_minZ, z);
536 m_maxZ = qMax(m_maxZ, z);
537 }
538 }
539 m_maxX+=minDeltaX;
540 m_maxY+=minDeltaY;
541 }
542
543 /*!
544 Returns the last value (bottom right Z value) of a \a cluster of points passed as argument.
545 */
546 double QColorMapSeriesPrivate::clusterStrategyLast(QVector<Point3D>& cluster)
547 {
548 if(!cluster.isEmpty())
549 return cluster.last().value();
550 return 0;
551 }
552
553 /*!
554 Returns the mean value (mean Z value) of a \a cluster of points passed as argument.
555 */
556 double QColorMapSeriesPrivate::clusterStrategyMean(QVector<Point3D>& cluster)
557 {
558 if(!cluster.isEmpty())
559 {
560 double sum=0;
561 int count =0;
562 for(int i =0;i<cluster.size();i++)
563 {
564 if(!std::isnan(Point3D(cluster.at(i)).value()))
565 {
566 sum+= Point3D(cluster.at(i)).value();
567 count++;
568 }
569 }
570 return (sum/count);
571 }
572 return 0;
573 }
574
575 /*!
576 Returns the median value (median Z value) of a \a cluster of points passed as argument.
577 */
578 double QColorMapSeriesPrivate::clusterStrategyMedian(QVector<Point3D>& cluster)
579 {
580 if(!cluster.isEmpty())
581 {
582 QVector<double> *copy = new QVector<double>();
583
584 for(int i =0;i<cluster.size();i++)
585 {
586 if(!std::isnan(Point3D(cluster.at(i)).value()))
587 copy->append(Point3D(cluster.at(i)).value());
588 }
589
590 if(!copy->isEmpty())
591 {
592 std::sort(copy->begin(), copy->end());
593
594 if(copy->count() & 1) // odd
595 {
596 int middle = (copy->count()+1)/2;
597 return copy->at(middle-1);
598 }
599 else //even
600 {
601 int middle = copy->count()/2;
602 return (copy->at(middle-1)+copy->at(middle))/2;
603 }
604 }
605 }
606 return 0;
607 }
608
609 /*!
610 Computes which data points correspond to the given position and returns the in a \a cluster.
611 */
612 void QColorMapSeriesPrivate::buildCluster(int xpos, int ypos, double dx, double dy, QVector<Point3D>& cluster)
613 {
614 foreach(ColorMapDataPart *dataPart, m_dataParts)
615 {
616 QPair<int,int> xRange = dataPart->getRange(dataPart->timesSeries(),m_requestedMinX+xpos*dx,m_requestedMinX+(xpos+1)*dx);
617 QPair<int,int> yRange = dataPart->getRange(dataPart->ySeries(),m_requestedMaxY-(ypos+1)*dy,m_requestedMaxY-ypos*dy);
618
619 int timeSeriesSize = dataPart->timesSeries().size();
620
621 if(xRange.first != xRange.second && yRange.first != yRange.second)
622 {
623 for(int i =xRange.first+1;i<xRange.second;i++)
624 {
625 qreal xval=dataPart->timesSeries()[i];
626 for(int j=yRange.first+1;j<yRange.second;j++)
627 {
628 qreal yval=dataPart->ySeries()[j];
629 qreal val=dataPart->dataSeries()[ i + (timeSeriesSize * j)];
630 cluster.append(Point3D(xval,yval,val));
631 }
632 }
633 }
634 }
635 }
636
637
638 void QColorMapSeriesPrivate::initializeAnimations(QtCharts::QChart::AnimationOptions options,
639 int duration, QEasingCurve &curve)
640 {
641 // ColorMapChart *item = static_cast<ColorMapChart *>(m_item.data());
642 // Q_ASSERT(item);
643 // if (item->animation())
644 // item->animation()->stopAndDestroyLater();
645
646 // if (options.testFlag(QChart::SeriesAnimations))
647 // item->setAnimation(new XYAnimation(item, duration, curve));
648 // else
649 // item->setAnimation(0);
650 QAbstractSeriesPrivate::initializeAnimations(options, duration, curve);
651 }
652
653
654
655 #include "moc_qcolormapseries.cpp"
656 #include "moc_qcolormapseries_p.cpp"
657
658 QT_CHARTS_END_NAMESPACE
@@ -0,0 +1,144
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #ifndef QCOLORMAPSERIES_H
24 #define QCOLORMAPSERIES_H
25
26 #include <QtCharts/QChartGlobal>
27 #include <QtCharts/QAbstractSeries>
28 #include <QtGui/QPen>
29 #include <QtGui/QBrush>
30 #include "colormapdatapart.h"
31
32 QT_CHARTS_BEGIN_NAMESPACE
33 class QColorMapSeriesPrivate;
34
35
36 class QT_CHARTS_EXPORT QColorMapSeries : public QAbstractSeries
37 {
38 Q_OBJECT
39 // Q_PROPERTY(bool pointsVisible READ pointsVisible WRITE setPointsVisible)
40 // Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
41 // Q_PROPERTY(QString pointLabelsFormat READ pointLabelsFormat WRITE setPointLabelsFormat NOTIFY pointLabelsFormatChanged)
42 // Q_PROPERTY(bool pointLabelsVisible READ pointLabelsVisible WRITE setPointLabelsVisible NOTIFY pointLabelsVisibilityChanged)
43 // Q_PROPERTY(QFont pointLabelsFont READ pointLabelsFont WRITE setPointLabelsFont NOTIFY pointLabelsFontChanged)
44 // Q_PROPERTY(QColor pointLabelsColor READ pointLabelsColor WRITE setPointLabelsColor NOTIFY pointLabelsColorChanged)
45 // Q_PROPERTY(bool pointLabelsClipping READ pointLabelsClipping WRITE setPointLabelsClipping NOTIFY pointLabelsClippingChanged)
46
47 protected:
48 explicit QColorMapSeries(QColorMapSeriesPrivate &d, QObject *parent =0);
49
50
51 public :
52
53 enum Strategy
54 {
55 LastPixel,
56 MeanPixel,
57 MedianPixel
58 };
59
60
61 explicit QColorMapSeries(QObject *parent =0);
62 ~QColorMapSeries();
63 QAbstractSeries::SeriesType type() const;
64 void append(ColorMapDataPart* dataPart, bool copy=true);
65 void append(const QList<ColorMapDataPart *> &dataParts, bool copy=true);
66
67 int count() const;
68 QVector<ColorMapDataPart*> dataParts() const;
69
70 QColorMapSeries &operator << (const ColorMapDataPart &dataPart);
71 QColorMapSeries &operator << (const QList<ColorMapDataPart *> &dataParts);
72
73 void getUniformGrid(double minX, double maxX, double minY, double maxY, int width, int height, QVector<double> &grid, QColorMapSeries::Strategy strategy);
74 // QVector<double> *getUniformGrid(int width, int height, QVector<double> &grid, Strategy lambda);
75
76 // virtual void attachAxis(QAbstractAxis *axis);
77
78 virtual void setPen(const QPen &pen);
79 QPen pen() const;
80
81 virtual void setBrush(const QBrush &brush);
82 QBrush brush() const;
83
84 // virtual void setColor(const QColor &color);
85 // QColor color() const;
86
87 // void setPointsVisible(bool visible = true);
88 // bool pointsVisible() const;
89
90 // void setPointLabelsFormat(const QString &format);
91 // QString pointLabelsFormat() const;
92
93 // void setPointLabelsVisible(bool visible = true);
94 // bool pointLabelsVisible() const;
95
96 // void setPointLabelsFont(const QFont &font);
97 // QFont pointLabelsFont() const;
98
99 // void setPointLabelsColor(const QColor &color);
100 // QColor pointLabelsColor() const;
101
102 // void setPointLabelsClipping(bool enabled = true);
103 // bool pointLabelsClipping() const;
104
105 double minX();
106 double minY();
107 double minZ();
108 double maxX();
109 double maxY();
110 double maxZ();
111
112
113
114 Q_SIGNALS:
115 // void clicked(const Point3D &point);
116 // void hovered(const Point3D &point, bool state);
117 // void pressed(const Point3D &point);
118 // void released(const Point3D &point);
119 // void doubleClicked(const Point3D &point);
120 // void pointReplaced(int index);
121 // void pointRemoved(int index);
122 void dataPartAdded(int index);
123 // void colorChanged(QColor color);
124 // void pointsReplaced();
125 // void pointLabelsFormatChanged(const QString &format);
126 // void pointLabelsVisibilityChanged(bool visible);
127 // void pointLabelsFontChanged(const QFont &font);
128 // void pointLabelsColorChanged(const QColor &color);
129 // void pointLabelsClippingChanged(bool clipping);
130 // void pointsRemoved(int index, int count);
131 void penChanged(const QPen &pen);
132
133 private:
134 Q_DECLARE_PRIVATE(QColorMapSeries)
135 Q_DISABLE_COPY(QColorMapSeries)
136 friend class ColorMapLegendMarker;
137 friend class ColorMapChart;
138 friend class QColorMapLegendMarkerPrivate;
139
140 };
141
142 QT_CHARTS_END_NAMESPACE
143
144 #endif // QCOLORMAPSERIES_H
@@ -0,0 +1,100
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #ifndef QCOLORMAPSERIES_P_H
24 #define QCOLORMAPSERIES_P_H
25
26 #include <private/qabstractseries_p.h>
27 #include "colormapdatapart.h"
28 #include <point3d.h>
29
30 QT_CHARTS_BEGIN_NAMESPACE
31
32 #include "qcolormapseries.h"
33
34 class QAbstractAxis;
35
36 class QColorMapSeriesPrivate : public QAbstractSeriesPrivate
37 {
38 Q_OBJECT
39
40 public :
41
42 QColorMapSeriesPrivate(QColorMapSeries *q);
43
44 void initializeDomain();
45 void initializeAxes();
46 void initializeGraphics(QGraphicsItem* parent);
47 void initializeTheme(int index, ChartTheme* theme, bool forced = false);
48 void initializeAnimations(QChart::AnimationOptions options, int duration,
49 QEasingCurve &curve);
50
51 QList<QLegendMarker *> createLegendMarkers(QLegend *legend);
52
53 QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const;
54 QAbstractAxis* createDefaultAxis(Qt::Orientation) const;
55
56 void getUniformGrid(double minX, double maxX, double minY, double maxY, int width, int height, QVector<double> &grid, QColorMapSeries::Strategy strategy);
57
58 Q_SIGNALS:
59 void updated();
60
61 protected:
62 QList<ColorMapDataPart*> m_dataParts;
63 QPen m_pen;
64 QBrush m_brush;
65 bool m_pointsVisible;
66 QString m_pointLabelsFormat;
67 bool m_pointLabelsVisible;
68 QFont m_pointLabelsFont;
69 QColor m_pointLabelsColor;
70 bool m_pointLabelsClipping;
71
72 private:
73 double m_minX;
74 double m_maxX;
75 double m_minY;
76 double m_maxY;
77 double m_minZ;
78 double m_maxZ;
79 int m_imageHeight;
80 int m_imageWidth;
81 QColorMapSeries::Strategy m_strategy;
82
83 double m_requestedMinX;
84 double m_requestedMaxX;
85 double m_requestedMinY;
86 double m_requestedMaxY;
87
88 void recomputeDataRange();
89 double computePixelValue(double &index);
90 double clusterStrategyLast(QVector<Point3D>& cluster);
91 double clusterStrategyMean(QVector<Point3D> &cluster);
92 double clusterStrategyMedian(QVector<Point3D>& cluster);
93 void buildCluster(int xpos,int ypos, double dx,double dy,QVector<Point3D>& cluster);
94 Q_DECLARE_PUBLIC(QColorMapSeries)
95 };
96
97 QT_CHARTS_END_NAMESPACE
98
99
100 #endif // QCOLORMAPSERIES_P_H
@@ -0,0 +1,105
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22
23 #include <QtCharts/QColorMapLegendMarker>
24 #include <private/qcolormapseries_p.h>
25 #include <private/qcolormaplegendmarker_p.h>
26 #include <QtCharts/QColorMapSeries>
27
28 QT_CHARTS_BEGIN_NAMESPACE
29
30 QColorMapLegendMarker::QColorMapLegendMarker(QColorMapSeries *series, QLegend *legend, QObject *parent)
31 : QLegendMarker(*new QColorMapLegendMarkerPrivate(this,series,legend), parent)
32 {
33 d_ptr->updated();
34 }
35
36 QColorMapLegendMarker::~QColorMapLegendMarker()
37 {
38 }
39
40 QColorMapLegendMarker::QColorMapLegendMarker(QColorMapLegendMarkerPrivate &d, QObject *parent) :
41 QLegendMarker(d, parent)
42 {
43 }
44
45 QColorMapSeries* QColorMapLegendMarker::series()
46 {
47 Q_D(QColorMapLegendMarker);
48 return d->m_series;
49 }
50
51 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
52
53 QColorMapLegendMarkerPrivate::QColorMapLegendMarkerPrivate(QColorMapLegendMarker *q, QColorMapSeries *series, QLegend *legend) :
54 QLegendMarkerPrivate(q,legend),
55 q_ptr(q),
56 m_series(series)
57 {
58 QObject::connect(m_series, SIGNAL(nameChanged()), this, SLOT(updated()));
59 QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(updated()));
60 }
61
62 QColorMapLegendMarkerPrivate::~QColorMapLegendMarkerPrivate()
63 {
64 }
65
66 QAbstractSeries* QColorMapLegendMarkerPrivate::series()
67 {
68 return m_series;
69 }
70
71 QObject* QColorMapLegendMarkerPrivate::relatedObject()
72 {
73 return m_series;
74 }
75
76 void QColorMapLegendMarkerPrivate::updated()
77 {
78 bool labelChanged = false;
79 bool brushChanged = false;
80
81 if (!m_customLabel && (m_item->label() != m_series->name())) {
82 m_item->setLabel(m_series->name());
83 labelChanged = true;
84 }
85
86 QBrush emptyBrush;
87 if (!m_customBrush
88 && (m_item->brush() == emptyBrush
89 || m_item->brush().color() != m_series->pen().color())) {
90 m_item->setBrush(QBrush(m_series->pen().color()));
91 brushChanged = true;
92 }
93
94 invalidateLegend();
95
96 if (labelChanged)
97 emit q_ptr->labelChanged();
98 if (brushChanged)
99 emit q_ptr->brushChanged();
100 }
101
102 //#include "moc_qcolormaplegendmarker.cpp"
103 //#include "moc_qcolormaplegendmarker_p.cpp"
104
105 QT_CHARTS_END_NAMESPACE
@@ -0,0 +1,55
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the ColorMapChart API
3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 --
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 2 of the License, or
8 -- (at your option) any later version.
9 --
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
14 --
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Hugo Winter
20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 ----------------------------------------------------------------------------*/
22 #ifndef QCOLORMAPLEGENDMARKER_H
23 #define QCOLORMAPLEGENDMARKER_H
24
25 #include <QtCharts/QChartGlobal>
26 #include <QtCharts/QLegendMarker>
27 #include <QtCharts/QColorMapSeries>
28
29 QT_CHARTS_BEGIN_NAMESPACE
30
31 class QColorMapLegendMarkerPrivate;
32
33 class QColorMapLegendMarker : public QLegendMarker
34 {
35 Q_OBJECT
36 public:
37 explicit QColorMapLegendMarker(QColorMapSeries *series, QLegend *legend, QObject *parent = 0);
38 virtual ~QColorMapLegendMarker();
39
40 virtual LegendMarkerType type() { return LegendMarkerTypeColorMap; }
41
42 // Related series
43 virtual QColorMapSeries* series();
44
45 protected:
46 QColorMapLegendMarker(QColorMapLegendMarkerPrivate &d, QObject *parent = 0);
47
48 private:
49 Q_DECLARE_PRIVATE(QColorMapLegendMarker)
50 Q_DISABLE_COPY(QColorMapLegendMarker)
51 };
52
53 QT_CHARTS_END_NAMESPACE
54
55 #endif // QCOLORMAPLEGENDMARKER_H
@@ -0,0 +1,35
1 #ifndef QCOLORMAPLEGENDMARKER_P_H
2 #define QCOLORMAPLEGENDMARKER_P_H
3
4 #include <QtCharts/QChartGlobal>
5 #include <private/qlegendmarker_p.h>
6 #include <private/legendmarkeritem_p.h>
7 #include <QtCharts/QColorMapSeries>
8
9 QT_CHARTS_BEGIN_NAMESPACE
10
11 class QColorMapLegendMarker;
12
13 class QColorMapLegendMarkerPrivate : public QLegendMarkerPrivate
14 {
15 Q_OBJECT
16 public:
17 explicit QColorMapLegendMarkerPrivate(QColorMapLegendMarker *q, QColorMapSeries *series, QLegend *legend);
18 virtual ~QColorMapLegendMarkerPrivate();
19
20 virtual QAbstractSeries* series();
21 virtual QObject* relatedObject();
22
23 public Q_SLOTS:
24 virtual void updated();
25
26 private:
27 QColorMapLegendMarker *q_ptr;
28 QColorMapSeries *m_series;
29
30 Q_DECLARE_PUBLIC(QColorMapLegendMarker)
31 };
32
33 QT_CHARTS_END_NAMESPACE
34
35 #endif // QCOLORMAPLEGENDMARKER_P_H
@@ -4,7 +4,8 INCLUDEPATH += $$PWD \
4 $$PWD/valueaxis \
4 $$PWD/valueaxis \
5 $$PWD/barcategoryaxis \
5 $$PWD/barcategoryaxis \
6 $$PWD/categoryaxis \
6 $$PWD/categoryaxis \
7 $$PWD/logvalueaxis
7 $$PWD/logvalueaxis \
8 $$PWD/colorbaraxis
8
9
9 SOURCES += \
10 SOURCES += \
10 $$PWD/chartaxiselement.cpp \
11 $$PWD/chartaxiselement.cpp \
@@ -23,7 +24,9 SOURCES += \
23 $$PWD/categoryaxis/qcategoryaxis.cpp \
24 $$PWD/categoryaxis/qcategoryaxis.cpp \
24 $$PWD/logvalueaxis/chartlogvalueaxisx.cpp \
25 $$PWD/logvalueaxis/chartlogvalueaxisx.cpp \
25 $$PWD/logvalueaxis/chartlogvalueaxisy.cpp \
26 $$PWD/logvalueaxis/chartlogvalueaxisy.cpp \
26 $$PWD/logvalueaxis/qlogvalueaxis.cpp
27 $$PWD/logvalueaxis/qlogvalueaxis.cpp \
28 $$PWD/colorbaraxis/qcolorbaraxis.cpp \
29 $$PWD/colorbaraxis/chartcolorbaraxisy.cpp
27
30
28 PRIVATE_HEADERS += \
31 PRIVATE_HEADERS += \
29 $$PWD/chartaxiselement_p.h \
32 $$PWD/chartaxiselement_p.h \
@@ -43,7 +46,9 PRIVATE_HEADERS += \
43 $$PWD/categoryaxis/qcategoryaxis_p.h \
46 $$PWD/categoryaxis/qcategoryaxis_p.h \
44 $$PWD/logvalueaxis/chartlogvalueaxisx_p.h \
47 $$PWD/logvalueaxis/chartlogvalueaxisx_p.h \
45 $$PWD/logvalueaxis/chartlogvalueaxisy_p.h \
48 $$PWD/logvalueaxis/chartlogvalueaxisy_p.h \
46 $$PWD/logvalueaxis/qlogvalueaxis_p.h
49 $$PWD/logvalueaxis/qlogvalueaxis_p.h \
50 $$PWD/colorbaraxis/qcolorbaraxis_p.h \
51 $$PWD/colorbaraxis/chartcolorbaraxisy_p.h
47
52
48 PUBLIC_HEADERS += \
53 PUBLIC_HEADERS += \
49 $$PWD/qabstractaxis.h \
54 $$PWD/qabstractaxis.h \
@@ -51,6 +56,7 PUBLIC_HEADERS += \
51 $$PWD/barcategoryaxis/qbarcategoryaxis.h \
56 $$PWD/barcategoryaxis/qbarcategoryaxis.h \
52 $$PWD/categoryaxis/qcategoryaxis.h \
57 $$PWD/categoryaxis/qcategoryaxis.h \
53 $$PWD/logvalueaxis/qlogvalueaxis.h \
58 $$PWD/logvalueaxis/qlogvalueaxis.h \
59 $$PWD/colorbaraxis/qcolorbaraxis.h
54
60
55 # polar
61 # polar
56 SOURCES += \
62 SOURCES += \
@@ -67,6 +67,7 include($$PWD/themes/themes.pri)
67 include($$PWD/xychart/xychart.pri)
67 include($$PWD/xychart/xychart.pri)
68 include($$PWD/layout/layout.pri)
68 include($$PWD/layout/layout.pri)
69 include($$PWD/boxplotchart/boxplotchart.pri)
69 include($$PWD/boxplotchart/boxplotchart.pri)
70 include($$PWD/colormapchart/colormapchart.pri)
70
71
71 HEADERS += $$PUBLIC_HEADERS
72 HEADERS += $$PUBLIC_HEADERS
72 HEADERS += $$PRIVATE_HEADERS
73 HEADERS += $$PRIVATE_HEADERS
@@ -10,7 +10,8 SOURCES += \
10 $$PWD/qxylegendmarker.cpp \
10 $$PWD/qxylegendmarker.cpp \
11 $$PWD/qarealegendmarker.cpp \
11 $$PWD/qarealegendmarker.cpp \
12 $$PWD/legendscroller.cpp \
12 $$PWD/legendscroller.cpp \
13 $$PWD/qboxplotlegendmarker.cpp
13 $$PWD/qboxplotlegendmarker.cpp \
14 $$PWD/qcolormaplegendmarker.cpp
14
15
15 PRIVATE_HEADERS += \
16 PRIVATE_HEADERS += \
16 $$PWD/legendscroller_p.h \
17 $$PWD/legendscroller_p.h \
@@ -22,7 +23,8 PRIVATE_HEADERS += \
22 $$PWD/qbarlegendmarker_p.h \
23 $$PWD/qbarlegendmarker_p.h \
23 $$PWD/qxylegendmarker_p.h \
24 $$PWD/qxylegendmarker_p.h \
24 $$PWD/qarealegendmarker_p.h \
25 $$PWD/qarealegendmarker_p.h \
25 $$PWD/qboxplotlegendmarker_p.h
26 $$PWD/qboxplotlegendmarker_p.h \
27 $$PWD/qcolormaplegendmarker_p.h
26
28
27
29
28 PUBLIC_HEADERS += \
30 PUBLIC_HEADERS += \
@@ -32,4 +34,5 PUBLIC_HEADERS += \
32 $$PWD/qbarlegendmarker.h \
34 $$PWD/qbarlegendmarker.h \
33 $$PWD/qxylegendmarker.h \
35 $$PWD/qxylegendmarker.h \
34 $$PWD/qarealegendmarker.h \
36 $$PWD/qarealegendmarker.h \
35 $$PWD/qboxplotlegendmarker.h
37 $$PWD/qboxplotlegendmarker.h \
38 $$PWD/qcolormaplegendmarker.h
@@ -52,7 +52,8 public:
52 LegendMarkerTypeBar,
52 LegendMarkerTypeBar,
53 LegendMarkerTypePie,
53 LegendMarkerTypePie,
54 LegendMarkerTypeXY,
54 LegendMarkerTypeXY,
55 LegendMarkerTypeBoxPlot
55 LegendMarkerTypeBoxPlot,
56 LegendMarkerTypeColorMap
56 };
57 };
57
58
58 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
59 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
@@ -63,7 +63,8 public:
63 SeriesTypeHorizontalBar,
63 SeriesTypeHorizontalBar,
64 SeriesTypeHorizontalStackedBar,
64 SeriesTypeHorizontalStackedBar,
65 SeriesTypeHorizontalPercentBar,
65 SeriesTypeHorizontalPercentBar,
66 SeriesTypeBoxPlot
66 SeriesTypeBoxPlot,
67 SeriesTypeColorMap
67 };
68 };
68
69
69 protected:
70 protected:
General Comments 0
You need to be logged in to leave comments. Login now