diff --git a/stats/barseries.cpp b/stats/barseries.cpp index 2a24c41a5..e34013b35 100644 --- a/stats/barseries.cpp +++ b/stats/barseries.cpp @@ -106,7 +106,7 @@ BarSeries::~BarSeries() BarSeries::BarLabel::BarLabel(StatsView &view, const std::vector &labels, int bin_nr, int binCount) : isOutside(false) { - QFont f; // make configurable + const QFont &f = view.getCurrentTheme().labelFont; item = view.createChartItem(ChartZValue::SeriesLabels, f, labels, true); //highlight(false, bin_nr, binCount); } diff --git a/stats/chartitem.h b/stats/chartitem.h index 797fba063..c2ba06bd2 100644 --- a/stats/chartitem.h +++ b/stats/chartitem.h @@ -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 { diff --git a/stats/informationbox.cpp b/stats/informationbox.cpp index f65208084..ee90c2de6 100644 --- a/stats/informationbox.cpp +++ b/stats/informationbox.cpp @@ -21,7 +21,7 @@ InformationBox::InformationBox(StatsView &v) : void InformationBox::setText(const std::vector &text, QPointF pos) { - QFontMetrics fm(font); + QFontMetrics fm(theme.informationBoxFont); double fontHeight = fm.height(); std::vector widths; @@ -37,6 +37,7 @@ void InformationBox::setText(const std::vector &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(sceneSize().height()); return maxHeight * 2 / fm.height() / 3; } diff --git a/stats/informationbox.h b/stats/informationbox.h index 330fbfd15..724a56626 100644 --- a/stats/informationbox.h +++ b/stats/informationbox.h @@ -8,7 +8,6 @@ #include #include -#include 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; }; diff --git a/stats/legend.cpp b/stats/legend.cpp index b3924264f..0bab2d19d 100644 --- a/stats/legend.cpp +++ b/stats/legend.cpp @@ -20,11 +20,10 @@ Legend::Legend(StatsView &view, const std::vector &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; diff --git a/stats/legend.h b/stats/legend.h index 2ba4edaae..cd43b09a9 100644 --- a/stats/legend.h +++ b/stats/legend.h @@ -7,7 +7,6 @@ #include #include -#include 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; diff --git a/stats/pieseries.cpp b/stats/pieseries.cpp index de40e56f8..3f246d6f6 100644 --- a/stats/pieseries.cpp +++ b/stats/pieseries.cpp @@ -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(); diff --git a/stats/statsaxis.cpp b/stats/statsaxis.cpp index 7f9f5a06d..c6017f1c5 100644 --- a/stats/statsaxis.cpp +++ b/stats/statsaxis.cpp @@ -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(fm.size(Qt::TextSingleLine, title).width()); } @@ -60,7 +56,7 @@ std::pair 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 &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(i)); addTick(static_cast(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(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); diff --git a/stats/statsaxis.h b/stats/statsaxis.h index 0cd6d668b..3a658a921 100644 --- a/stats/statsaxis.h +++ b/stats/statsaxis.h @@ -7,7 +7,6 @@ #include #include -#include 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. diff --git a/stats/statscolors.cpp b/stats/statscolors.cpp index f438437b7..23328da9b 100644 --- a/stats/statscolors.cpp +++ b/stats/statscolors.cpp @@ -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 diff --git a/stats/statscolors.h b/stats/statscolors.h index 2b8c09d73..eb45c27de 100644 --- a/stats/statscolors.h +++ b/stats/statscolors.h @@ -5,6 +5,7 @@ #include #include +#include #include 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 diff --git a/stats/statsview.cpp b/stats/statsview.cpp index 101746251..5cce63763 100644 --- a/stats/statsview.cpp +++ b/stats/statsview.cpp @@ -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(ChartZValue::Legend, titleFont, s); + title = createChartItem(ChartZValue::Legend, currentTheme->titleFont, s); title->setColor(currentTheme->darkLabelColor); } diff --git a/stats/statsview.h b/stats/statsview.h index 71d85c211..b85c55244 100644 --- a/stats/statsview.h +++ b/stats/statsview.h @@ -6,7 +6,6 @@ #include "statshelper.h" #include "statsselection.h" #include -#include #include #include #include @@ -144,7 +143,6 @@ private: StatsState state; const StatsTheme *currentTheme; - QFont titleFont; std::vector> series; std::unique_ptr grid; std::vector> quartileMarkers;