Added a bunch of key shortcuts.
[grr] / src / contentwindow.cpp
index 2acc73e..b18e78c 100644 (file)
@@ -3,6 +3,7 @@
 #include <QDesktopServices>
 #include <QtGui>
 #include <QtWebKit>
+#include <QtMaemo5>
 
 #include <QtGui/QX11Info>
 #include <X11/Xlib.h>
@@ -43,6 +44,14 @@ class ViewportItem : public QGraphicsWidget, public QAbstractKineticScroller {
                        }
                }
 
+       signals:
+               void showNextEntry();
+               void showPrevEntry();
+               void showOriginal();
+               void toggleStarred();
+               void toggleShared();
+               void toggleRead();
+
        protected:
                bool sceneEventFilter(QGraphicsItem *i, QEvent *e) {
                        bool res = false;
@@ -104,6 +113,43 @@ class ViewportItem : public QGraphicsWidget, public QAbstractKineticScroller {
                                        resizeWebViewToFrame();
                                }
                                break;
+
+                       case Qt::Key_J:
+                       case Qt::Key_N:
+                               emit showNextEntry();
+                               break;
+
+                       case Qt::Key_K:
+                       case Qt::Key_P:
+                               emit showPrevEntry();
+                               break;
+
+                       case Qt::Key_Space:
+                               if(e->modifiers() & Qt::ShiftModifier) {
+                                       y = m_widget->y() + size().height();
+                                       m_widget->setY(y > 0 ? 0 : y);
+                               }
+                               else {
+                                       y = m_widget->y() - size().height();;
+                                       h = (m_widget->size() - size()).height();
+                                       m_widget->setY(y < -h ? -h : y);
+                               }
+                               break;
+
+                       case Qt::Key_S:
+                               if(e->modifiers() & Qt::ShiftModifier)
+                                       emit toggleShared();
+                               else
+                                       emit toggleStarred();
+                               break;
+
+                       case Qt::Key_V:
+                               emit showOriginal();
+                               break;
+
+                       case Qt::Key_M:
+                               emit toggleRead();
+                               break;
                        }
                }
 
@@ -126,9 +172,6 @@ class ViewportItem : public QGraphicsWidget, public QAbstractKineticScroller {
                                m_widget->setPos(-p + m_overShoot);
                }
 
-               void cancelLeftMouseButtonPress(const QPoint & /*globalPressPos*/) {
-               }
-
                void sendEvent(QGraphicsItem *i, QEvent *e) {
                        m_ignoreEvents = true;
                        scene()->sendEvent(i, e);
@@ -203,44 +246,46 @@ ContentWindow::ContentWindow(QWidget *parent, Entry *e) : QMainWindow(parent) {
 
        QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);
 
-       entry = e;
-
        starred = new QAction(tr("Starred"), this);
        starred->setCheckable(true);
-       starred->setChecked((entry->flags & ENTRY_FLAG_STARRED));
        menuBar()->addAction(starred);
 
        shared = new QAction(tr("Shared"), this);
        shared->setCheckable(true);
-       shared->setChecked((entry->flags & ENTRY_FLAG_SHARED));
        menuBar()->addAction(shared);
 
        keepUnread = new QAction(tr("Keep unread"), this);
        keepUnread->setCheckable(true);
-       keepUnread->setEnabled((entry->flags & ENTRY_FLAG_LOCKED) == 0);
        menuBar()->addAction(keepUnread);
 
        menuBar()->addAction(tr("See original"), this, SLOT(seeOriginal()));
 
-       setWindowTitle(entry->title);
-
        GraphicsView *gv = new GraphicsView();
        webview = new QGraphicsWebView();
        gv->viewportItem()->setWidget(webview);
 
-       /* TODO: Configurable text size ?? */
        webview->settings()->setFontSize(QWebSettings::MinimumFontSize, 22);
+       webview->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
 
        connect(webview, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
        connect(webview, SIGNAL(loadStarted()), SLOT(loadStarted()));
-
-       webview->setHtml(entry->content);
+       connect(webview->page(), SIGNAL(linkClicked(const QUrl &)), SLOT(showLink(const QUrl &)));
+       connect(gv->viewportItem(), SIGNAL(showNextEntry()), SIGNAL(showNextEntry()));
+       connect(gv->viewportItem(), SIGNAL(showPrevEntry()), SIGNAL(showPrevEntry()));
+       connect(gv->viewportItem(), SIGNAL(showOriginal()), SLOT(seeOriginal()));
+       connect(gv->viewportItem(), SIGNAL(toggleStarred()), SLOT(toggleStarred()));
+       connect(gv->viewportItem(), SIGNAL(toggleShared()), SLOT(toggleShared()));
+       connect(gv->viewportItem(), SIGNAL(toggleRead()), SLOT(toggleRead()));
 
        setCentralWidget(gv);
        gv->viewportItem()->setFocus();
        gv->viewportItem()->grabKeyboard();
 
        grabZoomKeys(true);
+
+       entry = NULL;
+
+       showEntry(e);
 }
 
 ContentWindow::~ContentWindow() {
@@ -255,10 +300,76 @@ void ContentWindow::loadFinished(bool) {
        setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
 }
 
-void ContentWindow::seeOriginal() {
+void ContentWindow::showLink(const QUrl &url) {
        /* Attempt to launch external browser */
-       if(!QDesktopServices::openUrl(entry->link))
-               webview->setUrl(entry->link); /* Failed... Show inline */
+       if(!QDesktopServices::openUrl(url))
+               webview->setUrl(url); /* Failed... Show inline */
+}
+
+void ContentWindow::seeOriginal() {
+       showLink(entry->link);
+}
+
+void ContentWindow::toggleStarred() {
+       starred->toggle();
+       if(starred->isChecked()) {
+               QMaemo5InformationBox::information(this, "Starred",
+                       QMaemo5InformationBox::DefaultTimeout);
+       }
+       else {
+               QMaemo5InformationBox::information(this, "Star removed",
+                       QMaemo5InformationBox::DefaultTimeout);
+       }
+}
+
+void ContentWindow::toggleShared() {
+       shared->toggle();
+       if(shared->isChecked()) {
+               QMaemo5InformationBox::information(this, "Shared",
+                       QMaemo5InformationBox::DefaultTimeout);
+       }
+       else {
+               QMaemo5InformationBox::information(this, "Unshared",
+                       QMaemo5InformationBox::DefaultTimeout);
+       }
+}
+
+void ContentWindow::toggleRead() {
+       if(!keepUnread->isEnabled()) {
+               QMaemo5InformationBox::information(this, "Read state locked",
+                       QMaemo5InformationBox::DefaultTimeout);
+               return;
+       }
+
+       keepUnread->toggle();
+       if(keepUnread->isChecked()) {
+               QMaemo5InformationBox::information(this, "Marked unread",
+                       QMaemo5InformationBox::DefaultTimeout);
+       }
+       else {
+               QMaemo5InformationBox::information(this, "Marked read",
+                       QMaemo5InformationBox::DefaultTimeout);
+       }
+}
+
+void ContentWindow::showEntry(Entry *e) {
+       if(entry) {
+               /* Store settings of previously shown entry */
+               entry->markRead(!keepUnread->isChecked());
+               entry->markStar(starred->isChecked());
+               entry->markShared(shared->isChecked());
+       }
+
+       entry = e;
+
+       starred->setChecked((entry->flags & ENTRY_FLAG_STARRED));
+       shared->setChecked((entry->flags & ENTRY_FLAG_SHARED));
+       keepUnread->setChecked(false);
+       keepUnread->setEnabled((entry->flags & ENTRY_FLAG_LOCKED) == 0);
+
+       setWindowTitle(entry->title);
+
+       webview->setHtml(entry->content);
 }
 
 void ContentWindow::closeEvent(QCloseEvent *event) {