##// END OF EJS Templates
Adaptive layout to legend. Tries to fit all items inside given maximum size
Adaptive layout to legend. Tries to fit all items inside given maximum size

File last commit:

r615:91322882c9fd
r626:b05202e4f2ef
Show More
charttheme.cpp
302 lines | 9.4 KiB | text/x-c | CppLexer
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103 #include "charttheme_p.h"
#include "qchart.h"
Tero Ahola
New theme with light colors, chartview background
r584 #include "qchartview.h"
sauimone
background to legend, theme applies
r540 #include "qlegend.h"
Michal Klocek
Adds more axis handling...
r176 #include "qchartaxis.h"
Jani Honkonen
Fix color generation for pie
r324 #include <QTime>
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103
Michal Klocek
Refactor themes...
r143 //series
sauimone
Added pen & brush to QBarSet
r214 #include "qbarset.h"
sauimone
Naming convention change for barcharts. QBarChartSeries is now QBarSeries etc.
r338 #include "qbarseries.h"
#include "qstackedbarseries.h"
#include "qpercentbarseries.h"
Michal Klocek
Rename QLineChartSeries to QLineSeries
r349 #include "qlineseries.h"
Michal Klocek
Adds area chart...
r421 #include "qareaseries.h"
Tero Ahola
Enabled theme colors in scatter again
r182 #include "qscatterseries.h"
Jani Honkonen
Make pie work better with chartwidgettest
r163 #include "qpieseries.h"
Jani Honkonen
Refactor pie (again). QPieSlice's now emit signals and no id's anymore. Just pointers in the interface.
r203 #include "qpieslice.h"
Marek Rosa
Spline working somewhat
r401 #include "qsplineseries.h"
Jani Honkonen
Make pie work better with chartwidgettest
r163
Michal Klocek
Refactor themes...
r143 //items
#include "axisitem_p.h"
sauimone
added _p to private class headers
r381 #include "barpresenter_p.h"
#include "stackedbarpresenter_p.h"
#include "percentbarpresenter_p.h"
Michal Klocek
Fix previous broken commit
r145 #include "linechartitem_p.h"
Michal Klocek
Adds area chart...
r421 #include "areachartitem_p.h"
Michal Klocek
Refactor scatter chart to fit the other classes...
r470 #include "scatterchartitem_p.h"
Jani Honkonen
Rename piepresenter -> piechartitem
r568 #include "piechartitem_p.h"
Marek Rosa
Renamed SplinePresenter to SplineChartItem
r460 #include "splinechartitem_p.h"
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103
Michal Klocek
Refactor themes...
r143 //themes
Jani Honkonen
Adding list of series gradients to theme.
r494 #include "chartthemedefault_p.h"
Michal Klocek
Refactor themes...
r143 #include "chartthemevanilla_p.h"
#include "chartthemeicy_p.h"
#include "chartthemegrayscale_p.h"
#include "chartthemescientific_p.h"
Tero Ahola
Added draft of dark blue theme
r581 #include "chartthemebluecerulean_p.h"
Tero Ahola
New theme with light colors, chartview background
r584 #include "chartthemelight_p.h"
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103
Tero Ahola
tuning theme colors
r125
Michal Klocek
Refactor themes...
r143 QTCOMMERCIALCHART_BEGIN_NAMESPACE
Tero Ahola
Fonts and background of the themes...
r614 ChartTheme::ChartTheme(QChart::ChartTheme id) :
m_masterFont(QFont()),
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),
m_gridLinePen(QPen(QRgb(0x000000)))
Michal Klocek
Refactor themes...
r143 {
Michal Klocek
Adds missing ids to theme classes
r153 m_id = id;
Jani Honkonen
Fix color generation for pie
r324 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
Michal Klocek
Refactor themes...
r143 }
Michal Klocek
Adds missing ids to theme classes
r153 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
Michal Klocek
Refactor themes...
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();
Tero Ahola
Added draft of dark blue theme
r581 case QChart::ChartThemeBlueCerulean:
return new ChartThemeBlueCerulean();
Tero Ahola
New theme with light colors, chartview background
r584 case QChart::ChartThemeLight:
return new ChartThemeLight();
Jani Honkonen
Adding list of series gradients to theme.
r494 default:
return new ChartThemeDefault();
Michal Klocek
Refactor themes...
r143 }
}
void ChartTheme::decorate(QChart* chart)
{
Tero Ahola
New theme with light colors, chartview background
r584 if (m_backgroundShades == BackgroundShadesNone) {
chart->setChartBackgroundBrush(m_chartBackgroundGradient);
} else {
Tero Ahola
Added draft of dark blue theme
r581 chart->setChartBackgroundBrush(Qt::NoBrush);
Tero Ahola
New theme with light colors, chartview background
r584 }
Tero Ahola
Commented out legend temporarily (crashes for some reason)
r549 chart->setChartTitleFont(m_masterFont);
Tero Ahola
Fonts and background of the themes...
r614 chart->setChartTitleBrush(m_titleBrush);
Michal Klocek
Refactor themes...
r143 }
sauimone
background to legend, theme applies
r540 void ChartTheme::decorate(QLegend* legend)
{
Tero Ahola
New theme with light colors, chartview background
r584 legend->setBackgroundBrush(m_chartBackgroundGradient);
sauimone
background to legend, theme applies
r540 }
Michal Klocek
Fix theme decoration calls on all xyseries
r562 void ChartTheme::decorate(QAreaSeries* series, int index)
Michal Klocek
Adds area chart...
r421 {
Tero Ahola
Theme change now affects also XY series
r615 QPen pen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
pen.setWidthF(2);
series->setPen(pen);
Michal Klocek
Adds area chart...
r421
Tero Ahola
Theme change now affects also XY series
r615 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
series->setBrush(brush);
Michal Klocek
Adds area chart...
r421 }
Michal Klocek
Fix theme decoration calls on all xyseries
r562 void ChartTheme::decorate(QLineSeries* series,int index)
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103 {
Tero Ahola
Theme change now affects also XY series
r615 QPen pen(m_seriesColors.at(index%m_seriesColors.size()));
pen.setWidthF(2);
series->setPen(pen);
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103 }
Tero Ahola
Squashed bunch of warnings
r611 void ChartTheme::decorate(QBarSeries* series,int index)
Tero Ahola
One more alternative for changing themes
r108 {
sauimone
review fix: Removed iterator from barseries. Remove const from brush and pen. Renamed setters for tooltip, floating values and separators
r357 QList<QBarSet*> sets = series->barSets();
sauimone
Using new theme in barcharts
r509 for (int i=0; i<sets.count(); i++) {
Tero Ahola
Modified scientific theme
r523 qreal pos = 0.5;
if (sets.count() > 1)
pos = (qreal) i / (qreal) (sets.count() - 1);
Tero Ahola
Fixed bug with changing theme when several series exist
r538 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
sauimone
Using new theme in barcharts
r509 sets.at(i)->setBrush(QBrush(c));
sauimone
better use of gradients in barcharts
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) {
Tero Ahola
Fixed bug with changing theme when several series exist
r538 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
sauimone
better use of gradients in barcharts
r512 } else {
Tero Ahola
Fixed bug with changing theme when several series exist
r538 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
sauimone
better use of gradients in barcharts
r512 }
sets.at(i)->setFloatingValuePen(QPen(c));
sauimone
integrating bar charts to test app.. crashes for now
r164 }
Tero Ahola
One more alternative for changing themes
r108 }
Tero Ahola
Squashed bunch of warnings
r611 void ChartTheme::decorate(QStackedBarSeries* series,int index)
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103 {
sauimone
review fix: Removed iterator from barseries. Remove const from brush and pen. Renamed setters for tooltip, floating values and separators
r357 QList<QBarSet*> sets = series->barSets();
sauimone
Using new theme in barcharts
r509 for (int i=0; i<sets.count(); i++) {
Tero Ahola
Modified scientific theme
r523 qreal pos = 0.5;
if (sets.count() > 1)
pos = (qreal) i / (qreal) (sets.count() - 1);
Tero Ahola
Fixed bug with changing theme when several series exist
r538 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
sauimone
Using new theme in barcharts
r509 sets.at(i)->setBrush(QBrush(c));
sauimone
better use of gradients in barcharts
r512
if (pos < 0.3) {
Tero Ahola
Fixed bug with changing theme when several series exist
r538 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
sauimone
better use of gradients in barcharts
r512 } else {
Tero Ahola
Fixed bug with changing theme when several series exist
r538 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
sauimone
better use of gradients in barcharts
r512 }
sets.at(i)->setFloatingValuePen(QPen(c));
sauimone
integrating bar charts to test app.. crashes for now
r164 }
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103 }
Tero Ahola
Squashed bunch of warnings
r611 void ChartTheme::decorate(QPercentBarSeries* series,int index)
Michal Klocek
Refactor themes...
r143 {
sauimone
review fix: Removed iterator from barseries. Remove const from brush and pen. Renamed setters for tooltip, floating values and separators
r357 QList<QBarSet*> sets = series->barSets();
sauimone
Using new theme in barcharts
r509 for (int i=0; i<sets.count(); i++) {
Tero Ahola
Modified scientific theme
r523 qreal pos = 0.5;
if (sets.count() > 1)
pos = (qreal) i / (qreal) (sets.count() - 1);
Tero Ahola
Fixed bug with changing theme when several series exist
r538 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
sauimone
Using new theme in barcharts
r509 sets.at(i)->setBrush(QBrush(c));
sauimone
better use of gradients in barcharts
r512
if (pos < 0.3) {
Tero Ahola
Fixed bug with changing theme when several series exist
r538 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
sauimone
better use of gradients in barcharts
r512 } else {
Tero Ahola
Fixed bug with changing theme when several series exist
r538 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
sauimone
better use of gradients in barcharts
r512 }
sets.at(i)->setFloatingValuePen(QPen(c));
sauimone
integrating bar charts to test app.. crashes for now
r164 }
Michal Klocek
Refactor themes...
r143 }
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103
Michal Klocek
Fix theme decoration calls on all xyseries
r562 void ChartTheme::decorate(QScatterSeries* series, int index)
Tero Ahola
Enabled theme colors in scatter again
r182 {
Tero Ahola
Theme change now affects also XY series
r615 QPen pen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
pen.setWidthF(2);
series->setPen(pen);
Tero Ahola
Enabled theme colors in scatter again
r182
Tero Ahola
Theme change now affects also XY series
r615 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
series->setBrush(brush);
Tero Ahola
Enabled theme colors in scatter again
r182 }
Tero Ahola
Squashed bunch of warnings
r611 void ChartTheme::decorate(QPieSeries* series, int index)
Jani Honkonen
Make pie work better with chartwidgettest
r163 {
Tero Ahola
Theme gradients now generated from a single base color
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();
Tero Ahola
Fixed bug with changing theme when several series exist
r538 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1);
Tero Ahola
Pie pen now uses gradient start color
r510 series->slices().at(i)->setSlicePen(penColor);
Tero Ahola
Fixed bug with changing theme when several series exist
r538 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
Tero Ahola
Pie pen now uses gradient start color
r510 series->slices().at(i)->setSliceBrush(brushColor);
Jani Honkonen
Make pie work better with chartwidgettest
r163 }
}
Michal Klocek
Fix theme decoration calls on all xyseries
r562 void ChartTheme::decorate(QSplineSeries* series, int index)
Marek Rosa
Spline working somewhat
r401 {
Tero Ahola
Theme change now affects also XY series
r615 QPen pen(m_seriesColors.at(index%m_seriesColors.size()));
pen.setWidthF(2);
series->setPen(pen);
Marek Rosa
Spline working somewhat
r401 }
Michal Klocek
Fix theme decoration calls on all xyseries
r562 void ChartTheme::decorate(QChartAxis* axis,bool axisX)
Tero Ahola
Added axis related modifiers to theme
r548 {
if (axis->isAxisVisible()) {
axis->setLabelsBrush(m_axisLabelBrush);
Tero Ahola
Fonts and background of the themes...
r614 axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
Tero Ahola
Added axis related modifiers to theme
r548 if (m_backgroundShades == BackgroundShadesBoth
Michal Klocek
Fix theme decoration calls on all xyseries
r562 || (m_backgroundShades == BackgroundShadesVertical && axisX)
|| (m_backgroundShades == BackgroundShadesHorizontal && !axisX)) {
Tero Ahola
Added axis related modifiers to theme
r548 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);
}
}
Tero Ahola
Theme gradients now generated from a single base color
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;
}
}
Jani Honkonen
Adding list of series gradients to theme.
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);
}
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103 QTCOMMERCIALCHART_END_NAMESPACE