From f7ae82bf7c7821ad3bd9a4a0fa9c0068fc2fc2e3 Mon Sep 17 00:00:00 2001
From: Andrea Diamantini <adjam7@gmail.com>
Date: Thu, 2 Feb 2012 23:21:27 +0100
Subject: clean up WebSnap and provide automatic thumbs update for about:tabs
 page

---
 src/newtabpage.cpp      |  7 +++++++
 src/tabpreviewpopup.cpp |  7 ++++++-
 src/tabpreviewpopup.h   |  3 +++
 src/websnap.cpp         | 51 ++++++++++++++++++++++++++-----------------------
 src/websnap.h           | 14 --------------
 5 files changed, 43 insertions(+), 39 deletions(-)

(limited to 'src')

diff --git a/src/newtabpage.cpp b/src/newtabpage.cpp
index 6325bc79..7392c920 100644
--- a/src/newtabpage.cpp
+++ b/src/newtabpage.cpp
@@ -540,6 +540,13 @@ void NewTabPage::tabsPage()
             if (url.protocol() == QL1S("about"))
                 continue;
 
+            if (!WebSnap::existsImage(url))
+            {
+                kDebug() << "image doesn't exist for url: " << url;
+                QPixmap preview = WebSnap::renderPagePreview(*w->mainView()->webTab(i)->page());
+                QString path = WebSnap::imagePathFromUrl(url.url());
+                preview.save(path);
+            }
             QString name = w->mainView()->webTab(i)->view()->title();
             QWebElement prev;
 
diff --git a/src/tabpreviewpopup.cpp b/src/tabpreviewpopup.cpp
index 31d3cca9..4466868d 100644
--- a/src/tabpreviewpopup.cpp
+++ b/src/tabpreviewpopup.cpp
@@ -3,6 +3,7 @@
 * This file is a part of the rekonq project
 *
 * Copyright (C) 2011 by Vyacheslav Blinov <blinov dot vyacheslav at gmail dot com>
+* Copyright (C) 2011-2012 by Andrea Diamantini <adjam7 at gmail dot com>
 *
 *
 * This program is free software; you can redistribute it and/or
@@ -23,6 +24,7 @@
 *
 * ============================================================ */
 
+
 //Self Includes
 #include "tabpreviewpopup.h"
 
@@ -87,6 +89,7 @@ TabPreviewPopup::TabPreviewPopup(WebTab* tab, QWidget* parent)
     setWebTab(tab);
 }
 
+
 TabPreviewPopup::~TabPreviewPopup()
 {
     delete m_thumbnail;
@@ -100,7 +103,7 @@ void TabPreviewPopup::setWebTab(WebTab* tab)
     int h = w * rApp->mainWindow()->size().height() / rApp->mainWindow()->size().width();
 
     if (!tab->part())
-        setThumbnail(WebSnap::renderTabPreview(*tab->page(), w, h));
+        setThumbnail(WebSnap::renderPagePreview(*tab->page(), w, h));
     else
     {
         QWidget *part = tab->part()->widget();
@@ -119,11 +122,13 @@ void TabPreviewPopup::setThumbnail(const QPixmap& pixmap)
     m_thumbnail->setPixmap(pixmap);
 }
 
+
 void TabPreviewPopup::setUrl(const QString& text)
 {
     m_url->setText(text);
 }
 
+
 void TabPreviewPopup::setFixedSize(int w, int h)
 {
     KPassivePopup::setFixedSize(w, h);
diff --git a/src/tabpreviewpopup.h b/src/tabpreviewpopup.h
index 1521bae2..01995a20 100644
--- a/src/tabpreviewpopup.h
+++ b/src/tabpreviewpopup.h
@@ -3,6 +3,7 @@
 * This file is a part of the rekonq project
 *
 * Copyright (C) 2011 by Vyacheslav Blinov <blinov dot vyacheslav at gmail dot com>
+* Copyright (C) 2011-2012 by Andrea Diamantini <adjam7 at gmail dot com>
 *
 *
 * This program is free software; you can redistribute it and/or
@@ -23,6 +24,7 @@
 *
 * ============================================================ */
 
+
 #ifndef TABPREVIEWPOPUP_H
 #define TABPREVIEWPOPUP_H
 
@@ -36,6 +38,7 @@
 class WebTab;
 class QLabel;
 
+
 class REKONQ_TESTS_EXPORT TabPreviewPopup : public KPassivePopup
 {
 
diff --git a/src/websnap.cpp b/src/websnap.cpp
index 6db6b3be..f077db84 100644
--- a/src/websnap.cpp
+++ b/src/websnap.cpp
@@ -34,16 +34,16 @@
 #include <KStandardDirs>
 
 // Qt Includes
-#include <QtCore/QSize>
-#include <QtCore/QFile>
+#include <QSize>
+#include <QFile>
 
 #include <QCryptographicHash>
 
-#include <QtGui/QPainter>
-#include <QtGui/QAction>
+#include <QPainter>
+#include <QAction>
 
-#include <QtWebKit/QWebFrame>
-#include <QtWebKit/QWebSettings>
+#include <QWebFrame>
+#include <QWebSettings>
 
 
 WebSnap::WebSnap(const KUrl& url, QObject *parent)
@@ -92,34 +92,37 @@ QPixmap WebSnap::render(const QWebPage &page, int w, int h)
 }
 
 
-QPixmap WebSnap::renderTabPreview(const QWebPage &page, int w, int h)
+// NOTE
+// to render page preview in a safe way, you CANNOT work with scrollbars!
+// In fact, disabling temporarily them DOES NOT work without reloading a page
+// that is something we CANNOT do.
+QPixmap WebSnap::renderPagePreview(const QWebPage &page, int w, int h)
 {
+    // store actual viewportsize
     QSize oldSize = page.viewportSize();
-    int width = page.mainFrame()->contentsSize().width();
-    page.setViewportSize(QSize(width, width * ((0.0 + h) / w)));
-    QPixmap pageImage = WebSnap::render(page, page.viewportSize().width(), page.viewportSize().height());
-    page.setViewportSize(oldSize);
-    return pageImage.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
-}
 
+    // prepare page
+    int renderWidth = page.mainFrame()->contentsSize().width();
+    int renderHeight = renderWidth * ((0.0 + h) / w);
 
-QPixmap WebSnap::renderPagePreview(const QWebPage &page, int w, int h)
-{
-    // remove temporarily scrollbars
-    page.mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
+    page.setViewportSize(QSize(renderWidth, renderHeight));
 
-    // prepare page
-    int width = page.mainFrame()->contentsSize().width();
-    page.setViewportSize(QSize(width, width * ((0.0 + h) / w)));
+    // consider scrollbars and render the page
+    bool verticalScrollBarActive = !page.mainFrame()->scrollBarGeometry(Qt::Vertical).isEmpty();
+    if (verticalScrollBarActive)
+        renderWidth -= 15;
 
-    // render
-    QPixmap pageImage = WebSnap::render(page, page.viewportSize().width(), page.viewportSize().height());
+    bool horizontalScrollBarActive = !page.mainFrame()->scrollBarGeometry(Qt::Horizontal).isEmpty();
+    if (horizontalScrollBarActive)
+        renderHeight -= 15;
+
+    QPixmap pageImage = WebSnap::render(page, renderWidth, renderHeight);
 
     // resize image
     pageImage = pageImage.scaled(w, h, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
 
-    // restore scrollbars
-    page.mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded);
+    // restore page state
+    page.setViewportSize(oldSize);
 
     return pageImage;
 }
diff --git a/src/websnap.h b/src/websnap.h
index 12a5e04c..fc5da869 100644
--- a/src/websnap.h
+++ b/src/websnap.h
@@ -86,20 +86,6 @@ public:
      */
     static QPixmap renderPagePreview(const QWebPage &page, int w = defaultWidth, int h = defaultHeight);
 
-    // static QPixmap renderVisiblePagePreview(const QWebPage &page, int w = WIDTH, int h = HEIGHT);
-    // TODO: try to make this method work => more previews for the urlbar
-
-    /**
-     * Snaps a pixmap of size w * h from a page for tab preview
-     *
-     * @param page the page to snap
-     * @param w the image width
-     * @param h the image height
-     *
-     * @return the pixmap snapped from the page
-     */
-    static QPixmap renderTabPreview(const QWebPage &page, int w, int h);
-
     /**
      * Guess the local path where the image for the url provided
      * should be
-- 
cgit v1.2.1