ginebra2/ChromeWidget.cpp
changeset 3 0954f5dd2cd0
parent 0 1450b09d0cfd
child 9 b39122337a00
equal deleted inserted replaced
1:b0dd75e285d2 3:0954f5dd2cd0
     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
       
     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 *
     4 *
     9 * Initial Contributors:
     5 * This program is free software: you can redistribute it and/or modify
    10 * Nokia Corporation - initial contribution.
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
    11 *
     8 *
    12 * Contributors:
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    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 <QWebElement>
    22 #include <QWebElement>
    20 #include <QWebPage>
    23 #include <QWebPage>
    21 #include <QWebFrame>
    24 #include <QWebFrame>
    22 #include <QList>
    25 #include <QList>
    23 #include <QKeyEvent>
    26 #include <QKeyEvent>
    24 #include <QDebug>
    27 #include <QDebug>
    25 #include <QGraphicsSceneContextMenuEvent>
    28 #include <QDesktopServices>
    26 
    29 
       
    30 #include "bedrockprovisioning.h"
    27 #include "ChromeWidgetJSObject.h"
    31 #include "ChromeWidgetJSObject.h"
       
    32 #include "ChromeLayout.h"
    28 #include "ChromeRenderer.h"
    33 #include "ChromeRenderer.h"
    29 #include "ChromeDOM.h"
    34 #include "ChromeDOM.h"
    30 #include "Snippets.h"
    35 #include "Snippets.h"
       
    36 #include "ChromeEffect.h"
    31 #include "ChromeSnippet.h"
    37 #include "ChromeSnippet.h"
    32 #include "ChromeWidget.h"
    38 #include "ChromeWidget.h"
    33 #include "WebChromeContainerSnippet.h"
    39 #include "WebChromeContainerSnippet.h"
    34 #include "Application.h"
    40 #include "Application.h"
    35 #include "AppContentView.h"
       
    36 #include "BlueChromeSnippet.h"
       
    37 #include "GreenChromeSnippet.h"
       
    38 #include "ViewController.h"
    41 #include "ViewController.h"
    39 #include "ViewStack.h"
    42 #include "ViewStack.h"
    40 //#include "CollapsingWidget.h"
    43 //#include "CollapsingWidget.h"
    41 #include "SlidingWidget.h"
    44 #include "SlidingWidget.h"
    42 #include "ProgressSnippet.h"
       
    43 #include "GWebPage.h"
    45 #include "GWebPage.h"
    44 #include "webpagecontroller.h"
    46 #include "webpagecontroller.h"
    45 //#include "ViewStack.h"
    47 //#include "ViewStack.h"
    46 #include "BookmarksManager.h"
    48 #include "BookmarksManager.h"
    47 #include "ScriptObjects.h"
    49 #include "ScriptObjects.h"
    48 #include "LocaleDelegate.h"
    50 #include "LocaleDelegate.h"
    49 #include "DeviceDelegate.h"
    51 #include "DeviceDelegate.h"
    50 
    52 #include "NetworkDelegate.h"
    51 #ifdef USE_DOWNLOAD_MANAGER
    53 #include "ObjectCharm.h"
       
    54 #include "bedrockprovisioning.h"
       
    55 #include "Utilities.h"
       
    56 #include "PopupWebChromeItem.h"
       
    57 #ifdef QT_MOBILITY_SYSINFO
       
    58 #include "SystemDeviceImpl.h"
       
    59 #include "SystemNetworkImpl.h"
       
    60 #endif
       
    61 
    52 #include "Downloads.h"
    62 #include "Downloads.h"
    53 #endif
       
    54 
    63 
    55 #include "wrtbrowsercontainer.h"
    64 #include "wrtbrowsercontainer.h"
    56 #include "webpagecontroller.h"
    65 #include "webpagecontroller.h"
    57 
    66 
       
    67 #include "GAlternateFileChooser.h"
       
    68 
    58 namespace GVA {
    69 namespace GVA {
    59 
    70 
    60 // -----------------------------
    71 // -----------------------------
    61 
    72 
    62   ChromeWidget::ChromeWidget(QGraphicsItem * parent, Qt::WindowFlags wFlags) 
    73   ChromeWidget::ChromeWidget(QGraphicsItem * parent, Qt::WindowFlags wFlags)
    63     : QGraphicsWidget(parent, wFlags),
    74     : QObject(0),
    64       m_renderer(0),
    75       m_renderer(0),
    65       m_dom(0),
    76       m_dom(0),
    66       m_viewController(new ViewController()),
    77       m_viewController(new ViewController()),
    67       m_topBar(0),
       
    68       m_bottomBar(0),
       
    69       m_leftBar(0),
       
    70       m_rightBar(0),
       
    71       m_aspect(portrait),
       
    72       m_jsObject(new ChromeWidgetJSObject(0, this)),
    78       m_jsObject(new ChromeWidgetJSObject(0, this)),
    73       m_localeDelegate(new LocaleDelegate(this)),
    79       m_localeDelegate(new LocaleDelegate(this)),
    74       m_deviceDelegate(new DeviceDelegate()),
    80       m_downloads(new Downloads())
    75       m_downloads(0)
    81   {
    76   {
    82     m_layout = new ChromeLayout(parent, wFlags);
    77     m_scene = new QGraphicsScene();
    83     QObject::connect(m_layout, SIGNAL(resizing(QSizeF)), this, SLOT(onResize(QSizeF)));
    78     //Keep key events not otherwise consumed from going to the scene
    84     QObject::connect(m_layout, SIGNAL(aspectChanged(int)), this, SLOT(onAspectChanged(int)));
    79     //installEventFilter(this);
    85 
    80     //This is the root of the scene hierarchy
    86     DeviceImpl *deviceImpl = new DEVICEIMPL();
    81     m_scene->addItem(this);
    87     NetworkImpl *networkImpl = new NETWORKIMPL();
    82     m_layout = new QGraphicsAnchorLayout();
    88     m_deviceDelegate = new DeviceDelegate(deviceImpl);
    83     m_layout->setContentsMargins(0,0,0,0);
    89     m_networkDelegate = new NetworkDelegate(networkImpl);
    84     m_layout->setSpacing(0);
    90 
    85     setLayout(m_layout);
    91     BEDROCK_PROVISIONING::BedrockProvisioning *provisioning = BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning();
    86     addAnchors();
    92     ChromeEffect::disabledColor.setNamedColor(provisioning->valueAsString("DisabledColor", "#FFFFFF"));
    87     m_viewPort = new SlidingWidget(this);
    93     ChromeEffect::disabledOpacity = static_cast<qreal>(provisioning->valueAsString("DisabledOpacity", "0.65").toFloat());
    88     //m_viewBar = new CollapsingWidget(m_viewPort);
    94 
    89 #ifndef __gva_no_chrome__
    95 #ifndef __gva_no_chrome__
    90     m_snippets = new Snippets(this, this);
    96     m_snippets = new Snippets(this, this);
    91 #endif
    97 #endif
    92     m_layout->addAnchor(m_viewPort, Qt::AnchorTop, m_topBar, Qt::AnchorBottom);
       
    93     m_layout->addAnchor(m_viewPort, Qt::AnchorBottom, m_bottomBar, Qt::AnchorTop);
       
    94     m_layout->addAnchor(m_viewPort, Qt::AnchorLeft, m_leftBar, Qt::AnchorRight);
       
    95     m_layout->addAnchor(m_viewPort, Qt::AnchorRight, m_rightBar, Qt::AnchorLeft);
       
    96 #ifndef __gva_no_chrome__
    98 #ifndef __gva_no_chrome__
    97     m_page =  static_cast<QWebPage *>(new GVA::WebPageWrapper(this, "Chrome Javascript error"));
    99     m_page =  static_cast<QWebPage *>(new GVA::WebPageWrapper(this, "Chrome Javascript error"));
    98     m_page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
   100     m_page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
    99     m_page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
   101     m_page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
   100 #endif
   102 #endif
   102 
   104 
   103     connect(m_viewController, SIGNAL(currentViewChanged()), this, SLOT(onCurrentViewChanged()));
   105     connect(m_viewController, SIGNAL(currentViewChanged()), this, SLOT(onCurrentViewChanged()));
   104 #ifndef __gva_no_chrome__
   106 #ifndef __gva_no_chrome__
   105 
   107 
   106     m_jsObject->setObjectName("chrome");
   108     m_jsObject->setObjectName("chrome");
       
   109 
   107     // Pass some signals from this object to the Javascript object.
   110     // Pass some signals from this object to the Javascript object.
   108     QObject::connect(this, SIGNAL(chromeComplete()), m_jsObject, SIGNAL(chromeComplete()));
   111     QObject::connect(this, SIGNAL(chromeComplete()), m_jsObject, SIGNAL(chromeComplete()));
   109     QObject::connect(this, SIGNAL(aspectChanged(int)), m_jsObject, SIGNAL(aspectChanged(int)));
   112     QObject::connect(this, SIGNAL(aspectChanged(int)), m_jsObject, SIGNAL(aspectChanged(int)));
   110     QObject::connect(this, SIGNAL(prepareForGeometryChange()), m_jsObject, SIGNAL(prepareForGeometryChange()));
   113     QObject::connect(this, SIGNAL(prepareForGeometryChange()), m_jsObject, SIGNAL(prepareForGeometryChange()));
   111     QObject::connect(this, SIGNAL(symbianCarriageReturn()), m_jsObject, SIGNAL(symbianCarriageReturn()));
   114     QObject::connect(this, SIGNAL(symbianCarriageReturn()), m_jsObject, SIGNAL(symbianCarriageReturn()));
       
   115     QObject::connect(this, SIGNAL(popupShown(const QString &)), m_jsObject, SIGNAL(popupShown(const QString &)));
       
   116     QObject::connect(this, SIGNAL(popupHidden(const QString &)), m_jsObject, SIGNAL(popupHidden(const QString &)));
   112 
   117 
   113     //addJSObjectToEngine(this);
   118     //addJSObjectToEngine(this);
       
   119 
   114     m_app = new GinebraApplication();
   120     m_app = new GinebraApplication();
       
   121 
   115     //addJSObjectToEngine(m_app);
   122     //addJSObjectToEngine(m_app);
   116 
   123 
   117 #ifdef USE_DOWNLOAD_MANAGER
       
   118     m_downloads = new Downloads();
       
   119     QObject::connect(
   124     QObject::connect(
   120             WebPageController::getSingleton(), SIGNAL(pageCreated(WRT::WrtBrowserContainer*)),
   125             WebPageController::getSingleton(), SIGNAL(pageCreated(WRT::WrtBrowserContainer*)),
   121             this, SLOT(pageCreated(WRT::WrtBrowserContainer*)));
   126             this, SLOT(pageCreated(WRT::WrtBrowserContainer*)));
   122 #endif
   127 
   123     
       
   124     QObject::connect(m_page, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
   128     QObject::connect(m_page, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
   125     QObject::connect(m_page, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
   129     QObject::connect(m_page, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
   126     QObject::connect(m_page->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(exportJSObjects()));
   130     QObject::connect(m_page->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(exportJSObjects()));
   127 
   131 
   128 #endif
   132 #endif
   129     
   133 
   130     ViewStack::getSingleton()->setViewController(m_viewController);
   134     ViewStack * vs = ViewStack::getSingleton();
       
   135     vs->setViewController(m_viewController);
       
   136     vs->setChromeWidget(this);
       
   137 
   131     // TO DO: need a better home for this.
   138     // TO DO: need a better home for this.
   132     qMetaTypeId<QObjectList>();
   139     qMetaTypeId<QObjectList>();
   133     qRegisterMetaType<QObjectList>("QObjectList");
   140     qRegisterMetaType<QObjectList>("QObjectList");
   134   }
   141 	
   135 
   142 	//for QA Automation test tool purpose
   136   void ChromeWidget::loadUrlToCurrentPage(const QUrl & url)
   143 #if !defined(QT_NO_LIBRARY)
   137   {
   144     QLibrary testLib("qttestability");
   138     WRT::WrtBrowserContainer * activePage = WebPageController::getSingleton()->currentPage();
   145     if(testLib.load()){
   139 
   146         typedef void (*TasInitialize)(void);
   140     if (activePage) {
   147         TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
   141       activePage->mainFrame()->load(url.toString());
   148 #ifdef Q_OS_SYMBIAN
   142     }
   149         //not found so use ordinal
   143   }
   150         if(!initFunction){
   144 
   151             initFunction = (TasInitialize)testLib.resolve("1");
   145   void ChromeWidget::pageCreated(WRT::WrtBrowserContainer * page)
   152         }
   146   {
   153 #endif
   147 #ifdef USE_DOWNLOAD_MANAGER
   154         if(initFunction){
   148     if (m_downloads) {
   155             initFunction();
   149         m_downloads->handlePage(page);
   156         }
   150     }
   157     }
   151 #else
       
   152     Q_UNUSED(page)
       
   153 #endif
   158 #endif
   154   }
   159   }
   155 
   160 
   156   ChromeWidget::~ChromeWidget()
   161   ChromeWidget::~ChromeWidget()
   157   {
   162   {
   158     // clearChrome(); // crashes on exit
       
   159     delete m_viewController;
   163     delete m_viewController;
   160     delete m_bottomBar;
   164     delete m_jsObject;
   161     delete m_topBar;
   165     delete m_layout;
   162     delete m_leftBar;
       
   163     delete m_rightBar;
       
   164     delete m_viewPort;
       
   165     // delete m_viewLayout; // crashes on exit
       
   166     delete m_renderer;
   166     delete m_renderer;
   167     delete m_dom;
   167     delete m_dom;
   168     delete m_page;
   168     delete m_page;
   169     // delete m_scene;  // crashes on exit
       
   170     delete m_snippets;
   169     delete m_snippets;
   171     // delete m_layout; // crashes on exit
       
   172     delete m_localeDelegate;
   170     delete m_localeDelegate;
   173     delete m_deviceDelegate;
   171     delete m_deviceDelegate;
   174 #ifdef USE_DOWNLOAD_MANAGER
   172     delete m_networkDelegate;
       
   173     delete m_app;
   175     delete m_downloads;
   174     delete m_downloads;
   176 #endif
   175   }
   177   }
   176 
   178  
   177   //Handle resizing signal from layout
   179 
   178 
   180   //Eat key events not otherwise consumed.
   179   void ChromeWidget::onResize(QSizeF size)
   181   /*bool ChromeWidget::eventFilter(QObject * obj, QEvent * ev)
       
   182   {
       
   183    if(ev->type() == QEvent::KeyPress){
       
   184       return true;
       
   185     }
       
   186     return QObject::eventFilter(obj,ev);
       
   187  
       
   188   }*/
       
   189 
       
   190   void ChromeWidget::resizeEvent(QGraphicsSceneResizeEvent *ev)
       
   191   {
   180   {
   192 #ifndef __gva_no_chrome__
   181 #ifndef __gva_no_chrome__
   193     if(m_dom && m_renderer) {
   182     if (m_dom && m_renderer) {
   194       emit prepareForGeometryChange();
   183       emit prepareForGeometryChange();
   195       m_renderer->setGeometry(QRectF(-1200,-1200, ev->newSize().width(), m_dom->height()));
   184       m_renderer->resize(size);
   196     }
   185     }
   197     int aspect = m_aspect;
   186 #endif
   198     m_aspect = (ev->newSize().width() > ev->newSize().height())?landscape:portrait;
   187   }
   199     if(m_aspect != aspect) {
   188 
   200       QString mode = (m_aspect == landscape ? "Landscape" : "Portrait");
   189   //Handle aspectChanged signal from layout
   201       ControllableViewBase* cview = m_viewController->currentView();
   190 
   202       if (cview)
   191   void ChromeWidget::onAspectChanged(int aspect)
   203         cview->displayModeChanged(mode);
   192   {
   204       emit aspectChanged(m_aspect);
   193     QString mode = (aspect == landscape ? "Landscape" : "Portrait");
   205     }
   194     ControllableViewBase* cview = m_viewController->currentView();
   206 #endif
   195     if (cview)
   207     QGraphicsWidget::resizeEvent(ev);    
   196       cview->displayModeChanged(mode);
   208     
   197     emit aspectChanged(aspect);
   209   }
   198   }
   210 
   199 
   211   /*
   200   void ChromeWidget::updateChromeLayout() {
   212   //For layout debugging
   201     m_renderer->updateChromeLayout();
   213   void ChromeWidget::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget* widget){
   202   }
   214     painter->fillRect(geometry(), Qt::red);
   203 
   215   }
   204 
   216   */
   205   void ChromeWidget::loadUrlToCurrentPage(const QUrl & url)
       
   206   {
       
   207     WRT::WrtBrowserContainer * activePage = WebPageController::getSingleton()->currentPage();
       
   208 
       
   209     if (activePage) {
       
   210       activePage->mainFrame()->load(url.toString());
       
   211     }
       
   212   }
       
   213 
       
   214   void ChromeWidget::pageCreated(WRT::WrtBrowserContainer * page)
       
   215   {
       
   216 #ifdef Q_OS_SYMBIAN
       
   217     QString path = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
       
   218     GAlternateFileChooser * chooser = new GAlternateFileChooser(path);
       
   219     page->setFileChooser(chooser); // chooser is now owned by page
       
   220 #endif
       
   221     m_downloads->handlePage(page);
       
   222   }
   217 
   223 
   218   void ChromeWidget::setChromeBaseDirectory(const QString dir) {
   224   void ChromeWidget::setChromeBaseDirectory(const QString dir) {
   219     m_baseDirectory = dir;
   225     m_baseDirectory = dir;
       
   226     if(m_baseDirectory.left(2) == ":/") // resource path have to be converted to resource url
       
   227         m_baseDirectory = "qrc:///" + m_baseDirectory.mid(2);
   220   }
   228   }
   221 
   229 
   222   void ChromeWidget::setChromeFile(const QString filePath)
   230   void ChromeWidget::setChromeFile(const QString filePath)
   223   {
   231   {
   224 #ifndef __gva_no_chrome__
   232 #ifndef __gva_no_chrome__
   225     qDebug() << "ChromeWidget::setChromeFile: " << m_baseDirectory << filePath;
       
   226     m_page->mainFrame()->load(QUrl(m_baseDirectory + filePath));
   233     m_page->mainFrame()->load(QUrl(m_baseDirectory + filePath));
   227 #else
   234 #else
   228     Q_UNUSED(filePath)
   235     Q_UNUSED(filePath)
   229 #endif
   236 #endif
   230   }  
   237   }
   231 
   238 
   232   void ChromeWidget::reloadChrome()
   239   void ChromeWidget::reloadChrome()
   233   {
   240   {
   234     clearChrome();
   241     clearChrome();
   235     m_page->triggerAction(QWebPage::Reload);
   242     m_page->triggerAction(QWebPage::Reload);
   236   }
   243   }
   237 
   244 
       
   245   // TODO: needed?
   238   void ChromeWidget::addViewToLayout(ControllableViewBase * controllableView){
   246   void ChromeWidget::addViewToLayout(ControllableViewBase * controllableView){
   239     //qDebug() << "ChromeWidget::addViewToLayout: " << controllableView->widget();
   247     m_layout->addView(controllableView);
   240     if(controllableView->widget()) {
       
   241       m_viewPort->setWindow(controllableView->widget());
       
   242       controllableView->widget()->setFocusPolicy(Qt::ClickFocus); //NB: Is this needed? Does it break anything?
       
   243       controllableView->widget()->setZValue(-1.0); //Make sure we are behind any center anchored snippets
       
   244     }
       
   245   }
   248   }
   246 
   249 
   247   void ChromeWidget::addView(ControllableViewBase * controllableView) {
   250   void ChromeWidget::addView(ControllableViewBase * controllableView) {
   248     //qDebug() << "ChromeWidget::addView: " << controllableView->widget();
   251     //qDebug() << "ChromeWidget::addView: " << controllableView->widget();
   249     m_viewController->addView(controllableView);
   252     m_viewController->addView(controllableView);
   250 
   253   }
   251     /*
   254   
   252     if(controllableView->widget()) {
       
   253       addViewToLayout(controllableView);
       
   254     }
       
   255     else {
       
   256       qWarning() << "ChromeWidget::addView: view has no widget. " << controllableView;
       
   257       connect(controllableView, SIGNAL(instantiated(ControllableViewBase *)), this, SLOT(onViewInstantiated(ControllableViewBase *)));
       
   258     }
       
   259 //    m_jsObjects.append(controllableView); // Add to list of exported objects
       
   260 #ifndef __gva_no_chrome__
       
   261     addJSObjectToWindow(controllableView); // Export to DOM
       
   262 #endif
       
   263     //addJSObjectToEngine(viewDelegate); // Export to script engine
       
   264     */
       
   265   }
       
   266 
       
   267   void ChromeWidget::anchorToView(ChromeSnippet* snippet, const QString& where)
       
   268   {
       
   269     Q_UNUSED(where)
       
   270       //m_viewBar->addItem(snippet);
       
   271     m_viewPort->attachItem(snippet->widget());
       
   272   }
       
   273  
       
   274   void ChromeWidget::detachFromView(ChromeSnippet* snippet, const QString& where)
       
   275   {
       
   276     Q_UNUSED(where)
       
   277       //  m_viewBar->removeItem(snippet);
       
   278     m_viewPort->detachItem(snippet->widget());
       
   279   }
       
   280 
       
   281   void ChromeWidget::anchorTogether(ChromeSnippet* first, const QString& secondId, qreal x, qreal y)
   255   void ChromeWidget::anchorTogether(ChromeSnippet* first, const QString& secondId, qreal x, qreal y)
   282   { 
   256   {
   283     ChromeSnippet* second = getSnippet(secondId);
   257     ChromeSnippet* second = getSnippet(secondId);
   284     if (second){
   258     if (second){
   285       //qDebug() << "Anchoring: " << first->objectName() << " to: " << second->objectName();
   259       m_layout->anchorTogether(first, second, x, y);
   286       first->widget()->setParentItem(second->widget());
   260     }
   287       first->widget()->setPos(x,y);
   261     else {
   288     }
   262       qDebug() << "Chrome::anchorTogether: error, not found: " << secondId;
   289   }
   263     }
   290 
       
   291   void ChromeWidget::unAnchor(ChromeSnippet* snippet)
       
   292   {
       
   293     snippet->widget()->setParentItem(0);
       
   294     snippet->widget()->setParentItem(this);
       
   295   }
       
   296 
       
   297   qreal ChromeWidget::slideView(qreal delta)
       
   298   {
       
   299     //return m_viewBar->collapse(delta);
       
   300     return m_viewPort->slide(delta);
       
   301   }
   264   }
   302 
   265 
   303   ControllableViewBase * ChromeWidget::getView(const QString& view)
   266   ControllableViewBase * ChromeWidget::getView(const QString& view)
   304   {
   267   {
   305     return m_viewController->view(view);
   268     return m_viewController->view(view);
   306   }  
   269   }
   307 
   270 
   308   void ChromeWidget::showView(const QString &name) {
   271   void ChromeWidget::showView(const QString &name) {
   309     qDebug() << "ChromeWidget::showView: " << name;
       
   310     m_viewController->showView(name);
   272     m_viewController->showView(name);
   311   }
   273   }
   312 
   274 
   313   void ChromeWidget::onCurrentViewChanged() {
   275   void ChromeWidget::onCurrentViewChanged() {
   314     addViewToLayout(m_viewController->currentView());
   276     m_layout->addView(m_viewController->currentView());
   315   }
   277   }
   316 
   278 
   317   // Clean up all existing snippets;
   279   // Clean up all existing snippets;
   318   
   280 
   319   void ChromeWidget::clearChrome() 
   281   void ChromeWidget::clearChrome()
   320   {
   282   {
   321     m_snippets->clear();
   283     m_snippets->clear();
   322   }
   284   }
   323 
   285 
   324   void ChromeWidget::loadStarted() // slot
   286   void ChromeWidget::loadStarted() // slot
   326     clearChrome();
   288     clearChrome();
   327   }
   289   }
   328 
   290 
   329   void ChromeWidget::loadFinished(bool ok)  // slot
   291   void ChromeWidget::loadFinished(bool ok)  // slot
   330   {
   292   {
   331     //qDebug() << "ChromeWidget::loadFinished";
   293     if (!ok) {
   332     if(!ok)
       
   333       {
       
   334       qDebug() << "ChromeWidget::loadFinished: error";
       
   335       return;
   294       return;
   336       }
   295     }
   337     //NB: do we really need to instantiate a new renderer?
   296     if (!m_renderer)
   338     if(m_renderer)
   297       m_renderer = new ChromeRenderer(m_page, this);
   339       delete m_renderer;
   298     m_renderer->resize(m_layout->size());
   340     // qDebug() << "Instantiate renderer";
   299     if (m_dom)
   341     m_renderer = new ChromeRenderer(m_page, this);
       
   342     // qDebug() << "Resize the renderer 1";
       
   343     m_renderer->resize(size());
       
   344     m_renderer->setPos(-1200, -1200);
       
   345     m_renderer->setZValue(-3);
       
   346 #ifdef Q_OS_SYMBIAN
       
   347     connect(m_renderer, SIGNAL(symbianCarriageReturn()), this, SIGNAL(symbianCarriageReturn()));
       
   348 #endif
       
   349     if(m_dom) 
       
   350       delete m_dom; // NB: This may need some further investigation
   300       delete m_dom; // NB: This may need some further investigation
   351     m_dom = new ChromeDOM(m_page, this);
   301     m_dom = new ChromeDOM(m_page, this);
   352     getInitialSnippets();
   302     getInitialSnippets();
   353     //Set the final renderer size to match the chrome
   303     m_renderer->resize(QSizeF(m_layout->size().width(), m_dom->height()));
   354     m_renderer->resize(size().width(), m_dom->height());
       
   355     //qDebug() << m_dom->getCacheableScript();
   304     //qDebug() << m_dom->getCacheableScript();
   356 
       
   357     // Let internal objects know that the chrome is complete.
   305     // Let internal objects know that the chrome is complete.
   358     emit internalChromeComplete();
   306     emit internalChromeComplete();
   359     // Now let the javascript world know that it is complete.
   307     // Now let the javascript world know that it is complete.
   360     emit chromeComplete();
   308     emit chromeComplete();
       
   309     // connect ViewStack to creatingPage signal
       
   310     connect( WebPageController::getSingleton(), SIGNAL(creatingPage(WRT::WrtBrowserContainer*)),
       
   311              ViewStack::getSingleton(), SLOT(creatingPage(WRT::WrtBrowserContainer*)));
   361   }
   312   }
   362 
   313 
   363   void ChromeWidget::chromeInitialized()
   314   void ChromeWidget::chromeInitialized()
   364   {
   315   {
   365     //NB: Don't want to implement this, but just in case
   316     //NB: Don't want to implement this, but just in case
   378     addJSObjectToPage(WebPageController::getSingleton(), page);
   329     addJSObjectToPage(WebPageController::getSingleton(), page);
   379     addJSObjectToPage(WRT::BookmarksManager::getSingleton(), page);
   330     addJSObjectToPage(WRT::BookmarksManager::getSingleton(), page);
   380     addJSObjectToPage(ViewStack::getSingleton(), page);
   331     addJSObjectToPage(ViewStack::getSingleton(), page);
   381     addJSObjectToPage(m_localeDelegate, page);
   332     addJSObjectToPage(m_localeDelegate, page);
   382     addJSObjectToPage(m_deviceDelegate, page);
   333     addJSObjectToPage(m_deviceDelegate, page);
       
   334     addJSObjectToPage(m_networkDelegate, page);
   383     // Dynamically added objects
   335     // Dynamically added objects
   384     foreach(QObject * jsObj, m_jsObjects) {
   336     //foreach(QObject * jsObj, m_jsObjects) {
   385       addJSObjectToPage(jsObj, page);
   337     //  addJSObjectToPage(jsObj, page);
   386     }
   338     // }
   387 #ifdef USE_DOWNLOAD_MANAGER
   339     addJSObjectToPage(m_downloads, page);
   388     if (m_downloads != 0) {
       
   389         addJSObjectToPage(m_downloads, page);
       
   390     }
       
   391 #endif
       
   392   }
   340   }
   393 
   341 
   394   void ChromeWidget::getInitialSnippets()
   342   void ChromeWidget::getInitialSnippets()
   395   {
   343   {
   396     //TODO: get the list of containers form m_dom (via new method to be added).
   344     //TODO: get the list of containers form m_dom (via new method to be added).
   397     QList <QWebElement> initialSnippets = m_dom->getInitialElements();
   345     QList <QWebElement> initialSnippets = m_dom->getInitialElements();
   398     foreach(QWebElement element, initialSnippets) {
   346     foreach(QWebElement element, initialSnippets) {
   399       ChromeSnippet * s = getSnippet(element.attribute("id"));
   347       ChromeSnippet * s = getSnippet(element.attribute("id"));
   400       if(s->initiallyVisible())
   348       if (s->initiallyVisible())
   401         s->setVisible(true);
   349         s->setVisible(true);
   402     }
   350     }
   403   }
   351   }
   404 
   352 
   405   void ChromeWidget::addAnchors(){
   353   //TODO: check new logic
   406     if(!m_bottomBar){
   354  
   407       m_bottomBar = new QGraphicsWidget(this);
       
   408       m_bottomBar->setPreferredHeight(0);
       
   409       m_bottomBar->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
       
   410       m_layout->addAnchor(m_bottomBar, Qt::AnchorBottom, m_layout, Qt::AnchorBottom);
       
   411       m_layout->addAnchors(m_bottomBar, m_layout, Qt::Horizontal);
       
   412     }
       
   413     if(!m_topBar){
       
   414       m_topBar = new QGraphicsWidget(this);
       
   415       m_topBar->setPreferredHeight(0);
       
   416       m_topBar->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
       
   417       m_layout->addAnchor(m_topBar, Qt::AnchorTop, m_layout, Qt::AnchorTop);
       
   418       m_layout->addAnchors(m_topBar, m_layout, Qt::Horizontal);
       
   419     }
       
   420     if(!m_leftBar){
       
   421       m_leftBar = new QGraphicsWidget(this);
       
   422       m_leftBar->setPreferredWidth(0);
       
   423       m_leftBar->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred));
       
   424       m_layout->addAnchor(m_leftBar, Qt::AnchorLeft, m_layout, Qt::AnchorLeft);
       
   425       m_layout->addAnchors(m_leftBar, m_layout, Qt::Vertical);
       
   426     }
       
   427     if(!m_rightBar){
       
   428       m_rightBar = new QGraphicsWidget(this);
       
   429       m_rightBar->setPreferredWidth(0);
       
   430       m_rightBar->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred));
       
   431       m_layout->addAnchor(m_rightBar, Qt::AnchorRight, m_layout, Qt::AnchorRight);
       
   432       m_layout->addAnchors(m_rightBar, m_layout, Qt::Vertical);
       
   433     }
       
   434   }
       
   435   
       
   436   //Check to see if snippet has an anchor and lay it out accordingly.
       
   437   //Otherwise snippets determine their own positions from html
       
   438 
       
   439   void ChromeWidget::anchorSnippet(ChromeSnippet * snippet)
       
   440   {
       
   441     if(snippet->anchor() == anchorBottom){
       
   442       if(snippet->hidesContent()){
       
   443 	m_layout->addAnchor(snippet->widget(), Qt::AnchorBottom, m_bottomBar, Qt::AnchorTop);
       
   444 	m_layout->addAnchor(snippet->widget(), Qt::AnchorHorizontalCenter, m_bottomBar, Qt::AnchorHorizontalCenter);
       
   445       } else {
       
   446 	snippet->widget()->setParentItem(m_bottomBar);
       
   447 	snippet->widget()->setY(-snippet->anchorOffset());
       
   448       }
       
   449     }
       
   450     else if (snippet->anchor() == anchorTop){
       
   451       if(snippet->hidesContent()){
       
   452 	m_layout->addAnchor(snippet->widget(),Qt::AnchorTop, m_topBar, Qt::AnchorBottom);
       
   453 	m_layout->addAnchor(snippet->widget(), Qt::AnchorHorizontalCenter, m_topBar, Qt::AnchorHorizontalCenter);
       
   454       }
       
   455       else {
       
   456 	snippet->widget()->setParentItem(m_topBar);
       
   457 	snippet->widget()->setY(snippet->anchorOffset());
       
   458       }
       
   459     }
       
   460     else if (snippet->anchor() == anchorLeft){
       
   461       if(snippet->hidesContent())
       
   462 	m_layout->addAnchor(snippet->widget(),Qt::AnchorLeft, m_leftBar, Qt::AnchorRight);
       
   463       else {
       
   464 	snippet->widget()->setParentItem(m_leftBar);
       
   465 	snippet->widget()->setX(snippet->anchorOffset());
       
   466       }
       
   467     }
       
   468     else if (snippet->anchor() == anchorRight){
       
   469       if(snippet->hidesContent())
       
   470 	m_layout->addAnchor(snippet->widget(),Qt::AnchorRight, m_rightBar, Qt::AnchorLeft);
       
   471       else {
       
   472 	snippet->widget()->setParentItem(m_rightBar);
       
   473 	snippet->widget()->setX(-snippet->anchorOffset());
       
   474       }
       
   475     }
       
   476     else if (snippet->anchor() == anchorCenter) {
       
   477       snippet->widget()->setParentItem(m_viewPort);
       
   478       snippet->widget()->setZValue(0.0);
       
   479       QGraphicsAnchorLayout * vl = static_cast<QGraphicsAnchorLayout*>(m_viewPort->layout());
       
   480       vl->addAnchor(snippet->widget(), Qt::AnchorVerticalCenter, vl, Qt::AnchorVerticalCenter);
       
   481       vl->addAnchor(snippet->widget(), Qt::AnchorHorizontalCenter, vl, Qt::AnchorHorizontalCenter);
       
   482     }
       
   483     else if (snippet->anchor() == anchorTopLeft){
       
   484       qDebug() << "Setting top left anchor";
       
   485       m_layout->addCornerAnchors(snippet->widget(), Qt::TopLeftCorner, m_layout, Qt::TopLeftCorner);
       
   486     }
       
   487     else if (snippet->anchor() == anchorTopRight)
       
   488       m_layout->addCornerAnchors(snippet->widget(), Qt::TopRightCorner, m_layout, Qt::TopRightCorner);
       
   489     else if (snippet->anchor() == anchorBottomLeft)
       
   490       m_layout->addCornerAnchors(snippet->widget(), Qt::BottomLeftCorner, m_layout, Qt::BottomLeftCorner);
       
   491     else if (snippet->anchor() == anchorBottomRight)
       
   492       m_layout->addCornerAnchors(snippet->widget(), Qt::BottomRightCorner, m_layout, Qt::BottomRightCorner);
       
   493   }
       
   494 
       
   495   void ChromeWidget:: addSnippet(ChromeSnippet * snippet, const QString & docElementId )
   355   void ChromeWidget:: addSnippet(ChromeSnippet * snippet, const QString & docElementId )
   496   {
   356   {
   497     m_snippets->addSnippet(snippet, docElementId);
   357     m_snippets->addSnippet(snippet, docElementId);
   498     snippet->widget()->setParentItem(this);
   358     ChromeSnippet * container = 0;
   499     
   359     if (!snippet->parentId().isNull()){
   500     if(!snippet->parentId().isNull()){
   360       container = getSnippet(snippet->parentId());
   501       ChromeSnippet * container = getSnippet(snippet->parentId());
   361     }
   502       if(container)
   362     m_layout->addSnippet(snippet, container);
   503 	container->addChild(snippet);
       
   504     }
       
   505     
       
   506     anchorSnippet(snippet);
       
   507   }
   363   }
   508 
   364 
   509   //NB: This really shouldn't be necessary: anchor bars should be implemented as an expanding
   365   //NB: This really shouldn't be necessary: anchor bars should be implemented as an expanding
   510   //widget class !!!!!!
   366   //widget class !!!!!!
   511 
   367 
   512   void ChromeWidget::adjustAnchorOffset(ChromeSnippet * snippet, qreal delta)
   368   void ChromeWidget::adjustAnchorOffset(ChromeSnippet * snippet, qreal delta)
   513   {
   369   {
   514     if(snippet->anchor() == anchorBottom)
   370     m_layout->adjustAnchorOffset(snippet, delta);
   515       m_bottomBar->setPreferredHeight(m_bottomBar->preferredHeight() + delta);
       
   516     else if(snippet->anchor() == anchorTop)
       
   517       m_topBar->setPreferredHeight(m_topBar->preferredHeight() + delta);
       
   518     else if(snippet->anchor() == anchorLeft){
       
   519       m_leftBar->setPreferredWidth(m_leftBar->preferredWidth() + delta);
       
   520     }
       
   521     else if(snippet->anchor() == anchorRight){
       
   522       m_rightBar->setPreferredWidth(m_rightBar->preferredWidth() + delta);
       
   523     }
       
   524   }
       
   525 
       
   526   // Call after a snippet changes to visible state
       
   527   // (NB: does not check to see whether snippet has been
       
   528   // correctly added to chrome (by calling addSnippet).
       
   529   // At a minimum should assert that the anchor "bars"
       
   530   // are not null.)
       
   531 
       
   532   void ChromeWidget::snippetShown(ChromeSnippet * snippet)
       
   533   {
       
   534     if(snippet->hidesContent())
       
   535       return;
       
   536     if(snippet->anchor() == anchorBottom)
       
   537       m_bottomBar->setPreferredHeight(m_bottomBar->preferredHeight() + snippet->widget()->preferredHeight());
       
   538     else if(snippet->anchor() == anchorTop)
       
   539       m_topBar->setPreferredHeight(m_topBar->preferredHeight() + snippet->widget()->preferredHeight());
       
   540     else if(snippet->anchor() == anchorLeft){
       
   541       m_leftBar->setPreferredWidth(m_leftBar->preferredWidth() + snippet->widget()->preferredWidth());
       
   542     }
       
   543     else if(snippet->anchor() == anchorRight){
       
   544       m_rightBar->setPreferredWidth(m_rightBar->preferredWidth() + snippet->widget()->preferredWidth());
       
   545     }
       
   546   }
       
   547 
       
   548   // Call before a snippet changes to invisible state
       
   549   // (NB: does not check to see whether snippet has been
       
   550   // correctly added to chrome (by calling addSnippet).
       
   551   // At a minimum should assert that the anchor "bars"
       
   552   // are not null.)
       
   553 
       
   554   void ChromeWidget::snippetHiding(ChromeSnippet * snippet)
       
   555   {
       
   556     if(snippet->hidesContent())
       
   557       return;
       
   558     if(snippet->anchor() == anchorBottom)
       
   559       m_bottomBar->setPreferredHeight(m_bottomBar->preferredHeight() - snippet->widget()->preferredHeight());
       
   560     else if(snippet->anchor() == anchorTop)
       
   561       m_topBar->setPreferredHeight(m_topBar->preferredHeight() - snippet->widget()->preferredHeight());
       
   562     else if(snippet->anchor() == anchorLeft){
       
   563       m_leftBar->setPreferredWidth(m_leftBar->preferredWidth() - snippet->widget()->preferredWidth());
       
   564     }
       
   565     else if(snippet->anchor() == anchorRight){
       
   566       m_rightBar->setPreferredWidth(m_rightBar->preferredWidth() - snippet->widget()->preferredWidth());
       
   567     }
       
   568   }
   371   }
   569 
   372 
   570   ChromeSnippet *ChromeWidget::getSnippet(const QString & docElementId, QGraphicsItem * parent) {
   373   ChromeSnippet *ChromeWidget::getSnippet(const QString & docElementId, QGraphicsItem * parent) {
   571  
       
   572     ChromeSnippet *result = m_snippets->getSnippet(docElementId);
   374     ChromeSnippet *result = m_snippets->getSnippet(docElementId);
   573     if(!result){
   375     if (!result){
   574       result = m_dom->getSnippet(docElementId, parent);
   376       result = m_dom->getSnippet(docElementId, parent);
   575       if(result) {
   377       if (result) {
   576 	result->setParent(m_snippets); // Exports to "Snippets" JS object
   378         result->setParent(m_snippets); // Exports to "Snippets" JS object
   577         addSnippet(result, docElementId);
   379         addSnippet(result, docElementId);
   578       }
   380       }
   579       else{
   381       else{
   580 	qDebug() << "Snippet not found: " << docElementId;
   382         qDebug() << "Snippet not found: " << docElementId;
   581 	return 0;
   383         return 0;
   582       }
   384       }
   583     }else{
   385     }else{
   584       //qDebug() << "Found existing snippet: " << docElementId;
   386       //qDebug() << "Found existing snippet: " << docElementId;
   585     }
   387     }
   586     
   388 
   587     return result;
   389     return result;
   588   }
   390   }
   589 
   391 
   590   QRect ChromeWidget::getSnippetRect(const QString &docElementId)
   392   QRect ChromeWidget::getSnippetRect(const QString &docElementId)
   591   {
   393   {
   592     return m_dom->getElementRect(docElementId);
   394     return m_dom->getElementRect(docElementId);
   593   }
   395   }
   594 
   396 
   595   void ChromeWidget::addJSObjectToWindow(QObject *object) 
   397   void ChromeWidget::addJSObjectToWindow(QObject *object)
   596   {
   398   {
   597     m_page->mainFrame()->addToJavaScriptWindowObject(object->objectName(), object);
   399     m_page->mainFrame()->addToJavaScriptWindowObject(object->objectName(), object);
   598   }
   400   }
   599 
   401 
   600   void ChromeWidget::addJSObjectToPage(QObject *object, QWebPage *page)
   402   void ChromeWidget::addJSObjectToPage(QObject *object, QWebPage *page)
   609   void ChromeWidget::alert(const QString & msg) {
   411   void ChromeWidget::alert(const QString & msg) {
   610     // To do: open a dialog box showing msg.
   412     // To do: open a dialog box showing msg.
   611     qDebug() << msg;
   413     qDebug() << msg;
   612   }
   414   }
   613 
   415 
   614   QString ChromeWidget::getDisplayMode() const {
       
   615     return (m_aspect==portrait)?"portrait":"landscape";
       
   616   }
       
   617 
       
   618   void ChromeWidget::onViewInstantiated(ControllableViewBase *view) {   // slot
   416   void ChromeWidget::onViewInstantiated(ControllableViewBase *view) {   // slot
   619     qDebug() << "ChromeWidget::onViewInstantiated: "; // << view;
       
   620     addViewToLayout(view);
   417     addViewToLayout(view);
   621   }
   418   }
   622 
   419 
   623   QObject*  ChromeWidget::getDisplaySize() const 
   420   QObject*  ChromeWidget::getDisplaySize() const
   624   {
   421   {
   625     ScriptSize * sz = new ScriptSize(size().toSize());
   422     ScriptSize * sz = new ScriptSize(m_layout->size().toSize());
   626     m_page->mainFrame()->addToJavaScriptWindowObject("size", sz, QScriptEngine::ScriptOwnership);
   423     m_page->mainFrame()->addToJavaScriptWindowObject("size", sz, QScriptEngine::ScriptOwnership);
   627     return sz;
   424     return sz;
   628 
   425 
   629   }
   426   }
   630   
   427 
   631 void ChromeWidget::contextMenuEvent(QGraphicsSceneContextMenuEvent* event) 
       
   632 {
       
   633     event->accept();
       
   634 }
       
   635   
       
   636   /*
   428   /*
   637   void ChromeWidget::addJSObjectToEngine(QObject *object)
   429   void ChromeWidget::addJSObjectToEngine(QObject *object)
   638   {
   430   {
   639     QScriptValue val = m_engine.newQObject(object);
   431     QScriptValue val = m_engine.newQObject(object);
   640     m_engine.globalObject().setProperty(object->objectName(), val);
   432     m_engine.globalObject().setProperty(object->objectName(), val);
   643   QScriptValue ChromeWidget::evalWithEngineContext(const QString& program)
   435   QScriptValue ChromeWidget::evalWithEngineContext(const QString& program)
   644   {
   436   {
   645     return m_engine.evaluate(program);
   437     return m_engine.evaluate(program);
   646   }
   438   }
   647   */
   439   */
       
   440 
       
   441   void ChromeWidget::emitPopupShown(const QString &popupId) 
       
   442   {
       
   443       emit popupShown(popupId);
       
   444   }
       
   445 
       
   446   void ChromeWidget::emitPopupHidden(const QString &popupId) 
       
   447   {
       
   448       emit popupHidden(popupId);
       
   449   }
       
   450 
   648 
   451 
   649   void ChromeWidget::dump() {
   452   void ChromeWidget::dump() {
   650     qDebug() << "---------------------";
   453     qDebug() << "---------------------";
   651     qDebug() << "ChromeWidget::dump";
   454     qDebug() << "ChromeWidget::dump";
   652     m_snippets->dump();
   455     m_snippets->dump();