statistics: make fonts themeable

Move the various font objects the the StatsTheme structure to enable
different font weights for different themes.

For the dark theme, switch to a bold font, because the thin white
font was barely visible.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-02-21 17:51:44 +01:00 committed by Dirk Hohndel
parent 5f91c69f9c
commit 144e81e8f8
13 changed files with 42 additions and 39 deletions

View file

@ -106,7 +106,7 @@ BarSeries::~BarSeries()
BarSeries::BarLabel::BarLabel(StatsView &view, const std::vector<QString> &labels, int bin_nr, int binCount) :
isOutside(false)
{
QFont f; // make configurable
const QFont &f = view.getCurrentTheme().labelFont;
item = view.createChartItem<ChartTextItem>(ChartZValue::SeriesLabels, f, labels, true);
//highlight(false, bin_nr, binCount);
}

View file

@ -95,7 +95,7 @@ public:
void setColor(const QColor &color); // Draw on transparent background
void setColor(const QColor &color, const QColor &background); // Fill rectangle with given background color
private:
QFont f;
const QFont &f;
double fontHeight;
bool center;
struct Item {

View file

@ -21,7 +21,7 @@ InformationBox::InformationBox(StatsView &v) :
void InformationBox::setText(const std::vector<QString> &text, QPointF pos)
{
QFontMetrics fm(font);
QFontMetrics fm(theme.informationBoxFont);
double fontHeight = fm.height();
std::vector<double> widths;
@ -37,6 +37,7 @@ void InformationBox::setText(const std::vector<QString> &text, QPointF pos)
ChartRectItem::resize(QSizeF(width, height));
painter->setFont(theme.informationBoxFont);
painter->setPen(QPen(theme.darkLabelColor)); // QPainter uses QPen to set text color!
double y = 2.0 * informationBorder;
for (size_t i = 0; i < widths.size(); ++i) {
@ -71,7 +72,7 @@ void InformationBox::setPos(QPointF pos)
// Try to stay within three-thirds of the chart height
int InformationBox::recommendedMaxLines() const
{
QFontMetrics fm(font);
QFontMetrics fm(theme.informationBoxFont);
int maxHeight = static_cast<int>(sceneSize().height());
return maxHeight * 2 / fm.height() / 3;
}

View file

@ -8,7 +8,6 @@
#include <vector>
#include <memory>
#include <QFont>
struct dive;
class StatsView;
@ -21,7 +20,6 @@ struct InformationBox : ChartRectItem {
int recommendedMaxLines() const;
private:
const StatsTheme &theme; // Set once in constructor.
QFont font; // For future specialization.
double width, height;
};

View file

@ -20,11 +20,10 @@ Legend::Legend(StatsView &view, const std::vector<QString> &names) :
QBrush(view.getCurrentTheme().legendColor), legendBoxBorderRadius),
displayedItems(0), width(0.0), height(0.0),
theme(view.getCurrentTheme()),
font(QFont()), // Make configurable
posInitialized(false)
{
entries.reserve(names.size());
QFontMetrics fm(font);
QFontMetrics fm(theme.legendFont);
fontHeight = fm.height();
int idx = 0;
for (const QString &name: names)
@ -98,7 +97,7 @@ void Legend::resize()
// Paint labels
painter->setPen(theme.darkLabelColor); // QPainter uses pen not brush for text!
painter->setFont(font);
painter->setFont(theme.legendFont);
for (int i = 0; i < displayedItems; ++i) {
QPointF itemPos = entries[i].pos;
itemPos.rx() += fontHeight + 2.0 * legendBoxBorderSize;

View file

@ -7,7 +7,6 @@
#include <memory>
#include <vector>
#include <QFont>
class QFontMetrics;
class StatsTheme;
@ -30,7 +29,6 @@ private:
double width;
double height;
const StatsTheme &theme; // Set once in constructor.
QFont font;
// The position is specified with respect to the center and in relative terms
// with respect to the canvas.
QPointF centerPos;

View file

@ -23,7 +23,7 @@ PieSeries::Item::Item(StatsView &view, const QString &name, int from, std::vecto
dives(std::move(divesIn)),
selected(allDivesSelected(dives))
{
QFont f; // make configurable
const QFont &f = theme.labelFont;
QLocale loc;
int count = (int)dives.size();

View file

@ -31,11 +31,7 @@ StatsAxis::StatsAxis(StatsView &view, const QString &title, bool horizontal, boo
title(title), horizontal(horizontal), labelsBetweenTicks(labelsBetweenTicks),
size(1.0), zeroOnScreen(0.0), min(0.0), max(1.0), labelWidth(0.0)
{
// use a Light version of the application font for both labels and title
labelFont = QFont();
labelFont.setWeight(QFont::Light);
titleFont = labelFont;
QFontMetrics fm(titleFont);
QFontMetrics fm(theme.axisTitleFont);
titleWidth = title.isEmpty() ? 0.0
: static_cast<double>(fm.size(Qt::TextSingleLine, title).width());
}
@ -60,7 +56,7 @@ std::pair<double, double> StatsAxis::horizontalOverhang() const
// If the labels are between ticks, they cannot peak out
if (!horizontal || labelsBetweenTicks)
return { 0.0, 0.0 };
QFontMetrics fm(labelFont);
QFontMetrics fm(theme.axisLabelFont);
auto [firstLabel, lastLabel] = getFirstLastLabel();
return { fm.size(Qt::TextSingleLine, firstLabel).width() / 2.0,
fm.size(Qt::TextSingleLine, lastLabel).width() / 2.0 };
@ -79,7 +75,7 @@ void StatsAxis::setRange(double minIn, double maxIn)
// margins.
int StatsAxis::guessNumTicks(const std::vector<QString> &strings) const
{
QFontMetrics fm(labelFont);
QFontMetrics fm(theme.axisLabelFont);
int minSize = fm.height();
for (const QString &s: strings) {
QSize labelSize = fm.size(Qt::TextSingleLine, s);
@ -101,8 +97,8 @@ double StatsAxis::titleSpace() const
{
if (title.isEmpty())
return 0.0;
return horizontal ? QFontMetrics(titleFont).height() + axisTitleSpaceHorizontal
: QFontMetrics(titleFont).height() + axisTitleSpaceVertical;
return horizontal ? QFontMetrics(theme.axisTitleFont).height() + axisTitleSpaceHorizontal
: QFontMetrics(theme.axisTitleFont).height() + axisTitleSpaceVertical;
}
double StatsAxis::width() const
@ -117,7 +113,7 @@ double StatsAxis::height() const
{
if (!horizontal)
return 0.0; // Only supported for horizontal axes
return QFontMetrics(labelFont).height() + axisLabelSpaceHorizontal +
return QFontMetrics(theme.axisLabelFont).height() + axisLabelSpaceHorizontal +
titleSpace() +
(labelsBetweenTicks ? 0.0 : axisTickSizeHorizontal);
}
@ -173,7 +169,7 @@ void StatsAxis::setSize(double sizeIn)
labelWidth = label.width;
}
QFontMetrics fm(labelFont);
QFontMetrics fm(theme.axisLabelFont);
int fontHeight = fm.height();
if (horizontal) {
double pixmapWidth = size;
@ -192,7 +188,7 @@ void StatsAxis::setSize(double sizeIn)
img->fill(Qt::transparent);
painter->setPen(QPen(theme.darkLabelColor));
painter->setFont(labelFont);
painter->setFont(theme.axisLabelFont);
for (const Label &label: labels) {
double x = (label.pos - min) / (max - min) * size + offset.x() - round(label.width / 2.0);
QRectF rect(x, 0.0, label.width, fontHeight);
@ -202,7 +198,7 @@ void StatsAxis::setSize(double sizeIn)
QRectF rect(offset.x() + round((size - titleWidth) / 2.0),
fontHeight + axisTitleSpaceHorizontal,
titleWidth, fontHeight);
painter->setFont(titleFont);
painter->setFont(theme.axisTitleFont);
painter->drawText(rect, title);
}
} else {
@ -221,7 +217,7 @@ void StatsAxis::setSize(double sizeIn)
img->fill(Qt::transparent);
painter->setPen(QPen(theme.darkLabelColor));
painter->setFont(labelFont);
painter->setFont(theme.axisLabelFont);
for (const Label &label: labels) {
double y = (min - label.pos) / (max - min) * size + offset.y() - round(fontHeight / 2.0);
QRectF rect(pixmapWidth - label.width, y, label.width, fontHeight);
@ -230,7 +226,7 @@ void StatsAxis::setSize(double sizeIn)
if (!title.isEmpty()) {
painter->rotate(-90.0);
QRectF rect(round(-(offsetY + titleWidth) / 2.0), 0.0, titleWidth, fontHeight);
painter->setFont(titleFont);
painter->setFont(theme.axisTitleFont);
painter->drawText(rect, title);
painter->resetTransform();
}
@ -309,7 +305,7 @@ void ValueAxis::updateLabels()
double act = actMin;
labels.reserve(num + 1);
ticks.reserve(num + 1);
QFontMetrics fm(labelFont);
QFontMetrics fm(theme.axisLabelFont);
for (int i = 0; i <= num; ++i) {
addLabel(fm, loc.toString(act, 'f', decimals), act);
addTick(act);
@ -369,7 +365,7 @@ void CountAxis::updateLabels()
labels.reserve(max + 1);
ticks.reserve(max + 1);
QFontMetrics fm(labelFont);
QFontMetrics fm(theme.axisLabelFont);
for (int i = 0; i <= max; i += step) {
addLabel(fm, loc.toString(i), static_cast<double>(i));
addTick(static_cast<double>(i));
@ -414,7 +410,7 @@ void CategoryAxis::updateLabels()
if (labelsText.empty())
return;
QFontMetrics fm(labelFont);
QFontMetrics fm(theme.axisLabelFont);
double size_per_label = size / static_cast<double>(labelsText.size()) - axisTickWidth;
double fontHeight = fm.height();
@ -511,7 +507,7 @@ void HistogramAxis::updateLabels()
if (first != 0)
addTick(bin_values.front().value);
int last = first;
QFontMetrics fm(labelFont);
QFontMetrics fm(theme.axisLabelFont);
for (int i = first; i < (int)bin_values.size(); i += step) {
const auto &[name, value, recommended] = bin_values[i];
addLabel(fm, name, value);

View file

@ -7,7 +7,6 @@
#include <memory>
#include <vector>
#include <QFont>
class StatsView;
class ChartLineItem;
@ -63,7 +62,6 @@ protected:
bool horizontal;
bool labelsBetweenTicks; // When labels are between ticks, they can be moved closer to the axis
QFont labelFont, titleFont;
double size; // width for horizontal, height for vertical
double zeroOnScreen;
QPointF offset; // Offset of the label and title pixmap with respect to the (0,0) position.

View file

@ -51,6 +51,11 @@ public:
medianMarkerColor = Qt::red;
selectionLassoColor = Qt::black;
selectionOverlayColor = Qt::lightGray;
// use a light version of the application font for axis labels, axis title and chart title
axisLabelFont.setWeight(QFont::Light);
axisTitleFont.setWeight(QFont::Light);
titleFont.setWeight(QFont::Light);
}
private:
QString name() const
@ -118,6 +123,11 @@ public:
medianMarkerColor = Qt::cyan;
selectionLassoColor = Qt::white;
selectionOverlayColor = Qt::darkGray;
// use a bold version of the application font for axis labels, axis title and chart title
axisLabelFont.setWeight(QFont::Bold);
axisTitleFont.setWeight(QFont::Bold);
titleFont.setWeight(QFont::Bold);
}
private:
QString name() const

View file

@ -5,6 +5,7 @@
#include <memory>
#include <QColor>
#include <QFont>
#include <QString>
class QSGTexture;
@ -39,6 +40,13 @@ public:
virtual QColor binColor(int bin, int numBins) const = 0;
virtual QColor labelColor(int bin, size_t numBins) const = 0;
QFont axisLabelFont;
QFont axisTitleFont;
QFont informationBoxFont;
QFont labelFont;
QFont legendFont;
QFont titleFont;
// These are cashes for the QSG rendering engine
// Note: Originally these were std::unique_ptrs, which automatically
// freed the textures on exit. However, destroying textures after

View file

@ -54,9 +54,6 @@ StatsView::StatsView(QQuickItem *parent) : QQuickItem(parent),
setAcceptHoverEvents(true);
setAcceptedMouseButtons(Qt::LeftButton);
QFont font;
titleFont = QFont(font.family(), font.pointSize(), QFont::Light); // Make configurable
}
StatsView::StatsView() : StatsView(nullptr)
@ -470,7 +467,7 @@ void StatsView::setTitle(const QString &s)
// Ooops. Currently we do not support setting the title twice.
return;
}
title = createChartItem<ChartTextItem>(ChartZValue::Legend, titleFont, s);
title = createChartItem<ChartTextItem>(ChartZValue::Legend, currentTheme->titleFont, s);
title->setColor(currentTheme->darkLabelColor);
}

View file

@ -6,7 +6,6 @@
#include "statshelper.h"
#include "statsselection.h"
#include <memory>
#include <QFont>
#include <QImage>
#include <QPainter>
#include <QQuickItem>
@ -144,7 +143,6 @@ private:
StatsState state;
const StatsTheme *currentTheme;
QFont titleFont;
std::vector<std::unique_ptr<StatsSeries>> series;
std::unique_ptr<StatsGrid> grid;
std::vector<ChartItemPtr<QuartileMarker>> quartileMarkers;