on 747a319 help sign in
129 lines 3.5 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"
20// qtmirserver
21#include <qtmir/qtmir.h>
22#include <qtmir/screens.h>
23#include <QGuiApplication>
25// Qt
26#include <QScreen>
27#include <QWindow>
29Screens::Screens(QObject *parent)
30 : QAbstractListModel(parent)
31 , m_wrapped(qtmir::get_screen_model())
33 if (qGuiApp->platformName() != QLatin1String("mirserver")) {
34 qCritical("Not using 'mirserver' QPA plugin. Using qGuiApp may produce unknown results.");
35 }
37 connect(m_wrapped.data(), &qtmir::Screens::screenAdded, this, &Screens::onScreenAdded);
38 connect(m_wrapped.data(), &qtmir::Screens::screenRemoved, this, &Screens::onScreenRemoved);
39 connect(m_wrapped.data(), &qtmir::Screens::activeScreenChanged, this, &Screens::activeScreenChanged);
41 Q_FOREACH(qtmir::Screen* screen, m_wrapped->screens()) {
42 m_screenList.push_back(new Screen(screen));
43 }
46QHash<int, QByteArray> Screens::roleNames() const
48 QHash<int, QByteArray> roles;
49 roles[ScreenRole] = "screen";
50 return roles;
53QVariant Screens::data(const QModelIndex &index, int role) const
55 if (!index.isValid() || index.row() >= m_screenList.size()) {
56 return QVariant();
57 }
59 switch(role) {
60 case ScreenRole:
61 return QVariant::fromValue(m_screenList.at(index.row()));
62 } // switch
64 return QVariant();
67int Screens::rowCount(const QModelIndex &) const
69 return count();
72int Screens::count() const
74 return m_screenList.size();
77QVariant Screens::activeScreen() const
79 for (int i = 0; i < m_screenList.count(); i++) {
80 if (m_screenList[i]->isActive()) return i;
81 }
82 return QVariant();
85void Screens::activateScreen(const QVariant& vindex)
87 bool ok = false;
88 int index = vindex.toInt(&ok);
89 if (!ok || index < 0 || m_screenList.count() <= index) return;
91 auto screen = m_screenList.at(index);
92 screen->setActive(true);
95void Screens::onScreenAdded(qtmir::Screen *screen)
97 Q_FOREACH(auto screenWrapper, m_screenList) {
98 if (screenWrapper->wrapped() == screen) return;
99 }
101 beginInsertRows(QModelIndex(), count(), count());
102 auto screenWrapper(new Screen(screen));
103 m_screenList.push_back(screenWrapper);
104 endInsertRows();
105 Q_EMIT screenAdded(screenWrapper);
106 Q_EMIT countChanged();
109void Screens::onScreenRemoved(qtmir::Screen *screen)
111 int index = 0;
112 QMutableListIterator<Screen*> iter(m_screenList);
113 while(iter.hasNext()) {
114 auto screenWrapper = iter.next();
115 if (screenWrapper->wrapped() == screen) {
117 beginRemoveRows(QModelIndex(), index, index);
118 iter.remove();
119 endRemoveRows();
121 Q_EMIT screenRemoved(screenWrapper);
122 Q_EMIT countChanged();
124 screenWrapper->deleteLater();
125 break;
126 }
127 index++;
128 }