summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrea Diamantini <adjam7@gmail.com>2009-10-25 15:46:57 +0100
committerAndrea Diamantini <adjam7@gmail.com>2009-10-25 15:46:57 +0100
commitd206f183cda7dcb249846fcc15fcfeced0bf00ba (patch)
treeb63d722f21b55f6a0e21f12e22cd7bed493f0598
parentshould fix bug 211782 (diff)
parentA better preview implementation: (diff)
downloadrekonq-d206f183cda7dcb249846fcc15fcfeced0bf00ba.tar.xz
Merge commit 'refs/merge-requests/1904' of git://gitorious.org/rekonq/mainline into Lasts2
-rw-r--r--src/previewimage.cpp91
-rw-r--r--src/previewimage.h25
-rw-r--r--src/websnap.cpp30
-rw-r--r--src/websnap.h2
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 <QMouseEvent>
#include <QHBoxLayout>
#include <QVBoxLayout>
+#include <QPainter>
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();