From 2d82368990262d0cb1aec0d56e3580def50f66bc Mon Sep 17 00:00:00 2001 From: Leonardo Robol Date: Sun, 23 Oct 2011 11:58:23 +0200 Subject: [PATCH] Read status of feeds is honored now. --- mainwindow.cpp | 22 +++++++++++++++++----- mainwindow.h | 2 ++ mainwindow.ui | 54 ++++++++++++++++++++++++++++++++++++++++++++---------- rssparser.cpp | 39 +++++++++++++++++++++++++++++++++++---- rssparser.h | 16 ++++++++++++++++ 5 files changed, 114 insertions(+), 19 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 4dc4293..d6a97bc 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -24,11 +24,11 @@ MainWindow::MainWindow(QWidget *parent) : rssParser = new RssParser(db, feedModel, this); ui->newsTableView->setModel(rssParser); ui->newsTableView->setSelectionBehavior(QAbstractItemView::SelectRows); -// ui->newsTableView->setColumnHidden(0, true); // ID -// ui->newsTableView->setColumnHidden(1, true); // Feed ID -// ui->newsTableView->setColumnHidden(3, true); // Link -// ui->newsTableView->setColumnHidden(6, true); // Read state - + ui->newsTableView->setColumnHidden(0, true); // ID + ui->newsTableView->setColumnHidden(1, true); // Feed ID + ui->newsTableView->setColumnHidden(3, true); // Link + ui->newsTableView->setColumnHidden(6, true); // Read state + ui->newsTableView->verticalHeader()->setHidden(true); } MainWindow::~MainWindow() @@ -54,3 +54,15 @@ void Larss::MainWindow::on_feedTreeView_clicked(const QModelIndex &index) // Trigger refresh of selected item rssParser->loadItem(index); } + +void Larss::MainWindow::on_newsTableView_clicked(const QModelIndex &index) +{ + // A row got activated, so open it in the webview. + QString link = rssParser->getLink(index); + ui->webView->load(link); + qDebug () << "Loading " << link; + + // And then mark it as read + rssParser->setReadStatus(index, true); + ui->newsTableView->selectRow(index.row()); +} diff --git a/mainwindow.h b/mainwindow.h index 6d33ddf..40338d0 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -25,6 +25,8 @@ private slots: void on_feedTreeView_clicked(const QModelIndex &index); + void on_newsTableView_clicked(const QModelIndex &index); + private: Ui::MainWindow *ui; void do_exit(); diff --git a/mainwindow.ui b/mainwindow.ui index 729df6f..be324a5 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -6,15 +6,15 @@ 0 0 - 561 - 382 + 720 + 480 Larss 0.1 - + @@ -25,13 +25,47 @@ Qt::Vertical - - - - - about:blank - + + + false + + Qt::DashLine + + + true + + + + + + 0 + 0 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + 0 + + + 0 + + + + + + about:blank + + + + + @@ -43,7 +77,7 @@ 0 0 - 561 + 720 24 diff --git a/rssparser.cpp b/rssparser.cpp index 6a4b22b..b9d87dc 100644 --- a/rssparser.cpp +++ b/rssparser.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include using namespace Larss; @@ -23,7 +25,7 @@ Larss::RssParser::RssParser(QSqlDatabase db, FeedModel *model, QObject *parent) setTable("news"); // Select manual submit so user cannot modify content directly - setEditStrategy(QSqlTableModel::OnManualSubmit); + setEditStrategy(QSqlTableModel::OnRowChange); // Set the source to an empty string, that means no source rssContent = new QHash(); @@ -85,6 +87,24 @@ Larss::RssParser::~RssParser() delete workQueue; } +QVariant +Larss::RssParser::data(const QModelIndex &idx, int role) const +{ + if (role == Qt::FontRole) + { + // Get default font + QFont default_font = QSqlTableModel::data(idx, role).toString(); + + // Check if this news is read or not + QSqlRecord record = this->record(idx.row()); + if (record.value("read") == 0) + default_font.setBold(true); + return default_font; + } + // Call the default implementaton in almost every case + return QSqlTableModel::data(idx, role); +} + void Larss::RssParser::loadItem(const QModelIndex &index) { @@ -153,9 +173,6 @@ Larss::RssParser::networkManagerReplyFinished(QNetworkReply *reply) qDebug () << "Error inserting record"; } } - - if (!submitAll()) - qDebug () << "SubmitAll() call failed"; } } else @@ -176,3 +193,17 @@ Larss::RssParser::networkManagerReplyFinished(QNetworkReply *reply) manager->get(QNetworkRequest(QUrl(model->getUrl(next_item)))); } } + +QString +Larss::RssParser::getLink(const QModelIndex &index) +{ + QSqlRecord record = this->record(index.row()); + return record.value("link").toString(); +} + +void +Larss::RssParser::setReadStatus(const QModelIndex& index, bool read) +{ + QModelIndex read_index = createIndex(index.row(), 6, index.internalPointer()); + setData(read_index, read ? 1 : 0); +} diff --git a/rssparser.h b/rssparser.h index df022f2..8ee69f4 100644 --- a/rssparser.h +++ b/rssparser.h @@ -34,6 +34,22 @@ namespace Larss { */ QVariant headerData (int section, Qt::Orientation orientation, int role) const; + /** + * @brief Get the link associated with a given ModelIndex. + */ + QString getLink (const QModelIndex& index); + + /** + * @brief Set the read status on a news. + */ + void setReadStatus (const QModelIndex& index, bool read); + + /** + * @brief Reimplement data to make unread post bold. + */ + QVariant data(const QModelIndex &idx, int role) const; + + signals: public slots: -- 2.1.4