diff options
author | Renato Araujo Oliveira Filho <renato.filho@canonical.com> | 2013-01-03 18:38:23 -0300 |
---|---|---|
committer | Renato Araujo Oliveira Filho <renato.filho@canonical.com> | 2013-01-03 18:38:23 -0300 |
commit | 2d050cddb8a4aa3ada8f956e071efed9d53898fd (patch) | |
tree | 090bce4be2b8a8b4b56f37d0588c8e505a309c2f /libqmenumodel/src/menunode.cpp | |
parent | c34c5af8a6990f0af95d8dce59991868bfdc62d3 (diff) | |
download | qmenumodel-2d050cddb8a4aa3ada8f956e071efed9d53898fd.tar.gz qmenumodel-2d050cddb8a4aa3ada8f956e071efed9d53898fd.tar.bz2 qmenumodel-2d050cddb8a4aa3ada8f956e071efed9d53898fd.zip |
Implmeneted QMenuModel as tree model.
Diffstat (limited to 'libqmenumodel/src/menunode.cpp')
-rw-r--r-- | libqmenumodel/src/menunode.cpp | 223 |
1 files changed, 223 insertions, 0 deletions
diff --git a/libqmenumodel/src/menunode.cpp b/libqmenumodel/src/menunode.cpp new file mode 100644 index 0000000..bfdd28a --- /dev/null +++ b/libqmenumodel/src/menunode.cpp @@ -0,0 +1,223 @@ +/* + * Copyright 2012 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Renato Araujo Oliveira Filho <renato@canonical.com> + */ + +#include "menunode.h" + +#include <QMetaMethod> +#include <QDebug> + +MenuNode::MenuNode(const QString &linkType, GMenuModel *model, MenuNode *parent, int pos, QObject *listener) + : m_model(model), + m_parent(parent), + m_signalChangedId(0), + m_linkType(linkType) +{ + g_object_ref(model); + if (m_parent) { + m_parent->insertChild(this, pos); + } + + m_size = g_menu_model_get_n_items(model); + for(int i=0; i < m_size; i++) { + MenuNode::create(model, i, this, listener); + } + + connect(listener); +} + +MenuNode::~MenuNode() +{ + disconnect(); + Q_FOREACH(MenuNode *child, m_children) { + delete child; + } + m_children.clear(); + if (m_model) { + g_object_unref(m_model); + } +} + +void MenuNode::connect(QObject *listener) +{ + m_listener = listener; + if (m_model && (m_signalChangedId == 0)) { + m_signalChangedId = g_signal_connect(m_model, + "items-changed", + G_CALLBACK(MenuNode::onItemsChanged), + this); + } +} + +void MenuNode::disconnect() +{ + if (m_signalChangedId != 0) { + g_signal_handler_disconnect(m_model, m_signalChangedId); + } +} + +int MenuNode::position() const +{ + if (m_parent) { + return m_parent->childPosition(this); + } else { + return 0; + } +} + +MenuNode *MenuNode::parent() const +{ + return m_parent; +} + +GMenuModel *MenuNode::model() const +{ + return m_model; +} + +QString MenuNode::linkType() const +{ + return m_linkType; +} + +MenuNode *MenuNode::child(int pos) const +{ + if (m_children.contains(pos)) { + return m_children.value(pos); + } + return 0; +} + +int MenuNode::childPosition(GMenuModel *item) const +{ + QMap<int, MenuNode*>::const_iterator i = m_children.constBegin(); + while (i != m_children.constEnd()) { + if (i.value()->m_model == item) { + return i.key(); + } + ++i; + } + return 0; +} + +int MenuNode::childPosition(const MenuNode *item) const +{ + return childPosition(item->m_model); +} + +int MenuNode::size() const +{ + return m_size; +} + +int MenuNode::depth() const +{ + int depth = 0; + const MenuNode *child = this; + while(child->parent()) { + depth++; + child = child->parent(); + } + return depth; +} + +void MenuNode::change(int start, int added, int removed) +{ + if (added > 0) { + for (int i=(m_size - 1 + added), iMin=start; i >= iMin; i--) { + if (m_children.contains(i)) { + m_children.insert(i + added, m_children.take(i)); + } + } + } + + if (removed > 0) { + int removedEnd = start + removed; + for (int i=start, iMax=m_size; i < iMax; i++) { + if (i <= removedEnd) { + delete m_children.take(i); + } else if (m_children.contains(i)) { + m_children.insert(i - removed, m_children.take(i)); + } + } + } +} + +void MenuNode::insertChild(MenuNode *child, int pos) +{ + if (m_children.contains(pos)) { + qWarning() << "Section conflic: parent" << this << "child" << child << "pos" << pos; + return; + } + + child->m_parent = this; + m_children.insert(pos, child); +} + + +MenuNode *MenuNode::find(GMenuModel *item) +{ + if (m_model == item) { + return this; + } + + Q_FOREACH(MenuNode *child, m_children) { + MenuNode *found = child->find(item); + if (found) { + return found; + } + } + return 0; +} + +MenuNode *MenuNode::create(GMenuModel *model, int pos, MenuNode *parent, QObject *listener) +{ + QString linkType(G_MENU_LINK_SUBMENU); + GMenuModel *link = g_menu_model_get_item_link(model, pos, G_MENU_LINK_SUBMENU); + if (link == NULL) { + linkType = G_MENU_LINK_SECTION; + link = g_menu_model_get_item_link(model, pos, G_MENU_LINK_SECTION); + } + + if (link) { + return new MenuNode(linkType, link, parent, pos, listener); + } + return 0; +} + +void MenuNode::onItemsChanged(GMenuModel *model, gint position, gint removed, gint added, gpointer data) +{ + MenuNode *self = reinterpret_cast<MenuNode*>(data); + if (self->m_listener) { + const QMetaObject *mobj = self->m_listener->metaObject(); + int slotIndex = mobj->indexOfSlot(QMetaObject::normalizedSignature("onItemsChanged(MenuNode*, int, int, int)")); + if (slotIndex > -1) { + QMetaMethod slot = mobj->method(slotIndex); + qDebug() << "invoke" << model << "pos" << position << removed << added; + slot.invoke(self->m_listener, + Q_ARG(MenuNode*, self), + Q_ARG(int, position), + Q_ARG(int, removed), + Q_ARG(int, added)); + } else { + qWarning() << "Slot 'onItemsChanged(MenuNode*, int, int, int)' not found in" << self->m_listener; + } + } else { + qDebug() << "No listener" << position << removed << added; + } +} |