Typica is a free program for professional coffee roasters. https://typica.us
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

webview.w 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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();@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::print()
  98. {
  99. QPrinter *printer = new QPrinter(QPrinter::HighResolution);
  100. QPrintDialog printDialog(printer, NULL);
  101. if(printDialog.exec() == QDialog::Accepted)
  102. {
  103. QWebView::print(printer);
  104. }
  105. }
  106. void TypicaWebView::setHtml(const QString &html, const QUrl &baseUrl)
  107. {
  108. QWebView::setHtml(html, baseUrl);
  109. }
  110. void TypicaWebView::setContent(QIODevice *device)
  111. {
  112. QSettings settings;
  113. device->reset();
  114. QByteArray content = device->readAll();
  115. QUrl baseDir = QUrl("file://" + settings.value("config").toString() + "/");
  116. QWebView::setContent(content, "application/xhtml+xml", baseDir);
  117. }
  118. QString TypicaWebView::saveXml()
  119. {
  120. return page()->currentFrame()->documentElement().toOuterXml();
  121. }
  122. @ Web views are exposed to the host environment in the usual manner.
  123. @<Set up the scripting engine@>=
  124. constructor = engine->newFunction(constructWebView);
  125. value = engine->newQMetaObject(&TypicaWebView::staticMetaObject, constructor);
  126. engine->globalObject().setProperty("WebView", value);
  127. @ Now that |QWebView| is subclassed, all of the features that we need should
  128. be available through the meta-object system automatically.
  129. @<Functions for scripting@>=
  130. QScriptValue constructWebView(QScriptContext *, QScriptEngine *engine)
  131. {
  132. QScriptValue object = engine->newQObject(new TypicaWebView);
  133. setQWebViewProperties(object, engine);
  134. return object;
  135. }
  136. void setQWebViewProperties(QScriptValue value, QScriptEngine *engine)
  137. {
  138. setQWidgetProperties(value, engine);
  139. }
  140. @ It is also possible to create these web views from the XML portion of the
  141. configuration document. A function is provided to add a new view to a box
  142. layout.
  143. @<Functions for scripting@>=
  144. void addWebViewToLayout(QDomElement element, QStack<QWidget *> *,
  145. QStack<QLayout *> *layoutStack)
  146. {
  147. TypicaWebView *view = new TypicaWebView;
  148. if(element.hasAttribute("id"))
  149. {
  150. view->setObjectName(element.attribute("id"));
  151. }
  152. QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
  153. layout->addWidget(view);
  154. }
  155. @ Prototypes must be provided for these functions.
  156. @<Function prototypes for scripting@>=
  157. QScriptValue constructWebView(QScriptContext *context, QScriptEngine *engine);
  158. void setQWebViewProperties(QScriptValue value, QScriptEngine *engine);
  159. void addWebViewToLayout(QDomElement element, QStack<QWidget *> *widgetStack,
  160. QStack<QLayout *> *layoutStack);
  161. @ Finally, we must include our new header in |"typica.cpp"|.
  162. @<Header files to include@>=
  163. #include "webview.h"
  164. @* Web View DOM Access.
  165. \noindent Two methods in |TypicaWebView| provide access to the DOM for the
  166. currently displayed page. These are simple wrappers. The |QWebElement| is also
  167. @<TypicaWebView implementation@>=
  168. QWebElement TypicaWebView::documentElement()
  169. {
  170. return page()->mainFrame()->documentElement();
  171. }
  172. QWebElement TypicaWebView::findFirstElement(const QString &selector)
  173. {
  174. return page()->mainFrame()->findFirstElement(selector);
  175. }
  176. @ In order to call these methods we need to be able to convert a |QWebElement|
  177. to and from a |QScriptValue|.
  178. @<Function prototypes for scripting@>=
  179. QScriptValue QWebElement_toScriptValue(QScriptEngine *engine, const QWebElement &element);
  180. void QWebElement_fromScriptValue(const QScriptValue &value, QWebElement &element);
  181. @ The implementation simply packs these in a variant.
  182. @<Functions for scripting@>=
  183. QScriptValue QWebElement_toScriptValue(QScriptEngine *engine, const QWebElement &element)
  184. {
  185. QVariant var;
  186. var.setValue(element);
  187. QScriptValue object = engine->newVariant(var);
  188. return object;
  189. }
  190. void QWebElement_fromScriptValue(const QScriptValue &value, QWebElement &element)
  191. {
  192. element = value.toVariant().@[value<QWebElement>()@];
  193. }
  194. @ These methods must be registered with the engine.
  195. @<Set up the scripting engine@>=
  196. qScriptRegisterMetaType(engine, QWebElement_toScriptValue, QWebElement_fromScriptValue);
  197. @ As |QWebElement| by itself is not well suited for scripting, we must provide
  198. a way to attach useful properties to the returned object. We do this with a
  199. simple wrapper class.
  200. @(webelement.h@>=
  201. #include <QWebElement>
  202. #include <QObject>
  203. #ifndef TypicaWebElementHeader
  204. #define TypicaWebElementHeader
  205. class TypicaWebElement : public QObject@/
  206. {@/
  207. @[Q_OBJECT@]@;
  208. public:@/
  209. TypicaWebElement(QWebElement element);
  210. @[Q_INVOKABLE@,@, void@]@, appendInside(const QString &markup);@t\2\2@>@/
  211. @[Q_INVOKABLE@,@, void@]@, appendOutside(const QString &markup);@t\2\2@>@/
  212. @[Q_INVOKABLE@,@, void@]@, prependInside(const QString &markup);@t\2\2@>@/
  213. @[Q_INVOKABLE@,@, void@]@, prependOutside(const QString &markup);@t\2\2@>@/
  214. @[Q_INVOKABLE@,@, void@]@, removeFromDocument();@t\2\2@>@/
  215. @[Q_INVOKABLE@,@, void@]@, replace(const QString &markup);@t\2\2@>@/
  216. @[Q_INVOKABLE@,@, void@]@, setInnerXml(const QString &markup);@t\2\2@>@/
  217. @[Q_INVOKABLE@,@, void@]@, setOuterXml(const QString &markup);@t\2\2@>@/
  218. @[Q_INVOKABLE@,@, void@]@, setPlainText(const QString &text);@t\2\2@>@/
  219. private:@/
  220. QWebElement e;@/
  221. };
  222. #endif
  223. @ A constructor is required for creating this wrapper for the host environment.
  224. @<Function prototypes for scripting@>=
  225. QScriptValue constructWebElement(QScriptContext *context,
  226. QScriptEngine *engine);
  227. @ The script engine is informed of this function.
  228. @<Set up the scripting engine@>=
  229. constructor = engine->newFunction(constructWebElement);
  230. engine->globalObject().setProperty("WebElement", constructor);
  231. @ A specialization of the |argument()| template method is required to
  232. pass the |QWebElement| through to the wrapper constructor.
  233. @<Functions for scripting@>=
  234. template<> QWebElement argument(int arg, QScriptContext *context)
  235. {
  236. return qscriptvalue_cast<QWebElement>(context->argument(arg));
  237. }
  238. @ Our wrapper constructor takes a single argument which is the |QWebElement| to
  239. wrap.
  240. @<Functions for scripting@>=
  241. QScriptValue constructWebElement(QScriptContext *context,
  242. QScriptEngine *engine)
  243. {
  244. QWebElement element = argument<QWebElement>(0, context);
  245. QScriptValue object = engine->newQObject(new TypicaWebElement(element));
  246. return object;
  247. }
  248. @ The |TypicaWebElement| constructor just keeps a copy of the element passed as
  249. an argument.
  250. @<TypicaWebElement implementation@>=
  251. TypicaWebElement::TypicaWebElement(QWebElement element) : e(element)
  252. {
  253. /* Nothing needs to be done here. */
  254. }
  255. @ Everything else is a simple wrapper around the equivalent |QWebElement|
  256. method.
  257. @<TypicaWebElement implementation@>=
  258. void TypicaWebElement::appendInside(const QString &markup)
  259. {
  260. e.appendInside(markup);
  261. }
  262. void TypicaWebElement::appendOutside(const QString &markup)
  263. {
  264. e.appendOutside(markup);
  265. }
  266. void TypicaWebElement::prependInside(const QString &markup)
  267. {
  268. e.prependInside(markup);
  269. }
  270. void TypicaWebElement::prependOutside(const QString &markup)
  271. {
  272. e.prependOutside(markup);
  273. }
  274. void TypicaWebElement::removeFromDocument()
  275. {
  276. e.removeFromDocument();
  277. }
  278. void TypicaWebElement::replace(const QString &markup)
  279. {
  280. e.replace(markup);
  281. }
  282. void TypicaWebElement::setInnerXml(const QString &markup)
  283. {
  284. e.setInnerXml(markup);
  285. }
  286. void TypicaWebElement::setOuterXml(const QString &markup)
  287. {
  288. e.setOuterXml(markup);
  289. }
  290. void TypicaWebElement::setPlainText(const QString &text)
  291. {
  292. e.setPlainText(text);
  293. }
  294. @ Implementation is in a separate file.
  295. @(webelement.cpp@>=
  296. #include "webelement.h"
  297. @<TypicaWebElement implementation@>@;
  298. @ Another header is required.
  299. @<Header files to include@>=
  300. #include "webelement.h"