TicketTokenDelegate.qml 4.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
    Copyright (C) 2018 Volker Krause <vkrause@kde.org>

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU General Public License
15
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19 20
*/

import QtQuick 2.5
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.1 as QQC2
21
import QtQuick.Window 2.10
22 23 24 25 26 27
import org.kde.kirigami 2.0 as Kirigami
import org.kde.prison 1.0 as Prison
import org.kde.kitinerary 1.0
import org.kde.itinerary 1.0
import "." as App

28
ColumnLayout {
29
    id: root
30
    property var resIds
31

32
    readonly property var currentReservationId: ticketModel.reservationIdAt(travelerBox.currentIndex)
33
    readonly property var currentTicket: ticketModel.reservationAt(travelerBox.currentIndex).reservedTicket
34
    Layout.fillWidth: true
35

36 37 38 39 40 41 42 43 44 45 46
    TicketTokenModel {
        id: ticketModel
        reservationManager: _reservationManager
        reservationIds: resIds
    }

    QQC2.ComboBox {
        id: travelerBox
        Layout.alignment: Qt.AlignCenter
        model: ticketModel
        textRole: "display"
47
        Layout.fillWidth: true
48 49 50

        // ugly, but rowCount does not trigger binding changes
        Component.onCompleted: visible = ticketModel.rowCount() >= 1 && root.resIds.length > 1
51 52 53 54 55 56 57 58 59 60 61 62 63 64
    }

    Item {
        id: barcodeContainer
        Layout.alignment: Qt.AlignCenter
        Layout.fillWidth: true
        implicitHeight: childrenRect.height

        Rectangle {
            id: background
            anchors.centerIn: barcodeContainer
            anchors.top: barcodeContainer.top
            anchors.bottom: barcodeContainer.bottom
            color: "white"
65
            // aim at 50x50mm for 2d codes, and 25mm height for 1d codes, if we have the space for it
66
            property bool is1dCode: currentTicket.ticketTokenType == Ticket.Code128
67 68 69 70 71 72 73 74 75 76 77
            // if this gets too wide, we need to rotate by 90°
            property bool showVertical: is1dCode && barcodeTargetWidth > root.width

            // unrotated barcode sizes
            property int barcodeTargetWidth: Math.max(
                (is1dCode ? 2 * barcode.implicitWidth : barcode.implicitWidth) + 2 * barcode.anchors.margins,
                Screen.pixelDensity * 50)
            property int barcodeTargetHeight: is1dCode ? Screen.pixelDensity * 25 : barcodeTargetWidth

            implicitWidth: (showVertical ? barcodeTargetHeight : barcodeTargetWidth) + 2 * Kirigami.Units.smallSpacing
            implicitHeight: visible ? (showVertical ? barcodeTargetWidth : barcodeTargetHeight) + 2 * Kirigami.Units.smallSpacing : 0
78
            visible: barcode.implicitHeight > 0
79

80 81
            MouseArea {
                anchors.fill: parent
82
                onDoubleClicked: _brightnessManager.toggleBrightness();
83 84
            }

85 86
            Prison.Barcode {
                id: barcode
87 88 89 90
                anchors.centerIn: background
                width: background.barcodeTargetWidth
                height: background.barcodeTargetHeight
                rotation: background.showVertical ? 90 : 0
91 92 93 94 95 96 97
                barcodeType:
                {
                    if (currentTicket == undefined)
                        return Prison.Barcode.Null;
                    switch (currentTicket.ticketTokenType) {
                        case Ticket.QRCode: return Prison.Barcode.QRCode;
                        case Ticket.AztecCode: return Prison.Barcode.Aztec;
98
                        case Ticket.Code128: return Prison.Barcode.Code128;
99
                    }
100 101
                    return Prison.Barcode.Null;
                }
102 103 104 105 106 107
                content:
                {
                    if (barcodeType == Prison.Barcode.Null || currentTicket == undefined)
                        return "";
                    return currentTicket.ticketTokenData;
                }
108 109 110 111
            }
        }
    }
}