on 34be00a help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceListModel.cpp
159 lines 4.7 kB history raw
1/*
2 * Copyright (C) 2016 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 */
17#include "MirSurfaceListModel.h"
18#include "ApplicationInfo.h"
20#include "MirSurface.h"
22#define MIRSURFACELISTMODEL_DEBUG 0
24#ifdef MIRSURFACELISTMODEL_DEBUG
25#define DEBUG_MSG(params) qDebug().nospace() << "MirSurfaceListModel::" << __func__ << " " << params
26#else
27#define DEBUG_MSG(params) ((void)0)
28#endif
30using namespace unity::shell::application;
32MirSurfaceListModel::MirSurfaceListModel(QObject *parent) :
33 MirSurfaceListInterface(parent)
37int MirSurfaceListModel::rowCount(const QModelIndex &parent) const
39 return !parent.isValid() ? m_surfaceList.size() : 0;
42QVariant MirSurfaceListModel::data(const QModelIndex& index, int role) const
44 if (index.row() < 0 || index.row() >= m_surfaceList.size())
45 return QVariant();
47 if (role == SurfaceRole) {
48 MirSurface *surface = m_surfaceList.at(index.row());
49 return QVariant::fromValue(static_cast<unity::shell::application::MirSurfaceInterface*>(surface));
50 } else {
51 return QVariant();
52 }
55void MirSurfaceListModel::raise(MirSurface *surface)
57 int i = m_surfaceList.indexOf(surface);
58 if (i != -1) {
59 moveSurface(i, 0);
60 }
63void MirSurfaceListModel::appendSurface(MirSurface *surface)
65 beginInsertRows(QModelIndex(), m_surfaceList.size(), m_surfaceList.size());
66 m_surfaceList.append(surface);
67 connectSurface(surface);
68 endInsertRows();
69 Q_EMIT countChanged(m_surfaceList.count());
70 if (m_surfaceList.count() == 1) {
71 Q_EMIT firstChanged();
72 }
75void MirSurfaceListModel::prependSurface(MirSurface *surface)
77 beginInsertRows(QModelIndex(), 0, 0);
78 m_surfaceList.prepend(surface);
79 connectSurface(surface);
80 endInsertRows();
81 Q_EMIT countChanged(m_surfaceList.count());
82 Q_EMIT firstChanged();
85void MirSurfaceListModel::connectSurface(MirSurface *surface)
87 connect(surface, &MirSurface::raiseRequested, this, [this, surface](){ this->raise(surface); });
88 connect(surface, &QObject::destroyed, this, [this, surface](){ this->removeSurface(surface); });
91void MirSurfaceListModel::removeSurface(MirSurface *surface)
93 int i = m_surfaceList.indexOf(surface);
94 if (i != -1) {
95 beginRemoveRows(QModelIndex(), i, i);
96 m_surfaceList.removeAt(i);
97 endRemoveRows();
98 Q_EMIT countChanged(m_surfaceList.count());
99 if (m_surfaceList.count() == 0 || i == 0) {
100 Q_EMIT firstChanged();
101 }
102 }
105void MirSurfaceListModel::moveSurface(int from, int to)
107 if (from == to) return;
109 if (from >= 0 && from < m_surfaceList.size() && to >= 0 && to < m_surfaceList.size()) {
110 QModelIndex parent;
111 /* When moving an item down, the destination index needs to be incremented
112 by one, as explained in the documentation:
113 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
115 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
116 m_surfaceList.move(from, to);
117 endMoveRows();
118 }
120 if ((from == 0 || to == 0) && m_surfaceList.count() > 1) {
121 Q_EMIT firstChanged();
122 }
125MirSurfaceInterface *MirSurfaceListModel::get(int index)
127 if (index >=0 && index < m_surfaceList.count()) {
128 return m_surfaceList[index];
129 } else {
130 return nullptr;
131 }
134const MirSurfaceInterface *MirSurfaceListModel::get(int index) const
136 if (index >=0 && index < m_surfaceList.count()) {
137 return m_surfaceList.at(index);
138 } else {
139 return nullptr;
140 }
143MirSurfaceInterface *MirSurfaceListModel::createSurface()
145 QStringList screenshotIds = {"gallery", "map", "facebook", "camera", "browser", "music", "twitter"};
146 int i = rand() % screenshotIds.count();
148 QUrl screenshotUrl = QString("qrc:///Unity/Application/screenshots/%1@12.png")
149 .arg(screenshotIds[i]);
151 auto surface = new MirSurface(QString("prompt foo"),
152 Mir::NormalType,
153 Mir::RestoredState,
154 screenshotUrl);
156 prependSurface(surface);
158 return surface;