charttheme.cpp
398 lines
| 11.5 KiB
| text/x-c
|
CppLexer
/ src / charttheme.cpp
|
r794 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r103 | #include "charttheme_p.h" | ||
#include "qchart.h" | ||||
|
r584 | #include "qchartview.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" | ||||
|
r666 | #include "barchartitem_p.h" | ||
#include "stackedbarchartitem_p.h" | ||||
#include "percentbarchartitem_p.h" | ||||
|
r145 | #include "linechartitem_p.h" | ||
|
r421 | #include "areachartitem_p.h" | ||
|
r470 | #include "scatterchartitem_p.h" | ||
|
r568 | #include "piechartitem_p.h" | ||
|
r460 | #include "splinechartitem_p.h" | ||
|
r103 | |||
|
r143 | //themes | ||
|
r853 | #include "chartthemesystem_p.h" | ||
|
r651 | #include "chartthemelight_p.h" | ||
#include "chartthemebluecerulean_p.h" | ||||
#include "chartthemedark_p.h" | ||||
#include "chartthemebrownsand_p.h" | ||||
#include "chartthemebluencs_p.h" | ||||
|
r757 | #include "chartthemehighcontrast_p.h" | ||
#include "chartthemeblueicy_p.h" | ||||
|
r125 | |||
|
r143 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r614 | ChartTheme::ChartTheme(QChart::ChartTheme id) : | ||
|
r853 | m_masterFont(QFont("arial", 14)), | ||
|
r717 | m_labelFont(QFont("arial", 10)), | ||
|
r614 | m_titleBrush(QColor(QRgb(0x000000))), | ||
m_axisLinePen(QPen(QRgb(0x000000))), | ||||
m_axisLabelBrush(QColor(QRgb(0x000000))), | ||||
m_backgroundShadesPen(Qt::NoPen), | ||||
m_backgroundShadesBrush(Qt::NoBrush), | ||||
m_backgroundShades(BackgroundShadesNone), | ||||
|
r943 | m_gridLinePen(QPen(QRgb(0x000000))), | ||
m_force(false) | ||||
|
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) { | ||||
|
r651 | case QChart::ChartThemeLight: | ||
return new ChartThemeLight(); | ||||
case QChart::ChartThemeBlueCerulean: | ||||
return new ChartThemeBlueCerulean(); | ||||
case QChart::ChartThemeDark: | ||||
return new ChartThemeDark(); | ||||
case QChart::ChartThemeBrownSand: | ||||
return new ChartThemeBrownSand(); | ||||
case QChart::ChartThemeBlueNcs: | ||||
return new ChartThemeBlueNcs(); | ||||
|
r757 | case QChart::ChartThemeHighContrast: | ||
return new ChartThemeHighContrast(); | ||||
case QChart::ChartThemeBlueIcy: | ||||
return new ChartThemeBlueIcy(); | ||||
|
r494 | default: | ||
|
r853 | return new ChartThemeSystem(); | ||
|
r143 | } | ||
} | ||||
|
r948 | void ChartTheme::decorate(QChart *chart) | ||
|
r143 | { | ||
|
r645 | QBrush brush; | ||
|
r943 | if(brush == chart->backgroundBrush() || m_force) | ||
|
r757 | chart->setBackgroundBrush(m_chartBackgroundGradient); | ||
|
r645 | chart->setTitleFont(m_masterFont); | ||
chart->setTitleBrush(m_titleBrush); | ||||
|
r143 | } | ||
|
r948 | void ChartTheme::decorate(QLegend *legend) | ||
|
r540 | { | ||
|
r645 | QPen pen; | ||
QBrush brush; | ||||
|
r943 | if (pen == legend->pen() || m_force){ | ||
|
r783 | legend->setPen(Qt::NoPen); | ||
|
r645 | } | ||
|
r943 | if (brush == legend->brush() || m_force) { | ||
|
r645 | legend->setBrush(m_chartBackgroundGradient); | ||
} | ||||
|
r540 | } | ||
|
r948 | void ChartTheme::decorate(QAreaSeries *series, int index) | ||
|
r421 | { | ||
|
r644 | QPen pen; | ||
QBrush brush; | ||||
|
r421 | |||
|
r943 | if (pen == series->pen() || m_force){ | ||
|
r653 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); | ||
|
r644 | pen.setWidthF(2); | ||
series->setPen(pen); | ||||
} | ||||
|
r943 | if (brush == series->brush() || m_force) { | ||
|
r644 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); | ||
series->setBrush(brush); | ||||
} | ||||
|
r421 | } | ||
|
r948 | void ChartTheme::decorate(QLineSeries *series,int index) | ||
|
r103 | { | ||
|
r644 | QPen pen; | ||
|
r943 | if(pen == series->pen() || m_force ){ | ||
|
r644 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); | ||
pen.setWidthF(2); | ||||
series->setPen(pen); | ||||
} | ||||
|
r103 | } | ||
|
r948 | void ChartTheme::decorate(QBarSeries *series, int index) | ||
|
r108 | { | ||
|
r645 | QBrush brush; | ||
|
r653 | QPen pen; | ||
|
r948 | QList<QBarSet *> sets = series->barSets(); | ||
|
r645 | |||
|
r661 | qreal takeAtPos = 0.5; | ||
qreal step = 0.2; | ||||
if (sets.count() > 1 ) { | ||||
step = 1.0 / (qreal) sets.count(); | ||||
if (sets.count() % m_seriesGradients.count()) | ||||
step *= m_seriesGradients.count(); | ||||
else | ||||
step *= (m_seriesGradients.count() - 1); | ||||
} | ||||
|
r645 | |||
|
r661 | for (int i(0); i < sets.count(); i++) { | ||
int colorIndex = (index + i) % m_seriesGradients.count(); | ||||
if (i > 0 && i % m_seriesGradients.count() == 0) { | ||||
// There is no dedicated base color for each sets, generate more colors | ||||
takeAtPos += step; | ||||
if (takeAtPos == 1.0) | ||||
takeAtPos += step; | ||||
takeAtPos -= (int) takeAtPos; | ||||
|
r512 | } | ||
|
r943 | if (brush == sets.at(i)->brush() || m_force ) | ||
|
r661 | sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos)); | ||
|
r103 | |||
|
r653 | // Pick label color from the opposite end of the gradient. | ||
// 0.3 as a boundary seems to work well. | ||||
|
r661 | if (takeAtPos < 0.3) | ||
|
r820 | sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1)); | ||
|
r653 | else | ||
|
r820 | sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0)); | ||
|
r645 | |||
|
r943 | if (pen == sets.at(i)->pen() || m_force) { | ||
|
r653 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); | ||
sets.at(i)->setPen(c); | ||||
|
r512 | } | ||
|
r164 | } | ||
|
r143 | } | ||
|
r103 | |||
|
r948 | void ChartTheme::decorate(QScatterSeries *series, int index) | ||
|
r182 | { | ||
|
r644 | QPen pen; | ||
QBrush brush; | ||||
|
r943 | if (pen == series->pen() || m_force) { | ||
|
r653 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); | ||
|
r644 | pen.setWidthF(2); | ||
series->setPen(pen); | ||||
} | ||||
|
r943 | if (brush == series->brush() || m_force) { | ||
|
r644 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); | ||
series->setBrush(brush); | ||||
} | ||||
|
r182 | } | ||
|
r948 | void ChartTheme::decorate(QPieSeries *series, int index) | ||
|
r163 | { | ||
|
r818 | |||
|
r507 | for (int i(0); i < series->slices().count(); i++) { | ||
|
r691 | |||
QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); | ||||
|
r653 | |||
// Get color for a slice from a gradient linearly, beginning from the start of the gradient | ||||
|
r661 | qreal pos = (qreal) (i + 1) / (qreal) series->count(); | ||
|
r691 | QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | ||
|
r818 | QPieSlice *s = series->slices().at(i); | ||
|
r822 | PieSliceData data = PieSliceData::data(s); | ||
|
r691 | |||
|
r943 | if (data.m_slicePen.isThemed() || m_force) { | ||
|
r691 | data.m_slicePen = penColor; | ||
data.m_slicePen.setThemed(true); | ||||
} | ||||
|
r943 | if (data.m_sliceBrush.isThemed() || m_force) { | ||
|
r691 | data.m_sliceBrush = brushColor; | ||
data.m_sliceBrush.setThemed(true); | ||||
} | ||||
|
r943 | if (data.m_labelPen.isThemed() || m_force) { | ||
|
r714 | data.m_labelPen = QPen(m_titleBrush.color()); | ||
data.m_labelPen.setThemed(true); | ||||
} | ||||
|
r943 | if (data.m_labelFont.isThemed() || m_force) { | ||
|
r717 | data.m_labelFont = m_labelFont; | ||
|
r714 | data.m_labelFont.setThemed(true); | ||
} | ||||
|
r822 | if (PieSliceData::data(s) != data) { | ||
PieSliceData::data(s) = data; | ||||
emit PieSliceData::data(s).emitChangedSignal(s); | ||||
|
r645 | } | ||
|
r163 | } | ||
} | ||||
|
r948 | void ChartTheme::decorate(QSplineSeries *series, int index) | ||
|
r401 | { | ||
|
r644 | QPen pen; | ||
|
r943 | if(pen == series->pen() || m_force){ | ||
|
r644 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); | ||
pen.setWidthF(2); | ||||
series->setPen(pen); | ||||
} | ||||
|
r401 | } | ||
|
r948 | void ChartTheme::decorate(QChartAxis *axis,bool axisX) | ||
|
r548 | { | ||
|
r645 | QPen pen; | ||
QBrush brush; | ||||
QFont font; | ||||
|
r548 | if (axis->isAxisVisible()) { | ||
|
r645 | |||
|
r943 | if(brush == axis->labelsBrush() || m_force){ | ||
|
r645 | axis->setLabelsBrush(m_axisLabelBrush); | ||
} | ||||
|
r943 | if(pen == axis->labelsPen() || m_force){ | ||
|
r645 | axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons | ||
} | ||||
|
r943 | if (axis->shadesVisible() || m_force) { | ||
|
r645 | |||
|
r943 | if(brush == axis->shadesBrush() || m_force){ | ||
|
r645 | axis->setShadesBrush(m_backgroundShadesBrush); | ||
} | ||||
|
r943 | if(pen == axis->shadesPen() || m_force){ | ||
|
r645 | axis->setShadesPen(m_backgroundShadesPen); | ||
} | ||||
|
r943 | if( m_force && (m_backgroundShades == BackgroundShadesBoth | ||
|
r645 | || (m_backgroundShades == BackgroundShadesVertical && axisX) | ||
|| (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){ | ||||
axis->setShadesVisible(true); | ||||
} | ||||
} | ||||
|
r943 | if(pen == axis->axisPen() || m_force){ | ||
|
r645 | axis->setAxisPen(m_axisLinePen); | ||
} | ||||
|
r943 | if(pen == axis->gridLinePen() || m_force){ | ||
|
r645 | axis->setGridLinePen(m_gridLinePen); | ||
} | ||||
|
r943 | if(font == axis->labelsFont() || m_force){ | ||
|
r717 | axis->setLabelsFont(m_labelFont); | ||
|
r548 | } | ||
} | ||||
} | ||||
|
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; | ||||
|
r656 | start.setHsvF(h, 0.0, 1.0); | ||
|
r507 | 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) | ||
{ | ||||
|
r948 | Q_ASSERT(pos >= 0.0 && pos <= 1.0); | ||
|
r494 | 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) | ||||
{ | ||||
|
r948 | Q_ASSERT(pos >= 0 && pos <= 1.0); | ||
|
r494 | |||
// 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(); | ||||
|
r948 | for (int i = 0; i < count; i++) { | ||
|
r494 | 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(); | ||||
|
r948 | for (int i = count - 1; i >= 0; i--) { | ||
|
r494 | 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); | ||||
} | ||||
|
r943 | void ChartTheme::setForced(bool enabled) | ||
{ | ||||
m_force=enabled; | ||||
} | ||||
|
r103 | QTCOMMERCIALCHART_END_NAMESPACE | ||