ginebra2/mostvisitedpageview.cpp
changeset 5 0f2326c2a325
parent 0 1450b09d0cfd
child 6 1c3b8676e58c
equal deleted inserted replaced
1:b0dd75e285d2 5:0f2326c2a325
     1 /*
     1 /*
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 *
     5 * under the terms of "Eclipse Public License v1.0"
     5 * This program is free software: you can redistribute it and/or modify
     6 * which accompanies this distribution, and is available
     6 * it under the terms of the GNU Lesser General Public License as published by
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * the Free Software Foundation, version 2.1 of the License.
     8 *
     8 *
     9 * Initial Contributors:
     9 * This program is distributed in the hope that it will be useful,
    10 * Nokia Corporation - initial contribution.
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11 *
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12 * Contributors:
    12 * GNU Lesser General Public License for more details.
    13 *
    13 *
    14 * Description: 
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not,
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
    15 *
    19 *
    16 */
    20 */
    17 
       
    18 
    21 
    19 #include <QtGui>
    22 #include <QtGui>
    20 
    23 
    21 #include "mostvisitedpagestore.h"
    24 #include "mostvisitedpagestore.h"
    22 #include "linearflowsnippet.h"
    25 #include "linearflowsnippet.h"
    23 #include "mostvisitedpageview.h"
    26 #include "mostvisitedpageview.h"
    24 #include "webpagecontroller.h"
    27 #include "webpagecontroller.h"
    25 #include "BookmarksManager.h"
    28 #include "BookmarksManager.h"
       
    29 #include "webpagedata.h"
    26 
    30 
    27 const int KLinearSnippetHeight = 120;
    31 const int KLinearSnippetHeight = 120;
    28 
    32 
    29 namespace GVA {
    33 namespace GVA {
    30 MostVisitedPagesWidget::MostVisitedPagesWidget(ChromeSnippet* snippet,QGraphicsWidget* parent) 
    34 MostVisitedPagesWidget::MostVisitedPagesWidget(ChromeSnippet* snippet,QGraphicsWidget* parent)
    31                         : m_snippet(snippet)
    35                         : ChromeItem(snippet, parent)
    32                         , QGraphicsWidget(parent)
       
    33                         , m_parent(parent)
    36                         , m_parent(parent)
    34                         , m_flowInterface(0)
    37                         , m_flowInterface(0)
    35                         , m_hideOnClose(true)
    38                         , m_hideOnClose(true)
    36 {
    39 {
    37     setFlags(QGraphicsItem::ItemDoesntPropagateOpacityToChildren);
    40     setFlags(QGraphicsItem::ItemDoesntPropagateOpacityToChildren);
    38     setOpacity(0.5);
    41     setOpacity(0.5);
    39     m_mostVisitedPageStore = new MostVisitedPageStore();
    42     m_mostVisitedPageStore = new MostVisitedPageStore();
    40     WebPageController* pageController = WebPageController::getSingleton();
    43     WebPageController* pageController = WebPageController::getSingleton();
    41     bool ret = connect(pageController, SIGNAL(loadFinished(const bool)), this, SLOT(onLoadFinished(const bool)));
    44     connect(pageController, SIGNAL(loadFinished(const bool)), this, SLOT(onLoadFinished(const bool)));
       
    45     connect(WRT::BookmarksManager::getSingleton(),SIGNAL(historyCleared()),this,SLOT(clearMVStore()));
    42 }
    46 }
    43 
    47 
    44 MostVisitedPagesWidget::~MostVisitedPagesWidget()
    48 MostVisitedPagesWidget::~MostVisitedPagesWidget()
    45 {
    49 {
    46     if (m_flowInterface)
    50     if (m_flowInterface)
    47         m_flowInterface->deleteLater();
    51         m_flowInterface->deleteLater();
    48     if (m_mostVisitedPageStore)
    52     if (m_mostVisitedPageStore)
    49         delete m_mostVisitedPageStore;
    53         delete m_mostVisitedPageStore;
    50     WebPageController* pageController = WebPageController::getSingleton();
    54     WebPageController* pageController = WebPageController::getSingleton();
    51     disconnect(pageController, SIGNAL(loadFinished()), this, SLOT(onLoadFinished()));
    55     disconnect(pageController, SIGNAL(loadFinished(const bool)), this, SLOT(onLoadFinished(const bool)));
       
    56     disconnect(WRT::BookmarksManager::getSingleton(),SIGNAL(historyCleared()),this,SLOT(clearMVStore()));
    52 }
    57 }
    53 
    58 
    54 
    59 
    55 void MostVisitedPagesWidget::open()
    60 void MostVisitedPagesWidget::open()
    56 {
    61 {
    58         delete m_flowInterface;
    63         delete m_flowInterface;
    59 
    64 
    60     m_flowInterface = new GVA::LinearFlowSnippet(this);
    65     m_flowInterface = new GVA::LinearFlowSnippet(this);
    61     m_flowInterface->setZValue(m_parent->zValue() + 1);
    66     m_flowInterface->setZValue(m_parent->zValue() + 1);
    62     m_flowInterface->resize(QSize(m_parent->size().width(), KLinearSnippetHeight));
    67     m_flowInterface->resize(QSize(m_parent->size().width(), KLinearSnippetHeight));
    63     connect(m_flowInterface, SIGNAL(mouseEvent(QEvent::Type)),this->m_snippet,SIGNAL(snippetMouseEvent(QEvent::Type)));
    68     connect(m_flowInterface, SIGNAL(mouseEvent(QEvent::Type)),this,SIGNAL(mouseEvent(QEvent::Type)));
    64     
    69 
    65     //Initialize the page selection index
    70     //Initialize the page selection index
    66     m_selectIndex = -1;
    71     m_selectIndex = -1;
    67     
    72 
    68     QString displayMode;
    73     QString displayMode;
    69 
    74 
    70     if (m_parent->size().width() > m_parent->size().height()) {
    75     if (m_parent->size().width() > m_parent->size().height()) {
    71         displayMode = "Landscape";
    76         displayMode = "Landscape";
    72        
    77 
    73     } else {
    78     } else {
    74         displayMode = "Portrait";
    79         displayMode = "Portrait";
    75     }
    80     }
    76     m_flowInterface->init(displayMode,"Most Visited Pages");
    81     m_flowInterface->init(displayMode,qtTrId("txt_browser_most_visited_title"));
    77 
    82 
    78     MostVisitedPageList mvPageList = m_mostVisitedPageStore->pageList();
    83     MostVisitedPageList mvPageList = m_mostVisitedPageStore->pageList();
    79 
    84 
    80     for (int i = 0; i < mvPageList.size(); i++) {
    85     for (int i = 0; i < mvPageList.size(); i++) {
    81         QImage *pageThumbnail = mvPageList[i]->m_pageThumbnail;
    86         QImage *pageThumbnail = mvPageList[i]->m_pageThumbnail;
    88         m_flowInterface->addSlide(*pageThumbnail, url.host());
    93         m_flowInterface->addSlide(*pageThumbnail, url.host());
    89         if (removeTempThumbnail) {
    94         if (removeTempThumbnail) {
    90             delete pageThumbnail;
    95             delete pageThumbnail;
    91         }
    96         }
    92     }
    97     }
    93    
    98 
    94     setCenterIndex(displayMode);
    99     setCenterIndex(displayMode);
    95     m_flowInterface->prepareStartAnimation();
   100     m_flowInterface->prepareStartAnimation();
    96     m_flowInterface->runStartAnimation();
   101     m_flowInterface->runStartAnimation();
    97     m_flowInterface->installEventFilter(this);
   102     m_flowInterface->installEventFilter(this);
    98     connect(m_flowInterface, SIGNAL(ok(int)), this, SLOT(okTriggered(int)));
   103     connect(m_flowInterface, SIGNAL(ok(int)), this, SLOT(okTriggered(int)));
   120     m_flowInterface->resize(QSize(m_parent->size().width(), KLinearSnippetHeight));
   125     m_flowInterface->resize(QSize(m_parent->size().width(), KLinearSnippetHeight));
   121 }
   126 }
   122 
   127 
   123 void MostVisitedPagesWidget::displayModeChanged(QString& newMode)
   128 void MostVisitedPagesWidget::displayModeChanged(QString& newMode)
   124 {
   129 {
   125     m_flowInterface->displayModeChanged(newMode); 
   130     m_flowInterface->displayModeChanged(newMode);
   126 }
   131 }
   127 
   132 
   128 void MostVisitedPagesWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
   133 void MostVisitedPagesWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
   129 {
   134 {
   130     painter->fillRect(0, 0, size().width(), size().height(), QColor(107, 109, 107));
   135     painter->fillRect(0, 0, size().width(), size().height(), QColor(107, 109, 107));
   144 
   149 
   145 void MostVisitedPagesWidget::okTriggered(int index)
   150 void MostVisitedPagesWidget::okTriggered(int index)
   146 {
   151 {
   147     Q_ASSERT(m_flowInterface);
   152     Q_ASSERT(m_flowInterface);
   148     m_selectIndex = index;
   153     m_selectIndex = index;
   149     close();    
   154     close();
   150 }
   155 }
   151 
   156 
   152 void MostVisitedPagesWidget::closeAnimationCompleted()
   157 void MostVisitedPagesWidget::closeAnimationCompleted()
   153 {
   158 {
   154     if (!m_flowInterface)
   159     if (!m_flowInterface)
   155         return;
   160         return;
   156 
   161 
   157     QGraphicsWidget::hide();
   162     QGraphicsWidget::hide();
   158     disconnect(m_flowInterface, SIGNAL(ok(int)), this, SLOT(okTriggered(int)));
   163     disconnect(m_flowInterface, SIGNAL(ok(int)), this, SLOT(okTriggered(int)));
   159     disconnect(m_flowInterface, SIGNAL(endAnimationCplt()), this, SLOT(closeAnimationCompleted()));
   164     disconnect(m_flowInterface, SIGNAL(endAnimationCplt()), this, SLOT(closeAnimationCompleted()));
   160     disconnect(m_flowInterface, SIGNAL(mouseEvent(QEvent::Type)),this->m_snippet,SIGNAL(snippetMouseEvent(QEvent::Type)));
   165     disconnect(m_flowInterface, SIGNAL(mouseEvent(QEvent::Type)),this,SIGNAL(mouseEvent(QEvent::Type)));
   161 
   166 
   162     m_flowInterface->removeEventFilter(this);
   167     m_flowInterface->removeEventFilter(this);
   163     m_flowInterface->deleteLater();
   168     m_flowInterface->deleteLater();
   164     m_flowInterface = NULL;
   169     m_flowInterface = NULL;
   165     
   170 
   166     if (m_selectIndex != -1)
   171     if (m_selectIndex != -1)
   167         static_cast<ChromeWidget*>(m_parent)->loadUrlToCurrentPage(m_mostVisitedPageStore->pageAt(m_selectIndex)->pageUrl());
   172         static_cast<ChromeWidget*>(m_parent)->loadUrlToCurrentPage(m_mostVisitedPageStore->pageAt(m_selectIndex)->pageUrl());
   168 
   173 
   169     if (m_snippet->isVisible() && m_hideOnClose)
   174     if (m_snippet->isVisible() && m_hideOnClose)
   170         m_snippet->ChromeSnippet::toggleVisibility();
   175         m_snippet->ChromeSnippet::toggleVisibility();
   177 {
   182 {
   178     qreal toolBarHeight;
   183     qreal toolBarHeight;
   179 
   184 
   180     ChromeSnippet* visibleSnippet= static_cast<ChromeWidget*>(m_parent)->getSnippet("WebViewToolbarId");
   185     ChromeSnippet* visibleSnippet= static_cast<ChromeWidget*>(m_parent)->getSnippet("WebViewToolbarId");
   181     if (visibleSnippet)
   186     if (visibleSnippet)
   182         toolBarHeight = visibleSnippet->widget()->geometry().height(); 
   187         toolBarHeight = visibleSnippet->widget()->geometry().height();
   183 
   188 
   184     resize(m_parent->size().toSize());
   189     resize(m_parent->size().toSize());
   185     updatePos(QPointF(0, 0), toolBarHeight);
   190     updatePos(QPointF(0, 0), toolBarHeight);
   186 }
   191 }
   187 
   192 
   188  void MostVisitedPagesWidget::updateMVStore(QWebPage *page)
   193  void MostVisitedPagesWidget::updateMVStore(WRT::WrtBrowserContainer *page)
   189   {
   194   {
   190     Q_ASSERT(page);
   195     Q_ASSERT(page);
   191     Q_ASSERT(!page->mainFrame()->url().isEmpty());
   196     Q_ASSERT(!page->mainFrame()->url().isEmpty());
   192 
   197 
   193     QImage* pageThumbnail = 0;
       
   194     QUrl pageUrl = page->mainFrame()->url();
   198     QUrl pageUrl = page->mainFrame()->url();
   195     int pageRank = 0;
   199     int pageRank = 0;
   196     
   200     QImage* pageThumbnail = NULL;
   197     //check if page exits in store along with its thumbnail
   201     //check if page exits in store along with its thumbnail
   198     if (!m_mostVisitedPageStore->contains(pageUrl.toString(), true)) {
   202     if (!m_mostVisitedPageStore->contains(pageUrl.toString(), true)) {
   199         QSize thumbnailSize(200, 200);
   203         qreal scale = 200.0 / page->viewportSize().width();
   200         pageThumbnail = new QImage(thumbnailSize.width(), thumbnailSize.height(), QImage::Format_RGB32); 
   204         QImage img = page->pageThumbnail(scale, scale);
   201         QPainter painter(pageThumbnail);
   205         pageThumbnail = new QImage(img);
   202         qreal webcoreScale = page->mainFrame()->zoomFactor();
       
   203         painter.scale(1.f / webcoreScale, 1.f / webcoreScale);
       
   204         painter.fillRect(0, 0, size().width(), size().height(), QColor(255, 255, 255));
       
   205         page->mainFrame()->render(&painter, QWebFrame::AllLayers, QRegion(0, 0, thumbnailSize.width(), thumbnailSize.height()));
       
   206     }
   206     }
   207 
   207 
   208     //if it is a new page to the store, get its rank from history
   208     //if it is a new page to the store, get its rank from history
   209     //FIX ME : need to optimize this code
   209     //FIX ME : need to optimize this code
   210     pageRank = WRT::BookmarksManager::getSingleton()->getPageRank(pageUrl.toString());
   210     pageRank = WRT::BookmarksManager::getSingleton()->getPageRank(pageUrl.toString());
   217         WRT::WrtBrowserContainer * activePage = WebPageController::getSingleton()->currentPage();
   217         WRT::WrtBrowserContainer * activePage = WebPageController::getSingleton()->currentPage();
   218         updateMVStore(activePage);
   218         updateMVStore(activePage);
   219      }
   219      }
   220  }
   220  }
   221 
   221 
       
   222 void MostVisitedPagesWidget::clearMVStore()
       
   223 {
       
   224     m_mostVisitedPageStore->clearMostVisitedPageStore();
       
   225 }
       
   226 
   222 } // endof namespace GVA
   227 } // endof namespace GVA
   223 
   228