on 713e430 help sign in
unity8/tests/mocks/Unity/fake_navigation.cpp
136 lines 3.7 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::query() const
45 return "navigation:/" + m_navigationId;
48QString Navigation::label() const
50 return m_label;
53QString Navigation::allLabel() const
55 return m_allLabel;
58QString Navigation::parentNavigationId() const
60 return m_parentId;
63QString Navigation::parentQuery() const
65 return "navigation:/" + m_parentId;
68QString Navigation::parentLabel() const
70 return m_parentLabel;
73void Navigation::slotLoaded()
75 m_loaded = true;
76 Q_EMIT loadedChanged();
79bool Navigation::loaded() const
81 return m_loaded;
84int Navigation::count() const
86 return rowCount();
89bool Navigation::isRoot() const
91 return m_navigationId == "root" || m_navigationId == "altroot";
94bool Navigation::hidden() const
96 return m_navigationId == "altroot";
99int Navigation::rowCount(const QModelIndex & /*parent*/) const
101 if (!m_loaded || m_navigationId.startsWith("child") || m_navigationId == "middle3")
102 return 0;
103 else
104 return 8;
107QVariant Navigation::data(const QModelIndex &index, int role) const
109 switch (role) {
110 case RoleQuery:
111 return QString("navigation:/%1").arg(data(index, RoleNavigationId).toString());
112 case RoleNavigationId:
113 if (m_navigationId == "root")
114 return QString("middle%1").arg(index.row());
115 else if (m_navigationId.startsWith("middle"))
116 return QString("child%1%2").arg(m_navigationId).arg(index.row());
117 case RoleLabel:
118 return QString("%1Child%2").arg(m_navigationId).arg(index.row());
119 case RoleHasChildren:
120 return m_navigationId == "root" && index.row() != 3;
121 case RoleIsActive:
122 if (m_navigationId.startsWith("alt")) {
123 return m_scope->currentAltNavigationId() == data(index, RoleNavigationId);
124 } else {
125 return m_scope->currentNavigationId() == data(index, RoleNavigationId);
126 }
127 }
128 return QVariant();
131void Navigation::slotCurrentNavigationChanged()
133 // This is wasteful, should only emit it if really something changed in this
134 // deparment, but this is a mock, so no need to optimize
135 Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0), QVector<int>() << RoleIsActive);