~mzanetti/unity8/workspace-backend-fix-imports

« back to all changes in this revision

Viewing changes to tests/mocks/WindowManager/Workspace.cpp

  • Committer: Nick Dedekind
  • Date: 2017-03-01 17:22:51 UTC
  • Revision ID: nick.dedekind@canonical.com-20170301172251-1q5dzmzdpczn6mex
mock

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 */
16
 
 
17
 
#include "Workspace.h"
18
 
#include "WorkspaceModel.h"
19
 
#include "WorkspaceManager.h"
20
 
#include "TopLevelWindowModel.h"
21
 
 
22
 
#include "windowmanagementpolicy.h"
23
 
 
24
 
Workspace::Workspace(QObject *parent)
25
 
    : QObject(parent)
26
 
    , m_model(nullptr)
27
 
    , m_active(false)
28
 
{
29
 
    connect(WorkspaceManager::instance(), &WorkspaceManager::activeWorkspaceChanged, this, [this]() {
30
 
        bool newActive = WorkspaceManager::instance()->activeWorkspace() == this;
31
 
        if (newActive != m_active) {
32
 
            m_active = newActive;
33
 
            Q_EMIT activeChanged(m_active);
34
 
        }
35
 
    });
36
 
}
37
 
 
38
 
Workspace::~Workspace()
39
 
{
40
 
    unassign();
41
 
}
42
 
 
43
 
void Workspace::assign(WorkspaceModel *model, const QVariant& vIndex)
44
 
{
45
 
    if (m_model == model) return;
46
 
 
47
 
    if (m_model) {
48
 
        disconnect(model);
49
 
        m_model->remove(this);
50
 
    }
51
 
 
52
 
    m_model = model ? model : WorkspaceManager::instance();
53
 
 
54
 
    if (m_model) {
55
 
        int index = m_model->rowCount();
56
 
        if (vIndex.isValid() && vIndex.canConvert(QVariant::Int)) {
57
 
            index = vIndex.toInt();
58
 
        }
59
 
        m_model->insert(index, this);
60
 
 
61
 
        connect(m_model, &QObject::destroyed, this, [this]() {
62
 
            m_model->remove(this);
63
 
            m_model = nullptr;
64
 
        });
65
 
        Q_EMIT assigned();
66
 
    }
67
 
}
68
 
 
69
 
void Workspace::activate()
70
 
{
71
 
    WorkspaceManager::instance()->setActiveWorkspace(this);
72
 
}
73
 
 
74
 
void Workspace::unassign()
75
 
{
76
 
    assign(nullptr);
77
 
}