2
* Copyright (C) 2016 Canonical, Ltd.
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.
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.
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/>.
23
#include <QGuiApplication>
26
Q_DECLARE_METATYPE(QScreen*)
30
Screens::Screens(QObject *parent) :
31
QAbstractListModel(parent)
33
auto app = static_cast<QGuiApplication *>(QGuiApplication::instance());
37
connect(app, &QGuiApplication::screenAdded, this, &Screens::onScreenAdded);
38
connect(app, &QGuiApplication::screenRemoved, this, &Screens::onScreenRemoved);
40
m_screenList = QGuiApplication::screens();
43
QHash<int, QByteArray> Screens::roleNames() const
45
QHash<int, QByteArray> roles;
46
roles[ScreenRole] = "screen";
47
roles[OutputTypeRole] = "outputType";
48
roles[ScaleRole] = "scale";
49
roles[FormFactorRole] = "formFactor";
53
QVariant Screens::data(const QModelIndex &index, int role) const
55
if (!index.isValid() || index.row() >= m_screenList.size()) {
61
return QVariant::fromValue(m_screenList.at(index.row()));
62
case OutputTypeRole: {
63
auto screen = static_cast<Screen*>(m_screenList.at(index.row())->handle());
65
return QVariant(screen->outputType());
67
return OutputTypes::Unknown;
71
auto screen = static_cast<Screen*>(m_screenList.at(index.row())->handle());
73
return QVariant(screen->scale());
78
case FormFactorRole: {
79
auto screen = static_cast<Screen*>(m_screenList.at(index.row())->handle());
81
return QVariant(static_cast<FormFactor>(screen->formFactor())); //FIXME: cheeky
83
return FormFactor::FormFactorUnknown;
91
int Screens::rowCount(const QModelIndex &) const
96
int Screens::count() const
98
return m_screenList.size();
101
void Screens::onScreenAdded(QScreen *screen)
103
if (m_screenList.contains(screen))
106
beginInsertRows(QModelIndex(), count(), count());
107
m_screenList.push_back(screen);
109
Q_EMIT screenAdded(screen);
110
Q_EMIT countChanged();
113
void Screens::onScreenRemoved(QScreen *screen)
115
int index = m_screenList.indexOf(screen);
119
beginRemoveRows(QModelIndex(), index, index);
120
m_screenList.removeAt(index);
122
Q_EMIT screenRemoved(screen);
123
Q_EMIT countChanged();