Prison

barcodequickitem.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: MIT
5*/
6
7#include "barcodequickitem.h"
8
9#include <QGuiApplication>
10#include <QPainter>
11#include <QScreen>
12
13using namespace Prison;
14
15BarcodeQuickItem::BarcodeQuickItem(QQuickItem *parent)
16 : QQuickPaintedItem(parent)
17{
18}
19
20BarcodeQuickItem::~BarcodeQuickItem() = default;
21
22QVariant BarcodeQuickItem::content() const
23{
24 return m_content;
25}
26
27void BarcodeQuickItem::setContent(const QVariant &content)
28{
29 if (m_content == content) {
30 return;
31 }
32 m_content = content;
33 Q_EMIT contentChanged();
34 updateBarcode();
35}
36
37QJSValue BarcodeQuickItem::barcodeType() const
38{
39 if (m_type) {
40 return static_cast<BarcodeType>(m_type.value());
41 }
42 return QJSValue();
43}
44
45void BarcodeQuickItem::setBarcodeType(const QJSValue &type)
46{
47 if (!type.isNumber()) {
48 if (m_type) {
49 m_type.reset();
50 } else {
51 return;
52 }
53
54 } else {
55 auto enumType = static_cast<Prison::BarcodeType>(type.toInt());
56 if (enumType == m_type) {
57 return;
58 }
59 m_type = enumType;
60 }
61 Q_EMIT barcodeTypeChanged();
62 m_barcode.reset();
63 updateBarcode();
64}
65
66QColor BarcodeQuickItem::foregroundColor() const
67{
68 return m_fgColor;
69}
70
71void BarcodeQuickItem::setForegroundColor(const QColor &color)
72{
73 if (m_fgColor == color) {
74 return;
75 }
76 m_fgColor = color;
77 Q_EMIT foregroundColorChanged();
78 updateBarcode();
79}
80
81QColor BarcodeQuickItem::backgroundColor() const
82{
83 return m_bgColor;
84}
85
86void BarcodeQuickItem::setBackgroundColor(const QColor &color)
87{
88 if (m_bgColor == color) {
89 return;
90 }
91 m_bgColor = color;
92 Q_EMIT backgroundColorChanged();
93 updateBarcode();
94}
95
96BarcodeQuickItem::Dimensions Prison::BarcodeQuickItem::dimensions() const
97{
98 if (m_barcode)
99 return static_cast<BarcodeQuickItem::Dimensions>(m_barcode->dimensions());
100 return BarcodeQuickItem::Dimensions::NoDimensions;
101}
102
103void BarcodeQuickItem::paint(QPainter *painter)
104{
105 if (!m_barcode) {
106 return;
107 }
108
109 const auto w_max = std::max(minimumWidth(), width());
110 const auto h_max = std::max(minimumHeight(), height());
111 const auto img = m_barcode->toImage(QSizeF(w_max, h_max));
112 const auto x = (w_max - img.width()) / 2;
113 const auto y = (h_max - img.height()) / 2;
115 painter->drawImage(QRectF(x, y, img.width(), img.height()), img, img.rect());
116}
117
118void BarcodeQuickItem::componentComplete()
119{
121 updateBarcode();
122}
123
124qreal BarcodeQuickItem::minimumHeight() const
125{
126 return m_barcode ? m_barcode->minimumSize().height() : 0;
127}
128
129qreal BarcodeQuickItem::minimumWidth() const
130{
131 return m_barcode ? m_barcode->minimumSize().width() : 0;
132}
133
134bool BarcodeQuickItem::isEmpty() const
135{
136 switch (m_content.userType()) {
138 return m_content.toString().isEmpty();
140 return m_content.toByteArray().isEmpty();
141 default:
142 break;
143 }
144 return true;
145}
146
147void BarcodeQuickItem::updateBarcode()
148{
149 if (!isComponentComplete()) {
150 return;
151 }
152
153 if (isEmpty() || !m_type) {
154 m_barcode.reset();
155 update();
156 Q_EMIT dimensionsChanged();
157 return;
158 }
159 if (!m_barcode) {
160 m_barcode = Prison::Barcode::create(m_type.value());
161 }
162 if (!m_barcode) {
163 return;
164 }
165
166 if (m_content.userType() == QMetaType::QString) {
167 m_barcode->setData(m_content.toString());
168 } else {
169 m_barcode->setData(m_content.toByteArray());
170 }
171 m_barcode->setForegroundColor(m_fgColor);
172 m_barcode->setBackgroundColor(m_bgColor);
173 const auto size = m_barcode->preferredSize(QGuiApplication::primaryScreen()->devicePixelRatio());
174 setImplicitSize(size.width(), size.height());
175
176 update();
177 Q_EMIT dimensionsChanged();
178}
179
180#include "moc_barcodequickitem.cpp"
static std::optional< Prison::Barcode > create(Prison::BarcodeType type)
Create a new barcode generator.
Definition barcode.cpp:29
Type type(const QSqlDatabase &db)
Provides classes and methods for generating barcodes.
Definition barcode.h:24
BarcodeType
possible supported barcode types
Definition prison.h:22
bool isEmpty() const const
Q_EMITQ_EMIT
SmoothPixmapTransform
void drawImage(const QPoint &point, const QImage &image)
void setRenderHint(RenderHint hint, bool on)
virtual void componentComplete()=0
bool isComponentComplete() const const
QSizeF size() const const
void update()
qreal height() const const
qreal width() const const
bool isEmpty() const const
QByteArray toByteArray() const const
QString toString() const const
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:12:17 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.