profile: improve calculation of the right border

The time axis might need some space and the average depth item
puts a formatted depth at to right of the profile. Consider
these when calculating the right border.

Since I found no way to turn of the average depth, this creates
a permanent border, which might or might not be a good thing.

Contains some refactoring of the label-size functions provided
by DiveTextItem.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-11-13 18:05:31 +01:00 committed by Dirk Hohndel
parent 6b33c3e721
commit 176a62f752
8 changed files with 50 additions and 43 deletions

View file

@ -17,6 +17,22 @@ DiveTextItem::DiveTextItem(double dpr, double scale, int alignFlags, QGraphicsIt
setFlag(ItemIgnoresTransformations);
}
static QFont getFont(double dpr, double scale)
{
QFont fnt(qApp->font());
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));
} else {
size = fnt.pointSizeF();
size *= scale * dpr;
fnt.setPointSizeF(size);
}
return fnt;
}
void DiveTextItem::set(const QString &t, const QBrush &b)
{
internalText = t;
@ -66,27 +82,6 @@ const QString &DiveTextItem::text()
return internalText;
}
QFont DiveTextItem::getFont(double dpr, double scale)
{
QFont fnt(qApp->font());
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));
} else {
size = fnt.pointSizeF();
size *= scale * dpr;
fnt.setPointSizeF(size);
}
return fnt;
}
double DiveTextItem::outlineSpace(double dpr)
{
return outlineSize * dpr;
}
double DiveTextItem::fontHeight(double dpr, double scale)
{
QFont fnt = getFont(dpr, scale);
@ -98,3 +93,14 @@ double DiveTextItem::height() const
{
return fontHeight(dpr, scale) + outlineSize * dpr;
}
std::pair<double, double> DiveTextItem::getLabelSize(double dpr, double scale, const QString &label)
{
QFont fnt = getFont(dpr, scale);
double outlineSpace = outlineSize * dpr;
QFontMetrics fm(fnt);
/* Round up, because non-integers tend to give abysmal rendering. */
double width = ceil(fm.size(Qt::TextSingleLine, label).width() + outlineSpace);
double height = ceil(fm.height() + outlineSpace);
return std::make_pair(width, height);
}