ctkCmdLineModuleFrontendQtGui.cpp 5.3 KB

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