charttheme.cpp
329 lines
| 9.9 KiB
| text/x-c
|
CppLexer
/ src / charttheme.cpp
|
r103 | #include "charttheme_p.h" | ||
#include "qchart.h" | ||||
|
r540 | #include "qlegend.h" | ||
|
r176 | #include "qchartaxis.h" | ||
|
r324 | #include <QTime> | ||
|
r103 | |||
|
r143 | //series | ||
|
r214 | #include "qbarset.h" | ||
|
r338 | #include "qbarseries.h" | ||
#include "qstackedbarseries.h" | ||||
#include "qpercentbarseries.h" | ||||
|
r349 | #include "qlineseries.h" | ||
|
r421 | #include "qareaseries.h" | ||
|
r182 | #include "qscatterseries.h" | ||
|
r163 | #include "qpieseries.h" | ||
|
r203 | #include "qpieslice.h" | ||
|
r401 | #include "qsplineseries.h" | ||
|
r163 | |||
|
r143 | //items | ||
#include "axisitem_p.h" | ||||
|
r381 | #include "barpresenter_p.h" | ||
#include "stackedbarpresenter_p.h" | ||||
#include "percentbarpresenter_p.h" | ||||
|
r145 | #include "linechartitem_p.h" | ||
|
r421 | #include "areachartitem_p.h" | ||
|
r470 | #include "scatterchartitem_p.h" | ||
|
r419 | #include "piepresenter_p.h" | ||
|
r460 | #include "splinechartitem_p.h" | ||
|
r103 | |||
|
r143 | //themes | ||
|
r494 | #include "chartthemedefault_p.h" | ||
|
r143 | #include "chartthemevanilla_p.h" | ||
#include "chartthemeicy_p.h" | ||||
#include "chartthemegrayscale_p.h" | ||||
#include "chartthemescientific_p.h" | ||||
|
r103 | |||
|
r125 | |||
|
r143 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r153 | ChartTheme::ChartTheme(QChart::ChartTheme id) | ||
|
r143 | { | ||
|
r153 | m_id = id; | ||
|
r324 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | ||
|
r143 | } | ||
|
r153 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) | ||
|
r143 | { | ||
switch(theme) { | ||||
case QChart::ChartThemeVanilla: | ||||
return new ChartThemeVanilla(); | ||||
case QChart::ChartThemeIcy: | ||||
return new ChartThemeIcy(); | ||||
case QChart::ChartThemeGrayscale: | ||||
return new ChartThemeGrayscale(); | ||||
case QChart::ChartThemeScientific: | ||||
return new ChartThemeScientific(); | ||||
|
r494 | default: | ||
return new ChartThemeDefault(); | ||||
|
r143 | } | ||
} | ||||
void ChartTheme::decorate(QChart* chart) | ||||
{ | ||||
|
r494 | chart->setChartBackgroundBrush(m_backgroundGradient); | ||
|
r549 | chart->setChartTitleFont(m_masterFont); | ||
|
r143 | } | ||
|
r540 | void ChartTheme::decorate(QLegend* legend) | ||
{ | ||||
|
r550 | legend->setBackgroundBrush(m_backgroundGradient); | ||
|
r540 | } | ||
|
r538 | void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series, int index) | ||
|
r421 | { | ||
QPen pen; | ||||
QBrush brush; | ||||
|
r516 | if (pen != series->pen()){ | ||
|
r538 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0)); | ||
|
r421 | pen.setWidthF(2); | ||
|
r557 | series->setPen(pen); | ||
|
r421 | } | ||
|
r516 | if (brush != series->brush()) { | ||
|
r538 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); | ||
|
r557 | series->setBrush(brush); | ||
|
r421 | } | ||
} | ||||
|
r538 | void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int index) | ||
|
r103 | { | ||
|
r152 | QPen pen; | ||
if(pen != series->pen()){ | ||||
|
r544 | series->setPen(series->pen()); | ||
|
r152 | return; | ||
|
r544 | }else{ | ||
pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); | ||||
pen.setWidthF(2); | ||||
series->setPen(pen); | ||||
|
r152 | } | ||
|
r103 | } | ||
|
r538 | void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index) | ||
|
r108 | { | ||
|
r357 | QList<QBarSet*> sets = series->barSets(); | ||
|
r509 | for (int i=0; i<sets.count(); i++) { | ||
|
r523 | qreal pos = 0.5; | ||
if (sets.count() > 1) | ||||
pos = (qreal) i / (qreal) (sets.count() - 1); | ||||
|
r538 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | ||
|
r509 | sets.at(i)->setBrush(QBrush(c)); | ||
|
r512 | |||
// Pick label color as far as possible from bar color (within gradient). | ||||
// 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :) | ||||
// TODO: better picking of label color? | ||||
if (pos < 0.3) { | ||||
|
r538 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); | ||
|
r512 | } else { | ||
|
r538 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); | ||
|
r512 | } | ||
sets.at(i)->setFloatingValuePen(QPen(c)); | ||||
|
r164 | } | ||
|
r108 | } | ||
|
r538 | void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int index) | ||
|
r103 | { | ||
|
r357 | QList<QBarSet*> sets = series->barSets(); | ||
|
r509 | for (int i=0; i<sets.count(); i++) { | ||
|
r523 | qreal pos = 0.5; | ||
if (sets.count() > 1) | ||||
pos = (qreal) i / (qreal) (sets.count() - 1); | ||||
|
r538 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | ||
|
r509 | sets.at(i)->setBrush(QBrush(c)); | ||
|
r512 | |||
if (pos < 0.3) { | ||||
|
r538 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); | ||
|
r512 | } else { | ||
|
r538 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); | ||
|
r512 | } | ||
sets.at(i)->setFloatingValuePen(QPen(c)); | ||||
|
r164 | } | ||
|
r103 | } | ||
|
r538 | void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int index) | ||
|
r143 | { | ||
|
r357 | QList<QBarSet*> sets = series->barSets(); | ||
|
r509 | for (int i=0; i<sets.count(); i++) { | ||
|
r523 | qreal pos = 0.5; | ||
if (sets.count() > 1) | ||||
pos = (qreal) i / (qreal) (sets.count() - 1); | ||||
|
r538 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | ||
|
r509 | sets.at(i)->setBrush(QBrush(c)); | ||
|
r512 | |||
if (pos < 0.3) { | ||||
|
r538 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); | ||
|
r512 | } else { | ||
|
r538 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); | ||
|
r512 | } | ||
sets.at(i)->setFloatingValuePen(QPen(c)); | ||||
|
r164 | } | ||
|
r143 | } | ||
|
r103 | |||
|
r538 | void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int index) | ||
|
r182 | { | ||
|
r470 | Q_ASSERT(item); | ||
|
r182 | Q_ASSERT(series); | ||
|
r541 | QPen pen; | ||
QBrush brush; | ||||
if (pen == series->pen()) { | ||||
|
r543 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0)); | ||
|
r541 | pen.setWidthF(2); | ||
series->setPen(pen); | ||||
} | ||||
|
r195 | |||
|
r541 | if (brush == series->brush()) { | ||
|
r543 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); | ||
|
r541 | series->setBrush(brush); | ||
} | ||||
|
r182 | } | ||
|
r538 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int index) | ||
|
r163 | { | ||
|
r507 | // Get color for a slice from a gradient linearly, beginning from the start of the gradient | ||
for (int i(0); i < series->slices().count(); i++) { | ||||
qreal pos = (qreal) i / (qreal) series->count(); | ||||
|
r538 | QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1); | ||
|
r510 | series->slices().at(i)->setSlicePen(penColor); | ||
|
r538 | QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | ||
|
r510 | series->slices().at(i)->setSliceBrush(brushColor); | ||
|
r163 | } | ||
} | ||||
|
r538 | void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int index) | ||
|
r401 | { | ||
|
r460 | Q_ASSERT(item); | ||
|
r401 | Q_ASSERT(series); | ||
|
r434 | QPen pen; | ||
|
r476 | |||
|
r434 | if(pen != series->pen()){ | ||
|
r476 | item->setLinePen(series->pen()); | ||
}else{ | ||||
|
r538 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); | ||
|
r476 | pen.setWidthF(series->pen().widthF()); | ||
item->setLinePen(series->pen()); | ||||
|
r434 | } | ||
|
r538 | // QColor color = m_seriesColors.at(index % m_seriesColors.size()); | ||
|
r401 | // TODO: define alpha in the theme? or in the series? | ||
//color.setAlpha(120); | ||||
// QBrush brush(color, Qt::SolidPattern); | ||||
// presenter->m_markerBrush = brush; | ||||
// QPen pen(brush, 3); | ||||
// pen.setColor(color); | ||||
// presenter->m_markerPen = pen; | ||||
} | ||||
|
r548 | void ChartTheme::decorate(QChartAxis* axis, AxisItem* item) | ||
{ | ||||
Q_ASSERT(axis); | ||||
Q_ASSERT(item); | ||||
if (axis->isAxisVisible()) { | ||||
axis->setLabelsBrush(m_axisLabelBrush); | ||||
axis->setLabelsPen(m_axisLabelPen); | ||||
// TODO: check the axis type (x or y) should define whether to show the shades or not | ||||
if (m_backgroundShades == BackgroundShadesBoth | ||||
|| m_backgroundShades == BackgroundShadesVertical /*&& x axis ?*/ | ||||
|| m_backgroundShades == BackgroundShadesHorizontal /* && y axis*/) { | ||||
axis->setShadesPen(m_backgroundShadesPen); | ||||
axis->setShadesBrush(m_backgroundShadesBrush); | ||||
} else { | ||||
// The shades not supposed to be shown for this axis, clear possible brush and pen | ||||
axis->setShadesPen(Qt::NoPen); | ||||
axis->setShadesBrush(Qt::NoBrush); | ||||
} | ||||
axis->setAxisPen(m_axisLinePen); | ||||
axis->setGridLinePen(m_gridLinePen); | ||||
axis->setLabelsFont(m_masterFont); | ||||
} | ||||
} | ||||
|
r507 | void ChartTheme::generateSeriesGradients() | ||
{ | ||||
// Generate gradients in HSV color space | ||||
foreach (QColor color, m_seriesColors) { | ||||
QLinearGradient g; | ||||
qreal h = color.hsvHueF(); | ||||
qreal s = color.hsvSaturationF(); | ||||
// TODO: tune the algorithm to give nice results with most base colors defined in | ||||
// most themes. The rest of the gradients we can define manually in theme specific | ||||
// implementation. | ||||
QColor start = color; | ||||
start.setHsvF(h, 0.05, 0.95); | ||||
g.setColorAt(0.0, start); | ||||
g.setColorAt(0.5, color); | ||||
QColor end = color; | ||||
end.setHsvF(h, s, 0.25); | ||||
g.setColorAt(1.0, end); | ||||
m_seriesGradients << g; | ||||
} | ||||
} | ||||
|
r494 | QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos) | ||
{ | ||||
Q_ASSERT(pos >=0.0 && pos <= 1.0); | ||||
qreal r = start.redF() + ((end.redF() - start.redF()) * pos); | ||||
qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos); | ||||
qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos); | ||||
QColor c; | ||||
c.setRgbF(r, g, b); | ||||
return c; | ||||
} | ||||
QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos) | ||||
{ | ||||
Q_ASSERT(pos >=0 && pos <= 1.0); | ||||
// another possibility: | ||||
// http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient | ||||
QGradientStops stops = gradient.stops(); | ||||
int count = stops.count(); | ||||
// find previous stop relative to position | ||||
QGradientStop prev = stops.first(); | ||||
for (int i=0; i<count; i++) { | ||||
QGradientStop stop = stops.at(i); | ||||
if (pos > stop.first) | ||||
prev = stop; | ||||
// given position is actually a stop position? | ||||
if (pos == stop.first) { | ||||
//qDebug() << "stop color" << pos; | ||||
return stop.second; | ||||
} | ||||
} | ||||
// find next stop relative to position | ||||
QGradientStop next = stops.last(); | ||||
for (int i=count-1; i>=0; i--) { | ||||
QGradientStop stop = stops.at(i); | ||||
if (pos < stop.first) | ||||
next = stop; | ||||
} | ||||
//qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first; | ||||
qreal range = next.first - prev.first; | ||||
qreal posDelta = pos - prev.first; | ||||
qreal relativePos = posDelta / range; | ||||
//qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos; | ||||
return colorAt(prev.second, next.second, relativePos); | ||||
} | ||||
|
r103 | QTCOMMERCIALCHART_END_NAMESPACE | ||