Commit 0f4b1d8d authored by Sebastian Kügler's avatar Sebastian Kügler Committed by Daniel Vrátil
Browse files

clean up and improve osd code

parent 1fcb7bd5
......@@ -22,14 +22,13 @@
#include <KScreen/Mode>
#include <QDBusConnection>
#include <QLoggingCategory>
#include <QTimer>
#include <QWindow>
// #include <QWindow>
#include <QStandardPaths>
#include <QDebug>
#include <QUrl>
#include <QQuickItem>
// #include <QUrl>
// #include <QQuickItem>
#include <KDeclarative/QmlObject>
namespace KScreen {
......@@ -37,25 +36,35 @@ namespace KScreen {
Osd::Osd(QObject *parent)
: QObject(parent)
, m_osdPath(QStandardPaths::locate(QStandardPaths::QStandardPaths::GenericDataLocation, QStringLiteral("kded_kscreen/qml/Osd.qml")))
, m_osdObject(new KDeclarative::QmlObject(this))
{
init();
}
if (m_osdPath.isEmpty()) {
qWarning() << "Failed to find OSD QML file" << m_osdPath;
}
Osd::~Osd()
{
m_osdObject->setSource(QUrl::fromLocalFile(m_osdPath));
if (m_osdObject->status() != QQmlComponent::Ready) {
qWarning() << "Failed to load OSD QML file" << m_osdPath;
return;
}
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);
}
}
bool Osd::setRootProperty(const char* name, const QVariant& value)
Osd::~Osd()
{
return m_osdObject->rootObject()->setProperty(name, value);
}
void Osd::showOutputIdentifier(const KScreen::OutputPtr output)
{
m_output = output;
if (!init()) {
return;
}
m_outputGeometry = output->geometry();
auto *rootObject = m_osdObject->rootObject();
auto mode = output->currentMode();
......@@ -69,12 +78,13 @@ void Osd::showOutputIdentifier(const KScreen::OutputPtr output)
rootObject->setProperty("modeName", Utils::sizeToString(realSize));
rootObject->setProperty("outputName", Utils::outputName(output));
rootObject->setProperty("icon", QStringLiteral("preferences-desktop-display-randr"));
//QTimer::singleShot(200, this, &Osd::showOsd);
showOsd();
}
void Osd::updatePosition()
{
if (m_output == nullptr) {
if (!m_outputGeometry.isValid()) {
return;
}
......@@ -82,82 +92,15 @@ void Osd::updatePosition()
const int dialogWidth = rootObject->property("width").toInt();
const int dialogHeight = rootObject->property("height").toInt();
const int relx = m_output->pos().x();
const int rely = m_output->pos().y();
const int pos_x = relx + (m_output->geometry().width() - dialogWidth) / 2;
const int pos_y = rely + (m_output->geometry().height() - dialogHeight) / 2;
const int relx = m_outputGeometry.x();
const int rely = m_outputGeometry.y();
const int pos_x = relx + (m_outputGeometry.width() - dialogWidth) / 2;
const int pos_y = rely + (m_outputGeometry.height() - dialogHeight) / 2;
rootObject->setProperty("x", pos_x);
rootObject->setProperty("y", pos_y);
}
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)
{
if (!init()) {
return;
}
auto *rootObject = m_osdObject->rootObject();
int value = qBound(0, percent, 100);
rootObject->setProperty("osdValue", value);
rootObject->setProperty("osdAdditionalText", additionalText);
rootObject->setProperty("showingProgress", true);
rootObject->setProperty("icon", icon);
emit osdProgress(icon, value, additionalText);
showOsd();
}
void Osd::showText(const QString &icon, const QString &text)
{
if (!init()) {
return;
}
auto *rootObject = m_osdObject->rootObject();
rootObject->setProperty("showingProgress", false);
rootObject->setProperty("osdValue", text);
rootObject->setProperty("icon", icon);
emit osdText(icon, text);
showOsd();
}
void Osd::showOsd()
{
m_osdTimer->stop();
......
......@@ -17,60 +17,48 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef OSD_H
#define OSD_H
#ifndef KSCREEN_OSD_H
#define KSCREEN_OSD_H
#include <QObject>
#include <QRect>
#include <QString>
#include <KScreen/Output>
class QmlObject;
namespace KDeclarative {
class QmlObject;
}
namespace Plasma {
}
class QTimer;
namespace KScreen {
class Osd : public QObject {
Q_OBJECT
public:
Osd(QObject *parent = nullptr);
~Osd() override;
bool setRootProperty(const char *name, const QVariant &value);
void showOutputIdentifier(const KScreen::OutputPtr output);
public Q_SLOTS:
void showText(const QString &icon, const QString &text);
Q_SIGNALS:
void osdProgress(const QString &icon, const int percent, const QString &additionalText);
void osdText(const QString &icon, const QString &text);
private Q_SLOTS:
void hideOsd();
private:
bool init();
void showProgress(const QString &icon, const int percent, const QString &additionalText = QString());
void showOsd();
void updatePosition();
QString m_osdPath;
QRect m_outputGeometry;
KDeclarative::QmlObject *m_osdObject = nullptr;
QTimer *m_osdTimer = nullptr;
int m_timeout = 0;
KScreen::OutputPtr m_output;
};
} // ns
#endif // OSD_H
#endif // KSCREEN_OSD_H
......@@ -33,7 +33,16 @@ OsdManager* OsdManager::m_instance = 0;
OsdManager::OsdManager(QObject *parent)
: QObject(parent)
, m_cleanupTimer(new QTimer(this))
{
// free up memory when the osd hasn't been used for more than 1 minute
m_cleanupTimer->setInterval(60000);
m_cleanupTimer->setSingleShot(true);
connect(m_cleanupTimer, &QTimer::timeout, this, [this]() {
qDeleteAll(m_osds.begin(), m_osds.end());
m_osds.clear();
});
QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/kde/kscreen/osdService"), this, QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals);
}
OsdManager::~OsdManager()
......@@ -64,23 +73,9 @@ void OsdManager::slotIdentifyOutputs(KScreen::ConfigOperation *op)
const KScreen::ConfigPtr config = qobject_cast<KScreen::GetConfigOperation*>(op)->config();
Q_FOREACH (const KScreen::OutputPtr &output, config->outputs()) {
connect(output.data(), &KScreen::Output::isConnectedChanged,
this, [this](){
KScreen::Output *output = qobject_cast<KScreen::Output*>(sender());
qDebug() << "outputConnectedChanged():" << output->name();
if (!output->isConnected() || !output->isEnabled() || !output->currentMode()) {
KScreen::Osd* osd = nullptr;
if (m_osds.keys().contains(output->name())) {
osd->deleteLater();
m_osds.remove(output->name());
}
}
}, Qt::UniqueConnection);
if (!output->isConnected() || !output->isEnabled() || !output->currentMode()) {
continue;
}
KScreen::Osd* osd = nullptr;
if (m_osds.keys().contains(output->name())) {
osd = m_osds.value(output->name());
......@@ -90,6 +85,7 @@ void OsdManager::slotIdentifyOutputs(KScreen::ConfigOperation *op)
}
osd->showOutputIdentifier(output);
}
m_cleanupTimer->start();
}
......
......@@ -22,6 +22,7 @@
#include <QObject>
#include <QString>
#include <QMap>
#include <QTimer>
class QmlObject;
......@@ -34,22 +35,21 @@ class Output;
class OsdManager : public QObject {
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kscreen.osdService")
public:
OsdManager(QObject *parent = nullptr);
~OsdManager() override;
static OsdManager* self();
public Q_SLOTS:
void showOutputIdentifiers();
private:
OsdManager(QObject *parent = nullptr);
void slotIdentifyOutputs(KScreen::ConfigOperation *op);
QMap<QString, KScreen::Osd*> m_osds;
static OsdManager* m_instance;
bool m_showing;
QTimer* m_cleanupTimer;
};
} // ns
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment