|
@@ -0,0 +1,192 @@
|
|
1
|
+@* Web Views in Typica.
|
|
2
|
+
|
|
3
|
+\noindent Typica makes extensive use of web views. All printing is currently
|
|
4
|
+handled by generating HTML with the required information, loading it into a web
|
|
5
|
+view, and using the print capabilities provided there. Reports are provided by
|
|
6
|
+running SQL queries, generating HTML, and displaying the results in a web view.
|
|
7
|
+Even the program'@q'@>s about window is primarily a web view.
|
|
8
|
+
|
|
9
|
+In order to simplify the implementation of certain features, we subclass
|
|
10
|
+|QWebView| and provide some additional functionality.
|
|
11
|
+
|
|
12
|
+@(webview.h@>=
|
|
13
|
+#include <QWebView>
|
|
14
|
+#include <QFile>
|
|
15
|
+#include <QMessageBox>
|
|
16
|
+#include <QDesktopServices>
|
|
17
|
+#include <QPrinter>
|
|
18
|
+#include <QPrintDialog>
|
|
19
|
+#include <QWebFrame>
|
|
20
|
+#include <QWebElement>
|
|
21
|
+
|
|
22
|
+#ifndef TypicaWebViewHeader
|
|
23
|
+#define TypicaWebViewHeader
|
|
24
|
+
|
|
25
|
+class TypicaWebView : public QWebView
|
|
26
|
+{
|
|
27
|
+ Q_OBJECT
|
|
28
|
+ public:
|
|
29
|
+ TypicaWebView();
|
|
30
|
+ Q_INVOKABLE void load(const QString &url);
|
|
31
|
+ Q_INVOKABLE void print();
|
|
32
|
+ Q_INVOKABLE void setHtml(const QString &html, const QUrl &baseUrl = QUrl());
|
|
33
|
+ Q_INVOKABLE void setContent(QIODevice *device);
|
|
34
|
+ Q_INVOKABLE QString saveXml();
|
|
35
|
+ signals:
|
|
36
|
+ void scriptLinkClicked(const QString &link);
|
|
37
|
+ private slots:
|
|
38
|
+ void linkDelegate(const QUrl &url);
|
|
39
|
+};
|
|
40
|
+
|
|
41
|
+#endif
|
|
42
|
+
|
|
43
|
+@ The implementation is in a separate file.
|
|
44
|
+
|
|
45
|
+@(webview.cpp@>=
|
|
46
|
+#include "webview.h"
|
|
47
|
+
|
|
48
|
+@<TypicaWebView implementation@>@;
|
|
49
|
+
|
|
50
|
+@ In the constructor we set up our link delegation policy.
|
|
51
|
+
|
|
52
|
+@<TypicaWebView implementation@>=
|
|
53
|
+TypicaWebView::TypicaWebView() : QWebView()
|
|
54
|
+{
|
|
55
|
+ page()->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
|
|
56
|
+ connect(page(), SIGNAL(linkClicked(QUrl)), this, SLOT(linkDelegate(QUrl)));
|
|
57
|
+}
|
|
58
|
+
|
|
59
|
+@ When a link is clicked, one of three things may happen. Certain reserved URLs
|
|
60
|
+will trigger an immediate event which is just handled internally without
|
|
61
|
+providing any kind of external notification. URLs that start with
|
|
62
|
+|"typica://script/"| will produce a signal containing the rest of the URL. This
|
|
63
|
+is intended for script code to intercept, interpret, and update the web view
|
|
64
|
+with the requested information. Everything else is passed to |QDesktopServices|
|
|
65
|
+which will defer to system-wide preferences for how to handle different link
|
|
66
|
+and file types.
|
|
67
|
+
|
|
68
|
+@<TypicaWebView implementation@>=
|
|
69
|
+void TypicaWebView::linkDelegate(const QUrl &url)
|
|
70
|
+{
|
|
71
|
+ if(url.scheme() == "typica")
|
|
72
|
+ {
|
|
73
|
+ QString address(url.toEncoded());
|
|
74
|
+ @<Detect and handle special links@>@;
|
|
75
|
+ @<Detect and handle script links@>@;
|
|
76
|
+ }
|
|
77
|
+ else
|
|
78
|
+ {
|
|
79
|
+ QDesktopServices::openUrl(url);
|
|
80
|
+ }
|
|
81
|
+}
|
|
82
|
+
|
|
83
|
+@ Currently the only special link is |"typica://aboutqt"| which brings up
|
|
84
|
+information about the Qt framework. This is used in the About Typica window.
|
|
85
|
+
|
|
86
|
+@<Detect and handle special links@>=
|
|
87
|
+if(address == "typica://aboutqt")
|
|
88
|
+{
|
|
89
|
+ QMessageBox::aboutQt(this);
|
|
90
|
+ return;
|
|
91
|
+}
|
|
92
|
+
|
|
93
|
+@ Script links split the link data to simplify interpretation in script code.
|
|
94
|
+
|
|
95
|
+@<Detect and handle script links@>=
|
|
96
|
+if(address.startsWith("typica://script/"))
|
|
97
|
+{
|
|
98
|
+ emit scriptLinkClicked(address.remove(0, 16));
|
|
99
|
+ return;
|
|
100
|
+}
|
|
101
|
+
|
|
102
|
+@ There is a limited set of functions that should be available to the host
|
|
103
|
+environment which are not declared as slots or otherwise have some missing
|
|
104
|
+functionality in the base class. In some cases the new functions can be
|
|
105
|
+distinguished by signature.
|
|
106
|
+
|
|
107
|
+@<TypicaWebView implementation@>=
|
|
108
|
+void TypicaWebView::load(const QString &url)
|
|
109
|
+{
|
|
110
|
+ QWebView::load(QUrl(url));
|
|
111
|
+}
|
|
112
|
+
|
|
113
|
+void TypicaWebView::print()
|
|
114
|
+{
|
|
115
|
+ QPrinter *printer = new QPrinter(QPrinter::HighResolution);
|
|
116
|
+ QPrintDialog printDialog(printer, NULL);
|
|
117
|
+ if(printDialog.exec() == QDialog::Accepted)
|
|
118
|
+ {
|
|
119
|
+ QWebView::print(printer);
|
|
120
|
+ }
|
|
121
|
+}
|
|
122
|
+
|
|
123
|
+void TypicaWebView::setHtml(const QString &html, const QUrl &baseUrl)
|
|
124
|
+{
|
|
125
|
+ QWebView::setHtml(html, baseUrl);
|
|
126
|
+}
|
|
127
|
+
|
|
128
|
+void TypicaWebView::setContent(QIODevice *device)
|
|
129
|
+{
|
|
130
|
+ device->reset();
|
|
131
|
+ QByteArray content = device->readAll();
|
|
132
|
+ QWebView::setContent(content, "application/xhtml+xml");
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+QString TypicaWebView::saveXml()
|
|
136
|
+{
|
|
137
|
+ return page()->currentFrame()->documentElement().toOuterXml();
|
|
138
|
+}
|
|
139
|
+
|
|
140
|
+@ Web views are exposed to the host environment in the usual manner.
|
|
141
|
+
|
|
142
|
+@<Set up the scripting engine@>=
|
|
143
|
+constructor = engine->newFunction(constructWebView);
|
|
144
|
+value = engine->newQMetaObject(&TypicaWebView::staticMetaObject, constructor);
|
|
145
|
+engine->globalObject().setProperty("WebView", value);
|
|
146
|
+
|
|
147
|
+@ Now that |QWebView| is subclassed, all of the features that we need should
|
|
148
|
+be available through the meta-object system automatically.
|
|
149
|
+
|
|
150
|
+@<Functions for scripting@>=
|
|
151
|
+QScriptValue constructWebView(QScriptContext *, QScriptEngine *engine)
|
|
152
|
+{
|
|
153
|
+ QScriptValue object = engine->newQObject(new TypicaWebView);
|
|
154
|
+ setQWebViewProperties(object, engine);
|
|
155
|
+ return object;
|
|
156
|
+}
|
|
157
|
+
|
|
158
|
+void setQWebViewProperties(QScriptValue value, QScriptEngine *engine)
|
|
159
|
+{
|
|
160
|
+ setQWidgetProperties(value, engine);
|
|
161
|
+}
|
|
162
|
+
|
|
163
|
+@ It is also possible to create these web views from the XML portion of the
|
|
164
|
+configuration document. A function is provided to add a new view to a box
|
|
165
|
+layout.
|
|
166
|
+
|
|
167
|
+@<Functions for scripting@>=
|
|
168
|
+void addWebViewToLayout(QDomElement element, QStack<QWidget *> *,
|
|
169
|
+ QStack<QLayout *> *layoutStack)
|
|
170
|
+{
|
|
171
|
+ TypicaWebView *view = new TypicaWebView;
|
|
172
|
+ if(element.hasAttribute("id"))
|
|
173
|
+ {
|
|
174
|
+ view->setObjectName(element.attribute("id"));
|
|
175
|
+ }
|
|
176
|
+ QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
|
|
177
|
+ layout->addWidget(view);
|
|
178
|
+}
|
|
179
|
+
|
|
180
|
+@ Prototypes must be provided for these functions.
|
|
181
|
+
|
|
182
|
+@<Function prototypes for scripting@>=
|
|
183
|
+QScriptValue constructWebView(QScriptContext *context, QScriptEngine *engine);
|
|
184
|
+void setQWebViewProperties(QScriptValue value, QScriptEngine *engine);
|
|
185
|
+void addWebViewToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
|
|
186
|
+ QStack<QLayout *> *layoutStack);
|
|
187
|
+
|
|
188
|
+@ Finally, we must include our new header in |"typica.cpp"|.
|
|
189
|
+
|
|
190
|
+@<Header files to include@>=
|
|
191
|
+#include "webview.h"
|
|
192
|
+
|