TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
qrcodewidget.cpp
Go to the documentation of this file.
1 #include "qrcodewidget.h"
2 
3 #include <QPainter>
4 
8 class QRCodeQWidgetPrivate : public TTKPrivate<QRCodeQWidget>
9 {
10 public:
12 
13  bool m_casesen;
14  int m_margin;
15  QString m_iconPath;
16  float m_percent;
17  QByteArray m_text;
21 
22 };
23 
25  : m_casesen(true),
26  m_margin(10),
27  m_percent(0.23f),
28  m_foreground(QColor("black")),
29  m_background(QColor("white")),
30  m_mode(QR_MODE_8),
31  m_level(QR_ECLEVEL_Q)
32 {
33 
34 }
35 
36 
37 
38 QRCodeQWidget::QRCodeQWidget(const QByteArray &text, const QSize &size, QWidget *parent)
39  : QWidget(parent)
40 {
43 
44  d->m_text = text.isEmpty() ? QByteArray("https://github.com/Greedysky/TTKMusicPlayer") : text;
45  setFixedSize(size);
46 }
47 
48 void QRCodeQWidget::setMargin(const int margin)
49 {
51  d->m_margin = margin;
52  update();
53 }
54 
56 {
58  return d->m_margin;
59 }
60 
61 void QRCodeQWidget::setIcon(const QString &path, float percent)
62 {
64  setIconPercent(percent);
65  d->m_iconPath = path;
66  update();
67 }
68 
69 QString QRCodeQWidget::icon() const
70 {
72  return d->m_iconPath;
73 }
74 
75 void QRCodeQWidget::setIconPercent(float percent)
76 {
78  d->m_percent = percent < 0.5 ? percent : 0.3;
79 }
80 
82 {
84  return d->m_percent;
85 }
86 
88 {
90  d->m_casesen = flag;
91  update();
92 }
93 
95 {
97  return d->m_casesen;
98 }
99 
100 void QRCodeQWidget::setText(const QByteArray &text)
101 {
103  d->m_text = text;
104 }
105 
106 QByteArray QRCodeQWidget::text() const
107 {
109  return d->m_text;
110 }
111 
112 void QRCodeQWidget::setForegroundColor(const QColor &color)
113 {
115  d->m_foreground = color;
116 }
117 
119 {
121  return d->m_foreground;
122 }
123 
124 void QRCodeQWidget::setBackgroundColor(const QColor &color)
125 {
127  d->m_background = color;
128 }
129 
131 {
133  return d->m_background;
134 }
135 
137 {
139  d->m_mode = mode;
140 }
141 
143 {
145  return d->m_mode;
146 }
147 
149 {
151  d->m_level = level;
152 }
153 
155 {
157  return d->m_level;
158 }
159 
160 void QRCodeQWidget::paintEvent(QPaintEvent *event)
161 {
163  QWidget::paintEvent (event);
164  QPainter painter(this);
165 
166  QRcode *qrcode = QRcode_encodeString(d->m_text.constData(), 7, d->m_level, d->m_mode, d->m_casesen);
167  if(qrcode)
168  {
169  unsigned char *point = qrcode->data;
170  painter.setPen(Qt::NoPen);
171  painter.fillRect(0, 0, width(), height(), d->m_background);
172 
173  const double scale = (width () - 2.0 * d->m_margin) / qrcode->width;
174  painter.setBrush(d->m_foreground);
175 
176  for(int x = 0; x < qrcode->width; ++x)
177  {
178  for(int y = 0; y < qrcode->width; ++y)
179  {
180  if(*point & 1)
181  {
182  QRectF r(d->m_margin + y * scale, d->m_margin + x * scale, scale, scale);
183  painter.drawRects(&r, 1);
184  }
185  ++point;
186  }
187  }
188  point = nullptr;
189  QRcode_free(qrcode);
190 
192  painter.setBrush(d->m_background);
193  const double icon_width = (width () - 2.0 * d->m_margin) * d->m_percent;
194  const double icon_height = icon_width;
195  const double wrap_x = (width () - icon_width) / 2.0;
196  const double wrap_y = (width () - icon_height) / 2.0;
197  const QRectF wrap(wrap_x - 5, wrap_y - 5, icon_width + 10, icon_height + 10);
198  painter.drawRoundedRect(wrap, 10, 10);
199 
200  QPixmap image(d->m_iconPath);
201  const QRectF target(wrap_x, wrap_y, icon_width, icon_height);
202  const QRectF source(0, 0, image.width (), image.height ());
203  painter.drawPixmap(target, image, source);
204  }
205  qrcode = nullptr;
206 }
void QRcode_free(QRcode *qrcode)
Free the instance of QRcode class.
Definition: qrencode.c:421
QRcode class.
Definition: qrencode.h:377
void setCaseSensitive(bool flag)
void setBackgroundColor(const QColor &color)
QRencodeMode mode() const
QColor backgroundColor() const
void setIconPercent(float percent)
void setForegroundColor(const QColor &color)
void setText(const QByteArray &text)
float iconPercent() const
voidpf void uLong size
Definition: ioapi.h:136
void setIcon(const QString &path, float percent)
void setMargin(const int margin)
QRecLevel
Level of error correction.
Definition: qrencode.h:126
8-bit data mode
Definition: qrencode.h:115
QRecLevel level() const
QRCodeQWidget(const QByteArray &text, const QSize &size, QWidget *parent=nullptr)
QRencodeMode
Encoding mode.
Definition: qrencode.h:111
The class of the qr code widget.
Definition: qrcodewidget.h:31
QRcode * QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
Create a symbol from the string.
Definition: qrencode.c:647
void setMode(QRencodeMode mode)
int margin() const
bool caseSensitive() const
unsigned char * data
symbol data
Definition: qrencode.h:380
QString icon() const
The class of the qr code widget private.
Definition: qrcodewidget.cpp:8
int width
width of the symbol
Definition: qrencode.h:379
QColor foregroundColor() const
void setLevel(QRecLevel level)
const char int mode
Definition: ioapi.h:135
#define TTK_INIT_PRIVATE(Class)
Definition: ttkprivate.h:33
The class of the ttk private base.
Definition: ttkprivate.h:48
QByteArray text() const
virtual void paintEvent(QPaintEvent *event) overridefinal
#define TTK_D(Class)
Definition: ttkprivate.h:41