on 17872af help sign in
unity8/plugins/WindowManager/WorkspaceModel.cpp
90 lines 2.6 kB history raw
1/*
2 * Copyright (C) 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 */
17#include "WorkspaceModel.h"
18#include "Workspace.h"
20Q_LOGGING_CATEGORY(WORKSPACES, "Workspaces", QtInfoMsg)
22#define DEBUG_MSG qCDebug(WORKSPACES).nospace().noquote() << __func__
23#define INFO_MSG qCInfo(WORKSPACES).nospace().noquote() << __func__
25WorkspaceModel::WorkspaceModel(QObject *parent)
26 : QAbstractListModel(parent)
30void WorkspaceModel::append(Workspace *workspace)
32 insert(m_workspaces.count(), workspace);
35void WorkspaceModel::insert(int index, Workspace *workspace)
37 beginInsertRows(QModelIndex(), index, index);
38 m_workspaces.append(workspace);
39 endInsertRows();
40 Q_EMIT countChanged();
43void WorkspaceModel::remove(Workspace *workspace)
45 int index = m_workspaces.indexOf(workspace);
46 if (index < 0) return;
48 beginRemoveRows(QModelIndex(), index, index);
49 m_workspaces.removeAt(index);
50 endRemoveRows();
51 Q_EMIT countChanged();
54void WorkspaceModel::move(int from, int to)
56 if (from == to) return;
57 DEBUG_MSG << " from=" << from << " to=" << to;
59 if (from >= 0 && from < m_workspaces.size() && to >= 0 && to < m_workspaces.size()) {
60 QModelIndex parent;
62 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
63#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
64 const auto &window = m_windowModel.takeAt(from);
65 m_workspaces.insert(to, window);
66#else
67 m_workspaces.move(from, to);
68#endif
69 endMoveRows();
70 Q_EMIT countChanged();
71 }
74int WorkspaceModel::rowCount(const QModelIndex &) const
76 return m_workspaces.count();
79QVariant WorkspaceModel::data(const QModelIndex &index, int role) const
81 if (index.row() < 0 || index.row() >= m_workspaces.size())
82 return QVariant();
84 if (role == WorkspaceRole) {
85 Workspace *workspace = m_workspaces.at(index.row());
86 return QVariant::fromValue(workspace);
87 } else {
88 return QVariant();
89 }