charttheme.cpp
289 lines
| 8.5 KiB
| text/x-c
|
CppLexer
/ src / charttheme.cpp
|
r103 | #include "charttheme_p.h" | ||
#include "qchart.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" | ||
|
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" | ||
|
r194 | #include "scatterpresenter_p.h" | ||
|
r163 | #include "piepresenter.h" | ||
|
r401 | #include "splinepresenter_p.h" | ||
|
r103 | |||
|
r143 | //themes | ||
#include "chartthemevanilla_p.h" | ||||
#include "chartthemeicy_p.h" | ||||
#include "chartthemegrayscale_p.h" | ||||
#include "chartthemescientific_p.h" | ||||
|
r103 | |||
|
r125 | |||
|
r143 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
/* TODO | ||||
|
r103 | case QChart::ChartThemeUnnamed1: | ||
|
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)); | ||||
|
r103 | |||
m_gradientStartColor = QColor(QRgb(0xfff3dc9e)); | ||||
m_gradientEndColor = QColor(QRgb(0xffafafaf)); | ||||
|
r143 | */ | ||
|
r153 | ChartTheme::ChartTheme(QChart::ChartTheme id) | ||
|
r143 | { | ||
|
r153 | m_id = id; | ||
|
r143 | m_seriesColor.append(QRgb(0xff000000)); | ||
m_seriesColor.append(QRgb(0xff707070)); | ||||
m_gradientStartColor = QColor(QRgb(0xffffffff)); | ||||
m_gradientEndColor = QColor(QRgb(0xffafafaf)); | ||||
|
r324 | |||
qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | ||||
|
r143 | } | ||
|
r153 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) | ||
|
r143 | { | ||
switch(theme) { | ||||
case QChart::ChartThemeDefault: | ||||
|
r321 | return new ChartThemeIcy(); | ||
|
r143 | 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 | ||||
|
r360 | void ChartTheme::decorate(ChartItem* item, QSeries* series,int count) | ||
|
r143 | { | ||
switch(series->type()) | ||||
{ | ||||
|
r360 | case QSeries::SeriesTypeLine: { | ||
|
r349 | QLineSeries* s = static_cast<QLineSeries*>(series); | ||
|
r145 | LineChartItem* i = static_cast<LineChartItem*>(item); | ||
|
r143 | decorate(i,s,count); | ||
break; | ||||
} | ||||
|
r360 | case QSeries::SeriesTypeBar: { | ||
|
r338 | QBarSeries* b = static_cast<QBarSeries*>(series); | ||
|
r216 | BarPresenter* i = static_cast<BarPresenter*>(item); | ||
|
r143 | decorate(i,b,count); | ||
break; | ||||
} | ||||
|
r360 | case QSeries::SeriesTypeStackedBar: { | ||
|
r338 | QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series); | ||
|
r216 | StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item); | ||
|
r143 | decorate(i,s,count); | ||
break; | ||||
} | ||||
|
r360 | case QSeries::SeriesTypePercentBar: { | ||
|
r338 | QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series); | ||
|
r216 | PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item); | ||
|
r143 | decorate(i,s,count); | ||
break; | ||||
} | ||||
|
r360 | case QSeries::SeriesTypeScatter: { | ||
|
r195 | QScatterSeries* s = qobject_cast<QScatterSeries*>(series); | ||
Q_ASSERT(s); | ||||
ScatterPresenter* i = static_cast<ScatterPresenter*>(item); | ||||
Q_ASSERT(i); | ||||
decorate(i, s, count); | ||||
break; | ||||
} | ||||
|
r360 | case QSeries::SeriesTypePie: { | ||
|
r163 | QPieSeries* s = static_cast<QPieSeries*>(series); | ||
PiePresenter* i = static_cast<PiePresenter*>(item); | ||||
decorate(i,s,count); | ||||
break; | ||||
} | ||||
|
r143 | default: | ||
qDebug()<<"Wrong item to be decorated by theme"; | ||||
|
r103 | break; | ||
} | ||||
|
r143 | |||
|
r103 | } | ||
|
r349 | void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count) | ||
|
r103 | { | ||
|
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); | ||||
|
r103 | } | ||
|
r338 | void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count) | ||
|
r108 | { | ||
|
r357 | QList<QBarSet*> sets = series->barSets(); | ||
|
r366 | for (int i=0; i<series->barsetCount(); i++) { | ||
|
r357 | sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); | ||
|
r164 | } | ||
|
r108 | } | ||
|
r338 | void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count) | ||
|
r103 | { | ||
|
r357 | QList<QBarSet*> sets = series->barSets(); | ||
|
r366 | for (int i=0; i<series->barsetCount(); i++) { | ||
|
r357 | sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); | ||
|
r164 | } | ||
|
r103 | } | ||
|
r338 | void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count) | ||
|
r143 | { | ||
|
r357 | QList<QBarSet*> sets = series->barSets(); | ||
|
r366 | for (int i=0; i<series->barsetCount(); i++) { | ||
|
r357 | sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); | ||
|
r164 | } | ||
|
r143 | } | ||
|
r103 | |||
|
r182 | void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count) | ||
{ | ||||
Q_ASSERT(presenter); | ||||
Q_ASSERT(series); | ||||
|
r195 | QColor color = m_seriesColor.at(count % m_seriesColor.size()); | ||
// TODO: define alpha in the theme? or in the series? | ||||
|
r278 | //color.setAlpha(120); | ||
|
r195 | |||
QBrush brush(color, Qt::SolidPattern); | ||||
presenter->m_markerBrush = brush; | ||||
|
r182 | |||
|
r278 | QPen pen(brush, 3); | ||
|
r195 | pen.setColor(color); | ||
presenter->m_markerPen = pen; | ||||
|
r182 | } | ||
|
r163 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) | ||
{ | ||||
|
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(); | ||||
|
r340 | // dont use black colors... looks bad | ||
if (c == Qt::black) | ||||
continue; | ||||
|
r324 | // by default use the "raw" theme color | ||
if (!colors.contains(c)) { | ||||
colors << c; | ||||
continue; | ||||
} | ||||
// ...ok we need to generate something that looks like the same color | ||||
// but different lightness | ||||
int tryCount = 0; | ||||
while (tryCount++ < 100) { | ||||
|
r166 | |||
|
r324 | // find maximum value we can raise the lightness | ||
int lMax = 255; | ||||
if (lMax > 255 - c.red()) | ||||
lMax = 255 - c.red(); | ||||
if (lMax > 255 - c.green()) | ||||
lMax = 255 - c.green(); | ||||
if (lMax > 255 - c.blue()) | ||||
lMax = 255 - c.blue(); | ||||
|
r166 | |||
|
r324 | // find maximum value we can make it darker | ||
int dMax = 255; | ||||
if (dMax > c.red()) | ||||
dMax = c.red(); | ||||
if (dMax > c.green()) | ||||
dMax = c.green(); | ||||
if (dMax > c.blue()) | ||||
dMax = c.blue(); | ||||
int max = dMax + lMax; | ||||
if (max == 0) { | ||||
// no room to make color lighter or darker... | ||||
qDebug() << "cannot generate a color for pie!"; | ||||
break; | ||||
} | ||||
// generate random color | ||||
int r = c.red() - dMax; | ||||
int g = c.green() - dMax; | ||||
int b = c.blue() - dMax; | ||||
int d = qrand() % max; | ||||
c.setRgb(r+d, g+d, b+d); | ||||
// found a unique color? | ||||
if (!colors.contains(c)) | ||||
break; | ||||
|
r166 | } | ||
|
r324 | qDebug() << "generated a color for pie" << c; | ||
colors << c; | ||||
|
r166 | } | ||
// finally update colors | ||||
|
r203 | foreach (QPieSlice* s, series->slices()) { | ||
|
r324 | QColor c = colors.takeFirst(); | ||
s->setPen(c); | ||||
s->setBrush(c); | ||||
|
r163 | } | ||
} | ||||
|
r103 | |||
|
r223 | void ChartTheme::decorate(QChartAxis* axis,AxisItem* item) | ||
|
r176 | { | ||
//TODO: dummy defults for now | ||||
|
r223 | axis->setLabelsBrush(Qt::black); | ||
axis->setLabelsPen(Qt::NoPen); | ||||
axis->setShadesPen(Qt::NoPen); | ||||
axis->setShadesOpacity(0.5); | ||||
|
r176 | } | ||
|
r401 | void ChartTheme::decorate(SplinePresenter* presenter, QSplineSeries* series, int count) | ||
{ | ||||
Q_ASSERT(presenter); | ||||
Q_ASSERT(series); | ||||
// 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; | ||||
// QPen pen(brush, 3); | ||||
// pen.setColor(color); | ||||
// presenter->m_markerPen = pen; | ||||
} | ||||
|
r103 | QTCOMMERCIALCHART_END_NAMESPACE | ||