ginebra2/GWebPage.h
changeset 5 0f2326c2a325
parent 0 1450b09d0cfd
child 6 1c3b8676e58c
equal deleted inserted replaced
1:b0dd75e285d2 5:0f2326c2a325
     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 
    75 
    76   // ------------------------------
    76 protected slots:
    77   /*! \ingroup JavascriptAPI
    77     void onContextEvent(::WebViewEventContext *context) { emit contextEvent(context); }
    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 
    78 
   111     private:
    79 protected:
   112       ChromeWidget *m_chromeWidget;  // not owned
    80     QWebPage *m_page;  // owned
   113   };
    81 
       
    82     friend class GWebContentViewWidget;
       
    83 };
       
    84 
       
    85 // ------------------------------
       
    86 /*! \ingroup JavascriptAPI
       
    87 * \brief A content view that has full access to the Javascript APIs.
       
    88 *
       
    89 * Example code to load an HTML file into a super page:
       
    90 * \code
       
    91 * window.views.WebView.createSuperPage("BookmarkView", true);
       
    92 * window.views.WebView.BookmarkView.load("./chrome/BookmarkView.html");
       
    93 * \endcode
       
    94 */
       
    95 class GSuperWebPage : public GWebPage {
       
    96     Q_OBJECT
       
    97 public:
       
    98     GSuperWebPage(WebPageWrapper *page, ChromeWidget *chromeWidget);
       
    99 
       
   100 public slots:
       
   101     void load(const QString &url);
       
   102 
       
   103 signals:
       
   104     /*!
       
   105     * Triggered by the javascript code within the superpage when it wants a context menu to be displayed by
       
   106     * the chrome's javascript.
       
   107     *
       
   108     * The normal chain of events is:
       
   109     * \li User executes a long-press (or RMB click).
       
   110     * \li Qt sends QContextMenuEvent from QWebView.
       
   111     * \li GWebContentViewWidget::contextMenuEvent is called, which passes the event to the superpage
       
   112     * (if one is currently displayed).
       
   113     * \li The superpage emits \c contextEvent().
       
   114     * \li The context event handler in the superpage's javascript determines what was clicked on
       
   115     * and emits \c showContextMenu() from the superpage.
       
   116     * \li Javascript \c showContextMenu signal handler in the chrome is called which then displays the context menu.
       
   117     */
       
   118     void showContextMenu(QVariant obj);
       
   119 
       
   120 private slots:
       
   121     void onJavaScriptWindowObjectCleared();
       
   122 
       
   123 private:
       
   124     ChromeWidget *m_chromeWidget;  // not owned
       
   125 };
       
   126 
   114 }
   127 }
   115 
   128 
   116 #endif // __GINEBRA_GWEBPAGE_H__
   129 #endif // __GINEBRA_GWEBPAGE_H__