Cleanup, moving some QML objects to OffscreenUI

This commit is contained in:
Brad Davis 2015-12-31 12:39:56 -08:00
parent a883891197
commit 462cc325e5
5 changed files with 65 additions and 77 deletions

View file

@ -692,8 +692,8 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer) :
if (offscreenUi->navigationFocused()) {
auto actionEnum = static_cast<Action>(action);
int key = Qt::Key_unknown;
bool navAxis = false;
static int lastKey = Qt::Key_unknown;
bool navAxis = false;
switch (actionEnum) {
case Action::UI_NAV_VERTICAL:
navAxis = true;
@ -732,16 +732,13 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer) :
}
if (navAxis) {
qDebug() << "Axis " << action << " value " << state;
if (lastKey != Qt::Key_unknown) {
qDebug() << "Releasing key " << lastKey;
QKeyEvent event(QEvent::KeyRelease, lastKey, Qt::NoModifier);
sendEvent(offscreenUi->getWindow(), &event);
lastKey = Qt::Key_unknown;
}
if (key != Qt::Key_unknown) {
qDebug() << "Pressing key " << key;
QKeyEvent event(QEvent::KeyPress, key, Qt::NoModifier);
sendEvent(offscreenUi->getWindow(), &event);
lastKey = key;

View file

@ -37,7 +37,7 @@ public:
using MouseTranslator = std::function<QPointF(const QPointF&)>;
void create(QOpenGLContext* context);
virtual void create(QOpenGLContext* context);
void resize(const QSize& size);
QSize size() const;
Q_INVOKABLE QObject* load(const QUrl& qmlSource, std::function<void(QQmlContext*, QObject*)> f = [](QQmlContext*, QObject*) {});

View file

@ -9,10 +9,11 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "OffscreenUi.h"
#include <QOpenGLDebugLogger>
#include <QQuickWindow>
#include <QGLWidget>
#include <QtQml>
#include <QtQml/QtQml>
#include <QtQuick/QQuickWindow>
#include <AccountManager.h>
#include "ErrorDialog.h"
#include "MessageDialog.h"
@ -27,7 +28,52 @@ public:
}
};
class OffscreenFlags : public QObject {
Q_OBJECT
Q_PROPERTY(bool navigationFocused READ isNavigationFocused WRITE setNavigationFocused NOTIFY navigationFocusedChanged)
public:
OffscreenFlags(QObject* parent = nullptr) : QObject(parent) {}
bool isNavigationFocused() const { return _navigationFocused; }
void setNavigationFocused(bool focused) {
if (_navigationFocused != focused) {
_navigationFocused = focused;
emit navigationFocusedChanged();
}
}
signals:
void navigationFocusedChanged();
private:
bool _navigationFocused { false };
};
class UrlFixer : public QObject {
Q_OBJECT
public:
Q_INVOKABLE QString fixupUrl(const QString& originalUrl) {
static const QString ACCESS_TOKEN_PARAMETER = "access_token";
static const QString ALLOWED_HOST = "metaverse.highfidelity.com";
QString result = originalUrl;
QUrl url(originalUrl);
QUrlQuery query(url);
if (url.host() == ALLOWED_HOST && query.allQueryItemValues(ACCESS_TOKEN_PARAMETER).empty()) {
qDebug() << "Updating URL with auth token";
AccountManager& accountManager = AccountManager::getInstance();
query.addQueryItem(ACCESS_TOKEN_PARAMETER, accountManager.getAccountInfo().getAccessToken().token);
url.setQuery(query.query());
result = url.toString();
}
return result;
}
};
static UrlFixer * urlFixer { nullptr };
static OffscreenFlags* offscreenFlags { nullptr };
// This hack allows the QML UI to work with keys that are also bound as
// shortcuts at the application level. However, it seems as though the
@ -58,9 +104,15 @@ OffscreenUi::OffscreenUi() {
::qmlRegisterType<OffscreenUiRoot>("Hifi", 1, 0, "Root");
}
OffscreenUi::~OffscreenUi() {
}
void OffscreenUi::create(QOpenGLContext* context) {
OffscreenQmlSurface::create(context);
auto rootContext = getRootContext();
offscreenFlags = new OffscreenFlags();
rootContext->setContextProperty("offscreenFlags", offscreenFlags);
urlFixer = new UrlFixer();
rootContext->setContextProperty("urlFixer", urlFixer);
}
void OffscreenUi::show(const QUrl& url, const QString& name, std::function<void(QQmlContext*, QObject*)> f) {
QQuickItem* item = getRootItem()->findChild<QQuickItem*>(name);
@ -139,47 +191,14 @@ void OffscreenUi::error(const QString& text) {
pDialog->setEnabled(true);
}
OffscreenUi::ButtonCallback OffscreenUi::NO_OP_CALLBACK = [](QMessageBox::StandardButton) {};
static const char * const NAVIGATION_FOCUSED_PROPERTY = "NavigationFocused";
class OffscreenFlags : public QObject{
Q_OBJECT
Q_PROPERTY(bool navigationFocused READ isNavigationFocused WRITE setNavigationFocused NOTIFY navigationFocusedChanged)
public:
OffscreenFlags(QObject* parent = nullptr) : QObject(parent) {}
bool isNavigationFocused() const { return _navigationFocused; }
void setNavigationFocused(bool focused) {
if (_navigationFocused != focused) {
_navigationFocused = focused;
emit navigationFocusedChanged();
}
}
signals:
void navigationFocusedChanged();
private:
bool _navigationFocused { false };
};
OffscreenFlags* getFlags(QQmlContext* context) {
static OffscreenFlags* offscreenFlags { nullptr };
if (!offscreenFlags) {
offscreenFlags = new OffscreenFlags(context);
context->setContextProperty("OffscreenFlags", offscreenFlags);
}
return offscreenFlags;
}
bool OffscreenUi::navigationFocused() {
return getFlags(getRootContext())->isNavigationFocused();
return offscreenFlags->isNavigationFocused();
}
void OffscreenUi::setNavigationFocused(bool focused) {
getFlags(getRootContext())->setNavigationFocused(focused);
offscreenFlags->setNavigationFocused(focused);
}
#include "OffscreenUi.moc"

View file

@ -25,7 +25,7 @@ class OffscreenUi : public OffscreenQmlSurface, public Dependency {
public:
OffscreenUi();
virtual ~OffscreenUi();
virtual void create(QOpenGLContext* context) override;
void show(const QUrl& url, const QString& name, std::function<void(QQmlContext*, QObject*)> f = [](QQmlContext*, QObject*) {});
void toggle(const QUrl& url, const QString& name, std::function<void(QQmlContext*, QObject*)> f = [](QQmlContext*, QObject*) {});
bool shouldSwallowShortcut(QEvent* event);

View file

@ -29,36 +29,10 @@
static const char* const URL_PROPERTY = "source";
static const QRegExp HIFI_URL_PATTERN { "^hifi://" };
class UrlFixer : public QObject {
Q_OBJECT
public:
Q_INVOKABLE QString fixupUrl(const QString& originalUrl) {
static const QString ACCESS_TOKEN_PARAMETER = "access_token";
static const QString ALLOWED_HOST = "metaverse.highfidelity.com";
QString result = originalUrl;
QUrl url(originalUrl);
QUrlQuery query(url);
if (url.host() == ALLOWED_HOST && query.allQueryItemValues(ACCESS_TOKEN_PARAMETER).empty()) {
qDebug() << "Updating URL with auth token";
AccountManager& accountManager = AccountManager::getInstance();
query.addQueryItem(ACCESS_TOKEN_PARAMETER, accountManager.getAccountInfo().getAccessToken().token);
url.setQuery(query.query());
result = url.toString();
}
return result;
}
};
static UrlFixer URL_FIXER;
// Method called by Qt scripts to create a new web window in the overlay
QScriptValue QmlWebWindowClass::constructor(QScriptContext* context, QScriptEngine* engine) {
return QmlWindowClass::internalConstructor("QmlWebWindow.qml", context, engine,
[&](QQmlContext* context, QObject* object) {
context->setContextProperty("urlFixer", &URL_FIXER);
return new QmlWebWindowClass(object);
});
[&](QQmlContext* context, QObject* object) { return new QmlWebWindowClass(object); });
}
QmlWebWindowClass::QmlWebWindowClass(QObject* qmlWindow) : QmlWindowClass(qmlWindow) {
@ -100,6 +74,4 @@ void QmlWebWindowClass::setURL(const QString& urlString) {
QMetaObject::invokeMethod(this, "setURL", Qt::QueuedConnection, Q_ARG(QString, urlString));
}
_qmlWindow->setProperty(URL_PROPERTY, urlString);
}
#include "QmlWebWindowClass.moc"
}