File indexing completed on 2024-07-14 04:50:07

0001 /**
0002  * SPDX-FileCopyrightText: 2019 Aleix Pol Gonzalez <aleixpol@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "presenterplugin.h"
0008 
0009 #include <KLocalizedString>
0010 #include <KPluginFactory>
0011 
0012 #include <QDBusConnection>
0013 #include <QDebug>
0014 #include <QQmlError>
0015 #include <QQuickItem>
0016 #include <QQuickView>
0017 #include <QtGlobal>
0018 
0019 #include "plugin_presenter_debug.h"
0020 #include <QScreen>
0021 #include <core/daemon.h>
0022 #include <core/device.h>
0023 
0024 K_PLUGIN_CLASS_WITH_JSON(PresenterPlugin, "kdeconnect_presenter.json")
0025 
0026 class PresenterView : public QQuickView
0027 {
0028 public:
0029     PresenterView()
0030     {
0031         Qt::WindowFlags windowFlags = Qt::WindowFlags(Qt::WA_TranslucentBackground | Qt::WindowDoesNotAcceptFocus | Qt::WindowFullScreen
0032                                                       | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::Tool);
0033 #ifdef Q_OS_WIN
0034         windowFlags |= Qt::WindowTransparentForInput;
0035 #endif
0036         setFlags(windowFlags);
0037 #if QT_VERSION_MAJOR < 6
0038         setClearBeforeRendering(true);
0039 #endif
0040         setColor(QColor(Qt::transparent));
0041 
0042         setResizeMode(QQuickView::SizeViewToRootObject);
0043         setSource(QUrl(QStringLiteral("qrc:/presenter/Presenter.qml")));
0044 
0045         const auto ourErrors = errors();
0046         for (const auto &error : ourErrors) {
0047             qCWarning(KDECONNECT_PLUGIN_PRESENTER) << "error" << error.description() << error.url() << error.line() << error.column();
0048         }
0049     }
0050 };
0051 
0052 PresenterPlugin::PresenterPlugin(QObject *parent, const QVariantList &args)
0053     : KdeConnectPlugin(parent, args)
0054     , m_view(nullptr)
0055     , m_timer(new QTimer(this))
0056 {
0057     m_timer->setInterval(500);
0058     m_timer->setSingleShot(true);
0059 }
0060 
0061 void PresenterPlugin::receivePacket(const NetworkPacket &np)
0062 {
0063     if (np.get<bool>(QStringLiteral("stop"), false)) {
0064         delete m_view;
0065         m_view = nullptr;
0066         return;
0067     }
0068 
0069     if (!m_view) {
0070         m_view = new PresenterView;
0071         m_xPos = 0.5f;
0072         m_yPos = 0.5f;
0073         m_view->showFullScreen();
0074         connect(this, &QObject::destroyed, m_view, &QObject::deleteLater);
0075         connect(m_timer, &QTimer::timeout, m_view, &QObject::deleteLater);
0076     }
0077 
0078     QSize screenSize = m_view->screen()->size();
0079     float ratio = float(screenSize.width()) / screenSize.height();
0080 
0081     m_xPos += np.get<float>(QStringLiteral("dx"));
0082     m_yPos += np.get<float>(QStringLiteral("dy")) * ratio;
0083     m_xPos = qBound(0.f, m_xPos, 1.f);
0084     m_yPos = qBound(0.f, m_yPos, 1.f);
0085 
0086     m_timer->start();
0087 
0088     QQuickItem *object = m_view->rootObject();
0089     object->setProperty("xPos", m_xPos);
0090     object->setProperty("yPos", m_yPos);
0091 }
0092 
0093 #include "moc_presenterplugin.cpp"
0094 #include "presenterplugin.moc"