ctkCmdLineModuleFrontendQtWebKit.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCmdLineModuleFrontendQtWebKit.h"
  16. #include "ctkCmdLineModuleXslTransform.h"
  17. #include "ctkCmdLineModuleReference.h"
  18. #include <QWebView>
  19. #include <QWebFrame>
  20. #include <QWebElement>
  21. #include <QBuffer>
  22. #include <QFile>
  23. #include <QDebug>
  24. ctkCmdLineModuleFrontendQtWebKit::ctkCmdLineModuleFrontendQtWebKit(const ctkCmdLineModuleReference& moduleRef)
  25. : ctkCmdLineModuleFrontend(moduleRef)
  26. , WebView(NULL)
  27. {
  28. }
  29. QObject* ctkCmdLineModuleFrontendQtWebKit::guiHandle() const
  30. {
  31. if (WebView) return WebView;
  32. QBuffer input;
  33. input.setData(moduleReference().rawXmlDescription());
  34. QBuffer htmlOutput;
  35. htmlOutput.open(QIODevice::ReadWrite);
  36. ctkCmdLineModuleXslTransform xslTransform(&input, &htmlOutput);
  37. QFile htmlTransformation(":/ctkCmdLineModuleXmlToPlainHtml.xsl");
  38. xslTransform.setXslTransformation(&htmlTransformation);
  39. if (!xslTransform.transform())
  40. {
  41. // maybe throw an exception
  42. qCritical() << xslTransform.errorString();
  43. return 0;
  44. }
  45. this->WebView = new QWebView;
  46. QByteArray htmlContent = htmlOutput.readAll();
  47. this->WebView->setHtml(htmlContent);
  48. return this->WebView;
  49. }
  50. QVariant ctkCmdLineModuleFrontendQtWebKit::value(const QString &parameter) const
  51. {
  52. QWebElement webElement = this->WebView->page()->currentFrame()->findFirstElement("input[name=" + parameter + "]");
  53. if (webElement.isNull()) return QVariant();
  54. // Work around bug https://bugs.webkit.org/show_bug.cgi?id=32865 for input elements
  55. QVariant value = webElement.evaluateJavaScript("this.value");
  56. qDebug() << "Found element" << webElement.tagName() << "with value" << value;
  57. return value;
  58. }
  59. void ctkCmdLineModuleFrontendQtWebKit::setValue(const QString &parameter, const QVariant &value)
  60. {
  61. if (!this->WebView) return;
  62. QWebElement webElement = this->WebView->page()->currentFrame()->findFirstElement("input[name=" + parameter + "]");
  63. if (webElement.isNull()) return;
  64. // Work around bug https://bugs.webkit.org/show_bug.cgi?id=32865 for input elements
  65. webElement.evaluateJavaScript(QString("this.value='%1'").arg(value.toString()));
  66. }