diff --git a/applets/clipboard/contents/ui/BarcodePage.qml b/applets/clipboard/contents/ui/BarcodePage.qml --- a/applets/clipboard/contents/ui/BarcodePage.qml +++ b/applets/clipboard/contents/ui/BarcodePage.qml @@ -23,29 +23,12 @@ import org.kde.kquickcontrolsaddons 2.0 import org.kde.plasma.extras 2.0 as PlasmaExtras +import org.kde.prison 1.0 as Prison + ColumnLayout { id: barcodeView - property var uuid: "" - property int barcodeType: 0 - - function show(uuid) { - barcodeView.uuid = uuid; - barcodePreview.image = undefined; - barcodePreview.busy = true; - var service = clipboardSource.serviceForSource(uuid) - var operation = service.operationDescription("barcode"); - operation.width = barcodePreview.width; - operation.height = barcodePreview.height; - operation.barcodeType = barcodeView.barcodeType; - var serviceJob = service.startOperationCall(operation); - serviceJob.finished.connect(function (job) { - if (!job.error) { - barcodePreview.image = job.result; - barcodePreview.busy = false; - } - }); - } + property alias text: barcodeItem.content property var header: PlasmaExtras.PlasmoidHeading { RowLayout { @@ -56,6 +39,12 @@ text: i18n("Return to Clipboard") onClicked: stack.pop() } + + Component { + id: menuItemComponent + PlasmaComponents.MenuItem { } + } + PlasmaComponents.ContextMenu { id: menu visualParent: configureButton @@ -66,40 +55,27 @@ } } - function change(type) { - barcodeView.barcodeType = type; - barcodeView.show(barcodeView.uuid); - } - - PlasmaComponents.MenuItem { - text: i18n("QR Code") - checkable: true - checked: barcodeView.barcodeType == 0 - onClicked: menu.change(0) - } - PlasmaComponents.MenuItem { - text: i18n("Data Matrix") - checkable: true - checked: barcodeView.barcodeType == 1 - onClicked: menu.change(1) - } - PlasmaComponents.MenuItem { - text: i18nc("Aztec barcode", "Aztec") - checkable: true - checked: barcodeView.barcodeType == 4 - onClicked: menu.change(4) - } - PlasmaComponents.MenuItem { - text: i18n("Code 39") - checkable: true - checked: barcodeView.barcodeType == 2 - onClicked: menu.change(2) - } - PlasmaComponents.MenuItem { - text: i18n("Code 93") - checkable: true - checked: barcodeView.barcodeType == 3 - onClicked: menu.change(3) + Component.onCompleted: { + [ + {text: i18n("QR Code"), type: Prison.Barcode.QRCode}, + {text: i18n("Data Matrix"), type: Prison.Barcode.DataMatrix}, + {text: i18nc("Aztec barcode", "Aztec"), type: Prison.Barcode.Aztec}, + {text: i18n("Code 39"), type: Prison.Barcode.Code39}, + {text: i18n("Code 93"), type: Prison.Barcode.Code93}, + {text: i18n("Code 128"), type: Prison.Barcode.Code128} + ].forEach((item) => { + let menuItem = menuItemComponent.createObject(menu, { + text: item.text, + checkable: true, + checked: Qt.binding(() => { + return barcodeItem.barcodeType === item.type; + }) + }); + menuItem.clicked.connect(() => { + barcodeItem.barcodeType = item.type; + }); + menu.addMenuItem(menuItem); + }); } } PlasmaComponents.ToolButton { @@ -112,23 +88,36 @@ } } - QImageItem { - id: barcodePreview - property alias busy: busyIndicator.visible - fillMode: QImageItem.PreserveAspectFit - Layout.fillWidth: true - Layout.fillHeight: true + Item { + Layout.fillWidth: parent + Layout.fillHeight: parent Layout.topMargin: units.smallSpacing - onWidthChanged: barcodeView.show(barcodeView.uuid) - onHeightChanged: barcodeView.show(barcodeView.uuid) - PlasmaComponents.BusyIndicator { - id: busyIndicator - anchors.centerIn: parent + + Prison.Barcode { + id: barcodeItem + readonly property bool valid: implicitWidth > 0 && implicitHeight > 0 && implicitWidth <= width && implicitHeight <= height + anchors.fill: parent + barcodeType: Prison.Barcode.QRCode + // Cannot set visible to false as we need it to re-render when changing its size + opacity: valid ? 1 : 0 } + PlasmaComponents.Label { - anchors.centerIn: parent + anchors.fill: parent + horizontalAlignment: Text.AlignHCenter + verticalAlignment: Text.AlignVCenter text: i18n("Creating barcode failed") - visible: !barcodePreview.busy && barcodePreview.null + wrapMode: Text.WordWrap + visible: barcodeItem.implicitWidth === 0 && barcodeItem.implicitHeight === 0 + } + + PlasmaComponents.Label { + anchors.fill: parent + horizontalAlignment: Text.AlignHCenter + verticalAlignment: Text.AlignVCenter + text: i18n("The barcode is too large to be displayed") + wrapMode: Text.WordWrap + visible: barcodeItem.implicitWidth > barcodeItem.width || barcodeItem.implicitHeight > barcodeItem.height } } } diff --git a/applets/clipboard/contents/ui/ClipboardItemDelegate.qml b/applets/clipboard/contents/ui/ClipboardItemDelegate.qml --- a/applets/clipboard/contents/ui/ClipboardItemDelegate.qml +++ b/applets/clipboard/contents/ui/ClipboardItemDelegate.qml @@ -34,7 +34,7 @@ signal itemSelected(string uuid) signal remove(string uuid) signal edit(string uuid) - signal barcode(string uuid) + signal barcode(string text) signal action(string uuid) // the 1.6 comes from ToolButton's default height diff --git a/applets/clipboard/contents/ui/ClipboardPage.qml b/applets/clipboard/contents/ui/ClipboardPage.qml --- a/applets/clipboard/contents/ui/ClipboardPage.qml +++ b/applets/clipboard/contents/ui/ClipboardPage.qml @@ -102,16 +102,26 @@ filterRole: "DisplayRole" filterRegExp: filter.text } - supportsBarcodes: clipboardSource.data["clipboard"]["supportsBarcodes"] + supportsBarcodes: { + try { + let prisonTest = Qt.createQmlObject("import QtQml 2.0; import org.kde.prison 1.0; QtObject {}", this); + prisonTest.destroy(); + } catch (e) { + console.log("Barcodes not supported:", e); + return false; + } + return true; + } Layout.fillWidth: true Layout.fillHeight: true Layout.topMargin: units.smallSpacing onItemSelected: clipboardSource.service(uuid, "select") onRemove: clipboardSource.service(uuid, "remove") onEdit: clipboardSource.edit(uuid) onBarcode: { - var page = stack.push(barcodePage); - page.show(uuid); + stack.push(barcodePage, { + text: text + }); } onAction: { clipboardSource.service(uuid, "action") diff --git a/applets/clipboard/contents/ui/DelegateToolButtons.qml b/applets/clipboard/contents/ui/DelegateToolButtons.qml --- a/applets/clipboard/contents/ui/DelegateToolButtons.qml +++ b/applets/clipboard/contents/ui/DelegateToolButtons.qml @@ -36,7 +36,8 @@ id: barcodeToolButton iconSource: "view-barcode-qr" tooltip: i18n("Show barcode") - onClicked: menuItem.barcode(UuidRole) + visible: supportsBarcodes + onClicked: menuItem.barcode(DisplayRole) } PlasmaComponents.ToolButton { iconSource: "document-edit" diff --git a/applets/clipboard/contents/ui/Menu.qml b/applets/clipboard/contents/ui/Menu.qml --- a/applets/clipboard/contents/ui/Menu.qml +++ b/applets/clipboard/contents/ui/Menu.qml @@ -30,7 +30,7 @@ signal itemSelected(string uuid) signal remove(string uuid) signal edit(string uuid) - signal barcode(string uuid) + signal barcode(string text) signal action(string uuid) ListView { @@ -51,7 +51,7 @@ onItemSelected: menu.itemSelected(uuid) onRemove: menu.remove(uuid) onEdit: menu.edit(uuid) - onBarcode: menu.barcode(uuid) + onBarcode: menu.barcode(text) onAction: menu.action(uuid) }