diff --git a/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePage.cpp b/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePage.cpp index e013264a66..8b40802167 100644 --- a/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePage.cpp +++ b/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePage.cpp @@ -1,54 +1,244 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "QmitkSimulationPreferencePage.h" +#include +#include +#include +#include +#include + +typedef sofa::helper::system::Plugin Plugin; +typedef sofa::helper::system::PluginManager PluginManager; +typedef sofa::helper::system::PluginManager::PluginIterator PluginIterator; +typedef sofa::helper::system::PluginManager::PluginMap PluginMap; + +berry::IPreferences::Pointer getSimulationPreferences() +{ + berry::ServiceRegistry& serviceRegistry = berry::Platform::GetServiceRegistry(); + berry::IPreferencesService::Pointer preferencesService = serviceRegistry.GetServiceById(berry::IPreferencesService::ID); + berry::IPreferences::Pointer preferences = preferencesService->GetSystemPreferences(); + return preferences->Node("/org.mitk.views.simulation"); +} + +void initSOFAPlugins(berry::IPreferences::Pointer preferences) +{ + if (preferences.IsNull()) + return; + + QString pluginPaths = preferences->GetByteArray(QmitkSimulationPreferencePage::PLUGIN_PATHS, "").c_str(); + + if (pluginPaths.isEmpty()) + return; + + QStringList pluginPathList = pluginPaths.split(';', QString::SkipEmptyParts); + QStringListIterator it(pluginPathList); + + typedef sofa::helper::system::PluginManager PluginManager; + PluginManager& pluginManager = PluginManager::getInstance(); + + while (it.hasNext()) + { + std::string path = it.next().toStdString(); + std::ostringstream errlog; + + pluginManager.loadPlugin(path, &errlog); + + if (errlog.str().empty()) + pluginManager.getPluginMap()[path].initExternalModule(); + } +} + +const std::string QmitkSimulationPreferencePage::PLUGIN_PATHS = "plugin paths"; QmitkSimulationPreferencePage::QmitkSimulationPreferencePage() - : m_Control(NULL) + : m_Preferences(getSimulationPreferences()), + m_Control(NULL) { + initSOFAPlugins(m_Preferences); } QmitkSimulationPreferencePage::~QmitkSimulationPreferencePage() { } void QmitkSimulationPreferencePage::CreateQtControl(QWidget* parent) { m_Control = new QWidget(parent); m_Controls.setupUi(m_Control); + + QStringList headerLabels; + headerLabels << "Name" << "License" << "Version" << "Path"; + m_Controls.pluginsTreeWidget->setHeaderLabels(headerLabels); + + connect(m_Controls.pluginsTreeWidget, SIGNAL(itemSelectionChanged()), this, SLOT(OnPluginTreeWidgetItemSelectionChanged())); + connect(m_Controls.addButton, SIGNAL(clicked()), this, SLOT(OnAddButtonClicked())); + connect(m_Controls.removeButton, SIGNAL(clicked()), this, SLOT(OnRemoveButtonClicked())); + + this->Update(); } QWidget* QmitkSimulationPreferencePage::GetQtControl() const { return m_Control; } void QmitkSimulationPreferencePage::Init(berry::IWorkbench::Pointer) { } +void QmitkSimulationPreferencePage::OnAddButtonClicked() +{ + QString filter = "SOFA Plugins "; + +#if defined(__APPLE__) + filter += "(*.dylib*)"; +#elif defined(WIN32) + filter += "(*.dll)"; +#else + filter += "(*.so)"; +#endif + + std::string path = QFileDialog::getOpenFileName(m_Control, "Add SOFA Library", "", filter).toStdString(); + + PluginManager &pluginManager = PluginManager::getInstance(); + std::ostringstream errlog; + + if (pluginManager.loadPlugin(path, &errlog)) + { + if (!errlog.str().empty()) + { + QMessageBox* messageBox = new QMessageBox(m_Control); + messageBox->setIcon(QMessageBox::Warning); + messageBox->setStandardButtons(QMessageBox::Ok); + messageBox->setText(errlog.str().c_str()); + messageBox->setWindowTitle("Warning"); + messageBox->show(); + } + + Plugin& plugin = pluginManager.getPluginMap()[path]; + plugin.initExternalModule(); + + QStringList pluginItem; + + pluginItem + << plugin.getModuleName() + << plugin.getModuleLicense() + << plugin.getModuleVersion() + << path.c_str(); + + m_Controls.pluginsTreeWidget->addTopLevelItem(new QTreeWidgetItem(pluginItem)); + } + else + { + QMessageBox* messageBox = new QMessageBox(m_Control); + messageBox->setIcon(QMessageBox::Critical); + messageBox->setStandardButtons(QMessageBox::Ok); + messageBox->setText(errlog.str().c_str()); + messageBox->setWindowTitle("Error"); + messageBox->show(); + } +} + +void QmitkSimulationPreferencePage::OnPluginTreeWidgetItemSelectionChanged() +{ + QList selectedItems = m_Controls.pluginsTreeWidget->selectedItems(); + + if (!selectedItems.isEmpty()) + { + PluginMap& pluginMap = sofa::helper::system::PluginManager::getInstance().getPluginMap(); + std::string path = selectedItems[0]->text(3).toStdString(); + + m_Controls.descriptionPlainTextEdit->setPlainText(pluginMap[path].getModuleDescription()); + m_Controls.removeButton->setEnabled(true); + } + else + { + m_Controls.descriptionPlainTextEdit->clear(); + m_Controls.componentsListWidget->clear(); + m_Controls.removeButton->setDisabled(true); + } +} + +void QmitkSimulationPreferencePage::OnRemoveButtonClicked() +{ + QList selectedItems = m_Controls.pluginsTreeWidget->selectedItems(); + + if (selectedItems.isEmpty()) + return; + + std::string path = selectedItems[0]->text(3).toStdString(); + + PluginManager& pluginManager = PluginManager::getInstance(); + std::ostringstream errlog; + + if (pluginManager.unloadPlugin(path, &errlog)) + { + delete selectedItems[0]; + } + else + { + QMessageBox* messageBox = new QMessageBox(m_Control); + messageBox->setIcon(QMessageBox::Critical); + messageBox->setStandardButtons(QMessageBox::Ok); + messageBox->setText(errlog.str().c_str()); + messageBox->setWindowTitle("Error"); + messageBox->show(); + } +} + void QmitkSimulationPreferencePage::PerformCancel() { } bool QmitkSimulationPreferencePage::PerformOk() { + PluginManager& pluginManager = PluginManager::getInstance(); + PluginMap& pluginMap = pluginManager.getPluginMap(); + std::string pluginPaths; + + for (PluginIterator it = pluginMap.begin(); it != pluginMap.end(); ++it) + { + if (!pluginPaths.empty()) + pluginPaths += ";"; + + pluginPaths += it->first; + } + + m_Preferences->PutByteArray(PLUGIN_PATHS, pluginPaths); return true; } void QmitkSimulationPreferencePage::Update() { + PluginManager& pluginManager = PluginManager::getInstance(); + PluginMap& pluginMap = pluginManager.getPluginMap(); + + for (PluginIterator it = pluginMap.begin(); it != pluginMap.end(); ++it) + { + Plugin& plugin = it->second; + + QStringList pluginItem; + + pluginItem + << plugin.getModuleName() + << plugin.getModuleLicense() + << plugin.getModuleVersion() + << it->first.c_str(); + + m_Controls.pluginsTreeWidget->addTopLevelItem(new QTreeWidgetItem(pluginItem)); + } } diff --git a/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePage.h b/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePage.h index 1125354f07..484c855305 100644 --- a/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePage.h +++ b/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePage.h @@ -1,47 +1,57 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef QmitkSimulationPreferencePage_h #define QmitkSimulationPreferencePage_h #include #include #include #include +berry::IPreferences::Pointer getSimulationPreferences(); +void initSOFAPlugins(berry::IPreferences::Pointer preferences = getSimulationPreferences()); + class SIMULATION_EXPORT QmitkSimulationPreferencePage : public QObject, public berry::IQtPreferencePage { Q_OBJECT Q_INTERFACES(berry::IPreferencePage) public: + static const std::string PLUGIN_PATHS; + QmitkSimulationPreferencePage(); ~QmitkSimulationPreferencePage(); void CreateQtControl(QWidget* parent); QWidget* GetQtControl() const; void Init(berry::IWorkbench::Pointer workbench); void PerformCancel(); bool PerformOk(); void Update(); +private slots: + void OnAddButtonClicked(); + void OnPluginTreeWidgetItemSelectionChanged(); + void OnRemoveButtonClicked(); + private: + berry::IPreferences::Pointer m_Preferences; QWidget* m_Control; Ui::QmitkSimulationPreferencePageControls m_Controls; - berry::IPreferences::Pointer m_Preferences; }; #endif diff --git a/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePageControls.ui b/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePageControls.ui index 76892c3dd5..a7320be2ce 100644 --- a/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePageControls.ui +++ b/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePageControls.ui @@ -1,109 +1,157 @@ QmitkSimulationPreferencePageControls 0 0 640 480 - + Plugins - + + + QAbstractItemView::NoEditTriggers + true QAbstractItemView::SingleSelection QAbstractItemView::SelectRows - + + 0 + + + false + + false true - + false - + + 4 + + false + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + - - - - + Components - + Description - + true + + + + QAbstractItemView::NoEditTriggers + + + QAbstractItemView::NoSelection + + + QAbstractItemView::SelectRows + + + true + + + - + Add... - + + + false + Remove Qt::Horizontal 40 20 diff --git a/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationView.cpp b/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationView.cpp index 3d99571142..5bb6a5d37a 100644 --- a/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationView.cpp +++ b/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationView.cpp @@ -1,158 +1,161 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ +#include "QmitkSimulationPreferencePage.h" #include "QmitkSimulationView.h" #include #include #include +#include #include QmitkSimulationView::QmitkSimulationView() : m_Timer(this) { connect(&m_Timer, SIGNAL(timeout()), this, SLOT(OnTimerTimeout())); + initSOFAPlugins(); } QmitkSimulationView::~QmitkSimulationView() { } void QmitkSimulationView::CreateQtPartControl(QWidget* parent) { m_Controls.setupUi(parent); m_Controls.cmbSimulation->SetDataStorage(this->GetDataStorage()); m_Controls.cmbSimulation->SetPredicate(mitk::NodePredicateDataType::New("Simulation")); connect(m_Controls.btnAnimate, SIGNAL(toggled(bool)), this, SLOT(OnAnimateButtonToggled(bool))); connect(m_Controls.btnResetScene, SIGNAL(clicked()), this, SLOT(OnResetSceneButtonClicked())); connect(m_Controls.btnStep, SIGNAL(clicked()), this, SLOT(OnStepButtonClicked())); connect(m_Controls.cmbSimulation, SIGNAL(OnSelectionChanged(const mitk::DataNode*)), this, SLOT(OnSimulationComboBoxSelectionChanged(const mitk::DataNode*))); connect(m_Controls.spnDT, SIGNAL(valueChanged(double)), this, SLOT(OnDTSpinBoxValueChanged(double))); } void QmitkSimulationView::OnAnimateButtonToggled(bool toggled) { if (SetSelectionAsCurrentSimulation()) { mitk::Simulation::Pointer simulation = dynamic_cast(m_Controls.cmbSimulation->GetSelectedNode()->GetData()); sofa::simulation::Simulation::SPtr sofaSimulation = simulation->GetSimulation(); sofa::simulation::Node::SPtr rootNode = simulation->GetRootNode(); rootNode->getContext()->setAnimate(toggled); if (toggled) { m_Controls.btnStep->setEnabled(false); m_Timer.start(0); } } if (!toggled) { m_Timer.stop(); m_Controls.btnStep->setEnabled(true); } } void QmitkSimulationView::OnDTSpinBoxValueChanged(double value) { if (SetSelectionAsCurrentSimulation()) { mitk::Simulation::Pointer simulation = dynamic_cast(m_Controls.cmbSimulation->GetSelectedNode()->GetData()); sofa::simulation::Node::SPtr rootNode = simulation->GetRootNode(); rootNode->setDt(value == 0.0 ? simulation->GetDefaultDT() : value); } } void QmitkSimulationView::OnResetSceneButtonClicked() { if (SetSelectionAsCurrentSimulation()) { mitk::Simulation::Pointer simulation = dynamic_cast(m_Controls.cmbSimulation->GetSelectedNode()->GetData()); sofa::simulation::Simulation::SPtr sofaSimulation = simulation->GetSimulation(); sofa::simulation::Node::SPtr rootNode = simulation->GetRootNode(); m_Controls.spnDT->setValue(0.0); sofaSimulation->reset(rootNode.get()); rootNode->setTime(0.0); rootNode->execute(sofa::core::ExecParams::defaultInstance()); simulation->GetDrawTool()->Reset(); this->RequestRenderWindowUpdate(mitk::RenderingManager::REQUEST_UPDATE_3DWINDOWS); } } void QmitkSimulationView::OnSimulationComboBoxSelectionChanged(const mitk::DataNode* node) { if (m_Controls.btnAnimate->isChecked()) m_Controls.btnAnimate->setChecked(false); if (node != NULL) { m_Controls.grpSimulation->setEnabled(true); static_cast(node->GetData())->SetAsActiveSimulation(); } else { m_Controls.grpSimulation->setEnabled(false); mitk::Simulation::SetActiveSimulation(NULL); } } void QmitkSimulationView::OnStepButtonClicked() { if (SetSelectionAsCurrentSimulation()) { mitk::Simulation::Pointer simulation = dynamic_cast(m_Controls.cmbSimulation->GetSelectedNode()->GetData()); sofa::simulation::Simulation::SPtr sofaSimulation = simulation->GetSimulation(); sofa::simulation::Node::SPtr rootNode = simulation->GetRootNode(); simulation->GetDrawTool()->Reset(); sofaSimulation->animate(rootNode.get(), rootNode->getDt()); this->RequestRenderWindowUpdate(mitk::RenderingManager::REQUEST_UPDATE_3DWINDOWS); } } void QmitkSimulationView::SetFocus() { m_Controls.btnAnimate->setFocus(); } bool QmitkSimulationView::SetSelectionAsCurrentSimulation() const { mitk::DataNode::Pointer selectedNode = m_Controls.cmbSimulation->GetSelectedNode(); if (selectedNode.IsNotNull()) { static_cast(m_Controls.cmbSimulation->GetSelectedNode()->GetData())->SetAsActiveSimulation(); return true; } return false; } void QmitkSimulationView::OnTimerTimeout() { this->OnStepButtonClicked(); }