on 747a319 help sign in
unity8/plugins/WindowManager/WorkspaceModel.cpp
104 lines 2.8 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);
39 m_workspaces.append(workspace);
41 endInsertRows();
43 Q_EMIT workspaceAdded(workspace);
44 Q_EMIT countChanged();
47void WorkspaceModel::remove(Workspace *workspace)
49 int index = m_workspaces.indexOf(workspace);
50 if (index < 0) return;
52 beginRemoveRows(QModelIndex(), index, index);
54 m_workspaces.removeAt(index);
55 disconnect(workspace);
57 endRemoveRows();
59 Q_EMIT workspaceRemoved(workspace);
60 Q_EMIT countChanged();
63void WorkspaceModel::move(int from, int to)
65 if (from == to) return;
66 DEBUG_MSG << " from=" << from << " to=" << to;
68 if (from >= 0 && from < m_workspaces.size() && to >= 0 && to < m_workspaces.size()) {
69 QModelIndex parent;
71 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
72#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
73 const auto &window = m_windowModel.takeAt(from);
74 m_workspaces.insert(to, window);
75#else
76 m_workspaces.move(from, to);
77#endif
78 endMoveRows();
79 Q_EMIT countChanged();
80 }
83int WorkspaceModel::indexOf(Workspace *workspace) const
85 return m_workspaces.indexOf(workspace);
88int WorkspaceModel::rowCount(const QModelIndex &) const
90 return m_workspaces.count();
93QVariant WorkspaceModel::data(const QModelIndex &index, int role) const
95 if (index.row() < 0 || index.row() >= m_workspaces.size())
96 return QVariant();
98 if (role == WorkspaceRole) {
99 Workspace *workspace = m_workspaces.at(index.row());
100 return QVariant::fromValue(workspace);
101 } else {
102 return QVariant();
103 }