on xenial help sign in
The new tabbar code mandates a get function to exist so that it can check roles that it needs are there, provide one
commit0290ded8af3ad46bd5379537594863d3766840e9 authorAlbert Astals <albert.astals@canonical.com> dateWed 27 Nov 2013, 13:49 (13 years ago) parent262fb79
5 files changed+18−276
plugins/Unity/scopes.cpp.THIS−179 plugins/Unity/scopes.h.THIS−95 plugins/Utils/qsortfilterproxymodelqml.cpp+12 plugins/Utils/qsortfilterproxymodelqml.h+1 tests/qmltests/Dash/tst_DashContent.qml+5−2
plugins/Unity/scopes.cpp.THISdeleted−179
@@ -1,179 +0,0 @@
1−/*
2− * Copyright (C) 2011 Canonical, Ltd.
3− *
4− * Authors:
5− * Florian Boucault <florian.boucault@canonical.com>
6− *
7− * This program is free software; you can redistribute it and/or modify
8− * it under the terms of the GNU General Public License as published by
9− * the Free Software Foundation; version 3.
10− *
11− * This program is distributed in the hope that it will be useful,
12− * but WITHOUT ANY WARRANTY; without even the implied warranty of
13− * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14− * GNU General Public License for more details.
15− *
16− * You should have received a copy of the GNU General Public License
17− * along with this program. If not, see <http://www.gnu.org/licenses/>.
18− */
19−
20−// Self
21−#include "scopes.h"
22−
23−// Local
24−#include "scope.h"
25−
26−// Qt
27−#include <QDebug>
28−#include <QtCore/QStringList>
29−#include <QtGui/QKeySequence>
30−
31−Scopes::Scopes(QObject *parent)
32− : QAbstractListModel(parent)
33− , m_unityScopes(std::make_shared<unity::dash::GSettingsScopes>())
34− , m_loaded(false)
35−{
36− m_roles[Scopes::RoleScope] = "scope";
37− m_roles[Scopes::RoleId] = "id";
38− m_roles[Scopes::RoleVisible] = "visible";
39− m_roles[Scopes::RoleTitle] = "title";
40−
41− m_unityScopes->scope_added.connect(sigc::mem_fun(this, &Scopes::onScopeAdded));
42− m_unityScopes->scope_removed.connect(sigc::mem_fun(this, &Scopes::onScopeRemoved));
43− m_unityScopes->scopes_reordered.connect(sigc::mem_fun(this, &Scopes::onScopesReordered));
44− m_unityScopes->LoadScopes();
45−}
46−
47−QHash<int, QByteArray> Scopes::roleNames() const
48−{
49− return m_roles;
50−}
51−
52−int Scopes::rowCount(const QModelIndex& parent) const
53−{
54− Q_UNUSED(parent)
55−
56− return m_scopes.count();
57−}
58−
59−QVariant Scopes::data(const QModelIndex& index, int role) const
60−{
61− Q_UNUSED(role)
62−
63− if (!index.isValid()) {
64− return QVariant();
65− }
66−
67− Scope* scope = m_scopes.at(index.row());
68−
69− if (role == Scopes::RoleScope) {
70− return QVariant::fromValue(scope);
71− } else if (role == Scopes::RoleId) {
72− return QVariant::fromValue(scope->id());
73− } else if (role == Scopes::RoleVisible) {
74− return QVariant::fromValue(scope->visible());
75− } else if (role == Scopes::RoleTitle) {
76− return scope->name();
77− } else {
78− return QVariant();
79− }
80−}
81−
82−QVariant Scopes::get(int row) const
83−{
84− return data(QAbstractListModel::index(row), 0);
85−}
86−
87−QVariant Scopes::get(const QString& scope_id) const
88−{
89− Q_FOREACH(Scope* scope, m_scopes) {
90− if (scope->id() == scope_id) {
91− return QVariant::fromValue(scope);
92− }
93− }
94−
95− return QVariant();
96−}
97−
98−bool Scopes::loaded() const
99−{
100− return m_loaded;
101−}
102−
103−void Scopes::onScopeAdded(const unity::dash::Scope::Ptr& scope, int /*position*/)
104−{
105− int index = m_scopes.count();
106− beginInsertRows(QModelIndex(), index, index);
107− addUnityScope(scope);
108− endInsertRows();
109−
110− // FIXME: do only once after all loaded?
111− m_loaded = true;
112− Q_EMIT loadedChanged(m_loaded);
113−}
114−
115−void Scopes::onScopesLoaded()
116−{
117−}
118−
119−void Scopes::onScopeRemoved(const unity::dash::Scope::Ptr& scope)
120−{
121− auto id = QString::fromStdString(scope->id);
122− auto index = findScopeById(id);
123− if (index >= 0) {
124− beginRemoveRows(QModelIndex(), index, index);
125− removeUnityScope(index);
126− endRemoveRows();
127−
128− Q_EMIT scopeRemoved(id, index);
129− }
130−}
131−
132−void Scopes::onScopesReordered(const unity::dash::Scopes::ScopeList& scopes)
133−{
134− // FIXME Should use beginMoveRows()/endMoveRows() to not recreate the UI for every scope.
135− beginResetModel();
136−
137− // remove existing scopes
138− for (auto i=m_scopes.count()-1; i>=0; i--) {
139− removeUnityScope(i);
140− }
141−
142− // re-create scopes
143− for (uint i=0; i<scopes.size(); i++) {
144− addUnityScope(scopes[i]);
145− }
146− endResetModel();
147−}
148−
149−void Scopes::onScopePropertyChanged()
150−{
151− QModelIndex scopeIndex = index(m_scopes.indexOf(qobject_cast<Scope*>(sender())));
152− Q_EMIT dataChanged(scopeIndex, scopeIndex);
153−}
154−
155−void Scopes::addUnityScope(const unity::dash::Scope::Ptr& unity_scope)
156−{
157− Scope* scope = new Scope(this);
158− scope->setUnityScope(unity_scope);
159− /* DOCME */
160− QObject::connect(scope, SIGNAL(visibleChanged(bool)), this, SLOT(onScopePropertyChanged()));
161− m_scopes.append(scope);
162−}
163−
164−void Scopes::removeUnityScope(int index)
165−{
166− Scope* scope = m_scopes.takeAt(index);
167−
168− delete scope;
169−}
170−
171−int Scopes::findScopeById(const QString& scope_id)
172−{
173− for (int i=0; i<m_scopes.count(); i++) {
174− if (m_scopes[i]->id() == scope_id) {
175− return i;
176− }
177− }
178− return -1;
179−}
plugins/Unity/scopes.h.THISdeleted−95
@@ -1,95 +0,0 @@
1−/*
2− * Copyright (C) 2011 Canonical, Ltd.
3− *
4− * Authors:
5− * Florian Boucault <florian.boucault@canonical.com>
6− *
7− * This program is free software; you can redistribute it and/or modify
8− * it under the terms of the GNU General Public License as published by
9− * the Free Software Foundation; version 3.
10− *
11− * This program is distributed in the hope that it will be useful,
12− * but WITHOUT ANY WARRANTY; without even the implied warranty of
13− * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14− * GNU General Public License for more details.
15− *
16− * You should have received a copy of the GNU General Public License
17− * along with this program. If not, see <http://www.gnu.org/licenses/>.
18− */
19−
20−#ifndef SCOPES_H
21−#define SCOPES_H
22−
23−// Qt
24−#include <QAbstractListModel>
25−#include <QList>
26−
27−// libunity-core
28−#include <UnityCore/Scope.h>
29−#include <UnityCore/GSettingsScopes.h>
30−
31−namespace unity
32−{
33−namespace dash
34−{
35−class Scopes;
36−}
37−}
38−
39−class Hotkey;
40−class Scope;
41−
42−class Scopes : public QAbstractListModel
43−{
44− Q_OBJECT
45−
46− Q_ENUMS(Roles)
47−
48− Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged)
49−
50−public:
51− explicit Scopes(QObject *parent = 0);
52− ~Scopes() = default;
53−
54− enum Roles {
55− RoleScope,
56− RoleId,
57− RoleVisible,
58− RoleTitle
59− };
60−
61− QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
62− Q_INVOKABLE int rowCount(const QModelIndex& parent = QModelIndex()) const;
63−
64− Q_INVOKABLE QVariant get(int row) const;
65− Q_INVOKABLE QVariant get(const QString& scope_id) const;
66−
67− QHash<int, QByteArray> roleNames() const;
68−
69− bool loaded() const;
70−
71−Q_SIGNALS:
72− void activateScopeRequested(const QString& scope_id);
73− void loadedChanged(bool loaded);
74− void scopeRemoved(const QString& scope_id, int index);
75−
76−private Q_SLOTS:
77− void onScopesLoaded();
78− void onScopeAdded(const unity::dash::Scope::Ptr& scope, int position);
79− void onScopeRemoved(const unity::dash::Scope::Ptr& scope);
80− void onScopesReordered(const unity::dash::Scopes::ScopeList& scopes);
81− void onScopePropertyChanged();
82−
83−private:
84− unity::dash::GSettingsScopesReader::Ptr m_scopesReader;
85− unity::dash::Scopes::Ptr m_unityScopes;
86− QList<Scope*> m_scopes;
87− QHash<int, QByteArray> m_roles;
88− bool m_loaded;
89−
90− void addUnityScope(const unity::dash::Scope::Ptr& unity_scope);
91− void removeUnityScope(int index);
92− int findScopeById(const QString& scope_id);
93−};
94−
95−#endif // SCOPES_H
plugins/Utils/qsortfilterproxymodelqml.cpp+12
@@ -68,6 +68,18 @@ QSortFilterProxyModelQML::setModel(QAbstractItemModel *itemModel)
6868 }
6969}
7070
71+QVariantMap
72+QSortFilterProxyModelQML::get(int row)
73+{
74+ QVariantMap res;
75+ const QHash<int, QByteArray> roles = roleNames();
76+ auto it = roles.begin();
77+ for ( ; it != roles.end(); ++it) {
78+ res[*it] = data(row, it.key());
79+ }
80+ return res;
81+}
82+
7183QVariant
7284QSortFilterProxyModelQML::data(int row, int role)
7385{
plugins/Utils/qsortfilterproxymodelqml.h+1
@@ -31,6 +31,7 @@ class QSortFilterProxyModelQML : public QSortFilterProxyModel
3131public:
3232 explicit QSortFilterProxyModelQML(QObject *parent = 0);
3333
34+ Q_INVOKABLE QVariantMap get(int row); // Use with caution, it can be slow to query all the roles
3435 Q_INVOKABLE QVariant data(int row, int role);
3536 Q_INVOKABLE int count();
3637 Q_INVOKABLE int findFirst(int role, const QVariant& value) const;
tests/qmltests/Dash/tst_DashContent.qml+5−2
@@ -21,6 +21,7 @@ import "../../../Components"
2121import Ubuntu.Components 0.1
2222import Unity 0.1
2323import Unity.Test 0.1 as UT
24+import Utils 0.1
2425
2526Item {
2627 id: shell
@@ -55,7 +56,9 @@ Item {
5556 id: dashContent
5657 anchors.fill: parent
5758
58− model: scopesModel
59+ model: SortFilterProxyModel {
60+ model: scopesModel
61+ }
5962 scopes : scopesModel
6063
6164 scopeMapper : scopeDelegateMapper
@@ -110,7 +113,7 @@ Item {
110113
111114 UT.UnityTestCase {
112115 name: "DashContent"
113− when: windowShown
116+ when: scopesModel.loaded
114117
115118 function init() {
116119 scopesModel.clear();