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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-08-11 10:57:42 +02:00 committed by Dirk Hohndel
parent f713858ba4
commit 7dd9b65df0
4 changed files with 36 additions and 7 deletions

View file

@ -8,6 +8,8 @@
#include "profile-widget/divelineitem.h" #include "profile-widget/divelineitem.h"
#include "profile-widget/profilescene.h" #include "profile-widget/profilescene.h"
static const double labelSpace = 2.0; // space between label and ticks
QPen DiveCartesianAxis::gridPen() const QPen DiveCartesianAxis::gridPen() const
{ {
QPen pen; QPen pen;
@ -117,6 +119,18 @@ void emptyList(QList<T *> &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) void DiveCartesianAxis::updateTicks(int animSpeed, color_index_t color)
{ {
if (!changed && !printMode) if (!changed && !printMode)
@ -401,3 +415,8 @@ void PartialGasPressureAxis::update(int animSpeed)
setTickInterval(pp > 4 ? 0.5 : 0.25); setTickInterval(pp > 4 ? 0.5 : 0.25);
updateTicks(animSpeed); updateTicks(animSpeed);
} }
double PartialGasPressureAxis::width() const
{
return textWidth(textForValue(0.99));
}

View file

@ -49,6 +49,7 @@ public:
void setLineSize(qreal lineSize); void setLineSize(qreal lineSize);
void setLine(const QLineF& line); void setLine(const QLineF& line);
virtual void updateTicks(int animSpeed, color_index_t color = TIME_GRID); virtual void updateTicks(int animSpeed, color_index_t color = TIME_GRID);
double width() const;
signals: signals:
void sizeChanged(); void sizeChanged();
@ -57,6 +58,7 @@ protected:
ProfileScene &scene; ProfileScene &scene;
virtual QString textForValue(double value) const; virtual QString textForValue(double value) const;
virtual QColor colorForValue(double value) const; virtual QColor colorForValue(double value) const;
double textWidth(const QString &s) const;
Orientation orientation; Orientation orientation;
QList<DiveTextItem *> labels; QList<DiveTextItem *> labels;
QList<DiveLineItem *> lines; QList<DiveLineItem *> lines;
@ -105,6 +107,7 @@ class PartialGasPressureAxis : public DiveCartesianAxis {
public: public:
PartialGasPressureAxis(const DivePlotDataModel &model, double dpr, bool printMode, ProfileScene &scene); PartialGasPressureAxis(const DivePlotDataModel &model, double dpr, bool printMode, ProfileScene &scene);
void update(int animSpeed); void update(int animSpeed);
double width() const;
private: private:
const DivePlotDataModel &model; const DivePlotDataModel &model;
}; };

View file

@ -59,15 +59,11 @@ const QString &DiveTextItem::text()
return internalText; return internalText;
} }
void DiveTextItem::updateText() QFont DiveTextItem::getFont(double dpr, double scale)
{ {
double size;
if (internalText.isEmpty()) {
return;
}
QFont fnt(qApp->font()); 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 // set in pixels - so the scale factor may not make a difference if it's too close to 1
size *= scale * dpr; size *= scale * dpr;
fnt.setPixelSize(lrint(size)); fnt.setPixelSize(lrint(size));
@ -76,6 +72,15 @@ void DiveTextItem::updateText()
size *= scale * dpr; size *= scale * dpr;
fnt.setPointSizeF(size); fnt.setPointSizeF(size);
} }
return fnt;
}
void DiveTextItem::updateText()
{
if (internalText.isEmpty())
return;
QFont fnt = getFont(dpr, scale);
QFontMetrics fm(fnt); QFontMetrics fm(fnt);
QPainterPath textPath; QPainterPath textPath;

View file

@ -3,6 +3,7 @@
#define DIVETEXTITEM_H #define DIVETEXTITEM_H
#include <QObject> #include <QObject>
#include <QFont>
#include <QGraphicsItemGroup> #include <QGraphicsItemGroup>
class QBrush; class QBrush;
@ -20,6 +21,7 @@ public:
void setBrush(const QBrush &brush); void setBrush(const QBrush &brush);
const QString &text(); const QString &text();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
static QFont getFont(double dpr, double scale);
private: private:
void updateText(); void updateText();