ctkCmdLineModuleFrontendQtWebKit.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_p.h"
  16. #include "ctkCmdLineModuleXslTransform.h"
  17. #include "ctkCmdLineModuleReference.h"
  18. #include <QtGlobal>
  19. #if QT_VERSION < QT_VERSION_CHECK(5,6,0)
  20. #include <QWebView>
  21. #include <QWebFrame>
  22. #include <QWebElement>
  23. #else
  24. #include <QWebEngineView>
  25. #endif
  26. #include <QBuffer>
  27. #include <QFile>
  28. #include <QDebug>
  29. //----------------------------------------------------------------------------
  30. ctkCmdLineModuleFrontendQtWebKit::ctkCmdLineModuleFrontendQtWebKit(const ctkCmdLineModuleReference& moduleRef)
  31. : ctkCmdLineModuleFrontend(moduleRef)
  32. , WebView(NULL)
  33. {
  34. }
  35. //----------------------------------------------------------------------------
  36. QObject* ctkCmdLineModuleFrontendQtWebKit::guiHandle() const
  37. {
  38. if (WebView) return WebView;
  39. #if QT_VERSION < QT_VERSION_CHECK(5,6,0)
  40. QBuffer input;
  41. input.setData(moduleReference().rawXmlDescription());
  42. QBuffer htmlOutput;
  43. htmlOutput.open(QIODevice::ReadWrite);
  44. ctkCmdLineModuleXslTransform xslTransform(&input, &htmlOutput);
  45. QFile htmlTransformation(":/ctkCmdLineModuleXmlToPlainHtml.xsl");
  46. xslTransform.setXslTransformation(&htmlTransformation);
  47. if (!xslTransform.transform())
  48. {
  49. // maybe throw an exception
  50. qCritical() << xslTransform.errorString();
  51. return 0;
  52. }
  53. this->WebView = new QWebView;
  54. QByteArray htmlContent = htmlOutput.readAll();
  55. this->WebView->setHtml(htmlContent);
  56. #else
  57. qWarning() << "ctkCmdLineModuleFrontendQtWebKit::guiHandle() "
  58. "is *NOT* implemented";
  59. #endif
  60. return this->WebView;
  61. }
  62. //----------------------------------------------------------------------------
  63. QVariant ctkCmdLineModuleFrontendQtWebKit::value(const QString &parameter, int role) const
  64. {
  65. Q_UNUSED(role);
  66. #if QT_VERSION < QT_VERSION_CHECK(5,6,0)
  67. QWebElement webElement = this->WebView->page()->currentFrame()->findFirstElement("input[name=" + parameter + "]");
  68. if (webElement.isNull()) return QVariant();
  69. // Work around bug https://bugs.webkit.org/show_bug.cgi?id=32865 for input elements
  70. QVariant value = webElement.evaluateJavaScript("this.value");
  71. qDebug() << "Found element" << webElement.tagName() << "with value" << value;
  72. return value;
  73. #else
  74. Q_UNUSED(parameter);
  75. qWarning() << "ctkCmdLineModuleFrontendQtWebKit::value() "
  76. "is *NOT* implemented";
  77. return QVariant();
  78. #endif
  79. }
  80. //----------------------------------------------------------------------------
  81. void ctkCmdLineModuleFrontendQtWebKit::setValue(const QString &parameter, const QVariant &value, int role)
  82. {
  83. if (!this->WebView || role != DisplayRole) return;
  84. #if QT_VERSION < QT_VERSION_CHECK(5,6,0)
  85. QWebElement webElement = this->WebView->page()->currentFrame()->findFirstElement("input[name=" + parameter + "]");
  86. if (webElement.isNull()) return;
  87. // Work around bug https://bugs.webkit.org/show_bug.cgi?id=32865 for input elements
  88. webElement.evaluateJavaScript(QString("this.value='%1'").arg(value.toString()));
  89. #else
  90. Q_UNUSED(parameter);
  91. Q_UNUSED(value);
  92. qWarning() << "ctkCmdLineModuleFrontendQtWebKit::setValue() "
  93. "is *NOT* implemented";
  94. #endif
  95. }