~alan-griffiths/miral/debug

« back to all changes in this revision

Viewing changes to miral-qt/src/modules/Unity/Screens/screens.cpp

  • Committer: Gerry Boland
  • Date: 2016-06-01 22:06:51 UTC
  • mto: This revision was merged to the branch mainline in revision 178.
  • Revision ID: gerry.boland@canonical.com-20160601220651-ge508tffql4e7u7c
Import QtMir code into miral-qt subdirectory. Disabled by default, use -DMIRAL_ENABLE_QT=1 to build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 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
// mirserver
 
20
#include "screen.h"
 
21
 
 
22
// Qt
 
23
#include <QGuiApplication>
 
24
#include <QScreen>
 
25
 
 
26
Q_DECLARE_METATYPE(QScreen*)
 
27
 
 
28
namespace qtmir {
 
29
 
 
30
Screens::Screens(QObject *parent) :
 
31
    QAbstractListModel(parent)
 
32
{
 
33
    auto app = static_cast<QGuiApplication *>(QGuiApplication::instance());
 
34
    if (!app) {
 
35
        return;
 
36
    }
 
37
    connect(app, &QGuiApplication::screenAdded, this, &Screens::onScreenAdded);
 
38
    connect(app, &QGuiApplication::screenRemoved, this, &Screens::onScreenRemoved);
 
39
 
 
40
    m_screenList = QGuiApplication::screens();
 
41
}
 
42
 
 
43
QHash<int, QByteArray> Screens::roleNames() const
 
44
{
 
45
    QHash<int, QByteArray> roles;
 
46
    roles[ScreenRole] = "screen";
 
47
    roles[OutputTypeRole] = "outputType";
 
48
    roles[ScaleRole] = "scale";
 
49
    roles[FormFactorRole] = "formFactor";
 
50
    return roles;
 
51
}
 
52
 
 
53
QVariant Screens::data(const QModelIndex &index, int role) const
 
54
{
 
55
    if (!index.isValid() || index.row() >= m_screenList.size()) {
 
56
        return QVariant();
 
57
    }
 
58
 
 
59
    switch(role) {
 
60
    case ScreenRole:
 
61
        return QVariant::fromValue(m_screenList.at(index.row()));
 
62
    case OutputTypeRole: {
 
63
        auto screen = static_cast<Screen*>(m_screenList.at(index.row())->handle());
 
64
        if (screen) {
 
65
            return QVariant(static_cast<OutputTypes>(screen->outputType())); //FIXME: cheeky
 
66
        } else {
 
67
            return OutputTypes::Unknown;
 
68
        }
 
69
    }
 
70
    case ScaleRole: {
 
71
        auto screen = static_cast<Screen*>(m_screenList.at(index.row())->handle());
 
72
        if (screen) {
 
73
            return QVariant(screen->scale());
 
74
        } else {
 
75
            return 1.0;
 
76
        }
 
77
    }
 
78
    case FormFactorRole: {
 
79
        auto screen = static_cast<Screen*>(m_screenList.at(index.row())->handle());
 
80
        if (screen) {
 
81
            return QVariant(static_cast<FormFactor>(screen->formFactor())); //FIXME: cheeky
 
82
        } else {
 
83
            return FormFactor::FormFactorUnknown;
 
84
        }
 
85
    }
 
86
    } // switch
 
87
 
 
88
    return QVariant();
 
89
}
 
90
 
 
91
int Screens::rowCount(const QModelIndex &) const
 
92
{
 
93
    return count();
 
94
}
 
95
 
 
96
int Screens::count() const
 
97
{
 
98
    return m_screenList.size();
 
99
}
 
100
 
 
101
void Screens::onScreenAdded(QScreen *screen)
 
102
{
 
103
    if (m_screenList.contains(screen))
 
104
        return;
 
105
 
 
106
    beginInsertRows(QModelIndex(), count(), count());
 
107
    m_screenList.push_back(screen);
 
108
    endInsertRows();
 
109
    Q_EMIT screenAdded(screen);
 
110
    Q_EMIT countChanged();
 
111
}
 
112
 
 
113
void Screens::onScreenRemoved(QScreen *screen)
 
114
{
 
115
    int index = m_screenList.indexOf(screen);
 
116
    if (index < 0)
 
117
        return;
 
118
 
 
119
    beginRemoveRows(QModelIndex(), index, index);
 
120
    m_screenList.removeAt(index);
 
121
    endRemoveRows();
 
122
    Q_EMIT screenRemoved(screen);
 
123
    Q_EMIT countChanged();
 
124
}
 
125
 
 
126
} // namespace qtmir