@@ -0,0 +1,92 @@
1+/*
2+ * Copyright (C) 2015 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+ */
16+
17+#include "screens.h"
18+
19+// Qt
20+#include <QGuiApplication>
21+#include <QScreen>
22+
23+Q_DECLARE_METATYPE(QScreen*)
24+
25+Screens::Screens(QObject *parent) :
26+ QAbstractListModel(parent)
27+{
28+}
29+
30+Screens::~Screens()
31+{
32+ qDeleteAll(m_screenList);
33+}
34+
35+QHash<int, QByteArray> Screens::roleNames() const
36+{
37+ QHash<int, QByteArray> roles;
38+ roles[ScreenRole] = "screen";
39+ roles[OutputTypeRole] = "outputType";
40+ return roles;
41+}
42+
43+QVariant Screens::data(const QModelIndex &index, int role) const
44+{
45+ if (!index.isValid() || index.row() >= m_screenList.size()) {
46+ return QVariant();
47+ }
48+
49+ switch(role) {
50+ case ScreenRole:
51+ return QVariant::fromValue(m_screenList.at(index.row()));
52+ case OutputTypeRole:
53+ return OutputTypes::Unknown;
54+ }
55+
56+ return QVariant();
57+}
58+
59+int Screens::rowCount(const QModelIndex &) const
60+{
61+ return count();
62+}
63+
64+int Screens::count() const
65+{
66+ return m_screenList.size();
67+}
68+
69+void Screens::onScreenAdded(QScreen *screen)
70+{
71+ if (m_screenList.contains(screen))
72+ return;
73+
74+ beginInsertRows(QModelIndex(), count(), count());
75+ m_screenList.push_back(screen);
76+ endInsertRows();
77+ Q_EMIT screenAdded(screen);
78+ Q_EMIT countChanged();
79+}
80+
81+void Screens::onScreenRemoved(QScreen *screen)
82+{
83+ int index = m_screenList.indexOf(screen);
84+ if (index < 0)
85+ return;
86+
87+ beginRemoveRows(QModelIndex(), index, index);
88+ m_screenList.removeAt(index);
89+ endRemoveRows();
90+ Q_EMIT screenRemoved(screen);
91+ Q_EMIT countChanged();
92+}