Changeset View
Changeset View
Standalone View
Standalone View
kcm/output_identifier.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | Copyright © 2019 Roman Gilg <subdiff@gmail.com> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or modify | ||||
5 | it under the terms of the GNU General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or | ||||
7 | (at your option) any later version. | ||||
8 | | ||||
9 | This program is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | GNU General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU General Public License | ||||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
16 | *********************************************************************/ | ||||
17 | #include "output_identifier.h" | ||||
18 | | ||||
19 | #include "../common/utils.h" | ||||
20 | | ||||
21 | #include <kscreen/output.h> | ||||
22 | | ||||
23 | #include <QStandardPaths> | ||||
24 | #include <QTimer> | ||||
25 | #include <QQuickItem> | ||||
26 | #include <QQuickView> | ||||
27 | | ||||
28 | #define QML_PATH "kpackage/kcms/kcm_kscreen/contents/ui/" | ||||
29 | | ||||
30 | OutputIdentifier::OutputIdentifier(KScreen::ConfigPtr config, QObject *parent) | ||||
31 | : QObject(parent) | ||||
32 | { | ||||
33 | | ||||
34 | const QString qmlPath = | ||||
35 | QStandardPaths::locate(QStandardPaths::GenericDataLocation, | ||||
36 | QStringLiteral(QML_PATH | ||||
37 | "OutputIdentifier.qml")); | ||||
38 | | ||||
39 | for (const auto &output : config->connectedOutputs()) { | ||||
40 | if (!output->currentMode()) { | ||||
41 | continue; | ||||
42 | } | ||||
43 | | ||||
44 | const KScreen::ModePtr mode = output->currentMode(); | ||||
45 | | ||||
46 | auto *view = new QQuickView(); | ||||
47 | | ||||
48 | view->setFlags(Qt::X11BypassWindowManagerHint | Qt::FramelessWindowHint); | ||||
49 | view->setResizeMode(QQuickView::SizeViewToRootObject); | ||||
50 | view->setSource(QUrl::fromLocalFile(qmlPath)); | ||||
51 | view->installEventFilter(this); | ||||
52 | | ||||
53 | QQuickItem *rootObj = view->rootObject(); | ||||
54 | if (!rootObj) { | ||||
55 | continue; | ||||
56 | } | ||||
57 | | ||||
58 | QSize deviceSize, logicalSize; | ||||
59 | if (output->isHorizontal()) { | ||||
60 | deviceSize = mode->size(); | ||||
61 | } else { | ||||
62 | deviceSize = QSize(mode->size().height(), mode->size().width()); | ||||
63 | } | ||||
64 | if (config->supportedFeatures() & KScreen::Config::Feature::PerOutputScaling) { | ||||
65 | // no scale adjustment needed on Wayland | ||||
66 | logicalSize = deviceSize; | ||||
67 | } else { | ||||
68 | logicalSize = deviceSize / view->effectiveDevicePixelRatio(); | ||||
69 | } | ||||
70 | | ||||
71 | rootObj->setProperty("outputName", Utils::outputName(output)); | ||||
72 | rootObj->setProperty("modeName", Utils::sizeToString(deviceSize)); | ||||
73 | view->setProperty("screenSize", QRect(output->pos(), logicalSize)); | ||||
74 | m_views << view; | ||||
75 | } | ||||
76 | | ||||
77 | for (auto *view : m_views) { | ||||
78 | view->show(); | ||||
79 | } | ||||
80 | QTimer::singleShot(2500, this, &OutputIdentifier::identifiersFinished); | ||||
81 | } | ||||
82 | | ||||
83 | OutputIdentifier::~OutputIdentifier() | ||||
84 | { | ||||
85 | qDeleteAll(m_views); | ||||
86 | } | ||||
87 | | ||||
88 | bool OutputIdentifier::eventFilter(QObject* object, QEvent* event) | ||||
89 | { | ||||
90 | if (event->type() == QEvent::Resize) { | ||||
91 | if (m_views.contains(qobject_cast<QQuickView*>(object))) { | ||||
92 | QResizeEvent *e = static_cast<QResizeEvent*>(event); | ||||
93 | const QRect screenSize = object->property("screenSize").toRect(); | ||||
94 | QRect geometry(QPoint(0, 0), e->size()); | ||||
95 | geometry.moveCenter(screenSize.center()); | ||||
96 | static_cast<QQuickView*>(object)->setGeometry(geometry); | ||||
97 | } | ||||
98 | } | ||||
99 | return QObject::eventFilter(object, event); | ||||
100 | } |