diff options
Diffstat (limited to 'src/previewimage.cpp')
-rw-r--r-- | src/previewimage.cpp | 160 |
1 files changed, 102 insertions, 58 deletions
diff --git a/src/previewimage.cpp b/src/previewimage.cpp index 03741770..e7f8aea5 100644 --- a/src/previewimage.cpp +++ b/src/previewimage.cpp @@ -48,17 +48,31 @@ #include <QMovie> #include <QMouseEvent> #include <QHBoxLayout> +#include <QVBoxLayout> -PreviewImage::PreviewImage(const QUrl &url, int index, bool isFavorite) - : QLabel() +PreviewImage::PreviewImage(const QUrl &url, const QString &title, int index, bool isFavorite) + : QWidget() , ws(0) - , m_url(0) , loadingSnapshot(false) + , m_url(0) + , m_title(title) , m_isFavorite(isFavorite) , m_index(index) , m_button(0) -{ + , m_imageLabel(new QLabel) + , m_textLabel(new QLabel) +{ + setMinimumSize(300,200); + + m_imageLabel->setAlignment(Qt::AlignCenter); + m_textLabel->setAlignment(Qt::AlignCenter); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(m_imageLabel); + mainLayout->addWidget(m_textLabel); + setLayout(mainLayout); + loadUrlPreview(url); } @@ -66,6 +80,8 @@ PreviewImage::PreviewImage(const QUrl &url, int index, bool isFavorite) PreviewImage::~PreviewImage() { delete ws; + delete m_textLabel; + delete m_imageLabel; } @@ -73,33 +89,36 @@ PreviewImage::~PreviewImage() void PreviewImage::loadUrlPreview(const QUrl& url) { m_url = url; - + if(url.isEmpty()) { showEmptyPreview(); return; } - + m_savePath = KStandardDirs::locateLocal("cache", QString("thumbs/") + guessNameFromUrl(m_url) + ".png", true); - + if(QFile::exists(m_savePath)) { m_pixmap.load(m_savePath); - setPixmap(m_pixmap); + m_imageLabel->setPixmap(m_pixmap); + checkTitle(); + m_textLabel->setText(m_title); } else { loadingSnapshot = true; ws = new WebSnap( url ); connect(ws, SIGNAL(finished()), this, SLOT(snapFinished())); - + QString path = KStandardDirs::locate("appdata", "pics/busywidget.gif"); - + // load an animation waiting for site preview QMovie *movie = new QMovie(path, QByteArray(), this); movie->setSpeed(50); - setMovie(movie); + m_imageLabel->setMovie(movie); movie->start(); + m_textLabel->setText( i18n("Loading preview...") ); } } @@ -107,29 +126,36 @@ void PreviewImage::loadUrlPreview(const QUrl& url) void PreviewImage::snapFinished() { loadingSnapshot = false; - QMovie *m = movie(); + QMovie *m = m_imageLabel->movie(); delete m; - setMovie(0); + m_imageLabel->setMovie(0); m_pixmap = ws->previewImage(); - setPixmap(m_pixmap); - - m_pixmap.save(m_savePath); + m_imageLabel->setPixmap(m_pixmap); + checkTitle(); + m_textLabel->setText(m_title); +// 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) { // Update title QStringList names = ReKonfig::previewNames(); // update url (for added thumbs) QStringList urls = ReKonfig::previewUrls(); - + // stripTrailingSlash to be sure to get the same string for same adress urls.replace(m_index, ws->snapUrl().toString(QUrl::StripTrailingSlash)); names.replace(m_index, ws->snapTitle()); - + ReKonfig::setPreviewNames(names); ReKonfig::setPreviewUrls(urls); - + ReKonfig::self()->writeConfig(); } } @@ -140,10 +166,11 @@ void PreviewImage::showEmptyPreview() if(!m_isFavorite) return; - clear(); + m_imageLabel->clear(); + m_textLabel->clear(); - QHBoxLayout *layout = new QHBoxLayout(this); - m_button = new QToolButton(this); + QHBoxLayout *layout = new QHBoxLayout(m_imageLabel); + m_button = new QToolButton(m_imageLabel); m_button->setDefaultAction(historyMenu()); m_button->setPopupMode(QToolButton::InstantPopup); m_button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); @@ -151,6 +178,7 @@ void PreviewImage::showEmptyPreview() m_button->setAutoRaise(true); m_button->setIconSize(QSize(48, 48)); layout->addWidget(m_button); + m_imageLabel->setLayout(layout); } @@ -172,24 +200,10 @@ void PreviewImage::mousePressEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton) { - if(m_isFavorite) - { - Application::instance()->loadUrl(m_url); - } - else - { - MainView *mv = Application::instance()->mainWindow()->mainView(); - int actualIndex = mv->currentIndex(); - - kDebug() << "Actual index: " << actualIndex; - kDebug() << "m_index: " << m_index; - - mv->slotCloseTab(actualIndex); - mv->setCurrentIndex(m_index); - } + Application::instance()->loadUrl(m_url); return; }; - QLabel::mousePressEvent(event); + QWidget::mousePressEvent(event); } @@ -198,31 +212,31 @@ void PreviewImage::mouseReleaseEvent(QMouseEvent *event) kDebug() << "NO000... don't leave your finger from the button!!"; Q_UNUSED(event) } - + void PreviewImage::contextMenuEvent(QContextMenuEvent* event) { if(!m_isFavorite) return; - + if(loadingSnapshot) return; - + KMenu menu(this); KAction *a; - + if(!m_url.isEmpty()) { a = new KAction(KIcon("edit-delete"), i18n("Remove Thumbnail"), this); connect(a, SIGNAL(triggered(bool)), this, SLOT(removeMe())); menu.addAction(a); - + a = new KAction(KIcon("view-refresh"), i18n("Refresh Thumbnail"), &menu); connect(a, SIGNAL(triggered(bool)), this, SLOT(refreshPreview())); menu.addAction(a); } menu.addAction(historyMenu()); - + menu.exec(mapToGlobal(event->pos())); } @@ -231,7 +245,7 @@ KActionMenu* PreviewImage::historyMenu() { KActionMenu *histMenu = new KActionMenu(KIcon("insert-image"), i18n("Set page to preview"), this); QList<HistoryItem> history = Application::historyManager()->history(); - + if(history.isEmpty()) { KAction *a = new KAction(i18n("History is empty"), this); @@ -239,17 +253,19 @@ KActionMenu* PreviewImage::historyMenu() histMenu->addAction(a); return histMenu; } - + int maxItems = 15; for (int i = 0; i < maxItems && i < history.size() ; ++i) { HistoryItem it = history.at(i); KAction *a = new KAction(Application::icon(it.url), it.title, this); - a->setData(it.url); + QStringList urlData; + urlData << it.url << it.title; + a->setData(urlData); connect(a, SIGNAL(triggered(bool)), this, SLOT(setUrlFromAction())); histMenu->addAction(a); } - + return histMenu; } @@ -258,20 +274,20 @@ void PreviewImage::removeMe() { QStringList names = ReKonfig::previewNames(); QStringList urls = ReKonfig::previewUrls(); - + int index = urls.indexOf(QRegExp(m_url.toString(QUrl::StripTrailingSlash), Qt::CaseSensitive, QRegExp::FixedString)); urls.replace(index, QString("")); names.replace(index, QString("")); - + ReKonfig::setPreviewNames(names); ReKonfig::setPreviewUrls(urls); - + // sync file data ReKonfig::self()->writeConfig(); showEmptyPreview(); - + m_url = ""; } @@ -279,15 +295,33 @@ void PreviewImage::removeMe() void PreviewImage::setUrlFromAction() { KAction *a = qobject_cast<KAction*>(sender()); - KUrl url = KUrl(a->data().toString()); - + QStringList urlData = a->data().toStringList(); + + m_url = KUrl(urlData.at(0)); + m_title = urlData.at(1); + checkTitle(); + if(m_button) { - layout()->deleteLater(); + m_imageLabel->layout()->deleteLater(); m_button->menu()->deleteLater(); m_button->deleteLater(); - } - loadUrlPreview(url); + } + loadUrlPreview(m_url); + + // Update title + QStringList names = ReKonfig::previewNames(); + // update url (for added thumbs) + QStringList urls = ReKonfig::previewUrls(); + + // stripTrailingSlash to be sure to get the same string for same adress + urls.replace(m_index, m_url.toString(QUrl::StripTrailingSlash)); + names.replace(m_index, m_title); + + ReKonfig::setPreviewNames(names); + ReKonfig::setPreviewUrls(urls); + + ReKonfig::self()->writeConfig(); } @@ -313,6 +347,16 @@ QString PreviewImage::guessNameFromUrl(QUrl url) name.remove('?'); name.remove('='); name.remove('+'); - + return name; } + + +void PreviewImage::checkTitle() +{ + if(m_title.length() > 23) + { + m_title.truncate(20); + m_title += "..."; + } +} |