ctkCLModuleExplorerMainWindow.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "ctkCLModuleExplorerMainWindow.h"
  16. #include "ui_ctkCLModuleExplorerMainWindow.h"
  17. #include <ctkModuleDescriptionValidator.h>
  18. #include <ctkModuleManager.h>
  19. #include <ctkModuleProcessFuture.h>
  20. #include <QFile>
  21. #include <QBuffer>
  22. #include <QUiLoader>
  23. #include <QDebug>
  24. class ctkModuleDescriptionDefaultFactory : public ctkModuleDescriptionFactory
  25. {
  26. public:
  27. QObject* createObjectRepresentationFromXML(const QByteArray &xmlDescription)
  28. {
  29. return cachedObjectTree(xmlDescription);
  30. }
  31. QObject* createGUIFromXML(const QByteArray &xmlDescription)
  32. {
  33. return cachedObjectTree(xmlDescription);
  34. }
  35. private:
  36. QObject* cachedObjectTree(const QByteArray& xmlDescription)
  37. {
  38. QObject* root = cache[xmlDescription];
  39. if (root != 0) return root;
  40. QBuffer input;
  41. input.setData(xmlDescription);
  42. input.open(QIODevice::ReadOnly);
  43. ctkModuleDescriptionValidator validator(&input);
  44. if (!validator.validateXSLTOutput())
  45. {
  46. qCritical() << validator.errorString();
  47. return 0;
  48. }
  49. QUiLoader uiLoader;
  50. QByteArray uiBlob;
  51. uiBlob.append(validator.output());
  52. qDebug() << validator.output();
  53. QBuffer uiForm(&uiBlob);
  54. root = uiLoader.load(&uiForm);
  55. cache[xmlDescription] = root;
  56. return root;
  57. }
  58. // TODO: remove entry if QObject was deleted
  59. QHash<QByteArray, QObject*> cache;
  60. };
  61. ctkCLModuleExplorerMainWindow::ctkCLModuleExplorerMainWindow(QWidget *parent) :
  62. QMainWindow(parent),
  63. ui(new Ui::ctkCLModuleExplorerMainWindow),
  64. factory(new ctkModuleDescriptionDefaultFactory),
  65. moduleManager(factory)
  66. {
  67. ui->setupUi(this);
  68. }
  69. ctkCLModuleExplorerMainWindow::~ctkCLModuleExplorerMainWindow()
  70. {
  71. delete ui;
  72. delete factory;
  73. }
  74. void ctkCLModuleExplorerMainWindow::addModuleTab(const ctkModuleReference& moduleRef)
  75. {
  76. if (moduleRef.widgetTree() == 0) return;
  77. QWidget* widget = qobject_cast<QWidget*>(moduleRef.widgetTree());
  78. int tabIndex = ui->mainTabWidget->addTab(widget, widget->objectName());
  79. mapTabToModuleRef[tabIndex] = moduleRef;
  80. }
  81. void ctkCLModuleExplorerMainWindow::addModule(const QString &location)
  82. {
  83. ctkModuleReference ref = moduleManager.addModule(location);
  84. if (ref.isValid())
  85. {
  86. addModuleTab(ref);
  87. }
  88. }
  89. void ctkCLModuleExplorerMainWindow::testModuleXML(const QByteArray &xml)
  90. {
  91. QObject* root = factory->createGUIFromXML(xml);
  92. if (root)
  93. {
  94. QWidget* widget = qobject_cast<QWidget*>(root);
  95. int tabIndex = ui->mainTabWidget->addTab(widget, widget->objectName());
  96. //mapTabToModuleRef[tabIndex] = moduleRef;
  97. }
  98. }
  99. void ctkCLModuleExplorerMainWindow::on_actionRun_triggered()
  100. {
  101. qDebug() << "Creating module command line...";
  102. QStringList cmdLineArgs = ctkModuleManager::createCommandLineArgs(ui->mainTabWidget->currentWidget());
  103. qDebug() << cmdLineArgs;
  104. ctkModuleReference moduleRef = mapTabToModuleRef[ui->mainTabWidget->currentIndex()];
  105. if (!moduleRef.isValid())
  106. {
  107. qWarning() << "Invalid module reference";
  108. return;
  109. }
  110. connect(&futureWatcher, SIGNAL(finished()), this, SLOT(futureFinished()));
  111. ctkModuleProcessFuture future = moduleManager.run(moduleRef);
  112. futureWatcher.setFuture(future);
  113. }
  114. void ctkCLModuleExplorerMainWindow::futureFinished()
  115. {
  116. qDebug() << "*** Future finished";
  117. qDebug() << "stdout:" << futureWatcher.future().standardOutput();
  118. qDebug() << "stderr:" << futureWatcher.future().standardError();
  119. }
  120. ctkModuleReference ctkCLModuleExplorerMainWindow::moduleReference(int tabIndex)
  121. {
  122. return mapTabToModuleRef[tabIndex];
  123. }