ginebra2/MostVisitedView.cpp
author hgs
Fri, 15 Oct 2010 17:30:59 -0400
changeset 16 3c88a81ff781
permissions -rw-r--r--
201041
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
hgs
parents:
diff changeset
     1
#include "MostVisitedView.h"
hgs
parents:
diff changeset
     2
#include "GridView.h"
hgs
parents:
diff changeset
     3
#include "webpagecontroller.h"
hgs
parents:
diff changeset
     4
#include "mostvisitedpagestore.h"
hgs
parents:
diff changeset
     5
#include "wrtbrowsercontainer.h"
hgs
parents:
diff changeset
     6
#include "ScaleThreePainter.h"
hgs
parents:
diff changeset
     7
#include "qstmgestureevent.h"
hgs
parents:
diff changeset
     8
hgs
parents:
diff changeset
     9
#include <QGraphicsPixmapItem>
hgs
parents:
diff changeset
    10
#include <QGraphicsScene>
hgs
parents:
diff changeset
    11
#include <QGraphicsSceneResizeEvent>
hgs
parents:
diff changeset
    12
#include <QPainter>
hgs
parents:
diff changeset
    13
#include <QStyleOptionGraphicsItem>
hgs
parents:
diff changeset
    14
#include <QGraphicsLinearLayout>
hgs
parents:
diff changeset
    15
#include <QGraphicsSimpleTextItem>
hgs
parents:
diff changeset
    16
hgs
parents:
diff changeset
    17
namespace GVA {
hgs
parents:
diff changeset
    18
hgs
parents:
diff changeset
    19
hgs
parents:
diff changeset
    20
// -----------------------------------------------------------
hgs
parents:
diff changeset
    21
// MostVisitedViewItem
hgs
parents:
diff changeset
    22
// -----------------------------------------------------------
hgs
parents:
diff changeset
    23
hgs
parents:
diff changeset
    24
MostVisitedViewItem::MostVisitedViewItem(::MostVisitedPage *mostVisitedPage, QGraphicsItem *parent)
hgs
parents:
diff changeset
    25
    : QGraphicsWidget(parent),
hgs
parents:
diff changeset
    26
      m_mostVisitedPage(mostVisitedPage)
hgs
parents:
diff changeset
    27
{
hgs
parents:
diff changeset
    28
    grabGesture(QStm_Gesture::assignedType());
hgs
parents:
diff changeset
    29
    installEventFilter(this);
hgs
parents:
diff changeset
    30
}
hgs
parents:
diff changeset
    31
hgs
parents:
diff changeset
    32
void MostVisitedViewItem::activate() {
hgs
parents:
diff changeset
    33
    WRT::WrtBrowserContainer * activePage = WebPageController::getSingleton()->currentPage();
hgs
parents:
diff changeset
    34
hgs
parents:
diff changeset
    35
    if (activePage) {
hgs
parents:
diff changeset
    36
      activePage->mainFrame()->load(m_mostVisitedPage->m_url);
hgs
parents:
diff changeset
    37
    }
hgs
parents:
diff changeset
    38
    close();
hgs
parents:
diff changeset
    39
    emit activated();
hgs
parents:
diff changeset
    40
}
hgs
parents:
diff changeset
    41
hgs
parents:
diff changeset
    42
void MostVisitedViewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
hgs
parents:
diff changeset
    43
    if(m_mostVisitedPage->m_pageThumbnail) {
hgs
parents:
diff changeset
    44
        QPixmap pixmap(QPixmap::fromImage(*m_mostVisitedPage->m_pageThumbnail).scaled(size().toSize()));
hgs
parents:
diff changeset
    45
        painter->drawPixmap(option->exposedRect, pixmap, option->exposedRect);
hgs
parents:
diff changeset
    46
        
hgs
parents:
diff changeset
    47
        QPen pen;
hgs
parents:
diff changeset
    48
        int x, y, w, h;
hgs
parents:
diff changeset
    49
        const int itemBorderWidth = 2;
hgs
parents:
diff changeset
    50
        const QColor itemBorderColor = QColor("black");
hgs
parents:
diff changeset
    51
hgs
parents:
diff changeset
    52
        pen.setWidth(itemBorderWidth);
hgs
parents:
diff changeset
    53
        pen.setBrush(itemBorderColor);
hgs
parents:
diff changeset
    54
        pen.setJoinStyle(Qt::MiterJoin);
hgs
parents:
diff changeset
    55
        
hgs
parents:
diff changeset
    56
        painter->setPen(pen);
hgs
parents:
diff changeset
    57
        option->exposedRect.toRect().getRect(&x, &y, &w, &h);
hgs
parents:
diff changeset
    58
        painter->drawRect(x + itemBorderWidth / 2, y + itemBorderWidth / 2, w - itemBorderWidth, h - itemBorderWidth);
hgs
parents:
diff changeset
    59
    }
hgs
parents:
diff changeset
    60
}
hgs
parents:
diff changeset
    61
hgs
parents:
diff changeset
    62
bool MostVisitedViewItem::eventFilter(QObject* o, QEvent* e)
hgs
parents:
diff changeset
    63
 { 
hgs
parents:
diff changeset
    64
   if (o != this) 
hgs
parents:
diff changeset
    65
      return false;
hgs
parents:
diff changeset
    66
   return QStm_GestureEventFilter::instance()->eventFilter(o, e);
hgs
parents:
diff changeset
    67
 } 
hgs
parents:
diff changeset
    68
hgs
parents:
diff changeset
    69
bool MostVisitedViewItem::event(QEvent* event)
hgs
parents:
diff changeset
    70
 { 
hgs
parents:
diff changeset
    71
   if (event->type() == QEvent::Gesture) {
hgs
parents:
diff changeset
    72
       QStm_Gesture* gesture = getQStmGesture(event);
hgs
parents:
diff changeset
    73
       if (gesture) {  
hgs
parents:
diff changeset
    74
           handleQStmGesture(gesture);
hgs
parents:
diff changeset
    75
           event->accept();
hgs
parents:
diff changeset
    76
           return true;
hgs
parents:
diff changeset
    77
       }
hgs
parents:
diff changeset
    78
   }
hgs
parents:
diff changeset
    79
   return false;
hgs
parents:
diff changeset
    80
 }
hgs
parents:
diff changeset
    81
hgs
parents:
diff changeset
    82
bool MostVisitedViewItem::handleQStmGesture(QStm_Gesture* gesture)
hgs
parents:
diff changeset
    83
 {
hgs
parents:
diff changeset
    84
   QStm_GestureType type = gesture->getGestureStmType();
hgs
parents:
diff changeset
    85
   
hgs
parents:
diff changeset
    86
   if (type == QStmTapGestureType) {
hgs
parents:
diff changeset
    87
       activate();
hgs
parents:
diff changeset
    88
   }
hgs
parents:
diff changeset
    89
   return true;
hgs
parents:
diff changeset
    90
 }
hgs
parents:
diff changeset
    91
hgs
parents:
diff changeset
    92
hgs
parents:
diff changeset
    93
// -----------------------------------------------------------
hgs
parents:
diff changeset
    94
// MostVisitedView
hgs
parents:
diff changeset
    95
// -----------------------------------------------------------
hgs
parents:
diff changeset
    96
hgs
parents:
diff changeset
    97
MostVisitedView::MostVisitedView(const QString &title, QGraphicsItem *parent)
hgs
parents:
diff changeset
    98
    : QGraphicsWidget(parent),
hgs
parents:
diff changeset
    99
      m_layout(new QGraphicsLinearLayout(Qt::Vertical, this)),
hgs
parents:
diff changeset
   100
      m_title(0),
hgs
parents:
diff changeset
   101
      m_gridView(new GridView(this)),
hgs
parents:
diff changeset
   102
      m_backgroundPixmap(0),
hgs
parents:
diff changeset
   103
      m_backgroundDirty(true),
hgs
parents:
diff changeset
   104
      m_titleWrapper(new QGraphicsWidget(this))
hgs
parents:
diff changeset
   105
      
hgs
parents:
diff changeset
   106
{
hgs
parents:
diff changeset
   107
    m_titleWrapper->resize(50, 10);
hgs
parents:
diff changeset
   108
    m_title = new QGraphicsSimpleTextItem("  " + title, m_titleWrapper);
hgs
parents:
diff changeset
   109
    m_title->setBrush(QColor(Qt::white));
hgs
parents:
diff changeset
   110
    QFont textFont = m_title->font();
hgs
parents:
diff changeset
   111
    #ifdef Q_WS_MAEMO_5
hgs
parents:
diff changeset
   112
    textFont.setPointSize(16);
hgs
parents:
diff changeset
   113
    #else
hgs
parents:
diff changeset
   114
    textFont.setPointSize(6);
hgs
parents:
diff changeset
   115
    #endif
hgs
parents:
diff changeset
   116
    textFont.setWeight(QFont::Bold);
hgs
parents:
diff changeset
   117
    m_title->setFont(textFont);
hgs
parents:
diff changeset
   118
    #ifdef Q_WS_MAEMO_5
hgs
parents:
diff changeset
   119
    m_titleWrapper->setContentsMargins(16, 0, 0, 0);
hgs
parents:
diff changeset
   120
    #endif
hgs
parents:
diff changeset
   121
hgs
parents:
diff changeset
   122
    setLayout(m_layout);
hgs
parents:
diff changeset
   123
    m_layout->setSpacing(0);
hgs
parents:
diff changeset
   124
    
hgs
parents:
diff changeset
   125
    #ifdef Q_WS_MAEMO_5
hgs
parents:
diff changeset
   126
    m_layout->setContentsMargins(16, 4, 16, 16);    
hgs
parents:
diff changeset
   127
    m_layout->addItem(m_titleWrapper);
hgs
parents:
diff changeset
   128
    #else
hgs
parents:
diff changeset
   129
    
hgs
parents:
diff changeset
   130
    m_layout->addItem(m_titleWrapper); 
hgs
parents:
diff changeset
   131
    #endif
hgs
parents:
diff changeset
   132
    m_layout->addItem(m_gridView);
hgs
parents:
diff changeset
   133
hgs
parents:
diff changeset
   134
		#ifdef Q_WS_MAEMO_5
hgs
parents:
diff changeset
   135
    m_gridView->setColumnRowCounts(3, 2);    
hgs
parents:
diff changeset
   136
    #endif
hgs
parents:
diff changeset
   137
hgs
parents:
diff changeset
   138
    m_backgroundPainter = new ScaleThreePainter(
hgs
parents:
diff changeset
   139
            ":/mostvisited/most_visited_bac_left.png",
hgs
parents:
diff changeset
   140
            ":/mostvisited/most_visited_bac_middle.png",
hgs
parents:
diff changeset
   141
            ":/mostvisited/most_visited_bac_right.png"
hgs
parents:
diff changeset
   142
            );   
hgs
parents:
diff changeset
   143
}
hgs
parents:
diff changeset
   144
hgs
parents:
diff changeset
   145
MostVisitedView::~MostVisitedView() {
hgs
parents:
diff changeset
   146
    delete m_backgroundPainter;
hgs
parents:
diff changeset
   147
    delete m_backgroundPixmap;
hgs
parents:
diff changeset
   148
}
hgs
parents:
diff changeset
   149
hgs
parents:
diff changeset
   150
void MostVisitedView::update(QString mode) {
hgs
parents:
diff changeset
   151
	  
hgs
parents:
diff changeset
   152
    MostVisitedPageList pageList = MostVisitedPageStoreSingleton::Instance().pageList();
hgs
parents:
diff changeset
   153
    m_gridView->clear();
hgs
parents:
diff changeset
   154
    foreach(MostVisitedPage *page, pageList) {
hgs
parents:
diff changeset
   155
        MostVisitedViewItem *item = new MostVisitedViewItem(page, this);
hgs
parents:
diff changeset
   156
        //qDebug() << __PRETTY_FUNCTION__ << " url=" << page->m_url << page->m_pageThumbnail;
hgs
parents:
diff changeset
   157
        m_gridView->addItem(item);
hgs
parents:
diff changeset
   158
        safe_connect(item, SIGNAL(activated()), this, SLOT(onItemActivated()));
hgs
parents:
diff changeset
   159
    }
hgs
parents:
diff changeset
   160
    
hgs
parents:
diff changeset
   161
    m_displayMode = mode;       
hgs
parents:
diff changeset
   162
    m_gridView->rebuildLayout();
hgs
parents:
diff changeset
   163
}
hgs
parents:
diff changeset
   164
hgs
parents:
diff changeset
   165
void MostVisitedView::resizeEvent(QGraphicsSceneResizeEvent * event) {
hgs
parents:
diff changeset
   166
	  
hgs
parents:
diff changeset
   167
    m_gridView->resize(event->newSize());  
hgs
parents:
diff changeset
   168
    // Force background update on next paint().
hgs
parents:
diff changeset
   169
    m_backgroundDirty = true;
hgs
parents:
diff changeset
   170
}
hgs
parents:
diff changeset
   171
hgs
parents:
diff changeset
   172
void MostVisitedView::closeEvent(QCloseEvent *event) {
hgs
parents:
diff changeset
   173
    Q_UNUSED(event)
hgs
parents:
diff changeset
   174
    emit closeComplete();
hgs
parents:
diff changeset
   175
}
hgs
parents:
diff changeset
   176
hgs
parents:
diff changeset
   177
void MostVisitedView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
hgs
parents:
diff changeset
   178
    // Update the background pixmap if needed.
hgs
parents:
diff changeset
   179
    
hgs
parents:
diff changeset
   180
    #ifndef Q_WS_MAEMO_5
hgs
parents:
diff changeset
   181
    if (m_displayMode == "portrait")
hgs
parents:
diff changeset
   182
    {
hgs
parents:
diff changeset
   183
    	m_gridView->setColumnRowCounts(2, 3);
hgs
parents:
diff changeset
   184
    	m_titleWrapper->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
hgs
parents:
diff changeset
   185
    	m_layout->setContentsMargins(16, 4, 16, 16);  
hgs
parents:
diff changeset
   186
    	setMinimumSize(346, 523);    
hgs
parents:
diff changeset
   187
    	setMaximumSize(346, 523);    
hgs
parents:
diff changeset
   188
    }
hgs
parents:
diff changeset
   189
    else
hgs
parents:
diff changeset
   190
    {
hgs
parents:
diff changeset
   191
    	m_gridView->setColumnRowCounts(3, 2);
hgs
parents:
diff changeset
   192
    	m_titleWrapper->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
hgs
parents:
diff changeset
   193
    	m_layout->setContentsMargins(36, 4, 0, 0);   
hgs
parents:
diff changeset
   194
    	setMinimumSize(610, 260);
hgs
parents:
diff changeset
   195
    	setMaximumSize(610, 260);
hgs
parents:
diff changeset
   196
    }
hgs
parents:
diff changeset
   197
    m_gridView->rebuildLayout();
hgs
parents:
diff changeset
   198
    #endif
hgs
parents:
diff changeset
   199
    
hgs
parents:
diff changeset
   200
    #ifndef Q_WS_MAEMO_5   
hgs
parents:
diff changeset
   201
    updateBackgroundPixmap(geometry().size().toSize(), widget);    	
hgs
parents:
diff changeset
   202
    m_backgroundPainter->unloadPixmaps();
hgs
parents:
diff changeset
   203
    #else
hgs
parents:
diff changeset
   204
    if(m_backgroundDirty) {    	
hgs
parents:
diff changeset
   205
     	updateBackgroundPixmap(geometry().size().toSize(), widget);    	
hgs
parents:
diff changeset
   206
      m_backgroundPainter->unloadPixmaps();
hgs
parents:
diff changeset
   207
    }
hgs
parents:
diff changeset
   208
    #endif
hgs
parents:
diff changeset
   209
hgs
parents:
diff changeset
   210
    // Paint the background.
hgs
parents:
diff changeset
   211
    painter->drawPixmap(option->exposedRect, *m_backgroundPixmap, option->exposedRect);
hgs
parents:
diff changeset
   212
}
hgs
parents:
diff changeset
   213
hgs
parents:
diff changeset
   214
void MostVisitedView::updateBackgroundPixmap(const QSize &size, QWidget* widget) {
hgs
parents:
diff changeset
   215
    delete m_backgroundPixmap;
hgs
parents:
diff changeset
   216
    m_backgroundPixmap = new QPixmap(size);
hgs
parents:
diff changeset
   217
    m_backgroundPixmap->fill(QColor(0xff, 0xff, 0xff, 0));
hgs
parents:
diff changeset
   218
    QPainter painter(m_backgroundPixmap);
hgs
parents:
diff changeset
   219
    m_backgroundPainter->paint(&painter, QRect(0, 0, size.width(), size.height()), widget);
hgs
parents:
diff changeset
   220
    m_backgroundDirty = false;
hgs
parents:
diff changeset
   221
}
hgs
parents:
diff changeset
   222
hgs
parents:
diff changeset
   223
void MostVisitedView::onItemActivated() {
hgs
parents:
diff changeset
   224
    close();
hgs
parents:
diff changeset
   225
    emit activated();
hgs
parents:
diff changeset
   226
}
hgs
parents:
diff changeset
   227
hgs
parents:
diff changeset
   228
}  // namespace
hgs
parents:
diff changeset
   229