ginebra2/GWebPage.h
branchGCC_SURGE
changeset 8 2e16851ffecd
parent 6 1c3b8676e58c
equal deleted inserted replaced
2:bf4420e9fa4d 8:2e16851ffecd
     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 #ifndef __GINEBRA_GWEBPAGE_H__
    22 #ifndef __GINEBRA_GWEBPAGE_H__
    20 #define __GINEBRA_GWEBPAGE_H__
    23 #define __GINEBRA_GWEBPAGE_H__
    21 
    24 
    22 #include <QDebug>
    25 #include <QDebug>
    23 #include <QWebPage>
    26 #include <QWebPage>
    24 #include <QWebFrame>
    27 #include <QWebFrame>
    25 #include "ChromeWidget.h"
    28 #include "ChromeWidget.h"
       
    29 #include "WebViewEventContext.h"
    26 
    30 
    27 namespace GVA {
    31 namespace GVA {
    28 
    32 
    29   // ------------------------------
    33 // ------------------------------
    30   // Simple wrapper class for QWebPage to allow interception of javascript errors.
    34 // Simple wrapper class for QWebPage to allow interception of javascript errors.
    31   class WebPageWrapper : public QWebPage {
    35 class WebPageWrapper : public QWebPage {
    32     public:
    36 public:
    33       WebPageWrapper(QObject *parent, const QString &prefix)
    37     WebPageWrapper(QObject *parent, const QString &prefix);
    34         : QWebPage(parent),
       
    35           m_prefix(prefix) {
       
    36         qDebug() << "WebPageWrapper::WebPageWrapper";
       
    37       }
       
    38 
    38 
    39       // Called when javascript errors are hit in the chrome page.
    39     // Called when javascript errors are hit in the chrome page.
    40       virtual void javaScriptConsoleMessage(const QString & message, int lineNumber, const QString & sourceID) {
    40     virtual void javaScriptConsoleMessage(const QString & message, int lineNumber, const QString & sourceID);
    41         qDebug() << m_prefix << ":";
       
    42         qDebug() << (const char*)QString("===\t%2:%3 %4")
       
    43               .arg(sourceID)
       
    44               .arg(lineNumber)
       
    45               .arg(message).toAscii();
       
    46       }
       
    47       QString m_prefix;
       
    48   };
       
    49   // ------------------------------
       
    50 
    41 
    51   class GWebPage : public QObject {
    42     QString m_prefix;
    52       Q_OBJECT
    43 };
    53     public:
       
    54       GWebPage(QWebPage *page) {
       
    55         m_page = page;
       
    56       }
       
    57 
    44 
    58       Q_PROPERTY(QString name READ objectName)  // JS API
    45 // ------------------------------
    59       Q_PROPERTY(QString title READ getTitle)  // JS API
    46 
    60       QString getTitle() {
    47 class GWebPage : public QObject {
       
    48     Q_OBJECT
       
    49 public:
       
    50     GWebPage(QWebPage *page) {
       
    51         m_page = page;  // take ownership
       
    52     }
       
    53 
       
    54     virtual ~GWebPage() {
       
    55         delete m_page;
       
    56     }
       
    57 
       
    58     Q_PROPERTY(QString name READ objectName)  // JS API
       
    59     Q_PROPERTY(QString title READ getTitle)  // JS API
       
    60     QString getTitle() {
    61         return m_page->mainFrame()->title();
    61         return m_page->mainFrame()->title();
    62       }
    62     }
    63 
    63 
    64       QWebPage *page() { return m_page; }
    64     QWebPage *page() { return m_page; }
    65       operator QWebPage *() { return m_page; }
    65     operator QWebPage *() { return m_page; }
    66 
    66 
    67       void dump() {
    67     void dump() {
    68         qDebug() << "GWebPage::dump: " << this;
    68         qDebug() << "GWebPage::dump: " << this;
    69         qDebug() << "   page=" << (m_page ? m_page : 0);
    69         qDebug() << "   page=" << (m_page ? m_page : 0);
    70       }
    70     }
    71 
    71 
    72     protected:
    72 signals:
    73       QWebPage *m_page;
    73     /// Triggered by a QContextEvent such as a long-press or right mouse button click.
    74   };
    74     void contextEvent(::WebViewEventContext *context);
       
    75 #ifdef BEDROCK_TILED_BACKING_STORE
       
    76 public slots:
       
    77 #else
       
    78 protected slots:
       
    79 #endif
       
    80     void onContextEvent(::WebViewEventContext *context) { emit contextEvent(context); }
    75 
    81 
    76   // ------------------------------
    82 protected:
    77   /*! \ingroup JavascriptAPI
    83     QWebPage *m_page;  // owned
    78    * \brief A content view that has full access to the Javascript APIs.
       
    79    *
       
    80    * Example code to load an HTML file into a super page:
       
    81    * \code
       
    82    * window.views.WebView.createSuperPage("BookmarkView", true);
       
    83    * window.views.WebView.BookmarkView.load("./chrome/BookmarkView.html");
       
    84    * \endcode
       
    85    */
       
    86   class GSuperWebPage : public GWebPage {
       
    87       Q_OBJECT
       
    88     public:
       
    89       GSuperWebPage(WebPageWrapper *page, ChromeWidget *chromeWidget)
       
    90         : GWebPage(page),
       
    91           m_chromeWidget(chromeWidget)
       
    92       {
       
    93         if(!m_page) {
       
    94           m_page = new WebPageWrapper(this, "Superpage javascript error");
       
    95         }
       
    96         qDebug() << "GSuperWebPage::GSuperWebPage: page=" << GWebPage::page();
       
    97         connect(GWebPage::page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(onJavaScriptWindowObjectCleared()));
       
    98       }
       
    99     public slots:
       
   100       void load(const QString &url) {   // JS API
       
   101         qDebug() << "GSuperWebPage::load: " << url;
       
   102         page()->mainFrame()->load(url);
       
   103       }
       
   104     private slots:
       
   105       void onJavaScriptWindowObjectCleared() {
       
   106         qDebug() << "GSuperWebPage::onJavaScriptWindowObjectCleared: " << objectName();
       
   107         if(m_chromeWidget)
       
   108           m_chromeWidget->exportJSObjectsToPage(m_page);
       
   109       }
       
   110 
    84 
   111     private:
    85     friend class GWebContentViewWidget;
   112       ChromeWidget *m_chromeWidget;  // not owned
    86 };
   113   };
    87 
   114 }
    88 }
   115 
    89 
   116 #endif // __GINEBRA_GWEBPAGE_H__
    90 #endif // __GINEBRA_GWEBPAGE_H__