on 211fde5 help sign in
unity8/plugins/WindowManager/WorkspaceModel.cpp
247 lines 7.1 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);
59 endRemoveRows();
61 m_unassignedWorkspaces.insert(workspace);
62 connect(workspace, &Workspace::assigned, this, [=]() {
63 m_unassignedWorkspaces.remove(workspace);
64 disconnect(workspace, &Workspace::assigned, this, 0);
65 });
66 connect(workspace, &QObject::destroyed, this, [=]() {
67 m_unassignedWorkspaces.remove(workspace);
68 });
70 Q_EMIT workspaceRemoved(workspace);
71 Q_EMIT countChanged();
74void WorkspaceModel::move(int from, int to)
76 if (from == to) return;
77 DEBUG_MSG << " from=" << from << " to=" << to;
79 if (from >= 0 && from < m_workspaces.size() && to >= 0 && to < m_workspaces.size()) {
80 QModelIndex parent;
82 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
83#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
84 const auto &window = m_windowModel.takeAt(from);
85 m_workspaces.insert(to, window);
86#else
87 m_workspaces.move(from, to);
88#endif
89 endMoveRows();
91 Q_EMIT workspaceMoved(from, to);
92 }
95int WorkspaceModel::indexOf(Workspace *workspace) const
97 return m_workspaces.indexOf(workspace);
100Workspace *WorkspaceModel::get(int index) const
102 if (index < 0 || index >= rowCount()) return nullptr;
103 return m_workspaces.at(index);
106int WorkspaceModel::rowCount(const QModelIndex &) const
108 return m_workspaces.count();
111QVariant WorkspaceModel::data(const QModelIndex &index, int role) const
113 if (index.row() < 0 || index.row() >= m_workspaces.size())
114 return QVariant();
116 if (role == WorkspaceRole) {
117 Workspace *workspace = m_workspaces.at(index.row());
118 return QVariant::fromValue(workspace);
119 } else {
120 return QVariant();
121 }
124void WorkspaceModel::setSyncing(bool syncing)
126 m_syncing = syncing;
129void WorkspaceModel::sync(WorkspaceModel *proxy)
131 if (!proxy) return;
132 const auto& proxyList = proxy->list();
134 // check for removals
135 int removedIndexWhichWasActive = -1;
136 QVector<Workspace*> dpCpy(this->list());
137 Q_FOREACH(auto workspace, dpCpy) {
139 bool found = false;
140 Q_FOREACH(auto p, proxyList) {
141 auto workspaceProxy = qobject_cast<ProxyWorkspace*>(p);
142 if (workspaceProxy->proxyObject() == workspace) {
143 found = true;
144 break;
145 }
146 }
147 if (!found) {
148 if (workspace->isActive()) {
149 removedIndexWhichWasActive = indexOf(workspace);
150 }
151 workspace->unassign();
152 }
153 }
155 // existing
156 QSet<Workspace*> newWorkspaces;
157 for (int i = 0; i < proxyList.count(); i++) {
158 auto workspaceProxy = qobject_cast<ProxyWorkspace*>(proxyList[i]);
159 auto workspace = workspaceProxy->proxyObject();
161 int oldIndex = this->indexOf(workspace);
163 if (oldIndex < 0) {
164 workspace->assign(this, QVariant(i));
165 } else if (oldIndex != i) {
166 this->move(oldIndex, i);
167 }
168 newWorkspaces.insert(workspace);
169 }
171 // Make sure we have at least one workspace in the model.
172 if (rowCount() == 0) {
173 Workspace* workspace = WorkspaceManager::instance()->createWorkspace();
174 workspace->assign(this);
175 (new ProxyWorkspace(workspace))->assign(proxy);
176 }
178 if (removedIndexWhichWasActive != -1) {
179 int newActiveIndex = qMin(removedIndexWhichWasActive, this->rowCount()-1);
180 Workspace* newActiveWorkspace = newActiveIndex >= 0 ? this->get(newActiveIndex) : nullptr;
182 WorkspaceManager::instance()->setActiveWorkspace(newActiveWorkspace);
183 }
185 proxy->finishSync();
186 finishSync();
189void WorkspaceModel::finishSync()
191 QSet<Workspace*> dpCpy(m_unassignedWorkspaces);
192 Q_FOREACH(auto workspace, dpCpy) {
193 delete workspace;
194 }
195 m_unassignedWorkspaces.clear();
198ProxyWorkspaceModel::ProxyWorkspaceModel(WorkspaceModel * const model)
199 : m_original(model)
201 Q_FOREACH(auto workspace, model->list()) {
202 auto proxy = new ProxyWorkspace(workspace);
203 QQmlEngine::setObjectOwnership(proxy, QQmlEngine::CppOwnership);
204 proxy->assign(this);
205 }
206 connect(m_original, &WorkspaceModel::workspaceInserted, this, [this](int index, Workspace* inserted) {
207 if (m_original->isSyncing()) return;
209 (new ProxyWorkspace(inserted))->assign(this, index);
210 });
211 connect(m_original, &WorkspaceModel::workspaceRemoved, this, [this](Workspace* removed) {
212 if (m_original->isSyncing()) return;
214 for (int i = 0; i < rowCount(); i++) {
215 auto workspaceProxy = qobject_cast<ProxyWorkspace*>(get(i));
216 auto w = workspaceProxy->proxyObject();
217 if (w == removed) {
218 remove(workspaceProxy);
219 break;
220 }
221 }
222 });
223 connect(m_original, &WorkspaceModel::workspaceMoved, this, [this](int from, int to) {
224 if (m_original->isSyncing()) return;
226 move(from, to);
227 });
230ProxyWorkspaceModel::~ProxyWorkspaceModel()
232 qDeleteAll(m_workspaces.toList()); // make a copy so the list doesnt edit itself during delete.
233 m_workspaces.clear();
236void ProxyWorkspaceModel::move(int from, int to)
238 WorkspaceModel::move(from, to);
241void ProxyWorkspaceModel::addWorkspace()
243 auto newWorkspace = WorkspaceManager::instance()->createWorkspace();
244 m_original->m_unassignedWorkspaces.insert(newWorkspace);
246 (new ProxyWorkspace(newWorkspace))->assign(this);