File indexing completed on 2024-11-24 04:53:26

0001 /*
0002     Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License as published by the Free Software Foundation; either
0007     version 2 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Library General Public License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to
0016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018 */
0019 
0020 
0021 #ifndef qdeclarativewebview_p_h
0022 #define qdeclarativewebview_p_h
0023 
0024 #include <QtCore/QBasicTimer>
0025 #include <QtCore/QUrl>
0026 #include <QtDeclarative/QDeclarativeItem>
0027 #include <QtGui/QAction>
0028 #include <QtNetwork/QNetworkAccessManager>
0029 #include "qgraphicswebview.h"
0030 #include "qwebpage.h"
0031 
0032 
0033 QT_BEGIN_HEADER
0034 
0035 class QWebHistory;
0036 class QWebSettings;
0037 
0038 QT_MODULE(Declarative)
0039 class TrojitaDeclarativeWebSettings;
0040 class QNetworkRequest;
0041 class TrojitaQNAMDeclarativeWebView;
0042 class TrojitaQNAMDeclarativeWebViewPrivate;
0043 
0044 class TrojitaQNAMDeclarativeWebPage : public QWebPage {
0045     Q_OBJECT
0046 public:
0047     explicit TrojitaQNAMDeclarativeWebPage(TrojitaQNAMDeclarativeWebView *parent);
0048     ~TrojitaQNAMDeclarativeWebPage();
0049 protected:
0050     QWebPage *createWindow(WebWindowType type);
0051     QString chooseFile(QWebFrame *originatingFrame, const QString& oldFile);
0052     void javaScriptAlert(QWebFrame *originatingFrame, const QString& msg);
0053     bool javaScriptConfirm(QWebFrame *originatingFrame, const QString& msg);
0054     bool javaScriptPrompt(QWebFrame *originatingFrame, const QString& msg, const QString& defaultValue, QString* result);
0055 
0056 private:
0057     TrojitaQNAMDeclarativeWebView *viewItem();
0058 };
0059 
0060 class TrojitaQNAMGraphicsWebView : public QGraphicsWebView {
0061     Q_OBJECT
0062 public:
0063     explicit TrojitaQNAMGraphicsWebView(TrojitaQNAMDeclarativeWebView* parent = 0);
0064 protected:
0065     void mousePressEvent(QGraphicsSceneMouseEvent* event);
0066     void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
0067     void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
0068     void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
0069     void timerEvent(QTimerEvent* event);
0070     bool sceneEvent(QEvent *event);
0071 
0072 Q_SIGNALS:
0073     void doubleClick(int clickX, int clickY);
0074 private:
0075     TrojitaQNAMDeclarativeWebView *parent;
0076     QPointF pressPoint;
0077     QBasicTimer pressTimer;
0078     int pressTime; // milliseconds before the touch event becomes a "tap and hold"
0079     friend class TrojitaQNAMDeclarativeWebView;
0080 };
0081 
0082 class TrojitaQNAMDeclarativeWebViewAttached;
0083 
0084 // TODO: browser plugins
0085 
0086 class TrojitaQNAMDeclarativeWebView : public QDeclarativeItem {
0087     Q_OBJECT
0088 
0089     Q_ENUMS(Status SelectionMode)
0090 
0091     Q_PROPERTY(QString title READ title NOTIFY titleChanged)
0092     Q_PROPERTY(QPixmap icon READ icon NOTIFY iconChanged)
0093     Q_PROPERTY(QString statusText READ statusText NOTIFY statusTextChanged)
0094 
0095     Q_PROPERTY(QString html READ html WRITE setHtml NOTIFY htmlChanged)
0096 
0097     Q_PROPERTY(int pressGrabTime READ pressGrabTime WRITE setPressGrabTime NOTIFY pressGrabTimeChanged)
0098 
0099     Q_PROPERTY(int preferredWidth READ preferredWidth WRITE setPreferredWidth NOTIFY preferredWidthChanged)
0100     Q_PROPERTY(int preferredHeight READ preferredHeight WRITE setPreferredHeight NOTIFY preferredHeightChanged)
0101     Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
0102     Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
0103     Q_PROPERTY(Status status READ status NOTIFY statusChanged)
0104 
0105 #ifndef QT_NO_ACTION
0106     Q_PROPERTY(QAction* reload READ reloadAction CONSTANT)
0107     Q_PROPERTY(QAction* back READ backAction CONSTANT)
0108     Q_PROPERTY(QAction* forward READ forwardAction CONSTANT)
0109     Q_PROPERTY(QAction* stop READ stopAction CONSTANT)
0110 #endif
0111 
0112     Q_PROPERTY(TrojitaDeclarativeWebSettings* settings READ settingsObject CONSTANT)
0113 
0114     Q_PROPERTY(QDeclarativeListProperty<QObject> javaScriptWindowObjects READ javaScriptWindowObjects CONSTANT)
0115 
0116     Q_PROPERTY(QDeclarativeComponent* newWindowComponent READ newWindowComponent WRITE setNewWindowComponent NOTIFY newWindowComponentChanged)
0117     Q_PROPERTY(QDeclarativeItem* newWindowParent READ newWindowParent WRITE setNewWindowParent NOTIFY newWindowParentChanged)
0118 
0119     Q_PROPERTY(bool renderingEnabled READ renderingEnabled WRITE setRenderingEnabled NOTIFY renderingEnabledChanged)
0120 
0121     Q_PROPERTY(QSize contentsSize READ contentsSize NOTIFY contentsSizeChanged)
0122     Q_PROPERTY(qreal contentsScale READ contentsScale WRITE setContentsScale NOTIFY contentsScaleChanged)
0123 
0124     Q_PROPERTY(QNetworkAccessManager* networkAccessManager READ networkAccessManager WRITE setNetworkAccessManager)
0125 
0126 public:
0127     explicit TrojitaQNAMDeclarativeWebView(QDeclarativeItem *parent = 0);
0128     ~TrojitaQNAMDeclarativeWebView();
0129 
0130     QUrl url() const;
0131     void setUrl(const QUrl &);
0132 
0133     QString title() const;
0134 
0135     QPixmap icon() const;
0136 
0137     Q_INVOKABLE bool heuristicZoom(int clickX, int clickY, qreal maxzoom);
0138     QRect elementAreaAt(int x, int y, int minwidth, int minheight) const;
0139 
0140     int pressGrabTime() const;
0141     void setPressGrabTime(int);
0142 
0143     int preferredWidth() const;
0144     void setPreferredWidth(int);
0145     int preferredHeight() const;
0146     void setPreferredHeight(int);
0147 
0148     enum Status { Null, Ready, Loading, Error };
0149     Status status() const;
0150     qreal progress() const;
0151     QString statusText() const;
0152 
0153 #ifndef QT_NO_ACTION
0154     QAction *reloadAction() const;
0155     QAction *backAction() const;
0156     QAction *forwardAction() const;
0157     QAction *stopAction() const;
0158 #endif
0159 
0160     QWebPage *page() const;
0161     void setPage(QWebPage *page);
0162 
0163     void load(const QNetworkRequest &request,
0164               QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation,
0165               const QByteArray &body = QByteArray());
0166 
0167     QString html() const;
0168 
0169     void setHtml(const QString &html, const QUrl &baseUrl = QUrl());
0170     void setContent(const QByteArray &data, const QString &mimeType = QString(), const QUrl &baseUrl = QUrl());
0171 
0172     QWebHistory* history() const;
0173     QWebSettings* settings() const;
0174     TrojitaDeclarativeWebSettings *settingsObject() const;
0175 
0176     bool renderingEnabled() const;
0177     void setRenderingEnabled(bool);
0178 
0179     QDeclarativeListProperty<QObject> javaScriptWindowObjects();
0180 
0181     static TrojitaQNAMDeclarativeWebViewAttached* qmlAttachedProperties(QObject*);
0182 
0183     QDeclarativeComponent *newWindowComponent() const;
0184     void setNewWindowComponent(QDeclarativeComponent *newWindow);
0185     QDeclarativeItem* newWindowParent() const;
0186     void setNewWindowParent(QDeclarativeItem* newWindow);
0187 
0188     bool isComponentCompletePublic() const { return isComponentComplete(); }
0189 
0190     QSize contentsSize() const;
0191 
0192     void setContentsScale(qreal scale);
0193     qreal contentsScale() const;
0194 
0195     QNetworkAccessManager *networkAccessManager() const;
0196     void setNetworkAccessManager(QNetworkAccessManager *manager);
0197 
0198 Q_SIGNALS:
0199     void preferredWidthChanged();
0200     void preferredHeightChanged();
0201     void urlChanged();
0202     void progressChanged();
0203     void statusChanged(Status);
0204     void titleChanged(const QString&);
0205     void iconChanged();
0206     void statusTextChanged();
0207     void htmlChanged();
0208     void pressGrabTimeChanged();
0209     void newWindowComponentChanged();
0210     void newWindowParentChanged();
0211     void renderingEnabledChanged();
0212     void contentsSizeChanged(const QSize&);
0213     void contentsScaleChanged();
0214 
0215     void loadStarted();
0216     void loadFinished();
0217     void loadFailed();
0218 
0219     void doubleClick(int clickX, int clickY);
0220 
0221     void zoomTo(qreal zoom, int centerX, int centerY);
0222 
0223     void alert(const QString& message);
0224 
0225 public Q_SLOTS:
0226     QVariant evaluateJavaScript(const QString&);
0227 
0228 private Q_SLOTS:
0229     void doLoadStarted();
0230     void doLoadProgress(int p);
0231     void doLoadFinished(bool ok);
0232     void setStatusText(const QString&);
0233     void windowObjectCleared();
0234     void pageUrlChanged();
0235     void initialLayout();
0236 
0237     void updateDeclarativeWebViewSize();
0238 
0239     virtual void geometryChanged(const QRectF &newGeometry,
0240                                  const QRectF &oldGeometry);
0241     TrojitaQNAMDeclarativeWebView* createWindow(QWebPage::WebWindowType type);
0242 
0243 private:
0244     void updateContentsSize();
0245     void init();
0246     virtual void componentComplete();
0247     Q_DISABLE_COPY(TrojitaQNAMDeclarativeWebView)
0248     TrojitaQNAMDeclarativeWebViewPrivate* d;
0249     QMouseEvent* sceneMouseEventToMouseEvent(QGraphicsSceneMouseEvent*);
0250     QMouseEvent* sceneHoverMoveEventToMouseEvent(QGraphicsSceneHoverEvent*);
0251     friend class TrojitaQNAMDeclarativeWebPage;
0252 };
0253 
0254 class TrojitaQNAMDeclarativeWebViewAttached : public QObject {
0255     Q_OBJECT
0256     Q_PROPERTY(QString windowObjectName READ windowObjectName WRITE setWindowObjectName)
0257 public:
0258     explicit TrojitaQNAMDeclarativeWebViewAttached(QObject* parent)
0259         : QObject(parent)
0260     {
0261     }
0262 
0263     QString windowObjectName() const
0264     {
0265         return m_windowObjectName;
0266     }
0267 
0268     void setWindowObjectName(const QString &n)
0269     {
0270         m_windowObjectName = n;
0271     }
0272 
0273 private:
0274     QString m_windowObjectName;
0275 };
0276 
0277 class TrojitaDeclarativeWebSettings : public QObject {
0278     Q_OBJECT
0279 
0280     Q_PROPERTY(QString standardFontFamily READ standardFontFamily WRITE setStandardFontFamily)
0281     Q_PROPERTY(QString fixedFontFamily READ fixedFontFamily WRITE setFixedFontFamily)
0282     Q_PROPERTY(QString serifFontFamily READ serifFontFamily WRITE setSerifFontFamily)
0283     Q_PROPERTY(QString sansSerifFontFamily READ sansSerifFontFamily WRITE setSansSerifFontFamily)
0284     Q_PROPERTY(QString cursiveFontFamily READ cursiveFontFamily WRITE setCursiveFontFamily)
0285     Q_PROPERTY(QString fantasyFontFamily READ fantasyFontFamily WRITE setFantasyFontFamily)
0286 
0287     Q_PROPERTY(QUrl userStyleSheetUrl READ userStyleSheetUrl WRITE setUserStyleSheetUrl)
0288 
0289     Q_PROPERTY(int minimumFontSize READ minimumFontSize WRITE setMinimumFontSize)
0290     Q_PROPERTY(int minimumLogicalFontSize READ minimumLogicalFontSize WRITE setMinimumLogicalFontSize)
0291     Q_PROPERTY(int defaultFontSize READ defaultFontSize WRITE setDefaultFontSize)
0292     Q_PROPERTY(int defaultFixedFontSize READ defaultFixedFontSize WRITE setDefaultFixedFontSize)
0293 
0294     Q_PROPERTY(bool autoLoadImages READ autoLoadImages WRITE setAutoLoadImages)
0295     Q_PROPERTY(bool javascriptEnabled READ javascriptEnabled WRITE setJavascriptEnabled)
0296     Q_PROPERTY(bool javaEnabled READ javaEnabled WRITE setJavaEnabled)
0297     Q_PROPERTY(bool pluginsEnabled READ pluginsEnabled WRITE setPluginsEnabled)
0298     Q_PROPERTY(bool privateBrowsingEnabled READ privateBrowsingEnabled WRITE setPrivateBrowsingEnabled)
0299     Q_PROPERTY(bool javascriptCanOpenWindows READ javascriptCanOpenWindows WRITE setJavascriptCanOpenWindows)
0300     Q_PROPERTY(bool javascriptCanAccessClipboard READ javascriptCanAccessClipboard WRITE setJavascriptCanAccessClipboard)
0301     Q_PROPERTY(bool developerExtrasEnabled READ developerExtrasEnabled WRITE setDeveloperExtrasEnabled)
0302     Q_PROPERTY(bool linksIncludedInFocusChain READ linksIncludedInFocusChain WRITE setLinksIncludedInFocusChain)
0303     Q_PROPERTY(bool zoomTextOnly READ zoomTextOnly WRITE setZoomTextOnly)
0304     Q_PROPERTY(bool printElementBackgrounds READ printElementBackgrounds WRITE setPrintElementBackgrounds)
0305     Q_PROPERTY(bool offlineStorageDatabaseEnabled READ offlineStorageDatabaseEnabled WRITE setOfflineStorageDatabaseEnabled)
0306     Q_PROPERTY(bool offlineWebApplicationCacheEnabled READ offlineWebApplicationCacheEnabled WRITE setOfflineWebApplicationCacheEnabled)
0307     Q_PROPERTY(bool localStorageDatabaseEnabled READ localStorageDatabaseEnabled WRITE setLocalStorageDatabaseEnabled)
0308     Q_PROPERTY(bool localContentCanAccessRemoteUrls READ localContentCanAccessRemoteUrls WRITE setLocalContentCanAccessRemoteUrls)
0309 
0310 public:
0311     TrojitaDeclarativeWebSettings() {}
0312 
0313     QString standardFontFamily() const { return s->fontFamily(QWebSettings::StandardFont); }
0314     void setStandardFontFamily(const QString& f) { s->setFontFamily(QWebSettings::StandardFont, f); }
0315     QString fixedFontFamily() const { return s->fontFamily(QWebSettings::FixedFont); }
0316     void setFixedFontFamily(const QString& f) { s->setFontFamily(QWebSettings::FixedFont, f); }
0317     QString serifFontFamily() const { return s->fontFamily(QWebSettings::SerifFont); }
0318     void setSerifFontFamily(const QString& f) { s->setFontFamily(QWebSettings::SerifFont, f); }
0319     QString sansSerifFontFamily() const { return s->fontFamily(QWebSettings::SansSerifFont); }
0320     void setSansSerifFontFamily(const QString& f) { s->setFontFamily(QWebSettings::SansSerifFont, f); }
0321     QString cursiveFontFamily() const { return s->fontFamily(QWebSettings::CursiveFont); }
0322     void setCursiveFontFamily(const QString& f) { s->setFontFamily(QWebSettings::CursiveFont, f); }
0323     QString fantasyFontFamily() const { return s->fontFamily(QWebSettings::FantasyFont); }
0324     void setFantasyFontFamily(const QString& f) { s->setFontFamily(QWebSettings::FantasyFont, f); }
0325     QUrl userStyleSheetUrl() const { return s->userStyleSheetUrl(); }
0326     void setUserStyleSheetUrl(const QUrl &url) { s->setUserStyleSheetUrl(url); }
0327 
0328     int minimumFontSize() const { return s->fontSize(QWebSettings::MinimumFontSize); }
0329     void setMinimumFontSize(int size) { s->setFontSize(QWebSettings::MinimumFontSize, size); }
0330     int minimumLogicalFontSize() const { return s->fontSize(QWebSettings::MinimumLogicalFontSize); }
0331     void setMinimumLogicalFontSize(int size) { s->setFontSize(QWebSettings::MinimumLogicalFontSize, size); }
0332     int defaultFontSize() const { return s->fontSize(QWebSettings::DefaultFontSize); }
0333     void setDefaultFontSize(int size) { s->setFontSize(QWebSettings::DefaultFontSize, size); }
0334     int defaultFixedFontSize() const { return s->fontSize(QWebSettings::DefaultFixedFontSize); }
0335     void setDefaultFixedFontSize(int size) { s->setFontSize(QWebSettings::DefaultFixedFontSize, size); }
0336 
0337     bool autoLoadImages() const { return s->testAttribute(QWebSettings::AutoLoadImages); }
0338     void setAutoLoadImages(bool on) { s->setAttribute(QWebSettings::AutoLoadImages, on); }
0339     bool javascriptEnabled() const { return s->testAttribute(QWebSettings::JavascriptEnabled); }
0340     void setJavascriptEnabled(bool on) { s->setAttribute(QWebSettings::JavascriptEnabled, on); }
0341     bool javaEnabled() const { return s->testAttribute(QWebSettings::JavaEnabled); }
0342     void setJavaEnabled(bool on) { s->setAttribute(QWebSettings::JavaEnabled, on); }
0343     bool pluginsEnabled() const { return s->testAttribute(QWebSettings::PluginsEnabled); }
0344     void setPluginsEnabled(bool on) { s->setAttribute(QWebSettings::PluginsEnabled, on); }
0345     bool privateBrowsingEnabled() const { return s->testAttribute(QWebSettings::PrivateBrowsingEnabled); }
0346     void setPrivateBrowsingEnabled(bool on) { s->setAttribute(QWebSettings::PrivateBrowsingEnabled, on); }
0347     bool javascriptCanOpenWindows() const { return s->testAttribute(QWebSettings::JavascriptCanOpenWindows); }
0348     void setJavascriptCanOpenWindows(bool on) { s->setAttribute(QWebSettings::JavascriptCanOpenWindows, on); }
0349     bool javascriptCanAccessClipboard() const { return s->testAttribute(QWebSettings::JavascriptCanAccessClipboard); }
0350     void setJavascriptCanAccessClipboard(bool on) { s->setAttribute(QWebSettings::JavascriptCanAccessClipboard, on); }
0351     bool developerExtrasEnabled() const { return s->testAttribute(QWebSettings::DeveloperExtrasEnabled); }
0352     void setDeveloperExtrasEnabled(bool on) { s->setAttribute(QWebSettings::DeveloperExtrasEnabled, on); }
0353     bool linksIncludedInFocusChain() const { return s->testAttribute(QWebSettings::LinksIncludedInFocusChain); }
0354     void setLinksIncludedInFocusChain(bool on) { s->setAttribute(QWebSettings::LinksIncludedInFocusChain, on); }
0355     bool zoomTextOnly() const { return s->testAttribute(QWebSettings::ZoomTextOnly); }
0356     void setZoomTextOnly(bool on) { s->setAttribute(QWebSettings::ZoomTextOnly, on); }
0357     bool printElementBackgrounds() const { return s->testAttribute(QWebSettings::PrintElementBackgrounds); }
0358     void setPrintElementBackgrounds(bool on) { s->setAttribute(QWebSettings::PrintElementBackgrounds, on); }
0359     bool offlineStorageDatabaseEnabled() const { return s->testAttribute(QWebSettings::OfflineStorageDatabaseEnabled); }
0360     void setOfflineStorageDatabaseEnabled(bool on) { s->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, on); }
0361     bool offlineWebApplicationCacheEnabled() const { return s->testAttribute(QWebSettings::OfflineWebApplicationCacheEnabled); }
0362     void setOfflineWebApplicationCacheEnabled(bool on) { s->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, on); }
0363     bool localStorageDatabaseEnabled() const { return s->testAttribute(QWebSettings::LocalStorageDatabaseEnabled); }
0364     void setLocalStorageDatabaseEnabled(bool on) { s->setAttribute(QWebSettings::LocalStorageDatabaseEnabled, on); }
0365     bool localContentCanAccessRemoteUrls() const { return s->testAttribute(QWebSettings::LocalContentCanAccessRemoteUrls); }
0366     void setLocalContentCanAccessRemoteUrls(bool on) { s->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, on); }
0367 
0368     QWebSettings *s;
0369 };
0370 
0371 QML_DECLARE_TYPE(TrojitaQNAMDeclarativeWebPage)
0372 QML_DECLARE_TYPE(QNetworkAccessManager)
0373 QML_DECLARE_TYPEINFO(TrojitaQNAMDeclarativeWebView, QML_HAS_ATTACHED_PROPERTIES)
0374 
0375 QT_END_HEADER
0376 
0377 #endif