From a78a1a9c649d88d97dcb6f3dacbaf91325ec71d2 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Wed, 26 Apr 2017 08:09:09 +0300 Subject: [PATCH] Add invariant checking in ArticleListWidget --- src/gui/rss/articlelistwidget.cpp | 46 +++++++++++++++++++++---------- src/gui/rss/articlelistwidget.h | 3 +- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/gui/rss/articlelistwidget.cpp b/src/gui/rss/articlelistwidget.cpp index 76d04bdc6..bce26f1b4 100644 --- a/src/gui/rss/articlelistwidget.cpp +++ b/src/gui/rss/articlelistwidget.cpp @@ -38,6 +38,8 @@ ArticleListWidget::ArticleListWidget(QWidget *parent) { setContextMenuPolicy(Qt::CustomContextMenu); setSelectionMode(QAbstractItemView::ExtendedSelection); + + checkInvariant(); } RSS::Article *ArticleListWidget::getRSSArticle(QListWidgetItem *item) const @@ -61,23 +63,32 @@ void ArticleListWidget::setRSSItem(RSS::Item *rssItem, bool unreadOnly) m_unreadOnly = unreadOnly; m_rssItem = rssItem; - if (!m_rssItem) return; - - m_rssItem = rssItem; - connect(m_rssItem, &RSS::Item::newArticle, this, &ArticleListWidget::handleArticleAdded); - connect(m_rssItem, &RSS::Item::articleRead, this, &ArticleListWidget::handleArticleRead); - connect(m_rssItem, &RSS::Item::articleAboutToBeRemoved, this, &ArticleListWidget::handleArticleAboutToBeRemoved); - - foreach (auto article, rssItem->articles()) { - if (!(m_unreadOnly && article->isRead())) - addItem(createItem(article)); + if (m_rssItem) { + connect(m_rssItem, &RSS::Item::newArticle, this, &ArticleListWidget::handleArticleAdded); + connect(m_rssItem, &RSS::Item::articleRead, this, &ArticleListWidget::handleArticleRead); + connect(m_rssItem, &RSS::Item::articleAboutToBeRemoved, this, &ArticleListWidget::handleArticleAboutToBeRemoved); + + foreach (auto article, rssItem->articles()) { + if (!(m_unreadOnly && article->isRead())) { + auto item = createItem(article); + addItem(item); + m_rssArticleToListItemMapping.insert(article, item); + } + } } + + checkInvariant(); } void ArticleListWidget::handleArticleAdded(RSS::Article *rssArticle) { - if (!(m_unreadOnly && rssArticle->isRead())) - insertItem(0, createItem(rssArticle)); + if (!(m_unreadOnly && rssArticle->isRead())) { + auto item = createItem(rssArticle); + insertItem(0, item); + m_rssArticleToListItemMapping.insert(rssArticle, item); + } + + checkInvariant(); } void ArticleListWidget::handleArticleRead(RSS::Article *rssArticle) @@ -90,14 +101,22 @@ void ArticleListWidget::handleArticleRead(RSS::Article *rssArticle) item->setData(Qt::ForegroundRole, QColor("grey")); item->setData(Qt::DecorationRole, QIcon(":/icons/sphere.png")); } + + checkInvariant(); } void ArticleListWidget::handleArticleAboutToBeRemoved(RSS::Article *rssArticle) { delete m_rssArticleToListItemMapping.take(rssArticle); + checkInvariant(); +} + +void ArticleListWidget::checkInvariant() const +{ + Q_ASSERT(count() == m_rssArticleToListItemMapping.count()); } -QListWidgetItem *ArticleListWidget::createItem(RSS::Article *article) +QListWidgetItem *ArticleListWidget::createItem(RSS::Article *article) const { Q_ASSERT(article); QListWidgetItem *item = new QListWidgetItem; @@ -113,6 +132,5 @@ QListWidgetItem *ArticleListWidget::createItem(RSS::Article *article) item->setData(Qt::DecorationRole, QIcon(":/icons/sphere2.png")); } - m_rssArticleToListItemMapping.insert(article, item); return item; } diff --git a/src/gui/rss/articlelistwidget.h b/src/gui/rss/articlelistwidget.h index dec2fb90f..6c35892b0 100644 --- a/src/gui/rss/articlelistwidget.h +++ b/src/gui/rss/articlelistwidget.h @@ -56,7 +56,8 @@ private slots: void handleArticleAboutToBeRemoved(RSS::Article *rssArticle); private: - QListWidgetItem *createItem(RSS::Article *article); + void checkInvariant() const; + QListWidgetItem *createItem(RSS::Article *article) const; RSS::Item *m_rssItem = nullptr; bool m_unreadOnly = false;