From a1adc666aa4df43f3174f97220dcfcdcb52b262f Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Thu, 7 Sep 2017 18:21:08 +0300 Subject: [PATCH] [app] Always use the default clipboard Also, make sure that the QClipboard methods are invoked on the main thread. --- app/src/BooksDialogManager.cpp | 62 +++++++++++++++++++++++++++++----- app/src/BooksDialogManager.h | 27 ++++++++++----- 2 files changed, 73 insertions(+), 16 deletions(-) diff --git a/app/src/BooksDialogManager.cpp b/app/src/BooksDialogManager.cpp index 67a7d4b..e7d6f1b 100644 --- a/app/src/BooksDialogManager.cpp +++ b/app/src/BooksDialogManager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Jolla Ltd. + * Copyright (C) 2015-2017 Jolla Ltd. * Contact: Slava Monich * * You may use this file under the terms of the BSD license as follows: @@ -14,7 +14,7 @@ * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. - * * Neither the name of Nemo Mobile nor the names of its contributors + * * Neither the name of Jolla Ltd nor the names of its contributors * may be used to endorse or promote products derived from this * software without specific prior written permission. * @@ -43,7 +43,34 @@ #include #include -void BooksDialogManager::createApplicationWindow(ZLApplication* aApp) const +void BooksDialogManager::createInstance() +{ + if (!ourInstance) { + ourInstance = new BooksDialogManager(qApp); + } +} + +BooksDialogManager::BooksDialogManager( + QObject* aParent) : + QObject(aParent) +{ + connect(this, + SIGNAL(copyTextToClipboard(QString)), + SLOT(onCopyTextToClipboard(QString))); + connect(this, + SIGNAL(copyImageToClipboard(QImage)), + SLOT(onCopyImageToClipboard(QImage))); +} + +BooksDialogManager::~BooksDialogManager() +{ + HASSERT(this == ourInstance); + ourInstance = NULL; +} + +void +BooksDialogManager::createApplicationWindow( + ZLApplication* aApp) const { HDEBUG("THIS IS NOT SUPPOSED TO HAPPEN!"); } @@ -125,9 +152,11 @@ BooksDialogManager::setClipboardText( const std::string& text, ClipboardType type) const { + // May be invoked on non-UI thread if (!text.empty()) { - QGuiApplication::clipboard()->setText(QString::fromStdString(text), - (type == CLIPBOARD_MAIN) ? QClipboard::Clipboard : QClipboard::Selection); + QString str(QString::fromStdString(text)); + HDEBUG(str); + Q_EMIT copyTextToClipboard(str); } } @@ -136,7 +165,24 @@ BooksDialogManager::setClipboardImage( const ZLImageData &imageData, ClipboardType type) const { - QGuiApplication::clipboard()->setImage( - *static_cast(imageData).image(), - (type == CLIPBOARD_MAIN) ? QClipboard::Clipboard : QClipboard::Selection); + // May be invoked on non-UI thread + QImage image(*((const ZLQtImageData&)imageData).image()); + if (image.isNull()) { + HDEBUG(image.width() << "x" << image.height()); + Q_EMIT copyImageToClipboard(image); + } +} + +void +BooksDialogManager::onCopyTextToClipboard( + QString aText) +{ + QGuiApplication::clipboard()->setText(aText); +} + +void +BooksDialogManager::onCopyImageToClipboard( + QImage aImage) +{ + QGuiApplication::clipboard()->setImage(aImage); } diff --git a/app/src/BooksDialogManager.h b/app/src/BooksDialogManager.h index 65f6137..5b001ce 100644 --- a/app/src/BooksDialogManager.h +++ b/app/src/BooksDialogManager.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Jolla Ltd. + * Copyright (C) 2015-2017 Jolla Ltd. * Contact: Slava Monich * * You may use this file under the terms of the BSD license as follows: @@ -14,7 +14,7 @@ * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. - * * Neither the name of Nemo Mobile nor the names of its contributors + * * Neither the name of Jolla Ltd nor the names of its contributors * may be used to endorse or promote products derived from this * software without specific prior written permission. * @@ -36,18 +36,29 @@ #include -class BooksDialogManager: public ZLDialogManager { +#include +#include +#include + +class BooksDialogManager: public QObject, ZLDialogManager { + Q_OBJECT public: - static void createInstance() { ourInstance = new BooksDialogManager(); } + static void createInstance(); private: - BooksDialogManager() {} + BooksDialogManager(QObject* aParent); + ~BooksDialogManager(); + +Q_SIGNALS: + void copyTextToClipboard(QString aText) const; + void copyImageToClipboard(QImage aImage) const; + +private Q_SLOTS: + void onCopyTextToClipboard(QString aText); + void onCopyImageToClipboard(QImage aImage); public: - static const ZLResource& resource(const ZLResourceKey& aKey) - { return ZLDialogManager::resource()[aKey]; } - void createApplicationWindow(ZLApplication *application) const; shared_ptr createDialog(const ZLResourceKey &key) const;