on 76cbe21 help sign in
unity8/tests/mocks/Unity/fake_navigation.cpp
124 lines 3.4 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()));
35 connect(scope, SIGNAL(currentAltNavigationIdChanged()), this, SLOT(slotCurrentNavigationChanged()));
38QString Navigation::navigationId() const
40 return m_navigationId;
43QString Navigation::label() const
45 return m_label;
48QString Navigation::allLabel() const
50 return m_allLabel;
53QString Navigation::parentNavigationId() const
55 return m_parentId;
58QString Navigation::parentLabel() const
60 return m_parentLabel;
63void Navigation::slotLoaded()
65 m_loaded = true;
66 Q_EMIT loadedChanged();
69bool Navigation::loaded() const
71 return m_loaded;
74int Navigation::count() const
76 return rowCount();
79bool Navigation::isRoot() const
81 return m_navigationId == "root" || m_navigationId == "altroot";
84bool Navigation::hidden() const
86 return m_navigationId == "altroot";
89int Navigation::rowCount(const QModelIndex & /*parent*/) const
91 if (!m_loaded || m_navigationId.startsWith("child") || m_navigationId == "middle3")
92 return 0;
93 else
94 return 8;
97QVariant Navigation::data(const QModelIndex &index, int role) const
99 switch (role) {
100 case RoleNavigationId:
101 if (m_navigationId == "root")
102 return QString("middle%1").arg(index.row());
103 else if (m_navigationId.startsWith("middle"))
104 return QString("child%1%2").arg(m_navigationId).arg(index.row());
105 case RoleLabel:
106 return QString("%1Child%2").arg(m_navigationId).arg(index.row());
107 case RoleHasChildren:
108 return m_navigationId == "root" && index.row() != 3;
109 case RoleIsActive:
110 if (m_navigationId.startsWith("alt")) {
111 return m_scope->currentAltNavigationId() == data(index, RoleNavigationId);
112 } else {
113 return m_scope->currentNavigationId() == data(index, RoleNavigationId);
114 }
115 }
116 return QVariant();
119void Navigation::slotCurrentNavigationChanged()
121 // This is wasteful, should only emit it if really something changed in this
122 // deparment, but this is a mock, so no need to optimize
123 Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0), QVector<int>() << RoleIsActive);