ginebra2/ScrollZoomWidget.cpp
branchGCC_SURGE
changeset 8 2e16851ffecd
parent 2 bf4420e9fa4d
parent 6 1c3b8676e58c
equal deleted inserted replaced
2:bf4420e9fa4d 8:2e16851ffecd
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <QtGui>
       
    20 #include <QTimeLine>
       
    21 
       
    22 #include "ScrollZoomWidget.h"
       
    23 #include "GWebContentView.h"
       
    24 #include "ChromeSnippet.h"
       
    25 
       
    26 #include <QDebug>
       
    27 
       
    28 namespace GVA {
       
    29 
       
    30   ScrollZoomWidget::ScrollZoomWidget(ChromeSnippet* snippet, QGraphicsItem* parent)
       
    31     : NativeChromeItem(snippet, parent),
       
    32       m_zoomIn(true),
       
    33       m_scrolling(false),
       
    34       m_xScale(1),
       
    35       m_yScale(1),
       
    36       m_deltaX(0),
       
    37       m_deltaY(0),
       
    38       m_timeLine(0),
       
    39       m_webView(0),
       
    40       m_effect(0)
       
    41   {
       
    42     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemSendsGeometryChanges);
       
    43     setOpacity(0.50);
       
    44     // m_effect = new QGraphicsDropShadowEffect();
       
    45     //static_cast<QGraphicsDropShadowEffect*>(m_effect)->setOffset(4.0,4.0);
       
    46     //static_cast<QGraphicsDropShadowEffect*>(m_effect)->setBlurRadius(2.0);
       
    47     //setGraphicsEffect(m_effect);
       
    48     m_buttonImage = QImage(":/chrome/demochrome/Scroll.png");
       
    49   }
       
    50 
       
    51   ScrollZoomWidget:: ~ScrollZoomWidget()
       
    52   {
       
    53     delete m_timeLine;
       
    54   }
       
    55 
       
    56  
       
    57   QVariant ScrollZoomWidget::itemChange(GraphicsItemChange change, const QVariant & value)
       
    58   {
       
    59     qreal deltaX = 0;
       
    60     qreal deltaY = 0;
       
    61     qreal scrollY = 0;
       
    62 
       
    63     if(m_webView && (change == ItemPositionChange)) {
       
    64       QPointF newPos = value.toPointF();
       
    65       if((m_centerX != 0) && (m_centerY != 0)){
       
    66 	deltaX = newPos.x() - m_centerX;
       
    67 	deltaY = newPos.y() - m_centerY;
       
    68       }
       
    69       if(m_scrolling)
       
    70 	scrollY = deltaY*m_yScale/2;
       
    71       if(m_webView->scrollY() == 0){
       
    72 	qreal slide = m_snippet->chrome()->slideView(scrollY);	  
       
    73         scrollY-=slide;
       
    74       }
       
    75       m_webView->scrollBy(-deltaX*m_yScale/2, -scrollY);
       
    76     }
       
    77     return QGraphicsItem::itemChange(change, value);
       
    78   }
       
    79  
       
    80   void ScrollZoomWidget::onLoadFinished(bool ok)
       
    81   {
       
    82     if(m_webView->contentWidth() > parentWidget()->size().width())
       
    83       m_xScale = m_webView->contentWidth()/parentWidget()->size().width();
       
    84     if(m_webView->contentHeight() > parentWidget()->size().height())
       
    85       m_yScale = m_webView->contentHeight()/parentWidget()->size().height();
       
    86   }
       
    87 
       
    88    void ScrollZoomWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* opt, QWidget* widget)
       
    89   {
       
    90     painter->save();
       
    91     if(m_scrolling) {
       
    92       setOpacity(0.0);
       
    93       //painter->setBrush(Qt::transparent);
       
    94       //painter->scale(2.0, 2.0);
       
    95       //painter->setPen(QPen(Qt::transparent, 4.0));
       
    96       //painter->drawEllipse(QRectF(0,0, geometry().width(), geometry().height()));
       
    97     }
       
    98     else {
       
    99       setOpacity(0.60);
       
   100       //painter->setBrush(Qt::blue);
       
   101       //painter->setPen(QPen(Qt::blue, 4.0));     
       
   102       //painter->drawEllipse(QRectF(0,0, geometry().width(), geometry().height()));
       
   103     }
       
   104     painter->drawImage(QPointF(0,0),m_buttonImage);
       
   105     painter->restore();
       
   106   }
       
   107 
       
   108   void ScrollZoomWidget::mousePressEvent(QGraphicsSceneMouseEvent * ev)
       
   109   {
       
   110 
       
   111     if(!m_webView){
       
   112       m_webView  = static_cast<GWebContentView*> (m_snippet->chrome()->getView("WebView"));
       
   113       if(m_webView) 
       
   114 	connect(m_webView, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
       
   115       else
       
   116 	return;
       
   117     }
       
   118     m_centerX = pos().x();
       
   119     m_centerY = pos().y();
       
   120     onLoadFinished(true);
       
   121     m_scrolling = true;
       
   122     //zoom();
       
   123     //scroll();
       
   124     QGraphicsWidget::mouseReleaseEvent(ev);
       
   125   }
       
   126 
       
   127   void ScrollZoomWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent * ev)
       
   128   {
       
   129     m_scrolling = false;
       
   130     setPos(m_centerX, m_centerY);
       
   131     setOpacity(0.6);
       
   132     update();
       
   133     //m_timeLine->stop();
       
   134     QGraphicsWidget::mouseReleaseEvent(ev);
       
   135   }
       
   136 
       
   137   void ScrollZoomWidget::updateZoom(qreal delta){
       
   138     if(!m_webView) {
       
   139       m_webView  = static_cast<GWebContentView*> (m_snippet->chrome()->getView("WebView"));
       
   140       if(m_webView) 
       
   141 	connect(m_webView, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
       
   142     }
       
   143     if(!m_webView)
       
   144       return;
       
   145     if(m_zoomIn)
       
   146       m_webView->zoomBy(0.1);
       
   147     else
       
   148       m_webView->zoomBy(-0.1);
       
   149   }
       
   150 
       
   151 
       
   152   void ScrollZoomWidget::updateScroll(qreal delta){
       
   153     if(!m_webView) {
       
   154       m_webView  = static_cast<GWebContentView*> (m_snippet->chrome()->getView("WebView"));
       
   155       if(m_webView) 
       
   156 	connect(m_webView, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
       
   157     }
       
   158     if(!m_webView)
       
   159       return;
       
   160     m_webView->scrollBy(m_deltaX*2,m_deltaY*2);
       
   161   }
       
   162 
       
   163  
       
   164   void ScrollZoomWidget::zoom()
       
   165   {
       
   166     m_zoomIn = !m_zoomIn;
       
   167     if(!m_timeLine) {
       
   168       m_timeLine = new QTimeLine();
       
   169       connect(m_timeLine, SIGNAL(valueChanged(qreal)),
       
   170 	      this, SLOT(updateZoom(qreal)));
       
   171     }
       
   172     else {
       
   173       m_timeLine->stop();
       
   174     }
       
   175     m_timeLine->start();
       
   176   }
       
   177  
       
   178 
       
   179   void ScrollZoomWidget::scroll()
       
   180   { 
       
   181     m_zoomIn = !m_zoomIn;
       
   182     if(!m_timeLine) {
       
   183       m_timeLine = new QTimeLine();
       
   184       m_timeLine->setLoopCount(0);
       
   185       connect(m_timeLine, SIGNAL(valueChanged(qreal)),
       
   186 	      this, SLOT(updateScroll(qreal)));
       
   187     }
       
   188     else {
       
   189       m_timeLine->stop();
       
   190     }
       
   191     m_timeLine->start();
       
   192   }
       
   193 
       
   194 } // end of namespace GVA
       
   195 
       
   196