on 4e8f28f help sign in
unity8/plugins/WindowManager/WorkspaceModel.cpp
191 lines 5.3 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 "WorkspaceManager.h"
19#include "Workspace.h"
21#include <QQmlEngine>
23Q_LOGGING_CATEGORY(WORKSPACES, "Workspaces", QtInfoMsg)
25#define DEBUG_MSG qCDebug(WORKSPACES).nospace().noquote() << __func__
26#define INFO_MSG qCInfo(WORKSPACES).nospace().noquote() << __func__
28WorkspaceModel::WorkspaceModel(QObject *parent)
29 : QAbstractListModel(parent)
33void WorkspaceModel::append(Workspace *workspace)
35 insert(m_workspaces.count(), workspace);
38void WorkspaceModel::insert(int index, Workspace *workspace)
40 beginInsertRows(QModelIndex(), index, index);
42 m_workspaces.insert(index, workspace);
44 endInsertRows();
46 Q_EMIT workspaceAdded(workspace);
47 Q_EMIT countChanged();
50void WorkspaceModel::remove(Workspace *workspace)
52 int index = m_workspaces.indexOf(workspace);
53 if (index < 0) return;
55 beginRemoveRows(QModelIndex(), index, index);
57 m_workspaces.removeAt(index);
59 endRemoveRows();
61 Q_EMIT workspaceRemoved(workspace);
62 Q_EMIT countChanged();
65void WorkspaceModel::move(int from, int to)
67 if (from == to) return;
68 DEBUG_MSG << " from=" << from << " to=" << to;
70 if (from >= 0 && from < m_workspaces.size() && to >= 0 && to < m_workspaces.size()) {
71 QModelIndex parent;
73 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
74#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
75 const auto &window = m_windowModel.takeAt(from);
76 m_workspaces.insert(to, window);
77#else
78 m_workspaces.move(from, to);
79#endif
80 endMoveRows();
81 Q_EMIT countChanged();
82 }
85int WorkspaceModel::indexOf(Workspace *workspace) const
87 return m_workspaces.indexOf(workspace);
90Workspace *WorkspaceModel::get(int index) const
92 if (index < 0 || index >= rowCount()) return nullptr;
93 return m_workspaces.at(index);
96int WorkspaceModel::rowCount(const QModelIndex &) const
98 return m_workspaces.count();
101QVariant WorkspaceModel::data(const QModelIndex &index, int role) const
103 if (index.row() < 0 || index.row() >= m_workspaces.size())
104 return QVariant();
106 if (role == WorkspaceRole) {
107 Workspace *workspace = m_workspaces.at(index.row());
108 return QVariant::fromValue(workspace);
109 } else {
110 return QVariant();
111 }
114void WorkspaceModel::sync(WorkspaceModel *proxy)
116 if (!proxy) return;
117 const auto& proxyList = proxy->list();
119 // check for removals
120 int removedIndexWhichWasActive = -1;
121 QVector<Workspace*> dpCpy(this->list());
122 Q_FOREACH(auto workspace, dpCpy) {
124 bool found = false;
125 Q_FOREACH(auto p, proxyList) {
126 auto workspaceProxy = static_cast<WorkspaceProxy*>(p);
127 if (workspaceProxy->proxyObject() == workspace) {
128 found = true;
129 break;
130 }
131 }
132 if (!found) {
133 if (workspace->isActive()) {
134 removedIndexWhichWasActive = indexOf(workspace);
135 }
136 workspace->unassign();
137 }
138 }
140 // existing
141 QSet<Workspace*> newWorkspaces;
142 for (int i = 0; i < proxyList.count(); i++) {
143 auto workspaceProxy = static_cast<WorkspaceProxy*>(proxyList[i]);
144 auto workspace = workspaceProxy->proxyObject();
146 int oldIndex = this->indexOf(workspace);
148 if (oldIndex < 0) {
149 workspace->assign(this, QVariant(i));
150 } else if (oldIndex != i) {
151 this->move(oldIndex, i);
152 }
153 newWorkspaces.insert(workspace);
154 }
156 // Make sure we have at least one workspace in the model.
157 if (rowCount() == 0) {
158 Workspace* workspace = WorkspaceManager::instance()->createWorkspace();
159 workspace->assign(this);
160 (new WorkspaceProxy(workspace))->assign(proxy);
161 }
163 if (removedIndexWhichWasActive != -1) {
164 int newActiveIndex = qMin(removedIndexWhichWasActive, this->rowCount()-1);
165 Workspace* newActiveWorkspace = newActiveIndex >= 0 ? this->get(newActiveIndex) : nullptr;
167 WorkspaceManager::instance()->setActiveWorkspace(newActiveWorkspace);
168 }
172WorkspaceModelProxy::WorkspaceModelProxy(WorkspaceModel * const model)
173 : m_original(model)
175 Q_FOREACH(auto workspace, model->list()) {
176 auto proxy = new WorkspaceProxy(workspace);
177 QQmlEngine::setObjectOwnership(proxy, QQmlEngine::CppOwnership);
178 proxy->assign(this);
179 }
182WorkspaceModelProxy::~WorkspaceModelProxy()
184 qDeleteAll(m_workspaces.toList()); // make a copy so the list doesnt edit itself during delete.
185 m_workspaces.clear();
188void WorkspaceModelProxy::move(int from, int to)
190 WorkspaceModel::move(from, to);