1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
/*
Work sponsored by the LiMux project of the city of Munich:
SPDX-FileCopyrightText: 2018 Kai Uwe Broulik <kde@broulik.de>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "kscreenapplet.h"
#include <QMetaEnum>
#include <QQmlEngine> // for qmlRegisterType
#include <KScreen/Config>
#include <KScreen/ConfigMonitor>
#include <KScreen/GetConfigOperation>
#include <KScreen/Mode>
#include <KScreen/Output>
#include <KScreen/SetConfigOperation>
#include <algorithm>
K_PLUGIN_CLASS_WITH_JSON(KScreenApplet, "metadata.json")
KScreenApplet::KScreenApplet(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
: Plasma::Applet(parent, data, args)
{
qmlRegisterUncreatableMetaObject(KScreen::OsdAction::staticMetaObject,
"org.kde.private.kscreen",
1,
0,
"OsdAction",
QStringLiteral("Can't create OsdAction"));
}
KScreenApplet::~KScreenApplet() = default;
void KScreenApplet::init()
{
connect(new KScreen::GetConfigOperation(KScreen::GetConfigOperation::NoEDID),
&KScreen::ConfigOperation::finished,
this,
[this](KScreen::ConfigOperation *op) {
m_screenConfiguration = qobject_cast<KScreen::GetConfigOperation *>(op)->config();
KScreen::ConfigMonitor::instance()->addConfig(m_screenConfiguration);
connect(KScreen::ConfigMonitor::instance(), &KScreen::ConfigMonitor::configurationChanged, this, &KScreenApplet::checkOutputs);
checkOutputs();
});
}
int KScreenApplet::connectedOutputCount() const
{
return m_connectedOutputCount;
}
void KScreenApplet::applyLayoutPreset(KScreen::OsdAction::Action action)
{
KScreen::OsdAction::applyAction(m_screenConfiguration, action);
}
void KScreenApplet::checkOutputs()
{
if (!m_screenConfiguration) {
return;
}
const int oldConnectedOutputCount = m_connectedOutputCount;
const auto outputs = m_screenConfiguration->outputs();
m_connectedOutputCount = std::count_if(outputs.begin(), outputs.end(), [](const KScreen::OutputPtr &output) {
return output->isConnected();
});
if (m_connectedOutputCount != oldConnectedOutputCount) {
emit connectedOutputCountChanged();
}
}
QVariant KScreenApplet::availableActions()
{
auto actions = KScreen::OsdAction::availableActions();
QList<KScreen::OsdAction> ret;
ret.reserve(actions.size() - 1);
for (const auto &action : actions) {
if (action.action != KScreen::OsdAction::NoAction) {
ret.append(action);
}
}
// Need to wrap it in a QVariant, otherwise QML doesn't like the return type
return QVariant::fromValue(ret);
}
#include "kscreenapplet.moc"
#include "moc_kscreenapplet.cpp"
|