14.18 round 1: a starved PDF throws, an early zoom centres, inches round to even, tests tightened

This commit is contained in:
ZacharyZhang-NY committed 2026-10-11 09:38:52 -04:00
1 parent 9be01073df
commit cd31454815
13 files changed
+210 -15

No files matched your search

+17 -8
View File
@@ -51,8 +51,12 @@ void JPEGPreview::setImage(const QImage &image, QSize pixels, bool updating)
m_image = image;
m_updating = updating;
if (pixels != m_pixels) {
// Swift's onAppear: a zoom set before the first image centres.
const bool first = m_pixels.isEmpty();
m_pixels = pixels;
updateRanges();
if (first && m_zoom)
scrollTo(QPointF(0.5, 0.5));
}
viewport()->update();
}
@@ -82,20 +86,25 @@ void JPEGPreview::setZoom(std::optional<double> zoom)
}
m_zoom = zoom;
updateRanges();
if (m_zoom) {
const QSizeF size = shownSize(*m_zoom);
const QSizeF shown(viewport()->size());
const double x = std::clamp(fraction.x() * size.width() - shown.width() / 2, 0.0, std::max(0.0, size.width() - shown.width()));
const double y = std::clamp(fraction.y() * size.height() - shown.height() / 2, 0.0, std::max(0.0, size.height() - shown.height()));
horizontalScrollBar()->setValue(int(std::lround(x)));
verticalScrollBar()->setValue(int(std::lround(y)));
}
if (m_zoom)
scrollTo(fraction);
m_drag.reset();
showCursor(false);
viewport()->update();
m_zoomChanged();
}
// Puts `fraction` of the image in the view's middle.
void JPEGPreview::scrollTo(QPointF fraction)
{
const QSizeF size = shownSize(m_zoom.value());
const QSizeF shown(viewport()->size());
const double x = std::clamp(fraction.x() * size.width() - shown.width() / 2, 0.0, std::max(0.0, size.width() - shown.width()));
const double y = std::clamp(fraction.y() * size.height() - shown.height() / 2, 0.0, std::max(0.0, size.height() - shown.height()));
horizontalScrollBar()->setValue(int(std::lround(x)));
verticalScrollBar()->setValue(int(std::lround(y)));
}
// The image's size on screen at `zoom`, in points.
QSizeF JPEGPreview::shownSize(double zoom) const
{
+10 -2
View File
@@ -47,10 +47,18 @@ double startQuality()
return number && std::isfinite(saved) ? std::clamp(saved, 0.0, 1.0) : JPEGOptions().quality;
}
// Swift's 0...2 decimals, trailing zeros dropped.
// Swift's 0...2 decimals: its shortest digits, halves to even.
QString inches(double value)
{
QString shown = QLocale(QLocale::English, QLocale::UnitedStates).toString(value, 'f', 2);
const QString digits = QString::number(value, 'f', QLocale::FloatingPointShortest);
const qsizetype point = digits.indexOf(QLatin1Char('.'));
const QString fraction = point < 0 ? QString() : digits.mid(point + 1);
qint64 hundredths = digits.left(point < 0 ? digits.size() : point).toLongLong() * 100 + fraction.left(2).leftJustified(2, QLatin1Char('0')).toLongLong();
const QString rest = fraction.mid(2);
const int half = rest.isEmpty() ? -1 : QString(rest).compare(QStringLiteral("5").leftJustified(rest.size(), QLatin1Char('0')));
if (half > 0 || (half == 0 && hundredths % 2 == 1))
++hundredths;
QString shown = QLocale(QLocale::English, QLocale::UnitedStates).toString(hundredths / 100.0, 'f', 2);
while (shown.contains(QLatin1Char('.')) && (shown.endsWith(QLatin1Char('0')) || shown.endsWith(QLatin1Char('.'))))
shown.chop(1);
return shown;
+1
View File
@@ -54,6 +54,7 @@ protected:
private:
static QPixmap checkerboard(double ratio);
QSizeF shownSize(double zoom) const;
void scrollTo(QPointF fraction);
void updateRanges();
void showCursor(bool grabbing);