statistics: collect colors in a StatsTheme class

To enable rudimentary theming, collect all colors in a new
theme class. The class has to be passed down to the various
items.

In general the items save a reference to the them in the
constructor. Alternatively, they might also just query
the StatsView everytime they need to access a color.
For now, it's hard the say what is preferred: a reference
per item or a function call per invokation?

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-02-16 17:05:39 +01:00 committed by Dirk Hohndel
parent 56e02dbcc0
commit b5aac29cea
25 changed files with 272 additions and 166 deletions

View file

@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include "legend.h"
#include "statscolors.h"
#include "statsview.h"
#include "zvalues.h"
#include <cmath>
@ -15,8 +16,10 @@ static const double legendInternalBorderSize = 2.0;
Legend::Legend(StatsView &view, const std::vector<QString> &names) :
ChartRectItem(view, ChartZValue::Legend,
QPen(legendBorderColor, legendBorderSize), QBrush(legendColor), legendBoxBorderRadius),
QPen(view.getCurrentTheme().legendBorderColor, legendBorderSize),
QBrush(view.getCurrentTheme().legendColor), legendBoxBorderRadius),
displayedItems(0), width(0.0), height(0.0),
theme(view.getCurrentTheme()),
font(QFont()), // Make configurable
posInitialized(false)
{
@ -25,12 +28,12 @@ Legend::Legend(StatsView &view, const std::vector<QString> &names) :
fontHeight = fm.height();
int idx = 0;
for (const QString &name: names)
entries.emplace_back(name, idx++, (int)names.size(), fm);
entries.emplace_back(name, idx++, (int)names.size(), fm, theme);
}
Legend::Entry::Entry(const QString &name, int idx, int numBins, const QFontMetrics &fm) :
Legend::Entry::Entry(const QString &name, int idx, int numBins, const QFontMetrics &fm, const StatsTheme &theme) :
name(name),
rectBrush(QBrush(binColor(idx, numBins)))
rectBrush(QBrush(theme.binColor(idx, numBins)))
{
width = fm.height() + 2.0 * legendBoxBorderSize + fm.size(Qt::TextSingleLine, name).width();
}
@ -82,7 +85,7 @@ void Legend::resize()
ChartRectItem::resize(QSizeF(width, height));
// Paint rectangles
painter->setPen(QPen(legendBorderColor, legendBoxBorderSize));
painter->setPen(QPen(theme.legendBorderColor, legendBoxBorderSize));
for (int i = 0; i < displayedItems; ++i) {
QPointF itemPos = entries[i].pos;
painter->setBrush(entries[i].rectBrush);
@ -94,7 +97,7 @@ void Legend::resize()
}
// Paint labels
painter->setPen(darkLabelColor); // QPainter uses pen not brush for text!
painter->setPen(theme.darkLabelColor); // QPainter uses pen not brush for text!
painter->setFont(font);
for (int i = 0; i < displayedItems; ++i) {
QPointF itemPos = entries[i].pos;