File indexing completed on 2024-05-12 04:01:32

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 QJSValue BarcodeQuickItem::barcodeType() const
0038 {
0039     if (m_type) {
0040         return static_cast<BarcodeType>(m_type.value());
0041     }
0042     return QJSValue();
0043 }
0044 
0045 void BarcodeQuickItem::setBarcodeType(const QJSValue &type)
0046 {
0047     if (!type.isNumber()) {
0048         if (m_type) {
0049             m_type.reset();
0050         } else {
0051             return;
0052         }
0053 
0054     } else {
0055         auto enumType = static_cast<Prison::BarcodeType>(type.toInt());
0056         if (enumType == m_type) {
0057             return;
0058         }
0059         m_type = enumType;
0060     }
0061     Q_EMIT barcodeTypeChanged();
0062     m_barcode.reset();
0063     updateBarcode();
0064 }
0065 
0066 QColor BarcodeQuickItem::foregroundColor() const
0067 {
0068     return m_fgColor;
0069 }
0070 
0071 void BarcodeQuickItem::setForegroundColor(const QColor &color)
0072 {
0073     if (m_fgColor == color) {
0074         return;
0075     }
0076     m_fgColor = color;
0077     Q_EMIT foregroundColorChanged();
0078     updateBarcode();
0079 }
0080 
0081 QColor BarcodeQuickItem::backgroundColor() const
0082 {
0083     return m_bgColor;
0084 }
0085 
0086 void BarcodeQuickItem::setBackgroundColor(const QColor &color)
0087 {
0088     if (m_bgColor == color) {
0089         return;
0090     }
0091     m_bgColor = color;
0092     Q_EMIT backgroundColorChanged();
0093     updateBarcode();
0094 }
0095 
0096 BarcodeQuickItem::Dimensions Prison::BarcodeQuickItem::dimensions() const
0097 {
0098     if (m_barcode)
0099         return static_cast<BarcodeQuickItem::Dimensions>(m_barcode->dimensions());
0100     return BarcodeQuickItem::Dimensions::NoDimensions;
0101 }
0102 
0103 void BarcodeQuickItem::paint(QPainter *painter)
0104 {
0105     if (!m_barcode) {
0106         return;
0107     }
0108 
0109     const auto w_max = std::max(minimumWidth(), width());
0110     const auto h_max = std::max(minimumHeight(), height());
0111     const auto img = m_barcode->toImage(QSizeF(w_max, h_max));
0112     const auto x = (w_max - img.width()) / 2;
0113     const auto y = (h_max - img.height()) / 2;
0114     painter->setRenderHint(QPainter::SmoothPixmapTransform, false);
0115     painter->drawImage(QRectF(x, y, img.width(), img.height()), img, img.rect());
0116 }
0117 
0118 void BarcodeQuickItem::componentComplete()
0119 {
0120     QQuickPaintedItem::componentComplete();
0121     updateBarcode();
0122 }
0123 
0124 qreal BarcodeQuickItem::minimumHeight() const
0125 {
0126     return m_barcode ? m_barcode->minimumSize().height() : 0;
0127 }
0128 
0129 qreal BarcodeQuickItem::minimumWidth() const
0130 {
0131     return m_barcode ? m_barcode->minimumSize().width() : 0;
0132 }
0133 
0134 bool BarcodeQuickItem::isEmpty() const
0135 {
0136     switch (m_content.userType()) {
0137     case QMetaType::QString:
0138         return m_content.toString().isEmpty();
0139     case QMetaType::QByteArray:
0140         return m_content.toByteArray().isEmpty();
0141     default:
0142         break;
0143     }
0144     return true;
0145 }
0146 
0147 void BarcodeQuickItem::updateBarcode()
0148 {
0149     if (!isComponentComplete()) {
0150         return;
0151     }
0152 
0153     if (isEmpty() || !m_type) {
0154         m_barcode.reset();
0155         update();
0156         Q_EMIT dimensionsChanged();
0157         return;
0158     }
0159     if (!m_barcode) {
0160         m_barcode = Prison::Barcode::create(m_type.value());
0161     }
0162     if (!m_barcode) {
0163         return;
0164     }
0165 
0166         if (m_content.userType() == QMetaType::QString) {
0167             m_barcode->setData(m_content.toString());
0168         } else {
0169             m_barcode->setData(m_content.toByteArray());
0170         }
0171         m_barcode->setForegroundColor(m_fgColor);
0172         m_barcode->setBackgroundColor(m_bgColor);
0173         const auto size = m_barcode->preferredSize(QGuiApplication::primaryScreen()->devicePixelRatio());
0174         setImplicitSize(size.width(), size.height());
0175 
0176         update();
0177         Q_EMIT dimensionsChanged();
0178 }
0179 
0180 #include "moc_barcodequickitem.cpp"