##// END OF EJS Templates
Some perfs improvments....
jeandet -
r2902:1f7564788f9d 5.7
parent child
Show More
@@ -1,299 +1,310
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the ColorMapChart API
3 3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 2 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------*/
19 19 /*-- Author : Hugo Winter
20 20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 21 ----------------------------------------------------------------------------*/
22 22
23 23 #include <private/colormapchart_p.h>
24 24 #include <QtCharts/QColorMapSeries>
25 25 #include <private/qcolormapseries_p.h>
26 26 #include <private/chartpresenter_p.h>
27 27 #include <private/abstractdomain_p.h>
28 28 #include <private/chartdataset_p.h>
29 29 #include <private/qabstractaxis_p.h>
30 30 #include <QtGui/QPainter>
31 31 #include <QColorBarAxis>
32 32
33 33 #include <QRgb>
34 34
35 35 #define nbOfColors 65000
36 36
37 37 QT_CHARTS_BEGIN_NAMESPACE
38 38
39 39 ColorMapChart::ColorMapChart(QColorMapSeries *series, QGraphicsItem *item)
40 40 : ChartItem(series->d_func(), item),
41 41 m_series(series),
42 42 m_dirty(true),
43 43 m_gradientType(Rainbow),
44 44 m_colorbar(Q_NULLPTR),
45 45 m_isColorBarDrawn(false),
46 46 m_currentClipRect(QRectF())
47 47 {
48 48 // QObject::connect(series, SIGNAL(pointReplaced(int)), this, SLOT(handlePointReplaced(int)));
49 49 // QObject::connect(series, SIGNAL(pointsReplaced()), this, SLOT(handlePointsReplaced()));
50 50 // QObject::connect(series, SIGNAL(pointAdded(int)), this, SLOT(handlePointAdded(int)));
51 51 // QObject::connect(series, SIGNAL(pointRemoved(int)), this, SLOT(handlePointRemoved(int)));
52 52 // QObject::connect(series, SIGNAL(pointsRemoved(int, int)), this, SLOT(handlePointsRemoved(int, int)));
53 53 // QObject::connect(this, SIGNAL(clicked(Point3D)), series, SIGNAL(clicked(Point3D)));
54 54 // QObject::connect(this, SIGNAL(hovered(Point3D,bool)), series, SIGNAL(hovered(Point3D,bool)));
55 55 // QObject::connect(this, SIGNAL(pressed(Point3D)), series, SIGNAL(pressed(Point3D)));
56 56 // QObject::connect(this, SIGNAL(released(Point3D)), series, SIGNAL(released(Point3D)));
57 57 // QObject::connect(this, SIGNAL(doubleClicked(Point3D)), series, SIGNAL(doubleClicked(Point3D)));
58 58
59 59 connect(this,SIGNAL(gradientTypeChanged()), this, SLOT(populateColorTable()));
60 60
61 61 m_colorTable = new QVector<QRgb>();
62 62 m_colorTable->reserve(nbOfColors);
63 63 populateColorTable();
64 64 }
65 65
66 66 ColorMapChart::~ColorMapChart()
67 67 {
68 68 delete m_colorTable;
69 69 delete m_colorbar;
70 70 }
71 71
72 72 void ColorMapChart::setDirty(bool dirty)
73 73 {
74 74 m_dirty = dirty;
75 75 }
76 76
77 77 QRectF ColorMapChart::boundingRect() const
78 78 {
79 79 return m_rect;
80 80 }
81 81
82 82 void ColorMapChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
83 83 {
84 84 Q_UNUSED(widget)
85 85 Q_UNUSED(option)
86 86
87 87 painter->save();
88 //m_series->setUseOpenGL();
88 m_series->setUseOpenGL();
89 89
90 90 QRectF plotAreaRect = m_series->chart()->plotArea();
91 91 QRectF clipRect = mapColorMapToPlotArea();
92 92 painter->setClipRect(clipRect);
93 93
94 94 if(m_currentClipRect !=clipRect)
95 95 {
96 96 m_currentClipRect = clipRect;
97 m_grid.reserve(clipRect.width()*clipRect.height());
98 m_grid.resize(clipRect.width()*clipRect.height());
97 m_grid.resize(clipRect.width()*clipRect.height()); //faster than reserve
99 98
100 99 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 100 addColorBar(plotAreaRect);
102 101 }
103 102
104 103 QImage colorMapImage(clipRect.width(),clipRect.height(),QImage::Format_RGB32);
105 104 //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 105 colorMapImage.fill(QColor(Qt::white).rgb());
107 106
108 107 fillColorMapImage(colorMapImage);
109 108
110 109 painter->drawImage(clipRect,colorMapImage);
111 110 painter->restore();
112 111 }
113 112
114 113 void ColorMapChart::addColorBar(QRectF plotAreaRect)
115 114 {
116 115 double maxZ = m_series->maxZ();
117 116 double minZ = m_series->minZ();
118 117
119 118 if(m_isColorBarDrawn)
120 119 m_series->chart()->removeAxis(m_colorbar);
121 120
122 121 m_colorbar = new QColorBarAxis(plotAreaRect,createColorMapGradient(m_gradientType),minZ, maxZ,this);
123 122 m_series->chart()->addAxis(m_colorbar, Qt::AlignRight);
124 123 m_isColorBarDrawn = true;
125 124 }
126 125
127 126 QRectF ColorMapChart::mapColorMapToPlotArea()
128 127 {
129 128 QRectF plotAreaRect = m_series->chart()->plotArea();
130 129
131 130 double seriesMinX = m_series->minX();
132 131 double seriesMaxX = m_series->maxX();
133 132 double seriesMinY = m_series->minY();
134 133 double seriesMaxY = m_series->maxY();
135 134
136 135 double domainMinX = domain()->minX();
137 136 double domainMaxX = domain()->maxX();
138 137 double domainMinY = domain()->minY();
139 138 double domainMaxY = domain()->maxY();
140 139
141 140 double widthToPaint = (qMin(seriesMaxX, domainMaxX)-qMax(seriesMinX, domainMinX))*plotAreaRect.width()/(domainMaxX-domainMinX);
142 141 double heightTopaint= (qMin(seriesMaxY, domainMaxY)-qMax(seriesMinY, domainMinY))*plotAreaRect.height()/(domainMaxY-domainMinY);
143 142
144 QSizeF size = QSize(widthToPaint,heightTopaint);
143
145 144
146 145 double pointX = (qMax(seriesMinX,domainMinX)-domainMinX)*plotAreaRect.width()/(domainMaxX-domainMinX);
147 146 double pointY = (domainMaxY-qMin(seriesMaxY,domainMaxY))*plotAreaRect.height()/(domainMaxY-domainMinY);
148 147
148 if(Q_UNLIKELY(widthToPaint < 0))
149 widthToPaint = 0;
150 if(Q_UNLIKELY(heightTopaint < 0))
151 heightTopaint = 0;
152
153 QSizeF size = QSize(widthToPaint,heightTopaint);
154
155 if(Q_UNLIKELY(pointX < 0))
156 pointX = 0;
157 if(Q_UNLIKELY(pointY < 0))
158 pointY = 0;
159
149 160 return QRectF(QPointF(pointX,pointY) ,size);
150 161 }
151 162
152 163 void ColorMapChart::fillColorMapImage(QImage &colorMapImage)
153 164 {
154 165 double maxZ = m_series->maxZ();
155 166 double minZ = m_series->minZ();
156 167 double rangeZ = maxZ - minZ;
157 168
158 169 int imageWidth = colorMapImage.width();
159 170 int imageHeight = colorMapImage.height();
160 171
161 172 for(int i=0;i<imageWidth;i++)
162 173 {
163 174 for(int j=0;j<imageHeight;j++)
164 175 {
165 176 double value = m_grid.at(i+j*(imageWidth));
166 177 double pix=((value-minZ)/rangeZ);
167 178 int indexInColorTable = pix*(nbOfColors-1);
168 179 colorMapImage.setPixel(i,j,m_colorTable->at(indexInColorTable));
169 180 }
170 181 }
171 182 }
172 183
173 184 /*!
174 185 Returns the predefined QLinearGradient corresponding to the \a gradientType passed as argument.
175 186 */
176 187 QLinearGradient ColorMapChart::createColorMapGradient(GradientType gradientType)
177 188 {
178 189 QLinearGradient gradient(0,0,1,100);
179 190 switch(gradientType)
180 191 {
181 192 case Rainbow :
182 193 gradient.setColorAt(1.0,Qt::darkRed);
183 194 gradient.setColorAt(0.8,Qt::red);
184 195 gradient.setColorAt(0.6,Qt::yellow);
185 196 gradient.setColorAt(0.4,Qt::green);
186 197 gradient.setColorAt(0.2,Qt::cyan);
187 198 gradient.setColorAt(0.0,Qt::blue);
188 199 break;
189 200 case CyclingRainbow :
190 201 gradient.setColorAt(1.0,Qt::red);
191 202 gradient.setColorAt(0.8,Qt::yellow);
192 203 gradient.setColorAt(0.6,Qt::green);
193 204 gradient.setColorAt(0.4,Qt::cyan);
194 205 gradient.setColorAt(0.2,Qt::blue);
195 206 gradient.setColorAt(0.0,Qt::magenta);
196 207 break;
197 208 case BlackAndWhite :
198 209 gradient.setColorAt(1.0, Qt::black);
199 210 gradient.setColorAt(0.0, Qt::white);
200 211 break;
201 212 case ReverseBlackAndWhite :
202 213 gradient.setColorAt(1.0, Qt::white);
203 214 gradient.setColorAt(0.0, Qt::black);
204 215 break;
205 216 default:
206 217 break;
207 218 }
208 219 return gradient;
209 220 }
210 221
211 222 /*!
212 223 Changes the type of gradient used to paint the ColorMap.
213 224 */
214 225 void ColorMapChart::changeGradient(GradientType gradientType)
215 226 {
216 227 if(m_gradientType == gradientType)
217 228 return;
218 229 else
219 230 m_gradientType = gradientType;
220 231 emit gradientTypeChanged();
221 232 }
222 233
223 234 /*!
224 235 Creates a color table corresponding to the gradient type currently selected.
225 236 */
226 237 void ColorMapChart::populateColorTable()
227 238 {
228 239 QLinearGradient gradient = createColorMapGradient(m_gradientType);
229 240 QGradientStops colorStops = gradient.stops();
230 241
231 242 for(int i=0;i<nbOfColors;i++)
232 243 {
233 244 double colorIndex = (double)i/nbOfColors;
234 245 for(int k =0;k<colorStops.size()-1;k++)
235 246 {
236 247 QGradientStop lowerBound = colorStops.at(k);
237 248 QGradientStop upperBound = colorStops.at(k+1);
238 249 if(colorIndex >= lowerBound.first && colorIndex < upperBound.first)
239 250 {
240 251 double ratio = (colorIndex-lowerBound.first)/(upperBound.first - lowerBound.first);
241 252 int red = (int)((1-ratio)*lowerBound.second.red() + ratio*upperBound.second.red());
242 253 int green = (int)((1-ratio)*lowerBound.second.green() + ratio*upperBound.second.green());
243 254 int blue = (int)((1-ratio)*lowerBound.second.blue() + ratio*upperBound.second.blue());
244 255 m_colorTable->append(qRgb(red, green, blue));
245 256 break;
246 257 }
247 258 else
248 259 {
249 260 if(k==colorStops.size()-2)
250 261 {
251 262 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 263 }
253 264 }
254 265 }
255 266 }
256 267 }
257 268
258 269
259 270 //handlers
260 271
261 272 void ColorMapChart::handlePointAdded(int index)
262 273 {
263 274
264 275 }
265 276
266 277 void ColorMapChart::handlePointRemoved(int index)
267 278 {
268 279
269 280 }
270 281
271 282 void ColorMapChart::handlePointsRemoved(int index, int count)
272 283 {
273 284
274 285 }
275 286
276 287 void ColorMapChart::handlePointReplaced(int index)
277 288 {
278 289
279 290 }
280 291
281 292 void ColorMapChart::handlePointsReplaced()
282 293 {
283 294
284 295 }
285 296
286 297 void ColorMapChart::handleDomainUpdated()
287 298 {
288 299
289 300 }
290 301
291 302 bool ColorMapChart::isEmpty()
292 303 {
293 304
294 305 }
295 306
296 307
297 308 #include "moc_colormapchart_p.cpp"
298 309
299 310 QT_CHARTS_END_NAMESPACE
@@ -1,154 +1,161
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the ColorMapChart API
3 3 -- Copyright (C) 2016, Plasma Physics Laboratory - CNRS
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 2 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------*/
19 19 /*-- Author : Hugo Winter
20 20 -- Mail : hugo.winter@lpp.polytechnique.fr
21 21 ----------------------------------------------------------------------------*/
22 22
23 23 #ifndef COLORMAPCHART_H
24 24 #define COLORMAPCHART_H
25 25
26 26 #include "point3d.h"
27 27 #include <QtCharts/QChartGlobal>
28 28 #include <private/chartitem_p.h>
29 29 #include <QtCharts/QValueAxis>
30 30 #include <QtGui/QPen>
31 31
32 32 //#include <QtCharts/QColorBarAxis> TODO : fix this
33 33 #include "qcolorbaraxis.h"
34 34
35 35 QT_CHARTS_BEGIN_NAMESPACE
36 36
37 37 class ChartPresenter;
38 38 class QColorMapSeries;
39 39
40 40 class ColorMapChart : public ChartItem
41 41 {
42 42 Q_OBJECT
43 43 public:
44 44
45 45 enum GradientType
46 46 {
47 47 Rainbow,
48 48 CyclingRainbow,
49 49 BlackAndWhite,
50 50 ReverseBlackAndWhite
51 51 };
52 52
53 53 explicit ColorMapChart(QColorMapSeries *series,QGraphicsItem *item = 0);
54 54 ~ColorMapChart();
55 55
56 56 bool isDirty() const { return m_dirty; }
57 57 void setDirty(bool dirty);
58 58
59 59 // from QGraphicsItem
60 60 QRectF boundingRect() const;
61 61 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
62 62
63 63 public Q_SLOTS:
64 64 void handlePointAdded(int index);
65 65 void handlePointRemoved(int index);
66 66 void handlePointsRemoved(int index, int count);
67 67 void handlePointReplaced(int index);
68 68 void handlePointsReplaced();
69 69 void handleDomainUpdated();
70 70
71 71 private slots :
72 72 void populateColorTable();
73 73
74 74 Q_SIGNALS:
75 75 void clicked(const Point3D &point);
76 76 void hovered(const Point3D &point, bool state);
77 77 void pressed(const Point3D &point);
78 78 void released(const Point3D &point);
79 79 void doubleClicked(const Point3D &point);
80 80 void gradientTypeChanged();
81 81
82 protected:
83 // void updateGeometry();
84 // void mousePressEvent(QGraphicsSceneMouseEvent *event);
85 // void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
86 // void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
87 // void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
88 // void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
89
82 90 private:
83 91 inline bool isEmpty();
84 92 void addColorBar(QRectF plotAreaRect);
85 93
86 94 QRectF mapColorMapToPlotArea();
87 95 void fillColorMapImage(QImage &colorMapImage);
88 96 QLinearGradient createColorMapGradient(GradientType gradientType);
89 97 void changeGradient(GradientType gradientType);
90 98
91 protected:
92 99 QColorMapSeries *m_series;
93 100 QVector<Point3D> m_points;
94 101 QRectF m_rect;
95 102 bool m_dirty;
96 103 QVector<QRgb> *m_colorTable;
97 104 GradientType m_gradientType;
98 105 bool m_isColorBarDrawn;
99 106 QColorBarAxis *m_colorbar;
100 107 QRectF m_currentClipRect;
101 108 QVector<double> m_grid;
102 109 };
103 110
104 111 //class PixmapMarker: public QGraphicsRectItem
105 112 //{
106 113
107 114 //public:
108 115 // PixmapMarker(qreal x, qreal y, qreal w, qreal h, ColorMapChart *parent)
109 116 // : QGraphicsRectItem(x, y, w, h, parent),
110 117 // m_parent(parent)
111 118 // {
112 119 // setAcceptHoverEvents(true);
113 120 // setFlag(QGraphicsItem::ItemIsSelectable);
114 121 // }
115 122
116 123 //protected:
117 124 // void mousePressEvent(QGraphicsSceneMouseEvent *event)
118 125 // {
119 126 // QGraphicsRectItem::mousePressEvent(event);
120 127 // m_parent->markerPressed(this);
121 128 // m_parent->setMousePressed();
122 129 // }
123 130 // void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
124 131 // {
125 132 // QGraphicsRectItem::hoverEnterEvent(event);
126 133 // m_parent->markerHovered(this, true);
127 134 // }
128 135 // void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
129 136 // {
130 137 // QGraphicsRectItem::hoverLeaveEvent(event);
131 138 // m_parent->markerHovered(this, false);
132 139 // }
133 140 // void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
134 141 // {
135 142 // QGraphicsRectItem::mouseReleaseEvent(event);
136 143 // m_parent->markerReleased(this);
137 144 // if (m_parent->mousePressed())
138 145 // m_parent->markerSelected(this);
139 146 // m_parent->setMousePressed(false);
140 147 // }
141 148 // void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
142 149 // {
143 150 // QGraphicsRectItem::mouseDoubleClickEvent(event);
144 151 // m_parent->markerDoubleClicked(this);
145 152 // }
146 153
147 154 //private:
148 155 // ColorMapChart *m_parent;
149 156 //};
150 157
151 158
152 159 QT_CHARTS_END_NAMESPACE
153 160
154 161 #endif // COLORMAPCHART_H
General Comments 0
You need to be logged in to leave comments. Login now