File: barcodeexamplewidget.cpp

package info (click to toggle)
prison-kf5 5.116.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 716 kB
  • sloc: cpp: 4,661; makefile: 11
file content (90 lines) | stat: -rw-r--r-- 2,330 bytes parent folder | download | duplicates (2)
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
90
/*
    SPDX-FileCopyrightText: 2010-2014 Sune Vuorela <sune@vuorela.dk>

    SPDX-License-Identifier: MIT
*/

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

using namespace Prison;

BarcodeExampleWidget::BarcodeExampleWidget(AbstractBarcode *barcode, QWidget *parent)
    : QWidget(parent)
    , m_barcode(barcode)
{
}

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)
{
    if (m_barcode) {
        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();
    } else {
        return QWidget::minimumSizeHint();
    }
}

BarcodeExampleWidget::~BarcodeExampleWidget()
{
    delete m_barcode;
}