File indexing completed on 2024-05-12 05:35:54

0001 /*
0002     SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     Work sponsored by Technische Universität Dresden:
0005     SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB a KDAB Group company <info@kdab.com>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "kcmtouchscreen.h"
0011 #include "inputdevice.h"
0012 
0013 #include <KConfigGroup>
0014 #include <KLocalizedString>
0015 #include <KPluginFactory>
0016 #include <QGuiApplication>
0017 #include <QScreen>
0018 #include <QStandardItemModel>
0019 
0020 using namespace Qt::StringLiterals;
0021 
0022 K_PLUGIN_CLASS_WITH_JSON(Touchscreen, "kcm_touchscreen.json")
0023 
0024 class OutputsModel : public QStandardItemModel
0025 {
0026     Q_OBJECT
0027 public:
0028     OutputsModel()
0029     {
0030         setItemRoleNames({
0031             {Qt::DisplayRole, "display"},
0032             {Qt::UserRole, "name"},
0033         });
0034 
0035         reset();
0036 
0037         connect(qGuiApp, &QGuiApplication::screenAdded, this, &OutputsModel::reset);
0038         connect(qGuiApp, &QGuiApplication::screenRemoved, this, &OutputsModel::reset);
0039     }
0040 
0041     void reset()
0042     {
0043         clear();
0044 
0045         auto screens = qGuiApp->screens();
0046         auto it = new QStandardItem(i18n("Automatic"));
0047         appendRow(it);
0048 
0049         for (auto screen : screens) {
0050             auto geo = screen->geometry();
0051             auto it = new QStandardItem(i18nc("model - (x,y widthxheight)",
0052                                               "%1 - (%2,%3 %4×%5)",
0053                                               screen->model(),
0054                                               QString::number(geo.x()),
0055                                               QString::number(geo.y()),
0056                                               QString::number(geo.width()),
0057                                               QString::number(geo.height())));
0058             it->setData(screen->name(), Qt::UserRole);
0059             appendRow(it);
0060         }
0061     }
0062 
0063     Q_SCRIPTABLE QString outputNameAt(int row) const
0064     {
0065         return item(row)->data(Qt::UserRole).toString();
0066     }
0067     Q_SCRIPTABLE int rowForOutputName(const QString &outputName)
0068     {
0069         for (int i = 0, c = rowCount(); i < c; ++i) {
0070             if (item(i)->data(Qt::UserRole) == outputName) {
0071                 return i;
0072             }
0073         }
0074 
0075         return 0;
0076     }
0077 };
0078 
0079 Touchscreen::Touchscreen(QObject *parent, const KPluginMetaData &metaData)
0080     : KQuickManagedConfigModule(parent, metaData)
0081     , m_touchscreensModel(new DevicesModel("touch", this))
0082 {
0083     const char *uri = "org.kde.plasma.touchscreen.kcm";
0084     qmlRegisterType<OutputsModel>(uri, 1, 0, "OutputsModel");
0085     qmlRegisterUncreatableType<InputDevice>(uri, 1, 0, "InputDevice", u"Should be fetched from kcm.touchscreensModel"_s);
0086 
0087     connect(m_touchscreensModel, &DevicesModel::needsSaveChanged, this, &Touchscreen::refreshNeedsSave);
0088 }
0089 
0090 Touchscreen::~Touchscreen() = default;
0091 
0092 void Touchscreen::refreshNeedsSave()
0093 {
0094     setNeedsSave(isSaveNeeded());
0095 }
0096 
0097 bool Touchscreen::isSaveNeeded() const
0098 {
0099     return m_touchscreensModel->isSaveNeeded();
0100 }
0101 
0102 bool Touchscreen::isDefaults() const
0103 {
0104     return m_touchscreensModel->isDefaults();
0105 }
0106 
0107 void Touchscreen::load()
0108 {
0109     m_touchscreensModel->load();
0110 }
0111 
0112 void Touchscreen::save()
0113 {
0114     m_touchscreensModel->save();
0115 }
0116 
0117 void Touchscreen::defaults()
0118 {
0119     m_touchscreensModel->defaults();
0120 }
0121 
0122 DevicesModel *Touchscreen::touchscreensModel() const
0123 {
0124     return m_touchscreensModel;
0125 }
0126 
0127 #include "kcmtouchscreen.moc"