diff --git a/Modules/Simulation/mitkSimulationDrawTool.cpp b/Modules/Simulation/mitkSimulationDrawTool.cpp index 941dbdf00e..6b566f2ca6 100644 --- a/Modules/Simulation/mitkSimulationDrawTool.cpp +++ b/Modules/Simulation/mitkSimulationDrawTool.cpp @@ -1,627 +1,643 @@ /*=================================================================== 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 "mitkSimulation.h" #include "mitkSimulationDrawTool.h" #include #include #include #include #include #include #include #include #include #include #include #include mitk::SimulationDrawTool::SimulationDrawTool() : m_PolygonMode(0), m_Wireframe(false), m_Update(true) { } mitk::SimulationDrawTool::~SimulationDrawTool() { this->DeleteVtkObjects(); } void mitk::SimulationDrawTool::DeleteVtkObjects() { for (std::vector::const_iterator object = m_VtkObjects.begin(); object != m_VtkObjects.end(); ++object) (*object)->Delete(); m_VtkObjects.clear(); for (std::vector::const_iterator actor = m_Actors.begin(); actor != m_Actors.end(); ++actor) (*actor)->Delete(); m_Actors.clear(); } void mitk::SimulationDrawTool::DisableUpdate() { m_Update = false; } std::vector mitk::SimulationDrawTool::GetActors() const { return m_Actors; } void mitk::SimulationDrawTool::InitProperty(vtkProperty* property) const { if (m_Wireframe) property->SetRepresentationToWireframe(); else property->SetRepresentationToSurface(); } void mitk::SimulationDrawTool::Reset() { this->DeleteVtkObjects(); m_Update = true; } void mitk::SimulationDrawTool::drawPoints(const std::vector& points, float pointSize, const Vec4f color) { if (!m_Update || points.empty()) return; unsigned int numPoints = points.size(); vtkPoints* vtkPoints = vtkPoints::New(); vtkPoints->SetNumberOfPoints(numPoints); vtkCellArray* cellArray = vtkCellArray::New(); for (unsigned int i = 0; i < numPoints; ++i) { vtkPoints->SetPoint(i, points[i].elems); cellArray->InsertNextCell(1); cellArray->InsertCellPoint(i); } vtkPolyData* polyData = vtkPolyData::New(); polyData->SetPoints(vtkPoints); polyData->SetVerts(cellArray); vtkPoints->Delete(); cellArray->Delete(); vtkPolyDataMapper* polyDataMapper = vtkPolyDataMapper::New(); polyDataMapper->SetInput(polyData); vtkActor* actor = vtkActor::New(); actor->SetMapper(polyDataMapper); actor->SetScale(Simulation::ScaleFactor); vtkProperty* property = actor->GetProperty(); property->SetColor(color.x(), color.y(), color.z()); property->SetPointSize(pointSize); m_VtkObjects.push_back(polyData); m_VtkObjects.push_back(polyDataMapper); m_Actors.push_back(actor); } void mitk::SimulationDrawTool::drawLines(const std::vector& points, float lineWidth, const Vec4f color) { if (!m_Update || points.empty()) return; unsigned int numPoints = points.size(); std::vector indices; for (unsigned int i = 0; i < numPoints; i += 2) indices.push_back(Vec2i(i, i + 1)); this->drawLines(points, indices, lineWidth, color); } void mitk::SimulationDrawTool::drawLines(const std::vector& points, const std::vector& indices, float lineWidth, const Vec4f color) { if (!m_Update || points.empty()) return; unsigned int numPoints = points.size(); vtkPoints* vtkPoints = vtkPoints::New(); vtkPoints->SetNumberOfPoints(numPoints); for (unsigned int i = 0; i < numPoints; ++i) vtkPoints->SetPoint(i, points[i].elems); vtkPolyData* polyData = vtkPolyData::New(); polyData->SetPoints(vtkPoints); vtkPoints->Delete(); vtkCellArray* lines = vtkCellArray::New(); unsigned int numIndices = indices.size(); for (unsigned int i = 0; i < numIndices; ++i) { lines->InsertNextCell(2); lines->InsertCellPoint(indices[i].elems[0]); lines->InsertCellPoint(indices[i].elems[1]); } polyData->SetLines(lines); lines->Delete(); vtkPolyDataMapper* polyDataMapper = vtkPolyDataMapper::New(); polyDataMapper->SetInput(polyData); vtkActor* actor = vtkActor::New(); actor->SetMapper(polyDataMapper); actor->SetScale(Simulation::ScaleFactor); vtkProperty* property = actor->GetProperty(); property->SetLineWidth(lineWidth); property->SetColor(color.x(), color.y(), color.z()); m_VtkObjects.push_back(polyData); m_VtkObjects.push_back(polyDataMapper); m_Actors.push_back(actor); } void mitk::SimulationDrawTool::drawTriangles(const std::vector& points, const Vec4f color) { if (!m_Update || points.empty()) return; unsigned int numPoints = points.size(); vtkPoints* vtkPoints = vtkPoints::New(); vtkPoints->SetNumberOfPoints(numPoints); for (unsigned int i = 0; i < numPoints; ++i) vtkPoints->SetPoint(i, points[i].elems); vtkPolyData* polyData = vtkPolyData::New(); polyData->SetPoints(vtkPoints); vtkPoints->Delete(); vtkCellArray* triangles = vtkCellArray::New(); for (unsigned int i = 0; i < points.size(); i += 3) { triangles->InsertNextCell(3); triangles->InsertCellPoint(i); triangles->InsertCellPoint(i + 1); triangles->InsertCellPoint(i + 2); } polyData->SetPolys(triangles); triangles->Delete(); vtkPolyDataNormals* polyDataNormals = vtkPolyDataNormals::New(); polyDataNormals->ComputeCellNormalsOff(); polyDataNormals->SetInput(polyData); polyDataNormals->SplittingOff(); vtkPolyDataMapper* polyDataMapper = vtkPolyDataMapper::New(); polyDataMapper->SetInput(polyDataNormals->GetOutput()); vtkActor* actor = vtkActor::New(); actor->SetMapper(polyDataMapper); actor->SetScale(Simulation::ScaleFactor); vtkProperty* property = actor->GetProperty(); this->InitProperty(property); property->SetColor(color.x(), color.y(), color.z()); m_VtkObjects.push_back(polyData); m_VtkObjects.push_back(polyDataMapper); m_VtkObjects.push_back(polyDataNormals); m_Actors.push_back(actor); } void mitk::SimulationDrawTool::drawTriangles(const std::vector& points, const Vector3 normal, const Vec4f color) { if (!m_Update || points.empty()) return; unsigned int numPoints = points.size(); vtkPoints* vtkPoints = vtkPoints::New(); vtkPoints->SetNumberOfPoints(numPoints); for (unsigned int i = 0; i < numPoints; ++i) vtkPoints->SetPoint(i, points[i].elems); vtkPolyData* polyData = vtkPolyData::New(); polyData->SetPoints(vtkPoints); vtkPoints->Delete(); vtkCellArray* triangles = vtkCellArray::New(); for (unsigned int i = 0; i < points.size(); i += 3) { triangles->InsertNextCell(3); triangles->InsertCellPoint(i); triangles->InsertCellPoint(i + 1); triangles->InsertCellPoint(i + 2); } polyData->SetPolys(triangles); triangles->Delete(); vtkFloatArray* normals = vtkFloatArray::New(); normals->SetNumberOfComponents(3); normals->SetName("Normals"); for (int i = 0; i < numPoints; i += 3) normals->InsertNextTuple(normal.elems); polyData->GetCellData()->SetNormals(normals); normals->Delete(); vtkPolyDataMapper* polyDataMapper = vtkPolyDataMapper::New(); polyDataMapper->SetInput(polyData); vtkActor* actor = vtkActor::New(); actor->SetMapper(polyDataMapper); actor->SetScale(Simulation::ScaleFactor); vtkProperty* property = actor->GetProperty(); this->InitProperty(property); property->SetColor(color.x(), color.y(), color.z()); m_VtkObjects.push_back(polyData); m_VtkObjects.push_back(polyDataMapper); m_Actors.push_back(actor); } void mitk::SimulationDrawTool::drawTriangles(const std::vector& points, const std::vector& indices, const std::vector& normals, const Vec4f color) { if (!m_Update || points.empty() || indices.empty() || normals.empty()) return; unsigned int numPoints = points.size(); vtkPoints* vtkPoints = vtkPoints::New(); vtkPoints->SetNumberOfPoints(numPoints); for (unsigned int i = 0; i < numPoints; ++i) vtkPoints->SetPoint(i, points[i].elems); vtkPolyData* polyData = vtkPolyData::New(); polyData->SetPoints(vtkPoints); vtkPoints->Delete(); vtkCellArray* triangles = vtkCellArray::New(); unsigned int numIndices = indices.size(); for (unsigned int i = 0; i < numIndices; ++i) { triangles->InsertNextCell(3); triangles->InsertCellPoint(indices[i].elems[0]); triangles->InsertCellPoint(indices[i].elems[1]); triangles->InsertCellPoint(indices[i].elems[2]); } polyData->SetPolys(triangles); triangles->Delete(); unsigned int numNormals = normals.size(); vtkFloatArray* vtkNormals = vtkFloatArray::New(); vtkNormals->SetNumberOfComponents(3); vtkNormals->SetName("Normals"); for (int i = 0; i < numNormals; ++i) vtkNormals->InsertNextTuple(normals[i].elems); polyData->GetCellData()->SetNormals(vtkNormals); vtkNormals->Delete(); vtkPolyDataMapper* polyDataMapper = vtkPolyDataMapper::New(); polyDataMapper->SetInput(polyData); vtkActor* actor = vtkActor::New(); actor->SetMapper(polyDataMapper); actor->SetScale(Simulation::ScaleFactor); vtkProperty* property = actor->GetProperty(); this->InitProperty(property); property->SetColor(color.x(), color.y(), color.z()); m_VtkObjects.push_back(polyData); m_VtkObjects.push_back(polyDataMapper); m_Actors.push_back(actor); } void mitk::SimulationDrawTool::drawTriangles(const std::vector&, const std::vector&, const std::vector&) { } void mitk::SimulationDrawTool::drawTriangleStrip(const std::vector&, const std::vector&, const Vec4f) { } void mitk::SimulationDrawTool::drawTriangleFan(const std::vector&, const std::vector&, const Vec4f) { } void mitk::SimulationDrawTool::drawFrame(const Vector3& position, const Quaternion& orientation, const Vec3f& size) { if (!m_Update) return; - const float radius = 0.05f; + float minSize = std::min(std::min(size.x(), size.y()), size.z()); + float maxSize = std::max(std::max(size.x(), size.y()), size.z()); + + if (maxSize > minSize * 2.0f) + { + if (minSize > 0.0f) + maxSize = minSize * 2.0f; + else + minSize = maxSize * 0.707f; + } + + const float radii[] = { minSize * 0.1f, maxSize * 0.2f }; bool wireframeBackup = m_Wireframe; m_Wireframe = false; - std::vector colors; - colors.push_back(Vec4f(0.0f, 0.0f, 1.0f, 1.0f)); - colors.push_back(Vec4f(0.0f, 1.0f, 0.0f, 1.0f)); - colors.push_back(Vec4f(1.0f, 0.0f, 0.0f, 1.0f)); - if (size.x() != 0.0f) { Vector3 point2 = position + orientation.rotate(Vec3f(size.x(), 0.0f, 0.0f)); - this->drawArrow(position, point2, radius, colors.back()); - colors.pop_back(); + Vector3 point3 = point2 + orientation.rotate(Vec3f(radii[1], 0.0f, 0.0f)); + Vec4f red(1.0f, 0.0f, 0.0f, 1.0f); + + this->drawCylinder(position, point2, radii[0], red); + this->drawCone(point2, point3, radii[1], 0.0f, red); } if (size.y() != 0.0f) { Vector3 point2 = position + orientation.rotate(Vec3f(0.0f, size.y(), 0.0f)); - this->drawArrow(position, point2, radius, colors.back()); - colors.pop_back(); + Vector3 point3 = point2 + orientation.rotate(Vec3f(0.0f, radii[1], 0.0f)); + Vec4f green(0.0f, 1.0f, 0.0f, 1.0f); + + this->drawCylinder(position, point2, radii[0], green); + this->drawCone(point2, point3, radii[1], 0.0f, green); } if (size.z() != 0.0f) { Vector3 point2 = position + orientation.rotate(Vec3f(0.0f, 0.0f, size.z())); - this->drawArrow(position, point2, radius, colors.back()); + Vector3 point3 = point2 + orientation.rotate(Vec3f(0.0f, 0.0f, radii[1])); + Vec4f blue(0.0f, 0.0f, 1.0f, 1.0f); + + this->drawCylinder(position, point2, radii[0], blue); + this->drawCone(point2, point3, radii[1], 0.0f, blue); } m_Wireframe = wireframeBackup; } void mitk::SimulationDrawTool::drawSpheres(const std::vector& points, const std::vector& radii, const Vec4f color) { if (!m_Update || points.empty()) return; unsigned int numSpheres = points.size(); for (unsigned int i = 0; i < numSpheres; ++i) { vtkSphereSource *sphereSource = vtkSphereSource::New(); sphereSource->SetCenter(const_cast(points[i].elems)); sphereSource->SetRadius(radii[i]); sphereSource->SetPhiResolution(16); sphereSource->SetThetaResolution(32); sphereSource->LatLongTessellationOn(); vtkPolyDataMapper* polyDataMapper = vtkPolyDataMapper::New(); polyDataMapper->SetInput(sphereSource->GetOutput()); vtkActor* actor = vtkActor::New(); actor->SetMapper(polyDataMapper); actor->SetScale(Simulation::ScaleFactor); vtkProperty* property = actor->GetProperty(); this->InitProperty(property); property->SetColor(color.x(), color.y(), color.z()); m_VtkObjects.push_back(sphereSource); m_VtkObjects.push_back(polyDataMapper); m_Actors.push_back(actor); } } void mitk::SimulationDrawTool::drawSpheres(const std::vector& points, float radius, const Vec4f color) { if (!m_Update || points.empty()) return; unsigned int numPoints = points.size(); std::vector radii(numPoints, radius); this->drawSpheres(points, radii, color); } void mitk::SimulationDrawTool::drawCone(const Vector3& point1, const Vector3& point2, float radius1, float radius2, const Vec4f color, int subdivisions) { if (!m_Update) return; vtkPoints* points = vtkPoints::New(); points->SetNumberOfPoints(2); points->SetPoint(0, point1.elems); points->SetPoint(1, point2.elems); vtkCellArray* line = vtkCellArray::New(); line->InsertNextCell(2); line->InsertCellPoint(0); line->InsertCellPoint(1); vtkPolyData* polyData = vtkPolyData::New(); polyData->SetPoints(points); polyData->SetLines(line); points->Delete(); line->Delete(); const char* radiiName = "Radii"; vtkFloatArray* radii = vtkFloatArray::New(); radii->SetName(radiiName); radii->SetNumberOfTuples(2); radii->SetTuple1(0, radius1); radii->SetTuple1(1, radius2); vtkPointData* pointData = polyData->GetPointData(); pointData->AddArray(radii); pointData->SetActiveScalars(radiiName); radii->Delete(); vtkTubeFilter* tubeFilter = vtkTubeFilter::New(); tubeFilter->SetInput(polyData); tubeFilter->CappingOn(); tubeFilter->SetNumberOfSides(subdivisions); tubeFilter->SetVaryRadiusToVaryRadiusByAbsoluteScalar(); vtkPolyDataMapper* polyDataMapper = vtkPolyDataMapper::New(); polyDataMapper->SetInput(tubeFilter->GetOutput()); polyDataMapper->ScalarVisibilityOff(); vtkActor* actor = vtkActor::New(); actor->SetMapper(polyDataMapper); actor->SetScale(Simulation::ScaleFactor); vtkProperty* property = actor->GetProperty(); this->InitProperty(property); property->SetColor(color.x(), color.y(), color.z()); m_VtkObjects.push_back(polyData); m_VtkObjects.push_back(tubeFilter); m_VtkObjects.push_back(polyDataMapper); m_Actors.push_back(actor); } void mitk::SimulationDrawTool::drawCube(const float&, const Vec4f&, const int&) { } void mitk::SimulationDrawTool::drawCylinder(const Vector3& point1, const Vector3& point2, float radius, const Vec4f color, int subdivisions) { if (!m_Update) return; this->drawCone(point1, point2, radius, radius, color, subdivisions); } void mitk::SimulationDrawTool::drawCapsule(const Vector3&, const Vector3&, float, const Vec4f, int) { } void mitk::SimulationDrawTool::drawArrow(const Vector3& point1, const Vector3& point2, float radius, const Vec4f color, int subdivisions) { if (!m_Update) return; Vector3 point3 = point1 * 0.2f + point2 * 0.8f; this->drawCylinder(point1, point3, radius, color, subdivisions); this->drawCone(point3, point2, radius * 2.5f, 0.0f, color, subdivisions); } void mitk::SimulationDrawTool::drawPlus(const float& edgeRadius, const Vec4f& color, const int& subdivisions) { if (!m_Update) return; this->drawCylinder(Vector3(-1.0, 0.0, 0.0), Vector3(1.0, 0.0, 0.0), edgeRadius, color, subdivisions); this->drawCylinder(Vector3(0.0, -1.0, 0.0), Vector3(0.0, 1.0, 0.0), edgeRadius, color, subdivisions); this->drawCylinder(Vector3(0.0, 0.0, -1.0), Vector3(0.0, 0.0, 1.0), edgeRadius, color, subdivisions); } void mitk::SimulationDrawTool::drawPoint(const Vector3&, const Vec4f&) { } void mitk::SimulationDrawTool::drawPoint(const Vector3&, const Vector3&, const Vec4f&) { } void mitk::SimulationDrawTool::drawTriangle(const Vector3&, const Vector3&, const Vector3&, const Vector3&) { } void mitk::SimulationDrawTool::drawTriangle(const Vector3&, const Vector3&, const Vector3&, const Vector3&, const Vec4f&) { } void mitk::SimulationDrawTool::drawTriangle(const Vector3&, const Vector3&, const Vector3&, const Vector3&, const Vec4f&, const Vec4f&, const Vec4f&) { } void mitk::SimulationDrawTool::drawSphere(const Vector3&, float) { } void mitk::SimulationDrawTool::pushMatrix() { } void mitk::SimulationDrawTool::popMatrix() { } void mitk::SimulationDrawTool::multMatrix(float*) { } void mitk::SimulationDrawTool::scale(float) { } void mitk::SimulationDrawTool::setMaterial(const Vec4f&, std::string) { } void mitk::SimulationDrawTool::resetMaterial(const Vec4f&, std::string) { } void mitk::SimulationDrawTool::setPolygonMode(int mode, bool wireframe) { if (!m_Update) return; m_PolygonMode = mode; m_Wireframe = wireframe; } void mitk::SimulationDrawTool::setLightingEnabled(bool) { } void mitk::SimulationDrawTool::writeOverlayText(int, int, unsigned int, const Vec4f&, const char*) { } diff --git a/Plugins/org.mitk.gui.qt.simulation/files.cmake b/Plugins/org.mitk.gui.qt.simulation/files.cmake index 74291cf11e..342a0f859e 100644 --- a/Plugins/org.mitk.gui.qt.simulation/files.cmake +++ b/Plugins/org.mitk.gui.qt.simulation/files.cmake @@ -1,36 +1,39 @@ set(SRC_CPP_FILES ) set(INTERNAL_CPP_FILES org_mitk_gui_qt_simulation_Activator.cpp + QmitkSimulationPreferencePage.cpp QmitkSimulationView.cpp ) set(UI_FILES + src/internal/QmitkSimulationPreferencePageControls.ui src/internal/QmitkSimulationViewControls.ui ) set(MOC_H_FILES src/internal/org_mitk_gui_qt_simulation_Activator.h + src/internal/QmitkSimulationPreferencePage.h src/internal/QmitkSimulationView.h ) set(CACHED_RESOURCE_FILES resources/icon.png plugin.xml ) set(QRC_FILES resources/Simulation.qrc ) set(CPP_FILES ) foreach(file ${SRC_CPP_FILES}) set(CPP_FILES ${CPP_FILES} src/${file}) endforeach() foreach(file ${INTERNAL_CPP_FILES}) set(CPP_FILES ${CPP_FILES} src/internal/${file}) endforeach() diff --git a/Plugins/org.mitk.gui.qt.simulation/plugin.xml b/Plugins/org.mitk.gui.qt.simulation/plugin.xml index 36271f5cd6..b16639779a 100644 --- a/Plugins/org.mitk.gui.qt.simulation/plugin.xml +++ b/Plugins/org.mitk.gui.qt.simulation/plugin.xml @@ -1,9 +1,14 @@ + + + diff --git a/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePage.cpp b/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePage.cpp new file mode 100644 index 0000000000..8b40802167 --- /dev/null +++ b/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePage.cpp @@ -0,0 +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_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 new file mode 100644 index 0000000000..484c855305 --- /dev/null +++ b/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePage.h @@ -0,0 +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; +}; + +#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 new file mode 100644 index 0000000000..a7320be2ce --- /dev/null +++ b/Plugins/org.mitk.gui.qt.simulation/src/internal/QmitkSimulationPreferencePageControls.ui @@ -0,0 +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(); } diff --git a/Plugins/org.mitk.gui.qt.simulation/src/internal/org_mitk_gui_qt_simulation_Activator.cpp b/Plugins/org.mitk.gui.qt.simulation/src/internal/org_mitk_gui_qt_simulation_Activator.cpp index 3a15490521..d29e31d03b 100644 --- a/Plugins/org.mitk.gui.qt.simulation/src/internal/org_mitk_gui_qt_simulation_Activator.cpp +++ b/Plugins/org.mitk.gui.qt.simulation/src/internal/org_mitk_gui_qt_simulation_Activator.cpp @@ -1,40 +1,42 @@ /*=================================================================== 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 "org_mitk_gui_qt_simulation_Activator.h" +#include "QmitkSimulationPreferencePage.h" #include "QmitkSimulationView.h" #include #include #include void mitk::org_mitk_gui_qt_simulation_Activator::start(ctkPluginContext* context) { + BERRY_REGISTER_EXTENSION_CLASS(QmitkSimulationPreferencePage, context); BERRY_REGISTER_EXTENSION_CLASS(QmitkSimulationView, context); QmitkNodeDescriptorManager* nodeDescriptorManager = QmitkNodeDescriptorManager::GetInstance(); if (nodeDescriptorManager != NULL) { mitk::NodePredicateDataType::Pointer isSimulation = mitk::NodePredicateDataType::New("Simulation"); nodeDescriptorManager->AddDescriptor(new QmitkNodeDescriptor("Simulation", ":/Simulation/icon.png", isSimulation, nodeDescriptorManager)); } } void mitk::org_mitk_gui_qt_simulation_Activator::stop(ctkPluginContext*) { } Q_EXPORT_PLUGIN2(org_mitk_gui_qt_simulation, mitk::org_mitk_gui_qt_simulation_Activator)