File: WorkspaceModel.cpp

package info (click to toggle)
lomiri 0.5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (254 lines) | stat: -rw-r--r-- 7,188 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Copyright (C) 2017 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "WorkspaceModel.h"
#include "WorkspaceManager.h"
#include "Workspace.h"
#include "Screen.h"

#include <QQmlEngine>

Q_LOGGING_CATEGORY(WORKSPACES, "Workspaces", QtInfoMsg)

#define DEBUG_MSG qCDebug(WORKSPACES).nospace().noquote() << __func__
#define INFO_MSG qCInfo(WORKSPACES).nospace().noquote() << __func__

WorkspaceModel::WorkspaceModel(QObject *parent)
    : QAbstractListModel(parent)
{
}

WorkspaceModel::~WorkspaceModel()
{
    qDeleteAll(m_workspaces.toList()); // make a copy so the list doesnt edit itself during delete.
    m_workspaces.clear();
}

void WorkspaceModel::append(Workspace *workspace)
{
    insert(m_workspaces.count(), workspace);
}

void WorkspaceModel::insert(int index, Workspace *workspace)
{
    beginInsertRows(QModelIndex(), index, index);

    m_workspaces.insert(index, workspace);

    endInsertRows();

    Q_EMIT workspaceInserted(index, workspace);
    Q_EMIT countChanged();
}

void WorkspaceModel::remove(Workspace *workspace)
{
    int index = m_workspaces.indexOf(workspace);
    if (index < 0) return;

    beginRemoveRows(QModelIndex(), index, index);

    m_workspaces.removeAt(index);
    insertUnassigned(workspace);

    endRemoveRows();

    Q_EMIT workspaceRemoved(workspace);
    Q_EMIT countChanged();
}

void WorkspaceModel::move(int from, int to)
{
    if (from == to) return;
    DEBUG_MSG << " from=" << from << " to=" << to;

    if (from >= 0 && from < m_workspaces.size() && to >= 0 && to < m_workspaces.size()) {
        QModelIndex parent;

        beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
        const auto &window = m_windowModel.takeAt(from);
        m_workspaces.insert(to, window);
#else
        m_workspaces.move(from, to);
#endif
        endMoveRows();

        Q_EMIT workspaceMoved(from, to);
    }
}

int WorkspaceModel::indexOf(Workspace *workspace) const
{
    return m_workspaces.indexOf(workspace);
}

Workspace *WorkspaceModel::get(int index) const
{
    if (index < 0 || index >= rowCount()) return nullptr;
    return m_workspaces.at(index);
}

int WorkspaceModel::rowCount(const QModelIndex &) const
{
    return m_workspaces.count();
}

QVariant WorkspaceModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= m_workspaces.size())
        return QVariant();

    if (role == WorkspaceRole) {
        Workspace *workspace = m_workspaces.at(index.row());
        return QVariant::fromValue(workspace);
    } else {
        return QVariant();
    }
}

void WorkspaceModel::sync(WorkspaceModel *proxy)
{
    if (!proxy) return;
    const auto& proxyList = proxy->list();

    // check for removals
    int removedIndexWhichWasActive = -1;
    QVector<Workspace*> dpCpy(this->list());
    Q_FOREACH(auto workspace, dpCpy) {

        bool found = false;
        Q_FOREACH(auto p, proxyList) {
            auto workspaceProxy = qobject_cast<ProxyWorkspace*>(p);
            if (workspaceProxy->proxyObject() == workspace) {
                found = true;
                break;
            }
        }
        if (!found) {
            if (workspace->isActive()) {
                removedIndexWhichWasActive = indexOf(workspace);
            }
            workspace->unassign();
        }
    }

    // existing
    QSet<Workspace*> newWorkspaces;
    for (int i = 0; i < proxyList.count(); i++) {
        auto workspaceProxy = qobject_cast<ProxyWorkspace*>(proxyList[i]);
        auto workspace = workspaceProxy->proxyObject();

        int oldIndex = this->indexOf(workspace);

        if (oldIndex < 0) {
            workspace->assign(this, QVariant(i));
        } else if (oldIndex != i) {
            this->move(oldIndex, i);
        }
        newWorkspaces.insert(workspace);
    }

    // Make sure we have at least one workspace in the model.
    if (rowCount() == 0) {
        Workspace* workspace = WorkspaceManager::instance()->createWorkspace();
        workspace->assign(this);
        (new ProxyWorkspace(workspace))->assign(proxy);
    }

    if (removedIndexWhichWasActive != -1) {
        int newActiveIndex = qMin(removedIndexWhichWasActive, this->rowCount()-1);
        Workspace* newActiveWorkspace = newActiveIndex >= 0 ? this->get(newActiveIndex) : nullptr;

        WorkspaceManager::instance()->setActiveWorkspace(newActiveWorkspace);
    }

    proxy->finishSync();
    finishSync();
}

void WorkspaceModel::finishSync()
{
    QSet<Workspace*> dpCpy(m_unassignedWorkspaces);
    Q_FOREACH(auto workspace, dpCpy) {
        delete workspace;
    }
    m_unassignedWorkspaces.clear();
}

void WorkspaceModel::insertUnassigned(Workspace *workspace)
{
    m_unassignedWorkspaces.insert(workspace);
    connect(workspace, &Workspace::assigned, this, [=]() {
        m_unassignedWorkspaces.remove(workspace);
        disconnect(workspace, &Workspace::assigned, this, 0);
    });
    connect(workspace, &QObject::destroyed, this, [=]() {
        m_unassignedWorkspaces.remove(workspace);
    });
}


ProxyWorkspaceModel::ProxyWorkspaceModel(WorkspaceModel * const model, ProxyScreen* screen)
    : m_original(model)
    , m_screen(screen)
{
    Q_FOREACH(auto workspace, model->list()) {
        auto proxy = new ProxyWorkspace(workspace);
        QQmlEngine::setObjectOwnership(proxy, QQmlEngine::CppOwnership);
        proxy->assign(this);
    }
    connect(m_original, &WorkspaceModel::workspaceInserted, this, [this](int index, Workspace* inserted) {
        if (isSyncing()) return;

        (new ProxyWorkspace(inserted))->assign(this, index);
    });
    connect(m_original, &WorkspaceModel::workspaceRemoved, this, [this](Workspace* removed) {
        if (isSyncing()) return;

        for (int i = 0; i < rowCount(); i++) {
            auto workspaceProxy = qobject_cast<ProxyWorkspace*>(get(i));
            auto w = workspaceProxy->proxyObject();
            if (w == removed) {
                remove(workspaceProxy);
                break;
            }
        }
    });
    connect(m_original, &WorkspaceModel::workspaceMoved, this, [this](int from, int to) {
        if (isSyncing()) return;

        move(from, to);
    });
}

void ProxyWorkspaceModel::move(int from, int to)
{
    WorkspaceModel::move(from, to);
}

bool ProxyWorkspaceModel::isSyncing() const
{
    return m_screen->isSyncing();
}

void ProxyWorkspaceModel::addWorkspace()
{
    auto newWorkspace = WorkspaceManager::instance()->createWorkspace();
    m_original->insertUnassigned(newWorkspace);

    (new ProxyWorkspace(newWorkspace))->assign(this);
}