ctkCmdLineModuleInstanceQtGui.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "ctkCmdLineModuleInstanceQtGui_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 <QDebug>
  24. ctkCmdLineModuleInstanceQtGui::ctkCmdLineModuleInstanceQtGui(const ctkCmdLineModuleReference& moduleRef)
  25. : ctkCmdLineModuleInstance(moduleRef),
  26. WidgetTree(NULL)
  27. {
  28. }
  29. QObject* ctkCmdLineModuleInstanceQtGui::guiHandle() const
  30. {
  31. if (WidgetTree) return WidgetTree;
  32. QBuffer input;
  33. input.setData(moduleReference().rawXmlDescription());
  34. QBuffer uiForm;
  35. uiForm.open(QIODevice::ReadWrite);
  36. ctkCmdLineModuleXslTransform xslTransform(&input, &uiForm);
  37. if (!xslTransform.transform())
  38. {
  39. // maybe throw an exception
  40. qCritical() << xslTransform.errorString();
  41. return 0;
  42. }
  43. QUiLoader uiLoader;
  44. WidgetTree = uiLoader.load(&uiForm);
  45. return WidgetTree;
  46. }
  47. QVariant ctkCmdLineModuleInstanceQtGui::value(const QString &parameter) const
  48. {
  49. if (!WidgetTree) return QVariant();
  50. ctkCmdLineModuleObjectTreeWalker reader(WidgetTree);
  51. while(reader.readNextParameter())
  52. {
  53. if(reader.name() == parameter)
  54. {
  55. return reader.value();
  56. }
  57. }
  58. return QVariant();
  59. }
  60. void ctkCmdLineModuleInstanceQtGui::setValue(const QString &parameter, const QVariant &value)
  61. {
  62. if (!WidgetTree) return;
  63. ctkCmdLineModuleObjectTreeWalker walker(WidgetTree);
  64. while(walker.readNextParameter())
  65. {
  66. if(walker.name() == parameter && walker.value() != value)
  67. {
  68. walker.setValue(value);
  69. emit valueChanged(parameter, value);
  70. }
  71. }
  72. }
  73. QList<QString> ctkCmdLineModuleInstanceQtGui::parameterNames() const
  74. {
  75. if (!ParameterNames.empty()) return ParameterNames;
  76. // Compute the list of parameter names using the widget hierarchy
  77. // if it has already created (otherwise fall back to the superclass
  78. // implementation.
  79. // This avoids creating a ctkCmdLineModuleDescription instance.
  80. if (WidgetTree == 0) return ctkCmdLineModuleInstance::parameterNames();
  81. ctkCmdLineModuleObjectTreeWalker walker(WidgetTree);
  82. while(walker.readNextParameter())
  83. {
  84. ParameterNames.push_back(walker.name());
  85. }
  86. return ParameterNames;
  87. }