ctkCmdLineModuleFrontendQtGui.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "ctkCmdLineModuleFrontendQtGui.h"
  16. #include "ctkCmdLineModuleReference.h"
  17. #include "ctkCmdLineModuleXslTransform.h"
  18. #include "ctkCmdLineModuleObjectTreeWalker_p.h"
  19. #include <QBuffer>
  20. #include <QFile>
  21. #include <QUiLoader>
  22. #include <QWidget>
  23. #include <QVariant>
  24. #include <QCoreApplication>
  25. #include <QDebug>
  26. ctkCmdLineModuleFrontendQtGui::ctkCmdLineModuleFrontendQtGui(const ctkCmdLineModuleReference& moduleRef)
  27. : ctkCmdLineModuleFrontend(moduleRef),
  28. Widget(NULL)
  29. {
  30. }
  31. QObject* ctkCmdLineModuleFrontendQtGui::guiHandle() const
  32. {
  33. if (Widget) return Widget;
  34. QBuffer input;
  35. input.setData(moduleReference().rawXmlDescription());
  36. QBuffer uiForm;
  37. uiForm.open(QIODevice::ReadWrite);
  38. ctkCmdLineModuleXslTransform xslTransform(&input, &uiForm);
  39. QFile qtGuiTransformation(":/ctkCmdLineModuleXmlToQtUi.xsl");
  40. xslTransform.setXslTransformation(&qtGuiTransformation);
  41. if (!xslTransform.transform())
  42. {
  43. // maybe throw an exception
  44. qCritical() << xslTransform.errorString();
  45. return 0;
  46. }
  47. QUiLoader uiLoader;
  48. #ifdef CMAKE_INTDIR
  49. QString appPath = QCoreApplication::applicationDirPath();
  50. if (appPath.endsWith(CMAKE_INTDIR))
  51. {
  52. uiLoader.addPluginPath(appPath + "/../designer");
  53. }
  54. #endif
  55. this->Widget = uiLoader.load(&uiForm);
  56. return this->Widget;
  57. }
  58. QVariant ctkCmdLineModuleFrontendQtGui::value(const QString &parameter) const
  59. {
  60. if (!this->Widget) return QVariant();
  61. ctkCmdLineModuleObjectTreeWalker reader(this->Widget);
  62. while(reader.readNextParameter())
  63. {
  64. if(reader.name() == parameter)
  65. {
  66. return reader.value();
  67. }
  68. }
  69. return QVariant();
  70. }
  71. void ctkCmdLineModuleFrontendQtGui::setValue(const QString &parameter, const QVariant &value)
  72. {
  73. if (!this->Widget) return;
  74. ctkCmdLineModuleObjectTreeWalker walker(this->Widget);
  75. while(walker.readNextParameter())
  76. {
  77. if(walker.name() == parameter && walker.value() != value)
  78. {
  79. walker.setValue(value);
  80. emit valueChanged(parameter, value);
  81. }
  82. }
  83. }
  84. QList<QString> ctkCmdLineModuleFrontendQtGui::parameterNames() const
  85. {
  86. if (!ParameterNames.empty()) return ParameterNames;
  87. // Compute the list of parameter names using the widget hierarchy
  88. // if it has already created (otherwise fall back to the superclass
  89. // implementation.
  90. // This avoids creating a ctkCmdLineModuleDescription instance.
  91. if (this->Widget == 0) return ctkCmdLineModuleFrontend::parameterNames();
  92. ctkCmdLineModuleObjectTreeWalker walker(this->Widget);
  93. while(walker.readNextParameter())
  94. {
  95. ParameterNames.push_back(walker.name());
  96. }
  97. return ParameterNames;
  98. }