@@ -0,0 +1,90 @@
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+ */
16+
17+#include "WorkspaceModel.h"
18+#include "Workspace.h"
19+
20+Q_LOGGING_CATEGORY(WORKSPACES, "Workspaces", QtInfoMsg)
21+
22+#define DEBUG_MSG qCDebug(WORKSPACES).nospace().noquote() << __func__
23+#define INFO_MSG qCInfo(WORKSPACES).nospace().noquote() << __func__
24+
25+WorkspaceModel::WorkspaceModel(QObject *parent)
26+ : QAbstractListModel(parent)
27+{
28+}
29+
30+void WorkspaceModel::append(Workspace *workspace)
31+{
32+ insert(m_workspaces.count(), workspace);
33+}
34+
35+void WorkspaceModel::insert(int index, Workspace *workspace)
36+{
37+ beginInsertRows(QModelIndex(), index, index);
38+ m_workspaces.append(workspace);
39+ endInsertRows();
40+ Q_EMIT countChanged();
41+}
42+
43+void WorkspaceModel::remove(Workspace *workspace)
44+{
45+ int index = m_workspaces.indexOf(workspace);
46+ if (index < 0) return;
47+
48+ beginRemoveRows(QModelIndex(), index, index);
49+ m_workspaces.removeAt(index);
50+ endRemoveRows();
51+ Q_EMIT countChanged();
52+}
53+
54+void WorkspaceModel::move(int from, int to)
55+{
56+ if (from == to) return;
57+ DEBUG_MSG << " from=" << from << " to=" << to;
58+
59+ if (from >= 0 && from < m_workspaces.size() && to >= 0 && to < m_workspaces.size()) {
60+ QModelIndex parent;
61+
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+ }
72+}
73+
74+int WorkspaceModel::rowCount(const QModelIndex &) const
75+{
76+ return m_workspaces.count();
77+}
78+
79+QVariant WorkspaceModel::data(const QModelIndex &index, int role) const
80+{
81+ if (index.row() < 0 || index.row() >= m_workspaces.size())
82+ return QVariant();
83+
84+ if (role == WorkspaceRole) {
85+ Workspace *workspace = m_workspaces.at(index.row());
86+ return QVariant::fromValue(workspace);
87+ } else {
88+ return QVariant();
89+ }
90+}