Read status of feeds is honored now.

Leonardo Robol [2011-10-23 09:58]
Read status of feeds is honored now.
Filename
mainwindow.cpp
mainwindow.h
mainwindow.ui
rssparser.cpp
rssparser.h
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 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>561</width>
-    <height>382</height>
+    <width>720</width>
+    <height>480</height>
    </rect>
   </property>
   <property name="windowTitle">
    <string>Larss 0.1</string>
   </property>
   <widget class="QWidget" name="centralWidget">
-   <layout class="QVBoxLayout" name="verticalLayout">
+   <layout class="QVBoxLayout" name="verticalLayout_2">
     <item>
      <widget class="QSplitter" name="splitter_2">
       <property name="orientation">
@@ -25,13 +25,47 @@
        <property name="orientation">
         <enum>Qt::Vertical</enum>
        </property>
-       <widget class="QTableView" name="newsTableView"/>
-       <widget class="QWebView" name="webView">
-        <property name="url">
-         <url>
-          <string>about:blank</string>
-         </url>
+       <widget class="QTableView" name="newsTableView">
+        <property name="showGrid">
+         <bool>false</bool>
         </property>
+        <property name="gridStyle">
+         <enum>Qt::DashLine</enum>
+        </property>
+        <attribute name="verticalHeaderStretchLastSection">
+         <bool>true</bool>
+        </attribute>
+       </widget>
+       <widget class="QFrame" name="frame">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="frameShape">
+         <enum>QFrame::StyledPanel</enum>
+        </property>
+        <property name="frameShadow">
+         <enum>QFrame::Raised</enum>
+        </property>
+        <layout class="QVBoxLayout" name="verticalLayout">
+         <property name="spacing">
+          <number>0</number>
+         </property>
+         <property name="margin">
+          <number>0</number>
+         </property>
+         <item>
+          <widget class="QWebView" name="webView">
+           <property name="url">
+            <url>
+             <string>about:blank</string>
+            </url>
+           </property>
+          </widget>
+         </item>
+        </layout>
        </widget>
       </widget>
      </widget>
@@ -43,7 +77,7 @@
     <rect>
      <x>0</x>
      <y>0</y>
-     <width>561</width>
+     <width>720</width>
      <height>24</height>
     </rect>
    </property>
diff --git a/rssparser.cpp b/rssparser.cpp
index 6a4b22b..b9d87dc 100644
--- a/rssparser.cpp
+++ b/rssparser.cpp
@@ -3,6 +3,8 @@
 #include <QtXml>
 #include <QtSql>
 #include <QtNetwork>
+#include <QtGui>
+#include <QtCore>

 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<quint32, QString>();
@@ -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:
ViewGit