##// END OF EJS Templates
Add 5.7.0 changes file...
Add 5.7.0 changes file Change-Id: I72ac7ecf67786a2fe4491e848863367fdfd54c01 Reviewed-by: Tomi Korpipää <tomi.korpipaa@theqtcompany.com> Reviewed-by: Miikka Heikkinen <miikka.heikkinen@theqtcompany.com>

File last commit:

r2854:46147b040d06
r2881:7bc9c012efbb
Show More
chartthememanager.cpp
258 lines | 7.9 KiB | text/x-c | CppLexer
/ src / charts / chartthememanager.cpp
Miikka Heikkinen
Updated license...
r2854 /****************************************************************************
Michal Klocek
Refactors internals...
r2273 **
Miikka Heikkinen
Updated license...
r2854 ** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Michal Klocek
Refactors internals...
r2273 **
Miikka Heikkinen
Updated license...
r2854 ** This file is part of the Qt Charts module of the Qt Toolkit.
Michal Klocek
Refactors internals...
r2273 **
Miikka Heikkinen
Updated license...
r2854 ** $QT_BEGIN_LICENSE:GPL$
Titta Heikkala
Updated license headers...
r2845 ** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
Miikka Heikkinen
Updated license...
r2854 ** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
Michal Klocek
Refactors internals...
r2273 **
Titta Heikkala
Updated license headers...
r2845 ** $QT_END_LICENSE$
**
Miikka Heikkinen
Updated license...
r2854 ****************************************************************************/
Titta Heikkala
Fix include syntax...
r2714 #include <private/qabstractseries_p.h>
#include <private/qabstractaxis_p.h>
#include <QtCore/QTime>
Michal Klocek
Refactors internals...
r2273 //themes
Titta Heikkala
Fix include syntax...
r2714 #include <private/chartthemesystem_p.h>
#include <private/chartthemelight_p.h>
#include <private/chartthemebluecerulean_p.h>
#include <private/chartthemedark_p.h>
#include <private/chartthemebrownsand_p.h>
#include <private/chartthemebluencs_p.h>
#include <private/chartthemehighcontrast_p.h>
#include <private/chartthemeblueicy_p.h>
#include <private/chartthemeqt_p.h>
Michal Klocek
Refactors internals...
r2273
Titta Heikkala
Qt Charts project file structure change...
r2712 QT_CHARTS_BEGIN_NAMESPACE
Michal Klocek
Refactors internals...
r2273
ChartThemeManager::ChartThemeManager(QChart* chart) :
m_chart(chart)
{
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
}
void ChartThemeManager::setTheme(QChart::ChartTheme theme)
{
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 if (m_theme.isNull() || theme != m_theme->id()) {
Michal Klocek
Refactors internals...
r2273 switch (theme) {
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 case QChart::ChartThemeLight:
Michal Klocek
Refactors internals...
r2273 m_theme.reset(new ChartThemeLight());
break;
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 case QChart::ChartThemeBlueCerulean:
Michal Klocek
Refactors internals...
r2273 m_theme.reset(new ChartThemeBlueCerulean());
break;
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 case QChart::ChartThemeDark:
Michal Klocek
Refactors internals...
r2273 m_theme.reset(new ChartThemeDark());
break;
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 case QChart::ChartThemeBrownSand:
Michal Klocek
Refactors internals...
r2273 m_theme.reset(new ChartThemeBrownSand());
break;
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 case QChart::ChartThemeBlueNcs:
Michal Klocek
Refactors internals...
r2273 m_theme.reset(new ChartThemeBlueNcs());
break;
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 case QChart::ChartThemeHighContrast:
Michal Klocek
Refactors internals...
r2273 m_theme.reset(new ChartThemeHighContrast());
break;
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 case QChart::ChartThemeBlueIcy:
Michal Klocek
Refactors internals...
r2273 m_theme.reset(new ChartThemeBlueIcy());
break;
Titta Heikkala
Add new Qt theme template...
r2625 case QChart::ChartThemeQt:
m_theme.reset(new ChartThemeQt());
break;
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 default:
Michal Klocek
Refactors internals...
r2273 m_theme.reset(new ChartThemeSystem());
break;
}
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 if (!m_theme.isNull()) {
decorateChart(m_chart,m_theme.data());
decorateLegend(m_chart->legend(),m_theme.data());
foreach (QAbstractAxis* axis, m_axisList)
axis->d_ptr->initializeTheme(m_theme.data(), true);
foreach (QAbstractSeries* series, m_seriesMap.keys())
series->d_ptr->initializeTheme(m_seriesMap[series], m_theme.data(), true);
Michal Klocek
Refactors internals...
r2273 }
}
}
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 // decorateChart is only called when theme is forcibly initialized
void ChartThemeManager::decorateChart(QChart *chart, ChartTheme *theme) const
Michal Klocek
Refactors internals...
r2273 {
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 chart->setBackgroundBrush(theme->chartBackgroundGradient());
Michal Klocek
Refactors internals...
r2273
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 QPen pen(Qt::transparent);
QBrush brush;
chart->setPlotAreaBackgroundBrush(brush);
chart->setPlotAreaBackgroundPen(pen);
chart->setPlotAreaBackgroundVisible(false);
Miikka Heikkinen
Add API to specify plot area background....
r2498
Michal Klocek
Refactors internals...
r2273 chart->setTitleFont(theme->masterFont());
chart->setTitleBrush(theme->labelBrush());
chart->setDropShadowEnabled(theme->isBackgroundDropShadowEnabled());
}
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 // decorateLegend is only called when theme is forcibly initialized
void ChartThemeManager::decorateLegend(QLegend *legend, ChartTheme *theme) const
Michal Klocek
Refactors internals...
r2273 {
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 legend->setPen(theme->axisLinePen());
legend->setBrush(theme->chartBackgroundGradient());
legend->setFont(theme->labelFont());
legend->setLabelBrush(theme->labelBrush());
Michal Klocek
Refactors internals...
r2273 }
Miikka Heikkinen
Fix explicitly set default pen/brush/font getting overridden by theme...
r2516 int ChartThemeManager::createIndexKey(QList<int> keys) const
Michal Klocek
Refactors internals...
r2273 {
Titta Heikkala
Qt Charts project file structure change...
r2712 std::sort(keys.begin(), keys.end());
Michal Klocek
Refactors internals...
r2273
int key = 0;
QList<int>::iterator i;
i = keys.begin();
while (i != keys.end()) {
if (*i != key)
break;
key++;
i++;
}
return key;
}
int ChartThemeManager::seriesCount(QAbstractSeries::SeriesType type) const
{
int count = 0;
QList<QAbstractSeries *> series = m_seriesMap.keys();
foreach(QAbstractSeries *s, series) {
if (s->type() == type)
count++;
}
return count;
}
void ChartThemeManager::handleSeriesAdded(QAbstractSeries *series)
{
int key = createIndexKey(m_seriesMap.values());
m_seriesMap.insert(series,key);
series->d_ptr->initializeTheme(key,m_theme.data(),false);
}
void ChartThemeManager::handleSeriesRemoved(QAbstractSeries *series)
{
m_seriesMap.remove(series);
}
void ChartThemeManager::handleAxisAdded(QAbstractAxis *axis)
{
m_axisList.append(axis);
axis->d_ptr->initializeTheme(m_theme.data(),false);
}
void ChartThemeManager::handleAxisRemoved(QAbstractAxis *axis)
{
m_axisList.removeAll(axis);
}
void ChartThemeManager::updateSeries(QAbstractSeries *series)
{
if(m_seriesMap.contains(series)){
series->d_ptr->initializeTheme(m_seriesMap[series],m_theme.data(),false);
}
}
QList<QGradient> ChartThemeManager::generateSeriesGradients(const QList<QColor>& colors)
{
QList<QGradient> result;
// Generate gradients in HSV color space
foreach (const QColor &color, colors) {
QLinearGradient g;
qreal h = color.hsvHueF();
qreal s = color.hsvSaturationF();
QColor start = color;
start.setHsvF(h, 0.0, 1.0);
g.setColorAt(0.0, start);
g.setColorAt(0.5, color);
QColor end = color;
end.setHsvF(h, s, 0.25);
g.setColorAt(1.0, end);
result << g;
}
return result;
}
QColor ChartThemeManager::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 ChartThemeManager::colorAt(const QGradient &gradient, qreal pos)
{
Q_ASSERT(pos >= 0 && pos <= 1.0);
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);
}
#include "moc_chartthememanager_p.cpp"
Titta Heikkala
Qt Charts project file structure change...
r2712 QT_CHARTS_END_NAMESPACE