ctkCmdLineModuleQtGui.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "ctkCmdLineModuleQtGui_p.h"
  16. #include "ctkCmdLineModuleReference.h"
  17. #include "ctkCmdLineModuleXslTransform.h"
  18. #include "ctkCmdLineModuleObjectTreeWalker_p.h"
  19. #include <QBuffer>
  20. #include <QUiLoader>
  21. #include <QWidget>
  22. #include <QVariant>
  23. #include <QCoreApplication>
  24. #include <QDebug>
  25. //-----------------------------------------------------------------------------
  26. ctkCmdLineModuleQtGui::ctkCmdLineModuleQtGui(const ctkCmdLineModuleReference& moduleRef)
  27. : ctkCmdLineModule(moduleRef),
  28. Loader(NULL),
  29. Transform(NULL),
  30. WidgetTree(NULL)
  31. {
  32. }
  33. //-----------------------------------------------------------------------------
  34. ctkCmdLineModuleQtGui::~ctkCmdLineModuleQtGui()
  35. {
  36. if (Loader != NULL)
  37. {
  38. delete Loader;
  39. }
  40. if (Transform != NULL)
  41. {
  42. delete Transform;
  43. }
  44. }
  45. //-----------------------------------------------------------------------------
  46. QUiLoader* ctkCmdLineModuleQtGui::uiLoader() const
  47. {
  48. if (Loader == NULL)
  49. {
  50. Loader = new QUiLoader();
  51. }
  52. return Loader;
  53. }
  54. //-----------------------------------------------------------------------------
  55. ctkCmdLineModuleXslTransform* ctkCmdLineModuleQtGui::xslTransform() const
  56. {
  57. if (Transform == NULL)
  58. {
  59. Transform = new ctkCmdLineModuleXslTransform();
  60. }
  61. return Transform;
  62. }
  63. //-----------------------------------------------------------------------------
  64. QObject* ctkCmdLineModuleQtGui::guiHandle() const
  65. {
  66. if (WidgetTree) return WidgetTree;
  67. QBuffer input;
  68. input.setData(moduleReference().rawXmlDescription());
  69. QBuffer uiForm;
  70. uiForm.open(QIODevice::ReadWrite);
  71. ctkCmdLineModuleXslTransform* xslTransform = this->xslTransform();
  72. xslTransform->setInput(&input);
  73. xslTransform->setOutput(&uiForm);
  74. if (!xslTransform->transform())
  75. {
  76. // maybe throw an exception
  77. qCritical() << xslTransform->errorString();
  78. return 0;
  79. }
  80. QUiLoader* uiLoader = this->uiLoader();
  81. #ifdef CMAKE_INTDIR
  82. QString appPath = QCoreApplication::applicationDirPath();
  83. if (appPath.endsWith(CMAKE_INTDIR))
  84. {
  85. uiLoader.addPluginPath(appPath + "/../designer");
  86. }
  87. #endif
  88. WidgetTree = uiLoader->load(&uiForm);
  89. return WidgetTree;
  90. }
  91. //-----------------------------------------------------------------------------
  92. QVariant ctkCmdLineModuleQtGui::value(const QString &parameter) const
  93. {
  94. if (!WidgetTree) return QVariant();
  95. ctkCmdLineModuleObjectTreeWalker reader(WidgetTree);
  96. while(reader.readNextParameter())
  97. {
  98. if(reader.name() == parameter)
  99. {
  100. return reader.value();
  101. }
  102. }
  103. return QVariant();
  104. }
  105. //-----------------------------------------------------------------------------
  106. void ctkCmdLineModuleQtGui::setValue(const QString &parameter, const QVariant &value)
  107. {
  108. if (!WidgetTree) return;
  109. ctkCmdLineModuleObjectTreeWalker walker(WidgetTree);
  110. while(walker.readNextParameter())
  111. {
  112. if(walker.name() == parameter && walker.value() != value)
  113. {
  114. walker.setValue(value);
  115. emit valueChanged(parameter, value);
  116. }
  117. }
  118. }
  119. //-----------------------------------------------------------------------------
  120. QList<QString> ctkCmdLineModuleQtGui::parameterNames() const
  121. {
  122. if (!ParameterNames.empty()) return ParameterNames;
  123. // Compute the list of parameter names using the widget hierarchy
  124. // if it has already created (otherwise fall back to the superclass
  125. // implementation.
  126. // This avoids creating a ctkCmdLineModuleDescription instance.
  127. if (WidgetTree == 0) return ctkCmdLineModule::parameterNames();
  128. ctkCmdLineModuleObjectTreeWalker walker(WidgetTree);
  129. while(walker.readNextParameter())
  130. {
  131. ParameterNames.push_back(walker.name());
  132. }
  133. return ParameterNames;
  134. }