diff --git a/autotests/aztecbarcodetest.cpp b/autotests/aztecbarcodetest.cpp --- a/autotests/aztecbarcodetest.cpp +++ b/autotests/aztecbarcodetest.cpp @@ -391,6 +391,9 @@ QVERIFY(!barcode->minimumSize().isValid()); barcode->setData(QStringLiteral("UNIT TEST")); QCOMPARE(barcode->minimumSize(), QSize(60, 60)); + QCOMPARE(barcode->trueMinimumSize(), QSize(15, 15)); + QCOMPARE(barcode->preferredSize(1), QSize(60, 60)); + QCOMPARE(barcode->preferredSize(2), QSize(30, 30)); QCOMPARE(barcode->toImage(barcode->minimumSize()).size(), QSize(60, 60)); QCOMPARE(barcode->toImage({1, 1}).isNull(), true); } diff --git a/autotests/code128barcodetest.cpp b/autotests/code128barcodetest.cpp --- a/autotests/code128barcodetest.cpp +++ b/autotests/code128barcodetest.cpp @@ -108,6 +108,9 @@ QVERIFY(!barcode->minimumSize().isValid()); barcode->setData(QStringLiteral("UNIT TEST")); QCOMPARE(barcode->minimumSize(), QSize(154, 10)); + QCOMPARE(barcode->trueMinimumSize(), QSize(154, 1)); + QCOMPARE(barcode->preferredSize(1), QSize(308, 50)); + QCOMPARE(barcode->preferredSize(2), QSize(154, 50)); QCOMPARE(barcode->toImage(barcode->minimumSize()).size(), QSize(154, 10)); QCOMPARE(barcode->toImage({1, 1}).isNull(), true); } diff --git a/src/lib/abstractbarcode.h b/src/lib/abstractbarcode.h --- a/src/lib/abstractbarcode.h +++ b/src/lib/abstractbarcode.h @@ -61,9 +61,34 @@ QImage toImage(const QSizeF& size) ; /** * The minimal size of this barcode. + * @note This isn't the absolute minimum, but closer to the result of preferredSize(1). * @return the minimal size for this barcode. + * @deprecated since 5.69 Prefer preferredSize() or trueMinimumSize(). */ QSizeF minimumSize() const; + + /** + * The minimal amount of pixels needed to represent this barcode without loss of information. + * That is, the size of the barcode image if each line or dot is just one pixel wide. + * On normal screens that is not enough for barcode scanners to reliably detect the barcode + * though. + * @see preferredSize + * @since 5.69 + */ + QSizeF trueMinimumSize() const; // TODO KF6: rename to minimumSize + + /** + * The recommended size for this barcode when shown on a screen. + * This is typically significantly larger than trueMinimumSize() so that + * barcode scanners tend to reliably detect the code. As this depends + * on the physical resolution of the output, you have to pass the device + * pixel ration of the output screen here. + * @param devicePixelRatio The device pixel ratio of the screen this is shown on. + * @see trueMinimumSize + * @since 5.69 + */ + QSizeF preferredSize(qreal devicePixelRatio) const; + /** * @return the foreground color (by default black) to be used for the barcode. */ diff --git a/src/lib/abstractbarcode.cpp b/src/lib/abstractbarcode.cpp --- a/src/lib/abstractbarcode.cpp +++ b/src/lib/abstractbarcode.cpp @@ -106,6 +106,26 @@ return d->m_cache.size(); } +QSizeF AbstractBarcode::trueMinimumSize() const +{ + d->recompute(); + return d->m_cache.size(); +} + +QSizeF AbstractBarcode::preferredSize(qreal devicePixelRatio) const +{ + d->recompute(); + switch (d->m_dimension) { + case NoDimensions: + return {}; + case OneDimension: + return QSizeF(d->m_cache.width() * (devicePixelRatio < 2 ? 2 : 1), std::max(d->m_cache.height(), 50)); + case TwoDimensions: + return d->m_cache.size() * (devicePixelRatio < 2 ? 4 : 2); + } + return {}; +} + void AbstractBarcode::setMinimumSize(const QSizeF& minimumSize) { Q_UNUSED(minimumSize); diff --git a/src/quick/barcodequickitem.cpp b/src/quick/barcodequickitem.cpp --- a/src/quick/barcodequickitem.cpp +++ b/src/quick/barcodequickitem.cpp @@ -9,7 +9,9 @@ #include #include +#include #include +#include using namespace Prison; @@ -120,9 +122,8 @@ m_barcode->setData(m_content); m_barcode->setForegroundColor(m_fgColor); m_barcode->setBackgroundColor(m_bgColor); - // minimumSize() is only providing valid results after the first call to toImage()... - m_barcode->toImage(m_barcode->minimumSize()); - setImplicitSize(m_barcode->minimumSize().width(), m_barcode->minimumSize().height()); + const auto size = m_barcode->preferredSize(QGuiApplication::primaryScreen()->devicePixelRatio()); + setImplicitSize(size.width(), size.height()); } update();