ctkCmdLineModuleFrontendQtGui.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. : Loader(NULL)
  31. , Transform(NULL)
  32. , Widget(NULL)
  33. {}
  34. ~ctkCmdLineModuleFrontendQtGuiPrivate()
  35. {
  36. delete Loader;
  37. delete Transform;
  38. }
  39. mutable QUiLoader* Loader;
  40. mutable ctkCmdLineModuleXslTransform* Transform;
  41. mutable QWidget* Widget;
  42. // Cache the list of parameter names
  43. mutable QList<QString> ParameterNames;
  44. };
  45. //-----------------------------------------------------------------------------
  46. ctkCmdLineModuleFrontendQtGui::ctkCmdLineModuleFrontendQtGui(const ctkCmdLineModuleReference& moduleRef)
  47. : ctkCmdLineModuleFrontend(moduleRef),
  48. d(new ctkCmdLineModuleFrontendQtGuiPrivate)
  49. {
  50. }
  51. //-----------------------------------------------------------------------------
  52. ctkCmdLineModuleFrontendQtGui::~ctkCmdLineModuleFrontendQtGui()
  53. {
  54. }
  55. //-----------------------------------------------------------------------------
  56. QUiLoader* ctkCmdLineModuleFrontendQtGui::uiLoader() const
  57. {
  58. if (d->Loader == NULL)
  59. {
  60. d->Loader = new QUiLoader();
  61. }
  62. return d->Loader;
  63. }
  64. //-----------------------------------------------------------------------------
  65. ctkCmdLineModuleXslTransform* ctkCmdLineModuleFrontendQtGui::xslTransform() const
  66. {
  67. if (d->Transform == NULL)
  68. {
  69. d->Transform = new ctkCmdLineModuleXslTransform();
  70. }
  71. return d->Transform;
  72. }
  73. //-----------------------------------------------------------------------------
  74. QObject* ctkCmdLineModuleFrontendQtGui::guiHandle() const
  75. {
  76. if (d->Widget) return d->Widget;
  77. QBuffer input;
  78. input.setData(moduleReference().rawXmlDescription());
  79. QBuffer uiForm;
  80. uiForm.open(QIODevice::ReadWrite);
  81. ctkCmdLineModuleXslTransform* xslTransform = this->xslTransform();
  82. xslTransform->setInput(&input);
  83. xslTransform->setOutput(&uiForm);
  84. QFile qtGuiTransformation(":/ctkCmdLineModuleXmlToQtUi.xsl");
  85. xslTransform->setXslTransformation(&qtGuiTransformation);
  86. if (!xslTransform->transform())
  87. {
  88. // maybe throw an exception
  89. qCritical() << xslTransform->errorString();
  90. return 0;
  91. }
  92. QUiLoader* uiLoader = this->uiLoader();
  93. #ifdef CMAKE_INTDIR
  94. QString appPath = QCoreApplication::applicationDirPath();
  95. if (appPath.endsWith(CMAKE_INTDIR))
  96. {
  97. uiLoader.addPluginPath(appPath + "/../designer");
  98. }
  99. #endif
  100. d->Widget = uiLoader->load(&uiForm);
  101. return d->Widget;
  102. }
  103. //-----------------------------------------------------------------------------
  104. QVariant ctkCmdLineModuleFrontendQtGui::value(const QString &parameter) const
  105. {
  106. if (!d->Widget) return QVariant();
  107. ctkCmdLineModuleObjectTreeWalker reader(d->Widget);
  108. while(reader.readNextParameter())
  109. {
  110. if(reader.name() == parameter)
  111. {
  112. return reader.value();
  113. }
  114. }
  115. return QVariant();
  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. }