on 2917119 help sign in
unity8/tests/mocks/Unity/Application/MirSurfaceItemModel.cpp
102 lines 3.2 kB history raw
1/*
2 * Copyright (C) 2014 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 */
17#include <QQmlEngine>
19// local
20#include "MirSurfaceItemModel.h"
21#include "MirSurfaceItem.h"
23MirSurfaceItemModel::MirSurfaceItemModel(
24 QObject *parent)
25 : QAbstractListModel(parent)
27 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
30void MirSurfaceItemModel::insertSurface(uint index, MirSurfaceItem* surface)
32 index = qMin(index, (uint)count());
34 int existingSurfaceIndex = m_surfaceItems.indexOf(surface);
35 if (existingSurfaceIndex != -1) {
36 move(existingSurfaceIndex, index);
37 } else {
38 beginInsertRows(QModelIndex(), index, index);
39 m_surfaceItems.insert(index, surface);
40 endInsertRows();
41 Q_EMIT countChanged();
42 }
45void MirSurfaceItemModel::removeSurface(MirSurfaceItem* surface)
47 int existingSurfaceIndex = m_surfaceItems.indexOf(surface);
48 if (existingSurfaceIndex != -1) {
49 beginRemoveRows(QModelIndex(), existingSurfaceIndex, existingSurfaceIndex);
50 m_surfaceItems.removeAt(existingSurfaceIndex);
51 endRemoveRows();
52 Q_EMIT countChanged();
53 }
56void MirSurfaceItemModel::move(int from, int to)
58 if (from == to) return;
60 if (from >= 0 && from < m_surfaceItems.size() && to >= 0 && to < m_surfaceItems.size()) {
61 QModelIndex parent;
62 /* When moving an item down, the destination index needs to be incremented
63 by one, as explained in the documentation:
64 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
66 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
67 m_surfaceItems.move(from, to);
68 endMoveRows();
69 }
72QHash<int, QByteArray> MirSurfaceItemModel::roleNames() const
74 QHash<int, QByteArray> roleNames;
75 roleNames.insert(RoleSurface, "surface");
76 return roleNames;
79int MirSurfaceItemModel::rowCount(const QModelIndex & /*parent*/) const
81 return m_surfaceItems.count();
84QVariant MirSurfaceItemModel::data(const QModelIndex & index, int role) const
86 if (index.row() >= 0 && index.row() < m_surfaceItems.count()) {
87 MirSurfaceItem *surfaceItem = m_surfaceItems.at(index.row());
88 switch (role) {
89 case RoleSurface:
90 return QVariant::fromValue(surfaceItem);
91 default:
92 return QVariant();
93 }
94 } else {
95 return QVariant();
96 }
99MirSurfaceItem* MirSurfaceItemModel::getSurface(int index)
101 return m_surfaceItems[index];