From 94b9d215c37b6e7b5b539b48dcfe72f297a27973 Mon Sep 17 00:00:00 2001 From: megabigbug Date: Tue, 22 Jun 2010 18:32:14 +0200 Subject: fix (almost) empty area in the previews --- src/previewselectorbar.cpp | 3 --- src/tabbar.cpp | 2 +- src/websnap.cpp | 57 +++++++++++++++++++++++++++++----------------- src/websnap.h | 6 +++-- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/previewselectorbar.cpp b/src/previewselectorbar.cpp index cacf6968..037222d6 100644 --- a/src/previewselectorbar.cpp +++ b/src/previewselectorbar.cpp @@ -124,9 +124,6 @@ void PreviewSelectorBar::clicked() if (page) { - // this is done just lo let the render process being faster.. - WebSnap::renderPreview(*page); - KUrl url = page->mainFrame()->url(); QStringList names = ReKonfig::previewNames(); QStringList urls = ReKonfig::previewUrls(); diff --git a/src/tabbar.cpp b/src/tabbar.cpp index f3584eed..e8066715 100644 --- a/src/tabbar.cpp +++ b/src/tabbar.cpp @@ -181,7 +181,7 @@ void TabBar::showTabPreview() m_previewPopup.data()->setFixedSize(w, h); QLabel *l = new QLabel(); - l->setPixmap( WebSnap::renderPreview(*indexedTab->page(), w, h, false) ); + l->setPixmap( WebSnap::renderTabPreview(*indexedTab->page(), w, h) ); m_previewPopup.data()->setView(l); m_previewPopup.data()->layout()->setAlignment(Qt::AlignTop); diff --git a/src/websnap.cpp b/src/websnap.cpp index 03bdb544..c4e50b3c 100644 --- a/src/websnap.cpp +++ b/src/websnap.cpp @@ -79,9 +79,33 @@ void WebSnap::load() } + +QPixmap WebSnap::render(const QWebPage &page, int w, int h) +{ + // create the page image + QPixmap pageImage = QPixmap(w, h); + pageImage.fill(Qt::transparent); + + // render it + QPainter p(&pageImage); + page.mainFrame()->render(&p, QWebFrame::ContentsLayer); + p.end(); + + return pageImage; +} + + +QPixmap WebSnap::renderTabPreview(const QWebPage &page, int w, int h) +{ + QPixmap pageImage = WebSnap::render(page, page.viewportSize().width()+17, page.viewportSize().height()); + return pageImage.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); +} + + + // NOTE please, be careful modifying this. // You are playing with fire.. -QPixmap WebSnap::renderPreview(const QWebPage &page, int w, int h, bool save) +QPixmap WebSnap::renderPagePreview(const QWebPage &page, int w, int h) { // prepare page QSize oldSize = page.viewportSize(); @@ -96,34 +120,25 @@ QPixmap WebSnap::renderPreview(const QWebPage &page, int w, int h, bool save) size = QSize(width, width * ((0.0 + h) / w)); page.setViewportSize(size); - // create the page image - QImage pageImage = QImage(size, QImage::Format_ARGB32_Premultiplied); - pageImage.fill(Qt::transparent); + //render + QPixmap pageImage = WebSnap::render(page, page.viewportSize().width(), page.viewportSize().height()); + + // detect scrollbar size + int scrollbarWidth = (page.mainFrame()->scrollBarMaximum(Qt::Horizontal) ? 17 : 0); //TODO: detect QStyle size for scrollbars + int scrollbarHeight = (page.mainFrame()->scrollBarMaximum(Qt::Vertical) ? 17 : 0); - // render it - QPainter p(&pageImage); - page.mainFrame()->render(&p, QWebFrame::ContentsLayer); - p.end(); + // resize image + pageImage = pageImage.copy(0, 0, width - scrollbarWidth, size.height() - scrollbarHeight); pageImage = pageImage.scaled(w, h, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); // restore page settings page.setViewportSize(oldSize); - - QPixmap pm = QPixmap::fromImage(pageImage); - if(save) - { - KUrl url(page.mainFrame()->url()); - kDebug() << "saving preview"; - QString path = imagePathFromUrl(url); - QFile::remove(path); - pm.save(path); - } - - return pm; + return pageImage; } + QString WebSnap::imagePathFromUrl(const KUrl &url) { QUrl temp = QUrl(url.url()); @@ -148,7 +163,7 @@ void WebSnap::saveResult(bool ok) { if (ok) { - QPixmap image = renderPreview(m_page, WIDTH, HEIGHT); + QPixmap image = renderPagePreview(m_page, WIDTH, HEIGHT); QString path = imagePathFromUrl(m_url); QFile::remove(path); image.save(path); diff --git a/src/websnap.h b/src/websnap.h index cfc2c83e..c9ab2e71 100644 --- a/src/websnap.h +++ b/src/websnap.h @@ -89,8 +89,9 @@ public: * * @return the pixmap snapped from the page */ - static QPixmap renderPreview(const QWebPage &page, int w = WIDTH, int h = HEIGHT, bool save = true); - + static QPixmap renderPagePreview(const QWebPage &page, int w = WIDTH, int h = HEIGHT); + static QPixmap renderTabPreview(const QWebPage &page, int w, int h); + /** * Guess the local path where the image for the url provided * should be @@ -118,6 +119,7 @@ signals: private: QWebPage m_page; KUrl m_url; + static QPixmap render(const QWebPage &page, int w, int h); }; #endif // WEB_SNAP_H -- cgit v1.2.1