File: barcodeexamplewidget.cpp

package info (click to toggle)
kf6-prison 6.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 760 kB
  • sloc: cpp: 5,126; ansic: 21; makefile: 5
file content (89 lines) | stat: -rw-r--r-- 2,463 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
    SPDX-FileCopyrightText: 2010-2014 Sune Vuorela <sune@vuorela.dk>

    SPDX-License-Identifier: MIT
*/

#include "barcodeexamplewidget.h"
// Qt
#include <QDrag>
#include <QGuiApplication>
#include <QMimeData>
#include <QPainter>
#include <QResizeEvent>
#include <QScreen>

using namespace Prison;

BarcodeExampleWidget::BarcodeExampleWidget(std::optional<Barcode> barcode, QWidget *parent)
    : QWidget(parent)
    , m_barcode(std::move(barcode))
{
    if (!barcode) {
        qDebug() << "unsupported barcode, showing a black square";
    }
}

BarcodeExampleWidget::BarcodeExampleWidget(BarcodeType barcode, QWidget *parent)
    : BarcodeExampleWidget(Barcode::create(barcode), parent)
{
}

void BarcodeExampleWidget::setData(const QString &data)
{
    if (m_barcode) {
        m_barcode->setData(data);
        updateGeometry();
        repaint();
    }
}

void BarcodeExampleWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    if (m_barcode) {
        QRect targetrect = rect();
        QImage image = m_barcode->toImage(targetrect.size());
        if (!image.isNull()) {
            QRectF rect(targetrect.left() + targetrect.width() / 2 - image.size().width() / 2,
                        targetrect.top() + targetrect.height() / 2 - image.size().height() / 2,
                        targetrect.size().width(),
                        targetrect.height());
            painter.drawImage(rect.topLeft(), image, image.rect());
        } else {
            painter.fillRect(QRectF(QPointF(0, 0), size()), Qt::cyan);
        }
    } else {
        painter.fillRect(QRectF(QPointF(0, 0), size()), Qt::black);
    }
    QWidget::paintEvent(event);
}

void BarcodeExampleWidget::resizeEvent(QResizeEvent *event)
{
    updateGeometry();
    repaint();
    QWidget::resizeEvent(event);
}

void BarcodeExampleWidget::mousePressEvent(QMouseEvent *event)
{
    if (m_barcode && event->buttons() & Qt::LeftButton) {
        QMimeData *data = new QMimeData();
        data->setImageData(m_barcode->toImage(rect().size()));
        QDrag *drag = new QDrag(this);
        drag->setMimeData(data);
        drag->exec();
    } else {
        QWidget::mousePressEvent(event);
    }
}

QSize BarcodeExampleWidget::minimumSizeHint() const
{
    if (m_barcode)
        return m_barcode->preferredSize(QGuiApplication::primaryScreen()->devicePixelRatio()).toSize();
    return QSize(10, 10);
}

BarcodeExampleWidget::~BarcodeExampleWidget() = default;