@@ -0,0 +1,90 @@
2+ * Copyright (C) 2017 Canonical, Ltd.
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.
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.
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/>.
17+#include "WorkspaceModel.h"
18+#include "Workspace.h"
20+Q_LOGGING_CATEGORY(WORKSPACES, "Workspaces", QtInfoMsg)
22+#define DEBUG_MSG qCDebug(WORKSPACES).nospace().noquote() << __func__
23+#define INFO_MSG qCInfo(WORKSPACES).nospace().noquote() << __func__
25+WorkspaceModel::WorkspaceModel(QObject *parent)
26+ : QAbstractListModel(parent)
30+void WorkspaceModel::append(Workspace *workspace)
32+ insert(m_workspaces.count(), workspace);
35+void WorkspaceModel::insert(int index, Workspace *workspace)
37+ beginInsertRows(QModelIndex(), index, index);
38+ m_workspaces.append(workspace);
40+ Q_EMIT countChanged();
43+void 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);
51+ Q_EMIT countChanged();
54+void 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()) {
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);
67+ m_workspaces.move(from, to);
70+ Q_EMIT countChanged();
74+int WorkspaceModel::rowCount(const QModelIndex &) const
76+ return m_workspaces.count();
79+QVariant WorkspaceModel::data(const QModelIndex &index, int role) const
81+ if (index.row() < 0 || index.row() >= m_workspaces.size())
84+ if (role == WorkspaceRole) {
85+ Workspace *workspace = m_workspaces.at(index.row());
86+ return QVariant::fromValue(workspace);