ctkCmdLineModuleFrontendQtWebKit.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <QWebView>
  19. #include <QWebFrame>
  20. #include <QWebElement>
  21. #include <QBuffer>
  22. #include <QFile>
  23. #include <QDebug>
  24. //----------------------------------------------------------------------------
  25. ctkCmdLineModuleFrontendQtWebKit::ctkCmdLineModuleFrontendQtWebKit(const ctkCmdLineModuleReference& moduleRef)
  26. : ctkCmdLineModuleFrontend(moduleRef)
  27. , WebView(NULL)
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. QObject* ctkCmdLineModuleFrontendQtWebKit::guiHandle() const
  32. {
  33. if (WebView) return WebView;
  34. QBuffer input;
  35. input.setData(moduleReference().rawXmlDescription());
  36. QBuffer htmlOutput;
  37. htmlOutput.open(QIODevice::ReadWrite);
  38. ctkCmdLineModuleXslTransform xslTransform(&input, &htmlOutput);
  39. QFile htmlTransformation(":/ctkCmdLineModuleXmlToPlainHtml.xsl");
  40. xslTransform.setXslTransformation(&htmlTransformation);
  41. if (!xslTransform.transform())
  42. {
  43. // maybe throw an exception
  44. qCritical() << xslTransform.errorString();
  45. return 0;
  46. }
  47. this->WebView = new QWebView;
  48. QByteArray htmlContent = htmlOutput.readAll();
  49. this->WebView->setHtml(htmlContent);
  50. return this->WebView;
  51. }
  52. //----------------------------------------------------------------------------
  53. QVariant ctkCmdLineModuleFrontendQtWebKit::value(const QString &parameter, int role) const
  54. {
  55. Q_UNUSED(role)
  56. QWebElement webElement = this->WebView->page()->currentFrame()->findFirstElement("input[name=" + parameter + "]");
  57. if (webElement.isNull()) return QVariant();
  58. // Work around bug https://bugs.webkit.org/show_bug.cgi?id=32865 for input elements
  59. QVariant value = webElement.evaluateJavaScript("this.value");
  60. qDebug() << "Found element" << webElement.tagName() << "with value" << value;
  61. return value;
  62. }
  63. //----------------------------------------------------------------------------
  64. void ctkCmdLineModuleFrontendQtWebKit::setValue(const QString &parameter, const QVariant &value, int role)
  65. {
  66. if (!this->WebView || role != DisplayRole) return;
  67. QWebElement webElement = this->WebView->page()->currentFrame()->findFirstElement("input[name=" + parameter + "]");
  68. if (webElement.isNull()) return;
  69. // Work around bug https://bugs.webkit.org/show_bug.cgi?id=32865 for input elements
  70. webElement.evaluateJavaScript(QString("this.value='%1'").arg(value.toString()));
  71. }