Now the count of unread posts is back with a brand new
Leonardo Robol [2011-10-25 10:06]
Now the count of unread posts is back with a brand new
QStyledItemDelegate and custom painting.
diff --git a/Larss.pro b/Larss.pro
index 437cd54..e19a7e7 100644
--- a/Larss.pro
+++ b/Larss.pro
@@ -17,7 +17,8 @@ SOURCES += larss/main.cpp\
larss/feedpoller.cpp \
larss/editfeeddialog.cpp \
larss/editcategorydialog.cpp \
- larss/feednode.cpp
+ larss/feednode.cpp \
+ larss/unreadcountitemdelegate.cpp
HEADERS += include/mainwindow.h \
include/feedmodel.h \
@@ -25,7 +26,8 @@ HEADERS += include/mainwindow.h \
include/feedpoller.h \
include/editfeeddialog.h \
include/editcategorydialog.h \
- include/feednode.h
+ include/feednode.h \
+ include/unreadcountitemdelegate.h
FORMS += ui/mainwindow.ui \
ui/editfeeddialog.ui \
diff --git a/include/feedmodel.h b/include/feedmodel.h
index a7a2859..039970b 100644
--- a/include/feedmodel.h
+++ b/include/feedmodel.h
@@ -52,6 +52,11 @@ public:
*/
void select();
+ /**
+ * @brief Call dataChanged
+ */
+ void triggerDataChanged();
+
FeedNode * itemFromIndex (const QModelIndex& index);
signals:
diff --git a/include/unreadcountitemdelegate.h b/include/unreadcountitemdelegate.h
new file mode 100644
index 0000000..3f90d90
--- /dev/null
+++ b/include/unreadcountitemdelegate.h
@@ -0,0 +1,32 @@
+#ifndef UNREADCOUNTITEMDELEGATE_H
+#define UNREADCOUNTITEMDELEGATE_H
+
+#include <QObject>
+#include <QStyledItemDelegate>
+#include "feedmodel.h"
+#include "rssparser.h"
+
+namespace Larss {
+
+ class UnReadCountItemDelegate : public QStyledItemDelegate
+ {
+ Q_OBJECT
+ public:
+ explicit UnReadCountItemDelegate(FeedModel *feedModel, RssParser *rssParser, QObject *parent = 0);
+
+ protected:
+ void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
+ // QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
+
+ signals:
+
+ public slots:
+
+ private:
+ FeedModel *feedModel;
+ RssParser *rssParser;
+
+ };
+}
+
+#endif // UNREADCOUNTITEMDELEGATE_H
diff --git a/larss/feedmodel.cpp b/larss/feedmodel.cpp
index 944d2b8..e79b57b 100644
--- a/larss/feedmodel.cpp
+++ b/larss/feedmodel.cpp
@@ -177,3 +177,10 @@ FeedModel::itemFromIndex(const QModelIndex &index)
return rootNode;
}
+void
+FeedModel::triggerDataChanged()
+{
+ dataChanged(index(1, 1, QModelIndex()),
+ index(1, 1, QModelIndex()));
+}
+
diff --git a/larss/mainwindow.cpp b/larss/mainwindow.cpp
index 671c9d6..a052b92 100644
--- a/larss/mainwindow.cpp
+++ b/larss/mainwindow.cpp
@@ -2,6 +2,7 @@
#include "ui_mainwindow.h"
#include "editfeeddialog.h"
#include "editcategorydialog.h"
+#include "unreadcountitemdelegate.h"
#include <QDebug>
#include <QtGui>
@@ -26,7 +27,9 @@ MainWindow::MainWindow(QWidget *parent) :
feedModel = new FeedModel(db, this);
rssParser = new RssParser(db, feedModel, this);
+ // Create the delegate that counts unread posts.
ui->feedTreeView->setModel(feedModel);
+ ui->feedTreeView->setItemDelegate (new UnReadCountItemDelegate(feedModel, rssParser));
// Load the RSSParser, hiding the unnecessary columns
ui->newsTableView->setModel(rssParser);
diff --git a/larss/rssparser.cpp b/larss/rssparser.cpp
index e6ede52..28313b1 100644
--- a/larss/rssparser.cpp
+++ b/larss/rssparser.cpp
@@ -116,7 +116,7 @@ Larss::RssParser::setReadStatus(const QModelIndex& index, bool read)
setData(read_index, read ? 1 : 0);
if (!submitAll())
qDebug() << "Error while setting the read flag";
- dataChanged (index, index);
+ model->triggerDataChanged();
}
quint64
diff --git a/larss/unreadcountitemdelegate.cpp b/larss/unreadcountitemdelegate.cpp
new file mode 100644
index 0000000..c01e8d7
--- /dev/null
+++ b/larss/unreadcountitemdelegate.cpp
@@ -0,0 +1,39 @@
+#include "unreadcountitemdelegate.h"
+#include "feednode.h"
+#include <QStyledItemDelegate>
+#include <QPainter>
+
+using namespace Larss;
+
+UnReadCountItemDelegate::UnReadCountItemDelegate(FeedModel *feedModel, RssParser *rssParser, QObject *parent) :
+ QStyledItemDelegate(parent), feedModel(feedModel), rssParser(rssParser)
+{
+}
+
+void
+UnReadCountItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+ if (index.isValid())
+ {
+ FeedNode *node = feedModel->itemFromIndex(index);
+ if (node->type() == FeedNode::Feed)
+ {
+ quint32 unreadPosts = rssParser->getUnreadCount(index);
+ if (unreadPosts > 0)
+ {
+ // Draw highlight if necessary
+ if (option.state & QStyle::State_Selected)
+ painter->fillRect(option.rect, option.palette.highlight());
+
+ // Set bold font
+ QFont font = option.font;
+ font.setBold(true);
+ painter->setFont(font);
+ painter->drawText(option.rect, Qt::AlignLeft, QString ("%1 (%2)").arg(index.data(Qt::DisplayRole).toString()).arg(unreadPosts));
+
+ return;
+ }
+ }
+ }
+ QStyledItemDelegate::paint(painter, option, index);
+}