From 7dd9b65df0090493df42200eb35b8bd405532439 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Wed, 11 Aug 2021 10:57:42 +0200 Subject: [PATCH] profile: add width() function to DiveCartesian axis To properly layout the profile we need to know the expected space required by the vertical axes. In the general case, format the the text "999". For the partial-pressure-axis, use "0.99". Signed-off-by: Berthold Stoeger --- profile-widget/divecartesianaxis.cpp | 19 +++++++++++++++++++ profile-widget/divecartesianaxis.h | 3 +++ profile-widget/divetextitem.cpp | 19 ++++++++++++------- profile-widget/divetextitem.h | 2 ++ 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/profile-widget/divecartesianaxis.cpp b/profile-widget/divecartesianaxis.cpp index b84278770..2c78c62c5 100644 --- a/profile-widget/divecartesianaxis.cpp +++ b/profile-widget/divecartesianaxis.cpp @@ -8,6 +8,8 @@ #include "profile-widget/divelineitem.h" #include "profile-widget/profilescene.h" +static const double labelSpace = 2.0; // space between label and ticks + QPen DiveCartesianAxis::gridPen() const { QPen pen; @@ -117,6 +119,18 @@ void emptyList(QList &list, int steps, int speed) } } +double DiveCartesianAxis::textWidth(const QString &s) const +{ + QFont fnt = DiveTextItem::getFont(dpr, labelScale); + QFontMetrics fm(fnt); + return fm.size(Qt::TextSingleLine, s).width() + labelSpace * dpr; +} + +double DiveCartesianAxis::width() const +{ + return textWidth("999"); +} + void DiveCartesianAxis::updateTicks(int animSpeed, color_index_t color) { if (!changed && !printMode) @@ -401,3 +415,8 @@ void PartialGasPressureAxis::update(int animSpeed) setTickInterval(pp > 4 ? 0.5 : 0.25); updateTicks(animSpeed); } + +double PartialGasPressureAxis::width() const +{ + return textWidth(textForValue(0.99)); +} diff --git a/profile-widget/divecartesianaxis.h b/profile-widget/divecartesianaxis.h index c9cb22291..e131fe601 100644 --- a/profile-widget/divecartesianaxis.h +++ b/profile-widget/divecartesianaxis.h @@ -49,6 +49,7 @@ public: void setLineSize(qreal lineSize); void setLine(const QLineF& line); virtual void updateTicks(int animSpeed, color_index_t color = TIME_GRID); + double width() const; signals: void sizeChanged(); @@ -57,6 +58,7 @@ protected: ProfileScene &scene; virtual QString textForValue(double value) const; virtual QColor colorForValue(double value) const; + double textWidth(const QString &s) const; Orientation orientation; QList labels; QList lines; @@ -105,6 +107,7 @@ class PartialGasPressureAxis : public DiveCartesianAxis { public: PartialGasPressureAxis(const DivePlotDataModel &model, double dpr, bool printMode, ProfileScene &scene); void update(int animSpeed); + double width() const; private: const DivePlotDataModel &model; }; diff --git a/profile-widget/divetextitem.cpp b/profile-widget/divetextitem.cpp index e36728590..fd52f68ba 100644 --- a/profile-widget/divetextitem.cpp +++ b/profile-widget/divetextitem.cpp @@ -59,15 +59,11 @@ const QString &DiveTextItem::text() return internalText; } -void DiveTextItem::updateText() +QFont DiveTextItem::getFont(double dpr, double scale) { - double size; - if (internalText.isEmpty()) { - return; - } - QFont fnt(qApp->font()); - if ((size = fnt.pixelSize()) > 0) { + double size = fnt.pixelSize(); + if (size > 0) { // set in pixels - so the scale factor may not make a difference if it's too close to 1 size *= scale * dpr; fnt.setPixelSize(lrint(size)); @@ -76,6 +72,15 @@ void DiveTextItem::updateText() size *= scale * dpr; fnt.setPointSizeF(size); } + return fnt; +} + +void DiveTextItem::updateText() +{ + if (internalText.isEmpty()) + return; + + QFont fnt = getFont(dpr, scale); QFontMetrics fm(fnt); QPainterPath textPath; diff --git a/profile-widget/divetextitem.h b/profile-widget/divetextitem.h index c84750642..87815d0a6 100644 --- a/profile-widget/divetextitem.h +++ b/profile-widget/divetextitem.h @@ -3,6 +3,7 @@ #define DIVETEXTITEM_H #include +#include #include class QBrush; @@ -20,6 +21,7 @@ public: void setBrush(const QBrush &brush); const QString &text(); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + static QFont getFont(double dpr, double scale); private: void updateText();