on b8a5dcc help sign in
156 lines 3.7 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 "Workspace.h"
18#include "WorkspaceModel.h"
19#include "WorkspaceManager.h"
20#include "TopLevelWindowModel.h"
22#include "wmpolicyinterface.h"
24int nextWorkspace = 0;
26Workspace::Workspace(QObject *parent)
27 : QObject(parent)
28 , m_workspace(WMPolicyInterface::instance()->createWorkspace())
29 , m_model(nullptr)
31 setObjectName((QString("Wks%1").arg(nextWorkspace++)));
34Workspace::Workspace(const Workspace &other)
35 : QObject(nullptr)
36 , m_workspace(other.m_workspace)
37 , m_model(nullptr)
39 setObjectName(other.objectName());
41 connect(&other, &Workspace::activeChanged, this, &Workspace::activeChanged);
44Workspace::~Workspace()
46 if (m_model) {
47 m_model->remove(this);
48 }
51void Workspace::assign(WorkspaceModel *model, const QVariant& vIndex)
53 if (m_model == model) return;
55 if (m_model) {
56 disconnect(m_model, 0, this, 0);
57 m_model->remove(this);
58 }
60 m_model = model;
62 if (model) {
63 int index = m_model->rowCount();
64 if (vIndex.isValid() && vIndex.canConvert(QVariant::Int)) {
65 index = vIndex.toInt();
66 }
67 m_model->insert(index, this);
69 connect(m_model, &QObject::destroyed, this, [this]() {
70 m_model = nullptr;
71 Q_EMIT unassigned();
72 });
73 Q_EMIT assigned();
74 } else {
75 Q_EMIT unassigned();
76 }
79void Workspace::unassign()
81 assign(nullptr);
84bool Workspace::isAssigned() const
86 return m_model != nullptr;
90ConcreteWorkspace::ConcreteWorkspace(QObject *parent)
91 : Workspace(parent)
92 , m_active(false)
93 , m_windowModel(new TopLevelWindowModel(this))
95 connect(WorkspaceManager::instance(), &WorkspaceManager::activeWorkspaceChanged, this, [this](Workspace* activeWorkspace) {
96 bool newActive = activeWorkspace == this;
97 if (newActive != m_active) {
98 m_active = newActive;
99 Q_EMIT activeChanged(m_active);
101 if (m_active) {
102 WMPolicyInterface::instance()->setActiveWorkspace(m_workspace);
103 }
104 }
105 });
108ConcreteWorkspace::~ConcreteWorkspace()
110 WorkspaceManager::instance()->destroyWorkspace(this);
111 WMPolicyInterface::instance()->releaseWorkspace(m_workspace);
114TopLevelWindowModel *ConcreteWorkspace::windowModel() const
116 return m_windowModel.data();
119void ConcreteWorkspace::activate()
121 WorkspaceManager::instance()->setActiveWorkspace(this);
125ProxyWorkspace::ProxyWorkspace(Workspace * const workspace)
126 : Workspace(*workspace)
127 , m_original(workspace)
131void ProxyWorkspace::assign(WorkspaceModel *model, const QVariant &index)
133 Workspace::assign(model, index);
136void ProxyWorkspace::unassign()
138 Workspace::unassign();
141bool ProxyWorkspace::isActive() const
143 return m_original ? m_original->isActive() : false;
146TopLevelWindowModel *ProxyWorkspace::windowModel() const
148 return m_original ? m_original->windowModel() : nullptr;
151void ProxyWorkspace::activate()
153 if (m_original) {
154 m_original->activate();
155 }