From 79c081e2ea35eb36c66cd7fd7656ef5d98163b09 Mon Sep 17 00:00:00 2001 From: Kai Uwe Broulik Date: Mon, 25 Apr 2016 11:38:07 +0200 Subject: [PATCH] [OSD] Load QML file on demand Differential Revision: https://phabricator.kde.org/D1475 --- shell/osd.cpp | 67 ++++++++++++++++++++++++++++++--------------------- shell/osd.h | 9 ++++--- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/shell/osd.cpp b/shell/osd.cpp index dc2dcc902..290a661ce 100644 --- a/shell/osd.cpp +++ b/shell/osd.cpp @@ -31,27 +31,9 @@ Osd::Osd(ShellCorona *corona) : QObject(corona) + , m_osdPath(corona->lookAndFeelPackage().filePath("osdmainscript")) { - const QString osdPath = corona->lookAndFeelPackage().filePath("osdmainscript"); - if (osdPath.isEmpty()) { - qWarning() << "Failed to load the OSD QML file file from" << osdPath; - return; - } - - m_osdObject = new KDeclarative::QmlObject(this); - m_osdObject->setSource(QUrl::fromLocalFile(osdPath)); - if (m_osdObject->status() != QQmlComponent::Ready) { - qWarning() << "Failed to load OSD QML file"; - return; - } - - m_timeout = m_osdObject->rootObject()->property("timeout").toInt(); - QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/kde/osdService"), this, QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals); - - m_osdTimer = new QTimer(this); - m_osdTimer->setSingleShot(true); - connect(m_osdTimer, &QTimer::timeout, this, &Osd::hideOsd); } Osd::~Osd() @@ -106,14 +88,46 @@ void Osd::virtualDesktopChanged(const QString ¤tVirtualDesktopName) showText(QString(), currentVirtualDesktopName); } +bool Osd::init() +{ + if (m_osdObject && m_osdObject->rootObject()) { + return true; + } + + if (m_osdPath.isEmpty()) { + return false; + } + + if (!m_osdObject) { + m_osdObject = new KDeclarative::QmlObject(this); + } + + m_osdObject->setSource(QUrl::fromLocalFile(m_osdPath)); + + if (m_osdObject->status() != QQmlComponent::Ready) { + qWarning() << "Failed to load OSD QML file" << m_osdPath; + return false; + } + + m_timeout = m_osdObject->rootObject()->property("timeout").toInt(); + + if (!m_osdTimer) { + m_osdTimer = new QTimer(this); + m_osdTimer->setSingleShot(true); + connect(m_osdTimer, &QTimer::timeout, this, &Osd::hideOsd); + } + + return true; +} + void Osd::showProgress(const QString &icon, const int percent, const QString &additionalText) { - auto *rootObject = m_osdObject->rootObject(); - if (!rootObject) { - qWarning() << "Failed to load OSD QML file"; + if (!init()) { return; } + auto *rootObject = m_osdObject->rootObject(); + int value = qBound(0, percent, 100); rootObject->setProperty("osdValue", value); rootObject->setProperty("osdAdditionalText", additionalText); @@ -126,12 +140,12 @@ void Osd::showProgress(const QString &icon, const int percent, const QString &ad void Osd::showText(const QString &icon, const QString &text) { - auto *rootObject = m_osdObject->rootObject(); - if (!rootObject) { - qWarning() << "Failed to load OSD QML file"; + if (!init()) { return; } + auto *rootObject = m_osdObject->rootObject(); + rootObject->setProperty("showingProgress", false); rootObject->setProperty("osdValue", text); rootObject->setProperty("icon", icon); @@ -145,9 +159,6 @@ void Osd::showOsd() m_osdTimer->stop(); auto *rootObject = m_osdObject->rootObject(); - if (!rootObject) { - return; - } // if our OSD understands animating the opacity, do it; // otherwise just show it to not break existing lnf packages diff --git a/shell/osd.h b/shell/osd.h index 7dc080a46..9cc54acd7 100644 --- a/shell/osd.h +++ b/shell/osd.h @@ -54,13 +54,16 @@ private Q_SLOTS: void hideOsd(); private: + bool init(); + void showProgress(const QString &icon, const int percent, const QString &additionalText = QString()); void showText(const QString &icon, const QString &text); void showOsd(); - KDeclarative::QmlObject *m_osdObject; - QTimer *m_osdTimer; - int m_timeout; + QString m_osdPath; + KDeclarative::QmlObject *m_osdObject = nullptr; + QTimer *m_osdTimer = nullptr; + int m_timeout = 0; }; #endif // OSD_H