From ee95b665f335c966c80335c94a934d7a69b02be0 Mon Sep 17 00:00:00 2001 From: megabigbug Date: Sun, 25 Oct 2009 14:49:13 +0100 Subject: A better preview implementation: WebSnap doesn't render borders/background => borders/background not cached => style can be actualized easily. PreviewImage uses a label for display the background and an other label inside for display preview or animation. --- src/previewimage.cpp | 91 +++++++++++++++++++++++++++++++++++++++------------- src/previewimage.h | 25 +++++++++------ src/websnap.cpp | 30 ++--------------- src/websnap.h | 2 +- 4 files changed, 87 insertions(+), 61 deletions(-) diff --git a/src/previewimage.cpp b/src/previewimage.cpp index df3a3471..5b4d1efd 100644 --- a/src/previewimage.cpp +++ b/src/previewimage.cpp @@ -49,6 +49,7 @@ #include #include #include +#include PreviewImage::PreviewImage(const QUrl &url, const QString &title, int index, bool isFavorite) @@ -62,31 +63,47 @@ PreviewImage::PreviewImage(const QUrl &url, const QString &title, int index, boo , m_button(0) , m_imageLabel(new QLabel) , m_textLabel(new QLabel) + , m_backgroundLabel(new QLabel) + , m_previewLabel(new QLabel) { int borderTop = 14; - int borderRight = 14; + int borderRight = 16; int borderBottom = 14; - int borderLeft = 14; + int borderLeft = 16; int previewWidth=200; int previewHeight=150; int urlHeight=18; - setFixedSize(borderLeft+previewWidth+borderRight, borderTop+previewHeight+borderBottom+urlHeight); + m_size = QSize(borderLeft+previewWidth+borderRight, borderTop+previewHeight+borderBottom+urlHeight); + + setFixedSize(m_size); + m_previewLabel->setFixedSize(m_size); + + m_backgroundLabel->setPixmap(renderBackground(previewWidth,previewHeight, borderTop, borderBottom, borderLeft, borderRight)); + + m_previewLabel->setAlignment(Qt::AlignCenter); + m_backgroundLabel->setAlignment(Qt::AlignCenter); m_imageLabel->setAlignment(Qt::AlignCenter); m_textLabel->setAlignment(Qt::AlignCenter); - - QVBoxLayout *mainLayout = new QVBoxLayout; - mainLayout->setMargin(0); - mainLayout->addWidget(m_imageLabel); - mainLayout->addWidget(m_textLabel); - setLayout(mainLayout); - + + m_previewLabel->setLayout(new QVBoxLayout); + m_previewLabel->layout()->setMargin(0); + m_previewLabel->layout()->addWidget(m_backgroundLabel); + m_previewLabel->layout()->addWidget(m_textLabel); + m_previewLabel->setCursor(Qt::PointingHandCursor); + + m_backgroundLabel->setLayout(new QVBoxLayout); + m_backgroundLabel->layout()->addWidget(m_imageLabel); + + setLayout(new QHBoxLayout); + layout()->setMargin(0); + layout()->setAlignment(Qt::AlignCenter); + layout()->addWidget(m_previewLabel); + loadUrlPreview(url); - - setCursor(Qt::PointingHandCursor); } @@ -95,9 +112,38 @@ PreviewImage::~PreviewImage() delete ws; delete m_textLabel; delete m_imageLabel; + delete m_backgroundLabel; + delete m_previewLabel; } +QPixmap PreviewImage::renderBackground(int w, int h, int t, int b, int l, int r) +{ + QImage backImage(KStandardDirs::locate("appdata", "pics/bg.png")); + QImage resultImage(QSize(w + l + r, h + t + b), QImage::Format_ARGB32_Premultiplied); + + if (!backImage.isNull()) + { + int sw = backImage.width() - l - r; + int sh = backImage.height() - t - b; + QPainter pt(&resultImage); + pt.setCompositionMode(QPainter::CompositionMode_Source); + pt.fillRect(resultImage.rect(), Qt::transparent); + pt.drawImage(QRect(0, 0, l, t), backImage, QRect(0, 0, l, t)); + pt.drawImage(QRect(l, 0, w, t), backImage, QRect(l, 0, sw, t)); + pt.drawImage(QRect(l + w, 0, r, t), backImage, QRect(l + sw, 0, r, t)); + pt.drawImage(QRect(0, t, l, h), backImage, QRect(0, t, l, sh)); + pt.drawImage(QRect(l, t, w, h), backImage, QRect(l, t, sw, sh)); + pt.drawImage(QRect(l + w, t, r, h), backImage, QRect(l + sw, t, r, sh)); + pt.drawImage(QRect(0, t + h, l , b), backImage, QRect(0, t + sh, l , b)); + pt.drawImage(QRect(l, t + h, w, b), backImage, QRect(l, t + sh, sw, b)); + pt.drawImage(QRect(l + w, t + h, w, b), backImage, QRect(l + sw, t + sh, sw, b)); + pt.end(); + } + + return QPixmap::fromImage(resultImage); +} + void PreviewImage::loadUrlPreview(const QUrl& url) { @@ -109,7 +155,7 @@ void PreviewImage::loadUrlPreview(const QUrl& url) return; } - m_textLabel->setMaximumSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX); //unhide + m_previewLabel->setFixedSize(m_size); //unhide m_savePath = KStandardDirs::locateLocal("cache", QString("thumbs/") + guessNameFromUrl(m_url) + ".png", true); @@ -145,19 +191,19 @@ void PreviewImage::snapFinished() QMovie *m = m_imageLabel->movie(); delete m; m_imageLabel->setMovie(0); - + m_pixmap = ws->previewImage(); m_imageLabel->setPixmap(m_pixmap); checkTitle(); m_textLabel->setText(m_title); - + setCursor(Qt::PointingHandCursor); - + // kDebug() << "m_pixmap: " << m_pixmap.size(); // kDebug() << "text label: " << m_textLabel->size(); // kDebug() << "image label: " << m_imageLabel->size(); // kDebug() << "widget: " << size(); - + m_pixmap.save(m_savePath); if(m_index > -1) @@ -187,18 +233,17 @@ void PreviewImage::showEmptyPreview() m_imageLabel->clear(); m_textLabel->clear(); - m_textLabel->setMaximumSize(0,0); //hide (is there an other way for hide ?) + m_previewLabel->setFixedSize(0,0); //hide - QHBoxLayout *layout = new QHBoxLayout(m_imageLabel); - m_button = new QToolButton(m_imageLabel); + + m_button = new QToolButton(); m_button->setDefaultAction(historyMenu()); m_button->setPopupMode(QToolButton::InstantPopup); m_button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); m_button->setText(i18n("Add Preview")); m_button->setAutoRaise(true); m_button->setIconSize(QSize(48, 48)); - layout->addWidget(m_button); - m_imageLabel->setLayout(layout); + layout()->addWidget(m_button); } @@ -320,7 +365,7 @@ void PreviewImage::setUrlFromAction() m_url = KUrl(urlData.at(0)); m_title = urlData.at(1); checkTitle(); - + if(m_button) { m_imageLabel->layout()->deleteLater(); diff --git a/src/previewimage.h b/src/previewimage.h index d7f4d2aa..e9504210 100644 --- a/src/previewimage.h +++ b/src/previewimage.h @@ -49,44 +49,49 @@ class PreviewImage : public QWidget public: PreviewImage(const QUrl &url, const QString &title, int index, bool isFavorite); ~PreviewImage(); - + QString guessNameFromUrl(QUrl url); - + public slots: void snapFinished(); void removeMe(); void setUrlFromAction(); void refreshPreview(); - + protected: void contextMenuEvent(QContextMenuEvent *event); void mouseDoubleClickEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); - + void loadUrlPreview(const QUrl &url); KActionMenu *historyMenu(); void showEmptyPreview(); - + private: void checkTitle(); - + QPixmap renderBackground(int w, int h, int t, int b, int l, int r); + QPixmap m_pixmap; WebSnap *ws; - + QString m_savePath; bool loadingSnapshot; - + QUrl m_url; QString m_title; bool m_isFavorite; int m_index; - + QToolButton *m_button; - + QLabel *m_imageLabel; QLabel *m_textLabel; + QLabel *m_backgroundLabel; + QLabel *m_previewLabel; + + QSize m_size; }; #endif // PREVIEW_IMAGE_H diff --git a/src/websnap.cpp b/src/websnap.cpp index bb2baf49..7dcbb836 100644 --- a/src/websnap.cpp +++ b/src/websnap.cpp @@ -74,7 +74,7 @@ void WebSnap::load() } -QPixmap WebSnap::renderPreview(const QWebPage &page,int w, int h, bool border) +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? @@ -111,31 +111,7 @@ QPixmap WebSnap::renderPreview(const QWebPage &page,int w, int h, bool border) page.mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded); page.mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded); - if(!border) - return QPixmap::fromImage(pageImage); - - // background image - QSize fixedSize(w + 30, h + 26); - QImage backImage = QImage(fixedSize, QImage::Format_ARGB32_Premultiplied); - QString backImagePath = KStandardDirs::locate("appdata", "pics/bg.png"); - backImage.load( backImagePath ); - - // create target - QImage resultImage = QImage(fixedSize, QImage::Format_ARGB32_Premultiplied); - resultImage.fill(Qt::transparent); - - QPainter pt(&resultImage); - pt.setCompositionMode(QPainter::CompositionMode_Source); - pt.fillRect(resultImage.rect(), Qt::transparent); - pt.setCompositionMode(QPainter::CompositionMode_SourceOver); - pt.drawImage(0, 0, backImage); - pt.setCompositionMode(QPainter::CompositionMode_SourceOver); - pt.drawImage(15, 13, pageImage); - pt.setCompositionMode(QPainter::CompositionMode_DestinationOver); - pt.fillRect(resultImage.rect(), Qt::transparent); - pt.end(); - - return QPixmap::fromImage(resultImage); + return QPixmap::fromImage(pageImage); } @@ -148,7 +124,7 @@ void WebSnap::saveResult(bool ok) return; } - m_image = renderPreview(m_page, WIDTH, HEIGHT, true); + m_image = renderPreview(m_page, WIDTH, HEIGHT); emit finished(); } diff --git a/src/websnap.h b/src/websnap.h index 026c236c..6c5b4af9 100644 --- a/src/websnap.h +++ b/src/websnap.h @@ -52,7 +52,7 @@ public: ~WebSnap(); QPixmap previewImage(); - static QPixmap renderPreview(const QWebPage &page, int w, int h, bool border = false); + static QPixmap renderPreview(const QWebPage &page, int w, int h); QString snapTitle(); QUrl snapUrl(); -- cgit v1.2.1