on 43f0793 help sign in
unity8/tests/mocks/WindowManager/MockScreens.cpp
208 lines 6.4 kB history raw
1/*
2 * Copyright (C) 2016-2017 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "MockScreens.h"
18#include "ScreenWindow.h"
20// qtmirserver
21#include <qtmir/screen.h>
23#include <QGuiApplication>
24#include <QWindow>
25#include <QWeakPointer>
27namespace {
29QWeakPointer<MockScreens> m_screens;
31class MockScreen : public qtmir::Screen
33 Q_OBJECT
34public:
35 MockScreen()
36 {
37 m_sizes.append(new qtmir::ScreenMode(50, QSize(640,480)));
38 m_sizes.append(new qtmir::ScreenMode(60, QSize(1280,1024)));
39 m_sizes.append(new qtmir::ScreenMode(60, QSize(1440,900)));
40 m_sizes.append(new qtmir::ScreenMode(60, QSize(1920,1080)));
41 m_physicalSize = QSize(800,568);
42 }
43 ~MockScreen() {
44 qDeleteAll(m_sizes);
45 m_sizes.clear();
46 }
48 void connectToWindow(QWindow* w)
49 {
50 if (m_connectedWindow == w) return;
52 if (m_connectedWindow) {
53 disconnect(m_connectedWindow.data());
54 m_sizes.takeFirst()->deleteLater();
55 }
57 m_connectedWindow = w;
59 if (w) {
60 connect(w, &ScreenWindow::heightChanged, this, [this](int height) {
61 m_sizes.first()->size.rheight() = height;
62 Q_EMIT availableModesChanged();
64 });
65 connect(w, &ScreenWindow::widthChanged, this, [this](int width) {
66 m_sizes.first()->size.rwidth() = width;
67 Q_EMIT availableModesChanged();
68 });
70 m_sizes.push_front(new qtmir::ScreenMode(50, w->size()));
71 Q_EMIT availableModesChanged();
72 }
73 }
75 qtmir::OutputId outputId() const override { return m_id; }
76 bool used() const override { return m_used; }
77 QString name() const override { return m_name; }
78 float scale() const override { return m_scale; }
79 QSizeF physicalSize() const override { return m_physicalSize; }
80 qtmir::FormFactor formFactor() const override { return m_formFactor; }
81 qtmir::OutputTypes outputType() const override { return m_outputType; }
82 MirPowerMode powerMode() const override { return m_powerMode; }
83 Qt::ScreenOrientation orientation() const override { return m_orientation; }
84 QPoint position() const override { return m_position; }
85 uint currentModeIndex() const override { return m_currentModeIndex; }
86 bool isActive() const override { return m_active; }
88 QQmlListProperty<qtmir::ScreenMode> availableModes() override {
89 return QQmlListProperty<qtmir::ScreenMode>(this, m_sizes);
90 }
92 void setActive(bool active) override {
93 if (m_active != active) {
94 m_active = active;
95 Q_EMIT activeChanged(m_active);
96 }
97 }
99 QScreen* qscreen() const override {
100 if (qGuiApp->topLevelWindows().count() > 0) {
101 return qGuiApp->topLevelWindows()[0]->screen();
102 }
103 return qGuiApp->primaryScreen();
104 }
106 qtmir::ScreenConfiguration *beginConfiguration() const override {
107 auto config = new qtmir::ScreenConfiguration;
108 config->valid = true;
109 config->id = m_id;
110 config->used = m_used;
111 config->topLeft = m_position;
112 config->currentModeIndex = m_currentModeIndex;
113 config->powerMode = m_powerMode;
114 config->scale = m_scale;
115 config->formFactor = m_formFactor;
116 return config;
117 }
119 bool applyConfiguration(qtmir::ScreenConfiguration *configuration) override {
120 m_used = configuration->used;
121 m_position = configuration->topLeft;
122 m_currentModeIndex = configuration->currentModeIndex;
123 m_powerMode = configuration->powerMode;
124 m_scale = configuration->scale;
125 m_formFactor = configuration->formFactor;
126 return true;
127 }
129public:
130 qtmir::OutputId m_id{0};
131 bool m_active{false};
132 bool m_used{true};
133 QString m_name;
134 qtmir::OutputTypes m_outputType{qtmir::Unknown};
135 MirPowerMode m_powerMode{mir_power_mode_on};
136 Qt::ScreenOrientation m_orientation{Qt::PrimaryOrientation};
137 float m_scale{1.0};
138 qtmir::FormFactor m_formFactor{qtmir::FormFactorMonitor};
139 QPoint m_position;
140 uint m_currentModeIndex{0};
141 QList<qtmir::ScreenMode*> m_sizes;
142 QSizeF m_physicalSize;
144 QPointer<QWindow> m_connectedWindow;
145};
148MockScreens::MockScreens()
150 bool ok = false;
151 int screenCount = qEnvironmentVariableIntValue("UNITY_MOCK_SCREEN_COUNT", &ok);
152 if (!ok) screenCount = 1;
153 QPoint lastPoint(0,0);
154 for (int i = 0; i < screenCount; ++i) {
155 auto screen = new MockScreen();
156 screen->m_id = qtmir::OutputId{i};
157 screen->m_active = i == 0;
158 screen->m_name = QString("Monitor %1").arg(i);
159 screen->m_position = QPoint(lastPoint.x(), lastPoint.y());
160 screen->m_currentModeIndex = 3;
161 m_mocks.append(screen);
163 lastPoint.rx() += screen->m_sizes[screen->m_currentModeIndex]->size.width();
164 }
167MockScreens::~MockScreens()
169 qDeleteAll(m_mocks);
170 m_mocks.clear();
173QVector<qtmir::Screen *> MockScreens::screens() const
175 return m_mocks;
178qtmir::Screen *MockScreens::activeScreen() const
180 Q_FOREACH(auto screen, m_mocks) {
181 if (screen->isActive()) return screen;
182 }
183 return nullptr;
186QSharedPointer<MockScreens> MockScreens::instance()
188 if (!m_screens) {
189 QSharedPointer<MockScreens> screens(new MockScreens());
190 m_screens = screens.toWeakRef();
191 return screens;
192 } else {
193 return m_screens.lock();
194 }
197void MockScreens::connectWindow(ScreenWindow *w)
199 ConcreteScreen* screen = w->screenWrapper();
200 if (!screen) return;
202 auto mockScreen = qobject_cast<MockScreen*>(screen->wrapped());
203 if (mockScreen) {
204 mockScreen->connectToWindow(w);
205 }
208#include "MockScreens.moc"