on bcbc40d help sign in
213 lines 5.9 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 "Screens.h"
18#include "Screen.h"
19#include "WorkspaceManager.h"
21// qtmirserver
22#include <qtmir/screens.h>
23#include <QGuiApplication>
24#include <QQmlEngine>
26// Qt
27#include <QScreen>
28#include <QWindow>
30Screens* Screens::m_self{nullptr};
32Screens::Screens(const QSharedPointer<qtmir::Screens>& model)
33 : m_wrapped(model)
35 m_self = this;
36 connect(m_wrapped.data(), &qtmir::Screens::screenAdded, this, &Screens::onScreenAdded);
37 connect(m_wrapped.data(), &qtmir::Screens::screenRemoved, this, &Screens::onScreenRemoved);
38 connect(m_wrapped.data(), &qtmir::Screens::activeScreenChanged, this, &Screens::activeScreenChanged);
40 Q_FOREACH(qtmir::Screen* screen, m_wrapped->screens()) {
41 auto screenWrapper(new Screen(screen));
42 QQmlEngine::setObjectOwnership(screenWrapper, QQmlEngine::CppOwnership);
43 m_screens.push_back(screenWrapper);
44 }
47Screens::~Screens()
49 qDeleteAll(m_screens);
50 m_screens.clear();
53Screens::Screens(const Screens &other)
54 : QAbstractListModel(nullptr)
55 , m_wrapped(other.m_wrapped)
59QHash<int, QByteArray> Screens::roleNames() const
61 QHash<int, QByteArray> roles;
62 roles[ScreenRole] = "screen";
63 return roles;
66QVariant Screens::data(const QModelIndex &index, int role) const
68 if (!index.isValid() || index.row() >= m_screens.size()) {
69 return QVariant();
70 }
72 switch(role) {
73 case ScreenRole:
74 return QVariant::fromValue(m_screens.at(index.row()));
75 } // switch
77 return QVariant();
80int Screens::rowCount(const QModelIndex &) const
82 return count();
85int Screens::count() const
87 return m_screens.size();
90QVariant Screens::activeScreen() const
92 for (int i = 0; i < m_screens.count(); i++) {
93 if (m_screens[i]->isActive()) return i;
94 }
95 return QVariant();
98Screens *Screens::self()
100 return Screens::m_self;
103ScreensProxy *Screens::createProxy()
105 return new ScreensProxy(this);
108void Screens::sync(Screens *proxy)
110 if (!proxy) return;
112 const auto& proxyList = proxy->list();
113 for (int i = 0; i < m_screens.count() && i < proxyList.count(); ++i) {
114 m_screens[i]->sync(proxyList[i]);
115 }
117 // need to clean up all the workspaces we unassigned.
118 WorkspaceManager::instance()->destroyFloatingWorkspaces();
121void Screens::activateScreen(const QVariant& vindex)
123 bool ok = false;
124 int index = vindex.toInt(&ok);
125 if (!ok || index < 0 || m_screens.count() <= index) return;
127 auto screen = m_screens.at(index);
128 screen->setActive(true);
131void Screens::onScreenAdded(qtmir::Screen *added)
133 Q_FOREACH(auto screenWrapper, m_screens) {
134 if (screenWrapper->wrapped() == added) return;
135 }
137 beginInsertRows(QModelIndex(), count(), count());
138 auto screenWrapper(new Screen(added));
139 QQmlEngine::setObjectOwnership(screenWrapper, QQmlEngine::CppOwnership);
140 m_screens.push_back(screenWrapper);
141 endInsertRows();
142 Q_EMIT screenAdded(screenWrapper);
143 Q_EMIT countChanged();
146void Screens::onScreenRemoved(qtmir::Screen *removed)
148 int index = 0;
149 QMutableVectorIterator<ScreenInterface*> iter(m_screens);
150 while(iter.hasNext()) {
151 auto screenWrapper = iter.next();
152 if (screenWrapper->wrapped() == removed) {
154 beginRemoveRows(QModelIndex(), index, index);
155 iter.remove();
156 endRemoveRows();
158 Q_EMIT screenRemoved(screenWrapper);
159 Q_EMIT countChanged();
161 screenWrapper->deleteLater();
162 break;
163 }
164 index++;
165 }
168ScreensProxy::ScreensProxy(Screens * const screens)
169 : Screens(*screens)
170 , m_original(screens)
172 connect(screens, &Screens::screenAdded, this, [this](ScreenInterface *added) {
173 Q_FOREACH(auto screen, m_screens) {
174 auto proxy = static_cast<ScreenProxy*>(screen);
175 if (proxy->proxyObject() == added) return;
176 }
178 beginInsertRows(QModelIndex(), count(), count());
179 auto screenWrapper(new ScreenProxy(added));
180 QQmlEngine::setObjectOwnership(screenWrapper, QQmlEngine::CppOwnership);
181 m_screens.push_back(screenWrapper);
182 endInsertRows();
183 Q_EMIT screenAdded(screenWrapper);
184 Q_EMIT countChanged();
185 });
187 connect(screens, &Screens::screenRemoved, this, [this](ScreenInterface *removed) {
188 int index = 0;
189 QMutableVectorIterator<ScreenInterface*> iter(m_screens);
190 while(iter.hasNext()) {
191 auto proxy = static_cast<ScreenProxy*>(iter.next());
192 if (proxy->proxyObject() == removed) {
194 beginRemoveRows(QModelIndex(), index, index);
195 iter.remove();
196 endRemoveRows();
198 Q_EMIT screenRemoved(proxy);
199 Q_EMIT countChanged();
201 delete proxy;
202 break;
203 }
204 index++;
205 }
206 });
208 Q_FOREACH(ScreenInterface* screen, screens->list()) {
209 auto screenWrapper(new ScreenProxy(screen));
210 QQmlEngine::setObjectOwnership(screenWrapper, QQmlEngine::CppOwnership);
211 m_screens.push_back(new ScreenProxy(screen));
212 }