##// END OF EJS Templates
functionnal colorbar
winter -
r4:6986421aea60 default draft
parent child
Show More
@@ -23,6 +23,7 ChartColorBarAxisY::ChartColorBarAxisY(Q
23 23 QObject::connect(m_axis, SIGNAL(labelFormatChanged(QString)), this, SLOT(handleLabelFormatChanged(QString)));
24 24
25 25 createColorBar();
26
26 27 }
27 28
28 29 ChartColorBarAxisY::~ChartColorBarAxisY()
@@ -124,25 +125,32 QSizeF ChartColorBarAxisY::sizeHint(Qt::
124 125
125 126 void ChartColorBarAxisY::createColorBar()
126 127 {
127 //m_position.setX(80+m_position.x());// TODO : remove this when you know how to move labels
128 m_axis->setLabelsColor(QColor(0,0,255,0)); //make automatic labels disappear without changing layout (as it would be the case with setlabelVisible(false))
128 129
129 130 //gradient
130 131 QGradientStops stops = m_gradient.stops();
131 QLinearGradient gradient(0,0,1,m_height);
132 QLinearGradient gradient(0,m_position.y(),1,m_height);
132 133 foreach(QGradientStop stop, stops)
133 134 {
134 135 gradient.setColorAt(1-stop.first,stop.second);
135 136 }
136 137
137 138 //colorbar
138 QRectF rect(m_position.x(),m_position.y(),m_height/20,m_height);
139 QRectF rect(m_position.x()+10,m_position.y(),m_height/20,m_height); //remove 10 if needed
139 140
140 141 QGraphicsRectItem *colorbar = new QGraphicsRectItem(rect, this);
141 142 colorbar->setZValue(ChartPresenter::AxisZValue);
142 143 colorbar->setBrush(gradient);
143 setGraphicsItem(colorbar);
144
145 for(int i=0;i<m_axis->tickCount();i++)
146 {
147 qreal value = m_axis->max() - (i * (m_axis->max() - m_axis->min()) / (m_axis->tickCount() - 1));
148 QGraphicsTextItem *label = new QGraphicsTextItem(this);
149 label->setPlainText(QString::number(value));
150 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
151 }
144 152 }
145 153
146 154 #include "moc_chartcolorbaraxisy_p.cpp"
147 155
148 QT_CHARTS_END_NAMESPACE
156 QT_CHARTS_END_NAMESPACE
@@ -18,7 +18,10 ColorMapChart::ColorMapChart(QColorMapSe
18 18 m_series(series),
19 19 m_dirty(true),
20 20 m_gradientType(Rainbow),
21 m_colorbar(false)
21 m_colorbar(Q_NULLPTR),
22 m_isColorBarDrawn(false),
23 m_currentplotArea(QRectF()),
24 m_grid(Q_NULLPTR)
22 25 {
23 26 // QObject::connect(series, SIGNAL(pointReplaced(int)), this, SLOT(handlePointReplaced(int)));
24 27 // QObject::connect(series, SIGNAL(pointsReplaced()), this, SLOT(handlePointsReplaced()));
@@ -38,6 +41,12 ColorMapChart::ColorMapChart(QColorMapSe
38 41 populateColorTable();
39 42 }
40 43
44 ColorMapChart::~ColorMapChart()
45 {
46 delete m_colorTable;
47 delete m_colorbar;
48 }
49
41 50 void ColorMapChart::setDirty(bool dirty)
42 51 {
43 52 m_dirty = dirty;
@@ -61,18 +70,19 void ColorMapChart::paint(QPainter *pain
61 70 painter->setClipRect(clipRect);
62 71
63 72 QRectF plotAreaRect = m_series->chart()->plotArea();
73 if(m_currentplotArea !=plotAreaRect)
74 {
75 m_currentplotArea = plotAreaRect;
76
77 m_grid = m_series->getUniformGrid(0,0,plotAreaRect.width(),plotAreaRect.height(),QColorMapSeries::LastPixel);
78
79 addColorBar(plotAreaRect);
80 }
81
64 82 QImage colorMapImage(plotAreaRect.width(),plotAreaRect.height(),QImage::Format_RGB32);
65 83 //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.
66 84 colorMapImage.fill(QColor(Qt::white).rgb());
67 85
68 QElapsedTimer timer;
69 timer.start();
70
71 QVector<double> * grid = m_series->getUniformGrid(0,0,plotAreaRect.width(),plotAreaRect.height(),QColorMapSeries::LastPixel);
72
73 int msec = timer.elapsed();
74 int msec2 = 0+msec;
75
76 86 double maxZ = m_series->maxZ();
77 87 double minZ = m_series->minZ();
78 88 double rangeZ = maxZ - minZ;
@@ -84,22 +94,18 void ColorMapChart::paint(QPainter *pain
84 94 {
85 95 for(int j=0;j<imageHeight;j++)
86 96 {
87 double value = grid->at(i+j*(imageWidth));
97 double value = m_grid->at(i+j*(imageWidth));
88 98 double pix=((value-minZ)/rangeZ);
89 99 int indexInColorTable = pix*(nbOfColors-1);
90 100 colorMapImage.setPixel(i,j,m_colorTable->at(indexInColorTable));
91 101 }
92 102 }
93 103
94 addColorBar(plotAreaRect);
95
96
97 104 painter->drawImage(clipRect,colorMapImage);
98 update();//Qt docs: Warning: If you call repaint() in a function which may itself be called from paintEvent(), you may get infinite recursion. The update() function never causes recursion
105 //This line was causing re-painting loop
106 //update();//Qt docs: Warning: If you call repaint() in a function which may itself be called from paintEvent(), you may get infinite recursion. The update() function never causes recursion
99 107
100 108 painter->restore();
101
102
103 109 }
104 110
105 111 void ColorMapChart::addColorBar(QRectF plotAreaRect)
@@ -29,7 +29,7 public:
29 29 };
30 30
31 31 explicit ColorMapChart(QColorMapSeries *series,QGraphicsItem *item = 0);
32 ~ColorMapChart() {}
32 ~ColorMapChart();
33 33
34 34 bool isDirty() const { return m_dirty; }
35 35 void setDirty(bool dirty);
@@ -72,6 +72,8 protected:
72 72 GradientType m_gradientType;
73 73 bool m_isColorBarDrawn;
74 74 QColorBarAxis *m_colorbar;
75 QRectF m_currentplotArea;
76 QVector<double> * m_grid;
75 77 };
76 78
77 79 //class PixmapMarker: public QGraphicsRectItem
General Comments 0
You need to be logged in to leave comments. Login now