~gerboland/unity/8-refactor-wm-and-test

« back to all changes in this revision

Viewing changes to tests/qmltests/plugins/Unity/fake_lens.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 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
 */
 
16
 
 
17
// Self
 
18
#include "fake_lens.h"
 
19
 
 
20
#include <dee.h>
 
21
#include "paths.h"
 
22
 
 
23
static DeeModel* create_categories_model(unsigned category_count);
 
24
static DeeModel* create_results_model(unsigned category_count, unsigned result_count);
 
25
 
 
26
// TODO: Implement remaining pieces
 
27
 
 
28
Lens::Lens(QObject* parent)
 
29
: QObject(parent),
 
30
  m_visible(false),
 
31
  m_categories(new Categories(this)),
 
32
  m_results(new DeeListModel(this))
 
33
{
 
34
    m_categories->setModel(create_categories_model(4));
 
35
    m_results->setModel(create_results_model(4, 30));
 
36
 
 
37
    m_categories->setResultModel(m_results);
 
38
    m_categories->setGlobalResultModel(m_results);
 
39
}
 
40
 
 
41
Lens::Lens(QString const& id,
 
42
           QString const& name,
 
43
           bool visible,
 
44
           QObject* parent)
 
45
: QObject(parent),
 
46
  m_id(id),
 
47
  m_name(name),
 
48
  m_visible(visible),
 
49
  m_categories(new Categories(this)),
 
50
  m_results(new DeeListModel(this))
 
51
{
 
52
    m_categories->setModel(create_categories_model(4));
 
53
    m_results->setModel(create_results_model(4, 30));
 
54
 
 
55
    m_categories->setResultModel(m_results);
 
56
    m_categories->setGlobalResultModel(m_results);
 
57
}
 
58
 
 
59
QString Lens::id() const {
 
60
    return m_id;
 
61
}
 
62
 
 
63
QString Lens::name() const {
 
64
    return m_name;
 
65
}
 
66
 
 
67
QString Lens::searchQuery() const {
 
68
    return m_searchQuery;
 
69
}
 
70
 
 
71
void Lens::setName(const QString &str) {
 
72
    if (str != m_name) {
 
73
        m_name = str;
 
74
        Q_EMIT nameChanged(m_name);
 
75
    }
 
76
}
 
77
 
 
78
void Lens::setSearchQuery(const QString &str) {
 
79
    if (str != m_searchQuery) {
 
80
        m_searchQuery = str;
 
81
        Q_EMIT searchQueryChanged(m_searchQuery);
 
82
    }
 
83
}
 
84
 
 
85
bool Lens::visible() const {
 
86
    return m_visible;
 
87
}
 
88
 
 
89
Categories* Lens::categories() const {
 
90
  return m_categories;
 
91
}
 
92
 
 
93
static const gchar * categories_model_schema[] = {
 
94
  "s", // DISPLAY_NAME
 
95
  "s", // ICON_HINT
 
96
  "s", // RENDERER_NAME
 
97
  "a{sv}" // HINTS
 
98
};
 
99
 
 
100
 
 
101
DeeModel* create_categories_model(unsigned category_count) {
 
102
    DeeModel* category_model = dee_sequence_model_new();
 
103
    dee_model_set_schema_full(category_model, categories_model_schema, G_N_ELEMENTS(categories_model_schema));
 
104
 
 
105
    GVariantBuilder b;
 
106
    g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
 
107
    GVariant *hints = g_variant_builder_end(&b);
 
108
 
 
109
    for(unsigned i = 0; i < category_count; ++i)
 
110
    {
 
111
      dee_model_append(category_model,
 
112
                       ("Category "+std::to_string(i)).c_str(),
 
113
                       "gtk-apply",
 
114
                       "grid",
 
115
                       hints);
 
116
    }
 
117
    g_variant_unref(hints);
 
118
    return category_model;
 
119
}
 
120
 
 
121
 
 
122
/* Schema that is used in the DeeModel representing
 
123
   the results */
 
124
static const gchar * results_model_schema[] = {
 
125
  "s", // URI
 
126
  "s", // ICON_HINT
 
127
  "u", // CATEGORY
 
128
  "s", // MIMETYPE
 
129
  "s", // TITLE
 
130
  "s", // COMMENT
 
131
  "s", // DND_URI
 
132
  "a{sv}" // METADATA
 
133
};
 
134
 
 
135
static const gchar * icons[] = {
 
136
  "Applications.png",
 
137
  "Home.png",
 
138
  "Music.png",
 
139
  "People.png",
 
140
  "Videos.png",
 
141
};
 
142
 
 
143
DeeModel* create_results_model(unsigned category_count, unsigned result_count) {
 
144
    DeeModel* results_model = dee_sequence_model_new();
 
145
    dee_model_set_schema_full(results_model, results_model_schema, G_N_ELEMENTS(results_model_schema));
 
146
 
 
147
    GVariantBuilder b;
 
148
    g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
 
149
    GVariant *hints = g_variant_builder_end(&b);
 
150
 
 
151
    for(unsigned i = 0; i < result_count; ++i)
 
152
    {
 
153
      unsigned category = i % category_count;
 
154
 
 
155
      dee_model_append(results_model,
 
156
                       ("uri://result."+std::to_string(i)).c_str(),
 
157
                       (shellAppDirectory() + "Dash/graphics/lensIcons/" + (icons[i%G_N_ELEMENTS(icons)])).toLatin1().data(),
 
158
                       category,
 
159
                       "application/x-desktop",
 
160
                       ("Title."+std::to_string(i)).c_str(),
 
161
                       ("Comment."+std::to_string(i)).c_str(),
 
162
                       ("uri://result."+std::to_string(i)).c_str(),
 
163
                       hints);
 
164
    }
 
165
    g_variant_unref(hints);
 
166
    return results_model;
 
167
  }