diff options
author | Renato Araujo Oliveira Filho <renato.filho@canonical.com> | 2012-12-14 12:03:52 -0300 |
---|---|---|
committer | Renato Araujo Oliveira Filho <renato.filho@canonical.com> | 2012-12-14 12:03:52 -0300 |
commit | 9b94a0f9c2cad99fef925a7b33f852aa5e38913a (patch) | |
tree | 872ceaef6e79c2d1d1e356f44875e4696ea38d79 /libqmenumodel/src | |
parent | c7cd7a43482319d81a2eade26d911b1e05a17441 (diff) | |
download | qmenumodel-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')
-rw-r--r-- | libqmenumodel/src/qmenumodel.cpp | 18 | ||||
-rw-r--r-- | libqmenumodel/src/qmenumodel.h | 1 |
2 files changed, 12 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(); } } diff --git a/libqmenumodel/src/qmenumodel.h b/libqmenumodel/src/qmenumodel.h index f186b5f..b6f2d37 100644 --- a/libqmenumodel/src/qmenumodel.h +++ b/libqmenumodel/src/qmenumodel.h @@ -67,6 +67,7 @@ private: QHash<int, QMenuModel*>* m_cache; GMenuModel *m_menuModel; guint m_signalChangedId; + guint m_rowCount; QVariant getStringAttribute(const QModelIndex &index, const QString &attribute) const; QVariant getLink(const QModelIndex &index, const QString &linkName) const; |