File indexing completed on 2024-05-12 15:49:09

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "barcodequickitem.h"
0008 
0009 #include <QGuiApplication>
0010 #include <QPainter>
0011 #include <QScreen>
0012 
0013 using namespace Prison;
0014 
0015 BarcodeQuickItem::BarcodeQuickItem(QQuickItem *parent)
0016     : QQuickPaintedItem(parent)
0017 {
0018 }
0019 
0020 BarcodeQuickItem::~BarcodeQuickItem() = default;
0021 
0022 QVariant BarcodeQuickItem::content() const
0023 {
0024     return m_content;
0025 }
0026 
0027 void BarcodeQuickItem::setContent(const QVariant &content)
0028 {
0029     if (m_content == content) {
0030         return;
0031     }
0032     m_content = content;
0033     Q_EMIT contentChanged();
0034     updateBarcode();
0035 }
0036 
0037 BarcodeQuickItem::BarcodeType BarcodeQuickItem::barcodeType() const
0038 {
0039     return static_cast<BarcodeType>(m_type);
0040 }
0041 
0042 void BarcodeQuickItem::setBarcodeType(BarcodeQuickItem::BarcodeType type)
0043 {
0044     if (m_type == static_cast<Prison::BarcodeType>(type)) {
0045         return;
0046     }
0047     m_type = static_cast<Prison::BarcodeType>(type);
0048     Q_EMIT barcodeTypeChanged();
0049     m_barcode.reset();
0050     updateBarcode();
0051 }
0052 
0053 QColor BarcodeQuickItem::foregroundColor() const
0054 {
0055     return m_fgColor;
0056 }
0057 
0058 void BarcodeQuickItem::setForegroundColor(const QColor &color)
0059 {
0060     if (m_fgColor == color) {
0061         return;
0062     }
0063     m_fgColor = color;
0064     Q_EMIT foregroundColorChanged();
0065     updateBarcode();
0066 }
0067 
0068 QColor BarcodeQuickItem::backgroundColor() const
0069 {
0070     return m_bgColor;
0071 }
0072 
0073 void BarcodeQuickItem::setBackgroundColor(const QColor &color)
0074 {
0075     if (m_bgColor == color) {
0076         return;
0077     }
0078     m_bgColor = color;
0079     Q_EMIT backgroundColorChanged();
0080     updateBarcode();
0081 }
0082 
0083 BarcodeQuickItem::Dimensions Prison::BarcodeQuickItem::dimensions() const
0084 {
0085     return m_barcode ? static_cast<BarcodeQuickItem::Dimensions>(m_barcode->dimensions()) : NoDimensions;
0086 }
0087 
0088 void BarcodeQuickItem::paint(QPainter *painter)
0089 {
0090     if (!m_barcode) {
0091         return;
0092     }
0093 
0094     const auto w_max = std::max(minimumWidth(), width());
0095     const auto h_max = std::max(minimumHeight(), height());
0096     const auto img = m_barcode->toImage(QSizeF(w_max, h_max));
0097     const auto x = (w_max - img.width()) / 2;
0098     const auto y = (h_max - img.height()) / 2;
0099     painter->setRenderHint(QPainter::SmoothPixmapTransform, false);
0100     painter->drawImage(QRectF(x, y, img.width(), img.height()), img, img.rect());
0101 }
0102 
0103 void BarcodeQuickItem::componentComplete()
0104 {
0105     QQuickPaintedItem::componentComplete();
0106     updateBarcode();
0107 }
0108 
0109 qreal BarcodeQuickItem::minimumHeight() const
0110 {
0111     return m_barcode ? m_barcode->trueMinimumSize().height() : 0.0;
0112 }
0113 
0114 qreal BarcodeQuickItem::minimumWidth() const
0115 {
0116     return m_barcode ? m_barcode->trueMinimumSize().width() : 0.0;
0117 }
0118 
0119 bool BarcodeQuickItem::isEmpty() const
0120 {
0121     switch (m_content.type()) {
0122     case QVariant::String:
0123         return m_content.toString().isEmpty();
0124     case QVariant::ByteArray:
0125         return m_content.toByteArray().isEmpty();
0126     default:
0127         break;
0128     }
0129     return true;
0130 }
0131 
0132 void BarcodeQuickItem::updateBarcode()
0133 {
0134     if (!isComponentComplete()) {
0135         return;
0136     }
0137 
0138     QString content;
0139     if (m_content.type() == QVariant::String) {
0140         content = m_content.toString();
0141     }
0142     if (m_content.type() == QVariant::ByteArray) {
0143         const auto b = m_content.toByteArray();
0144         content = QString::fromLatin1(b.constData(), b.size()); // ### fix this once Prison::Barcode can consume QByteArrays
0145     }
0146 
0147     if (m_type == Prison::Null || isEmpty()) {
0148         m_barcode.reset();
0149         update();
0150         Q_EMIT dimensionsChanged();
0151         return;
0152     }
0153 
0154     if (!m_barcode) {
0155         m_barcode.reset(Prison::createBarcode(m_type));
0156     }
0157     if (m_barcode) {
0158         if (m_content.type() == QVariant::String) {
0159             m_barcode->setData(m_content.toString());
0160         } else {
0161             m_barcode->setData(m_content.toByteArray());
0162         }
0163         m_barcode->setForegroundColor(m_fgColor);
0164         m_barcode->setBackgroundColor(m_bgColor);
0165         const auto size = m_barcode->preferredSize(QGuiApplication::primaryScreen()->devicePixelRatio());
0166         setImplicitSize(size.width(), size.height());
0167     }
0168 
0169     update();
0170     Q_EMIT dimensionsChanged();
0171 }
0172 
0173 #include "moc_barcodequickitem.cpp"