diff --git a/src/quick/barcodequickitem.cpp b/src/quick/barcodequickitem.cpp index 1f60bd1..0d3494b 100644 --- a/src/quick/barcodequickitem.cpp +++ b/src/quick/barcodequickitem.cpp @@ -1,131 +1,141 @@ /* SPDX-FileCopyrightText: 2018 Volker Krause SPDX-License-Identifier: MIT */ #include "barcodequickitem.h" #include #include #include #include #include using namespace Prison; BarcodeQuickItem::BarcodeQuickItem(QQuickItem *parent) : QQuickPaintedItem(parent) { } BarcodeQuickItem::~BarcodeQuickItem() = default; QString BarcodeQuickItem::content() const { return m_content; } void BarcodeQuickItem::setContent(const QString& content) { if (m_content == content) return; m_content = content; emit contentChanged(); updateBarcode(); } BarcodeQuickItem::BarcodeType BarcodeQuickItem::barcodeType() const { return static_cast(m_type); } void BarcodeQuickItem::setBarcodeType(BarcodeQuickItem::BarcodeType type) { if (m_type == static_cast(type)) return; m_type = static_cast(type); emit barcodeTypeChanged(); m_barcode.reset(); updateBarcode(); } QColor BarcodeQuickItem::foregroundColor() const { return m_fgColor; } void BarcodeQuickItem::setForegroundColor(const QColor &color) { if (m_fgColor == color) return; m_fgColor = color; emit foregroundColorChanged(); updateBarcode(); } QColor BarcodeQuickItem::backgroundColor() const { return m_bgColor; } void BarcodeQuickItem::setBackgroundColor(const QColor &color) { if (m_bgColor == color) return; m_bgColor = color; emit backgroundColorChanged(); updateBarcode(); } BarcodeQuickItem::Dimensions Prison::BarcodeQuickItem::dimensions() const { return m_barcode ? static_cast(m_barcode->dimensions()) : NoDimensions; } void BarcodeQuickItem::paint(QPainter* painter) { if (!m_barcode) return; const auto w_max = std::max(implicitWidth(), width()); const auto h_max = std::max(implicitHeight(), height()); const auto img = m_barcode->toImage(QSizeF(w_max, h_max)); const auto x = (w_max - img.width()) / 2; const auto y = (h_max - img.height()) / 2; painter->setRenderHint(QPainter::SmoothPixmapTransform, false); painter->drawImage(QRectF(x, y, img.width(), img.height()), img, img.rect()); } void BarcodeQuickItem::componentComplete() { QQuickPaintedItem::componentComplete(); updateBarcode(); } +qreal BarcodeQuickItem::minimumHeight() const +{ + return m_barcode ? m_barcode->trueMinimumSize().height() : 0.0; +} + +qreal BarcodeQuickItem::minimumWidth() const +{ + return m_barcode ? m_barcode->trueMinimumSize().width() : 0.0; +} + void BarcodeQuickItem::updateBarcode() { if (!isComponentComplete()) return; if (m_type == Prison::Null || m_content.isEmpty()) { m_barcode.reset(); update(); emit dimensionsChanged(); return; } if (!m_barcode) m_barcode.reset(Prison::createBarcode(m_type)); if (m_barcode) { m_barcode->setData(m_content); m_barcode->setForegroundColor(m_fgColor); m_barcode->setBackgroundColor(m_bgColor); const auto size = m_barcode->preferredSize(QGuiApplication::primaryScreen()->devicePixelRatio()); setImplicitSize(size.width(), size.height()); } update(); emit dimensionsChanged(); } diff --git a/src/quick/barcodequickitem.h b/src/quick/barcodequickitem.h index f908aa2..6c14413 100644 --- a/src/quick/barcodequickitem.h +++ b/src/quick/barcodequickitem.h @@ -1,85 +1,98 @@ /* SPDX-FileCopyrightText: 2018 Volker Krause SPDX-License-Identifier: MIT */ #ifndef PRISON_BARCODEQUICKITEM_H #define PRISON_BARCODEQUICKITEM_H #include #include #include #include namespace Prison { class AbstractBarcode; class BarcodeQuickItem : public QQuickPaintedItem { Q_OBJECT Q_PROPERTY(QString content READ content WRITE setContent NOTIFY contentChanged) Q_PROPERTY(BarcodeType barcodeType READ barcodeType WRITE setBarcodeType NOTIFY barcodeTypeChanged) Q_PROPERTY(QColor foregroundColor READ foregroundColor WRITE setForegroundColor NOTIFY foregroundColorChanged) Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged) Q_PROPERTY(Dimensions dimensions READ dimensions NOTIFY dimensionsChanged) + /** + * @see Prison::AbstractBarcode::trueMinimumSize() + * @since 5.69 + */ + Q_PROPERTY(qreal minimumHeight READ minimumHeight NOTIFY implicitHeightChanged) + /** + * @see Prison::AbstractBarcode::trueMinimumSize() + * @since 5.69 + */ + Q_PROPERTY(qreal minimumWidth READ minimumWidth NOTIFY implicitWidthChanged) public: enum BarcodeType { Null = Prison::Null, QRCode = Prison::QRCode, DataMatrix = Prison::DataMatrix, Aztec = Prison::Aztec, Code39 = Prison::Code39, Code93 = Prison::Code93, Code128 = Prison::Code128 }; Q_ENUM(BarcodeType) explicit BarcodeQuickItem(QQuickItem *parent = nullptr); ~BarcodeQuickItem(); QString content() const; void setContent(const QString &data); Prison::BarcodeQuickItem::BarcodeType barcodeType() const; void setBarcodeType(Prison::BarcodeQuickItem::BarcodeType type); QColor foregroundColor() const; void setForegroundColor(const QColor &color); QColor backgroundColor() const; void setBackgroundColor(const QColor &color); enum Dimensions { NoDimensions, OneDimension, TwoDimensions }; Q_ENUM(Dimensions); Dimensions dimensions() const; void paint(QPainter *painter) override; void componentComplete() override; + qreal minimumHeight() const; + qreal minimumWidth() const; + Q_SIGNALS: void contentChanged(); void barcodeTypeChanged(); void foregroundColorChanged(); void backgroundColorChanged(); void dimensionsChanged(); private: void updateBarcode(); QString m_content; std::unique_ptr m_barcode; QColor m_fgColor = Qt::black; QColor m_bgColor = Qt::white; Prison::BarcodeType m_type = Prison::Null; }; } #endif // PRISON_BARCODEQUICKITEM_H diff --git a/tests/barcode.qml b/tests/barcode.qml index 6c90825..86371a6 100644 --- a/tests/barcode.qml +++ b/tests/barcode.qml @@ -1,51 +1,54 @@ /* SPDX-FileCopyrightText: 2018 Volker Krause SPDX-License-Identifier: MIT */ import QtQuick 2.0 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.0 import org.kde.prison 1.0 as Prison Rectangle { width: 640 height: 320 color: "lightsteelblue" ColumnLayout { anchors.fill: parent RowLayout { Layout.fillWidth: true TextField { id: contentEdit Layout.fillWidth: true text: "KF5::Prison - The KDE barcode generation framework." } ComboBox { id: typeCombobox model: [ "Null", "QRCode", "DataMatrix", "Aztec", "Code39", "Code93", "Code128" ] currentIndex: 3 } } Prison.Barcode { id: barcode Layout.fillWidth: true Layout.fillHeight: true content: contentEdit.text barcodeType: typeCombobox.currentIndex // foregroundColor: "red" // backgroundColor: "green" } RowLayout { Label { text: "1D: " + (barcode.dimensions == Prison.Barcode.OneDimension) } Label { text: "2D: " + (barcode.dimensions == 2) } + Label { + text: "Min size: " + barcode.minimumWidth + "x" + barcode.minimumHeight + } } } }