~indicator-applet-developers/qmenumodel/trunk

« back to all changes in this revision

Viewing changes to src/QMenuModel/qmenumodel.cpp

  • Committer: Renato Araujo Oliveira Filho
  • Date: 2012-09-11 20:57:08 UTC
  • Revision ID: renato.filho@canonical.com-20120911205708-1r60av48rdde4f10
Splitted qmenumodel plugin in two libraries to make it testable.
Create unit test for qmenumodel library.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "qmenumodel.h"
2
 
#include <QDebug>
3
 
 
4
 
QMenuModel::QMenuModel(GMenuModel *other, QObject *parent)
5
 
    : QAbstractListModel(parent),
6
 
      m_menuModel(0),
7
 
      m_signalChangedId(0)
8
 
{
9
 
    static QHash<int, QByteArray> rolesNames;
10
 
    if (rolesNames.empty()) {
11
 
        rolesNames[Action] = "action";
12
 
        rolesNames[Label] = "label";
13
 
        rolesNames[LinkSection] = "linkSection";
14
 
        rolesNames[LinkSubMenu] = "linkSubMenu";
15
 
    }
16
 
    setRoleNames(rolesNames);
17
 
    setMenuModel(other);
18
 
}
19
 
 
20
 
QMenuModel::~QMenuModel()
21
 
{
22
 
   setMenuModel(NULL);
23
 
}
24
 
 
25
 
void QMenuModel::setMenuModel(GMenuModel *other)
26
 
{
27
 
    if (m_menuModel == other) {
28
 
        return;
29
 
    }
30
 
 
31
 
    beginResetModel();
32
 
 
33
 
    if (m_menuModel) {
34
 
        g_signal_handler_disconnect(m_menuModel, m_signalChangedId);
35
 
        m_signalChangedId = 0;
36
 
        g_object_unref(m_menuModel);
37
 
    }
38
 
 
39
 
    m_menuModel = other;
40
 
 
41
 
    endResetModel();
42
 
 
43
 
    if (m_menuModel) {
44
 
        qDebug() << "Menu size:" << g_menu_model_get_n_items(m_menuModel);
45
 
        m_signalChangedId = g_signal_connect(m_menuModel,
46
 
                                             "items-changed",
47
 
                                             G_CALLBACK(QMenuModel::onItemsChanged),
48
 
                                             this);
49
 
    }
50
 
}
51
 
 
52
 
GMenuModel *QMenuModel::menuModel() const
53
 
{
54
 
    return m_menuModel;
55
 
}
56
 
 
57
 
/* QAbstractItemModel */
58
 
int QMenuModel::columnCount(const QModelIndex &) const
59
 
{
60
 
    return 1;
61
 
}
62
 
 
63
 
QVariant QMenuModel::data(const QModelIndex &index, int role) const
64
 
{
65
 
    QVariant attribute;
66
 
    int rowCountValue = rowCount();
67
 
 
68
 
    if ((rowCountValue > 0) && (index.row() >= 0) && (index.row() < rowCountValue)) {
69
 
        if (m_menuModel) {
70
 
            switch (role)
71
 
            {
72
 
            case Action:
73
 
                attribute = getStringAttribute(index, G_MENU_ATTRIBUTE_ACTION);
74
 
                break;
75
 
            case Label:
76
 
                attribute = getStringAttribute(index, G_MENU_ATTRIBUTE_LABEL);
77
 
                break;
78
 
            case LinkSection:
79
 
                attribute = getLink(index, G_MENU_LINK_SECTION);
80
 
                break;
81
 
            case LinkSubMenu:
82
 
                attribute = getLink(index, G_MENU_LINK_SUBMENU);
83
 
                break;
84
 
            default:
85
 
                break;
86
 
            }
87
 
        }
88
 
    }
89
 
    return attribute;
90
 
}
91
 
 
92
 
QModelIndex QMenuModel::parent(const QModelIndex &index) const
93
 
{
94
 
    return QModelIndex();
95
 
}
96
 
 
97
 
int QMenuModel::rowCount(const QModelIndex &) const
98
 
{
99
 
    if (m_menuModel) {
100
 
        return g_menu_model_get_n_items(m_menuModel);
101
 
    }
102
 
    return 0;
103
 
}
104
 
 
105
 
QVariant QMenuModel::getStringAttribute(const QModelIndex &index,
106
 
                                        const QString &attribute) const
107
 
{
108
 
    QVariant result;
109
 
    gchar* value = NULL;
110
 
    g_menu_model_get_item_attribute(m_menuModel,
111
 
                                    index.row(),
112
 
                                    attribute.toLatin1(),
113
 
                                    "s", &value);
114
 
    if (value) {
115
 
        result = QVariant(QString::fromLatin1(value));
116
 
        g_free(value);
117
 
    }
118
 
    return result;
119
 
}
120
 
 
121
 
QVariant QMenuModel::getLink(const QModelIndex &index,
122
 
                             const QString &linkName) const
123
 
{
124
 
    GMenuModel *link;
125
 
 
126
 
    link = g_menu_model_get_item_link(m_menuModel,
127
 
                                      index.row(),
128
 
                                      linkName.toLatin1());
129
 
 
130
 
    if (link) {      
131
 
        QMenuModel *other = new QMenuModel(link, const_cast<QMenuModel*>(this));
132
 
        return QVariant::fromValue<QObject*>(other);
133
 
    }
134
 
 
135
 
    return QVariant();
136
 
}
137
 
 
138
 
void QMenuModel::onItemsChanged(GMenuModel *,
139
 
                                gint position,
140
 
                                gint removed,
141
 
                                gint added,
142
 
                                gpointer data)
143
 
{
144
 
    QMenuModel *self = reinterpret_cast<QMenuModel*>(data);
145
 
 
146
 
    if (removed > 0) {
147
 
        self->beginRemoveRows(QModelIndex(), position, position + removed - 1);
148
 
        self->endRemoveRows();
149
 
    }
150
 
 
151
 
    if (added > 0) {
152
 
        self->beginInsertRows(QModelIndex(), position, position + added - 1);
153
 
        self->endInsertRows();
154
 
    }
155
 
}
156