on 4e8f28f help sign in
205 lines 5.7 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(const QSharedPointer<qtmir::Screens>& model)
31 : m_wrapped(model)
33 connect(m_wrapped.data(), &qtmir::Screens::screenAdded, this, &Screens::onScreenAdded);
34 connect(m_wrapped.data(), &qtmir::Screens::screenRemoved, this, &Screens::onScreenRemoved);
35 connect(m_wrapped.data(), &qtmir::Screens::activeScreenChanged, this, &Screens::activeScreenChanged);
37 Q_FOREACH(qtmir::Screen* screen, m_wrapped->screens()) {
38 auto screenWrapper(new Screen(screen));
39 QQmlEngine::setObjectOwnership(screenWrapper, QQmlEngine::CppOwnership);
40 m_screens.push_back(screenWrapper);
41 }
44Screens::~Screens()
46 qDeleteAll(m_screens);
47 m_screens.clear();
50Screens::Screens(const Screens &other)
51 : QAbstractListModel(nullptr)
52 , m_wrapped(other.m_wrapped)
56QHash<int, QByteArray> Screens::roleNames() const
58 QHash<int, QByteArray> roles;
59 roles[ScreenRole] = "screen";
60 return roles;
63QVariant Screens::data(const QModelIndex &index, int role) const
65 if (!index.isValid() || index.row() >= m_screens.size()) {
66 return QVariant();
67 }
69 switch(role) {
70 case ScreenRole:
71 return QVariant::fromValue(m_screens.at(index.row()));
72 } // switch
74 return QVariant();
77int Screens::rowCount(const QModelIndex &) const
79 return count();
82int Screens::count() const
84 return m_screens.size();
87QVariant Screens::activeScreen() const
89 for (int i = 0; i < m_screens.count(); i++) {
90 if (m_screens[i]->isActive()) return i;
91 }
92 return QVariant();
95ScreensProxy *Screens::createProxy()
97 return new ScreensProxy(this);
100void Screens::sync(Screens *proxy)
102 if (!proxy) return;
104 const auto& proxyList = proxy->list();
105 for (int i = 0; i < m_screens.count() && i < proxyList.count(); ++i) {
106 m_screens[i]->sync(proxyList[i]);
107 }
109 // need to clean up all the workspaces we unassigned.
110 WorkspaceManager::instance()->destroyFloatingWorkspaces();
113void Screens::activateScreen(const QVariant& vindex)
115 bool ok = false;
116 int index = vindex.toInt(&ok);
117 if (!ok || index < 0 || m_screens.count() <= index) return;
119 auto screen = m_screens.at(index);
120 screen->setActive(true);
123void Screens::onScreenAdded(qtmir::Screen *added)
125 Q_FOREACH(auto screenWrapper, m_screens) {
126 if (screenWrapper->wrapped() == added) return;
127 }
129 beginInsertRows(QModelIndex(), count(), count());
130 auto screenWrapper(new Screen(added));
131 QQmlEngine::setObjectOwnership(screenWrapper, QQmlEngine::CppOwnership);
132 m_screens.push_back(screenWrapper);
133 endInsertRows();
134 Q_EMIT screenAdded(screenWrapper);
135 Q_EMIT countChanged();
138void Screens::onScreenRemoved(qtmir::Screen *removed)
140 int index = 0;
141 QMutableVectorIterator<Screen*> iter(m_screens);
142 while(iter.hasNext()) {
143 auto screenWrapper = iter.next();
144 if (screenWrapper->wrapped() == removed) {
146 beginRemoveRows(QModelIndex(), index, index);
147 iter.remove();
148 endRemoveRows();
150 Q_EMIT screenRemoved(screenWrapper);
151 Q_EMIT countChanged();
153 screenWrapper->deleteLater();
154 break;
155 }
156 index++;
157 }
160ScreensProxy::ScreensProxy(Screens * const screens)
161 : Screens(*screens)
162 , m_original(screens)
164 connect(screens, &Screens::screenAdded, this, [this](Screen *added) {
165 Q_FOREACH(auto screen, m_screens) {
166 auto proxy = static_cast<ScreenProxy*>(screen);
167 if (proxy->proxyObject() == added) return;
168 }
170 beginInsertRows(QModelIndex(), count(), count());
171 auto screenWrapper(new ScreenProxy(added));
172 QQmlEngine::setObjectOwnership(screenWrapper, QQmlEngine::CppOwnership);
173 m_screens.push_back(screenWrapper);
174 endInsertRows();
175 Q_EMIT screenAdded(screenWrapper);
176 Q_EMIT countChanged();
177 });
179 connect(screens, &Screens::screenRemoved, this, [this](Screen *removed) {
180 int index = 0;
181 QMutableVectorIterator<Screen*> iter(m_screens);
182 while(iter.hasNext()) {
183 auto proxy = static_cast<ScreenProxy*>(iter.next());
184 if (proxy->proxyObject() == removed) {
186 beginRemoveRows(QModelIndex(), index, index);
187 iter.remove();
188 endRemoveRows();
190 Q_EMIT screenRemoved(proxy);
191 Q_EMIT countChanged();
193 delete proxy;
194 break;
195 }
196 index++;
197 }
198 });
200 Q_FOREACH(Screen* screen, screens->list()) {
201 auto screenWrapper(new ScreenProxy(screen));
202 QQmlEngine::setObjectOwnership(screenWrapper, QQmlEngine::CppOwnership);
203 m_screens.push_back(new ScreenProxy(screen));
204 }