aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRenato Araujo Oliveira Filho <renato.filho@canonical.com>2013-01-10 15:54:35 -0300
committerRenato Araujo Oliveira Filho <renato.filho@canonical.com>2013-01-10 15:54:35 -0300
commit33147e96ae17a01a3f025a5b7b78bff7943b4851 (patch)
tree5b9f5421585d19516f0b8f964eb878e0695f5d43
parent2d050cddb8a4aa3ada8f956e071efed9d53898fd (diff)
downloadqmenumodel-33147e96ae17a01a3f025a5b7b78bff7943b4851.tar.gz
qmenumodel-33147e96ae17a01a3f025a5b7b78bff7943b4851.tar.bz2
qmenumodel-33147e96ae17a01a3f025a5b7b78bff7943b4851.zip
Fixed item removal and insertion.
Removed debug messages. Fixed tests to work with new tree model structure.
-rw-r--r--libqmenumodel/src/menunode.cpp47
-rw-r--r--libqmenumodel/src/menunode.h6
-rw-r--r--libqmenumodel/src/qmenumodel.cpp25
-rw-r--r--tests/client/CMakeLists.txt4
-rw-r--r--tests/client/modeltest.cpp52
-rw-r--r--tests/client/treetest.cpp6
-rw-r--r--tests/client/treeviewtest.cpp88
7 files changed, 63 insertions, 165 deletions
diff --git a/libqmenumodel/src/menunode.cpp b/libqmenumodel/src/menunode.cpp
index bfdd28a..3d8c5fd 100644
--- a/libqmenumodel/src/menunode.cpp
+++ b/libqmenumodel/src/menunode.cpp
@@ -26,9 +26,13 @@ MenuNode::MenuNode(const QString &linkType, GMenuModel *model, MenuNode *parent,
: m_model(model),
m_parent(parent),
m_signalChangedId(0),
- m_linkType(linkType)
+ m_linkType(linkType),
+ m_currentOpPosition(-1),
+ m_currentOpAdded(0),
+ m_currentOpRemoved(0)
{
g_object_ref(model);
+
if (m_parent) {
m_parent->insertChild(this, pos);
}
@@ -114,7 +118,6 @@ int MenuNode::childPosition(GMenuModel *item) const
}
return 0;
}
-
int MenuNode::childPosition(const MenuNode *item) const
{
return childPosition(item->m_model);
@@ -136,6 +139,23 @@ int MenuNode::depth() const
return depth;
}
+int MenuNode::realPosition(int row) const
+{
+ int result = row;
+ if ((row >= 0) && (row < m_size)) {
+ if (row >= m_currentOpPosition) {
+ if ((m_currentOpRemoved > 0) && (row < (m_currentOpPosition + m_currentOpRemoved))) {
+ result = -1;
+ } else {
+ result += (m_currentOpAdded - m_currentOpRemoved);
+ }
+ }
+ return result;
+ } else {
+ return -1;
+ }
+}
+
void MenuNode::change(int start, int added, int removed)
{
if (added > 0) {
@@ -144,6 +164,12 @@ void MenuNode::change(int start, int added, int removed)
m_children.insert(i + added, m_children.take(i));
}
}
+
+ m_size += added;
+
+ for (int i = start; i < (start + added); i++) {
+ MenuNode::create(m_model, i, this, m_listener);
+ }
}
if (removed > 0) {
@@ -155,6 +181,7 @@ void MenuNode::change(int start, int added, int removed)
m_children.insert(i - removed, m_children.take(i));
}
}
+ m_size -= removed;
}
}
@@ -200,15 +227,26 @@ MenuNode *MenuNode::create(GMenuModel *model, int pos, MenuNode *parent, QObject
return 0;
}
+void MenuNode::commitOperation()
+{
+ change(m_currentOpPosition, m_currentOpAdded, m_currentOpRemoved);
+
+ m_currentOpPosition = -1;
+ m_currentOpAdded = m_currentOpRemoved = 0;
+}
+
void MenuNode::onItemsChanged(GMenuModel *model, gint position, gint removed, gint added, gpointer data)
{
MenuNode *self = reinterpret_cast<MenuNode*>(data);
+ self->m_currentOpPosition = position;
+ self->m_currentOpAdded = added;
+ self->m_currentOpRemoved = removed;
+
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),
@@ -217,7 +255,6 @@ void MenuNode::onItemsChanged(GMenuModel *model, gint position, gint removed, gi
} else {
qWarning() << "Slot 'onItemsChanged(MenuNode*, int, int, int)' not found in" << self->m_listener;
}
- } else {
- qDebug() << "No listener" << position << removed << added;
}
+ self->commitOperation();
}
diff --git a/libqmenumodel/src/menunode.h b/libqmenumodel/src/menunode.h
index d7a893c..21fc5bc 100644
--- a/libqmenumodel/src/menunode.h
+++ b/libqmenumodel/src/menunode.h
@@ -53,6 +53,9 @@ public:
void change(int start, int added, int removed);
MenuNode *find(GMenuModel *item);
+ int realPosition(int row) const;
+ void commitOperation();
+
static MenuNode *create(GMenuModel *model, int pos, MenuNode *parent=0, QObject *listener=0);
private:
@@ -63,6 +66,9 @@ private:
QObject *m_listener;
gulong m_signalChangedId;
QString m_linkType;
+ int m_currentOpPosition;
+ int m_currentOpAdded;
+ int m_currentOpRemoved;
static void onItemsChanged(GMenuModel *model, gint position, gint removed, gint added, gpointer data);
};
diff --git a/libqmenumodel/src/qmenumodel.cpp b/libqmenumodel/src/qmenumodel.cpp
index 4f0769c..5faeb43 100644
--- a/libqmenumodel/src/qmenumodel.cpp
+++ b/libqmenumodel/src/qmenumodel.cpp
@@ -25,7 +25,6 @@ extern "C" {
#include "qmenumodel.h"
#include "menunode.h"
#include "converter.h"
-#include <QDebug>
#include <QCoreApplication>
#include <QThread>
@@ -54,7 +53,6 @@ QMenuModel::~QMenuModel()
/*! \internal */
void QMenuModel::setMenuModel(GMenuModel *other)
{
- //qDebug() << "SetModel" << m_root << other;
if ((m_root != 0) && (m_root->model() == other)) {
return;
}
@@ -65,7 +63,6 @@ void QMenuModel::setMenuModel(GMenuModel *other)
if (other) {
m_root = new MenuNode("", other, 0, 0, this);
- //qDebug() << "new size:" << m_root << m_root->size();
}
endResetModel();
@@ -99,7 +96,6 @@ QHash<int, QByteArray> QMenuModel::roleNames() const
QModelIndex QMenuModel::index(int row, int column, const QModelIndex &parent) const
{
MenuNode *node = nodeFromIndex(parent);
- //qDebug() << "get index: root" << m_root << "node:" << node << "row" << row << "parent is valid:" << parent.isValid() << "parent row:" << parent.row();
if (node == 0) {
return QModelIndex();
}
@@ -135,10 +131,9 @@ QVariant QMenuModel::data(const QModelIndex &index, int role) const
}
MenuNode *node = nodeFromIndex(index);
- int rowCountValue = node->size();
- int row = index.row();
+ int row = node ? node->realPosition(index.row()) : -1;
- if (node && (row >= 0) && (row < rowCountValue)) {
+ if (row >= 0) {
switch (role) {
case Action:
attribute = getStringAttribute(node, row, G_MENU_ATTRIBUTE_ACTION);
@@ -179,7 +174,10 @@ int QMenuModel::rowCount(const QModelIndex &index) const
}
return 0;
}
- return m_root->size();
+ if (m_root) {
+ return m_root->size();
+ }
+ return 0;
}
/*! \internal */
@@ -233,13 +231,11 @@ void QMenuModel::onItemsChanged(MenuNode *node,
int removed,
int added)
{
- qDebug() << "+[onItemsChanged]" << this << "pos" << position << "removed" << removed << "added" << added << "thread" << QThread::currentThread();
-
QModelIndex index = indexFromNode(node);
if (removed > 0) {
beginRemoveRows(index, position, position + removed - 1);
- node->change(position, added, removed);
+ node->commitOperation();
endRemoveRows();
}
@@ -247,15 +243,10 @@ void QMenuModel::onItemsChanged(MenuNode *node,
if (added > 0) {
beginInsertRows(index, position, position + added - 1);
- node->change(position, added, removed);
- for (int i = position; i < (position + added); i++) {
- MenuNode::create(node->model(), i, node, this);
- }
+ node->commitOperation();
endInsertRows();
}
-
- //qDebug() << "-[onItemsChanged]" << self << "pos" << position << "removed" << removed << "added" << added << "thread" << QThread::currentThread();
}
/*! \internal */
diff --git a/tests/client/CMakeLists.txt b/tests/client/CMakeLists.txt
index 62f7ccb..74439ed 100644
--- a/tests/client/CMakeLists.txt
+++ b/tests/client/CMakeLists.txt
@@ -54,14 +54,12 @@ endif()
declare_test(servicetest)
declare_test(menuchangestest)
-#declare_test(modeltest)
+declare_test(modeltest)
declare_test(actiongrouptest)
declare_test(qmltest)
declare_simple_test(convertertest)
-#declare_simple_test(cachetest)
declare_simple_test(modelsignalstest)
declare_simple_test(treetest)
-declare_simple_test(treeviewtest)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/qmlfiles.h.in
${CMAKE_CURRENT_BINARY_DIR}/qmlfiles.h)
diff --git a/tests/client/modeltest.cpp b/tests/client/modeltest.cpp
index 017f859..7bc9706 100644
--- a/tests/client/modeltest.cpp
+++ b/tests/client/modeltest.cpp
@@ -117,22 +117,9 @@ private Q_SLOTS:
QCOMPARE(action.type(), QVariant::String);
QCOMPARE(action.toString(), QString("Menu1Act"));
- // Section (QObject)
- QVariant vSection = m_model.data(m_model.index(2, 0), QMenuModel::LinkSection);
- QVERIFY(vSection.isValid());
- QMenuModel *section = qobject_cast<QMenuModel*>(vSection.value<QObject*>());
- QVERIFY(section);
- QCOMPARE(section->rowCount(), 2);
-
- // SubMenu (QObject)
- QVariant vSubMenu = m_model.data(m_model.index(3, 0), QMenuModel::LinkSubMenu);
- QVERIFY(vSubMenu.isValid());
- QMenuModel *submenu = qobject_cast<QMenuModel*>(vSubMenu.value<QObject*>());
- QVERIFY(submenu);
-
// Wait for menu load (submenus are loaded async)
QTest::qWait(500);
- QCOMPARE(submenu->rowCount(), 2);
+ QCOMPARE(m_model.rowCount(m_model.index(2, 0)), 2);
}
/*
@@ -251,43 +238,6 @@ private Q_SLOTS:
delete model;
}
-
- /*
- * Test get function
- */
- void testGetData()
- {
- // Make menu available
- m_script.publishMenu();
- m_script.run();
-
- // create a new model
- QDBusMenuModel *model = new QDBusMenuModel();
- model->setBusType(DBusEnums::SessionBus);
- model->setBusName(MENU_SERVICE_NAME);
- model->setObjectPath(MENU_OBJECT_PATH);
- model->start();
-
- // Wait for dbus sync
- QTest::qWait(500);
-
- // count
- QCOMPARE(model->property("count").toInt(), model->rowCount());
-
- QVariantMap data = model->get(0);
-
- QVERIFY(data.contains("action"));
- QVERIFY(data.contains("extra"));
- QVERIFY(data.contains("label"));
- QVERIFY(data.contains("linkSection"));
- QVERIFY(data.contains("linkSubMenu"));
-
- QCOMPARE(data["action"].toString(), QString("Menu0Act"));
-
- QVariantMap extra = data["extra"].toMap();
- QCOMPARE(extra.size(), 13);
- QCOMPARE(extra["boolean"].toBool(), true);
- }
};
QTEST_MAIN(ModelTest)
diff --git a/tests/client/treetest.cpp b/tests/client/treetest.cpp
index 3fce236..b6fec3c 100644
--- a/tests/client/treetest.cpp
+++ b/tests/client/treetest.cpp
@@ -83,29 +83,33 @@ private Q_SLOTS:
QModelIndex row4 = row3.child(0, 0);
QVERIFY(row4.isValid());
QCOMPARE(menu.rowCount(row4), 0);
+ QCOMPARE(menu.data(row4, QMenuModel::Depth).toInt(), 1);
QCOMPARE(menu.data(row4, QMenuModel::Label).toString(), QString("menu4"));
QModelIndex row5 = row3.child(1, 0);
QVERIFY(row5.isValid());
QCOMPARE(menu.rowCount(row5), 2);
+ QCOMPARE(menu.data(row5, QMenuModel::Depth).toInt(), 1);
QCOMPARE(menu.data(row5, QMenuModel::Label).toString(), QString("menu5"));
QModelIndex row6 = row5.child(0, 0);
QVERIFY(row6.isValid());
QCOMPARE(menu.rowCount(row6), 0);
+ QCOMPARE(menu.data(row6, QMenuModel::Depth).toInt(), 2);
QCOMPARE(menu.data(row6, QMenuModel::Label).toString(), QString("menu6"));
QModelIndex row7 = row5.child(1, 0);
QVERIFY(row7.isValid());
QCOMPARE(menu.rowCount(row7), 0);
+ QCOMPARE(menu.data(row7, QMenuModel::Depth).toInt(), 2);
QCOMPARE(menu.data(row7, QMenuModel::Label).toString(), QString("menu7"));
QModelIndex parent_6 = menu.parent(row6);
QVERIFY(parent_6.isValid());
QCOMPARE(menu.rowCount(parent_6), 2);
+ QCOMPARE(menu.data(parent_6, QMenuModel::Depth).toInt(), 1);
QCOMPARE(menu.data(parent_6, QMenuModel::Label).toString(), QString("menu5"));
}
-
};
QTEST_MAIN(TreeTest)
diff --git a/tests/client/treeviewtest.cpp b/tests/client/treeviewtest.cpp
deleted file mode 100644
index 527ebd3..0000000
--- a/tests/client/treeviewtest.cpp
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * 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>
- * Olivier Tilloy <olivier.tilloy@canonical.com>
- */
-
-#include "qmenumodel.h"
-
-extern "C" {
-#include <gio/gio.h>
-}
-
-#include <QtTest>
-#include <QTreeView>
-#include <QFileSystemModel>
-
-
-class TestModel : public QMenuModel
-{
- Q_OBJECT
-public:
- TestModel() : QMenuModel(0)
- {
- GMenu *menu5 = g_menu_new();
- g_menu_append(menu5, "menu6", NULL);
- g_menu_append(menu5, "menu7", NULL);
-
- GMenu *menu3 = g_menu_new();
- g_menu_append(menu3, "menu4", NULL);
- g_menu_append_section(menu3, "menu5", G_MENU_MODEL(menu5));
- g_menu_append(menu3, "menu8", NULL);
-
- GMenu *menu = g_menu_new();
- g_menu_append(menu, "menu0", NULL);
- g_menu_append(menu, "menu1", NULL);
- g_menu_append(menu, "menu2", NULL);
- g_menu_append_section(menu, "menu3", G_MENU_MODEL(menu3));
-
- setMenuModel(G_MENU_MODEL(menu));
-
- m_menus << menu << menu3 << menu5;
- }
-
-private:
- QList<GMenu*> m_menus;
-};
-
-class TreeViewTest : public QObject
-{
- Q_OBJECT
-
-private Q_SLOTS:
- void initTestCase()
- {
- g_type_init();
- }
-
- void testView()
- {
- TestModel menu;
- //QFileSystemModel menu; // = new QFileSystemModel;
- //menu.setRootPath(QDir::currentPath());
- QTreeView *tree = new QTreeView;
- tree->setModel(&menu);
- tree->show();
- QApplication::exec();
- }
-
-};
-
-QTEST_MAIN(TreeViewTest)
-
-#include "treeviewtest.moc"
-