on b3c03fa help sign in
146 lines 3.6 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 "windowmanagementpolicy.h"
24int nextWorkspace = 0;
26Workspace::Workspace(QObject *parent)
27 : QObject(parent)
28 , m_workspace(WindowManagementPolicy::instance()->createWorkspace())
29 , m_model(nullptr)
30 , m_active(false)
32 setObjectName((QString("Wks%1").arg(nextWorkspace++)));
34 connect(WorkspaceManager::instance(), &WorkspaceManager::activeWorkspaceChanged, this, [this]() {
35 bool newActive = WorkspaceManager::instance()->activeWorkspace() == this;
36 if (newActive != m_active) {
37 m_active = newActive;
38 Q_EMIT activeChanged(m_active);
40 if (m_active) {
41 WindowManagementPolicy::instance()->setActiveWorkspace(m_workspace);
42 }
43 }
44 });
47Workspace::Workspace(const Workspace &other)
48 : QObject(nullptr)
49 , m_workspace(other.m_workspace)
50 , m_model(nullptr)
51 , m_active(other.m_active)
53 setObjectName(other.objectName());
55 connect(&other, &Workspace::activeChanged, this, [this](bool active) {
56 m_active = active;
57 Q_EMIT activeChanged(m_active);
58 });
61Workspace::~Workspace()
63 if (m_model) {
64 m_model->remove(this);
65 }
68void Workspace::moveWindowsTo(Workspace *workspace)
70 WindowManagementPolicy::instance()->moveWorkspaceContentToWorkspace(workspace->m_workspace, m_workspace);
73void Workspace::activate()
75 WorkspaceManager::instance()->setActiveWorkspace(this);
78void Workspace::assign(WorkspaceModel *model, const QVariant& vIndex)
80 if (m_model == model) return;
82 if (m_model) {
83 disconnect(m_model, 0, this, 0);
84 m_model->remove(this);
85 }
87 m_model = model;
89 if (model) {
90 int index = m_model->rowCount();
91 if (vIndex.isValid() && vIndex.canConvert(QVariant::Int)) {
92 index = vIndex.toInt();
93 }
94 m_model->insert(index, this);
96 connect(m_model, &QObject::destroyed, this, [this]() {
97 m_model = nullptr;
98 Q_EMIT unassigned();
99 });
100 Q_EMIT assigned();
101 } else {
102 Q_EMIT unassigned();
103 }
106void Workspace::unassign()
108 assign(nullptr);
111void Workspace::release()
113 WindowManagementPolicy::instance()->releaseWorkspace(m_workspace);
114 deleteLater();
117bool Workspace::isAssigned() const
119 return m_model != nullptr;
122WorkspaceProxy::WorkspaceProxy(Workspace * const workspace)
123 : Workspace(*workspace)
124 , m_original(workspace)
128void WorkspaceProxy::assign(WorkspaceModel *model, const QVariant &index)
130 Workspace::assign(model, index);
133void WorkspaceProxy::unassign()
135 Workspace::unassign();
138bool WorkspaceProxy::isActive() const
140 return m_original ? m_original->isActive() : false;
143void WorkspaceProxy::activate()
145 if (m_original) m_original->activate();