From 0b50199b9262003cfa685a5de78c9aab440c3e16 Mon Sep 17 00:00:00 2001 From: megabigbug Date: Tue, 22 Sep 2009 18:59:38 +0200 Subject: fix page preview --- src/mainview.cpp | 2 +- src/websnap.cpp | 46 ++++++++++++++++++++++++++++++---------------- src/websnap.h | 2 +- 3 files changed, 32 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/mainview.cpp b/src/mainview.cpp index 47e39524..59ffde4d 100644 --- a/src/mainview.cpp +++ b/src/mainview.cpp @@ -661,7 +661,7 @@ void MainView::showTabPreview(int tab) m_previewPopup->setFrameShape(QFrame::NoFrame); m_previewPopup->setFixedSize(w, h); QLabel *l = new QLabel(); - l->setPixmap(WebSnap::renderPreview(webView(tab)->page(), w, h)); + l->setPixmap(WebSnap::renderPreview(*(webView(tab)->page()), w, h)); m_previewPopup->setView(l); m_previewPopup->layout()->setAlignment(Qt::AlignTop); m_previewPopup->layout()->setMargin(0); diff --git a/src/websnap.cpp b/src/websnap.cpp index 59275522..cec81dcd 100644 --- a/src/websnap.cpp +++ b/src/websnap.cpp @@ -61,30 +61,44 @@ void WebSnap::load() m_page.mainFrame()->load( QUrl(m_url) ); } -QPixmap WebSnap::renderPreview(QWebPage *page,int w, int h) +QPixmap WebSnap::renderPreview(const QWebPage &page,int w, int h) { // prepare page - page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff); //Why it doesn't work with one setScrollBarPolicy ? bug in qtwebkit ? - page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff); - page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff); - page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff); - + page.mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff); //Why it doesn't work with one setScrollBarPolicy ? bug in qtwebkit ? + page.mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff); + page.mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff); + page.mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff); + + // find the best size + QSize size; + if (page.viewportSize().width() && page.viewportSize().height()) + { + size = page.viewportSize(); + } + else + { + int width = page.mainFrame()->contentsSize().width(); + if (width < 640) width = 640; + size = QSize(width,width*((0.0+h)/w)); + page.setViewportSize(size); + } + // create the target surface - QPixmap image = QPixmap(page->viewportSize()); + QPixmap image = QPixmap(size); image.fill(Qt::transparent); - + // render QPainter p(&image); - page->mainFrame()->render(&p); + page.mainFrame()->render(&p); p.end(); image = image.scaled(w, h, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); // restore page settings - page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAsNeeded); - page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAsNeeded); - page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded); - page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded); - + page.mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAsNeeded); + page.mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAsNeeded); + page.mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded); + page.mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded); + return image; } @@ -96,9 +110,9 @@ void WebSnap::saveResult(bool ok) kDebug() << "Error loading site.."; return; } - + QString path = KStandardDirs::locateLocal("cache", QString("thumbs/rek") + m_pos + ".png", true); - m_image = renderPreview(&m_page, WIDTH, HEIGHT); + m_image = renderPreview(m_page, WIDTH, HEIGHT); if(m_image.save(path)) { kDebug() << "finished"; diff --git a/src/websnap.h b/src/websnap.h index 63f8dccf..5a550838 100644 --- a/src/websnap.h +++ b/src/websnap.h @@ -48,7 +48,7 @@ public: WebSnap(const QString &url, const QString &pos); QPixmap previewImage(); - static QPixmap renderPreview(QWebPage *page, int w, int h); + static QPixmap renderPreview(const QWebPage &page, int w, int h); signals: void finished(); -- cgit v1.2.1