charttheme.cpp
255 lines
| 7.9 KiB
| text/x-c
|
CppLexer
/ src / charttheme.cpp
Tero Ahola
|
r103 | #include "charttheme_p.h" | ||
#include "qchart.h" | ||||
Michal Klocek
|
r176 | #include "qchartaxis.h" | ||
Tero Ahola
|
r103 | |||
Michal Klocek
|
r143 | //series | ||
#include "barchartseries.h" | ||||
#include "stackedbarchartseries.h" | ||||
#include "percentbarchartseries.h" | ||||
Michal Klocek
|
r145 | #include "qlinechartseries.h" | ||
Tero Ahola
|
r182 | #include "qscatterseries.h" | ||
Jani Honkonen
|
r163 | #include "qpieseries.h" | ||
Michal Klocek
|
r143 | //items | ||
#include "axisitem_p.h" | ||||
#include "bargroup.h" | ||||
#include "stackedbargroup.h" | ||||
Michal Klocek
|
r145 | #include "linechartitem_p.h" | ||
Michal Klocek
|
r143 | #include "percentbargroup.h" | ||
Tero Ahola
|
r194 | #include "scatterpresenter_p.h" | ||
Jani Honkonen
|
r163 | #include "piepresenter.h" | ||
Tero Ahola
|
r103 | |||
Michal Klocek
|
r143 | //themes | ||
#include "chartthemevanilla_p.h" | ||||
#include "chartthemeicy_p.h" | ||||
#include "chartthemegrayscale_p.h" | ||||
#include "chartthemescientific_p.h" | ||||
Tero Ahola
|
r103 | |||
Tero Ahola
|
r125 | |||
Michal Klocek
|
r143 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
/* TODO | ||||
Tero Ahola
|
r103 | case QChart::ChartThemeUnnamed1: | ||
Tero Ahola
|
r108 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2)); | ||
m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2)); | ||||
m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2)); | ||||
m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2)); | ||||
m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2)); | ||||
Tero Ahola
|
r103 | |||
m_gradientStartColor = QColor(QRgb(0xfff3dc9e)); | ||||
m_gradientEndColor = QColor(QRgb(0xffafafaf)); | ||||
Michal Klocek
|
r143 | */ | ||
Michal Klocek
|
r153 | ChartTheme::ChartTheme(QChart::ChartTheme id) | ||
Michal Klocek
|
r143 | { | ||
Michal Klocek
|
r153 | m_id = id; | ||
Michal Klocek
|
r143 | m_seriesColor.append(QRgb(0xff000000)); | ||
m_seriesColor.append(QRgb(0xff707070)); | ||||
m_gradientStartColor = QColor(QRgb(0xffffffff)); | ||||
m_gradientEndColor = QColor(QRgb(0xffafafaf)); | ||||
} | ||||
Michal Klocek
|
r153 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) | ||
Michal Klocek
|
r143 | { | ||
switch(theme) { | ||||
case QChart::ChartThemeDefault: | ||||
return new ChartTheme(); | ||||
case QChart::ChartThemeVanilla: | ||||
return new ChartThemeVanilla(); | ||||
case QChart::ChartThemeIcy: | ||||
return new ChartThemeIcy(); | ||||
case QChart::ChartThemeGrayscale: | ||||
return new ChartThemeGrayscale(); | ||||
case QChart::ChartThemeScientific: | ||||
return new ChartThemeScientific(); | ||||
} | ||||
} | ||||
void ChartTheme::decorate(QChart* chart) | ||||
{ | ||||
QLinearGradient backgroundGradient; | ||||
backgroundGradient.setColorAt(0.0, m_gradientStartColor); | ||||
backgroundGradient.setColorAt(1.0, m_gradientEndColor); | ||||
backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); | ||||
chart->setChartBackgroundBrush(backgroundGradient); | ||||
} | ||||
//TODO helper to by removed later | ||||
void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count) | ||||
{ | ||||
switch(series->type()) | ||||
{ | ||||
case QChartSeries::SeriesTypeLine: { | ||||
Michal Klocek
|
r145 | QLineChartSeries* s = static_cast<QLineChartSeries*>(series); | ||
LineChartItem* i = static_cast<LineChartItem*>(item); | ||||
Michal Klocek
|
r143 | decorate(i,s,count); | ||
break; | ||||
} | ||||
case QChartSeries::SeriesTypeBar: { | ||||
BarChartSeries* b = static_cast<BarChartSeries*>(series); | ||||
BarGroup* i = static_cast<BarGroup*>(item); | ||||
decorate(i,b,count); | ||||
break; | ||||
} | ||||
case QChartSeries::SeriesTypeStackedBar: { | ||||
StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series); | ||||
StackedBarGroup* i = static_cast<StackedBarGroup*>(item); | ||||
decorate(i,s,count); | ||||
break; | ||||
} | ||||
case QChartSeries::SeriesTypePercentBar: { | ||||
PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series); | ||||
PercentBarGroup* i = static_cast<PercentBarGroup*>(item); | ||||
decorate(i,s,count); | ||||
break; | ||||
} | ||||
Tero Ahola
|
r195 | case QChartSeries::SeriesTypeScatter: { | ||
QScatterSeries* s = qobject_cast<QScatterSeries*>(series); | ||||
Q_ASSERT(s); | ||||
ScatterPresenter* i = static_cast<ScatterPresenter*>(item); | ||||
Q_ASSERT(i); | ||||
decorate(i, s, count); | ||||
break; | ||||
} | ||||
Jani Honkonen
|
r163 | case QChartSeries::SeriesTypePie: { | ||
QPieSeries* s = static_cast<QPieSeries*>(series); | ||||
PiePresenter* i = static_cast<PiePresenter*>(item); | ||||
decorate(i,s,count); | ||||
break; | ||||
} | ||||
Michal Klocek
|
r143 | default: | ||
qDebug()<<"Wrong item to be decorated by theme"; | ||||
Tero Ahola
|
r103 | break; | ||
} | ||||
Michal Klocek
|
r143 | |||
Tero Ahola
|
r103 | } | ||
Michal Klocek
|
r145 | void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count) | ||
Tero Ahola
|
r103 | { | ||
Michal Klocek
|
r152 | QPen pen; | ||
if(pen != series->pen()){ | ||||
item->setPen(series->pen()); | ||||
return; | ||||
} | ||||
pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); | ||||
pen.setWidthF(2); | ||||
item->setPen(pen); | ||||
Tero Ahola
|
r103 | } | ||
Michal Klocek
|
r143 | void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count) | ||
Tero Ahola
|
r108 | { | ||
sauimone
|
r183 | // TODO: better way to descide series color and remove hard coded colors. | ||
item->resetBrushes(); | ||||
sauimone
|
r167 | for (int i=0; i<m_seriesColor.count(); i++) { | ||
sauimone
|
r183 | QBrush brush(m_seriesColor.at(i)); | ||
item->addBrush(brush); | ||||
sauimone
|
r164 | } | ||
sauimone
|
r183 | item->addBrush(QBrush(QColor(255,0,0,128))); | ||
item->addBrush(QBrush(QColor(255,255,0,128))); | ||||
item->addBrush(QBrush(QColor(0,255,0,128))); | ||||
item->addBrush(QBrush(QColor(0,0,255,128))); | ||||
item->addBrush(QBrush(QColor(255,128,0,128))); | ||||
Tero Ahola
|
r108 | } | ||
Michal Klocek
|
r143 | void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count) | ||
Tero Ahola
|
r103 | { | ||
sauimone
|
r183 | // TODO: better way to descide series color and remove hard coded colors. | ||
item->resetBrushes(); | ||||
for (int i=0; i<m_seriesColor.count(); i++) { | ||||
QBrush brush(m_seriesColor.at(i)); | ||||
item->addBrush(brush); | ||||
sauimone
|
r164 | } | ||
sauimone
|
r183 | item->addBrush(QBrush(QColor(255,0,0,128))); | ||
item->addBrush(QBrush(QColor(255,255,0,128))); | ||||
item->addBrush(QBrush(QColor(0,255,0,128))); | ||||
item->addBrush(QBrush(QColor(0,0,255,128))); | ||||
item->addBrush(QBrush(QColor(255,128,0,128))); | ||||
Tero Ahola
|
r103 | } | ||
Michal Klocek
|
r143 | void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count) | ||
{ | ||||
sauimone
|
r183 | // TODO: better way to descide series color and remove hard coded colors. | ||
item->resetBrushes(); | ||||
for (int i=0; i<m_seriesColor.count(); i++) { | ||||
QBrush brush(m_seriesColor.at(i)); | ||||
item->addBrush(brush); | ||||
sauimone
|
r164 | } | ||
sauimone
|
r183 | item->addBrush(QBrush(QColor(255,0,0,128))); | ||
item->addBrush(QBrush(QColor(255,255,0,128))); | ||||
item->addBrush(QBrush(QColor(0,255,0,128))); | ||||
item->addBrush(QBrush(QColor(0,0,255,128))); | ||||
item->addBrush(QBrush(QColor(255,128,0,128))); | ||||
Michal Klocek
|
r143 | } | ||
Tero Ahola
|
r103 | |||
Tero Ahola
|
r182 | void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count) | ||
{ | ||||
Q_ASSERT(presenter); | ||||
Q_ASSERT(series); | ||||
Tero Ahola
|
r195 | QColor color = m_seriesColor.at(count % m_seriesColor.size()); | ||
// TODO: define alpha in the theme? or in the series? | ||||
color.setAlpha(120); | ||||
QBrush brush(color, Qt::SolidPattern); | ||||
presenter->m_markerBrush = brush; | ||||
Tero Ahola
|
r182 | |||
Tero Ahola
|
r195 | QPen pen(brush, 1); | ||
pen.setColor(color); | ||||
presenter->m_markerPen = pen; | ||||
Tero Ahola
|
r182 | } | ||
Jani Honkonen
|
r163 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) | ||
{ | ||||
Jani Honkonen
|
r166 | // create a list of slice colors based on current theme | ||
int i = 0; | ||||
QList<QColor> colors; | ||||
while (colors.count() < series->count()) { | ||||
// get base color | ||||
QColor c = m_seriesColor[i++]; | ||||
i = i % m_seriesColor.count(); | ||||
// -1 means achromatic color -> cannot manipulate lightness | ||||
// TODO: find a better way to randomize lightness | ||||
if (c.toHsv().hue() == -1) | ||||
qWarning() << "ChartTheme::decorate() warning: achromatic theme color"; | ||||
// randomize lightness | ||||
qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter | ||||
c = c.lighter(f); | ||||
// find duplicates | ||||
bool isUnique = true; | ||||
foreach (QColor color, colors) { | ||||
if (c == color) | ||||
isUnique = false; | ||||
} | ||||
// add to array if unique | ||||
//if (isUnique) | ||||
colors << c; | ||||
} | ||||
// finally update colors | ||||
Jani Honkonen
|
r174 | foreach (QPieSliceId id, series->ids()) { | ||
QPieSlice s = series->slice(id); | ||||
s.setPen(QPen(Qt::black)); // TODO: get from theme | ||||
s.setBrush(colors.takeFirst()); | ||||
series->update(s); | ||||
Jani Honkonen
|
r163 | } | ||
} | ||||
Tero Ahola
|
r103 | |||
Michal Klocek
|
r176 | void ChartTheme::decorate(QChartAxis& axis,AxisItem* item) | ||
{ | ||||
//TODO: dummy defults for now | ||||
axis.setLabelsBrush(Qt::black); | ||||
axis.setLabelsPen(Qt::NoPen); | ||||
Michal Klocek
|
r184 | axis.setShadesPen(Qt::NoPen); | ||
Michal Klocek
|
r188 | axis.setShadesOpacity(0.5); | ||
Michal Klocek
|
r176 | item->handleAxisChanged(axis); | ||
} | ||||
Tero Ahola
|
r103 | QTCOMMERCIALCHART_END_NAMESPACE | ||