2 * Copyright (C) 2017 Canonical, Ltd.
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.
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.
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/>.
17#include "WorkspaceModel.h"
18#include "WorkspaceManager.h"
24Q_LOGGING_CATEGORY(WORKSPACES, "Workspaces", QtInfoMsg)
26#define DEBUG_MSG qCDebug(WORKSPACES).nospace().noquote() << __func__
27#define INFO_MSG qCInfo(WORKSPACES).nospace().noquote() << __func__
29WorkspaceModel::WorkspaceModel(QObject *parent)
30 : QAbstractListModel(parent)
34WorkspaceModel::~WorkspaceModel()
36 qDeleteAll(m_workspaces.toList()); // make a copy so the list doesnt edit itself during delete.
40void WorkspaceModel::append(Workspace *workspace)
42 insert(m_workspaces.count(), workspace);
45void WorkspaceModel::insert(int index, Workspace *workspace)
47 beginInsertRows(QModelIndex(), index, index);
49 m_workspaces.insert(index, workspace);
53 Q_EMIT workspaceInserted(index, workspace);
54 Q_EMIT countChanged();
57void WorkspaceModel::remove(Workspace *workspace)
59 int index = m_workspaces.indexOf(workspace);
60 if (index < 0) return;
62 beginRemoveRows(QModelIndex(), index, index);
64 m_workspaces.removeAt(index);
65 insertUnassigned(workspace);
69 Q_EMIT workspaceRemoved(workspace);
70 Q_EMIT countChanged();
73void WorkspaceModel::move(int from, int to)
75 if (from == to) return;
76 DEBUG_MSG << " from=" << from << " to=" << to;
78 if (from >= 0 && from < m_workspaces.size() && to >= 0 && to < m_workspaces.size()) {
81 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
82#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
83 const auto &window = m_windowModel.takeAt(from);
84 m_workspaces.insert(to, window);
86 m_workspaces.move(from, to);
90 Q_EMIT workspaceMoved(from, to);
94int WorkspaceModel::indexOf(Workspace *workspace) const
96 return m_workspaces.indexOf(workspace);
99Workspace *WorkspaceModel::get(int index) const
101 if (index < 0 || index >= rowCount()) return nullptr;
102 return m_workspaces.at(index);
105int WorkspaceModel::rowCount(const QModelIndex &) const
107 return m_workspaces.count();
110QVariant WorkspaceModel::data(const QModelIndex &index, int role) const
112 if (index.row() < 0 || index.row() >= m_workspaces.size())
115 if (role == WorkspaceRole) {
116 Workspace *workspace = m_workspaces.at(index.row());
117 return QVariant::fromValue(workspace);
123void WorkspaceModel::sync(WorkspaceModel *proxy)
126 const auto& proxyList = proxy->list();
128 // check for removals
129 int removedIndexWhichWasActive = -1;
130 QVector<Workspace*> dpCpy(this->list());
131 Q_FOREACH(auto workspace, dpCpy) {
134 Q_FOREACH(auto p, proxyList) {
135 auto workspaceProxy = qobject_cast<ProxyWorkspace*>(p);
136 if (workspaceProxy->proxyObject() == workspace) {
142 if (workspace->isActive()) {
143 removedIndexWhichWasActive = indexOf(workspace);
145 workspace->unassign();
150 QSet<Workspace*> newWorkspaces;
151 for (int i = 0; i < proxyList.count(); i++) {
152 auto workspaceProxy = qobject_cast<ProxyWorkspace*>(proxyList[i]);
153 auto workspace = workspaceProxy->proxyObject();
155 int oldIndex = this->indexOf(workspace);
158 workspace->assign(this, QVariant(i));
159 } else if (oldIndex != i) {
160 this->move(oldIndex, i);
162 newWorkspaces.insert(workspace);
165 // Make sure we have at least one workspace in the model.
166 if (rowCount() == 0) {
167 Workspace* workspace = WorkspaceManager::instance()->createWorkspace();
168 workspace->assign(this);
169 (new ProxyWorkspace(workspace))->assign(proxy);
172 if (removedIndexWhichWasActive != -1) {
173 int newActiveIndex = qMin(removedIndexWhichWasActive, this->rowCount()-1);
174 Workspace* newActiveWorkspace = newActiveIndex >= 0 ? this->get(newActiveIndex) : nullptr;
176 WorkspaceManager::instance()->setActiveWorkspace(newActiveWorkspace);
183void WorkspaceModel::finishSync()
185 QSet<Workspace*> dpCpy(m_unassignedWorkspaces);
186 Q_FOREACH(auto workspace, dpCpy) {
189 m_unassignedWorkspaces.clear();
192void WorkspaceModel::insertUnassigned(Workspace *workspace)
194 m_unassignedWorkspaces.insert(workspace);
195 connect(workspace, &Workspace::assigned, this, [=]() {
196 m_unassignedWorkspaces.remove(workspace);
197 disconnect(workspace, &Workspace::assigned, this, 0);
199 connect(workspace, &QObject::destroyed, this, [=]() {
200 m_unassignedWorkspaces.remove(workspace);
205ProxyWorkspaceModel::ProxyWorkspaceModel(WorkspaceModel * const model, ProxyScreen* screen)
209 Q_FOREACH(auto workspace, model->list()) {
210 auto proxy = new ProxyWorkspace(workspace);
211 QQmlEngine::setObjectOwnership(proxy, QQmlEngine::CppOwnership);
214 connect(m_original, &WorkspaceModel::workspaceInserted, this, [this](int index, Workspace* inserted) {
215 if (isSyncing()) return;
217 (new ProxyWorkspace(inserted))->assign(this, index);
219 connect(m_original, &WorkspaceModel::workspaceRemoved, this, [this](Workspace* removed) {
220 if (isSyncing()) return;
222 for (int i = 0; i < rowCount(); i++) {
223 auto workspaceProxy = qobject_cast<ProxyWorkspace*>(get(i));
224 auto w = workspaceProxy->proxyObject();
226 remove(workspaceProxy);
231 connect(m_original, &WorkspaceModel::workspaceMoved, this, [this](int from, int to) {
232 if (isSyncing()) return;
238void ProxyWorkspaceModel::move(int from, int to)
240 WorkspaceModel::move(from, to);
243bool ProxyWorkspaceModel::isSyncing() const
245 return m_screen->isSyncing();
248void ProxyWorkspaceModel::addWorkspace()
250 auto newWorkspace = WorkspaceManager::instance()->createWorkspace();
251 m_original->insertUnassigned(newWorkspace);
253 (new ProxyWorkspace(newWorkspace))->assign(this);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list