on de450aa help sign in
178 lines 4.2 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) return false;
93 if (wks == this) return true;
94 return wks->workspace() == workspace();
98ConcreteWorkspace::ConcreteWorkspace(QObject *parent)
99 : Workspace(parent)
100 , m_active(false)
101 , m_windowModel(new TopLevelWindowModel(this))
103 connect(WorkspaceManager::instance(), &WorkspaceManager::activeWorkspaceChanged, this, [this](Workspace* activeWorkspace) {
104 bool newActive = activeWorkspace == this;
105 if (newActive != m_active) {
106 m_active = newActive;
107 Q_EMIT activeChanged(m_active);
109 if (m_active) {
110 WMPolicyInterface::instance()->setActiveWorkspace(m_workspace);
111 }
112 }
113 });
116ConcreteWorkspace::~ConcreteWorkspace()
118 WorkspaceManager::instance()->destroyWorkspace(this);
119 WMPolicyInterface::instance()->releaseWorkspace(m_workspace);
122TopLevelWindowModel *ConcreteWorkspace::windowModel() const
124 return m_windowModel.data();
127void ConcreteWorkspace::activate()
129 WorkspaceManager::instance()->setActiveWorkspace(this);
132void ConcreteWorkspace::setCurrentOn(Screen *screen)
134 if (screen) {
135 screen->setCurrentWorkspace(this);
136 }
140ProxyWorkspace::ProxyWorkspace(Workspace * const workspace)
141 : Workspace(*workspace)
142 , m_original(workspace)
146void ProxyWorkspace::assign(WorkspaceModel *model, const QVariant &index)
148 Workspace::assign(model, index);
151void ProxyWorkspace::unassign()
153 Workspace::unassign();
156bool ProxyWorkspace::isActive() const
158 return m_original ? m_original->isActive() : false;
161TopLevelWindowModel *ProxyWorkspace::windowModel() const
163 return m_original ? m_original->windowModel() : nullptr;
166void ProxyWorkspace::activate()
168 if (m_original) {
169 m_original->activate();
170 }
173void ProxyWorkspace::setCurrentOn(Screen *screen)
175 if (screen && m_original) {
176 screen->setCurrentWorkspace(m_original);
177 }