on xenial help sign in
implement setFavorite in fake_scopes
commitfd86d641e3a5898a3ac5511a8bbd8329a7e5c9a3 authorAlbert Astals <albert.astals@canonical.com> dateFri 19 Sep 2014, 13:59 (12 years ago) parent442e110
4 files changed+98−8
tests/mocks/Unity/fake_scopes.cpp+33 tests/mocks/Unity/fake_scopes.h+6−1 tests/mocks/Unity/fake_scopesoverview.cpp+44−4 tests/mocks/Unity/fake_scopesoverview.h+15−3
tests/mocks/Unity/fake_scopes.cpp+33
@@ -165,6 +165,39 @@ unity::shell::scopes::ScopeInterface* Scopes::overviewScope() const
165165 return m_scopesOverview;
166166}
167167
168+void Scopes::setFavorite(const QString& scopeId, bool favorite)
169+{
170+ if (favorite) {
171+ for (Scope *scope : m_scopes) {
172+ // Check it's not already there
173+ Q_ASSERT(scope->id() != scopeId);
174+ }
175+ for (Scope *scope : m_allScopes) {
176+ if (scope->id() == scopeId) {
177+ const int index = rowCount();
178+ beginInsertRows(QModelIndex(), index, index);
179+ m_scopes << scope;
180+ endInsertRows();
181+ m_scopesOverview->setFavorite(scope, true);
182+ return;
183+ }
184+ }
185+ Q_ASSERT(false && "Unknown scopeId");
186+ } else {
187+ for (Scope *scope : m_scopes) {
188+ if (scope->id() == scopeId) {
189+ const int index = m_scopes.indexOf(scope);
190+ beginRemoveRows(QModelIndex(), index, index);
191+ m_scopes.removeAt(index);
192+ endRemoveRows();
193+ m_scopesOverview->setFavorite(scope, false);
194+ return;
195+ }
196+ }
197+ Q_ASSERT(false && "Unknown scopeId");
198+ }
199+}
200+
168201QList<Scope*> Scopes::favScopes() const
169202{
170203 return m_scopes;
tests/mocks/Unity/fake_scopes.h+6−1
@@ -28,6 +28,8 @@
2828#include <QList>
2929#include <QTimer>
3030
31+class ScopesOverview;
32+
3133class Scopes : public unity::shell::scopes::ScopesInterface
3234{
3335 Q_OBJECT
@@ -53,6 +55,9 @@ public:
5355 int count() const override;
5456 unity::shell::scopes::ScopeInterface* overviewScope() const override;
5557
58+ // TODO This will go to the api, mark as override
59+ Q_INVOKABLE void setFavorite(const QString& scopeId, bool favorite);
60+
5661 // This is used as part of implementation of the other C++ code, not API
5762 QList<Scope*> favScopes() const;
5863 QList<Scope*> nonFavScopes() const;
@@ -64,7 +69,7 @@ private Q_SLOTS:
6469private:
6570 QList<Scope*> m_scopes; // the favorite ones
6671 QList<Scope*> m_allScopes;
67− Scope *m_scopesOverview;
72+ ScopesOverview *m_scopesOverview;
6873 bool m_loaded;
6974 QTimer timer;
7075};
tests/mocks/Unity/fake_scopesoverview.cpp+44−4
@@ -48,6 +48,11 @@ Q_INVOKABLE void ScopesOverview::activate(QVariant const& result)
4848 Q_EMIT openScope(m_openScope);
4949}
5050
51+void ScopesOverview::setFavorite(Scope *scope, bool favorite)
52+{
53+ m_scopesOverviewCategories->setFavorite(scope, favorite);
54+}
55+
5156ScopesOverviewCategories::ScopesOverviewCategories(Scopes *scopes, QObject* parent)
5257 : unity::shell::scopes::CategoriesInterface(parent)
5358 , m_scopes(scopes)
@@ -69,6 +74,24 @@ bool ScopesOverviewCategories::overrideCategoryJson(QString const& /* categoryId
6974 qFatal("Using un-implemented ScopesOverviewCategories::overrideCategoryJson");
7075}
7176
77+void ScopesOverviewCategories::setFavorite(Scope *scope, bool favorite)
78+{
79+ if (m_resultsModels.value(0)) {
80+ if (favorite) {
81+ m_resultsModels[0]->appendScope(scope);
82+ } else {
83+ m_resultsModels[0]->removeScope(scope);
84+ }
85+ }
86+ if (m_resultsModels.value(1)) {
87+ if (favorite) {
88+ m_resultsModels[1]->removeScope(scope);
89+ } else {
90+ m_resultsModels[1]->appendScope(scope);
91+ }
92+ }
93+}
94+
7295QVariant
7396ScopesOverviewCategories::data(const QModelIndex& index, int role) const
7497{
@@ -78,7 +101,7 @@ ScopesOverviewCategories::data(const QModelIndex& index, int role) const
78101
79102 const QString categoryId = index.row() == 0 ? "favorites" : "nonfavorites";
80103
81− unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
104+ ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
82105 if (!resultsModel) {
83106 QObject *that = const_cast<ScopesOverviewCategories*>(this);
84107 QList<Scope*> scopes = index.row() == 0 ? m_scopes->favScopes() : m_scopes->nonFavScopes();
@@ -157,7 +180,7 @@ ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
157180
158181 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
159182
160− unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
183+ ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
161184 if (!resultsModel) {
162185 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
163186 QList<Scope *> scopes;
@@ -256,8 +279,7 @@ int ScopesOverviewResultsModel::count() const
256279 return rowCount();
257280}
258281
259−QVariant
260−ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
282+QVariant ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
261283{
262284 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
263285 switch (role) {
@@ -281,3 +303,21 @@ ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
281303 return QVariant();
282304 }
283305}
306+
307+void ScopesOverviewResultsModel::appendScope(Scope *scope)
308+{
309+ Q_ASSERT(!m_scopes.contains(scope));
310+ const int index = rowCount();
311+ beginInsertRows(QModelIndex(), index, index);
312+ m_scopes << scope;
313+ endInsertRows();
314+}
315+
316+void ScopesOverviewResultsModel::removeScope(Scope *scope)
317+{
318+ const int index = m_scopes.indexOf(scope);
319+ Q_ASSERT(index != -1);
320+ beginRemoveRows(QModelIndex(), index, index);
321+ m_scopes.removeAt(index);
322+ endRemoveRows();
323+}
tests/mocks/Unity/fake_scopesoverview.h+15−3
@@ -21,6 +21,8 @@
2121#include <unity/shell/scopes/ResultsModelInterface.h>
2222
2323class Scopes;
24+class ScopesOverviewCategories;
25+class ScopesOverviewResultsModel;
2426
2527class ScopesOverview : public Scope
2628{
@@ -32,8 +34,11 @@ public:
3234 void setSearchQuery(const QString& search_query) override;
3335 Q_INVOKABLE void activate(QVariant const& result) override;
3436
37+ // This is implementation detail
38+ void setFavorite(Scope *scope, bool favorite);
39+
3540private:
36− unity::shell::scopes::CategoriesInterface *m_scopesOverviewCategories;
41+ ScopesOverviewCategories *m_scopesOverviewCategories;
3742 unity::shell::scopes::CategoriesInterface *m_searchCategories;
3843};
3944
@@ -50,8 +55,11 @@ public:
5055 Q_INVOKABLE void addSpecialCategory(QString const& categoryId, QString const& name, QString const& icon, QString const& rawTemplate, QObject* countObject) override;
5156 Q_INVOKABLE bool overrideCategoryJson(QString const& categoryId, QString const& json) override;
5257
58+ // This is implementation detail
59+ void setFavorite(Scope *scope, bool favorite);
60+
5361private:
54− mutable QHash<int, unity::shell::scopes::ResultsModelInterface*> m_resultsModels;
62+ mutable QHash<int, ScopesOverviewResultsModel*> m_resultsModels;
5563
5664 Scopes *m_scopes;
5765};
@@ -70,7 +78,7 @@ public:
7078 Q_INVOKABLE bool overrideCategoryJson(QString const& categoryId, QString const& json) override;
7179
7280private:
73− mutable QHash<int, unity::shell::scopes::ResultsModelInterface*> m_resultsModels;
81+ mutable QHash<int, ScopesOverviewResultsModel*> m_resultsModels;
7482
7583 Scopes *m_scopes;
7684};
@@ -96,6 +104,10 @@ public:
96104 Q_INVOKABLE int scopeIndex(QString const& id) const;
97105 QHash<int, QByteArray> roleNames() const override;
98106
107+ // This is implementation detail
108+ void appendScope(Scope *scope);
109+ void removeScope(Scope *scope);
110+
99111private:
100112 QList<Scope *> m_scopes;
101113 QString m_categoryId;