aboutsummaryrefslogtreecommitdiff
path: root/libqmenumodel/src/qmenumodel.cpp
diff options
context:
space:
mode:
authorRenato Araujo Oliveira Filho <renato.filho@canonical.com>2012-12-14 12:03:52 -0300
committerRenato Araujo Oliveira Filho <renato.filho@canonical.com>2012-12-14 12:03:52 -0300
commit9b94a0f9c2cad99fef925a7b33f852aa5e38913a (patch)
tree872ceaef6e79c2d1d1e356f44875e4696ea38d79 /libqmenumodel/src/qmenumodel.cpp
parentc7cd7a43482319d81a2eade26d911b1e05a17441 (diff)
downloadqmenumodel-9b94a0f9c2cad99fef925a7b33f852aa5e38913a.tar.gz
qmenumodel-9b94a0f9c2cad99fef925a7b33f852aa5e38913a.tar.bz2
qmenumodel-9b94a0f9c2cad99fef925a7b33f852aa5e38913a.zip
Avoid change rowCount value outside of BeginModel[Insert|Remove|Reset] functions
Diffstat (limited to 'libqmenumodel/src/qmenumodel.cpp')
-rw-r--r--libqmenumodel/src/qmenumodel.cpp18
1 files changed, 11 insertions, 7 deletions
diff --git a/libqmenumodel/src/qmenumodel.cpp b/libqmenumodel/src/qmenumodel.cpp
index 57c61d0..c0d8c94 100644
--- a/libqmenumodel/src/qmenumodel.cpp
+++ b/libqmenumodel/src/qmenumodel.cpp
@@ -38,7 +38,8 @@ extern "C" {
QMenuModel::QMenuModel(GMenuModel *other, QObject *parent)
: QAbstractListModel(parent),
m_menuModel(0),
- m_signalChangedId(0)
+ m_signalChangedId(0),
+ m_rowCount(0)
{
m_cache = new QHash<int, QMenuModel*>;
setMenuModel(other);
@@ -87,7 +88,7 @@ QVariantMap QMenuModel::get(int row) const
*/
int QMenuModel::count() const
{
- return rowCount();
+ return m_rowCount;
}
/*! \internal */
@@ -105,12 +106,13 @@ void QMenuModel::setMenuModel(GMenuModel *other)
if (m_menuModel) {
g_object_ref(m_menuModel);
- // this will trigger the menu load
- (void) g_menu_model_get_n_items(m_menuModel);
+ m_rowCount = g_menu_model_get_n_items(m_menuModel);
m_signalChangedId = g_signal_connect(m_menuModel,
"items-changed",
G_CALLBACK(QMenuModel::onItemsChanged),
this);
+ } else {
+ m_rowCount = 0;
}
endResetModel();
@@ -296,12 +298,13 @@ void QMenuModel::onItemsChanged(GMenuModel *model,
int prevcount = g_menu_model_get_n_items(model) + removed - added;
if (removed > 0) {
self->beginRemoveRows(QModelIndex(), position, position + removed - 1);
+ self->m_rowCount -= removed;
// Remove invalidated menus from the cache
for (int i = position, iMax = position + removed; i < iMax; ++i) {
if (cache->contains(i)) {
- QMenuModel *model = cache->take(i);
- model->setMenuModel(NULL);
- model->deleteLater();
+ QMenuModel *cached = cache->take(i);
+ cached->setMenuModel(NULL);
+ cached->deleteLater();
}
}
// Update the indexes of other cached menus to account for the removals
@@ -321,6 +324,7 @@ void QMenuModel::onItemsChanged(GMenuModel *model,
cache->insert(i + added, cache->take(i));
}
}
+ self->m_rowCount += removed;
self->endInsertRows();
}
}