Skip to content

Commit b1b60c0

Browse files
committed
added custom Quick1 webview component
1 parent 0a540ef commit b1b60c0

File tree

2 files changed

+1434
-0
lines changed

2 files changed

+1434
-0
lines changed
Lines changed: 354 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,354 @@
1+
#ifndef qdeclarativewebview_p_h
2+
#define qdeclarativewebview_p_h
3+
4+
#include <QtGui/QAction>
5+
#include <QtDeclarative/QDeclarativeItem>
6+
#include <QtCore/QBasicTimer>
7+
#include <QtCore/QUrl>
8+
#include <QtNetwork/QNetworkAccessManager>
9+
#include <QtWebKit/QGraphicsWebView>
10+
#include <QtWebKit/QtWebKit>
11+
12+
13+
class QWebHistory;
14+
class QWebSettings;
15+
16+
QT_BEGIN_NAMESPACE
17+
18+
QT_MODULE(Declarative)
19+
class QDeclarativeWebSettings;
20+
class QDeclarativeWebViewPrivate;
21+
class QNetworkRequest;
22+
class QDeclarativeWebView;
23+
class QDeclarativeWebViewPrivate;
24+
25+
class QDeclarativeWebPage : public QWebPage {
26+
Q_OBJECT
27+
public:
28+
explicit QDeclarativeWebPage(QDeclarativeWebView *parent);
29+
~QDeclarativeWebPage();
30+
protected:
31+
QWebPage *createWindow(WebWindowType type);
32+
QString chooseFile(QWebFrame *originatingFrame, const QString& oldFile);
33+
void javaScriptAlert(QWebFrame *originatingFrame, const QString& msg);
34+
bool javaScriptConfirm(QWebFrame *originatingFrame, const QString& msg);
35+
bool javaScriptPrompt(QWebFrame *originatingFrame, const QString& msg, const QString& defaultValue, QString* result);
36+
37+
private:
38+
QDeclarativeWebView *viewItem();
39+
};
40+
41+
class GraphicsWebView : public QGraphicsWebView {
42+
Q_OBJECT
43+
public:
44+
GraphicsWebView(QDeclarativeWebView* parent = 0);
45+
protected:
46+
void mousePressEvent(QGraphicsSceneMouseEvent* event);
47+
void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
48+
void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
49+
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
50+
void timerEvent(QTimerEvent* event);
51+
bool sceneEvent(QEvent *event);
52+
53+
Q_SIGNALS:
54+
void doubleClick(int clickX, int clickY);
55+
private:
56+
QDeclarativeWebView *parent;
57+
QPointF pressPoint;
58+
QBasicTimer pressTimer;
59+
int pressTime; // milliseconds before the touch event becomes a "tap and hold"
60+
friend class QDeclarativeWebView;
61+
};
62+
63+
class QDeclarativeWebViewAttached;
64+
65+
// TODO: browser plugins
66+
67+
class QDeclarativeWebView : public QDeclarativeItem {
68+
Q_OBJECT
69+
70+
Q_ENUMS(Status SelectionMode)
71+
72+
Q_PROPERTY(QString title READ title NOTIFY titleChanged)
73+
Q_PROPERTY(QPixmap icon READ icon NOTIFY iconChanged)
74+
Q_PROPERTY(QString statusText READ statusText NOTIFY statusTextChanged)
75+
76+
Q_PROPERTY(QString html READ html WRITE setHtml NOTIFY htmlChanged)
77+
78+
Q_PROPERTY(int pressGrabTime READ pressGrabTime WRITE setPressGrabTime NOTIFY pressGrabTimeChanged)
79+
80+
Q_PROPERTY(int preferredWidth READ preferredWidth WRITE setPreferredWidth NOTIFY preferredWidthChanged)
81+
Q_PROPERTY(int preferredHeight READ preferredHeight WRITE setPreferredHeight NOTIFY preferredHeightChanged)
82+
Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
83+
Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
84+
Q_PROPERTY(Status status READ status NOTIFY statusChanged)
85+
86+
#ifndef QT_NO_ACTION
87+
Q_PROPERTY(QAction* reload READ reloadAction CONSTANT)
88+
Q_PROPERTY(QAction* back READ backAction CONSTANT)
89+
Q_PROPERTY(QAction* forward READ forwardAction CONSTANT)
90+
Q_PROPERTY(QAction* stop READ stopAction CONSTANT)
91+
#endif
92+
93+
Q_PROPERTY(QDeclarativeWebSettings* settings READ settingsObject CONSTANT)
94+
95+
Q_PROPERTY(QDeclarativeListProperty<QObject> javaScriptWindowObjects READ javaScriptWindowObjects CONSTANT)
96+
97+
Q_PROPERTY(QDeclarativeComponent* newWindowComponent READ newWindowComponent WRITE setNewWindowComponent NOTIFY newWindowComponentChanged)
98+
Q_PROPERTY(QDeclarativeItem* newWindowParent READ newWindowParent WRITE setNewWindowParent NOTIFY newWindowParentChanged)
99+
100+
Q_PROPERTY(bool renderingEnabled READ renderingEnabled WRITE setRenderingEnabled NOTIFY renderingEnabledChanged)
101+
102+
Q_PROPERTY(QSize contentsSize READ contentsSize NOTIFY contentsSizeChanged)
103+
Q_PROPERTY(qreal contentsScale READ contentsScale WRITE setContentsScale NOTIFY contentsScaleChanged)
104+
Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged REVISION 1)
105+
106+
public:
107+
QDeclarativeWebView(QDeclarativeItem *parent = 0);
108+
~QDeclarativeWebView();
109+
110+
QUrl url() const;
111+
void setUrl(const QUrl &);
112+
113+
QString title() const;
114+
115+
QPixmap icon() const;
116+
117+
Q_INVOKABLE bool heuristicZoom(int clickX, int clickY, qreal maxzoom);
118+
QRect elementAreaAt(int x, int y, int minwidth, int minheight) const;
119+
120+
int pressGrabTime() const;
121+
void setPressGrabTime(int);
122+
123+
int preferredWidth() const;
124+
void setPreferredWidth(int);
125+
int preferredHeight() const;
126+
void setPreferredHeight(int);
127+
128+
enum Status { Null, Ready, Loading, Error };
129+
Status status() const;
130+
qreal progress() const;
131+
QString statusText() const;
132+
133+
#ifndef QT_NO_ACTION
134+
QAction *reloadAction() const;
135+
QAction *backAction() const;
136+
QAction *forwardAction() const;
137+
QAction *stopAction() const;
138+
#endif
139+
140+
QWebPage *page() const;
141+
void setPage(QWebPage *page);
142+
143+
void load(const QNetworkRequest &request,
144+
QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation,
145+
const QByteArray &body = QByteArray());
146+
147+
QString html() const;
148+
149+
void setHtml(const QString &html, const QUrl &baseUrl = QUrl());
150+
void setContent(const QByteArray &data, const QString &mimeType = QString(), const QUrl &baseUrl = QUrl());
151+
152+
QWebHistory* history() const;
153+
QWebSettings* settings() const;
154+
QDeclarativeWebSettings *settingsObject() const;
155+
156+
bool renderingEnabled() const;
157+
void setRenderingEnabled(bool);
158+
159+
QDeclarativeListProperty<QObject> javaScriptWindowObjects();
160+
161+
static QDeclarativeWebViewAttached* qmlAttachedProperties(QObject*);
162+
163+
QDeclarativeComponent *newWindowComponent() const;
164+
void setNewWindowComponent(QDeclarativeComponent *newWindow);
165+
QDeclarativeItem* newWindowParent() const;
166+
void setNewWindowParent(QDeclarativeItem* newWindow);
167+
168+
bool isComponentCompletePublic() const { return isComponentComplete(); }
169+
170+
QSize contentsSize() const;
171+
172+
void setContentsScale(qreal scale);
173+
qreal contentsScale() const;
174+
175+
Q_REVISION(1) QColor backgroundColor() const;
176+
Q_REVISION(1) void setBackgroundColor(const QColor&);
177+
178+
Q_SIGNALS:
179+
void preferredWidthChanged();
180+
void preferredHeightChanged();
181+
void urlChanged();
182+
void progressChanged();
183+
void statusChanged(Status);
184+
void titleChanged(const QString&);
185+
void iconChanged();
186+
void statusTextChanged();
187+
void htmlChanged();
188+
void pressGrabTimeChanged();
189+
void newWindowComponentChanged();
190+
void newWindowParentChanged();
191+
void renderingEnabledChanged();
192+
void contentsSizeChanged(const QSize&);
193+
void contentsScaleChanged();
194+
void backgroundColorChanged();
195+
196+
void loadStarted();
197+
void loadFinished();
198+
void loadFailed();
199+
200+
void doubleClick(int clickX, int clickY);
201+
202+
void zoomTo(qreal zoom, int centerX, int centerY);
203+
204+
void alert(const QString& message);
205+
206+
public Q_SLOTS:
207+
QVariant evaluateJavaScript(const QString&);
208+
209+
private Q_SLOTS:
210+
void doLoadStarted();
211+
void doLoadProgress(int p);
212+
void doLoadFinished(bool ok);
213+
void setStatusText(const QString&);
214+
void windowObjectCleared();
215+
void pageUrlChanged();
216+
void initialLayout();
217+
218+
void updateDeclarativeWebViewSize();
219+
220+
virtual void geometryChanged(const QRectF &newGeometry,
221+
const QRectF &oldGeometry);
222+
QDeclarativeWebView* createWindow(QWebPage::WebWindowType type);
223+
224+
private:
225+
void updateContentsSize();
226+
void init();
227+
virtual void componentComplete();
228+
Q_DISABLE_COPY(QDeclarativeWebView)
229+
QDeclarativeWebViewPrivate* d;
230+
QMouseEvent* sceneMouseEventToMouseEvent(QGraphicsSceneMouseEvent*);
231+
QMouseEvent* sceneHoverMoveEventToMouseEvent(QGraphicsSceneHoverEvent*);
232+
friend class QDeclarativeWebPage;
233+
};
234+
235+
class QDeclarativeWebViewAttached : public QObject {
236+
Q_OBJECT
237+
Q_PROPERTY(QString windowObjectName READ windowObjectName WRITE setWindowObjectName)
238+
public:
239+
QDeclarativeWebViewAttached(QObject* parent)
240+
: QObject(parent)
241+
{
242+
}
243+
244+
QString windowObjectName() const
245+
{
246+
return m_windowObjectName;
247+
}
248+
249+
void setWindowObjectName(const QString &n)
250+
{
251+
m_windowObjectName = n;
252+
}
253+
254+
private:
255+
QString m_windowObjectName;
256+
};
257+
258+
class QDeclarativeWebSettings : public QObject {
259+
Q_OBJECT
260+
261+
Q_PROPERTY(QString standardFontFamily READ standardFontFamily WRITE setStandardFontFamily)
262+
Q_PROPERTY(QString fixedFontFamily READ fixedFontFamily WRITE setFixedFontFamily)
263+
Q_PROPERTY(QString serifFontFamily READ serifFontFamily WRITE setSerifFontFamily)
264+
Q_PROPERTY(QString sansSerifFontFamily READ sansSerifFontFamily WRITE setSansSerifFontFamily)
265+
Q_PROPERTY(QString cursiveFontFamily READ cursiveFontFamily WRITE setCursiveFontFamily)
266+
Q_PROPERTY(QString fantasyFontFamily READ fantasyFontFamily WRITE setFantasyFontFamily)
267+
268+
Q_PROPERTY(int minimumFontSize READ minimumFontSize WRITE setMinimumFontSize)
269+
Q_PROPERTY(int minimumLogicalFontSize READ minimumLogicalFontSize WRITE setMinimumLogicalFontSize)
270+
Q_PROPERTY(int defaultFontSize READ defaultFontSize WRITE setDefaultFontSize)
271+
Q_PROPERTY(int defaultFixedFontSize READ defaultFixedFontSize WRITE setDefaultFixedFontSize)
272+
273+
Q_PROPERTY(bool autoLoadImages READ autoLoadImages WRITE setAutoLoadImages)
274+
Q_PROPERTY(bool javascriptEnabled READ javascriptEnabled WRITE setJavascriptEnabled)
275+
Q_PROPERTY(bool javaEnabled READ javaEnabled WRITE setJavaEnabled)
276+
Q_PROPERTY(bool pluginsEnabled READ pluginsEnabled WRITE setPluginsEnabled)
277+
Q_PROPERTY(bool privateBrowsingEnabled READ privateBrowsingEnabled WRITE setPrivateBrowsingEnabled)
278+
Q_PROPERTY(bool javascriptCanOpenWindows READ javascriptCanOpenWindows WRITE setJavascriptCanOpenWindows)
279+
Q_PROPERTY(bool javascriptCanAccessClipboard READ javascriptCanAccessClipboard WRITE setJavascriptCanAccessClipboard)
280+
Q_PROPERTY(bool developerExtrasEnabled READ developerExtrasEnabled WRITE setDeveloperExtrasEnabled)
281+
Q_PROPERTY(bool linksIncludedInFocusChain READ linksIncludedInFocusChain WRITE setLinksIncludedInFocusChain)
282+
Q_PROPERTY(bool zoomTextOnly READ zoomTextOnly WRITE setZoomTextOnly)
283+
Q_PROPERTY(bool printElementBackgrounds READ printElementBackgrounds WRITE setPrintElementBackgrounds)
284+
Q_PROPERTY(bool offlineStorageDatabaseEnabled READ offlineStorageDatabaseEnabled WRITE setOfflineStorageDatabaseEnabled)
285+
Q_PROPERTY(bool offlineWebApplicationCacheEnabled READ offlineWebApplicationCacheEnabled WRITE setOfflineWebApplicationCacheEnabled)
286+
Q_PROPERTY(bool localStorageDatabaseEnabled READ localStorageDatabaseEnabled WRITE setLocalStorageDatabaseEnabled)
287+
Q_PROPERTY(bool localContentCanAccessRemoteUrls READ localContentCanAccessRemoteUrls WRITE setLocalContentCanAccessRemoteUrls)
288+
289+
public:
290+
QDeclarativeWebSettings() {}
291+
292+
QString standardFontFamily() const { return s->fontFamily(QWebSettings::StandardFont); }
293+
void setStandardFontFamily(const QString& f) { s->setFontFamily(QWebSettings::StandardFont, f); }
294+
QString fixedFontFamily() const { return s->fontFamily(QWebSettings::FixedFont); }
295+
void setFixedFontFamily(const QString& f) { s->setFontFamily(QWebSettings::FixedFont, f); }
296+
QString serifFontFamily() const { return s->fontFamily(QWebSettings::SerifFont); }
297+
void setSerifFontFamily(const QString& f) { s->setFontFamily(QWebSettings::SerifFont, f); }
298+
QString sansSerifFontFamily() const { return s->fontFamily(QWebSettings::SansSerifFont); }
299+
void setSansSerifFontFamily(const QString& f) { s->setFontFamily(QWebSettings::SansSerifFont, f); }
300+
QString cursiveFontFamily() const { return s->fontFamily(QWebSettings::CursiveFont); }
301+
void setCursiveFontFamily(const QString& f) { s->setFontFamily(QWebSettings::CursiveFont, f); }
302+
QString fantasyFontFamily() const { return s->fontFamily(QWebSettings::FantasyFont); }
303+
void setFantasyFontFamily(const QString& f) { s->setFontFamily(QWebSettings::FantasyFont, f); }
304+
305+
int minimumFontSize() const { return s->fontSize(QWebSettings::MinimumFontSize); }
306+
void setMinimumFontSize(int size) { s->setFontSize(QWebSettings::MinimumFontSize, size); }
307+
int minimumLogicalFontSize() const { return s->fontSize(QWebSettings::MinimumLogicalFontSize); }
308+
void setMinimumLogicalFontSize(int size) { s->setFontSize(QWebSettings::MinimumLogicalFontSize, size); }
309+
int defaultFontSize() const { return s->fontSize(QWebSettings::DefaultFontSize); }
310+
void setDefaultFontSize(int size) { s->setFontSize(QWebSettings::DefaultFontSize, size); }
311+
int defaultFixedFontSize() const { return s->fontSize(QWebSettings::DefaultFixedFontSize); }
312+
void setDefaultFixedFontSize(int size) { s->setFontSize(QWebSettings::DefaultFixedFontSize, size); }
313+
314+
bool autoLoadImages() const { return s->testAttribute(QWebSettings::AutoLoadImages); }
315+
void setAutoLoadImages(bool on) { s->setAttribute(QWebSettings::AutoLoadImages, on); }
316+
bool javascriptEnabled() const { return s->testAttribute(QWebSettings::JavascriptEnabled); }
317+
void setJavascriptEnabled(bool on) { s->setAttribute(QWebSettings::JavascriptEnabled, on); }
318+
bool javaEnabled() const { return s->testAttribute(QWebSettings::JavaEnabled); }
319+
void setJavaEnabled(bool on) { s->setAttribute(QWebSettings::JavaEnabled, on); }
320+
bool pluginsEnabled() const { return s->testAttribute(QWebSettings::PluginsEnabled); }
321+
void setPluginsEnabled(bool on) { s->setAttribute(QWebSettings::PluginsEnabled, on); }
322+
bool privateBrowsingEnabled() const { return s->testAttribute(QWebSettings::PrivateBrowsingEnabled); }
323+
void setPrivateBrowsingEnabled(bool on) { s->setAttribute(QWebSettings::PrivateBrowsingEnabled, on); }
324+
bool javascriptCanOpenWindows() const { return s->testAttribute(QWebSettings::JavascriptCanOpenWindows); }
325+
void setJavascriptCanOpenWindows(bool on) { s->setAttribute(QWebSettings::JavascriptCanOpenWindows, on); }
326+
bool javascriptCanAccessClipboard() const { return s->testAttribute(QWebSettings::JavascriptCanAccessClipboard); }
327+
void setJavascriptCanAccessClipboard(bool on) { s->setAttribute(QWebSettings::JavascriptCanAccessClipboard, on); }
328+
bool developerExtrasEnabled() const { return s->testAttribute(QWebSettings::DeveloperExtrasEnabled); }
329+
void setDeveloperExtrasEnabled(bool on) { s->setAttribute(QWebSettings::DeveloperExtrasEnabled, on); }
330+
bool linksIncludedInFocusChain() const { return s->testAttribute(QWebSettings::LinksIncludedInFocusChain); }
331+
void setLinksIncludedInFocusChain(bool on) { s->setAttribute(QWebSettings::LinksIncludedInFocusChain, on); }
332+
bool zoomTextOnly() const { return s->testAttribute(QWebSettings::ZoomTextOnly); }
333+
void setZoomTextOnly(bool on) { s->setAttribute(QWebSettings::ZoomTextOnly, on); }
334+
bool printElementBackgrounds() const { return s->testAttribute(QWebSettings::PrintElementBackgrounds); }
335+
void setPrintElementBackgrounds(bool on) { s->setAttribute(QWebSettings::PrintElementBackgrounds, on); }
336+
bool offlineStorageDatabaseEnabled() const { return s->testAttribute(QWebSettings::OfflineStorageDatabaseEnabled); }
337+
void setOfflineStorageDatabaseEnabled(bool on) { s->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, on); }
338+
bool offlineWebApplicationCacheEnabled() const { return s->testAttribute(QWebSettings::OfflineWebApplicationCacheEnabled); }
339+
void setOfflineWebApplicationCacheEnabled(bool on) { s->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, on); }
340+
bool localStorageDatabaseEnabled() const { return s->testAttribute(QWebSettings::LocalStorageDatabaseEnabled); }
341+
void setLocalStorageDatabaseEnabled(bool on) { s->setAttribute(QWebSettings::LocalStorageDatabaseEnabled, on); }
342+
bool localContentCanAccessRemoteUrls() const { return s->testAttribute(QWebSettings::LocalContentCanAccessRemoteUrls); }
343+
void setLocalContentCanAccessRemoteUrls(bool on) { s->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, on); }
344+
345+
QWebSettings *s;
346+
};
347+
348+
QT_END_NAMESPACE
349+
350+
QML_DECLARE_TYPE(QDeclarativeWebView)
351+
QML_DECLARE_TYPE(QDeclarativeWebSettings)
352+
QML_DECLARE_TYPEINFO(QDeclarativeWebView, QML_HAS_ATTACHED_PROPERTIES)
353+
354+
#endif

0 commit comments

Comments
 (0)