ctkCmdLineModuleFrontendQtGui.cpp 5.3 KB

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