ctkCmdLineModuleInstanceQtGui.cpp 3.0 KB

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