1
#include "qmenumodel.h"
4
QMenuModel::QMenuModel(GMenuModel *other, QObject *parent)
5
: QAbstractListModel(parent),
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";
16
setRoleNames(rolesNames);
20
QMenuModel::~QMenuModel()
25
void QMenuModel::setMenuModel(GMenuModel *other)
27
if (m_menuModel == other) {
34
g_signal_handler_disconnect(m_menuModel, m_signalChangedId);
35
m_signalChangedId = 0;
36
g_object_unref(m_menuModel);
44
qDebug() << "Menu size:" << g_menu_model_get_n_items(m_menuModel);
45
m_signalChangedId = g_signal_connect(m_menuModel,
47
G_CALLBACK(QMenuModel::onItemsChanged),
52
GMenuModel *QMenuModel::menuModel() const
57
/* QAbstractItemModel */
58
int QMenuModel::columnCount(const QModelIndex &) const
63
QVariant QMenuModel::data(const QModelIndex &index, int role) const
66
int rowCountValue = rowCount();
68
if ((rowCountValue > 0) && (index.row() >= 0) && (index.row() < rowCountValue)) {
73
attribute = getStringAttribute(index, G_MENU_ATTRIBUTE_ACTION);
76
attribute = getStringAttribute(index, G_MENU_ATTRIBUTE_LABEL);
79
attribute = getLink(index, G_MENU_LINK_SECTION);
82
attribute = getLink(index, G_MENU_LINK_SUBMENU);
92
QModelIndex QMenuModel::parent(const QModelIndex &index) const
97
int QMenuModel::rowCount(const QModelIndex &) const
100
return g_menu_model_get_n_items(m_menuModel);
105
QVariant QMenuModel::getStringAttribute(const QModelIndex &index,
106
const QString &attribute) const
110
g_menu_model_get_item_attribute(m_menuModel,
112
attribute.toLatin1(),
115
result = QVariant(QString::fromLatin1(value));
121
QVariant QMenuModel::getLink(const QModelIndex &index,
122
const QString &linkName) const
126
link = g_menu_model_get_item_link(m_menuModel,
128
linkName.toLatin1());
131
QMenuModel *other = new QMenuModel(link, const_cast<QMenuModel*>(this));
132
return QVariant::fromValue<QObject*>(other);
138
void QMenuModel::onItemsChanged(GMenuModel *,
144
QMenuModel *self = reinterpret_cast<QMenuModel*>(data);
147
self->beginRemoveRows(QModelIndex(), position, position + removed - 1);
148
self->endRemoveRows();
152
self->beginInsertRows(QModelIndex(), position, position + added - 1);
153
self->endInsertRows();