on 129ee8b help sign in
unity8/tests/mocks/Unity/fake_navigation.cpp
119 lines 3.1 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_navigation.h"
19#include "fake_scope.h"
21#include <QDebug>
22#include <QTimer>
24Navigation::Navigation(const QString& navigationId, const QString& label, const QString& allLabel, const QString& parentId, const QString& parentLabel, Scope* scope)
25 : m_navigationId(navigationId)
26 , m_label(label)
27 , m_allLabel(allLabel)
28 , m_parentId(parentId)
29 , m_parentLabel(parentLabel)
30 , m_loaded(false)
31 , m_scope(scope)
33 QTimer::singleShot(1500, this, SLOT(slotLoaded()));
34 connect(scope, SIGNAL(currentNavigationIdChanged()), this, SLOT(slotCurrentNavigationChanged()));
37QString Navigation::navigationId() const
39 return m_navigationId;
42QString Navigation::label() const
44 return m_label;
47QString Navigation::allLabel() const
49 return m_allLabel;
52QString Navigation::parentNavigationId() const
54 return m_parentId;
57QString Navigation::parentLabel() const
59 return m_parentLabel;
62void Navigation::slotLoaded()
64 m_loaded = true;
65 Q_EMIT loadedChanged();
68bool Navigation::loaded() const
70 return m_loaded;
73int Navigation::count() const
75 return rowCount();
78bool Navigation::isRoot() const
80 return m_navigationId == "root" || m_navigationId == "altroot";
83bool Navigation::hidden() const
85 return m_navigationId == "altroot";
88int Navigation::rowCount(const QModelIndex & /*parent*/) const
90 if (!m_loaded || m_navigationId.startsWith("child") || m_navigationId == "middle3")
91 return 0;
92 else
93 return 8;
96QVariant Navigation::data(const QModelIndex &index, int role) const
98 switch (role) {
99 case RoleNavigationId:
100 if (m_navigationId == "root")
101 return QString("middle%1").arg(index.row());
102 else if (m_navigationId.startsWith("middle"))
103 return QString("child%1%2").arg(m_navigationId).arg(index.row());
104 case RoleLabel:
105 return QString("%1Child%2").arg(m_navigationId).arg(index.row());
106 case RoleHasChildren:
107 return m_navigationId == "root" && index.row() != 3;
108 case RoleIsActive:
109 return m_scope->currentNavigationId() == data(index, RoleNavigationId);
110 }
111 return QVariant();
114void Navigation::slotCurrentNavigationChanged()
116 // This is wasteful, should only emit it if really something changed in this
117 // deparment, but this is a mock, so no need to optimize
118 Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0), QVector<int>() << RoleIsActive);