From 23ec54ecd051badaff971dd4c5991dfbb618a0c8 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Thu, 3 Dec 2020 21:43:58 +0100 Subject: [PATCH] Allow Ctrl-V pasting of images from the clipboard Fix #117 --- imports/NeoChat/Component/ChatTextInput.qml | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/imports/NeoChat/Component/ChatTextInput.qml b/imports/NeoChat/Component/ChatTextInput.qml index f8341e1c1..30a5959a5 100644 --- a/imports/NeoChat/Component/ChatTextInput.qml +++ b/imports/NeoChat/Component/ChatTextInput.qml @@ -6,6 +6,7 @@ import QtQuick 2.12 import QtQuick.Controls 2.12 import QtQuick.Layouts 1.12 +import Qt.labs.platform 1.0 as Platform import org.kde.kirigami 2.13 as Kirigami import NeoChat.Component 1.0 @@ -39,6 +40,17 @@ ToolBar { } Kirigami.Theme.colorSet: Kirigami.Theme.View + Action { + id: pasteAction + shortcut: StandardKey.Paste + onTriggered: { + if (Clipboard.hasImage) { + root.pasteImage(); + } + activeFocusItem.paste(); + } + } + contentItem: ColumnLayout { id: layout spacing: 0 @@ -303,10 +315,13 @@ ToolBar { Keys.onEscapePressed: closeAll() Keys.onPressed: { + console.log(Qt.Key_Paste, event.key); if (event.key === Qt.Key_PageDown) { switchRoomDown(); } else if (event.key === Qt.Key_PageUp) { switchRoomUp(); + } else if (event.key === Qt.Key_V && event.modifiers & Qt.ControlModifier) { + root.pasteImage(); } } @@ -407,6 +422,11 @@ ToolBar { onClicked: { if (Clipboard.hasImage) { + documentHandler.paste() + const localPath = Platform.StandardPaths.writableLocation(Platform.StandardPaths.CacheLocation) + "/screenshots/" + (new Date()).getTime() + ".png" + if (!Clipboard.saveImage(localPath)) return + chatTextInput.attach(localPath) + attachDialog.close() attachDialog.open() } else { var fileDialog = openFileDialog.createObject(ApplicationWindow.overlay) @@ -497,4 +517,12 @@ ToolBar { hasAttachment = false attachmentPath = "" } + + function pasteImage() { + var localPath = Platform.StandardPaths.writableLocation(Platform.StandardPaths.CacheLocation) + "/screenshots/" + (new Date()).getTime() + ".png"; + if (!Clipboard.saveImage(localPath)) { + return; + } + root.attach(localPath); + } }