2 * Copyright (C) 2014 Canonical, Ltd.
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.
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.
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/>.
20#include "MirSurfaceItemModel.h"
21#include "MirSurfaceItem.h"
23MirSurfaceItemModel::MirSurfaceItemModel(
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);
38 beginInsertRows(QModelIndex(), index, index);
39 m_surfaceItems.insert(index, surface);
41 Q_EMIT countChanged();
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);
52 Q_EMIT countChanged();
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()) {
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);
72QHash<int, QByteArray> MirSurfaceItemModel::roleNames() const
74 QHash<int, QByteArray> roleNames;
75 roleNames.insert(RoleSurface, "surface");
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());
90 return QVariant::fromValue(surfaceItem);
99MirSurfaceItem* MirSurfaceItemModel::getSurface(int index)
101 return m_surfaceItems[index];
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list