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

0001 /*
0002     SPDX-FileCopyrightText: 2010-2016 Sune Vuorela <sune@vuorela.dk>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "abstractbarcode_p.h"
0008 
0009 #include <QColor>
0010 #include <QVariant>
0011 
0012 using namespace Prison;
0013 
0014 AbstractBarcodePrivate::AbstractBarcodePrivate(Barcode::Dimensions dim)
0015     : m_dimension(dim)
0016 {
0017 }
0018 
0019 AbstractBarcodePrivate::~AbstractBarcodePrivate() = default;
0020 
0021 bool AbstractBarcodePrivate::sizeTooSmall(const QSizeF &size) const
0022 {
0023     return m_cache.width() > size.width() || m_cache.height() > size.height();
0024 }
0025 
0026 bool AbstractBarcodePrivate::isEmpty() const
0027 {
0028     switch (m_data.userType()) {
0029     case QMetaType::QString:
0030         return m_data.toString().isEmpty();
0031     case QMetaType::QByteArray:
0032         return m_data.toByteArray().isEmpty();
0033     default:
0034         break;
0035     }
0036     return true;
0037 }
0038 
0039 void AbstractBarcodePrivate::recompute()
0040 {
0041     if (m_cache.isNull() && !isEmpty()) {
0042         m_cache = paintImage();
0043     }
0044 }
0045 
0046 QSizeF AbstractBarcodePrivate::preferredSize(qreal devicePixelRatio) const
0047 {
0048     switch (m_dimension) {
0049     case Barcode::NoDimensions:
0050         return {};
0051     case Barcode::OneDimension:
0052         return QSizeF(m_cache.width() * (devicePixelRatio < 2 ? 2 : 1), std::max(m_cache.height(), 50));
0053     case Barcode::TwoDimensions:
0054         return m_cache.size() * (devicePixelRatio < 2 ? 4 : 2);
0055     }
0056     return {};
0057 }