From 18b87086d1a0e472662f3883962b60dbe57e215f Mon Sep 17 00:00:00 2001
From: matgic78 <matgic78@gmail.com>
Date: Sat, 26 Dec 2009 13:00:53 +0100
Subject: First expermimental implementation of a new way of choosing a preview

---
 src/CMakeLists.txt                |   1 +
 src/rekonqpage/newtabpage.cpp     |  57 +++++++++++----
 src/rekonqpage/newtabpage.h       |   2 +
 src/rekonqpage/previewchooser.cpp | 143 ++++++++++++++++++++++++++++++++++++++
 src/rekonqpage/previewchooser.h   |  70 +++++++++++++++++++
 src/websnap.cpp                   |   2 +-
 6 files changed, 262 insertions(+), 13 deletions(-)
 create mode 100644 src/rekonqpage/previewchooser.cpp
 create mode 100644 src/rekonqpage/previewchooser.h

(limited to 'src')

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 2295e9e0..2ad18a02 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -31,6 +31,7 @@ SET( rekonq_KDEINIT_SRCS
     history/historypanel.cpp
     #----------------------------------------
     rekonqpage/newtabpage.cpp
+    rekonqpage/previewchooser.cpp
     #----------------------------------------
     settings/settingsdialog.cpp
     #----------------------------------------
diff --git a/src/rekonqpage/newtabpage.cpp b/src/rekonqpage/newtabpage.cpp
index a587502c..c91af2dc 100644
--- a/src/rekonqpage/newtabpage.cpp
+++ b/src/rekonqpage/newtabpage.cpp
@@ -36,6 +36,7 @@
 #include "application.h"
 #include "mainwindow.h"
 #include "mainview.h"
+#include "previewchooser.h"
 
 // KDE Includes
 #include <KStandardDirs>
@@ -43,6 +44,7 @@
 #include <KDebug>
 #include <KConfig>
 #include <KConfigGroup>
+#include <KDialog>
 
 // Qt Includes
 #include <QFile>
@@ -82,6 +84,9 @@ void NewTabPage::generate(const KUrl &url)
         }
         if(url.directory() == QString("preview/modify"))
         {
+            PreviewChooser *pc = new PreviewChooser(url.fileName().toInt());
+            connect(pc, SIGNAL(urlChoosed(int,KUrl)), SLOT(setPreview(int,KUrl)));
+            pc->show();
             return;
         }
     }
@@ -139,7 +144,8 @@ void NewTabPage::favoritesPage()
         else
             prev = validPreview(i, url, names.at(i));
         
-        prev.setAttribute("id", "preview" + QVariant(i).toString());
+        setupPreview(prev, i);
+        
         m_root.appendInside(prev);
     }
 }
@@ -152,7 +158,9 @@ QWebElement NewTabPage::emptyPreview(int index)
     prev.findFirst(".preview img").setAttribute("src" , QString("file:///") +
                     KIconLoader::global()->iconPath("insert-image", KIconLoader::Desktop));
     prev.findFirst("span").appendInside(i18n("Set a Preview..."));
-    prev.findFirst("a").setAttribute("href", QString("about:/preview/modify/" + QVariant(index).toString()));
+    prev.findFirst("a").setAttribute("href", QString("about:preview/modify/" + QVariant(index).toString()));
+    
+    setupPreview(prev, index);
     hideControls(prev);
     
     return prev;
@@ -166,6 +174,8 @@ QWebElement NewTabPage::loadingPreview(int index, KUrl url)
     prev.findFirst(".preview img").setAttribute("src" , 
                 QString("file:///") + KStandardDirs::locate("appdata", "pics/busywidget.gif"));
     prev.findFirst("span").appendInside(i18n("Loading Preview..."));
+    
+    setupPreview(prev, index);
     showControls(prev);
     
     WebSnap *snap = new WebSnap(url);
@@ -184,19 +194,11 @@ QWebElement NewTabPage::validPreview(int index, KUrl url, QString title)
     prev.findFirst(".preview img").setAttribute("src" , previewPath.toMimeDataString());
     prev.findFirst("a").setAttribute("href", url.toMimeDataString());
     prev.findFirst("span a").setAttribute("href", url.toMimeDataString());
-    prev.findFirst("span").appendInside(checkTitle(title));
-    
-    prev.findFirst(".modify img").setAttribute("src", QString("file:///") +
-                KIconLoader::global()->iconPath("insert-image", KIconLoader::DefaultState));
-    prev.findFirst(".modify").setAttribute("href", QString("about:preview/modify/" + iString ));
-    
-    prev.findFirst(".remove img").setAttribute("src", QString("file:///") +
-                KIconLoader::global()->iconPath("edit-delete", KIconLoader::DefaultState));
-    prev.findFirst(".remove").setAttribute("href", QString("about:preview/remove/" + iString ));
+    prev.findFirst("span").setPlainText(checkTitle(title));
     
+    setupPreview(prev, index);
     showControls(prev);
     
-    
     return prev;
 }
 
@@ -212,6 +214,19 @@ void NewTabPage::showControls(QWebElement e)
     e.findFirst(".modify").setStyleProperty("visibility", "visible");
 }
 
+void NewTabPage::setupPreview(QWebElement e, int index)
+{
+    e.findFirst(".remove img").setAttribute("src", QString("file:///") +
+    KIconLoader::global()->iconPath("edit-delete", KIconLoader::DefaultState));
+    e.findFirst(".modify img").setAttribute("src", QString("file:///") +
+    KIconLoader::global()->iconPath("insert-image", KIconLoader::DefaultState));
+    
+    e.findFirst(".modify").setAttribute("href", QString("about:preview/modify/" + QVariant(index).toString()));
+    e.findFirst(".remove").setAttribute("href", QString("about:preview/remove/" + QVariant(index).toString()));
+    
+    e.setAttribute("id", "preview" + QVariant(index).toString());
+}
+
 
 void NewTabPage::snapFinished()
 {
@@ -255,6 +270,24 @@ void NewTabPage::removePreview(int index)
 }
 
 
+void NewTabPage::setPreview(int index, KUrl url)
+{
+    if(url.isEmpty())
+        return;
+    
+    QWebElement prev = m_root.findFirst("#preview" + QVariant(index).toString());
+    
+    QStringList urls = ReKonfig::previewUrls();
+    urls.replace(index, url.toMimeDataString());
+    ReKonfig::setPreviewUrls(urls);
+    ReKonfig::self()->writeConfig();
+
+    prev.replace(loadingPreview(index, url));
+
+    
+}
+
+
 void NewTabPage::browsingMenu(const KUrl &currentUrl)
 {
     QList<QWebElement> navItems;
diff --git a/src/rekonqpage/newtabpage.h b/src/rekonqpage/newtabpage.h
index 9ac743ea..973028b3 100644
--- a/src/rekonqpage/newtabpage.h
+++ b/src/rekonqpage/newtabpage.h
@@ -58,6 +58,7 @@ public:
 protected slots:
     void snapFinished();
     void removePreview(int index);
+    void setPreview(int index, KUrl url);
      
 protected:  // these are the function to build the new tab page
     void browsingMenu(const KUrl &currentUrl);
@@ -72,6 +73,7 @@ protected:  // these are the function to build the new tab page
     */
     void hideControls(QWebElement e);
     void showControls(QWebElement e);
+    void setupPreview(QWebElement e, int index);
     
     
     void historyPage();
diff --git a/src/rekonqpage/previewchooser.cpp b/src/rekonqpage/previewchooser.cpp
new file mode 100644
index 00000000..a7c86d12
--- /dev/null
+++ b/src/rekonqpage/previewchooser.cpp
@@ -0,0 +1,143 @@
+/* ============================================================
+*
+* This file is a part of the rekonq project
+*
+* 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 <http://www.gnu.org/licenses/>.
+*
+* ============================================================ */
+
+
+// Self Includes
+#include "previewchooser.h"
+
+// Local Includes
+#include "rekonq.h"
+#include <bookmarkstreemodel.h>
+#include <bookmarksproxy.h>
+#include <mainview.h>
+
+// Qt Includes
+#include <QLabel>
+#include <QVBoxLayout>
+
+// KDE Includes
+#include <KLineEdit>
+#include <KLocalizedString>
+
+
+PreviewChooser::PreviewChooser(int previewIndex)
+        : KDialog(0)
+        , m_treeView(new QTreeView)
+        , m_model(new QStandardItemModel)
+        , m_previewIndex(previewIndex)
+{
+    setMinimumSize(350, 100);
+    setWindowTitle(i18n("Set Page to preview"));
+    setModal(true);
+    
+    setButtons(KDialog::Cancel | KDialog::Apply | KDialog::Ok);
+    setDefaultButton(KDialog::Ok);
+    connect(this, SIGNAL(buttonClicked(KDialog::ButtonCode)), this, SLOT(buttonClicked(KDialog::ButtonCode)));
+    
+    m_treeView->setUniformRowHeights(true);
+    m_treeView->setSelectionBehavior(QAbstractItemView::SelectRows);
+    m_treeView->setTextElideMode(Qt::ElideRight);
+    m_treeView->setHeaderHidden(true);
+    m_treeView->setIndentation(0);
+    
+    QWidget *mainWidget = new QWidget(this);
+
+    // add url bar
+    QHBoxLayout *urlLayout = new QHBoxLayout;
+    urlLayout->setContentsMargins(5, 0, 0, 0);
+    QLabel *searchLabel = new QLabel(i18n("adress:"));
+    urlLayout->addWidget(searchLabel);
+    m_line = new KLineEdit;
+    connect(m_line, SIGNAL(textChanged(QString)), SLOT(urlChanged()));
+    urlLayout->addWidget(m_line);
+
+    // setup view
+    QVBoxLayout *vBoxLayout = new QVBoxLayout;
+    vBoxLayout->setContentsMargins(0, 0, 0, 0);
+    vBoxLayout->addLayout(urlLayout);
+    vBoxLayout->addWidget(m_treeView);
+    mainWidget->setLayout(vBoxLayout);
+    setMainWidget(mainWidget);
+    
+    refreshModel();
+    connect(Application::instance()->mainWindow()->mainView(), SIGNAL(tabsChanged()), SLOT(refreshModel()));
+    
+    m_treeView->setModel(m_model);
+    
+    connect(m_treeView, SIGNAL(activated(QModelIndex)), SLOT(setUrl(QModelIndex)));
+}
+
+
+PreviewChooser::~PreviewChooser()
+{
+    delete m_model;
+}
+
+
+
+void PreviewChooser::refreshModel()
+{
+    m_model->clear();
+    MainView *mv = Application::instance()->mainWindow()->mainView();
+    for(int i=0; i < mv->count(); ++i)
+    {
+        WebView *view = qobject_cast<WebView *>(mv->webView(i));
+        
+        if(view->url().scheme() == "about:")
+            continue;
+        
+        QStandardItem *it = new QStandardItem(view->icon(), view->title());
+        it->setData(QVariant(view->url()), Qt::ToolTipRole);
+        m_model->insertRow(i, it);
+    }
+}
+
+void PreviewChooser::setUrl(QModelIndex i)
+{
+    if(!i.data().canConvert(QVariant::String))
+        return;
+    
+    m_line->setText(i.data(Qt::ToolTipRole).toString());
+}
+
+
+void PreviewChooser::buttonClicked(KDialog::ButtonCode code)
+{
+    if(code == KDialog::Apply || code == KDialog::Ok)
+    {
+        emit urlChoosed(m_previewIndex, KUrl(m_line->text()));
+        enableButtonApply(false);
+    }
+    
+    if(code == KDialog::Cancel || code == KDialog::Ok)
+    {
+        close();
+        deleteLater();
+    }
+}
+
+
+void PreviewChooser::urlChanged()
+{
+    enableButtonApply(true);
+}
+
diff --git a/src/rekonqpage/previewchooser.h b/src/rekonqpage/previewchooser.h
new file mode 100644
index 00000000..79766fdc
--- /dev/null
+++ b/src/rekonqpage/previewchooser.h
@@ -0,0 +1,70 @@
+/* ============================================================
+*
+* This file is a part of the rekonq project
+*
+* 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 <http://www.gnu.org/licenses/>.
+*
+* ============================================================ */
+
+
+#ifndef PREVIEWCHOOSER_H
+#define PREVIEWCHOOSER_H
+
+
+// Local Includes
+#include "application.h"
+
+// KDE Includes
+#include <KLineEdit>
+#include <KDialog>
+
+// Qt Includes
+#include <QWidget>
+#include <QTreeView>
+#include <QStandardItemModel>
+
+// Forward Declarations
+class KUrl;
+
+
+class PreviewChooser : public KDialog
+{
+Q_OBJECT
+
+public:
+    explicit PreviewChooser(int previewIndex);
+    virtual ~PreviewChooser();
+
+signals:
+    void urlChoosed(int, const KUrl &);
+    
+public slots:
+    void refreshModel();
+    void setUrl(QModelIndex i);
+    void buttonClicked(KDialog::ButtonCode code);
+    void urlChanged();
+
+private:
+    QTreeView *m_treeView;
+    QStandardItemModel *m_model;
+    
+    KLineEdit *m_line;
+    
+    int m_previewIndex;
+};
+
+#endif // PREVIEWCHOOSER_H
diff --git a/src/websnap.cpp b/src/websnap.cpp
index e5db7c4e..0d72ef3a 100644
--- a/src/websnap.cpp
+++ b/src/websnap.cpp
@@ -167,7 +167,7 @@ void WebSnap::saveResult(bool ok)
     {
         m_image = renderPreview(m_page, WIDTH, HEIGHT);
     }
-    
+    QFile::remove(fileForUrl(m_url).toLocalFile());
     m_image.save(fileForUrl(m_url).toLocalFile());
     kDebug() << "finished";
     
-- 
cgit v1.2.1