123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- /*=============================================================================
- Library: CTK
- Copyright (c) German Cancer Research Center,
- Division of Medical and Biological Informatics
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =============================================================================*/
- #include "ctkPluginBrowser.h"
- #include "ctkPluginTableModel.h"
- #include "ctkPluginResourcesTreeModel.h"
- #include "ctkQtResourcesTreeModel.h"
- #include "ctkServiceReference.h"
- #include "ctkServiceException.h"
- #include "ctkPluginConstants.h"
- #include <ui_ctkPluginBrowserMainWindow.h>
- #include <ctkPluginException.h>
- #include <ctkPluginFramework.h>
- #include <ctkPluginContext.h>
- #include <QApplication>
- #include <QMainWindow>
- #include <QStringList>
- #include <QDirIterator>
- #include <QUrl>
- #include <QSettings>
- #include <QCloseEvent>
- #define SETTINGS_WND_GEOM "mainwindow.geom"
- #define SETTINGS_WND_STATE "mainwindow.state"
- ctkPluginBrowser::ctkPluginBrowser(ctkPluginFramework* framework)
- : framework(framework)
- {
- pluginEventTypeToString[ctkPluginEvent::INSTALLED] = "Installed";
- pluginEventTypeToString[ctkPluginEvent::LAZY_ACTIVATION] = "Lazy Activation";
- pluginEventTypeToString[ctkPluginEvent::RESOLVED] = "Resolved";
- pluginEventTypeToString[ctkPluginEvent::STARTED] = "Started";
- pluginEventTypeToString[ctkPluginEvent::STARTING] = "Starting";
- pluginEventTypeToString[ctkPluginEvent::STOPPED] = "Stopped";
- pluginEventTypeToString[ctkPluginEvent::STOPPING] = "Stopping";
- pluginEventTypeToString[ctkPluginEvent::UNINSTALLED] = "Uninstalled";
- pluginEventTypeToString[ctkPluginEvent::UNRESOLVED] = "Unresolved";
- pluginEventTypeToString[ctkPluginEvent::UPDATED] = "Updated";
- framework->getPluginContext()->connectFrameworkListener(this, SLOT(frameworkEvent(ctkPluginFrameworkEvent)));
- framework->getPluginContext()->connectPluginListener(this, SLOT(pluginEvent(ctkPluginEvent)));
- framework->getPluginContext()->connectServiceListener(this, "serviceEvent");
- QStringList pluginDirs;
- #ifdef CMAKE_INTDIR
- pluginDirs << qApp->applicationDirPath() + "/../plugins/" CMAKE_INTDIR "/";
- #else
- pluginDirs << qApp->applicationDirPath() + "/plugins/";
- #endif
- QStringListIterator dirIt(pluginDirs);
- while (dirIt.hasNext())
- {
- QApplication::addLibraryPath(dirIt.next());
- }
- QStringList libFilter;
- libFilter << "*.dll" << "*.so" << "*.dylib";
- QDirIterator dirIter(pluginDirs.at(0), libFilter, QDir::Files);
- while(dirIter.hasNext())
- {
- try
- {
- framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(dirIter.next()).toString());
- //plugin->start(ctkPlugin::START_ACTIVATION_POLICY);
- }
- catch (const ctkPluginException& e)
- {
- qCritical() << e.what();
- }
- }
- framework->start();
- ui.setupUi(this);
- tabifyDockWidget(ui.qtResourcesDockWidget, ui.pluginResourcesDockWidget);
- editors = new ctkPluginBrowserEditors(ui.centralwidget);
- QAbstractItemModel* pluginTableModel = new ctkPluginTableModel(framework->getPluginContext(), this);
- ui.pluginsTableView->setModel(pluginTableModel);
- QAbstractItemModel* qtresourcesTreeModel = new ctkQtResourcesTreeModel(this);
- ui.qtResourcesTreeView->setModel(qtresourcesTreeModel);
- connect(ui.pluginsTableView, SIGNAL(clicked(QModelIndex)), this, SLOT(pluginSelected(QModelIndex)));
- connect(ui.pluginsTableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(pluginDoubleClicked(QModelIndex)));
- connect(ui.pluginResourcesTreeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(dbResourceDoubleClicked(QModelIndex)));
- connect(ui.qtResourcesTreeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(qtResourceDoubleClicked(QModelIndex)));
- startPluginNowAction = new QAction(QIcon(":/pluginbrowser/images/run-now.png"), "Start Plugin (ignore activation policy)", this);
- startPluginAction = new QAction(QIcon(":/pluginbrowser/images/run.png"), "Start Plugin", this);
- stopPluginAction = new QAction(QIcon(":/pluginbrowser/images/stop.png"), "Stop Plugin", this);
- connect(startPluginNowAction, SIGNAL(triggered()), this, SLOT(startPluginNow()));
- connect(startPluginAction, SIGNAL(triggered()), this, SLOT(startPlugin()));
- connect(stopPluginAction, SIGNAL(triggered()), this, SLOT(stopPlugin()));
- startPluginNowAction->setEnabled(false);
- startPluginAction->setEnabled(false);
- stopPluginAction->setEnabled(false);
- ui.pluginToolBar->addAction(startPluginNowAction);
- ui.pluginToolBar->addAction(startPluginAction);
- ui.pluginToolBar->addAction(stopPluginAction);
- QSettings settings;
- if(settings.contains(SETTINGS_WND_GEOM))
- {
- this->restoreGeometry(settings.value(SETTINGS_WND_GEOM).toByteArray());
- }
- if (settings.contains(SETTINGS_WND_STATE))
- {
- this->restoreState(settings.value(SETTINGS_WND_STATE).toByteArray());
- }
- }
- void ctkPluginBrowser::pluginSelected(const QModelIndex &index)
- {
- QVariant v = index.data(Qt::UserRole);
- QSharedPointer<ctkPlugin> plugin = framework->getPluginContext()->getPlugin(v.toLongLong());
- if (!plugin) return;
- updatePluginToolbar(plugin);
- QAbstractItemModel* oldModel = ui.pluginResourcesTreeView->model();
- ui.pluginResourcesTreeView->setModel(new ctkPluginResourcesTreeModel(plugin, this));
- if (oldModel) oldModel->deleteLater();;
- }
- void ctkPluginBrowser::updatePluginToolbar(QSharedPointer<ctkPlugin> plugin)
- {
- startPluginNowAction->setEnabled(false);
- startPluginAction->setEnabled(false);
- stopPluginAction->setEnabled(false);
- if (!plugin) return;
- const ctkPlugin::States startStates = ctkPlugin::INSTALLED | ctkPlugin::RESOLVED | ctkPlugin::STOPPING;
- const ctkPlugin::States stopStates = ctkPlugin::STARTING | ctkPlugin::ACTIVE;
- if (startStates.testFlag(plugin->getState()))
- {
- startPluginNowAction->setEnabled(true);
- startPluginAction->setEnabled(true);
- }
- if (stopStates.testFlag(plugin->getState()))
- {
- stopPluginAction->setEnabled(true);
- }
- }
- void ctkPluginBrowser::pluginDoubleClicked(const QModelIndex& index)
- {
- long pluginId = index.data(Qt::UserRole).toLongLong();
- QSharedPointer<ctkPlugin> plugin = framework->getPluginContext()->getPlugin(pluginId);
- QByteArray mfContent = plugin->getResource("/META-INF/MANIFEST.MF");
- QString location = QString("/") + plugin->getSymbolicName() + "/META-INF/MANIFEST.MF";
- editors->openEditor(location, mfContent, location + " [cached]");
- QList<ctkServiceReference> serviceRefs = plugin->getPluginContext()->getServiceReferences("");
- QListIterator<ctkServiceReference> it(serviceRefs);
- while (it.hasNext())
- {
- ctkServiceReference ref(it.next());
- qDebug() << "Service from" << ref.getPlugin()->getSymbolicName() << ":" << ref.getPropertyKeys();
- qDebug() << "Object Classes:" << ref.getProperty(ctkPluginConstants::OBJECTCLASS).toStringList();
- }
- }
- void ctkPluginBrowser::qtResourceDoubleClicked(const QModelIndex& index)
- {
- Q_UNUSED(index)
- }
- void ctkPluginBrowser::dbResourceDoubleClicked(const QModelIndex& index)
- {
- QString resPath = index.data(Qt::UserRole).toString();
- if (resPath.isEmpty() || resPath.endsWith('/')) return;
- qDebug() << "Trying to open: " << resPath;
- QModelIndex pluginIndex = ui.pluginsTableView->selectionModel()->selectedIndexes().first();
- long pluginId = pluginIndex.data(Qt::UserRole).toLongLong();
- QSharedPointer<ctkPlugin> plugin = framework->getPluginContext()->getPlugin(pluginId);
- QByteArray resContent = plugin->getResource(resPath);
- QString location = QString("/") + plugin->getSymbolicName() + resPath;
- editors->openEditor(location, resContent, location + " [cached]");
- }
- void ctkPluginBrowser::frameworkEvent(const ctkPluginFrameworkEvent& event)
- {
- qDebug() << "FrameworkEvent: [" << event.getPlugin()->getSymbolicName() << "]" << event.getErrorString();
- }
- void ctkPluginBrowser::pluginEvent(const ctkPluginEvent& event)
- {
- qDebug() << "PluginEvent: [" << event.getPlugin()->getSymbolicName() << "]" << pluginEventTypeToString[event.getType()];
- QSharedPointer<ctkPlugin> plugin = event.getPlugin();
- QModelIndexList selection = ui.pluginsTableView->selectionModel()->selectedIndexes();
- if (!selection.isEmpty() && selection.first().data(Qt::UserRole).toLongLong() == plugin->getPluginId())
- {
- updatePluginToolbar(plugin);
- }
- }
- void ctkPluginBrowser::serviceEvent(const ctkServiceEvent &event)
- {
- qDebug() << "ServiceEvent: [" << event.getType() << "]" << event.getServiceReference().getUsingPlugins();
- }
- void ctkPluginBrowser::startPlugin()
- {
- startPlugin(ctkPlugin::START_TRANSIENT | ctkPlugin::START_ACTIVATION_POLICY);
- }
- void ctkPluginBrowser::startPluginNow()
- {
- startPlugin(ctkPlugin::START_TRANSIENT);
- }
- void ctkPluginBrowser::startPlugin(ctkPlugin::StartOptions options)
- {
- QModelIndex selection = ui.pluginsTableView->selectionModel()->currentIndex();
- QVariant v = selection.data(Qt::UserRole);
- QSharedPointer<ctkPlugin> plugin = framework->getPluginContext()->getPlugin(v.toLongLong());
- plugin->start(options);
- }
- void ctkPluginBrowser::stopPlugin()
- {
- QModelIndex selection = ui.pluginsTableView->selectionModel()->currentIndex();
- QVariant v = selection.data(Qt::UserRole);
- QSharedPointer<ctkPlugin> plugin = framework->getPluginContext()->getPlugin(v.toLongLong());
- plugin->stop();
- }
- void ctkPluginBrowser::closeEvent(QCloseEvent *closeEvent)
- {
- QSettings settings;
- settings.setValue(SETTINGS_WND_GEOM, this->saveGeometry());
- settings.setValue(SETTINGS_WND_STATE, this->saveState());
- QMainWindow::closeEvent(closeEvent);
- }
|