ginebra2/ChromeWidget.cpp
changeset 6 1c3b8676e58c
parent 5 0f2326c2a325
child 10 232fbd5a2dcb
equal deleted inserted replaced
5:0f2326c2a325 6:1c3b8676e58c
    24 #include <QWebFrame>
    24 #include <QWebFrame>
    25 #include <QList>
    25 #include <QList>
    26 #include <QKeyEvent>
    26 #include <QKeyEvent>
    27 #include <QDebug>
    27 #include <QDebug>
    28 #include <QDesktopServices>
    28 #include <QDesktopServices>
    29 #include <QGraphicsSceneContextMenuEvent>
       
    30 
    29 
    31 #include "bedrockprovisioning.h"
    30 #include "bedrockprovisioning.h"
    32 #include "ChromeWidgetJSObject.h"
    31 #include "ChromeWidgetJSObject.h"
       
    32 #include "ChromeLayout.h"
    33 #include "ChromeRenderer.h"
    33 #include "ChromeRenderer.h"
    34 #include "ChromeDOM.h"
    34 #include "ChromeDOM.h"
    35 #include "Snippets.h"
    35 #include "Snippets.h"
    36 #include "ChromeEffect.h"
    36 #include "ChromeEffect.h"
    37 #include "ChromeSnippet.h"
    37 #include "ChromeSnippet.h"
    69 namespace GVA {
    69 namespace GVA {
    70 
    70 
    71 // -----------------------------
    71 // -----------------------------
    72 
    72 
    73   ChromeWidget::ChromeWidget(QGraphicsItem * parent, Qt::WindowFlags wFlags)
    73   ChromeWidget::ChromeWidget(QGraphicsItem * parent, Qt::WindowFlags wFlags)
    74     : QGraphicsWidget(parent, wFlags),
    74     : QObject(0),
    75       m_renderer(0),
    75       m_renderer(0),
    76       m_dom(0),
    76       m_dom(0),
    77       m_viewController(new ViewController()),
    77       m_viewController(new ViewController()),
    78       m_topBar(0),
       
    79       m_bottomBar(0),
       
    80       m_leftBar(0),
       
    81       m_rightBar(0),
       
    82       m_aspect(portrait),
       
    83       m_jsObject(new ChromeWidgetJSObject(0, this)),
    78       m_jsObject(new ChromeWidgetJSObject(0, this)),
    84       m_localeDelegate(new LocaleDelegate(this)),
    79       m_localeDelegate(new LocaleDelegate(this)),
    85       m_downloads(new Downloads()),
    80       m_downloads(new Downloads())
    86       m_bottomBarHeight(0)
    81   {
    87   {
    82     m_layout = new ChromeLayout(parent, wFlags);
       
    83     QObject::connect(m_layout, SIGNAL(resizing(QSizeF)), this, SLOT(onResize(QSizeF)));
       
    84     QObject::connect(m_layout, SIGNAL(aspectChanged(int)), this, SLOT(onAspectChanged(int)));
       
    85 
    88     DeviceImpl *deviceImpl = new DEVICEIMPL();
    86     DeviceImpl *deviceImpl = new DEVICEIMPL();
    89     NetworkImpl *networkImpl = new NETWORKIMPL();
    87     NetworkImpl *networkImpl = new NETWORKIMPL();
    90     m_deviceDelegate = new DeviceDelegate(deviceImpl);
    88     m_deviceDelegate = new DeviceDelegate(deviceImpl);
    91     m_networkDelegate = new NetworkDelegate(networkImpl);
    89     m_networkDelegate = new NetworkDelegate(networkImpl);
    92 
    90 
    93     BEDROCK_PROVISIONING::BedrockProvisioning *provisioning = BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning();
    91     BEDROCK_PROVISIONING::BedrockProvisioning *provisioning = BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning();
    94     ChromeEffect::disabledColor.setNamedColor(provisioning->valueAsString("DisabledColor", "#FFFFFF"));
    92     ChromeEffect::disabledColor.setNamedColor(provisioning->valueAsString("DisabledColor", "#FFFFFF"));
    95     ChromeEffect::disabledOpacity = static_cast<qreal>(provisioning->valueAsString("DisabledOpacity", "0.65").toFloat());
    93     ChromeEffect::disabledOpacity = static_cast<qreal>(provisioning->valueAsString("DisabledOpacity", "0.65").toFloat());
    96 
    94 
    97     //Keep key events not otherwise consumed from going to the scene
       
    98     //installEventFilter(this);
       
    99     m_layout = new QGraphicsAnchorLayout();
       
   100     m_layout->setContentsMargins(0,0,0,0);
       
   101     m_layout->setSpacing(0);
       
   102     setLayout(m_layout);
       
   103     addAnchors();
       
   104     m_viewPort = new SlidingWidget(this);
       
   105     //m_viewBar = new CollapsingWidget(m_viewPort);
       
   106 #ifndef __gva_no_chrome__
    95 #ifndef __gva_no_chrome__
   107     m_snippets = new Snippets(this, this);
    96     m_snippets = new Snippets(this, this);
   108 #endif
    97 #endif
   109     m_layout->addAnchor(m_viewPort, Qt::AnchorTop, m_topBar, Qt::AnchorBottom);
       
   110     m_layout->addAnchor(m_viewPort, Qt::AnchorBottom, m_bottomBar, Qt::AnchorTop);
       
   111     m_layout->addAnchor(m_viewPort, Qt::AnchorLeft, m_leftBar, Qt::AnchorRight);
       
   112     m_layout->addAnchor(m_viewPort, Qt::AnchorRight, m_rightBar, Qt::AnchorLeft);
       
   113 #ifndef __gva_no_chrome__
    98 #ifndef __gva_no_chrome__
   114     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"));
   115     m_page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
   100     m_page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
   116     m_page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
   101     m_page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
   117 #endif
   102 #endif
   119 
   104 
   120     connect(m_viewController, SIGNAL(currentViewChanged()), this, SLOT(onCurrentViewChanged()));
   105     connect(m_viewController, SIGNAL(currentViewChanged()), this, SLOT(onCurrentViewChanged()));
   121 #ifndef __gva_no_chrome__
   106 #ifndef __gva_no_chrome__
   122 
   107 
   123     m_jsObject->setObjectName("chrome");
   108     m_jsObject->setObjectName("chrome");
       
   109 
   124     // Pass some signals from this object to the Javascript object.
   110     // Pass some signals from this object to the Javascript object.
   125     QObject::connect(this, SIGNAL(chromeComplete()), m_jsObject, SIGNAL(chromeComplete()));
   111     QObject::connect(this, SIGNAL(chromeComplete()), m_jsObject, SIGNAL(chromeComplete()));
   126     QObject::connect(this, SIGNAL(aspectChanged(int)), m_jsObject, SIGNAL(aspectChanged(int)));
   112     QObject::connect(this, SIGNAL(aspectChanged(int)), m_jsObject, SIGNAL(aspectChanged(int)));
   127     QObject::connect(this, SIGNAL(prepareForGeometryChange()), m_jsObject, SIGNAL(prepareForGeometryChange()));
   113     QObject::connect(this, SIGNAL(prepareForGeometryChange()), m_jsObject, SIGNAL(prepareForGeometryChange()));
   128     QObject::connect(this, SIGNAL(symbianCarriageReturn()), m_jsObject, SIGNAL(symbianCarriageReturn()));
   114     QObject::connect(this, SIGNAL(symbianCarriageReturn()), m_jsObject, SIGNAL(symbianCarriageReturn()));
   129     QObject::connect(this, SIGNAL(popupShown(const QString &)), m_jsObject, SIGNAL(popupShown(const QString &)));
   115     QObject::connect(this, SIGNAL(popupShown(const QString &)), m_jsObject, SIGNAL(popupShown(const QString &)));
   130     QObject::connect(this, SIGNAL(popupHidden(const QString &)), m_jsObject, SIGNAL(popupHidden(const QString &)));
   116     QObject::connect(this, SIGNAL(popupHidden(const QString &)), m_jsObject, SIGNAL(popupHidden(const QString &)));
   131 
   117 
   132     //addJSObjectToEngine(this);
   118     //addJSObjectToEngine(this);
       
   119 
   133     m_app = new GinebraApplication();
   120     m_app = new GinebraApplication();
       
   121 
   134     //addJSObjectToEngine(m_app);
   122     //addJSObjectToEngine(m_app);
   135 
   123 
   136     QObject::connect(
   124     QObject::connect(
   137             WebPageController::getSingleton(), SIGNAL(pageCreated(WRT::WrtBrowserContainer*)),
   125             WebPageController::getSingleton(), SIGNAL(pageCreated(WRT::WrtBrowserContainer*)),
   138             this, SLOT(pageCreated(WRT::WrtBrowserContainer*)));
   126             this, SLOT(pageCreated(WRT::WrtBrowserContainer*)));
   148     vs->setChromeWidget(this);
   136     vs->setChromeWidget(this);
   149 
   137 
   150     // TO DO: need a better home for this.
   138     // TO DO: need a better home for this.
   151     qMetaTypeId<QObjectList>();
   139     qMetaTypeId<QObjectList>();
   152     qRegisterMetaType<QObjectList>("QObjectList");
   140     qRegisterMetaType<QObjectList>("QObjectList");
   153   }
   141 	
   154 
   142 	//for QA Automation test tool purpose
   155   void ChromeWidget::loadUrlToCurrentPage(const QUrl & url)
   143 #if !defined(QT_NO_LIBRARY)
   156   {
   144     QLibrary testLib("qttestability");
   157     WRT::WrtBrowserContainer * activePage = WebPageController::getSingleton()->currentPage();
   145     if(testLib.load()){
   158 
   146         typedef void (*TasInitialize)(void);
   159     if (activePage) {
   147         TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
   160       activePage->mainFrame()->load(url.toString());
       
   161     }
       
   162   }
       
   163 
       
   164   void ChromeWidget::pageCreated(WRT::WrtBrowserContainer * page)
       
   165   {
       
   166 #ifdef Q_OS_SYMBIAN
   148 #ifdef Q_OS_SYMBIAN
   167     QString path = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
   149         //not found so use ordinal
   168     GAlternateFileChooser * chooser = new GAlternateFileChooser(path);
   150         if(!initFunction){
   169     page->setFileChooser(chooser); // chooser is now owned by page
   151             initFunction = (TasInitialize)testLib.resolve("1");
   170 #endif
   152         }
   171     m_downloads->handlePage(page);
   153 #endif
       
   154         if(initFunction){
       
   155             initFunction();
       
   156         }
       
   157     }
       
   158 #endif
   172   }
   159   }
   173 
   160 
   174   ChromeWidget::~ChromeWidget()
   161   ChromeWidget::~ChromeWidget()
   175   {
   162   {
   176     // clearChrome(); // Crashes on exit
       
   177     delete m_viewController;
   163     delete m_viewController;
   178     delete m_jsObject;
   164     delete m_jsObject;
   179     delete m_bottomBar;
   165     delete m_layout;
   180     delete m_topBar;
       
   181     delete m_leftBar;
       
   182     delete m_rightBar;
       
   183     delete m_viewPort;
       
   184     // delete m_viewLayout; // Crashes on exit
       
   185     delete m_renderer;
   166     delete m_renderer;
   186     delete m_dom;
   167     delete m_dom;
   187     delete m_page;
   168     delete m_page;
   188     // delete m_scene; // Crashes on exit
       
   189     delete m_snippets;
   169     delete m_snippets;
   190     // delete m_layout; // Crashes on exit
       
   191     delete m_localeDelegate;
   170     delete m_localeDelegate;
   192     delete m_deviceDelegate;
   171     delete m_deviceDelegate;
   193     delete m_networkDelegate;
   172     delete m_networkDelegate;
   194     delete m_app;
   173     delete m_app;
   195     delete m_downloads;
   174     delete m_downloads;
   196   }
   175   }
   197 
   176 
   198 
   177   //Handle resizing signal from layout
   199   //Eat key events not otherwise consumed.
   178 
   200   /*bool ChromeWidget::eventFilter(QObject * obj, QEvent * ev)
   179   void ChromeWidget::onResize(QSizeF size)
   201   {
       
   202    if (ev->type() == QEvent::KeyPress){
       
   203       return true;
       
   204     }
       
   205     return QObject::eventFilter(obj,ev);
       
   206 
       
   207   }*/
       
   208 
       
   209   void ChromeWidget::resizeEvent(QGraphicsSceneResizeEvent *ev)
       
   210   {
   180   {
   211 #ifndef __gva_no_chrome__
   181 #ifndef __gva_no_chrome__
   212     if (m_dom && m_renderer) {
   182     if (m_dom && m_renderer) {
   213       emit prepareForGeometryChange();
   183       emit prepareForGeometryChange();
   214      // m_renderer->setGeometry(QRectF(-1200,-1200, ev->newSize().width(), m_dom->height()));
   184       m_renderer->resize(size);
   215       m_renderer->resize(QSizeF(ev->newSize().width(), m_dom->height()));
   185     }
   216     }
   186 #endif
   217     int aspect = m_aspect;
   187   }
   218     m_aspect = (ev->newSize().width() > ev->newSize().height())?landscape:portrait;
   188 
   219     if (m_aspect != aspect) {
   189   //Handle aspectChanged signal from layout
   220       QString mode = (m_aspect == landscape ? "Landscape" : "Portrait");
   190 
   221       ControllableViewBase* cview = m_viewController->currentView();
   191   void ChromeWidget::onAspectChanged(int aspect)
   222       if (cview)
   192   {
   223         cview->displayModeChanged(mode);
   193     QString mode = (aspect == landscape ? "Landscape" : "Portrait");
   224       emit aspectChanged(m_aspect);
   194     ControllableViewBase* cview = m_viewController->currentView();
   225     }
   195     if (cview)
   226 #endif
   196       cview->displayModeChanged(mode);
   227     QGraphicsWidget::resizeEvent(ev);
   197     emit aspectChanged(aspect);
   228   }
   198   }
   229 
   199 
   230   void ChromeWidget::updateChromeLayout() {
   200   void ChromeWidget::updateChromeLayout() {
   231     qDebug() << "ChromeWidget::updateChromeLayout";
       
   232     m_renderer->updateChromeLayout();
   201     m_renderer->updateChromeLayout();
   233   }
   202   }
   234 
   203 
   235   /*
   204 
   236   //For layout debugging
   205   void ChromeWidget::loadUrlToCurrentPage(const QUrl & url)
   237   void ChromeWidget::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget* widget){
   206   {
   238     painter->fillRect(geometry(), Qt::red);
   207     WRT::WrtBrowserContainer * activePage = WebPageController::getSingleton()->currentPage();
   239   }
   208 
   240   */
   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   }
   241 
   223 
   242   void ChromeWidget::setChromeBaseDirectory(const QString dir) {
   224   void ChromeWidget::setChromeBaseDirectory(const QString dir) {
   243     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);
   244   }
   228   }
   245 
   229 
   246   void ChromeWidget::setChromeFile(const QString filePath)
   230   void ChromeWidget::setChromeFile(const QString filePath)
   247   {
   231   {
   248 #ifndef __gva_no_chrome__
   232 #ifndef __gva_no_chrome__
   249     qDebug() << "ChromeWidget::setChromeFile: " << m_baseDirectory << filePath;
       
   250     m_page->mainFrame()->load(QUrl(m_baseDirectory + filePath));
   233     m_page->mainFrame()->load(QUrl(m_baseDirectory + filePath));
   251     qDebug() << "ChromeWidget::setChromeFile: done";
       
   252 #else
   234 #else
   253     Q_UNUSED(filePath)
   235     Q_UNUSED(filePath)
   254 #endif
   236 #endif
   255   }
   237   }
   256 
   238 
   258   {
   240   {
   259     clearChrome();
   241     clearChrome();
   260     m_page->triggerAction(QWebPage::Reload);
   242     m_page->triggerAction(QWebPage::Reload);
   261   }
   243   }
   262 
   244 
       
   245   // TODO: needed?
   263   void ChromeWidget::addViewToLayout(ControllableViewBase * controllableView){
   246   void ChromeWidget::addViewToLayout(ControllableViewBase * controllableView){
   264     //qDebug() << "ChromeWidget::addViewToLayout: " << controllableView->widget();
   247     m_layout->addView(controllableView);
   265     if (controllableView->widget()) {
       
   266       m_viewPort->setWindow(controllableView->widget());
       
   267       controllableView->widget()->setFocusPolicy(Qt::ClickFocus); //NB: Is this needed? Does it break anything?
       
   268       controllableView->widget()->setZValue(-1.0); //Make sure we are behind any center anchored snippets
       
   269     }
       
   270   }
   248   }
   271 
   249 
   272   void ChromeWidget::addView(ControllableViewBase * controllableView) {
   250   void ChromeWidget::addView(ControllableViewBase * controllableView) {
   273     //qDebug() << "ChromeWidget::addView: " << controllableView->widget();
   251     //qDebug() << "ChromeWidget::addView: " << controllableView->widget();
   274     m_viewController->addView(controllableView);
   252     m_viewController->addView(controllableView);
   275 
   253   }
   276     /*
   254   
   277     if (controllableView->widget()) {
       
   278       addViewToLayout(controllableView);
       
   279     }
       
   280     else {
       
   281       qWarning() << "ChromeWidget::addView: view has no widget. " << controllableView;
       
   282       connect(controllableView, SIGNAL(instantiated(ControllableViewBase *)), this, SLOT(onViewInstantiated(ControllableViewBase *)));
       
   283     }
       
   284 //    m_jsObjects.append(controllableView); // Add to list of exported objects
       
   285 #ifndef __gva_no_chrome__
       
   286     addJSObjectToWindow(controllableView); // Export to DOM
       
   287 #endif
       
   288     //addJSObjectToEngine(viewDelegate); // Export to script engine
       
   289     */
       
   290   }
       
   291 
       
   292   void ChromeWidget::anchorToView(ChromeSnippet* snippet, const QString& where)
       
   293   {
       
   294     Q_UNUSED(where)
       
   295       //m_viewBar->addItem(snippet);
       
   296     m_viewPort->attachItem(snippet->widget());
       
   297   }
       
   298 
       
   299   void ChromeWidget::detachFromView(ChromeSnippet* snippet, const QString& where)
       
   300   {
       
   301     Q_UNUSED(where)
       
   302       //  m_viewBar->removeItem(snippet);
       
   303     m_viewPort->detachItem(snippet->widget());
       
   304   }
       
   305 
       
   306   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)
   307   {
   256   {
   308     ChromeSnippet* second = getSnippet(secondId);
   257     ChromeSnippet* second = getSnippet(secondId);
   309     if (second){
   258     if (second){
   310       //qDebug() << "Anchoring: " << first->objectName() << " to: " << second->objectName();
   259       m_layout->anchorTogether(first, second, x, y);
   311       first->widget()->setParentItem(second->widget());
       
   312       first->widget()->setPos(x,y);
       
   313     }
   260     }
   314     else {
   261     else {
   315       qDebug() << "ChromeWidget::anchorTogether: error, not found: " << secondId;
   262       qDebug() << "Chrome::anchorTogether: error, not found: " << secondId;
   316     }
   263     }
   317   }
       
   318 
       
   319   void ChromeWidget::unAnchor(ChromeSnippet* snippet)
       
   320   {
       
   321     snippet->widget()->setParentItem(0);
       
   322     snippet->widget()->setParentItem(this);
       
   323   }
       
   324 
       
   325   qreal ChromeWidget::shrinkView(qreal delta)
       
   326     {
       
   327       m_viewPort->setShrinkMax(bottomBarHeight());
       
   328       return m_viewPort->shrink(delta);
       
   329     }
       
   330 
       
   331   qreal ChromeWidget::slideView(qreal delta)
       
   332   {
       
   333     //return m_viewBar->collapse(delta);
       
   334     return m_viewPort->slide(delta);
       
   335   }
   264   }
   336 
   265 
   337   ControllableViewBase * ChromeWidget::getView(const QString& view)
   266   ControllableViewBase * ChromeWidget::getView(const QString& view)
   338   {
   267   {
   339     return m_viewController->view(view);
   268     return m_viewController->view(view);
   340   }
   269   }
   341 
   270 
   342   void ChromeWidget::showView(const QString &name) {
   271   void ChromeWidget::showView(const QString &name) {
   343     qDebug() << "ChromeWidget::showView: " << name;
       
   344     m_viewController->showView(name);
   272     m_viewController->showView(name);
   345   }
   273   }
   346 
   274 
   347   void ChromeWidget::onCurrentViewChanged() {
   275   void ChromeWidget::onCurrentViewChanged() {
   348     addViewToLayout(m_viewController->currentView());
   276     m_layout->addView(m_viewController->currentView());
   349   }
   277   }
   350 
   278 
   351   // Clean up all existing snippets;
   279   // Clean up all existing snippets;
   352 
   280 
   353   void ChromeWidget::clearChrome()
   281   void ChromeWidget::clearChrome()
   360     clearChrome();
   288     clearChrome();
   361   }
   289   }
   362 
   290 
   363   void ChromeWidget::loadFinished(bool ok)  // slot
   291   void ChromeWidget::loadFinished(bool ok)  // slot
   364   {
   292   {
   365     qDebug() << "ChromeWidget::loadFinished: " << ok;
       
   366     if (!ok) {
   293     if (!ok) {
   367       qDebug() << "ChromeWidget::loadFinished: error";
       
   368       return;
   294       return;
   369     }
   295     }
   370     //NB: do we really need to instantiate a new renderer?
   296     if (!m_renderer)
   371     if (m_renderer)
   297       m_renderer = new ChromeRenderer(m_page, this);
   372       delete m_renderer;
   298     m_renderer->resize(m_layout->size());
   373     // qDebug() << "Instantiate renderer";
       
   374     m_renderer = new ChromeRenderer(m_page, this);
       
   375     // qDebug() << "Resize the renderer 1";
       
   376     m_renderer->resize(size());
       
   377    // m_renderer->setPos(-1200, -1200);
       
   378    // m_renderer->setZValue(-3);
       
   379 #ifdef Q_OS_SYMBIAN
       
   380    //connect(m_renderer, SIGNAL(symbianCarriageReturn()), this, SIGNAL(symbianCarriageReturn()));
       
   381 #endif
       
   382     if (m_dom)
   299     if (m_dom)
   383       delete m_dom; // NB: This may need some further investigation
   300       delete m_dom; // NB: This may need some further investigation
   384     m_dom = new ChromeDOM(m_page, this);
   301     m_dom = new ChromeDOM(m_page, this);
   385     getInitialSnippets();
   302     getInitialSnippets();
   386     //Set the final renderer size to match the chrome
   303     m_renderer->resize(QSizeF(m_layout->size().width(), m_dom->height()));
   387     //m_renderer->resize(size().width(), m_dom->height());
       
   388     m_renderer->resize(QSizeF(size().width(), m_dom->height()));
       
   389     //qDebug() << m_dom->getCacheableScript();
   304     //qDebug() << m_dom->getCacheableScript();
   390 
       
   391     // Let internal objects know that the chrome is complete.
   305     // Let internal objects know that the chrome is complete.
   392     emit internalChromeComplete();
   306     emit internalChromeComplete();
   393     // Now let the javascript world know that it is complete.
   307     // Now let the javascript world know that it is complete.
   394     emit chromeComplete();
   308     emit chromeComplete();
   395 
       
   396     // connect ViewStack to creatingPage signal
   309     // connect ViewStack to creatingPage signal
   397     connect( WebPageController::getSingleton(), SIGNAL(creatingPage(WRT::WrtBrowserContainer*)),
   310     connect( WebPageController::getSingleton(), SIGNAL(creatingPage(WRT::WrtBrowserContainer*)),
   398              ViewStack::getSingleton(), SLOT(creatingPage(WRT::WrtBrowserContainer*)));
   311              ViewStack::getSingleton(), SLOT(creatingPage(WRT::WrtBrowserContainer*)));
   399     qDebug() << "ChromeWidget::loadFinished: done";
       
   400   }
   312   }
   401 
   313 
   402   void ChromeWidget::chromeInitialized()
   314   void ChromeWidget::chromeInitialized()
   403   {
   315   {
   404     //NB: Don't want to implement this, but just in case
   316     //NB: Don't want to implement this, but just in case
   436       if (s->initiallyVisible())
   348       if (s->initiallyVisible())
   437         s->setVisible(true);
   349         s->setVisible(true);
   438     }
   350     }
   439   }
   351   }
   440 
   352 
   441   void ChromeWidget::addAnchors(){
   353   //TODO: check new logic
   442     if (!m_bottomBar){
   354  
   443       m_bottomBar = new QGraphicsWidget(this);
       
   444       m_bottomBar->setPreferredHeight(0);
       
   445       m_bottomBar->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
       
   446       m_layout->addAnchor(m_bottomBar, Qt::AnchorBottom, m_layout, Qt::AnchorBottom);
       
   447       m_layout->addAnchors(m_bottomBar, m_layout, Qt::Horizontal);
       
   448     }
       
   449     if (!m_topBar){
       
   450       m_topBar = new QGraphicsWidget(this);
       
   451       m_topBar->setPreferredHeight(0);
       
   452       m_topBar->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
       
   453       m_layout->addAnchor(m_topBar, Qt::AnchorTop, m_layout, Qt::AnchorTop);
       
   454       m_layout->addAnchors(m_topBar, m_layout, Qt::Horizontal);
       
   455     }
       
   456     if (!m_leftBar){
       
   457       m_leftBar = new QGraphicsWidget(this);
       
   458       m_leftBar->setPreferredWidth(0);
       
   459       m_leftBar->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred));
       
   460       m_layout->addAnchor(m_leftBar, Qt::AnchorLeft, m_layout, Qt::AnchorLeft);
       
   461       m_layout->addAnchors(m_leftBar, m_layout, Qt::Vertical);
       
   462     }
       
   463     if (!m_rightBar){
       
   464       m_rightBar = new QGraphicsWidget(this);
       
   465       m_rightBar->setPreferredWidth(0);
       
   466       m_rightBar->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred));
       
   467       m_layout->addAnchor(m_rightBar, Qt::AnchorRight, m_layout, Qt::AnchorRight);
       
   468       m_layout->addAnchors(m_rightBar, m_layout, Qt::Vertical);
       
   469     }
       
   470   }
       
   471 
       
   472   //Check to see if snippet has an anchor and lay it out accordingly.
       
   473   //Otherwise snippets determine their own positions from html
       
   474 
       
   475   void ChromeWidget::anchorSnippet(ChromeSnippet * snippet)
       
   476   {
       
   477     if (snippet->anchor() == anchorBottom){
       
   478       if (snippet->hidesContent()){
       
   479     m_layout->addAnchor(snippet->widget(), Qt::AnchorBottom, m_bottomBar, Qt::AnchorTop);
       
   480     m_layout->addAnchor(snippet->widget(), Qt::AnchorHorizontalCenter, m_bottomBar, Qt::AnchorHorizontalCenter);
       
   481       } else {
       
   482     snippet->widget()->setParentItem(m_bottomBar);
       
   483     snippet->widget()->setY(-snippet->anchorOffset());
       
   484       }
       
   485     }
       
   486     else if (snippet->anchor() == anchorTop){
       
   487       if (snippet->hidesContent()){
       
   488     m_layout->addAnchor(snippet->widget(),Qt::AnchorTop, m_topBar, Qt::AnchorBottom);
       
   489     m_layout->addAnchor(snippet->widget(), Qt::AnchorHorizontalCenter, m_topBar, Qt::AnchorHorizontalCenter);
       
   490       }
       
   491       else {
       
   492     snippet->widget()->setParentItem(m_topBar);
       
   493     snippet->widget()->setY(snippet->anchorOffset());
       
   494       }
       
   495     }
       
   496     else if (snippet->anchor() == anchorLeft){
       
   497       if (snippet->hidesContent())
       
   498     m_layout->addAnchor(snippet->widget(),Qt::AnchorLeft, m_leftBar, Qt::AnchorRight);
       
   499       else {
       
   500     snippet->widget()->setParentItem(m_leftBar);
       
   501     snippet->widget()->setX(snippet->anchorOffset());
       
   502       }
       
   503     }
       
   504     else if (snippet->anchor() == anchorRight){
       
   505       if (snippet->hidesContent())
       
   506     m_layout->addAnchor(snippet->widget(),Qt::AnchorRight, m_rightBar, Qt::AnchorLeft);
       
   507       else {
       
   508     snippet->widget()->setParentItem(m_rightBar);
       
   509     snippet->widget()->setX(-snippet->anchorOffset());
       
   510       }
       
   511     }
       
   512     else if (snippet->anchor() == anchorCenter) {
       
   513       snippet->widget()->setParentItem(m_viewPort);
       
   514       snippet->widget()->setZValue(0.0);
       
   515       QGraphicsAnchorLayout * vl = static_cast<QGraphicsAnchorLayout*>(m_viewPort->layout());
       
   516       vl->addAnchor(snippet->widget(), Qt::AnchorVerticalCenter, vl, Qt::AnchorVerticalCenter);
       
   517       vl->addAnchor(snippet->widget(), Qt::AnchorHorizontalCenter, vl, Qt::AnchorHorizontalCenter);
       
   518     }
       
   519     else if (snippet->anchor() == anchorTopLeft){
       
   520       qDebug() << "Setting top left anchor";
       
   521       m_layout->addCornerAnchors(snippet->widget(), Qt::TopLeftCorner, m_layout, Qt::TopLeftCorner);
       
   522     }
       
   523     else if (snippet->anchor() == anchorTopRight)
       
   524       m_layout->addCornerAnchors(snippet->widget(), Qt::TopRightCorner, m_layout, Qt::TopRightCorner);
       
   525     else if (snippet->anchor() == anchorBottomLeft)
       
   526       m_layout->addCornerAnchors(snippet->widget(), Qt::BottomLeftCorner, m_layout, Qt::BottomLeftCorner);
       
   527     else if (snippet->anchor() == anchorBottomRight)
       
   528       m_layout->addCornerAnchors(snippet->widget(), Qt::BottomRightCorner, m_layout, Qt::BottomRightCorner);
       
   529   }
       
   530 
       
   531   void ChromeWidget:: addSnippet(ChromeSnippet * snippet, const QString & docElementId )
   355   void ChromeWidget:: addSnippet(ChromeSnippet * snippet, const QString & docElementId )
   532   {
   356   {
   533     m_snippets->addSnippet(snippet, docElementId);
   357     m_snippets->addSnippet(snippet, docElementId);
   534     snippet->widget()->setParentItem(this);
   358     ChromeSnippet * container = 0;
   535 
       
   536     if (!snippet->parentId().isNull()){
   359     if (!snippet->parentId().isNull()){
   537       ChromeSnippet * container = getSnippet(snippet->parentId());
   360       container = getSnippet(snippet->parentId());
   538       if (container)
   361     }
   539     container->addChild(snippet);
   362     m_layout->addSnippet(snippet, container);
   540     }
       
   541 
       
   542     anchorSnippet(snippet);
       
   543   }
   363   }
   544 
   364 
   545   //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
   546   //widget class !!!!!!
   366   //widget class !!!!!!
   547 
   367 
   548   void ChromeWidget::adjustAnchorOffset(ChromeSnippet * snippet, qreal delta)
   368   void ChromeWidget::adjustAnchorOffset(ChromeSnippet * snippet, qreal delta)
   549   {
   369   {
   550     if (snippet->anchor() == anchorBottom)
   370     m_layout->adjustAnchorOffset(snippet, delta);
   551       m_bottomBar->setPreferredHeight(m_bottomBar->preferredHeight() + delta);
       
   552     else if (snippet->anchor() == anchorTop)
       
   553       m_topBar->setPreferredHeight(m_topBar->preferredHeight() + delta);
       
   554     else if (snippet->anchor() == anchorLeft){
       
   555       m_leftBar->setPreferredWidth(m_leftBar->preferredWidth() + delta);
       
   556     }
       
   557     else if (snippet->anchor() == anchorRight){
       
   558       m_rightBar->setPreferredWidth(m_rightBar->preferredWidth() + delta);
       
   559     }
       
   560   }
       
   561 
       
   562   // Call after a snippet changes to visible state
       
   563   // (NB: does not check to see whether snippet has been
       
   564   // correctly added to chrome (by calling addSnippet).
       
   565   // At a minimum should assert that the anchor "bars"
       
   566   // are not null.)
       
   567 
       
   568   void ChromeWidget::snippetShown(ChromeSnippet * snippet)
       
   569   {
       
   570      if (snippet->hidesContent()) {
       
   571         if (snippet->anchor() == anchorBottom) {
       
   572             /* snippet->widget()->size().height() is zero for  WebChromeContainerSnippet
       
   573                when default visible container snippet is shown, get ownerArea height instead */
       
   574             WebChromeContainerSnippet * s = dynamic_cast<WebChromeContainerSnippet *>(snippet);
       
   575             if (!s) {
       
   576                 m_bottomBarHeight = snippet->widget()->size().height();
       
   577             }
       
   578             else {
       
   579                 m_bottomBarHeight = s->ownerArea().height();
       
   580             }
       
   581         }
       
   582         return;
       
   583     }
       
   584     if (snippet->anchor() == anchorBottom)
       
   585       m_bottomBar->setPreferredHeight(m_bottomBar->preferredHeight() + snippet->widget()->preferredHeight());
       
   586     else if (snippet->anchor() == anchorTop)
       
   587       m_topBar->setPreferredHeight(m_topBar->preferredHeight() + snippet->widget()->preferredHeight());
       
   588     else if (snippet->anchor() == anchorLeft){
       
   589       m_leftBar->setPreferredWidth(m_leftBar->preferredWidth() + snippet->widget()->preferredWidth());
       
   590     }
       
   591     else if (snippet->anchor() == anchorRight){
       
   592       m_rightBar->setPreferredWidth(m_rightBar->preferredWidth() + snippet->widget()->preferredWidth());
       
   593     }
       
   594   }
       
   595 
       
   596   // Call before a snippet changes to invisible state
       
   597   // (NB: does not check to see whether snippet has been
       
   598   // correctly added to chrome (by calling addSnippet).
       
   599   // At a minimum should assert that the anchor "bars"
       
   600   // are not null.)
       
   601 
       
   602   void ChromeWidget::snippetHiding(ChromeSnippet * snippet)
       
   603   {
       
   604     if (snippet->hidesContent())
       
   605       return;
       
   606     if (snippet->anchor() == anchorBottom)
       
   607       m_bottomBar->setPreferredHeight(m_bottomBar->preferredHeight() - snippet->widget()->preferredHeight());
       
   608     else if (snippet->anchor() == anchorTop)
       
   609       m_topBar->setPreferredHeight(m_topBar->preferredHeight() - snippet->widget()->preferredHeight());
       
   610     else if (snippet->anchor() == anchorLeft){
       
   611       m_leftBar->setPreferredWidth(m_leftBar->preferredWidth() - snippet->widget()->preferredWidth());
       
   612     }
       
   613     else if (snippet->anchor() == anchorRight){
       
   614       m_rightBar->setPreferredWidth(m_rightBar->preferredWidth() - snippet->widget()->preferredWidth());
       
   615     }
       
   616   }
   371   }
   617 
   372 
   618   ChromeSnippet *ChromeWidget::getSnippet(const QString & docElementId, QGraphicsItem * parent) {
   373   ChromeSnippet *ChromeWidget::getSnippet(const QString & docElementId, QGraphicsItem * parent) {
   619     ChromeSnippet *result = m_snippets->getSnippet(docElementId);
   374     ChromeSnippet *result = m_snippets->getSnippet(docElementId);
   620     if (!result){
   375     if (!result){
   656   void ChromeWidget::alert(const QString & msg) {
   411   void ChromeWidget::alert(const QString & msg) {
   657     // To do: open a dialog box showing msg.
   412     // To do: open a dialog box showing msg.
   658     qDebug() << msg;
   413     qDebug() << msg;
   659   }
   414   }
   660 
   415 
   661   QString ChromeWidget::getDisplayMode() const {
       
   662     return (m_aspect==portrait)?"portrait":"landscape";
       
   663   }
       
   664 
       
   665   void ChromeWidget::onViewInstantiated(ControllableViewBase *view) {   // slot
   416   void ChromeWidget::onViewInstantiated(ControllableViewBase *view) {   // slot
   666     qDebug() << "ChromeWidget::onViewInstantiated: "; // << view;
       
   667     addViewToLayout(view);
   417     addViewToLayout(view);
   668   }
   418   }
   669 
   419 
   670   QObject*  ChromeWidget::getDisplaySize() const
   420   QObject*  ChromeWidget::getDisplaySize() const
   671   {
   421   {
   672     ScriptSize * sz = new ScriptSize(size().toSize());
   422     ScriptSize * sz = new ScriptSize(m_layout->size().toSize());
   673     m_page->mainFrame()->addToJavaScriptWindowObject("size", sz, QScriptEngine::ScriptOwnership);
   423     m_page->mainFrame()->addToJavaScriptWindowObject("size", sz, QScriptEngine::ScriptOwnership);
   674     return sz;
   424     return sz;
   675 
   425 
   676   }
   426   }
   677 
   427 
   678 void ChromeWidget::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
       
   679 {
       
   680     event->accept();
       
   681 }
       
   682 
       
   683   /*
   428   /*
   684   void ChromeWidget::addJSObjectToEngine(QObject *object)
   429   void ChromeWidget::addJSObjectToEngine(QObject *object)
   685   {
   430   {
   686     QScriptValue val = m_engine.newQObject(object);
   431     QScriptValue val = m_engine.newQObject(object);
   687     m_engine.globalObject().setProperty(object->objectName(), val);
   432     m_engine.globalObject().setProperty(object->objectName(), val);
   691   {
   436   {
   692     return m_engine.evaluate(program);
   437     return m_engine.evaluate(program);
   693   }
   438   }
   694   */
   439   */
   695 
   440 
   696   void ChromeWidget::setScene(QGraphicsScene *scene)
   441   void ChromeWidget::emitPopupShown(const QString &popupId) 
   697   {
   442   {
   698     m_scene = scene;
       
   699     //This is the root of the scene hierarchy
       
   700     m_scene->addItem(this);
       
   701 
       
   702     // For testing, turn on red dot that appears in response to various mouse events.
       
   703     if(BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->valueAsInt("EnableTouchCircle", false)) {
       
   704         new TouchCircleCharm(m_scene, this);
       
   705     }
       
   706   }
       
   707 
       
   708   void ChromeWidget::emitPopupShown(const QString &popupId) {
       
   709       qDebug() << "ChromeWidget::emitPopupShown: " << popupId;
       
   710       emit popupShown(popupId);
   443       emit popupShown(popupId);
   711   }
   444   }
   712 
   445 
   713   void ChromeWidget::emitPopupHidden(const QString &popupId) {
   446   void ChromeWidget::emitPopupHidden(const QString &popupId) 
       
   447   {
   714       emit popupHidden(popupId);
   448       emit popupHidden(popupId);
   715   }
   449   }
       
   450 
   716 
   451 
   717   void ChromeWidget::dump() {
   452   void ChromeWidget::dump() {
   718     qDebug() << "---------------------";
   453     qDebug() << "---------------------";
   719     qDebug() << "ChromeWidget::dump";
   454     qDebug() << "ChromeWidget::dump";
   720     m_snippets->dump();
   455     m_snippets->dump();