Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

webview.w 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. @* Web Views in Typica.
  2. \noindent Typica makes extensive use of web views. All printing is currently
  3. handled by generating HTML with the required information, loading it into a web
  4. view, and using the print capabilities provided there. Reports are provided by
  5. running SQL queries, generating HTML, and displaying the results in a web view.
  6. Even the program'@q'@>s about window is primarily a web view.
  7. In order to simplify the implementation of certain features, we subclass
  8. |QWebView| and provide some additional functionality.
  9. @s QWebElement int
  10. @(webview.h@>=
  11. #include <QWebView>
  12. #include <QFile>
  13. #include <QMessageBox>
  14. #include <QDesktopServices>
  15. #include <QPrinter>
  16. #include <QPrintDialog>
  17. #include <QWebFrame>
  18. #include <QWebElement>
  19. #include <QSettings>
  20. #ifndef TypicaWebViewHeader
  21. #define TypicaWebViewHeader
  22. class TypicaWebView : public QWebView@/
  23. {
  24. @[Q_OBJECT@]@;
  25. public:@/
  26. TypicaWebView();
  27. @[Q_INVOKABLE@,@, void@]@, load(const QString &url);@t\2\2@>@/
  28. @[Q_INVOKABLE@,@, void@]@, print(const QString &printerName = QString());@t\2\2@>@/
  29. @[Q_INVOKABLE@,@, void@]@, setHtml(const QString &html, const QUrl &baseUrl = QUrl());@t\2\2@>@/
  30. @[Q_INVOKABLE@,@, void@]@, setContent(QIODevice *device);@t\2\2@>@/
  31. @[Q_INVOKABLE@,@, QString@]@, saveXml();@t\2\2@>@/
  32. @[Q_INVOKABLE@,@, QWebElement@]@, documentElement();@t\2\2@>@/
  33. @[Q_INVOKABLE@,@, QWebElement@]@, findFirstElement(const QString &selector);@t\2\2@>@/
  34. @[signals@]:@/
  35. void scriptLinkClicked(const QString &link);
  36. @[private slots@]:@/
  37. void linkDelegate(const QUrl &url);
  38. };
  39. #endif
  40. @ The implementation is in a separate file.
  41. @(webview.cpp@>=
  42. #include "webview.h"
  43. @<TypicaWebView implementation@>@;
  44. @ In the constructor we set up our link delegation policy.
  45. @<TypicaWebView implementation@>=
  46. TypicaWebView::TypicaWebView() : QWebView()
  47. {
  48. page()->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
  49. connect(page(), SIGNAL(linkClicked(QUrl)), this, SLOT(linkDelegate(QUrl)));
  50. }
  51. @ When a link is clicked, one of three things may happen. Certain reserved URLs
  52. will trigger an immediate event which is just handled internally without
  53. providing any kind of external notification. URLs that start with
  54. |"typica://script/"| will produce a signal containing the rest of the URL. This
  55. is intended for script code to intercept, interpret, and update the web view
  56. with the requested information. Everything else is passed to |QDesktopServices|
  57. which will defer to system-wide preferences for how to handle different link
  58. and file types.
  59. @<TypicaWebView implementation@>=
  60. void TypicaWebView::linkDelegate(const QUrl &url)
  61. {
  62. if(url.scheme() == "typica")
  63. {
  64. QString address(url.toEncoded());
  65. @<Detect and handle special links@>@;
  66. @<Detect and handle script links@>@;
  67. }
  68. else
  69. {
  70. QDesktopServices::openUrl(url);
  71. }
  72. }
  73. @ Currently the only special link is |"typica://aboutqt"| which brings up
  74. information about the Qt framework. This is used in the About Typica window.
  75. @<Detect and handle special links@>=
  76. if(address == "typica://aboutqt")
  77. {
  78. QMessageBox::aboutQt(this);
  79. return;
  80. }
  81. @ Script links split the link data to simplify interpretation in script code.
  82. @<Detect and handle script links@>=
  83. if(address.startsWith("typica://script/"))
  84. {
  85. emit scriptLinkClicked(address.remove(0, 16));
  86. return;
  87. }
  88. @ There is a limited set of functions that should be available to the host
  89. environment which are not declared as slots or otherwise have some missing
  90. functionality in the base class. In some cases the new functions can be
  91. distinguished by signature.
  92. @<TypicaWebView implementation@>=
  93. void TypicaWebView::load(const QString &url)
  94. {
  95. QWebView::load(QUrl(url));
  96. }
  97. void TypicaWebView::setHtml(const QString &html, const QUrl &baseUrl)
  98. {
  99. QWebView::setHtml(html, baseUrl);
  100. }
  101. void TypicaWebView::setContent(QIODevice *device)
  102. {
  103. QSettings settings;
  104. device->reset();
  105. QByteArray content = device->readAll();
  106. QUrl baseDir = QUrl("file://" + settings.value("config").toString() + "/");
  107. QWebView::setContent(content, "application/xhtml+xml", baseDir);
  108. }
  109. QString TypicaWebView::saveXml()
  110. {
  111. return page()->currentFrame()->documentElement().toOuterXml();
  112. }
  113. @ Print functionality has been extended to allow an optional argument. If the
  114. name of a printer is passed in, the print dialog will be bypassed. Note that
  115. when bypassing the print dialog, page margins are set to 0. This is intentional
  116. as the use case is for printing to more specialized printers where default page
  117. margins are not appropriate and CSS can be customized to ensure that all
  118. information fits in the printable area with the assumption of no margin.
  119. @<TypicaWebView implementation@>=
  120. void TypicaWebView::print(const QString &printerName)
  121. {
  122. QPrinter *printer = new QPrinter(QPrinter::HighResolution);
  123. if(!printerName.isEmpty())
  124. {
  125. printer->setPrinterName(printerName);
  126. printer->setFullPage(true);
  127. QWebView::print(printer);
  128. return;
  129. }
  130. QPrintDialog printDialog(printer, NULL);
  131. if(printDialog.exec() == QDialog::Accepted)
  132. {
  133. QWebView::print(printer);
  134. }
  135. }
  136. @ Web views are exposed to the host environment in the usual manner.
  137. @<Set up the scripting engine@>=
  138. constructor = engine->newFunction(constructWebView);
  139. value = engine->newQMetaObject(&TypicaWebView::staticMetaObject, constructor);
  140. engine->globalObject().setProperty("WebView", value);
  141. @ Now that |QWebView| is subclassed, all of the features that we need should
  142. be available through the meta-object system automatically.
  143. @<Functions for scripting@>=
  144. QScriptValue constructWebView(QScriptContext *, QScriptEngine *engine)
  145. {
  146. QScriptValue object = engine->newQObject(new TypicaWebView);
  147. setQWebViewProperties(object, engine);
  148. return object;
  149. }
  150. void setQWebViewProperties(QScriptValue value, QScriptEngine *engine)
  151. {
  152. setQWidgetProperties(value, engine);
  153. }
  154. @ It is also possible to create these web views from the XML portion of the
  155. configuration document. A function is provided to add a new view to a box
  156. layout.
  157. @<Functions for scripting@>=
  158. void addWebViewToLayout(QDomElement element, QStack<QWidget *> *,
  159. QStack<QLayout *> *layoutStack)
  160. {
  161. TypicaWebView *view = new TypicaWebView;
  162. if(element.hasAttribute("id"))
  163. {
  164. view->setObjectName(element.attribute("id"));
  165. }
  166. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  167. layout->addWidget(view);
  168. }
  169. @ Prototypes must be provided for these functions.
  170. @<Function prototypes for scripting@>=
  171. QScriptValue constructWebView(QScriptContext *context, QScriptEngine *engine);
  172. void setQWebViewProperties(QScriptValue value, QScriptEngine *engine);
  173. void addWebViewToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  174. QStack<QLayout *> *layoutStack);
  175. @ Finally, we must include our new header in |"typica.cpp"|.
  176. @<Header files to include@>=
  177. #include "webview.h"
  178. @* Web View DOM Access.
  179. \noindent Two methods in |TypicaWebView| provide access to the DOM for the
  180. currently displayed page. These are simple wrappers. The |QWebElement| is also
  181. @<TypicaWebView implementation@>=
  182. QWebElement TypicaWebView::documentElement()
  183. {
  184. return page()->mainFrame()->documentElement();
  185. }
  186. QWebElement TypicaWebView::findFirstElement(const QString &selector)
  187. {
  188. return page()->mainFrame()->findFirstElement(selector);
  189. }
  190. @ In order to call these methods we need to be able to convert a |QWebElement|
  191. to and from a |QScriptValue|.
  192. @<Function prototypes for scripting@>=
  193. QScriptValue QWebElement_toScriptValue(QScriptEngine *engine, const QWebElement &element);
  194. void QWebElement_fromScriptValue(const QScriptValue &value, QWebElement &element);
  195. @ The implementation simply packs these in a variant.
  196. @<Functions for scripting@>=
  197. QScriptValue QWebElement_toScriptValue(QScriptEngine *engine, const QWebElement &element)
  198. {
  199. QVariant var;
  200. var.setValue(element);
  201. QScriptValue object = engine->newVariant(var);
  202. return object;
  203. }
  204. void QWebElement_fromScriptValue(const QScriptValue &value, QWebElement &element)
  205. {
  206. element = value.toVariant().@[value<QWebElement>()@];
  207. }
  208. @ These methods must be registered with the engine.
  209. @<Set up the scripting engine@>=
  210. qScriptRegisterMetaType(engine, QWebElement_toScriptValue, QWebElement_fromScriptValue);
  211. @ As |QWebElement| by itself is not well suited for scripting, we must provide
  212. a way to attach useful properties to the returned object. We do this with a
  213. simple wrapper class.
  214. @(webelement.h@>=
  215. #include <QWebElement>
  216. #include <QObject>
  217. #ifndef TypicaWebElementHeader
  218. #define TypicaWebElementHeader
  219. class TypicaWebElement : public QObject@/
  220. {@/
  221. @[Q_OBJECT@]@;
  222. public:@/
  223. TypicaWebElement(QWebElement element);
  224. @[Q_INVOKABLE@,@, void@]@, appendInside(const QString &markup);@t\2\2@>@/
  225. @[Q_INVOKABLE@,@, void@]@, appendOutside(const QString &markup);@t\2\2@>@/
  226. @[Q_INVOKABLE@,@, void@]@, prependInside(const QString &markup);@t\2\2@>@/
  227. @[Q_INVOKABLE@,@, void@]@, prependOutside(const QString &markup);@t\2\2@>@/
  228. @[Q_INVOKABLE@,@, void@]@, removeFromDocument();@t\2\2@>@/
  229. @[Q_INVOKABLE@,@, void@]@, replace(const QString &markup);@t\2\2@>@/
  230. @[Q_INVOKABLE@,@, void@]@, setInnerXml(const QString &markup);@t\2\2@>@/
  231. @[Q_INVOKABLE@,@, void@]@, setOuterXml(const QString &markup);@t\2\2@>@/
  232. @[Q_INVOKABLE@,@, void@]@, setPlainText(const QString &text);@t\2\2@>@/
  233. private:@/
  234. QWebElement e;@/
  235. };
  236. #endif
  237. @ A constructor is required for creating this wrapper for the host environment.
  238. @<Function prototypes for scripting@>=
  239. QScriptValue constructWebElement(QScriptContext *context,
  240. QScriptEngine *engine);
  241. @ The script engine is informed of this function.
  242. @<Set up the scripting engine@>=
  243. constructor = engine->newFunction(constructWebElement);
  244. engine->globalObject().setProperty("WebElement", constructor);
  245. @ A specialization of the |argument()| template method is required to
  246. pass the |QWebElement| through to the wrapper constructor.
  247. @<Functions for scripting@>=
  248. template<> QWebElement argument(int arg, QScriptContext *context)
  249. {
  250. return qscriptvalue_cast<QWebElement>(context->argument(arg));
  251. }
  252. @ Our wrapper constructor takes a single argument which is the |QWebElement| to
  253. wrap.
  254. @<Functions for scripting@>=
  255. QScriptValue constructWebElement(QScriptContext *context,
  256. QScriptEngine *engine)
  257. {
  258. QWebElement element = argument<QWebElement>(0, context);
  259. QScriptValue object = engine->newQObject(new TypicaWebElement(element));
  260. return object;
  261. }
  262. @ The |TypicaWebElement| constructor just keeps a copy of the element passed as
  263. an argument.
  264. @<TypicaWebElement implementation@>=
  265. TypicaWebElement::TypicaWebElement(QWebElement element) : e(element)
  266. {
  267. /* Nothing needs to be done here. */
  268. }
  269. @ Everything else is a simple wrapper around the equivalent |QWebElement|
  270. method.
  271. @<TypicaWebElement implementation@>=
  272. void TypicaWebElement::appendInside(const QString &markup)
  273. {
  274. e.appendInside(markup);
  275. }
  276. void TypicaWebElement::appendOutside(const QString &markup)
  277. {
  278. e.appendOutside(markup);
  279. }
  280. void TypicaWebElement::prependInside(const QString &markup)
  281. {
  282. e.prependInside(markup);
  283. }
  284. void TypicaWebElement::prependOutside(const QString &markup)
  285. {
  286. e.prependOutside(markup);
  287. }
  288. void TypicaWebElement::removeFromDocument()
  289. {
  290. e.removeFromDocument();
  291. }
  292. void TypicaWebElement::replace(const QString &markup)
  293. {
  294. e.replace(markup);
  295. }
  296. void TypicaWebElement::setInnerXml(const QString &markup)
  297. {
  298. e.setInnerXml(markup);
  299. }
  300. void TypicaWebElement::setOuterXml(const QString &markup)
  301. {
  302. e.setOuterXml(markup);
  303. }
  304. void TypicaWebElement::setPlainText(const QString &text)
  305. {
  306. e.setPlainText(text);
  307. }
  308. @ Implementation is in a separate file.
  309. @(webelement.cpp@>=
  310. #include "webelement.h"
  311. @<TypicaWebElement implementation@>@;
  312. @ Another header is required.
  313. @<Header files to include@>=
  314. #include "webelement.h"