From a411760f0c8459c164606b1a93d4267aa5b2108c Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Tue, 10 Apr 2018 16:00:53 -0700 Subject: [PATCH] Finally fix the grey security pic bug! --- interface/src/commerce/Wallet.cpp | 20 +++++-- libraries/ui/src/ui/ImageProvider.cpp | 59 ------------------- libraries/ui/src/ui/OffscreenQmlSurface.cpp | 7 ++- libraries/ui/src/ui/SecurityImageProvider.cpp | 52 ++++++++++++++++ ...mageProvider.h => SecurityImageProvider.h} | 19 +++--- 5 files changed, 79 insertions(+), 78 deletions(-) delete mode 100644 libraries/ui/src/ui/ImageProvider.cpp create mode 100644 libraries/ui/src/ui/SecurityImageProvider.cpp rename libraries/ui/src/ui/{ImageProvider.h => SecurityImageProvider.h} (57%) diff --git a/interface/src/commerce/Wallet.cpp b/interface/src/commerce/Wallet.cpp index 060f8de09b..c632c9d94a 100644 --- a/interface/src/commerce/Wallet.cpp +++ b/interface/src/commerce/Wallet.cpp @@ -13,8 +13,9 @@ #include "Ledger.h" #include "Wallet.h" #include "Application.h" -#include "ui/ImageProvider.h" +#include "ui/SecurityImageProvider.h" #include "scripting/HMDScriptingInterface.h" +#include #include #include @@ -607,11 +608,17 @@ QString Wallet::signWithKey(const QByteArray& text, const QString& key) { } void Wallet::updateImageProvider() { - // inform the image provider. Note it doesn't matter which one you inform, as the - // images are statics - auto engine = DependencyManager::get()->getSurfaceContext()->engine(); - auto imageProvider = reinterpret_cast(engine->imageProvider(ImageProvider::PROVIDER_NAME)); - imageProvider->setSecurityImage(_securityImage); + SecurityImageProvider* securityImageProvider; + + // inform offscreenUI security image provider + QQmlEngine* engine = DependencyManager::get()->getSurfaceContext()->engine(); + securityImageProvider = reinterpret_cast(engine->imageProvider(SecurityImageProvider::PROVIDER_NAME)); + securityImageProvider->setSecurityImage(_securityImage); + + // inform tablet security image provider + QQmlEngine* tabletEngine = DependencyManager::get()->getTablet("com.highfidelity.interface.tablet.system")->getTabletSurface()->getSurfaceContext()->engine(); + securityImageProvider = reinterpret_cast(tabletEngine->imageProvider(SecurityImageProvider::PROVIDER_NAME)); + securityImageProvider->setSecurityImage(_securityImage); } void Wallet::chooseSecurityImage(const QString& filename) { @@ -651,6 +658,7 @@ bool Wallet::getSecurityImage() { // if already decrypted, don't do it again if (_securityImage) { + updateImageProvider(); emit securityImageResult(true); return true; } diff --git a/libraries/ui/src/ui/ImageProvider.cpp b/libraries/ui/src/ui/ImageProvider.cpp deleted file mode 100644 index 7bbad43f2e..0000000000 --- a/libraries/ui/src/ui/ImageProvider.cpp +++ /dev/null @@ -1,59 +0,0 @@ -// -// ImageProvider.cpp -// interface/src/ui -// -// Created by David Kelly on 8/23/2017. -// Copyright 2017 High Fidelity, Inc. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -#include "ImageProvider.h" - -#include -#include - -const QString ImageProvider::PROVIDER_NAME = "security"; -QReadWriteLock ImageProvider::_rwLock; -QPixmap* ImageProvider::_securityImage = nullptr; - -ImageProvider::~ImageProvider() { - QWriteLocker lock(&_rwLock); - if (_securityImage) { - delete _securityImage; - _securityImage = nullptr; - } -} - -void ImageProvider::setSecurityImage(const QPixmap* pixmap) { - // no need to delete old one, that is managed by the wallet - QWriteLocker lock(&_rwLock); - if (_securityImage) { - delete _securityImage; - } - if (pixmap) { - _securityImage = new QPixmap(*pixmap); - } else { - _securityImage = nullptr; - } -} - -QPixmap ImageProvider::requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) { - - // adjust the internal pixmap to have the requested size - QReadLocker lock(&_rwLock); - if (id == "securityImage" && _securityImage) { - *size = _securityImage->size(); - if (requestedSize.width() > 0 && requestedSize.height() > 0) { - return _securityImage->scaled(requestedSize.width(), requestedSize.height(), Qt::KeepAspectRatio); - } else { - return _securityImage->copy(); - } - } - // otherwise just return a grey pixmap. This avoids annoying error messages in qml we would get - // when sending a 'null' pixmap (QPixmap()) - QPixmap greyPixmap(200, 200); - greyPixmap.fill(QColor("darkGrey")); - return greyPixmap; -} diff --git a/libraries/ui/src/ui/OffscreenQmlSurface.cpp b/libraries/ui/src/ui/OffscreenQmlSurface.cpp index 12e9b8b87c..0ae0e2fe78 100644 --- a/libraries/ui/src/ui/OffscreenQmlSurface.cpp +++ b/libraries/ui/src/ui/OffscreenQmlSurface.cpp @@ -48,7 +48,7 @@ #include #include -#include "ImageProvider.h" +#include "SecurityImageProvider.h" #include "types/FileTypeProfile.h" #include "types/HFWebEngineProfile.h" #include "types/SoundEffect.h" @@ -233,8 +233,8 @@ void OffscreenQmlSurface::initializeEngine(QQmlEngine* engine) { qmlRegisterType("Hifi", 1, 0, "SoundEffect"); }); - // register the pixmap image provider (used only for security image, for now) - engine->addImageProvider(ImageProvider::PROVIDER_NAME, new ImageProvider()); + // Register the pixmap Security Image Provider + engine->addImageProvider(SecurityImageProvider::PROVIDER_NAME, new SecurityImageProvider()); engine->setNetworkAccessManagerFactory(new QmlNetworkAccessManagerFactory); auto importList = engine->importPathList(); @@ -335,6 +335,7 @@ void OffscreenQmlSurface::onRootCreated() { tabletScriptingInterface->setQmlTabletRoot("com.highfidelity.interface.tablet.system", this); QObject* tablet = tabletScriptingInterface->getTablet("com.highfidelity.interface.tablet.system"); getSurfaceContext()->engine()->setObjectOwnership(tablet, QQmlEngine::CppOwnership); + getSurfaceContext()->engine()->addImageProvider(SecurityImageProvider::PROVIDER_NAME, new SecurityImageProvider()); } QMetaObject::invokeMethod(this, "forceQmlAudioOutputDeviceUpdate", Qt::QueuedConnection); } diff --git a/libraries/ui/src/ui/SecurityImageProvider.cpp b/libraries/ui/src/ui/SecurityImageProvider.cpp new file mode 100644 index 0000000000..190e58d6e8 --- /dev/null +++ b/libraries/ui/src/ui/SecurityImageProvider.cpp @@ -0,0 +1,52 @@ +// +// SecurityImageProvider.cpp +// interface/src/ui +// +// Created by David Kelly on 8/23/2017. +// Copyright 2017 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "SecurityImageProvider.h" + +#include +#include + +const QString SecurityImageProvider::PROVIDER_NAME = "security"; + +SecurityImageProvider::~SecurityImageProvider() { +} + +void SecurityImageProvider::setSecurityImage(const QPixmap* pixmap) { + // no need to delete old one, that is managed by the wallet + QWriteLocker lock(&_rwLock); + + if (pixmap) { + _securityImage = pixmap->copy(); + } else { + QPixmap greyPixmap(200, 200); + greyPixmap.fill(QColor("darkGrey")); + _securityImage = greyPixmap.copy(); + } +} + +QPixmap SecurityImageProvider::requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) { + + // adjust the internal pixmap to have the requested size + QReadLocker lock(&_rwLock); + if (id == "securityImage") { + *size = _securityImage.size(); + if (requestedSize.width() > 0 && requestedSize.height() > 0) { + return _securityImage.scaled(requestedSize.width(), requestedSize.height(), Qt::KeepAspectRatio); + } else { + return _securityImage.copy(); + } + } + // otherwise just return a grey pixmap. This avoids annoying error messages in qml we would get + // when sending a 'null' pixmap (QPixmap()) + QPixmap greyPixmap(200, 200); + greyPixmap.fill(QColor("darkGrey")); + return greyPixmap; +} diff --git a/libraries/ui/src/ui/ImageProvider.h b/libraries/ui/src/ui/SecurityImageProvider.h similarity index 57% rename from libraries/ui/src/ui/ImageProvider.h rename to libraries/ui/src/ui/SecurityImageProvider.h index 0093b60655..b12932e225 100644 --- a/libraries/ui/src/ui/ImageProvider.h +++ b/libraries/ui/src/ui/SecurityImageProvider.h @@ -1,5 +1,5 @@ // -// ImageProvider.h +// SecurityImageProvider.h // // Created by David Kelly on 2017/08/23 // Copyright 2017 High Fidelity, Inc. @@ -9,26 +9,25 @@ // #pragma once -#ifndef hifi_ImageProvider_h -#define hifi_ImageProvider_h +#ifndef hifi_SecurityImageProvider_h +#define hifi_SecurityImageProvider_h #include #include -class ImageProvider: public QQuickImageProvider { +class SecurityImageProvider: public QQuickImageProvider { public: static const QString PROVIDER_NAME; - ImageProvider() : QQuickImageProvider(QQuickImageProvider::Pixmap) {} - virtual ~ImageProvider(); + SecurityImageProvider() : QQuickImageProvider(QQuickImageProvider::Pixmap) {} + virtual ~SecurityImageProvider(); QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override; void setSecurityImage(const QPixmap* pixmap); protected: - static QReadWriteLock _rwLock; - static QPixmap* _securityImage; - + QReadWriteLock _rwLock; + QPixmap _securityImage; }; -#endif //hifi_ImageProvider_h +#endif //hifi_SecurityImageProvider_h