on 80fc88d help sign in
unity8/plugins/WindowManager/WorkspaceModel.cpp
252 lines 7.2 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 workspaceInserted(index, 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);
58 insertUnassigned(workspace);
60 endRemoveRows();
62 Q_EMIT workspaceRemoved(workspace);
63 Q_EMIT countChanged();
66void WorkspaceModel::move(int from, int to)
68 if (from == to) return;
69 DEBUG_MSG << " from=" << from << " to=" << to;
71 if (from >= 0 && from < m_workspaces.size() && to >= 0 && to < m_workspaces.size()) {
72 QModelIndex parent;
74 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
75#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
76 const auto &window = m_windowModel.takeAt(from);
77 m_workspaces.insert(to, window);
78#else
79 m_workspaces.move(from, to);
80#endif
81 endMoveRows();
83 Q_EMIT workspaceMoved(from, to);
84 }
87int WorkspaceModel::indexOf(Workspace *workspace) const
89 return m_workspaces.indexOf(workspace);
92Workspace *WorkspaceModel::get(int index) const
94 if (index < 0 || index >= rowCount()) return nullptr;
95 return m_workspaces.at(index);
98int WorkspaceModel::rowCount(const QModelIndex &) const
100 return m_workspaces.count();
103QVariant WorkspaceModel::data(const QModelIndex &index, int role) const
105 if (index.row() < 0 || index.row() >= m_workspaces.size())
106 return QVariant();
108 if (role == WorkspaceRole) {
109 Workspace *workspace = m_workspaces.at(index.row());
110 return QVariant::fromValue(workspace);
111 } else {
112 return QVariant();
113 }
116void WorkspaceModel::setSyncing(bool syncing)
118 m_syncing = syncing;
121void WorkspaceModel::sync(WorkspaceModel *proxy)
123 if (!proxy) return;
124 const auto& proxyList = proxy->list();
126 // check for removals
127 int removedIndexWhichWasActive = -1;
128 QVector<Workspace*> dpCpy(this->list());
129 Q_FOREACH(auto workspace, dpCpy) {
131 bool found = false;
132 Q_FOREACH(auto p, proxyList) {
133 auto workspaceProxy = qobject_cast<ProxyWorkspace*>(p);
134 if (workspaceProxy->proxyObject() == workspace) {
135 found = true;
136 break;
137 }
138 }
139 if (!found) {
140 if (workspace->isActive()) {
141 removedIndexWhichWasActive = indexOf(workspace);
142 }
143 workspace->unassign();
144 }
145 }
147 // existing
148 QSet<Workspace*> newWorkspaces;
149 for (int i = 0; i < proxyList.count(); i++) {
150 auto workspaceProxy = qobject_cast<ProxyWorkspace*>(proxyList[i]);
151 auto workspace = workspaceProxy->proxyObject();
153 int oldIndex = this->indexOf(workspace);
155 if (oldIndex < 0) {
156 workspace->assign(this, QVariant(i));
157 } else if (oldIndex != i) {
158 this->move(oldIndex, i);
159 }
160 newWorkspaces.insert(workspace);
161 }
163 // Make sure we have at least one workspace in the model.
164 if (rowCount() == 0) {
165 Workspace* workspace = WorkspaceManager::instance()->createWorkspace();
166 workspace->assign(this);
167 (new ProxyWorkspace(workspace))->assign(proxy);
168 }
170 if (removedIndexWhichWasActive != -1) {
171 int newActiveIndex = qMin(removedIndexWhichWasActive, this->rowCount()-1);
172 Workspace* newActiveWorkspace = newActiveIndex >= 0 ? this->get(newActiveIndex) : nullptr;
174 WorkspaceManager::instance()->setActiveWorkspace(newActiveWorkspace);
175 }
177 proxy->finishSync();
178 finishSync();
181void WorkspaceModel::finishSync()
183 QSet<Workspace*> dpCpy(m_unassignedWorkspaces);
184 Q_FOREACH(auto workspace, dpCpy) {
185 delete workspace;
186 }
187 m_unassignedWorkspaces.clear();
190void WorkspaceModel::insertUnassigned(Workspace *workspace)
192 m_unassignedWorkspaces.insert(workspace);
193 connect(workspace, &Workspace::assigned, this, [=]() {
194 m_unassignedWorkspaces.remove(workspace);
195 disconnect(workspace, &Workspace::assigned, this, 0);
196 });
197 connect(workspace, &QObject::destroyed, this, [=]() {
198 m_unassignedWorkspaces.remove(workspace);
199 });
203ProxyWorkspaceModel::ProxyWorkspaceModel(WorkspaceModel * const model)
204 : m_original(model)
206 Q_FOREACH(auto workspace, model->list()) {
207 auto proxy = new ProxyWorkspace(workspace);
208 QQmlEngine::setObjectOwnership(proxy, QQmlEngine::CppOwnership);
209 proxy->assign(this);
210 }
211 connect(m_original, &WorkspaceModel::workspaceInserted, this, [this](int index, Workspace* inserted) {
212 if (m_original->isSyncing()) return;
214 (new ProxyWorkspace(inserted))->assign(this, index);
215 });
216 connect(m_original, &WorkspaceModel::workspaceRemoved, this, [this](Workspace* removed) {
217 if (m_original->isSyncing()) return;
219 for (int i = 0; i < rowCount(); i++) {
220 auto workspaceProxy = qobject_cast<ProxyWorkspace*>(get(i));
221 auto w = workspaceProxy->proxyObject();
222 if (w == removed) {
223 remove(workspaceProxy);
224 break;
225 }
226 }
227 });
228 connect(m_original, &WorkspaceModel::workspaceMoved, this, [this](int from, int to) {
229 if (m_original->isSyncing()) return;
231 move(from, to);
232 });
235ProxyWorkspaceModel::~ProxyWorkspaceModel()
237 qDeleteAll(m_workspaces.toList()); // make a copy so the list doesnt edit itself during delete.
238 m_workspaces.clear();
241void ProxyWorkspaceModel::move(int from, int to)
243 WorkspaceModel::move(from, to);
246void ProxyWorkspaceModel::addWorkspace()
248 auto newWorkspace = WorkspaceManager::instance()->createWorkspace();
249 m_original->insertUnassigned(newWorkspace);
251 (new ProxyWorkspace(newWorkspace))->assign(this);