Prison

abstractbarcode.cpp
1 /*
2  SPDX-FileCopyrightText: 2010-2016 Sune Vuorela <[email protected]>
3 
4  SPDX-License-Identifier: MIT
5 */
6 
7 #include "abstractbarcode.h"
8 #include "config-prison.h"
9 #include "pdf417barcode.h"
10 
11 #include <QColor>
12 #include <QPainter>
13 #include <QVariant>
14 
15 using namespace Prison;
16 /**
17  * @cond private
18  */
19 class Prison::AbstractBarcodePrivate
20 {
21 public:
22  QVariant m_data;
23  QImage m_cache;
24  QColor m_foreground = Qt::black;
25  QColor m_background = Qt::white;
27  AbstractBarcode *q;
28 
29  bool sizeTooSmall(const QSizeF &size) const
30  {
31  return m_cache.width() > size.width() || m_cache.height() > size.height();
32  }
33 
34  bool isEmpty() const
35  {
36  switch (m_data.type()) {
37  case QVariant::String:
38  return m_data.toString().isEmpty();
40  return m_data.toByteArray().isEmpty();
41  default:
42  break;
43  }
44  return true;
45  }
46 
47  void recompute()
48  {
49  if (m_cache.isNull() && !isEmpty()) {
50  m_cache = q->paintImage({});
51  }
52  }
53 
54  explicit AbstractBarcodePrivate(AbstractBarcode *barcode)
55  : q(barcode)
56  {
57  }
58 };
59 /**
60  * @endcond
61  */
62 
63 #if PRISON_BUILD_DEPRECATED_SINCE(5, 69)
65  : d(new AbstractBarcodePrivate(this))
66 {
67 }
68 #endif
69 
71  : d(new AbstractBarcodePrivate(this))
72 {
73  d->m_dimension = dim;
74 }
75 
77 {
78  return d->m_data.type() == QVariant::String ? d->m_data.toString() : QString();
79 }
80 
82 {
83  return d->m_data.type() == QVariant::ByteArray ? d->m_data.toByteArray() : QByteArray();
84 }
85 
87 {
88  d->recompute();
89  if (d->m_cache.isNull() || d->sizeTooSmall(size)) {
90  return QImage();
91  }
92 
93  // scale to the requested size, using only full integer factors to keep the code readable
94  int scaleX = std::max<int>(1, size.width() / d->m_cache.width());
95  int scaleY = std::max<int>(1, size.height() / d->m_cache.height());
96  if (dimensions() == TwoDimensions) {
97  scaleX = scaleY = std::min(scaleX, scaleY);
98  }
99 
100  QImage out(d->m_cache.width() * scaleX, d->m_cache.height() * scaleY, d->m_cache.format());
101  QPainter p(&out);
103  p.drawImage(out.rect(), d->m_cache, d->m_cache.rect());
104  return out;
105 }
106 
108 {
109  d->m_data = data;
110  d->m_cache = QImage();
111 }
112 
114 {
115  d->m_data = data;
116  d->m_cache = QImage();
117 }
118 
119 #if PRISON_BUILD_DEPRECATED_SINCE(5, 72)
121 {
122  d->recompute();
123 
124  // ### backward compatibility: this is applying minimum size behavior that the specific
125  // implementations were doing prior to 5.69. This is eventually to be dropped.
126  if (d->m_cache.isNull()) {
127  return {};
128  }
129  switch (d->m_dimension) {
130  case NoDimensions:
131  return {};
132  case OneDimension:
133  return QSizeF(d->m_cache.width(), std::max(d->m_cache.height(), 10));
134  case TwoDimensions:
135  return d->m_cache.size() * 4;
136  }
137 
138  return d->m_cache.size();
139 }
140 #endif
141 
143 {
144  d->recompute();
145  return d->m_cache.size();
146 }
147 
148 QSizeF AbstractBarcode::preferredSize(qreal devicePixelRatio) const
149 {
150  d->recompute();
151  switch (d->m_dimension) {
152  case NoDimensions:
153  return {};
154  case OneDimension:
155  return QSizeF(d->m_cache.width() * (devicePixelRatio < 2 ? 2 : 1), std::max(d->m_cache.height(), 50));
156  case TwoDimensions:
157  // TODO KF6: clean this up once preferredSize is virtual
158 #if HAVE_ZXING
159  // the smallest element of a PDF417 code is 1x 3px, for Aztec/QR/DataMatrix it's just 1x1 px
160  if (dynamic_cast<const Pdf417Barcode *>(this)) {
161  return d->m_cache.size() * (devicePixelRatio < 2 ? 2 : 1);
162  }
163 #endif
164  return d->m_cache.size() * (devicePixelRatio < 2 ? 4 : 2);
165  }
166  return {};
167 }
168 
169 #if PRISON_BUILD_DEPRECATED_SINCE(5, 69)
170 void AbstractBarcode::setMinimumSize(const QSizeF &minimumSize)
171 {
172  Q_UNUSED(minimumSize);
173 }
174 #endif
175 
177 {
178  return d->m_background;
179 }
180 
182 {
183  return d->m_foreground;
184 }
185 
186 void AbstractBarcode::setBackgroundColor(const QColor &backgroundcolor)
187 {
188  if (backgroundcolor != backgroundColor()) {
189  d->m_background = backgroundcolor;
190  d->m_cache = QImage();
191  }
192 }
193 
194 void AbstractBarcode::setForegroundColor(const QColor &foregroundcolor)
195 {
196  if (foregroundcolor != foregroundColor()) {
197  d->m_foreground = foregroundcolor;
198  d->m_cache = QImage();
199  }
200 }
201 
203 {
204  return d->m_dimension;
205 }
206 
207 AbstractBarcode::~AbstractBarcode() = default;
SmoothPixmapTransform
void setMinimumSize(const QSizeF &minimumSize)
Sets the minimum size for this barcode.
qreal height() const const
PDF417 barcode.
Definition: pdf417barcode.h:18
int height() const const
Dimensions
Dimensions of the barcode.
QByteArray toByteArray() const const
QRect rect() const const
AbstractBarcode()
creates a barcode generator without any data
QSizeF preferredSize(qreal devicePixelRatio) const
The recommended size for this barcode when shown on a screen.
const QColor & backgroundColor() const
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags)
virtual QImage paintImage(const QSizeF &size)=0
Doing the actual painting of the image.
QSizeF minimumSize() const
The minimal size of this barcode.
QVariant::Type type() const const
@ OneDimension
One-dimensional barcode.
QImage toImage(const QSizeF &size)
Creates a image with a barcode on.
bool isEmpty() const const
bool isNull() const const
@ NoDimensions
Null barcode.
@ TwoDimensions
2D matrix code.
void setBackgroundColor(const QColor &backgroundcolor)
sets the background color
base class for barcode generators To add your own barcode generator, subclass this class and reimplem...
bool isEmpty() const const
void setForegroundColor(const QColor &foregroundcolor)
sets the foreground color
QByteArray byteArrayData() const
Binary data encoded in this barcode.
Dimensions dimensions() const
Returns the amount of dimensions of the barcode.
const QColor & foregroundColor() const
QString data() const
Textual content encoded in this barcode.
QSizeF trueMinimumSize() const
The minimal amount of pixels needed to represent this barcode without loss of information.
void setRenderHint(QPainter::RenderHint hint, bool on)
void setData(const QString &data)
Sets textual data to be drawn as a barcode.
QString toString() const const
qreal width() const const
int width() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Sep 26 2023 03:56:49 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.