From 7c6db1a17dbede4911d2c13cd36a485272f3e5a4 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Sun, 13 Sep 2009 18:20:57 +0200 Subject: We have thumbnails! Hugh! --- src/CMakeLists.txt | 1 + src/homepage.cpp | 216 ++++++++++++++++++++++++++++++++--------------------- src/homepage.h | 9 +-- src/websnap.cpp | 88 ++++++++++++++++++++++ src/websnap.h | 65 ++++++++++++++++ 5 files changed, 286 insertions(+), 93 deletions(-) create mode 100644 src/websnap.cpp create mode 100644 src/websnap.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f7d54c39..db3722fb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,7 @@ ### ------- SETTING REKONQ FILES.. SET( rekonq_SRCS + websnap.cpp homepage.cpp networkaccessmanager.cpp autosaver.cpp diff --git a/src/homepage.cpp b/src/homepage.cpp index 6acd4afd..59078c51 100644 --- a/src/homepage.cpp +++ b/src/homepage.cpp @@ -32,6 +32,7 @@ #include "historymodels.h" #include "bookmarks.h" #include "application.h" +#include "websnap.h" // KDE Includes #include @@ -46,7 +47,6 @@ HomePage::HomePage(QObject *parent) : QObject(parent) { m_homePagePath = KStandardDirs::locate("data", "rekonq/htmls/home.html"); - m_imagesPath = "file://" + KStandardDirs::locate("appdata", "pics/"); } @@ -65,114 +65,156 @@ QString HomePage::rekonqHomePage() return QString(""); } - QString history = fillHistory(); - - QString bookmarks = fillBookmarks(); + QString speed = speedDial(); + QString search = searchEngines(); + QString closedtabs = recentlyClosedTabs(); + QString html = QString(QLatin1String(file.readAll())) - .arg(m_imagesPath) - .arg(history) - .arg(bookmarks); + .arg(search) + .arg(closedtabs) + .arg(speed) + ; return html; } -QString HomePage::fillHistory() +QString HomePage::speedDial() { - QString history = QString(); - HistoryTreeModel *model = Application::historyManager()->historyTreeModel(); + KUrl::List ul ; + ul << KUrl("http://www.google.it") << KUrl("http://www.kde.org") << KUrl("http://rekonq.sourceforge.net") + << KUrl("http://www.slacky.eu") << KUrl("http://kde-apps.org") << KUrl("http://www.kernel.org") + << KUrl("http://it.wikipedia.org") << KUrl("http://www.adjam.org") << KUrl("http://adjamblog.wordpress.com"); - int i = 0; - do + QString speed = QString(); + for(int i = 0; i< ul.count(); ++i) { - QModelIndex index = model->index(i, 0, QModelIndex() ); - if(model->hasChildren(index)) - { - QString s = QString::number(i); - history += createSubMenu(index.data().toString(), s); - history += "

"; - for(int j=0; j< model->rowCount(index); ++j) - { - QModelIndex son = model->index(j, 0, index ); -// FIXME add an icon to each history item history += "\"icon\""; - history += QString("") + - son.data().toString() + QString("
"); - } - history += "

"; - } - else + KUrl url = ul.at(i); + QString fileName = QString("thumb") + QString::number(i) + QString(".png"); + QString path = KStandardDirs::locateLocal("cache", QString("thumbs/") + fileName, true); + if( !QFile::exists(path) ) { - history += QString("

NO CHILDREN: ") + index.data().toString() + QString("

"); + kDebug() << "websnap"; + WebSnap *ws = new WebSnap(url, fileName); } - i++; + + speed += "\"""; } - while( model->hasIndex( i , 0 , QModelIndex() ) ); - - return history; - + return speed; } -QString HomePage::fillBookmarks() +QString HomePage::searchEngines() { - KBookmarkGroup toolBarGroup = Application::bookmarkProvider()->rootGroup(); - if (toolBarGroup.isNull()) - { - return QString("Error retrieving bookmarks!"); - } - - QString str = QString(""); - KBookmark bookmark = toolBarGroup.first(); - while (!bookmark.isNull()) - { - str += createBookItem(bookmark); - bookmark = toolBarGroup.next(bookmark); - } + QString engines = "engines"; - return str; + return engines; } -QString HomePage::createSubMenu(const QString &item, const QString &s) +QString HomePage::recentlyClosedTabs() { - QString menu = "
"; + QString closedtabs = "closed"; - menu += "

" + item + "

"; - return menu; + return closedtabs; } - -QString HomePage::createBookItem(const KBookmark &bookmark) -{ - static int i = 0; - - if (bookmark.isGroup()) - { - QString result = QString(""); - QString ss = "b" + QString::number(i); - i++; - - KBookmarkGroup group = bookmark.toGroup(); - KBookmark bm = group.first(); - result += createSubMenu( bookmark.text() , ss ); - result += "

"; - - while (!bm.isNull()) - { - result += createBookItem(bm); //menuAction->addAction(fillBookmarkBar(bm)); - bm = group.next(bm); - } - result += "

"; - return result; - } - - if(bookmark.isSeparator()) - { - return QString("
"); - } - - QString str = ""; // FIXME Add icon "\"icon\""; - str += "" + bookmark.text() + "
"; - return str; -} +// QString HomePage::fillHistory() +// { +// QString history = QString(); +// HistoryTreeModel *model = Application::historyManager()->historyTreeModel(); +// +// int i = 0; +// do +// { +// QModelIndex index = model->index(i, 0, QModelIndex() ); +// if(model->hasChildren(index)) +// { +// QString s = QString::number(i); +// history += createSubMenu(index.data().toString(), s); +// history += "

"; +// for(int j=0; j< model->rowCount(index); ++j) +// { +// QModelIndex son = model->index(j, 0, index ); +// // FIXME add an icon to each history item history += "\"icon\""; +// history += QString("") + +// son.data().toString() + QString("
"); +// } +// history += "

"; +// } +// else +// { +// history += QString("

NO CHILDREN: ") + index.data().toString() + QString("

"); +// } +// i++; +// } +// while( model->hasIndex( i , 0 , QModelIndex() ) ); +// +// return history; +// +// } +// +// +// QString HomePage::fillBookmarks() +// { +// KBookmarkGroup toolBarGroup = Application::bookmarkProvider()->rootGroup(); +// if (toolBarGroup.isNull()) +// { +// return QString("Error retrieving bookmarks!"); +// } +// +// QString str = QString(""); +// KBookmark bookmark = toolBarGroup.first(); +// while (!bookmark.isNull()) +// { +// str += createBookItem(bookmark); +// bookmark = toolBarGroup.next(bookmark); +// } +// +// return str; +// } +// +// +// QString HomePage::createSubMenu(const QString &item, const QString &s) +// { +// QString menu = "
"; +// +// menu += "

" + item + "

"; +// return menu; +// } +// +// +// QString HomePage::createBookItem(const KBookmark &bookmark) +// { +// static int i = 0; +// +// if (bookmark.isGroup()) +// { +// QString result = QString(""); +// QString ss = "b" + QString::number(i); +// i++; +// +// KBookmarkGroup group = bookmark.toGroup(); +// KBookmark bm = group.first(); +// result += createSubMenu( bookmark.text() , ss ); +// result += "

"; +// +// while (!bm.isNull()) +// { +// result += createBookItem(bm); //menuAction->addAction(fillBookmarkBar(bm)); +// bm = group.next(bm); +// } +// result += "

"; +// return result; +// } +// +// if(bookmark.isSeparator()) +// { +// return QString("
"); +// } +// +// QString str = ""; // FIXME Add icon "\"icon\""; +// str += "" + bookmark.text() + "
"; +// return str; +// } diff --git a/src/homepage.h b/src/homepage.h index e8b5b4f5..c4ef7fc5 100644 --- a/src/homepage.h +++ b/src/homepage.h @@ -46,14 +46,11 @@ public: QString rekonqHomePage(); private: - QString fillHistory(); - QString fillBookmarks(); - - QString createSubMenu(const QString &, const QString &); - QString createBookItem(const KBookmark &); + QString speedDial(); + QString searchEngines(); + QString recentlyClosedTabs(); QString m_homePagePath; - QString m_imagesPath; }; #endif // REKONQ_HOME_PAGE diff --git a/src/websnap.cpp b/src/websnap.cpp new file mode 100644 index 00000000..8bacd3b2 --- /dev/null +++ b/src/websnap.cpp @@ -0,0 +1,88 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2009 Nokia Corporation +* Copyright (C) 2009 by Andrea Diamantini +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* ============================================================ */ + + +#include "websnap.h" +#include "websnap.moc" + +#include +#include + +#include +#include +#include +#include + + +WebSnap::WebSnap(const KUrl &url, const QString &fileName) + : QObject() + , m_url(url) + , m_image(QImage()) + , m_fileName(fileName) +{ + m_targetSize = QSize(200, 150); + connect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(saveResult(bool))); + QTimer::singleShot(0, this, SLOT(load())); + +} + + +void WebSnap::load() +{ + m_page.mainFrame()->load(m_url); +} + + +void WebSnap::saveResult(bool ok) +{ + // crude error-checking + if (!ok) + { + kDebug() << "Error loading site.."; + emit finished(); + return; + } + + // find proper size, we stick to sensible aspect ratio + QSize size = m_page.mainFrame()->contentsSize(); + size.setHeight(size.width() * m_targetSize.height() / m_targetSize.width()); + + // create the target surface + m_image = QImage( size , QImage::Format_ARGB32_Premultiplied); + m_image.fill(Qt::transparent); + + // render and rescale + QPainter p(&m_image); + m_page.setViewportSize(m_page.mainFrame()->contentsSize()); + m_page.mainFrame()->render(&p); + p.end(); + m_image = m_image.scaled(m_targetSize, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); + + QString path = KStandardDirs::locateLocal("cache", QString("thumbs/") + m_fileName, true); + m_image.save( path ); + + emit finished(); +} diff --git a/src/websnap.h b/src/websnap.h new file mode 100644 index 00000000..cbb786c7 --- /dev/null +++ b/src/websnap.h @@ -0,0 +1,65 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2009 Nokia Corporation +* Copyright (C) 2009 by Andrea Diamantini +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* ============================================================ */ + +#ifndef WEB_SNAP_H +#define WEB_SNAP_H + + +#include + +#include +#include +#include + + +/** + * This class renders a site producing an image based + * on that. + * Heavily based on Graphics-Dojo WebSnap example (thanks!) + */ +class WebSnap : public QObject +{ + Q_OBJECT + +public: + WebSnap(const KUrl &url, const QString &fileName); + +signals: + void finished(); + +private slots: + void load(); + void saveResult(bool ok); + +private: + QWebPage m_page; + KUrl m_url; + QImage m_image; + QString m_fileName; + QSize m_targetSize; +}; + +#endif // WEB_SNAP_H -- cgit v1.2.1