ctkCmdLineModuleFrontendQtGui.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. void ctkCmdLineModuleFrontendQtGui::setCustomValue(const QString& parameter, const QVariant &value,
  86. const QString& propertyName)
  87. {
  88. if (!d->Widget) return;
  89. ctkCmdLineModuleObjectTreeWalker walker(d->Widget);
  90. while(walker.readNextParameter())
  91. {
  92. if(walker.name() == parameter && walker.value(propertyName) != value)
  93. {
  94. walker.setValue(value, propertyName);
  95. break;
  96. }
  97. }
  98. }
  99. //-----------------------------------------------------------------------------
  100. QObject* ctkCmdLineModuleFrontendQtGui::guiHandle() const
  101. {
  102. if (d->Widget) return d->Widget;
  103. QBuffer input;
  104. input.setData(moduleReference().rawXmlDescription());
  105. QBuffer uiForm;
  106. uiForm.open(QIODevice::ReadWrite);
  107. ctkCmdLineModuleXslTransform* xslTransform = this->xslTransform();
  108. xslTransform->setInput(&input);
  109. xslTransform->setOutput(&uiForm);
  110. if (!xslTransform->transform())
  111. {
  112. // maybe throw an exception
  113. qCritical() << xslTransform->errorString();
  114. return 0;
  115. }
  116. QUiLoader* uiLoader = this->uiLoader();
  117. #ifdef CMAKE_INTDIR
  118. QString appPath = QCoreApplication::applicationDirPath();
  119. if (appPath.endsWith(CMAKE_INTDIR))
  120. {
  121. uiLoader->addPluginPath(appPath + "/../designer");
  122. }
  123. #endif
  124. d->Widget = uiLoader->load(&uiForm);
  125. return d->Widget;
  126. }
  127. //-----------------------------------------------------------------------------
  128. QVariant ctkCmdLineModuleFrontendQtGui::value(const QString &parameter, int role) const
  129. {
  130. Q_UNUSED(role)
  131. // This will always return data using the default property for parameter values,
  132. // which holds the data for the DisplayRole.
  133. return customValue(parameter);
  134. }
  135. //-----------------------------------------------------------------------------
  136. void ctkCmdLineModuleFrontendQtGui::setValue(const QString &parameter, const QVariant &value, int role)
  137. {
  138. if (role != DisplayRole) return;
  139. QVariant oldValue = this->customValue(parameter);
  140. // This sets the value of the default QObject property for the DisplayRole.
  141. this->setCustomValue(parameter, value);
  142. // Before emitting the signal, get the actual value because it might be different
  143. // (Widgets with constraints on the value domain might adapt the value).
  144. QVariant currentValue = this->customValue(parameter);
  145. if (currentValue != oldValue)
  146. {
  147. emit valueChanged(parameter, currentValue);
  148. }
  149. }
  150. //-----------------------------------------------------------------------------
  151. QList<QString> ctkCmdLineModuleFrontendQtGui::parameterNames() const
  152. {
  153. if (!d->ParameterNames.empty()) return d->ParameterNames;
  154. // Compute the list of parameter names using the widget hierarchy
  155. // if it has already created (otherwise fall back to the superclass
  156. // implementation.
  157. // This avoids creating a ctkCmdLineModuleDescription instance.
  158. if (d->Widget == 0) return ctkCmdLineModuleFrontend::parameterNames();
  159. ctkCmdLineModuleObjectTreeWalker walker(d->Widget);
  160. while(walker.readNextParameter())
  161. {
  162. d->ParameterNames.push_back(walker.name());
  163. }
  164. return d->ParameterNames;
  165. }