ginebra2/AppContentView.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 "AppContentView.h"
       
    20 #include "ChromeWidget.h"
       
    21 #include <QGraphicsWebView>
       
    22 #include <QWebPage>
       
    23 #include <QWebFrame>
       
    24 #ifndef NO_QSTM_GESTURE
       
    25 #include "qstmgestureevent.h"
       
    26 #endif
       
    27 #include <QDebug>
       
    28 
       
    29 
       
    30 
       
    31 namespace GVA 
       
    32 {
       
    33 
       
    34   AppContentView::AppContentView(ChromeWidget * chrome, QObject * parent)
       
    35     : ContentViewDelegate(chrome, parent),
       
    36       m_view(new QGraphicsWebView),
       
    37       m_page(new QWebPage),
       
    38       m_timeLine(0),
       
    39       m_zoomIn(false) 
       
    40   {
       
    41 #ifndef NO_QSTM_GESTURE
       
    42     m_view->installEventFilter(this);
       
    43 #endif  
       
    44     m_page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
       
    45     m_page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
       
    46     //NB: Here's how to set default webview backgound color
       
    47     QPalette viewPalette = m_view->palette();
       
    48     viewPalette.setBrush(QPalette::Base, Qt::white);
       
    49     //viewPalette.setColor(QPalette::Window, Qt::transparent);
       
    50     m_page->setPalette(viewPalette);
       
    51     m_view->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
       
    52     m_view->setPage(m_page);
       
    53     setObjectName("appView");
       
    54     QObject::connect(m_view, SIGNAL(titleChanged(const QString &)), this, SIGNAL(titleChanged(const QString &)));
       
    55     QObject::connect(m_view, SIGNAL(loadStarted()), this, SIGNAL(loadStarted()));
       
    56     QObject::connect(m_view, SIGNAL(loadProgress(int)), this, SIGNAL(loadProgress(int)));
       
    57     QObject::connect(m_view, SIGNAL(loadFinished(bool)), this, SIGNAL(loadFinished(bool)));
       
    58     QObject::connect(m_view, SIGNAL(urlChanged(const QUrl&)), this, SLOT(onUrlChanged(const QUrl&)));
       
    59     QObject::connect(m_page->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SIGNAL(javaScriptWindowObjectCleared()));
       
    60  }
       
    61   
       
    62   AppContentView::~AppContentView()
       
    63   {
       
    64     delete m_timeLine;
       
    65     delete m_page;
       
    66     delete m_view;
       
    67   }
       
    68 
       
    69   void AppContentView::load(const QString& url)
       
    70   {
       
    71     // TO DO: need to filter out non-local URLs.  Should only load local files.
       
    72       
       
    73 	qDebug() << "AppContentView::load: " << url;
       
    74     m_page->mainFrame()->load(QUrl(url));
       
    75     //m_page->setViewportSize(m_page->mainFrame()->contentsSize()); //NB:?
       
    76   }
       
    77 
       
    78   void AppContentView::setHtml(const QString& html)
       
    79   {
       
    80     m_view->setHtml(html);
       
    81     // m_page->setViewportSize(m_page->mainFrame()->contentsSize()); //NB:?
       
    82   }  
       
    83 
       
    84   void AppContentView::triggerAction(const QString & action)
       
    85   {
       
    86     QWebPage::WebAction webAction;
       
    87     if(action=="Stop")
       
    88       webAction = QWebPage::Stop;
       
    89     else if (action=="Back")
       
    90       webAction = QWebPage::Back;
       
    91     else if(action=="Reload")
       
    92       webAction = QWebPage::Reload;
       
    93     else if (action=="Forward")
       
    94       webAction = QWebPage::Forward;
       
    95     else
       
    96       return;
       
    97     m_view->triggerPageAction(webAction);
       
    98   } 
       
    99 
       
   100   void AppContentView::stop()
       
   101   {
       
   102     m_view->stop();
       
   103   }
       
   104   
       
   105   void AppContentView::back()
       
   106   {
       
   107     m_view->back();
       
   108   }
       
   109   
       
   110   void AppContentView::forward()
       
   111   {
       
   112     m_view->forward();
       
   113   }
       
   114   
       
   115   void AppContentView::reload()
       
   116   {
       
   117     m_view->reload();
       
   118   }
       
   119   
       
   120   void AppContentView::zoomBy(qreal delta)
       
   121   {
       
   122     m_page->mainFrame()->setZoomFactor(m_page->mainFrame()->zoomFactor() + delta);
       
   123   }
       
   124 
       
   125   void AppContentView::scrollBy(int deltaX, int deltaY)
       
   126   {
       
   127     m_page->mainFrame()->setScrollPosition(m_page->mainFrame()->scrollPosition() + QPoint(deltaX, deltaY));
       
   128   }
       
   129 
       
   130   int AppContentView::scrollX()
       
   131   {
       
   132     return m_page->mainFrame()->scrollPosition().x();
       
   133   }
       
   134 
       
   135   int AppContentView::scrollY()
       
   136   {
       
   137     return m_page->mainFrame()->scrollPosition().y();
       
   138   }
       
   139 
       
   140   int AppContentView::contentWidth()
       
   141   {
       
   142     return m_page->mainFrame()->contentsSize().width();
       
   143   }
       
   144 
       
   145   int AppContentView::contentHeight()
       
   146   {
       
   147     return m_page->mainFrame()->contentsSize().height();
       
   148   }
       
   149 
       
   150   void AppContentView::onUrlChanged(const QUrl& url)
       
   151   {
       
   152     emit urlChanged(url.toString());
       
   153   }
       
   154 
       
   155 
       
   156   void AppContentView::updateZoom(qreal delta){
       
   157     if(m_zoomIn)
       
   158       zoomBy(0.1);
       
   159     else
       
   160       zoomBy(-0.1);
       
   161   }
       
   162 
       
   163 
       
   164   void AppContentView::zoom(bool in)
       
   165   {
       
   166     m_zoomIn = in;
       
   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   void AppContentView::toggleZoom(){
       
   179     zoom(!m_zoomIn);
       
   180   }
       
   181 
       
   182   void AppContentView::stopZoom() {
       
   183     m_timeLine->stop();
       
   184   }
       
   185 
       
   186   void AppContentView::addJSObjectToWindow(QObject *object) {
       
   187     m_page->mainFrame()->addToJavaScriptWindowObject(object->objectName(), object);
       
   188   }
       
   189 #ifndef NO_QSTM_GESTURE
       
   190   bool AppContentView::eventFilter(QObject* object, QEvent* event)
       
   191   {
       
   192 	  if (event->type() == QEvent::Gesture) {
       
   193 		  QStm_Gesture* gesture = getQStmGesture(event);
       
   194 		  if (gesture) {
       
   195 		      QStm_GestureType gtype = gesture->getGestureStmType();
       
   196 		  	  if (gtype == QStmTapGestureType) {
       
   197 				  QPoint gpos = gesture->position();
       
   198 				  QWidget* topWidget = QApplication::topLevelAt(gpos);
       
   199 				  QPoint pos = topWidget->mapFromGlobal(gpos);
       
   200 				  QWidget* w = topWidget->childAt(pos);
       
   201 				  gesture->sendMouseEvents(w);	   
       
   202 				  return true;
       
   203 			  }  
       
   204 		  }
       
   205 		  return true;
       
   206 	  }
       
   207 	  return false;
       
   208   }
       
   209 #endif
       
   210 } // end of namespace GVA
       
   211