@@ -1,316 +1,316 | |||||
1 | #include "charttheme_p.h" |
|
1 | #include "charttheme_p.h" | |
2 | #include "qchart.h" |
|
2 | #include "qchart.h" | |
3 | #include "qlegend.h" |
|
3 | #include "qlegend.h" | |
4 | #include "qchartaxis.h" |
|
4 | #include "qchartaxis.h" | |
5 | #include <QTime> |
|
5 | #include <QTime> | |
6 |
|
6 | |||
7 | //series |
|
7 | //series | |
8 | #include "qbarset.h" |
|
8 | #include "qbarset.h" | |
9 | #include "qbarseries.h" |
|
9 | #include "qbarseries.h" | |
10 | #include "qstackedbarseries.h" |
|
10 | #include "qstackedbarseries.h" | |
11 | #include "qpercentbarseries.h" |
|
11 | #include "qpercentbarseries.h" | |
12 | #include "qlineseries.h" |
|
12 | #include "qlineseries.h" | |
13 | #include "qareaseries.h" |
|
13 | #include "qareaseries.h" | |
14 | #include "qscatterseries.h" |
|
14 | #include "qscatterseries.h" | |
15 | #include "qpieseries.h" |
|
15 | #include "qpieseries.h" | |
16 | #include "qpieslice.h" |
|
16 | #include "qpieslice.h" | |
17 | #include "qsplineseries.h" |
|
17 | #include "qsplineseries.h" | |
18 |
|
18 | |||
19 | //items |
|
19 | //items | |
20 | #include "axisitem_p.h" |
|
20 | #include "axisitem_p.h" | |
21 | #include "barpresenter_p.h" |
|
21 | #include "barpresenter_p.h" | |
22 | #include "stackedbarpresenter_p.h" |
|
22 | #include "stackedbarpresenter_p.h" | |
23 | #include "percentbarpresenter_p.h" |
|
23 | #include "percentbarpresenter_p.h" | |
24 | #include "linechartitem_p.h" |
|
24 | #include "linechartitem_p.h" | |
25 | #include "areachartitem_p.h" |
|
25 | #include "areachartitem_p.h" | |
26 | #include "scatterchartitem_p.h" |
|
26 | #include "scatterchartitem_p.h" | |
27 | #include "piepresenter_p.h" |
|
27 | #include "piepresenter_p.h" | |
28 | #include "splinechartitem_p.h" |
|
28 | #include "splinechartitem_p.h" | |
29 |
|
29 | |||
30 | //themes |
|
30 | //themes | |
31 | #include "chartthemedefault_p.h" |
|
31 | #include "chartthemedefault_p.h" | |
32 | #include "chartthemevanilla_p.h" |
|
32 | #include "chartthemevanilla_p.h" | |
33 | #include "chartthemeicy_p.h" |
|
33 | #include "chartthemeicy_p.h" | |
34 | #include "chartthemegrayscale_p.h" |
|
34 | #include "chartthemegrayscale_p.h" | |
35 | #include "chartthemescientific_p.h" |
|
35 | #include "chartthemescientific_p.h" | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
38 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
39 |
|
39 | |||
40 | ChartTheme::ChartTheme(QChart::ChartTheme id) |
|
40 | ChartTheme::ChartTheme(QChart::ChartTheme id) | |
41 | { |
|
41 | { | |
42 | m_id = id; |
|
42 | m_id = id; | |
43 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
43 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |
44 | } |
|
44 | } | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) |
|
47 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) | |
48 | { |
|
48 | { | |
49 | switch(theme) { |
|
49 | switch(theme) { | |
50 | case QChart::ChartThemeVanilla: |
|
50 | case QChart::ChartThemeVanilla: | |
51 | return new ChartThemeVanilla(); |
|
51 | return new ChartThemeVanilla(); | |
52 | case QChart::ChartThemeIcy: |
|
52 | case QChart::ChartThemeIcy: | |
53 | return new ChartThemeIcy(); |
|
53 | return new ChartThemeIcy(); | |
54 | case QChart::ChartThemeGrayscale: |
|
54 | case QChart::ChartThemeGrayscale: | |
55 | return new ChartThemeGrayscale(); |
|
55 | return new ChartThemeGrayscale(); | |
56 | case QChart::ChartThemeScientific: |
|
56 | case QChart::ChartThemeScientific: | |
57 | return new ChartThemeScientific(); |
|
57 | return new ChartThemeScientific(); | |
58 | default: |
|
58 | default: | |
59 | return new ChartThemeDefault(); |
|
59 | return new ChartThemeDefault(); | |
60 | } |
|
60 | } | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | void ChartTheme::decorate(QChart* chart) |
|
63 | void ChartTheme::decorate(QChart* chart) | |
64 | { |
|
64 | { | |
65 | chart->setChartBackgroundBrush(m_backgroundGradient); |
|
65 | chart->setChartBackgroundBrush(m_backgroundGradient); | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | void ChartTheme::decorate(QLegend* legend) |
|
68 | void ChartTheme::decorate(QLegend* legend) | |
69 | { |
|
69 | { | |
70 | legend->setBackgroundBrush(m_backgroundGradient); |
|
70 | legend->setBackgroundBrush(m_backgroundGradient); | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series, int index) |
|
73 | void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series, int index) | |
74 | { |
|
74 | { | |
75 | QPen pen; |
|
75 | QPen pen; | |
76 | QBrush brush; |
|
76 | QBrush brush; | |
77 |
|
77 | |||
78 | if (pen != series->pen()){ |
|
78 | if (pen != series->pen()){ | |
79 | item->setPen(series->pen()); |
|
79 | item->setPen(series->pen()); | |
80 | } else { |
|
80 | } else { | |
81 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0)); |
|
81 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0)); | |
82 | pen.setWidthF(2); |
|
82 | pen.setWidthF(2); | |
83 | item->setPen(pen); |
|
83 | item->setPen(pen); | |
84 | } |
|
84 | } | |
85 |
|
85 | |||
86 | if (brush != series->brush()) { |
|
86 | if (brush != series->brush()) { | |
87 | item->setBrush(series->brush()); |
|
87 | item->setBrush(series->brush()); | |
88 | } else { |
|
88 | } else { | |
89 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); |
|
89 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); | |
90 | item->setBrush(brush); |
|
90 | item->setBrush(brush); | |
91 | } |
|
91 | } | |
92 | } |
|
92 | } | |
93 |
|
93 | |||
94 |
|
94 | |||
95 | void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int index) |
|
95 | void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int index) | |
96 | { |
|
96 | { | |
97 | QPen pen; |
|
97 | QPen pen; | |
98 | if(pen != series->pen()){ |
|
98 | if(pen != series->pen()){ | |
99 | item->setLinePen(series->pen()); |
|
99 | item->setLinePen(series->pen()); | |
100 | return; |
|
100 | return; | |
101 | } |
|
101 | } | |
102 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); |
|
102 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); | |
103 | pen.setWidthF(2); |
|
103 | pen.setWidthF(2); | |
104 | item->setLinePen(pen); |
|
104 | item->setLinePen(pen); | |
105 | } |
|
105 | } | |
106 |
|
106 | |||
107 | void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index) |
|
107 | void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index) | |
108 | { |
|
108 | { | |
109 | QList<QBarSet*> sets = series->barSets(); |
|
109 | QList<QBarSet*> sets = series->barSets(); | |
110 | for (int i=0; i<sets.count(); i++) { |
|
110 | for (int i=0; i<sets.count(); i++) { | |
111 | qreal pos = 0.5; |
|
111 | qreal pos = 0.5; | |
112 | if (sets.count() > 1) |
|
112 | if (sets.count() > 1) | |
113 | pos = (qreal) i / (qreal) (sets.count() - 1); |
|
113 | pos = (qreal) i / (qreal) (sets.count() - 1); | |
114 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); |
|
114 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |
115 | sets.at(i)->setBrush(QBrush(c)); |
|
115 | sets.at(i)->setBrush(QBrush(c)); | |
116 |
|
116 | |||
117 | // Pick label color as far as possible from bar color (within gradient). |
|
117 | // Pick label color as far as possible from bar color (within gradient). | |
118 | // 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :) |
|
118 | // 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :) | |
119 | // TODO: better picking of label color? |
|
119 | // TODO: better picking of label color? | |
120 | if (pos < 0.3) { |
|
120 | if (pos < 0.3) { | |
121 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); |
|
121 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); | |
122 | } else { |
|
122 | } else { | |
123 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); |
|
123 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); | |
124 | } |
|
124 | } | |
125 | sets.at(i)->setFloatingValuePen(QPen(c)); |
|
125 | sets.at(i)->setFloatingValuePen(QPen(c)); | |
126 | } |
|
126 | } | |
127 | } |
|
127 | } | |
128 |
|
128 | |||
129 | void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int index) |
|
129 | void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int index) | |
130 | { |
|
130 | { | |
131 | QList<QBarSet*> sets = series->barSets(); |
|
131 | QList<QBarSet*> sets = series->barSets(); | |
132 | for (int i=0; i<sets.count(); i++) { |
|
132 | for (int i=0; i<sets.count(); i++) { | |
133 | qreal pos = 0.5; |
|
133 | qreal pos = 0.5; | |
134 | if (sets.count() > 1) |
|
134 | if (sets.count() > 1) | |
135 | pos = (qreal) i / (qreal) (sets.count() - 1); |
|
135 | pos = (qreal) i / (qreal) (sets.count() - 1); | |
136 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); |
|
136 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |
137 | sets.at(i)->setBrush(QBrush(c)); |
|
137 | sets.at(i)->setBrush(QBrush(c)); | |
138 |
|
138 | |||
139 | if (pos < 0.3) { |
|
139 | if (pos < 0.3) { | |
140 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); |
|
140 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); | |
141 | } else { |
|
141 | } else { | |
142 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); |
|
142 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); | |
143 | } |
|
143 | } | |
144 | sets.at(i)->setFloatingValuePen(QPen(c)); |
|
144 | sets.at(i)->setFloatingValuePen(QPen(c)); | |
145 | } |
|
145 | } | |
146 | } |
|
146 | } | |
147 |
|
147 | |||
148 | void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int index) |
|
148 | void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int index) | |
149 | { |
|
149 | { | |
150 | QList<QBarSet*> sets = series->barSets(); |
|
150 | QList<QBarSet*> sets = series->barSets(); | |
151 | for (int i=0; i<sets.count(); i++) { |
|
151 | for (int i=0; i<sets.count(); i++) { | |
152 | qreal pos = 0.5; |
|
152 | qreal pos = 0.5; | |
153 | if (sets.count() > 1) |
|
153 | if (sets.count() > 1) | |
154 | pos = (qreal) i / (qreal) (sets.count() - 1); |
|
154 | pos = (qreal) i / (qreal) (sets.count() - 1); | |
155 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); |
|
155 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |
156 | sets.at(i)->setBrush(QBrush(c)); |
|
156 | sets.at(i)->setBrush(QBrush(c)); | |
157 |
|
157 | |||
158 | if (pos < 0.3) { |
|
158 | if (pos < 0.3) { | |
159 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); |
|
159 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); | |
160 | } else { |
|
160 | } else { | |
161 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); |
|
161 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); | |
162 | } |
|
162 | } | |
163 | sets.at(i)->setFloatingValuePen(QPen(c)); |
|
163 | sets.at(i)->setFloatingValuePen(QPen(c)); | |
164 | } |
|
164 | } | |
165 | } |
|
165 | } | |
166 |
|
166 | |||
167 | void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int index) |
|
167 | void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int index) | |
168 | { |
|
168 | { | |
169 | Q_ASSERT(item); |
|
169 | Q_ASSERT(item); | |
170 | Q_ASSERT(series); |
|
170 | Q_ASSERT(series); | |
171 |
|
171 | |||
172 | QPen pen; |
|
172 | QPen pen; | |
173 | QBrush brush; |
|
173 | QBrush brush; | |
174 |
|
174 | |||
175 | if (pen == series->pen()) { |
|
175 | if (pen == series->pen()) { | |
176 |
pen.setColor(colorAt(m_seriesGradients.at( |
|
176 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0)); | |
177 | pen.setWidthF(2); |
|
177 | pen.setWidthF(2); | |
178 | series->setPen(pen); |
|
178 | series->setPen(pen); | |
179 | } |
|
179 | } | |
180 |
|
180 | |||
181 | if (brush == series->brush()) { |
|
181 | if (brush == series->brush()) { | |
182 |
QBrush brush(m_seriesColors.at( |
|
182 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); | |
183 | series->setBrush(brush); |
|
183 | series->setBrush(brush); | |
184 | } |
|
184 | } | |
185 | } |
|
185 | } | |
186 |
|
186 | |||
187 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int index) |
|
187 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int index) | |
188 | { |
|
188 | { | |
189 | // Get color for a slice from a gradient linearly, beginning from the start of the gradient |
|
189 | // Get color for a slice from a gradient linearly, beginning from the start of the gradient | |
190 | for (int i(0); i < series->slices().count(); i++) { |
|
190 | for (int i(0); i < series->slices().count(); i++) { | |
191 | qreal pos = (qreal) i / (qreal) series->count(); |
|
191 | qreal pos = (qreal) i / (qreal) series->count(); | |
192 | QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1); |
|
192 | QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1); | |
193 | series->slices().at(i)->setSlicePen(penColor); |
|
193 | series->slices().at(i)->setSlicePen(penColor); | |
194 | QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); |
|
194 | QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |
195 | series->slices().at(i)->setSliceBrush(brushColor); |
|
195 | series->slices().at(i)->setSliceBrush(brushColor); | |
196 | } |
|
196 | } | |
197 | } |
|
197 | } | |
198 |
|
198 | |||
199 |
|
199 | |||
200 | void ChartTheme::decorate(QChartAxis* axis, AxisItem* item) |
|
200 | void ChartTheme::decorate(QChartAxis* axis, AxisItem* item) | |
201 | { |
|
201 | { | |
202 | //TODO: dummy defults for now |
|
202 | //TODO: dummy defults for now | |
203 | axis->setLabelsBrush(Qt::black); |
|
203 | axis->setLabelsBrush(Qt::black); | |
204 | axis->setLabelsPen(Qt::NoPen); |
|
204 | axis->setLabelsPen(Qt::NoPen); | |
205 | axis->setShadesPen(Qt::NoPen); |
|
205 | axis->setShadesPen(Qt::NoPen); | |
206 | axis->setShadesOpacity(0.5); |
|
206 | axis->setShadesOpacity(0.5); | |
207 | } |
|
207 | } | |
208 |
|
208 | |||
209 | void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int index) |
|
209 | void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int index) | |
210 | { |
|
210 | { | |
211 | Q_ASSERT(item); |
|
211 | Q_ASSERT(item); | |
212 | Q_ASSERT(series); |
|
212 | Q_ASSERT(series); | |
213 |
|
213 | |||
214 | QPen pen; |
|
214 | QPen pen; | |
215 |
|
215 | |||
216 | if(pen != series->pen()){ |
|
216 | if(pen != series->pen()){ | |
217 | item->setLinePen(series->pen()); |
|
217 | item->setLinePen(series->pen()); | |
218 | }else{ |
|
218 | }else{ | |
219 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); |
|
219 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); | |
220 | pen.setWidthF(series->pen().widthF()); |
|
220 | pen.setWidthF(series->pen().widthF()); | |
221 | item->setLinePen(series->pen()); |
|
221 | item->setLinePen(series->pen()); | |
222 | } |
|
222 | } | |
223 |
|
223 | |||
224 | // QColor color = m_seriesColors.at(index % m_seriesColors.size()); |
|
224 | // QColor color = m_seriesColors.at(index % m_seriesColors.size()); | |
225 | // TODO: define alpha in the theme? or in the series? |
|
225 | // TODO: define alpha in the theme? or in the series? | |
226 | //color.setAlpha(120); |
|
226 | //color.setAlpha(120); | |
227 |
|
227 | |||
228 | // QBrush brush(color, Qt::SolidPattern); |
|
228 | // QBrush brush(color, Qt::SolidPattern); | |
229 | // presenter->m_markerBrush = brush; |
|
229 | // presenter->m_markerBrush = brush; | |
230 |
|
230 | |||
231 | // QPen pen(brush, 3); |
|
231 | // QPen pen(brush, 3); | |
232 | // pen.setColor(color); |
|
232 | // pen.setColor(color); | |
233 | // presenter->m_markerPen = pen; |
|
233 | // presenter->m_markerPen = pen; | |
234 | } |
|
234 | } | |
235 |
|
235 | |||
236 | void ChartTheme::generateSeriesGradients() |
|
236 | void ChartTheme::generateSeriesGradients() | |
237 | { |
|
237 | { | |
238 | // Generate gradients in HSV color space |
|
238 | // Generate gradients in HSV color space | |
239 | foreach (QColor color, m_seriesColors) { |
|
239 | foreach (QColor color, m_seriesColors) { | |
240 | QLinearGradient g; |
|
240 | QLinearGradient g; | |
241 | qreal h = color.hsvHueF(); |
|
241 | qreal h = color.hsvHueF(); | |
242 | qreal s = color.hsvSaturationF(); |
|
242 | qreal s = color.hsvSaturationF(); | |
243 |
|
243 | |||
244 | // TODO: tune the algorithm to give nice results with most base colors defined in |
|
244 | // TODO: tune the algorithm to give nice results with most base colors defined in | |
245 | // most themes. The rest of the gradients we can define manually in theme specific |
|
245 | // most themes. The rest of the gradients we can define manually in theme specific | |
246 | // implementation. |
|
246 | // implementation. | |
247 | QColor start = color; |
|
247 | QColor start = color; | |
248 | start.setHsvF(h, 0.05, 0.95); |
|
248 | start.setHsvF(h, 0.05, 0.95); | |
249 | g.setColorAt(0.0, start); |
|
249 | g.setColorAt(0.0, start); | |
250 |
|
250 | |||
251 | g.setColorAt(0.5, color); |
|
251 | g.setColorAt(0.5, color); | |
252 |
|
252 | |||
253 | QColor end = color; |
|
253 | QColor end = color; | |
254 | end.setHsvF(h, s, 0.25); |
|
254 | end.setHsvF(h, s, 0.25); | |
255 | g.setColorAt(1.0, end); |
|
255 | g.setColorAt(1.0, end); | |
256 |
|
256 | |||
257 | m_seriesGradients << g; |
|
257 | m_seriesGradients << g; | |
258 | } |
|
258 | } | |
259 | } |
|
259 | } | |
260 |
|
260 | |||
261 |
|
261 | |||
262 | QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos) |
|
262 | QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos) | |
263 | { |
|
263 | { | |
264 | Q_ASSERT(pos >=0.0 && pos <= 1.0); |
|
264 | Q_ASSERT(pos >=0.0 && pos <= 1.0); | |
265 | qreal r = start.redF() + ((end.redF() - start.redF()) * pos); |
|
265 | qreal r = start.redF() + ((end.redF() - start.redF()) * pos); | |
266 | qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos); |
|
266 | qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos); | |
267 | qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos); |
|
267 | qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos); | |
268 | QColor c; |
|
268 | QColor c; | |
269 | c.setRgbF(r, g, b); |
|
269 | c.setRgbF(r, g, b); | |
270 | return c; |
|
270 | return c; | |
271 | } |
|
271 | } | |
272 |
|
272 | |||
273 | QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos) |
|
273 | QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos) | |
274 | { |
|
274 | { | |
275 | Q_ASSERT(pos >=0 && pos <= 1.0); |
|
275 | Q_ASSERT(pos >=0 && pos <= 1.0); | |
276 |
|
276 | |||
277 | // another possibility: |
|
277 | // another possibility: | |
278 | // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient |
|
278 | // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient | |
279 |
|
279 | |||
280 | QGradientStops stops = gradient.stops(); |
|
280 | QGradientStops stops = gradient.stops(); | |
281 | int count = stops.count(); |
|
281 | int count = stops.count(); | |
282 |
|
282 | |||
283 | // find previous stop relative to position |
|
283 | // find previous stop relative to position | |
284 | QGradientStop prev = stops.first(); |
|
284 | QGradientStop prev = stops.first(); | |
285 | for (int i=0; i<count; i++) { |
|
285 | for (int i=0; i<count; i++) { | |
286 | QGradientStop stop = stops.at(i); |
|
286 | QGradientStop stop = stops.at(i); | |
287 | if (pos > stop.first) |
|
287 | if (pos > stop.first) | |
288 | prev = stop; |
|
288 | prev = stop; | |
289 |
|
289 | |||
290 | // given position is actually a stop position? |
|
290 | // given position is actually a stop position? | |
291 | if (pos == stop.first) { |
|
291 | if (pos == stop.first) { | |
292 | //qDebug() << "stop color" << pos; |
|
292 | //qDebug() << "stop color" << pos; | |
293 | return stop.second; |
|
293 | return stop.second; | |
294 | } |
|
294 | } | |
295 | } |
|
295 | } | |
296 |
|
296 | |||
297 | // find next stop relative to position |
|
297 | // find next stop relative to position | |
298 | QGradientStop next = stops.last(); |
|
298 | QGradientStop next = stops.last(); | |
299 | for (int i=count-1; i>=0; i--) { |
|
299 | for (int i=count-1; i>=0; i--) { | |
300 | QGradientStop stop = stops.at(i); |
|
300 | QGradientStop stop = stops.at(i); | |
301 | if (pos < stop.first) |
|
301 | if (pos < stop.first) | |
302 | next = stop; |
|
302 | next = stop; | |
303 | } |
|
303 | } | |
304 |
|
304 | |||
305 | //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first; |
|
305 | //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first; | |
306 |
|
306 | |||
307 | qreal range = next.first - prev.first; |
|
307 | qreal range = next.first - prev.first; | |
308 | qreal posDelta = pos - prev.first; |
|
308 | qreal posDelta = pos - prev.first; | |
309 | qreal relativePos = posDelta / range; |
|
309 | qreal relativePos = posDelta / range; | |
310 |
|
310 | |||
311 | //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos; |
|
311 | //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos; | |
312 |
|
312 | |||
313 | return colorAt(prev.second, next.second, relativePos); |
|
313 | return colorAt(prev.second, next.second, relativePos); | |
314 | } |
|
314 | } | |
315 |
|
315 | |||
316 | QTCOMMERCIALCHART_END_NAMESPACE |
|
316 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,96 +1,96 | |||||
1 | #include "charttheme_p.h" |
|
1 | #include "charttheme_p.h" | |
2 | #ifdef Q_OS_WIN |
|
2 | #ifdef Q_OS_WIN | |
3 | #include <windows.h> |
|
3 | #include <windows.h> | |
4 | #include <stdio.h> |
|
4 | #include <stdio.h> | |
5 | #endif |
|
5 | #endif | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | class ChartThemeDefault: public ChartTheme |
|
9 | class ChartThemeDefault: public ChartTheme | |
10 | { |
|
10 | { | |
11 | public: |
|
11 | public: | |
12 | ChartThemeDefault():ChartTheme(QChart::ChartThemeDefault) |
|
12 | ChartThemeDefault():ChartTheme(QChart::ChartThemeDefault) | |
13 | { |
|
13 | { | |
14 | #ifdef Q_OS_WIN |
|
14 | #ifdef Q_OS_WIN | |
15 | // First series base color from COLOR_WINDOWFRAME |
|
15 | // First series base color from COLOR_WINDOWFRAME | |
16 | DWORD colorWindowFrame; |
|
16 | DWORD colorWindowFrame; | |
17 | colorWindowFrame = GetSysColor(COLOR_WINDOWFRAME); |
|
17 | colorWindowFrame = GetSysColor(COLOR_WINDOWFRAME); | |
18 | m_seriesColors.append(QColor(GetRValue(colorWindowFrame), GetGValue(colorWindowFrame),GetBValue(colorWindowFrame))); |
|
18 | m_seriesColors.append(QColor(GetRValue(colorWindowFrame), GetGValue(colorWindowFrame),GetBValue(colorWindowFrame))); | |
19 |
|
19 | |||
20 | // Second series base color from COLOR_HIGHLIGHT |
|
20 | // Second series base color from COLOR_HIGHLIGHT | |
21 | DWORD colorHighlight; |
|
21 | DWORD colorHighlight; | |
22 | colorHighlight = GetSysColor(COLOR_HIGHLIGHT); |
|
22 | colorHighlight = GetSysColor(COLOR_HIGHLIGHT); | |
23 | m_seriesColors.append(QColor(GetRValue(colorHighlight), GetGValue(colorHighlight),GetBValue(colorHighlight))); |
|
23 | m_seriesColors.append(QColor(GetRValue(colorHighlight), GetGValue(colorHighlight),GetBValue(colorHighlight))); | |
24 |
|
24 | |||
25 | // Thirs series base color from the middle of the COLOR_ACTIVECAPTION / |
|
25 | // Thirs series base color from the middle of the COLOR_ACTIVECAPTION / | |
26 | // COLOR_GRADIENTACTIVECAPTION gradient |
|
26 | // COLOR_GRADIENTACTIVECAPTION gradient | |
27 | DWORD colorGradientActiveCaptionLeft; |
|
27 | DWORD colorGradientActiveCaptionLeft; | |
28 | colorGradientActiveCaptionLeft = GetSysColor(COLOR_ACTIVECAPTION); |
|
28 | colorGradientActiveCaptionLeft = GetSysColor(COLOR_ACTIVECAPTION); | |
29 | DWORD colorGradientActiveCaptionRight; |
|
29 | DWORD colorGradientActiveCaptionRight; | |
30 | colorGradientActiveCaptionRight = GetSysColor(COLOR_GRADIENTACTIVECAPTION); |
|
30 | colorGradientActiveCaptionRight = GetSysColor(COLOR_GRADIENTACTIVECAPTION); | |
31 | QLinearGradient g; |
|
31 | QLinearGradient g; | |
32 | QColor start = QColor(GetRValue(colorGradientActiveCaptionLeft), |
|
32 | QColor start = QColor(GetRValue(colorGradientActiveCaptionLeft), | |
33 | GetGValue(colorGradientActiveCaptionLeft), |
|
33 | GetGValue(colorGradientActiveCaptionLeft), | |
34 | GetBValue(colorGradientActiveCaptionLeft)); |
|
34 | GetBValue(colorGradientActiveCaptionLeft)); | |
35 | g.setColorAt(0.0, start); |
|
35 | g.setColorAt(0.0, start); | |
36 | QColor end = QColor(GetRValue(colorGradientActiveCaptionRight), |
|
36 | QColor end = QColor(GetRValue(colorGradientActiveCaptionRight), | |
37 | GetGValue(colorGradientActiveCaptionRight), |
|
37 | GetGValue(colorGradientActiveCaptionRight), | |
38 | GetBValue(colorGradientActiveCaptionRight)); |
|
38 | GetBValue(colorGradientActiveCaptionRight)); | |
39 | g.setColorAt(1.0, end); |
|
39 | g.setColorAt(1.0, end); | |
40 | m_seriesColors.append(colorAt(g, 0.5)); |
|
40 | m_seriesColors.append(colorAt(g, 0.5)); | |
41 |
|
41 | |||
42 | // Generate gradients from the base colors |
|
42 | // Generate gradients from the base colors | |
43 | generateSeriesGradients(); |
|
43 | generateSeriesGradients(); | |
44 |
|
44 | |||
45 | // Background fill color from COLOR_WINDOW |
|
45 | // Background fill color from COLOR_WINDOW | |
46 | QLinearGradient backgroundGradient; |
|
46 | QLinearGradient backgroundGradient; | |
47 | DWORD colorWindow; |
|
47 | DWORD colorWindow; | |
48 | colorWindow = GetSysColor(COLOR_WINDOW); |
|
48 | colorWindow = GetSysColor(COLOR_WINDOW); | |
49 | backgroundGradient.setColorAt(0.0, QColor(GetRValue(colorWindow), |
|
49 | backgroundGradient.setColorAt(0.0, QColor(GetRValue(colorWindow), | |
50 | GetGValue(colorWindow), |
|
50 | GetGValue(colorWindow), | |
51 | GetBValue(colorWindow))); |
|
51 | GetBValue(colorWindow))); | |
52 | backgroundGradient.setColorAt(1.0, QColor(GetRValue(colorWindow), |
|
52 | backgroundGradient.setColorAt(1.0, QColor(GetRValue(colorWindow), | |
53 | GetGValue(colorWindow), |
|
53 | GetGValue(colorWindow), | |
54 | GetBValue(colorWindow))); |
|
54 | GetBValue(colorWindow))); | |
55 |
|
55 | |||
56 | // TODO: COLOR_WINDOWTEXT for text color? |
|
56 | // TODO: COLOR_WINDOWTEXT for text color? | |
57 | // TODO: COLOR_INFOTEXT for tooltip text color? |
|
57 | // TODO: COLOR_INFOTEXT for tooltip text color? | |
58 | // TODO: COLOR_INFOBK for tooltip background color? |
|
58 | // TODO: COLOR_INFOBK for tooltip background color? | |
59 | #elif Q_OS_LINUX |
|
59 | #elif defined(Q_OS_LINUX) | |
60 | // TODO: replace this dummy theme with linux specific theme |
|
60 | // TODO: replace this dummy theme with linux specific theme | |
61 | m_seriesColors << QRgb(0xff707070); |
|
61 | m_seriesColors << QRgb(0xff707070); | |
62 | m_seriesColors << QRgb(0xffA0A0A0); |
|
62 | m_seriesColors << QRgb(0xffA0A0A0); | |
63 | generateSeriesGradients(); |
|
63 | generateSeriesGradients(); | |
64 |
|
64 | |||
65 | QLinearGradient backgroundGradient; |
|
65 | QLinearGradient backgroundGradient; | |
66 | backgroundGradient.setColorAt(0.0, QRgb(0xffffffff)); |
|
66 | backgroundGradient.setColorAt(0.0, QRgb(0xffffffff)); | |
67 | backgroundGradient.setColorAt(1.0, QRgb(0xffafafaf)); |
|
67 | backgroundGradient.setColorAt(1.0, QRgb(0xffafafaf)); | |
68 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
68 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); | |
69 | m_backgroundGradient = backgroundGradient; |
|
69 | m_backgroundGradient = backgroundGradient; | |
70 | #elif Q_OS_MAC |
|
70 | #elif Q_OS_MAC | |
71 | // TODO: replace this dummy theme with OSX specific theme |
|
71 | // TODO: replace this dummy theme with OSX specific theme | |
72 | m_seriesColors << QRgb(0xff707070); |
|
72 | m_seriesColors << QRgb(0xff707070); | |
73 | m_seriesColors << QRgb(0xffA0A0A0); |
|
73 | m_seriesColors << QRgb(0xffA0A0A0); | |
74 | generateSeriesGradients(); |
|
74 | generateSeriesGradients(); | |
75 |
|
75 | |||
76 | QLinearGradient backgroundGradient; |
|
76 | QLinearGradient backgroundGradient; | |
77 | backgroundGradient.setColorAt(0.0, QRgb(0xffffffff)); |
|
77 | backgroundGradient.setColorAt(0.0, QRgb(0xffffffff)); | |
78 | backgroundGradient.setColorAt(1.0, QRgb(0xffafafaf)); |
|
78 | backgroundGradient.setColorAt(1.0, QRgb(0xffafafaf)); | |
79 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
79 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); | |
80 | m_backgroundGradient = backgroundGradient; |
|
80 | m_backgroundGradient = backgroundGradient; | |
81 | #else |
|
81 | #else | |
82 | // TODO: replace this dummy theme with generic (not OS specific) theme |
|
82 | // TODO: replace this dummy theme with generic (not OS specific) theme | |
83 | m_seriesColors << QRgb(0xff707070); |
|
83 | m_seriesColors << QRgb(0xff707070); | |
84 | m_seriesColors << QRgb(0xffA0A0A0); |
|
84 | m_seriesColors << QRgb(0xffA0A0A0); | |
85 | generateSeriesGradients(); |
|
85 | generateSeriesGradients(); | |
86 |
|
86 | |||
87 | QLinearGradient backgroundGradient; |
|
87 | QLinearGradient backgroundGradient; | |
88 | backgroundGradient.setColorAt(0.0, QRgb(0xffffffff)); |
|
88 | backgroundGradient.setColorAt(0.0, QRgb(0xffffffff)); | |
89 | backgroundGradient.setColorAt(1.0, QRgb(0xffafafaf)); |
|
89 | backgroundGradient.setColorAt(1.0, QRgb(0xffafafaf)); | |
90 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
90 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); | |
91 | m_backgroundGradient = backgroundGradient; |
|
91 | m_backgroundGradient = backgroundGradient; | |
92 | #endif |
|
92 | #endif | |
93 | } |
|
93 | } | |
94 | }; |
|
94 | }; | |
95 |
|
95 | |||
96 | QTCOMMERCIALCHART_END_NAMESPACE |
|
96 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now