File: Workspace.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 (178 lines) | stat: -rw-r--r-- 4,158 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
/*
 * 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 "Workspace.h"
#include "WorkspaceModel.h"
#include "WorkspaceManager.h"
#include "TopLevelWindowModel.h"
#include "Screen.h"

#include "wmpolicyinterface.h"

int nextWorkspace = 0;

Workspace::Workspace(QObject *parent)
    : QObject(parent)
    , m_workspace(WMPolicyInterface::instance()->createWorkspace())
    , m_model(nullptr)
{
    setObjectName((QString("Wks%1").arg(nextWorkspace++)));
}

Workspace::Workspace(const Workspace &other)
    : QObject(nullptr)
    , m_workspace(other.m_workspace)
    , m_model(nullptr)
{
    setObjectName(other.objectName());

    connect(&other, &Workspace::activeChanged, this, &Workspace::activeChanged);
}

Workspace::~Workspace()
{
    if (m_model) {
        m_model->remove(this);
    }
}

void Workspace::assign(WorkspaceModel *model, const QVariant& vIndex)
{
    if (m_model == model) return;

    if (m_model) {
        disconnect(m_model, 0, this, 0);
        m_model->remove(this);
    }

    m_model = model;

    if (model) {
        int index = m_model->rowCount();
        if (vIndex.isValid() && vIndex.canConvert(QVariant::Int)) {
            index = vIndex.toInt();
        }
        m_model->insert(index, this);

        connect(m_model, &QObject::destroyed, this, [this]() {
            m_model = nullptr;
            Q_EMIT unassigned();
        });
        Q_EMIT assigned();
    } else {
        Q_EMIT unassigned();
    }
}

void Workspace::unassign()
{
    assign(nullptr);
}

bool Workspace::isAssigned() const
{
    return m_model != nullptr;
}

bool Workspace::isSameAs(Workspace *wks) const
{
    if (!wks) return false;
    if (wks == this) return true;
    return wks->workspace() == workspace();
}


ConcreteWorkspace::ConcreteWorkspace(QObject *parent)
    : Workspace(parent)
    , m_active(false)
    , m_windowModel(new TopLevelWindowModel(this))
{
    connect(WorkspaceManager::instance(), &WorkspaceManager::activeWorkspaceChanged, this, [this](Workspace* activeWorkspace) {
        bool newActive = activeWorkspace == this;
        if (newActive != m_active) {
            m_active = newActive;
            Q_EMIT activeChanged(m_active);

            if (m_active) {
                WMPolicyInterface::instance()->setActiveWorkspace(m_workspace);
            }
        }
    });
}

ConcreteWorkspace::~ConcreteWorkspace()
{
    WorkspaceManager::instance()->destroyWorkspace(this);
    WMPolicyInterface::instance()->releaseWorkspace(m_workspace);
}

TopLevelWindowModel *ConcreteWorkspace::windowModel() const
{
    return m_windowModel.data();
}

void ConcreteWorkspace::activate()
{
    WorkspaceManager::instance()->setActiveWorkspace(this);
}

void ConcreteWorkspace::setCurrentOn(Screen *screen)
{
    if (screen) {
        screen->setCurrentWorkspace(this);
    }
}


ProxyWorkspace::ProxyWorkspace(Workspace * const workspace)
    : Workspace(*workspace)
    , m_original(workspace)
{
}

void ProxyWorkspace::assign(WorkspaceModel *model, const QVariant &index)
{
    Workspace::assign(model, index);
}

void ProxyWorkspace::unassign()
{
    Workspace::unassign();
}

bool ProxyWorkspace::isActive() const
{
    return m_original ? m_original->isActive() : false;
}

TopLevelWindowModel *ProxyWorkspace::windowModel() const
{
    return m_original ? m_original->windowModel() : nullptr;
}

void ProxyWorkspace::activate()
{
    if (m_original) {
        m_original->activate();
    }
}

void ProxyWorkspace::setCurrentOn(Screen *screen)
{
    if (screen && m_original) {
        screen->setCurrentWorkspace(m_original);
    }
}