1/*
2    SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
3
4    SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7import QtQuick 2.0
8import QtQuick.Layouts 1.1
9import org.kde.plasma.core 2.0 as PlasmaCore
10import org.kde.plasma.components 2.0 as PlasmaComponents // For ContextMenu
11import org.kde.plasma.components 3.0 as PlasmaComponents3
12import org.kde.kquickcontrolsaddons 2.0
13import org.kde.plasma.extras 2.0 as PlasmaExtras
14
15import org.kde.prison 1.0 as Prison
16
17ColumnLayout {
18    id: barcodeView
19
20    property alias text: barcodeItem.content
21
22    property var header: PlasmaExtras.PlasmoidHeading {
23        RowLayout {
24            anchors.fill: parent
25            PlasmaComponents3.Button {
26                Layout.fillWidth: true
27                icon.name: "go-previous-view"
28                text: i18n("Return to Clipboard")
29                onClicked: stack.pop()
30            }
31
32            Component {
33                id: menuItemComponent
34                PlasmaComponents.MenuItem { }
35            }
36
37            PlasmaComponents.ContextMenu {
38                id: menu
39                visualParent: configureButton
40                placement: PlasmaCore.Types.BottomPosedLeftAlignedPopup
41                onStatusChanged: {
42                    if (status == PlasmaComponents.DialogStatus.Closed) {
43                        configureButton.checked = false;
44                    }
45                }
46
47                Component.onCompleted: {
48                    [
49                        {text: i18n("QR Code"), type: Prison.Barcode.QRCode},
50                        {text: i18n("Data Matrix"), type: Prison.Barcode.DataMatrix},
51                        {text: i18nc("Aztec barcode", "Aztec"), type: Prison.Barcode.Aztec},
52                        {text: i18n("Code 39"), type: Prison.Barcode.Code39},
53                        {text: i18n("Code 93"), type: Prison.Barcode.Code93},
54                        {text: i18n("Code 128"), type: Prison.Barcode.Code128}
55                    ].forEach((item) => {
56                        let menuItem = menuItemComponent.createObject(menu, {
57                            text: item.text,
58                            checkable: true,
59                            checked: Qt.binding(() => {
60                                return barcodeItem.barcodeType === item.type;
61                            })
62                        });
63                        menuItem.clicked.connect(() => {
64                            barcodeItem.barcodeType = item.type;
65                        });
66                        menu.addMenuItem(menuItem);
67                    });
68                }
69            }
70            PlasmaComponents3.ToolButton {
71                id: configureButton
72                checkable: true
73                icon.name: "configure"
74                onClicked: menu.openRelative()
75
76                PlasmaComponents3.ToolTip {
77                    text: i18n("Change the barcode type")
78                }
79            }
80        }
81    }
82
83    Item {
84        Layout.fillWidth: parent
85        Layout.fillHeight: parent
86        Layout.topMargin: PlasmaCore.Units.smallSpacing
87
88        Prison.Barcode {
89            id: barcodeItem
90            readonly property bool valid: implicitWidth > 0 && implicitHeight > 0 && implicitWidth <= width && implicitHeight <= height
91            anchors.fill: parent
92            barcodeType: Prison.Barcode.QRCode
93            // Cannot set visible to false as we need it to re-render when changing its size
94            opacity: valid ? 1 : 0
95        }
96
97        PlasmaComponents3.Label {
98            anchors.fill: parent
99            horizontalAlignment: Text.AlignHCenter
100            verticalAlignment: Text.AlignVCenter
101            text: i18n("Creating barcode failed")
102            wrapMode: Text.WordWrap
103            visible: barcodeItem.implicitWidth === 0 && barcodeItem.implicitHeight === 0
104        }
105
106        PlasmaComponents3.Label {
107            anchors.fill: parent
108            horizontalAlignment: Text.AlignHCenter
109            verticalAlignment: Text.AlignVCenter
110            text: i18n("The barcode is too large to be displayed")
111            wrapMode: Text.WordWrap
112            visible: barcodeItem.implicitWidth > barcodeItem.width || barcodeItem.implicitHeight > barcodeItem.height
113        }
114    }
115}
116