From 6bf7e7e0c94808223a715307e47408ea5b0b04e6 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Sun, 19 May 2019 22:35:08 +0800 Subject: [PATCH] Fix imageclipboard saveImage(). --- imports/Spectral/Panel/RoomPanel.qml | 2 +- imports/Spectral/Panel/RoomPanelInput.qml | 5 +++-- spectral.pro | 16 ++++++++-------- src/imageclipboard.cpp | 22 ++++++++++++++++++---- src/imageclipboard.h | 5 ++--- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/imports/Spectral/Panel/RoomPanel.qml b/imports/Spectral/Panel/RoomPanel.qml index 7cb1c48..df05a5f 100644 --- a/imports/Spectral/Panel/RoomPanel.qml +++ b/imports/Spectral/Panel/RoomPanel.qml @@ -125,7 +125,7 @@ Item { background: RippleEffect { onClicked: { var localPath = StandardPaths.writableLocation(StandardPaths.CacheLocation) + "/screenshots/" + (new Date()).getTime() + ".png" - imageClipboard.saveImage(localPath) + if (!imageClipboard.saveImage(localPath)) return roomPanelInput.attach(localPath) attachDialog.close() } diff --git a/imports/Spectral/Panel/RoomPanelInput.qml b/imports/Spectral/Panel/RoomPanelInput.qml index 41bbd72..6930290 100644 --- a/imports/Spectral/Panel/RoomPanelInput.qml +++ b/imports/Spectral/Panel/RoomPanelInput.qml @@ -301,7 +301,7 @@ Control { Keys.onReturnPressed: { if (event.modifiers & Qt.ShiftModifier) { insert(cursorPosition, "\n") - } else if (text) { + } else { postMessage(text) text = "" closeAll() @@ -353,7 +353,6 @@ Control { } function postMessage(text) { - if (text.trim().length === 0) { return } if(!currentRoom) { return } if (hasAttachment) { @@ -362,6 +361,8 @@ Control { return } + if (text.trim().length === 0) { return } + var PREFIX_ME = '/me ' var PREFIX_NOTICE = '/notice ' var PREFIX_RAINBOW = '/rainbow ' diff --git a/spectral.pro b/spectral.pro index 91e9392..dfcab20 100644 --- a/spectral.pro +++ b/spectral.pro @@ -42,9 +42,7 @@ HEADERS += \ include/hoedown/escape.h \ include/hoedown/html.h \ include/hoedown/stack.h \ - include/hoedown/version.h \ - src/imageclipboard.h \ - src/matriximageprovider.h + include/hoedown/version.h SOURCES += \ include/hoedown/autolink.c \ @@ -55,9 +53,7 @@ SOURCES += \ include/hoedown/html_blocks.c \ include/hoedown/html_smartypants.c \ include/hoedown/stack.c \ - include/hoedown/version.c \ - src/imageclipboard.cpp \ - src/matriximageprovider.cpp + include/hoedown/version.c # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked deprecated (the exact warnings @@ -128,7 +124,9 @@ HEADERS += \ src/accountlistmodel.h \ src/spectraluser.h \ src/notifications/manager.h \ - src/utils.h + src/utils.h \ + src/imageclipboard.h \ + src/matriximageprovider.h SOURCES += src/main.cpp \ src/controller.cpp \ @@ -139,7 +137,9 @@ SOURCES += src/main.cpp \ src/userlistmodel.cpp \ src/accountlistmodel.cpp \ src/spectraluser.cpp \ - src/utils.cpp + src/utils.cpp \ + src/imageclipboard.cpp \ + src/matriximageprovider.cpp unix:!mac { SOURCES += src/notifications/managerlinux.cpp diff --git a/src/imageclipboard.cpp b/src/imageclipboard.cpp index deb00ed..d825835 100644 --- a/src/imageclipboard.cpp +++ b/src/imageclipboard.cpp @@ -1,7 +1,10 @@ #include "imageclipboard.h" +#include +#include #include #include +#include ImageClipboard::ImageClipboard(QObject* parent) : QObject(parent), m_clipboard(QGuiApplication::clipboard()) { @@ -17,10 +20,21 @@ QImage ImageClipboard::image() { return m_clipboard->image(); } -void ImageClipboard::saveImage(const QUrl& localPath) { - auto i = image(); +bool ImageClipboard::saveImage(const QUrl& localPath) { + if (!localPath.isLocalFile()) + return false; - if (i.isNull()) return; + auto i = image(); - i.save(localPath.toString()); + if (i.isNull()) + return false; + + QString path = QFileInfo(localPath.toString()).absolutePath(); + QDir dir; + if (!dir.exists(path)) + dir.mkpath(path); + + i.save(localPath.toLocalFile()); + + return true; } diff --git a/src/imageclipboard.h b/src/imageclipboard.h index d548c7e..a8cc181 100644 --- a/src/imageclipboard.h +++ b/src/imageclipboard.h @@ -16,14 +16,13 @@ class ImageClipboard : public QObject { bool hasImage(); QImage image(); + Q_INVOKABLE bool saveImage(const QUrl& localPath); + private: QClipboard* m_clipboard; signals: void imageChanged(); - - public slots: - void saveImage(const QUrl& localPath); }; #endif // IMAGECLIPBOARD_H