~nick-dedekind/unity8/indicators.hint-interval

« back to all changes in this revision

Viewing changes to tests/mocks/Unity/scopes-ng/fake_preview_model.cpp

  • Committer: Nick Dedekind
  • Date: 2014-03-07 15:54:57 UTC
  • mfrom: (638.1.118 unity8)
  • Revision ID: nicholas.dedekind@gmail.com-20140307155457-f0s1zu5ll2czt3rq
merged with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014 Canonical, Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Michał Sawicz <michal.sawicz@canonical.com>
 
6
 *  Michal Hruby <michal.hruby@canonical.com>
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; version 3.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
// self
 
22
#include "fake_preview_model.h"
 
23
 
 
24
// Qt
 
25
#include <QVariantMap>
 
26
 
 
27
namespace scopes_ng
 
28
{
 
29
 
 
30
struct PreviewData
 
31
{
 
32
    QString id;
 
33
    QString type;
 
34
    QVariantMap data;
 
35
 
 
36
    PreviewData(QString const& id_, QString const& type_, QVariantMap const& data_): id(id_), type(type_), data(data_)
 
37
    {
 
38
    }
 
39
};
 
40
 
 
41
PreviewModel::PreviewModel(QObject* parent) : QAbstractListModel(parent)
 
42
{
 
43
    m_roles[Roles::RoleWidgetId] = "widgetId";
 
44
    m_roles[Roles::RoleType] = "type";
 
45
    m_roles[Roles::RoleProperties] = "properties";
 
46
 
 
47
    populateWidgets();
 
48
}
 
49
 
 
50
void PreviewModel::populateWidgets()
 
51
{
 
52
    beginResetModel();
 
53
    m_previewWidgets.clear();
 
54
    for (int i = 0; i <= 20; i++) {
 
55
        // FIXME: the API will expose nicer getters soon, use those!
 
56
        QVariantMap attributes;
 
57
        attributes["text"] = QVariant::fromValue(QString("Widget %1").arg(i));
 
58
        attributes["title"] = QVariant::fromValue(QString("Title %1").arg(i));
 
59
        PreviewData* preview_data = new PreviewData(QString("widget-%1").arg(i), QString("text"), attributes);
 
60
        m_previewWidgets.append(QSharedPointer<PreviewData>(preview_data));
 
61
    }
 
62
    endResetModel();
 
63
 
 
64
}
 
65
 
 
66
void PreviewModel::triggered(QString widgetId, QString actionId, QVariantMap data)
 
67
{
 
68
    Q_EMIT actionTriggered(widgetId, actionId, data);
 
69
}
 
70
 
 
71
QHash<int, QByteArray> PreviewModel::roleNames() const
 
72
{
 
73
    return m_roles;
 
74
}
 
75
 
 
76
int PreviewModel::rowCount(const QModelIndex&) const
 
77
{
 
78
    return m_previewWidgets.size();
 
79
}
 
80
 
 
81
QVariant PreviewModel::data(const QModelIndex& index, int role) const
 
82
{
 
83
    auto widget_data = m_previewWidgets.at(index.row());
 
84
    switch (role) {
 
85
        case RoleWidgetId:
 
86
            return widget_data->id;
 
87
        case RoleType:
 
88
            return widget_data->type;
 
89
        case RoleProperties:
 
90
            return widget_data->data;
 
91
        default:
 
92
            return QVariant();
 
93
    }
 
94
}
 
95
 
 
96
} // namespace scopes_ng