Debian Codegraph @

tests/mocks/QtMir/Application/MirSurfaceListModel.cpp

raw
1 /*
2 * Copyright (C) 2016-2017 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include "MirSurfaceListModel.h"
18 #include "ApplicationInfo.h"
19
20 #include <lomiri/shell/application/MirSurfaceInterface.h>
21
22 #define MIRSURFACELISTMODEL_DEBUG 0
23
24 #if MIRSURFACELISTMODEL_DEBUG
25 #include <QDebug>
26 #define DEBUG_MSG(params) qDebug().nospace() << "MirSurfaceListModel::" << __func__ << params
27 #else
28 #define DEBUG_MSG(params) ((void)0)
29 #endif
30
31 using namespace lomiri::shell::application;
32
33 MirSurfaceListModel::MirSurfaceListModel(QObject *parent) :
34 MirSurfaceListInterface(parent)
35 {
36 }
37
38 int MirSurfaceListModel::rowCount(const QModelIndex &parent) const
39 {
40 return !parent.isValid() ? m_surfaceList.size() : 0;
41 }
42
43 QVariant MirSurfaceListModel::data(const QModelIndex& index, int role) const
44 {
45 if (index.row() < 0 || index.row() >= m_surfaceList.size())
46 return QVariant();
47
48 if (role == SurfaceRole) {
49 MirSurfaceInterface *surface = m_surfaceList.at(index.row());
50 return QVariant::fromValue(static_cast<lomiri::shell::application::MirSurfaceInterface*>(surface));
51 } else {
52 return QVariant();
53 }
54 }
55
56 void MirSurfaceListModel::raise(MirSurfaceInterface *surface)
57 {
58 DEBUG_MSG("(" << surface << ")");
59 int i = m_surfaceList.indexOf(surface);
60 if (i != -1) {
61 moveSurface(i, 0);
62 }
63 }
64
65 void MirSurfaceListModel::addSurface(MirSurfaceInterface *surface)
66 {
67 DEBUG_MSG("(" << surface << ")");
68 beginInsertRows(QModelIndex(), 0, 0);
69 m_surfaceList.prepend(surface);
70 connectSurface(surface);
71 endInsertRows();
72 Q_EMIT countChanged(m_surfaceList.count());
73 Q_EMIT firstChanged();
74 }
75
76 void MirSurfaceListModel::connectSurface(MirSurfaceInterface *surface)
77 {
78 connect(surface, &QObject::destroyed, this, [this, surface](){ this->removeSurface(surface); });
79 connect(surface, &MirSurfaceInterface::focusedChanged, this, [this, surface](bool surfaceFocused){
80 if (surfaceFocused) {
81 raise(surface);
82 }
83 });
84 }
85
86 void MirSurfaceListModel::removeSurface(MirSurfaceInterface *surface)
87 {
88 int i = m_surfaceList.indexOf(surface);
89 if (i != -1) {
90 beginRemoveRows(QModelIndex(), i, i);
91 m_surfaceList.removeAt(i);
92 endRemoveRows();
93 Q_EMIT countChanged(m_surfaceList.count());
94 if (m_surfaceList.count() == 0 || i == 0) {
95 Q_EMIT firstChanged();
96 }
97 }
98 }
99
100 void MirSurfaceListModel::moveSurface(int from, int to)
101 {
102 if (from == to) return;
103
104 if (from >= 0 && from < m_surfaceList.size() && to >= 0 && to < m_surfaceList.size()) {
105 QModelIndex parent;
106 /* When moving an item down, the destination index needs to be incremented
107 by one, as explained in the documentation:
108 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
109
110 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
111 m_surfaceList.move(from, to);
112 endMoveRows();
113 }
114
115 if ((from == 0 || to == 0) && m_surfaceList.count() > 1) {
116 Q_EMIT firstChanged();
117 }
118 }
119
120 MirSurfaceInterface *MirSurfaceListModel::get(int index)
121 {
122 if (index >=0 && index < m_surfaceList.count()) {
123 return m_surfaceList[index];
124 } else {
125 return nullptr;
126 }
127 }
128
129 const MirSurfaceInterface *MirSurfaceListModel::get(int index) const
130 {
131 if (index >=0 && index < m_surfaceList.count()) {
132 return m_surfaceList.at(index);
133 } else {
134 return nullptr;
135 }
136 }