on 08bf9db help sign in
unity8/tests/mocks/Unity/fake_department.cpp
127 lines 3.3 kB history raw
1/*
2 * Copyright (C) 2014 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 "fake_department.h"
19#include "fake_scope.h"
21#include <QTimer>
23Department::Department(const QString& departmentId, const QString& label, const QString& allLabel, const QString& parentId, const QString& parentLabel, Scope* scope)
24 : m_departmentId(departmentId)
25 , m_label(label)
26 , m_allLabel(allLabel)
27 , m_parentId(parentId)
28 , m_parentLabel(parentLabel)
29 , m_loaded(false)
30 , m_scope(scope)
32 QTimer::singleShot(1500, this, SLOT(slotLoaded()));
33 connect(scope, SIGNAL(currentDepartmentIdChanged()), this, SLOT(slotCurrentDepartmentChanged()));
36QString Department::departmentId() const
38 return m_departmentId;
41QString Department::label() const
43 return m_label;
46QString Department::allLabel() const
48 return m_allLabel;
51QString Department::parentDepartmentId() const
53 return m_parentId;
56QString Department::parentLabel() const
58 return m_parentLabel;
61void Department::slotLoaded()
63 m_loaded = true;
64 Q_EMIT loadedChanged();
67bool Department::loaded() const
69 return m_loaded;
72int Department::count() const
74 return rowCount();
77bool Department::isRoot() const
79 return m_departmentId == "root";
82int Department::rowCount(const QModelIndex & /*parent*/) const
84 if (!m_loaded || m_departmentId.startsWith("child") || m_departmentId == "middle3")
85 return 0;
86 else
87 return 8;
90QHash<int, QByteArray> Department::roleNames() const
92 QHash<int, QByteArray> res;
93 res[RoleDepartmentId] = "departmentId";
94 res[RoleLabel] = "label";
95 res[RoleHasChildren] = "hasChildren";
96 res[RoleIsActive] = "isActive";
97 return res;
100QVariant Department::data(const QModelIndex &index, int role) const
102 switch (role) {
103 case RoleDepartmentId:
104 if (m_departmentId == "root")
105 return QString("middle%1").arg(index.row());
106 else if (m_departmentId.startsWith("middle"))
107 return QString("child%1%2").arg(m_departmentId).arg(index.row());
108 break;
109 case RoleLabel:
110 return QString("%1Child%2").arg(m_departmentId).arg(index.row());
111 break;
112 case RoleHasChildren:
113 return m_departmentId == "root" && index.row() != 3;
114 break;
115 case RoleIsActive:
116 return m_scope->currentDepartmentId() == data(index, RoleDepartmentId);
117 break;
118 }
119 return QVariant();
122void Department::slotCurrentDepartmentChanged()
124 // This is wasteful, should only emit it if really something changed in this
125 // deparment, but this is a mock, so no need to optimize
126 Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0), QVector<int>() << RoleIsActive);