ctkPluginBrowser.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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 "ctkPluginBrowser.h"
  16. #include "ctkPluginTableModel.h"
  17. #include "ctkPluginResourcesTreeModel.h"
  18. #include "ctkQtResourcesTreeModel.h"
  19. #include "ctkServiceReference.h"
  20. #include "ctkPluginConstants.h"
  21. #include <ui_ctkPluginBrowserMainWindow.h>
  22. #include <ctkPluginException.h>
  23. #include <ctkPluginFramework.h>
  24. #include <QApplication>
  25. #include <QMainWindow>
  26. #include <QStringList>
  27. #include <QDirIterator>
  28. #include <QUrl>
  29. ctkPluginBrowser::ctkPluginBrowser(ctkPluginFramework* framework)
  30. : framework(framework)
  31. {
  32. framework->getPluginContext()->connectFrameworkListener(this, SLOT(frameworkEvent(ctkPluginFrameworkEvent)));
  33. QStringList pluginDirs;
  34. #ifdef CMAKE_INTDIR
  35. pluginDirs << qApp->applicationDirPath() + "/../plugins/" CMAKE_INTDIR "/";
  36. #else
  37. pluginDirs << qApp->applicationDirPath() + "/plugins/";
  38. #endif
  39. QStringListIterator dirIt(pluginDirs);
  40. while (dirIt.hasNext())
  41. {
  42. QApplication::addLibraryPath(dirIt.next());
  43. }
  44. QStringList libFilter;
  45. libFilter << "*.dll" << "*.so" << "*.dylib";
  46. QDirIterator dirIter(pluginDirs.at(0), libFilter, QDir::Files);
  47. while(dirIter.hasNext())
  48. {
  49. try
  50. {
  51. ctkPlugin* plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(dirIter.next()).toString());
  52. plugin->start(ctkPlugin::START_ACTIVATION_POLICY);
  53. }
  54. catch (const ctkPluginException& e)
  55. {
  56. qCritical() << e.what();
  57. }
  58. }
  59. framework->start();
  60. ui.setupUi(this);
  61. editors = new ctkPluginBrowserEditors(ui.centralwidget);
  62. QAbstractItemModel* pluginTableModel = new ctkPluginTableModel(framework->getPluginContext(), this);
  63. ui.pluginsTableView->setModel(pluginTableModel);
  64. QAbstractItemModel* qtresourcesTreeModel = new ctkQtResourcesTreeModel(this);
  65. ui.qtResourcesTreeView->setModel(qtresourcesTreeModel);
  66. connect(ui.pluginsTableView, SIGNAL(clicked(QModelIndex)), this, SLOT(pluginSelected(QModelIndex)));
  67. connect(ui.pluginsTableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(pluginDoubleClicked(QModelIndex)));
  68. connect(ui.pluginResourcesTreeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(dbResourceDoubleClicked(QModelIndex)));
  69. connect(ui.qtResourcesTreeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(qtResourceDoubleClicked(QModelIndex)));
  70. }
  71. void ctkPluginBrowser::pluginSelected(const QModelIndex &index)
  72. {
  73. QVariant v = index.data(Qt::UserRole);
  74. ctkPlugin* plugin = framework->getPluginContext()->getPlugin(v.toLongLong());
  75. if (!plugin) return;
  76. QAbstractItemModel* oldModel = ui.pluginResourcesTreeView->model();
  77. ui.pluginResourcesTreeView->setModel(new ctkPluginResourcesTreeModel(plugin, this));
  78. if (oldModel) oldModel->deleteLater();;
  79. }
  80. void ctkPluginBrowser::pluginDoubleClicked(const QModelIndex& index)
  81. {
  82. long pluginId = index.data(Qt::UserRole).toLongLong();
  83. ctkPlugin* plugin = framework->getPluginContext()->getPlugin(pluginId);
  84. QByteArray mfContent = plugin->getResource("/META-INF/MANIFEST.MF");
  85. QString location = QString("/") + plugin->getSymbolicName() + "/META-INF/MANIFEST.MF";
  86. editors->openEditor(location, mfContent, location + " [cached]");
  87. QList<ctkServiceReference*> serviceRefs = plugin->getPluginContext()->getServiceReferences("");
  88. QListIterator<ctkServiceReference*> it(serviceRefs);
  89. while (it.hasNext())
  90. {
  91. ctkServiceReference* ref = it.next();
  92. qDebug() << "Service from" << ref->getPlugin()->getSymbolicName() << ":" << ref->getPropertyKeys();
  93. qDebug() << "Object Classes:" << ref->getProperty(ctkPluginConstants::OBJECTCLASS).toStringList();
  94. }
  95. ctkServiceReference* cliRef = plugin->getPluginContext()->getServiceReference("ctkICLIManager");
  96. QObject* cliService = plugin->getPluginContext()->getService(cliRef);
  97. if (cliService)
  98. qDebug() << "Got service object: " << cliService->metaObject()->className();
  99. else qDebug() << "Got null service";
  100. }
  101. void ctkPluginBrowser::qtResourceDoubleClicked(const QModelIndex& index)
  102. {
  103. Q_UNUSED(index)
  104. }
  105. void ctkPluginBrowser::dbResourceDoubleClicked(const QModelIndex& index)
  106. {
  107. QString resPath = index.data(Qt::UserRole).toString();
  108. if (resPath.isEmpty() || resPath.endsWith('/')) return;
  109. qDebug() << "Trying to open: " << resPath;
  110. QModelIndex pluginIndex = ui.pluginsTableView->selectionModel()->selectedIndexes().first();
  111. long pluginId = pluginIndex.data(Qt::UserRole).toLongLong();
  112. ctkPlugin* plugin = framework->getPluginContext()->getPlugin(pluginId);
  113. QByteArray resContent = plugin->getResource(resPath);
  114. QString location = QString("/") + plugin->getSymbolicName() + resPath;
  115. editors->openEditor(location, resContent, location + " [cached]");
  116. }
  117. void ctkPluginBrowser::frameworkEvent(const ctkPluginFrameworkEvent& event)
  118. {
  119. qDebug() << "FrameworkEvent: [" << event.getPlugin()->getSymbolicName() << "]" << event.getErrorString();
  120. }