on 27430d5 help sign in
177 lines 4.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 "Workspace.h"
18#include "WorkspaceModel.h"
19#include "WorkspaceManager.h"
20#include "TopLevelWindowModel.h"
21#include "Screen.h"
23#include "wmpolicyinterface.h"
25int nextWorkspace = 0;
27Workspace::Workspace(QObject *parent)
28 : QObject(parent)
29 , m_workspace(WMPolicyInterface::instance()->createWorkspace())
30 , m_model(nullptr)
32 setObjectName((QString("Wks%1").arg(nextWorkspace++)));
35Workspace::Workspace(const Workspace &other)
36 : QObject(nullptr)
37 , m_workspace(other.m_workspace)
38 , m_model(nullptr)
40 setObjectName(other.objectName());
42 connect(&other, &Workspace::activeChanged, this, &Workspace::activeChanged);
45Workspace::~Workspace()
47 if (m_model) {
48 m_model->remove(this);
49 }
52void Workspace::assign(WorkspaceModel *model, const QVariant& vIndex)
54 if (m_model == model) return;
56 if (m_model) {
57 disconnect(m_model, 0, this, 0);
58 m_model->remove(this);
59 }
61 m_model = model;
63 if (model) {
64 int index = m_model->rowCount();
65 if (vIndex.isValid() && vIndex.canConvert(QVariant::Int)) {
66 index = vIndex.toInt();
67 }
68 m_model->insert(index, this);
70 connect(m_model, &QObject::destroyed, this, [this]() {
71 m_model = nullptr;
72 Q_EMIT unassigned();
73 });
74 Q_EMIT assigned();
75 } else {
76 Q_EMIT unassigned();
77 }
80void Workspace::unassign()
82 assign(nullptr);
85bool Workspace::isAssigned() const
87 return m_model != nullptr;
90bool Workspace::isSameAs(Workspace *wks) const
92 if (wks == this) return true;
93 return wks->workspace() == workspace();
97ConcreteWorkspace::ConcreteWorkspace(QObject *parent)
98 : Workspace(parent)
99 , m_active(false)
100 , m_windowModel(new TopLevelWindowModel(this))
102 connect(WorkspaceManager::instance(), &WorkspaceManager::activeWorkspaceChanged, this, [this](Workspace* activeWorkspace) {
103 bool newActive = activeWorkspace == this;
104 if (newActive != m_active) {
105 m_active = newActive;
106 Q_EMIT activeChanged(m_active);
108 if (m_active) {
109 WMPolicyInterface::instance()->setActiveWorkspace(m_workspace);
110 }
111 }
112 });
115ConcreteWorkspace::~ConcreteWorkspace()
117 WorkspaceManager::instance()->destroyWorkspace(this);
118 WMPolicyInterface::instance()->releaseWorkspace(m_workspace);
121TopLevelWindowModel *ConcreteWorkspace::windowModel() const
123 return m_windowModel.data();
126void ConcreteWorkspace::activate()
128 WorkspaceManager::instance()->setActiveWorkspace(this);
131void ConcreteWorkspace::setCurrentOn(Screen *screen)
133 if (screen) {
134 screen->setCurrentWorkspace(this);
135 }
139ProxyWorkspace::ProxyWorkspace(Workspace * const workspace)
140 : Workspace(*workspace)
141 , m_original(workspace)
145void ProxyWorkspace::assign(WorkspaceModel *model, const QVariant &index)
147 Workspace::assign(model, index);
150void ProxyWorkspace::unassign()
152 Workspace::unassign();
155bool ProxyWorkspace::isActive() const
157 return m_original ? m_original->isActive() : false;
160TopLevelWindowModel *ProxyWorkspace::windowModel() const
162 return m_original ? m_original->windowModel() : nullptr;
165void ProxyWorkspace::activate()
167 if (m_original) {
168 m_original->activate();
169 }
172void ProxyWorkspace::setCurrentOn(Screen *screen)
174 if (screen && m_original) {
175 screen->setCurrentWorkspace(m_original);
176 }