on xenial help sign in
implement setFavorite in fake_scopes
fd86d64Albert Astals
tests/mocks/Unity/fake_scopes.cpp+33
@@ -165,6 +165,39 @@ unity::shell::scopes::ScopeInterface* Scopes::overviewScope() const
165 return m_scopesOverview;
165 return m_scopesOverview;
166}
166}
167
167
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+
168QList<Scope*> Scopes::favScopes() const
201QList<Scope*> Scopes::favScopes() const
169{
202{
170 return m_scopes;
203 return m_scopes;
tests/mocks/Unity/fake_scopes.h+6−1
@@ -28,6 +28,8 @@
28#include <QList>
28#include <QList>
29#include <QTimer>
29#include <QTimer>
30
30
31+class ScopesOverview;
32+
31class Scopes : public unity::shell::scopes::ScopesInterface
33class Scopes : public unity::shell::scopes::ScopesInterface
32{
34{
33 Q_OBJECT
35 Q_OBJECT
@@ -53,6 +55,9 @@ public:
53 int count() const override;
55 int count() const override;
54 unity::shell::scopes::ScopeInterface* overviewScope() const override;
56 unity::shell::scopes::ScopeInterface* overviewScope() const override;
55
57
58+ // TODO This will go to the api, mark as override
59+ Q_INVOKABLE void setFavorite(const QString& scopeId, bool favorite);
60+
56 // This is used as part of implementation of the other C++ code, not API
61 // This is used as part of implementation of the other C++ code, not API
57 QList<Scope*> favScopes() const;
62 QList<Scope*> favScopes() const;
58 QList<Scope*> nonFavScopes() const;
63 QList<Scope*> nonFavScopes() const;
@@ -64,7 +69,7 @@ private Q_SLOTS:
64private:
69private:
65 QList<Scope*> m_scopes; // the favorite ones
70 QList<Scope*> m_scopes; // the favorite ones
66 QList<Scope*> m_allScopes;
71 QList<Scope*> m_allScopes;
67− Scope *m_scopesOverview;
72+ ScopesOverview *m_scopesOverview;
68 bool m_loaded;
73 bool m_loaded;
69 QTimer timer;
74 QTimer timer;
70};
75};
tests/mocks/Unity/fake_scopesoverview.cpp+44−4
@@ -48,6 +48,11 @@ Q_INVOKABLE void ScopesOverview::activate(QVariant const& result)
48 Q_EMIT openScope(m_openScope);
48 Q_EMIT openScope(m_openScope);
49}
49}
50
50
51+void ScopesOverview::setFavorite(Scope *scope, bool favorite)
52+{
53+ m_scopesOverviewCategories->setFavorite(scope, favorite);
54+}
55+
51ScopesOverviewCategories::ScopesOverviewCategories(Scopes *scopes, QObject* parent)
56ScopesOverviewCategories::ScopesOverviewCategories(Scopes *scopes, QObject* parent)
52 : unity::shell::scopes::CategoriesInterface(parent)
57 : unity::shell::scopes::CategoriesInterface(parent)
53 , m_scopes(scopes)
58 , m_scopes(scopes)
@@ -69,6 +74,24 @@ bool ScopesOverviewCategories::overrideCategoryJson(QString const& /* categoryId
69 qFatal("Using un-implemented ScopesOverviewCategories::overrideCategoryJson");
74 qFatal("Using un-implemented ScopesOverviewCategories::overrideCategoryJson");
70}
75}
71
76
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+
72QVariant
95QVariant
73ScopesOverviewCategories::data(const QModelIndex& index, int role) const
96ScopesOverviewCategories::data(const QModelIndex& index, int role) const
74{
97{
@@ -78,7 +101,7 @@ ScopesOverviewCategories::data(const QModelIndex& index, int role) const
78
101
79 const QString categoryId = index.row() == 0 ? "favorites" : "nonfavorites";
102 const QString categoryId = index.row() == 0 ? "favorites" : "nonfavorites";
80
103
81− unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
104+ ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
82 if (!resultsModel) {
105 if (!resultsModel) {
83 QObject *that = const_cast<ScopesOverviewCategories*>(this);
106 QObject *that = const_cast<ScopesOverviewCategories*>(this);
84 QList<Scope*> scopes = index.row() == 0 ? m_scopes->favScopes() : m_scopes->nonFavScopes();
107 QList<Scope*> scopes = index.row() == 0 ? m_scopes->favScopes() : m_scopes->nonFavScopes();
@@ -157,7 +180,7 @@ ScopesOverviewSearchCategories::data(const QModelIndex& index, int role) const
157
180
158 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
181 const QString categoryId = index.row() == 0 ? "searchA" : "searchB";
159
182
160− unity::shell::scopes::ResultsModelInterface *resultsModel = m_resultsModels[index.row()];
183+ ScopesOverviewResultsModel *resultsModel = m_resultsModels[index.row()];
161 if (!resultsModel) {
184 if (!resultsModel) {
162 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
185 QObject *that = const_cast<ScopesOverviewSearchCategories*>(this);
163 QList<Scope *> scopes;
186 QList<Scope *> scopes;
@@ -256,8 +279,7 @@ int ScopesOverviewResultsModel::count() const
256 return rowCount();
279 return rowCount();
257}
280}
258
281
259−QVariant
260−ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
282+QVariant ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
261{
283{
262 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
284 unity::shell::scopes::ScopeInterface *scope = m_scopes[index.row()];
263 switch (role) {
285 switch (role) {
@@ -281,3 +303,21 @@ ScopesOverviewResultsModel::data(const QModelIndex& index, int role) const
281 return QVariant();
303 return QVariant();
282 }
304 }
283}
305}
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 @@
21#include <unity/shell/scopes/ResultsModelInterface.h>
21#include <unity/shell/scopes/ResultsModelInterface.h>
22
22
23class Scopes;
23class Scopes;
24+class ScopesOverviewCategories;
25+class ScopesOverviewResultsModel;
24
26
25class ScopesOverview : public Scope
27class ScopesOverview : public Scope
26{
28{
@@ -32,8 +34,11 @@ public:
32 void setSearchQuery(const QString& search_query) override;
34 void setSearchQuery(const QString& search_query) override;
33 Q_INVOKABLE void activate(QVariant const& result) override;
35 Q_INVOKABLE void activate(QVariant const& result) override;
34
36
37+ // This is implementation detail
38+ void setFavorite(Scope *scope, bool favorite);
39+
35private:
40private:
36− unity::shell::scopes::CategoriesInterface *m_scopesOverviewCategories;
41+ ScopesOverviewCategories *m_scopesOverviewCategories;
37 unity::shell::scopes::CategoriesInterface *m_searchCategories;
42 unity::shell::scopes::CategoriesInterface *m_searchCategories;
38};
43};
39
44
@@ -50,8 +55,11 @@ public:
50 Q_INVOKABLE void addSpecialCategory(QString const& categoryId, QString const& name, QString const& icon, QString const& rawTemplate, QObject* countObject) override;
55 Q_INVOKABLE void addSpecialCategory(QString const& categoryId, QString const& name, QString const& icon, QString const& rawTemplate, QObject* countObject) override;
51 Q_INVOKABLE bool overrideCategoryJson(QString const& categoryId, QString const& json) override;
56 Q_INVOKABLE bool overrideCategoryJson(QString const& categoryId, QString const& json) override;
52
57
58+ // This is implementation detail
59+ void setFavorite(Scope *scope, bool favorite);
60+
53private:
61private:
54− mutable QHash<int, unity::shell::scopes::ResultsModelInterface*> m_resultsModels;
62+ mutable QHash<int, ScopesOverviewResultsModel*> m_resultsModels;
55
63
56 Scopes *m_scopes;
64 Scopes *m_scopes;
57};
65};
@@ -70,7 +78,7 @@ public:
70 Q_INVOKABLE bool overrideCategoryJson(QString const& categoryId, QString const& json) override;
78 Q_INVOKABLE bool overrideCategoryJson(QString const& categoryId, QString const& json) override;
71
79
72private:
80private:
73− mutable QHash<int, unity::shell::scopes::ResultsModelInterface*> m_resultsModels;
81+ mutable QHash<int, ScopesOverviewResultsModel*> m_resultsModels;
74
82
75 Scopes *m_scopes;
83 Scopes *m_scopes;
76};
84};
@@ -96,6 +104,10 @@ public:
96 Q_INVOKABLE int scopeIndex(QString const& id) const;
104 Q_INVOKABLE int scopeIndex(QString const& id) const;
97 QHash<int, QByteArray> roleNames() const override;
105 QHash<int, QByteArray> roleNames() const override;
98
106
107+ // This is implementation detail
108+ void appendScope(Scope *scope);
109+ void removeScope(Scope *scope);
110+
99private:
111private:
100 QList<Scope *> m_scopes;
112 QList<Scope *> m_scopes;
101 QString m_categoryId;
113 QString m_categoryId;