diff --git a/Modules/Python/mitkIPythonService.cpp b/Modules/MitkExt/Controllers/mitkIPythonService.cpp similarity index 100% rename from Modules/Python/mitkIPythonService.cpp rename to Modules/MitkExt/Controllers/mitkIPythonService.cpp diff --git a/Modules/Python/mitkIPythonService.h b/Modules/MitkExt/Controllers/mitkIPythonService.h similarity index 84% rename from Modules/Python/mitkIPythonService.h rename to Modules/MitkExt/Controllers/mitkIPythonService.h index 5255aeeb63..bba15edce5 100644 --- a/Modules/Python/mitkIPythonService.h +++ b/Modules/MitkExt/Controllers/mitkIPythonService.h @@ -1,135 +1,132 @@ /*=================================================================== 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 mitkIPythonService_h #define mitkIPythonService_h // mitk -#include "mitkPythonExports.h" +#include "MitkExtExports.h" #include "mitkImage.h" //for microservices #include -// Qt -#include -#include -#include #include "mitkSurface.h" +#include namespace mitk { /// /// describes a python variable (data container) /// \see IPythonService::GetVariableStack() /// struct PythonVariable { - QString m_Name; - QString m_Type; - QString m_Value; + std::string m_Name; + std::string m_Type; + std::string m_Value; }; /// /// a PythonCommandObserver gets informed as soon as a python command was issued /// \see IPythonService::AddPythonCommandObserver() /// class PythonCommandObserver { public: - virtual void CommandExecuted(const QString& pythonCommand) = 0; + virtual void CommandExecuted(const std::string& pythonCommand) = 0; }; /// /// The central service for issuing Python Code /// The class also enables to transfer mitk images to python as itk::Image and vice versa /// \see IPythonService::GetVariableStack() /// - class MITK_PYTHON_EXPORT IPythonService + class MitkExt_EXPORT IPythonService { public: /// /// Constant representing a single line command /// \see IPythonService::Execute() static const int SINGLE_LINE_COMMAND = 0; /// /// Constant representing a command in which the commands are seperated by new lines, i.e. "\\n" /// \see IPythonService::Execute() static const int MULTI_LINE_COMMAND = 1; /// /// Constant representing a single line command x which is run as "eval(x)" /// \see IPythonService::Execute() static const int EVAL_COMMAND = 2; /// /// Executes a python command. - /// \return A variant containing the return value of the python code (if any) - virtual QVariant Execute( const QString& pythonCommand, int commandType = SINGLE_LINE_COMMAND ) = 0; + /// \return A variant containing the return value as string of the python code (if any) + virtual std::string Execute( const std::string& pythonCommand, int commandType = SINGLE_LINE_COMMAND ) = 0; /// /// \return The list of variables in the __main__ namespace - virtual QList GetVariableStack() const = 0; + virtual std::vector GetVariableStack() const = 0; /// /// adds a command observer which is informed after a command was issued with "Execute" virtual void AddPythonCommandObserver( PythonCommandObserver* observer ) = 0; /// /// removes a specific command observer virtual void RemovePythonCommandObserver( PythonCommandObserver* observer ) = 0; /// /// notify all observer. this should only be used if it can be garantueed that the /// current python interpreter instance got another command from anywhere else /// the the Execute() method of this service, e.g. the shell widget uses this function /// since it does not use Execute() - virtual void NotifyObserver( const QString& command ) = 0; + virtual void NotifyObserver( const std::string& command ) = 0; /// /// \return true, if itk wrapping is available, false otherwise virtual bool IsItkPythonWrappingAvailable() = 0; /// /// copies an mitk image as itk image into the python interpreter process /// the image will be available as "varName" in python if everythin worked /// \return true if image was copied, else false - virtual bool CopyToPythonAsItkImage( mitk::Image* image, const QString& varName ) = 0; + virtual bool CopyToPythonAsItkImage( mitk::Image* image, const std::string& varName ) = 0; /// /// copies an itk image from the python process that is named "varName" /// \return the image or 0 if copying was not possible - virtual mitk::Image::Pointer CopyItkImageFromPython( const QString& varName ) = 0; + virtual mitk::Image::Pointer CopyItkImageFromPython( const std::string& varName ) = 0; /// /// \return true, if OpenCv wrapping is available, false otherwise virtual bool IsOpenCvPythonWrappingAvailable() = 0; /// /// \see CopyToPythonAsItkImage() - virtual bool CopyToPythonAsCvImage( mitk::Image* image, const QString& varName ) = 0; + virtual bool CopyToPythonAsCvImage( mitk::Image* image, const std::string& varName ) = 0; /// /// \see CopyCvImageFromPython() - virtual mitk::Image::Pointer CopyCvImageFromPython( const QString& varName ) = 0; + virtual mitk::Image::Pointer CopyCvImageFromPython( const std::string& varName ) = 0; /// /// \return true, if vtk wrapping is available, false otherwise virtual bool IsVtkPythonWrappingAvailable() = 0; /// /// \see CopyToPythonAsItkImage() - virtual bool CopyToPythonAsVtkPolyData( mitk::Surface* surface, const QString& varName ) = 0; + virtual bool CopyToPythonAsVtkPolyData( mitk::Surface* surface, const std::string& varName ) = 0; /// /// \see CopyCvImageFromPython() - virtual mitk::Surface::Pointer CopyVtkPolyDataFromPython( const QString& varName ) = 0; + virtual mitk::Surface::Pointer CopyVtkPolyDataFromPython( const std::string& varName ) = 0; /// /// nothing to do here virtual ~IPythonService(); // leer in mitkIPythonService.cpp implementieren }; } US_DECLARE_SERVICE_INTERFACE(mitk::IPythonService, "org.mitk.services.IPythonService") #endif diff --git a/Modules/MitkExt/files.cmake b/Modules/MitkExt/files.cmake index 1f2936e157..4061c11ef5 100644 --- a/Modules/MitkExt/files.cmake +++ b/Modules/MitkExt/files.cmake @@ -1,150 +1,151 @@ set(CPP_FILES Algorithms/mitkMaskAndCutRoiImageFilter.cpp Algorithms/mitkBoundingObjectToSegmentationFilter.cpp Algorithms/vtkPointSetSlicer.cxx Algorithms/mitkCoreExtObjectFactory.cpp Algorithms/mitkAngleCorrectByPointFilter.cpp Algorithms/mitkAutoCropImageFilter.cpp Algorithms/mitkBoundingObjectCutter.cpp Algorithms/mitkCylindricToCartesianFilter.cpp Algorithms/mitkDopplerToStrainRateFilter.cpp Algorithms/mitkGeometryClipImageFilter.cpp Algorithms/mitkGeometryDataSource.cpp Algorithms/mitkHeightFieldSurfaceClipImageFilter.cpp Algorithms/mitkImageToLookupTableFilter.cpp Algorithms/mitkImageToSurfaceFilter.cpp Algorithms/mitkInterpolateLinesFilter.cpp Algorithms/mitkLabeledImageToSurfaceFilter.cpp Algorithms/mitkLabeledImageVolumeCalculator.cpp Algorithms/mitkLookupTableSource.cpp Algorithms/mitkMaskImageFilter.cpp Algorithms/mitkMeshSource.cpp Algorithms/mitkNonBlockingAlgorithm.cpp Algorithms/mitkPadImageFilter.cpp Algorithms/mitkPlaneCutFilter.cpp Algorithms/mitkPlaneFit.cpp Algorithms/mitkPlanesPerpendicularToLinesFilter.cpp Algorithms/mitkPointLocator.cpp Algorithms/mitkPointSetToCurvedGeometryFilter.cpp Algorithms/mitkPointSetToGeometryDataFilter.cpp Algorithms/mitkPointSetIndexToWorldTransformFilter.cpp Algorithms/mitkSurfaceIndexToWorldTransformFilter.cpp Algorithms/mitkPolygonToRingFilter.cpp Algorithms/mitkProbeFilter.cpp Algorithms/mitkSimpleHistogram.cpp Algorithms/mitkSimpleUnstructuredGridHistogram.cpp Algorithms/mitkSurfaceToImageFilter.cpp Algorithms/mitkUnstructuredGridHistogram.cpp Algorithms/mitkUnstructuredGridSource.cpp Algorithms/mitkVolumeVisualizationImagePreprocessor.cpp + Controllers/mitkIPythonService.cpp Controllers/mitkMovieGenerator.cpp Controllers/mitkMultiStepper.cpp Controllers/mitkToolManager.cpp DataManagement/mitkAffineTransformationOperation.cpp DataManagement/mitkApplyDiffImageOperation.cpp DataManagement/mitkBoundingObject.cpp DataManagement/mitkBoundingObjectGroup.cpp DataManagement/mitkCellOperation.cpp DataManagement/mitkColorConversions.cpp DataManagement/mitkColorSequence.cpp DataManagement/mitkColorSequenceCycleH.cpp DataManagement/mitkColorSequenceHalfTones.cpp DataManagement/mitkColorSequenceRainbow.cpp DataManagement/mitkCompressedImageContainer.cpp DataManagement/mitkCone.cpp DataManagement/mitkCuboid.cpp DataManagement/mitkCylinder.cpp DataManagement/mitkDataStorageSelection.cpp DataManagement/mitkDelegateManager.cpp DataManagement/mitkDrawOperation.cpp DataManagement/mitkEllipsoid.cpp DataManagement/mitkExternAbstractTransformGeometry.cpp DataManagement/mitkFrameOfReferenceUIDManager.cpp DataManagement/mitkGridRepresentationProperty.cpp DataManagement/mitkGridVolumeMapperProperty.cpp DataManagement/mitkItkBaseDataAdapter.cpp DataManagement/mitkLabeledImageLookupTable.cpp DataManagement/mitkLineOperation.cpp DataManagement/mitkMesh.cpp DataManagement/mitkObjectSet.cpp DataManagement/mitkOrganTypeProperty.cpp DataManagement/mitkPlaneLandmarkProjector.cpp DataManagement/mitkPlane.cpp DataManagement/mitkPropertyManager.cpp DataManagement/mitkPropertyObserver.cpp DataManagement/mitkSeedsImage.cpp DataManagement/mitkSeedsImageLookupTableSource.cpp DataManagement/mitkSphereLandmarkProjector.cpp # DataManagement/mitkUSLookupTableSource.cpp DataManagement/mitkUnstructuredGrid.cpp DataManagement/mitkVideoSource.cpp DataManagement/vtkObjectSet.cpp IO/mitkObjFileIOFactory.cpp IO/mitkObjFileReader.cpp IO/mitkPACSPlugin.cpp IO/mitkParRecFileIOFactory.cpp IO/mitkParRecFileReader.cpp IO/mitkStlVolumeTimeSeriesIOFactory.cpp IO/mitkStlVolumeTimeSeriesReader.cpp IO/mitkUnstructuredGridVtkWriter.cpp IO/mitkUnstructuredGridVtkWriterFactory.cpp IO/mitkVtkUnstructuredGridIOFactory.cpp IO/mitkVtkUnstructuredGridReader.cpp IO/mitkVtkVolumeTimeSeriesIOFactory.cpp IO/mitkVtkVolumeTimeSeriesReader.cpp Interactions/mitkConferenceEventMapper.cpp Interactions/mitkConnectPointsInteractor.cpp #Interactions/mitkCoordinateSupplier.cpp #Interactions/mitkDisplayCoordinateOperation.cpp #Interactions/mitkDisplayInteractor.cpp Interactions/mitkAffineInteractor3D.cpp Interactions/mitkDisplayPointSetInteractor.cpp #Interactions/mitkDisplayVectorInteractor.cpp Interactions/mitkInteractionDebug.cpp Interactions/mitkInteractionDebugger.cpp Interactions/mitkPointInteractor.cpp Interactions/mitkPointSelectorInteractor.cpp #Interactions/mitkPositionTracker.cpp Interactions/mitkSeedsInteractor.cpp Interactions/mitkSocketClient.cpp Interactions/mitkSurfaceDeformationInteractor3D.cpp Interactions/mitkSurfaceInteractor.cpp Interactions/mitkTool.cpp # Interactions/mitkCreateSurfaceTool.cpp Interactions/mitkMorphologicTool.cpp Interactions/mitkErodeTool.cpp Interactions/mitkDilateTool.cpp Interactions/mitkOpeningTool.cpp Interactions/mitkClosingTool.cpp Interactions/mitkPixelManipulationTool.cpp Rendering/mitkEnhancedPointSetVtkMapper3D.cpp Rendering/mitkImageBackground2D.cpp Rendering/mitkLineMapper2D.cpp # Rendering/mitkLineVtkMapper3D.cpp Rendering/mitkMeshMapper2D.cpp Rendering/mitkMeshVtkMapper3D.cpp Rendering/mitkNativeRenderWindowInteractor.cpp Rendering/mitkSplineMapper2D.cpp Rendering/mitkSplineVtkMapper3D.cpp Rendering/mitkUnstructuredGridMapper2D.cpp Rendering/mitkUnstructuredGridVtkMapper3D.cpp Rendering/mitkVectorImageMapper2D.cpp Rendering/vtkUnstructuredGridMapper.cpp Rendering/vtkMaskedGlyph2D.cpp Rendering/vtkMaskedGlyph3D.cpp Rendering/vtkMitkVolumeTextureMapper3D.cpp Rendering/vtkMitkOpenGLVolumeTextureMapper3D.cpp Rendering/mitkGPUVolumeMapper3D.cpp Rendering/vtkMitkGPUVolumeRayCastMapper.cpp ) if(WIN32 AND NOT MINGW) set(CPP_FILES Controllers/mitkMovieGeneratorWin32.cpp ${CPP_FILES} ) endif(WIN32 AND NOT MINGW) diff --git a/Modules/Python/CMakeLists.txt b/Modules/Python/CMakeLists.txt index 9c74db8b32..dcb9feddc7 100644 --- a/Modules/Python/CMakeLists.txt +++ b/Modules/Python/CMakeLists.txt @@ -1,13 +1,13 @@ if( MITK_USE_Python ) MITK_CREATE_MODULE(mitkPython - DEPENDS Mitk + DEPENDS MitkExt EXPORT_DEFINE MITK_PYTHON_EXPORT PACKAGE_DEPENDS QT CTK QT_MODULE ) configure_file(PythonPath.h.in "${CMAKE_CURRENT_BINARY_DIR}/PythonPath.h" @ONLY) add_subdirectory(Testing) endif() \ No newline at end of file diff --git a/Modules/Python/QmitkCtkPythonShell.cpp b/Modules/Python/QmitkCtkPythonShell.cpp index cabe523711..7eb4746901 100644 --- a/Modules/Python/QmitkCtkPythonShell.cpp +++ b/Modules/Python/QmitkCtkPythonShell.cpp @@ -1,81 +1,82 @@ /*=================================================================== 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 "QmitkCtkPythonShell.h" #include #include #include #include #include #include "mitkPythonService.h" #include #include #include struct QmitkCtkPythonShellData { mitk::PythonService* m_PythonService; }; QmitkCtkPythonShell::QmitkCtkPythonShell(QWidget* parent) : ctkPythonConsole(parent), d( new QmitkCtkPythonShellData ) { mitk::ModuleContext* context = mitk::GetModuleContext(); mitk::ServiceReference serviceRef = context->GetServiceReference(); d->m_PythonService = dynamic_cast ( context->GetService(serviceRef) ); assert( d->m_PythonService ); this->initialize( d->m_PythonService->GetPythonManager() ); } QmitkCtkPythonShell::~QmitkCtkPythonShell() { delete d; } void QmitkCtkPythonShell::dragEnterEvent(QDragEnterEvent *event) { event->accept(); } void QmitkCtkPythonShell::dropEvent(QDropEvent *event) { QList urls = event->mimeData()->urls(); for(int i = 0; i < urls.size(); i++) { - d->m_PythonService->Execute( urls[i].toString(), mitk::IPythonService::SINGLE_LINE_COMMAND ); + d->m_PythonService->Execute( urls[i].toString().toStdString(), mitk::IPythonService::SINGLE_LINE_COMMAND ); } } bool QmitkCtkPythonShell::canInsertFromMimeData( const QMimeData *source ) const { return true; } void QmitkCtkPythonShell::executeCommand(const QString& command) { MITK_DEBUG("QmitkCtkPythonShell") << "executing command " << command.toStdString(); ctkPythonConsole::executeCommand(command); - d->m_PythonService->NotifyObserver(command); + d->m_PythonService->NotifyObserver(command.toStdString()); } void QmitkCtkPythonShell::Paste(const QString &command) { if( this->isVisible() ) { - this->executeCommand( command ); + this->exec( command ); + //this->executeCommand( command ); } } diff --git a/Modules/Python/QmitkPythonTextEditor.cpp b/Modules/Python/QmitkPythonTextEditor.cpp index 1dbf1ae301..43d5bd8b29 100644 --- a/Modules/Python/QmitkPythonTextEditor.cpp +++ b/Modules/Python/QmitkPythonTextEditor.cpp @@ -1,180 +1,180 @@ /*=================================================================== 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 "QmitkPythonTextEditor.h" #include #include #include #include #include #include #include #include #include #include "QmitkPythonScriptEditorHighlighter.h" struct QmitkPythonTextEditorData { QString m_FilePath; QAction* m_LoadScript; QAction* m_SaveScript; QAction* m_RunScript; QToolBar* m_Toolbar; QTextEdit* m_Content; QGridLayout* m_Layout; mitk::IPythonService* m_PythonService; QString m_FileName; }; QmitkPythonTextEditor::QmitkPythonTextEditor(QWidget *parent) :QWidget(parent), d(new QmitkPythonTextEditorData) { mitk::ModuleContext* context = mitk::GetModuleContext(); mitk::ServiceReference serviceRef = context->GetServiceReference(); d->m_PythonService = context->GetService(serviceRef); d->m_LoadScript = new QAction(this); d->m_LoadScript->setToolTip("Load script from disk."); d->m_LoadScript->setObjectName(QString::fromUtf8("LoadScript")); QIcon icon2; icon2.addFile(QString::fromUtf8(":/mitkPython/document-open.png"), QSize(), QIcon::Normal, QIcon::Off); d->m_LoadScript->setIcon(icon2); d->m_SaveScript = new QAction(this); d->m_SaveScript->setToolTip("Save script to disk."); d->m_SaveScript->setObjectName(QString::fromUtf8("SaveScript")); QIcon icon3; icon3.addFile(QString::fromUtf8(":/mitkPython/document-save.png"), QSize(), QIcon::Normal, QIcon::Off); d->m_SaveScript->setIcon(icon3); d->m_RunScript = new QAction(this); d->m_RunScript->setToolTip("Run the current script."); d->m_RunScript->setObjectName(QString::fromUtf8("RunScript")); QIcon icon4; icon4.addFile(QString::fromUtf8(":/mitkPython/media-playback-start.png"), QSize(), QIcon::Normal, QIcon::Off); d->m_RunScript->setIcon(icon4); d->m_Toolbar = new QToolBar; d->m_Toolbar->addAction( d->m_LoadScript ); d->m_Toolbar->addAction( d->m_SaveScript ); d->m_Toolbar->addAction( d->m_RunScript ); d->m_Content = new QTextEdit(this); d->m_Content->setObjectName(QString::fromUtf8("Content")); QmitkPythonScriptEditorHighlighter* highlighter = new QmitkPythonScriptEditorHighlighter( d->m_Content->document() ); d->m_Layout = new QGridLayout; d->m_Layout->addWidget( d->m_Toolbar, 0, 0, 1, 1 ); d->m_Layout->addWidget( d->m_Content, 1, 0, 1, 1 ); d->m_Layout->setContentsMargins(2,2,2,2); this->setLayout(d->m_Layout); QMetaObject::connectSlotsByName(this); } QmitkPythonTextEditor::~QmitkPythonTextEditor() { delete d; } void QmitkPythonTextEditor::dragEnterEvent(QDragEnterEvent *event) { event->accept(); } void QmitkPythonTextEditor::dropEvent(QDropEvent *event) { QList urls = event->mimeData()->urls(); for(int i = 0; i < urls.size(); i++) { this->Paste( urls[i].toString() ); } } /* bool QmitkPythonTextEditor::canInsertFromMimeData( const QMimeData * ) const { return true; } */ void QmitkPythonTextEditor::Paste(const QString &command) { if( this->isVisible() ) { d->m_Content->insertPlainText(command + "\n"); } } QString QmitkPythonTextEditor::ReadFile(const QString& filename) { QFile file(filename); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { MITK_ERROR << "Could not open file " << filename; return NULL; } QByteArray total; QByteArray line; while (!file.atEnd()) { line = file.read(1024); total.append(line); } return QString(total); } void QmitkPythonTextEditor::on_SaveScript_triggered( bool ) { d->m_FileName = QFileDialog::getSaveFileName(this,tr("Save File"), d->m_FileName,tr("*.py")); if( d->m_FileName.compare("") != 0) { ofstream myfile; myfile.open(d->m_FileName.toLocal8Bit().data()); myfile << d->m_Content->toPlainText().toLocal8Bit().data(); myfile.close(); } } void QmitkPythonTextEditor::on_LoadScript_triggered( bool ) { d->m_FileName = QFileDialog::getOpenFileName( this, "Load Script", d->m_FileName, tr("*.py")); if( !d->m_FileName.isEmpty() ) { QString contents = this->ReadFile( d->m_FileName ); d->m_Content->setText(contents); } } void QmitkPythonTextEditor::on_RunScript_triggered( bool ) { if( !d->m_PythonService ) { MITK_ERROR << "Python service not available."; return; } - d->m_PythonService->Execute( d->m_Content->toPlainText(), mitk::IPythonService::MULTI_LINE_COMMAND ); + d->m_PythonService->Execute( d->m_Content->toPlainText().toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND ); } diff --git a/Modules/Python/QmitkPythonVariableStackTableModel.cpp b/Modules/Python/QmitkPythonVariableStackTableModel.cpp index fec5626ce7..a7e496ef7b 100755 --- a/Modules/Python/QmitkPythonVariableStackTableModel.cpp +++ b/Modules/Python/QmitkPythonVariableStackTableModel.cpp @@ -1,219 +1,219 @@ /*=================================================================== 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 "QmitkPythonVariableStackTableModel.h" #include #include #include #include #include #include #include const QString QmitkPythonVariableStackTableModel::MITK_IMAGE_VAR_NAME = "mitkImage"; const QString QmitkPythonVariableStackTableModel::MITK_SURFACE_VAR_NAME = "mitkSurface"; QmitkPythonVariableStackTableModel::QmitkPythonVariableStackTableModel(QObject *parent) :QAbstractTableModel(parent) { mitk::ModuleContext* context = mitk::GetModuleContext(); mitk::ServiceReference serviceRef = context->GetServiceReference(); m_PythonService = context->GetService(serviceRef); m_PythonService->AddPythonCommandObserver( this ); } QmitkPythonVariableStackTableModel::~QmitkPythonVariableStackTableModel() { m_PythonService->RemovePythonCommandObserver( this ); } bool QmitkPythonVariableStackTableModel::dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ) { // Early exit, returning true, but not actually doing anything (ignoring data). if (action == Qt::IgnoreAction) return true; // Note, we are returning true if we handled it, and false otherwise bool returnValue = false; if(data->hasFormat("application/x-mitk-datanodes")) { MITK_DEBUG("QmitkPythonVariableStackTableModel") << "dropped MITK DataNode"; returnValue = true; QString arg = QString(data->data("application/x-mitk-datanodes").data()); QStringList listOfDataNodeAddressPointers = arg.split(","); QStringList::iterator slIter; int i = 0; int j = 0; for (slIter = listOfDataNodeAddressPointers.begin(); slIter != listOfDataNodeAddressPointers.end(); slIter++) { long val = (*slIter).toLong(); mitk::DataNode* node = static_cast((void*)val); mitk::Image* mitkImage = dynamic_cast(node->GetData()); MITK_DEBUG("QmitkPythonVariableStackTableModel") << "mitkImage is not null " << (mitkImage != 0? "true": "false"); if( mitkImage ) { QString varName = MITK_IMAGE_VAR_NAME; if( i > 0 ) varName = QString("%1%2").arg(MITK_IMAGE_VAR_NAME).arg(i); MITK_DEBUG("varName") << "varName" << varName.toStdString(); bool exportAsCvImage = m_PythonService->IsOpenCvPythonWrappingAvailable(); if( mitkImage->GetDimension() == 2 && exportAsCvImage ) { int ret = QMessageBox::question(NULL, "Export option", "2D image detected. Export as OpenCV image to Python instead of an ITK image?", QMessageBox::Yes | QMessageBox::No, QMessageBox::No); exportAsCvImage = ret == QMessageBox::Yes; if(exportAsCvImage) { - m_PythonService->CopyToPythonAsCvImage( mitkImage, MITK_IMAGE_VAR_NAME ); + m_PythonService->CopyToPythonAsCvImage( mitkImage, MITK_IMAGE_VAR_NAME.toStdString() ); ++i; } } if( !exportAsCvImage ) { if( m_PythonService->IsItkPythonWrappingAvailable() ) { - m_PythonService->CopyToPythonAsItkImage( mitkImage, MITK_IMAGE_VAR_NAME ); + m_PythonService->CopyToPythonAsItkImage( mitkImage, MITK_IMAGE_VAR_NAME.toStdString() ); ++i; } else { MITK_ERROR << "ITK Python wrapping not available. Skipping export for image " << node->GetName(); } } } else { mitk::Surface* surface = dynamic_cast(node->GetData()); if( surface ) { QString varName = MITK_SURFACE_VAR_NAME; if( j > 0 ) varName = QString("%1%2").arg(MITK_SURFACE_VAR_NAME).arg(j); MITK_DEBUG("varName") << "varName" << varName; if( m_PythonService->IsVtkPythonWrappingAvailable() ) { - m_PythonService->CopyToPythonAsVtkPolyData( surface, MITK_SURFACE_VAR_NAME ); + m_PythonService->CopyToPythonAsVtkPolyData( surface, MITK_SURFACE_VAR_NAME.toStdString() ); ++j; } else { MITK_ERROR << "VTK Python wrapping not available. Skipping export for surface " << node->GetName(); } } } } } return returnValue; } QVariant QmitkPythonVariableStackTableModel::headerData(int section, Qt::Orientation orientation, int role) const { QVariant headerData; // show only horizontal header if ( role == Qt::DisplayRole ) { if( orientation == Qt::Horizontal ) { // first column: "Attribute" if(section == 0) headerData = "Attribute"; else if(section == 1) headerData = "Value"; else if(section == 2) headerData = "Type"; } } return headerData; } Qt::ItemFlags QmitkPythonVariableStackTableModel::flags(const QModelIndex &index) const { Qt::ItemFlags flags = QAbstractItemModel::flags(index); if(index.isValid()) return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | flags; else return Qt::ItemIsDropEnabled | flags; } int QmitkPythonVariableStackTableModel::rowCount(const QModelIndex &) const { return m_VariableStack.size(); } int QmitkPythonVariableStackTableModel::columnCount(const QModelIndex &) const { return 3; } QVariant QmitkPythonVariableStackTableModel::data(const QModelIndex &index, int role) const { if (index.isValid() && !m_VariableStack.empty()) { if(role == Qt::DisplayRole) { mitk::PythonVariable item = m_VariableStack.at(index.row()); if(index.column() == 0) - return item.m_Name; + return QString::fromStdString(item.m_Name); if(index.column() == 1) - return item.m_Value; + return QString::fromStdString(item.m_Value); if(index.column() == 2) - return item.m_Type; + return QString::fromStdString(item.m_Type); } } return QVariant(); } QStringList QmitkPythonVariableStackTableModel::mimeTypes() const { return QAbstractTableModel::mimeTypes(); QStringList types; types << "application/x-mitk-datanodes"; types << "application/x-qabstractitemmodeldatalist"; return types; } Qt::DropActions QmitkPythonVariableStackTableModel::supportedDropActions() const { return Qt::CopyAction | Qt::MoveAction; } -void QmitkPythonVariableStackTableModel::CommandExecuted(const QString &pythonCommand) +void QmitkPythonVariableStackTableModel::CommandExecuted(const std::string& pythonCommand) { - MITK_DEBUG("QmitkPythonVariableStackTableModel") << "command was executed " << pythonCommand.toStdString(); + MITK_DEBUG("QmitkPythonVariableStackTableModel") << "command was executed " << pythonCommand; m_VariableStack = m_PythonService->GetVariableStack(); QAbstractTableModel::reset(); } -QList QmitkPythonVariableStackTableModel::GetVariableStack() const +std::vector QmitkPythonVariableStackTableModel::GetVariableStack() const { return m_VariableStack; } diff --git a/Modules/Python/QmitkPythonVariableStackTableModel.h b/Modules/Python/QmitkPythonVariableStackTableModel.h index 8b9d933e85..c21fac358b 100755 --- a/Modules/Python/QmitkPythonVariableStackTableModel.h +++ b/Modules/Python/QmitkPythonVariableStackTableModel.h @@ -1,61 +1,61 @@ /*=================================================================== 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 QmitkPythonVariableStackTableModel_h #define QmitkPythonVariableStackTableModel_h #include #include #include #include "mitkIPythonService.h" #include "mitkPythonExports.h" /// /// implements a table model to show the variables of the Python "__main__" dictionary /// furthermore implements dragging and dropping of datanodes (conversion from and to python) /// class MITK_PYTHON_EXPORT QmitkPythonVariableStackTableModel : public QAbstractTableModel, public mitk::PythonCommandObserver { Q_OBJECT public: static const QString MITK_IMAGE_VAR_NAME; static const QString MITK_SURFACE_VAR_NAME; QmitkPythonVariableStackTableModel(QObject *parent = 0); virtual ~QmitkPythonVariableStackTableModel(); int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; Qt::ItemFlags flags( const QModelIndex& index ) const; virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const; QStringList mimeTypes() const; bool dropMimeData ( const QMimeData *, Qt::DropAction, int, int, const QModelIndex & ); Qt::DropActions supportedDropActions() const; Qt::DropActions supportedDragActions() const; - void CommandExecuted(const QString& pythonCommand); + void CommandExecuted(const std::string& pythonCommand); - QList GetVariableStack() const; + std::vector GetVariableStack() const; private: - QList m_VariableStack; + std::vector m_VariableStack; mitk::IPythonService* m_PythonService; }; #endif // QmitkPythonVariableStackTableModel_h diff --git a/Modules/Python/QmitkPythonVariableStackTableView.cpp b/Modules/Python/QmitkPythonVariableStackTableView.cpp index f3bf206bb1..4e44369e58 100755 --- a/Modules/Python/QmitkPythonVariableStackTableView.cpp +++ b/Modules/Python/QmitkPythonVariableStackTableView.cpp @@ -1,84 +1,84 @@ /*=================================================================== 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 "QmitkPythonVariableStackTableView.h" #include #include #include #include QmitkPythonVariableStackTableView::QmitkPythonVariableStackTableView(QWidget *parent) :QTableView(parent) { m_TableModel = new QmitkPythonVariableStackTableModel(parent); m_TableModel->CommandExecuted(""); this->setSelectionBehavior( QAbstractItemView::SelectRows ); this->setAlternatingRowColors(true); this->setDropIndicatorShown(true); this->setAcceptDrops(true); this->setModel( m_TableModel ); mitk::ModuleContext* context = mitk::GetModuleContext(); mitk::ServiceReference serviceRef = context->GetServiceReference(); m_PythonService = context->GetService(serviceRef); connect( this, SIGNAL(doubleClicked ( const QModelIndex& )), this, SLOT( OnVariableStackDoubleClicked(const QModelIndex&) ) ); } QmitkPythonVariableStackTableView::~QmitkPythonVariableStackTableView() { } void QmitkPythonVariableStackTableView::SetDataStorage(mitk::DataStorage *_DataStorage) { m_DataStorage = _DataStorage; } void QmitkPythonVariableStackTableView::OnVariableStackDoubleClicked(const QModelIndex &index) { if( m_DataStorage.IsNull() || m_PythonService == 0 ) { MITK_ERROR << "QmitkPythonVariableStackTableView not configured correctly. Quit"; return; } int row = index.row(); - QList variableStack = m_TableModel->GetVariableStack(); + std::vector variableStack = m_TableModel->GetVariableStack(); { MITK_DEBUG("QmitkPythonVariableStackTableView") << "row " << row; MITK_DEBUG("QmitkPythonVariableStackTableView") << "variableStack.size(): " << variableStack.size(); } - QString varName = variableStack.at(row).m_Name; + QString varName = QString::fromStdString( variableStack.at(row).m_Name ); { MITK_DEBUG("QmitkPythonVariableStackTableView") << varName.toStdString(); } - mitk::Image::Pointer mitkImage = m_PythonService->CopyItkImageFromPython(varName); + mitk::Image::Pointer mitkImage = m_PythonService->CopyItkImageFromPython(varName.toStdString()); if( mitkImage.IsNotNull() ) { std::string nodeName = varName.toStdString(); mitk::DataNode::Pointer node = mitk::DataNode::New(); node->SetName ( nodeName ); m_DataStorage->Add(node); node->SetData( mitkImage ); mitk::RenderingManager::GetInstance()->RequestUpdateAll(); } } diff --git a/Modules/Python/Testing/mitkPythonTest.cpp b/Modules/Python/Testing/mitkPythonTest.cpp index 383741a65f..01bcbcc9e9 100644 --- a/Modules/Python/Testing/mitkPythonTest.cpp +++ b/Modules/Python/Testing/mitkPythonTest.cpp @@ -1,23 +1,35 @@ /*=================================================================== 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 +#include +#include +#include +#include +#include int mitkPythonTest(int /*argc*/, char* /*argv*/[]) { - return EXIT_SUCCESS; + MITK_TEST_BEGIN("DICOMTestingSanity") + + mitk::ModuleContext* context = mitk::GetModuleContext(); + mitk::ServiceReference serviceRef = context->GetServiceReference(); + mitk::IPythonService* _PythonService = context->GetService(serviceRef); + + std::string result = _PythonService->Execute( "5+5", mitk::IPythonService::EVAL_COMMAND ); + MITK_TEST_CONDITION( result == "10", "Testing if running python code 5+5 results in 10" ) + + MITK_TEST_END() } diff --git a/Modules/Python/files.cmake b/Modules/Python/files.cmake index 581c111cf3..6750579650 100644 --- a/Modules/Python/files.cmake +++ b/Modules/Python/files.cmake @@ -1,28 +1,27 @@ SET(CPP_FILES mitkPythonActivator.cpp - mitkIPythonService.cpp mitkPythonService.cpp QmitkCtkPythonShell.cpp QmitkPythonVariableStackTableModel.cpp QmitkPythonVariableStackTableView.cpp QmitkPythonScriptEditorHighlighter.cpp QmitkPythonTextEditor.cpp QmitkPythonSnippets.cpp ) #SET(UI_FILES # QmitkPythonSnippets.ui #) SET(MOC_H_FILES QmitkCtkPythonShell.h QmitkPythonVariableStackTableModel.h QmitkPythonVariableStackTableView.h QmitkPythonScriptEditorHighlighter.h QmitkPythonTextEditor.h QmitkPythonSnippets.h ) set(QRC_FILES resources/mitkPython.qrc ) diff --git a/Modules/Python/mitkPythonService.cpp b/Modules/Python/mitkPythonService.cpp index ae86c6c6d6..41881e8f8a 100644 --- a/Modules/Python/mitkPythonService.cpp +++ b/Modules/Python/mitkPythonService.cpp @@ -1,422 +1,431 @@ /*=================================================================== 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 "mitkPythonService.h" #include #include #include #include #include #include "PythonPath.h" const QString mitk::PythonService::m_TmpImageName("temp_mitk_image"); mitk::PythonService::PythonService() : m_ItkWrappingAvailable( true ), m_OpenCVWrappingAvailable( true ), m_VtkWrappingAvailable( true ) { if( !m_PythonManager.isPythonInitialized() ) { MITK_DEBUG("PythonService") << "initialize python"; m_PythonManager.setInitializationFlags(PythonQt::RedirectStdOut); m_PythonManager.initialize(); - QVariant result = m_PythonManager.executeString( "sys.path", ctkAbstractPythonManager::EvalInput ); - MITK_DEBUG("mitk::PythonService") << "result of 'sys.path': " << result.toString().toStdString(); + //std::string result = m_PythonManager.executeString( "sys.path", ctkAbstractPythonManager::EvalInput ); + //MITK_DEBUG("mitk::PythonService") << "result of 'sys.path': " << result.toString().toStdString(); QString pythonCommand(PYTHONPATH_COMMAND); MITK_DEBUG("PythonService") << "registering python paths" << PYTHONPATH_COMMAND; m_PythonManager.executeString( pythonCommand, ctkAbstractPythonManager::FileInput ); MITK_DEBUG("mitk::PythonService") << "Trying to import ITK"; m_PythonManager.executeString( "import itk", ctkAbstractPythonManager::SingleInput ); m_ItkWrappingAvailable = !m_PythonManager.pythonErrorOccured(); MITK_DEBUG("mitk::PythonService") << "m_ItkWrappingAvailable: " << (m_ItkWrappingAvailable? "yes": "no"); if( !m_ItkWrappingAvailable ) { MITK_WARN << "ITK Python wrapping not available. Please check build settings or PYTHON_PATH settings."; } MITK_DEBUG("mitk::PythonService") << "Trying to import OpenCv"; m_PythonManager.executeString( "import cv2", ctkAbstractPythonManager::SingleInput ); m_OpenCVWrappingAvailable = !m_PythonManager.pythonErrorOccured(); MITK_DEBUG("mitk::PythonService") << "m_OpenCVWrappingAvailable: " << (m_OpenCVWrappingAvailable? "yes": "no"); if( !m_OpenCVWrappingAvailable ) { MITK_WARN << "OpenCV Python wrapping not available. Please check build settings or PYTHON_PATH settings."; } MITK_DEBUG("mitk::PythonService") << "Trying to import VTK"; m_PythonManager.executeString( "import vtk", ctkAbstractPythonManager::SingleInput ); m_VtkWrappingAvailable = !m_PythonManager.pythonErrorOccured(); MITK_DEBUG("mitk::PythonService") << "m_VtkWrappingAvailable: " << (m_VtkWrappingAvailable? "yes": "no"); if( !m_VtkWrappingAvailable ) { MITK_WARN << "VTK Python wrapping not available. Please check build settings or PYTHON_PATH settings."; } } - //QVariant result = m_PythonManager.executeString( "5+5", ctkAbstractPythonManager::EvalInput ); + //std::string result = m_PythonManager.executeString( "5+5", ctkAbstractPythonManager::EvalInput ); //MITK_DEBUG("mitk::PythonService") << "result of '5+5': " << result.toString().toStdString(); } mitk::PythonService::~PythonService() { } -QVariant mitk::PythonService::Execute(const QString &pythonCommand, int commandType) +std::string mitk::PythonService::Execute(const std::string &stdpythonCommand, int commandType) { + QString pythonCommand = QString::fromStdString(stdpythonCommand); + { MITK_DEBUG("mitk::PythonService") << "pythonCommand = " << pythonCommand.toStdString(); MITK_DEBUG("mitk::PythonService") << "commandType = " << commandType; } QVariant result; bool commandIssued = true; if(commandType == IPythonService::SINGLE_LINE_COMMAND ) result = m_PythonManager.executeString(pythonCommand, ctkAbstractPythonManager::SingleInput ); else if(commandType == IPythonService::MULTI_LINE_COMMAND ) result = m_PythonManager.executeString(pythonCommand, ctkAbstractPythonManager::FileInput ); else if(commandType == IPythonService::EVAL_COMMAND ) result = m_PythonManager.executeString(pythonCommand, ctkAbstractPythonManager::EvalInput ); else commandIssued = false; if(commandIssued) - this->NotifyObserver(pythonCommand); + this->NotifyObserver(pythonCommand.toStdString()); - return result; + return result.toString().toStdString(); } -QList mitk::PythonService::GetVariableStack() const +std::vector mitk::PythonService::GetVariableStack() const { - QList list; + std::vector list; PyObject* dict = PyImport_GetModuleDict(); PyObject* object = PyDict_GetItemString(dict, "__main__"); PyObject* dirMain = PyObject_Dir(object); PyObject* tempObject; if(dirMain) { - QString attr, attrValue, attrType; + std::string attr, attrValue, attrType; for(int i = 0; iob_type->tp_name; if(PyUnicode_Check(tempObject) || PyString_Check(tempObject)) attrValue = PyString_AsString(tempObject); else attrValue = ""; mitk::PythonVariable var; var.m_Name = attr; var.m_Value = attrValue; var.m_Type = attrType; - list.append(var); + list.push_back(var); } } return list; } void mitk::PythonService::AddPythonCommandObserver(mitk::PythonCommandObserver *observer) { if(!m_Observer.contains(observer)) m_Observer.append(observer); } void mitk::PythonService::RemovePythonCommandObserver(mitk::PythonCommandObserver *observer) { m_Observer.removeOne(observer); } -void mitk::PythonService::NotifyObserver(const QString &command) +void mitk::PythonService::NotifyObserver(const std::string &command) { MITK_DEBUG("mitk::PythonService") << "number of observer " << m_Observer.size(); - foreach(mitk::PythonCommandObserver* observer, m_Observer) + for( size_t i=0; i< m_Observer.size(); ++i ) { - observer->CommandExecuted(command); + m_Observer.at(i)->CommandExecuted(command); } } -QString mitk::PythonService::GetTempImageName(const QString& ext) const +QString mitk::PythonService::GetTempImageName(const std::string& ext) const { QString tmpFolder = QDir::tempPath(); - QString fileName = tmpFolder + QDir::separator() + m_TmpImageName + ext; + QString fileName = tmpFolder + QDir::separator() + m_TmpImageName + QString::fromStdString(ext); return fileName; } -bool mitk::PythonService::CopyToPythonAsItkImage(mitk::Image *image, const QString &varName) +bool mitk::PythonService::CopyToPythonAsItkImage(mitk::Image *image, const std::string &stdvarName) { + QString varName = QString::fromStdString( stdvarName ); // save image - QString fileName = this->GetTempImageName( QString::fromStdString(mitk::IOUtil::DEFAULTIMAGEEXTENSION) ); + QString fileName = this->GetTempImageName( mitk::IOUtil::DEFAULTIMAGEEXTENSION ); - MITK_DEBUG("PythonService") << "Saving temporary file " << fileName.toStdString(); + MITK_DEBUG("PythonService") << "Saving temporary file " << fileName; if( !mitk::IOUtil::SaveImage(image, fileName.toStdString()) ) { MITK_ERROR << "Temporary file could not be created."; } else { // TODO CORRECT TYPE SETUP, MAKE MITK_DEBUG("PythonService") MITK_DEBUG("PythonService") int dim = image->GetDimension(); mitk::PixelType pixelType = image->GetPixelType(); itk::ImageIOBase::IOPixelType ioPixelType = image->GetPixelType().GetPixelTypeId(); // default pixeltype: unsigned short QString type = "US"; if( ioPixelType == itk::ImageIOBase::SCALAR ) { if( pixelType.GetTypeId() == typeid(double) ) type = "D"; else if( pixelType.GetTypeId() == typeid(float) ) type = "F"; if( pixelType.GetTypeId() == typeid(long double) ) type = "LD"; else if( pixelType.GetTypeId() == typeid(short) ) type = "SS"; else if( pixelType.GetTypeId() == typeid(signed char) ) type = "SC"; else if( pixelType.GetTypeId() == typeid(signed int) ) type = "SI"; else if( pixelType.GetTypeId() == typeid(signed long) ) type = "SL"; else if( pixelType.GetTypeId() == typeid(signed short) ) type = "SS"; else if( pixelType.GetTypeId() == typeid(unsigned char) ) type = "UC"; else if( pixelType.GetTypeId() == typeid(unsigned int) ) type = "UI"; else if( pixelType.GetTypeId() == typeid(unsigned long) ) type = "UL"; else if( pixelType.GetTypeId() == typeid(unsigned short) ) type = "US"; } MITK_DEBUG("PythonService") << "Got mitk image with type " << type.toStdString() << " and dim " << dim; QString command; command.append( QString("imageType = itk.Image[itk.%1, %2]\n") .arg( type ).arg( dim ) ); command.append( QString("readerType = itk.ImageFileReader[imageType]\n") ); command.append( QString("reader = readerType.New()\n") ); command.append( QString("reader.SetFileName( \"%1\" )\n") .arg(fileName) ); command.append( QString("reader.Update()\n") ); command.append( QString("%1 = reader.GetOutput()\n").arg( varName ) ); MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); - this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); + this->Execute( command.toStdString(), IPythonService::MULTI_LINE_COMMAND ); - QFile file(fileName); + QFile file( fileName ); MITK_DEBUG("PythonService") << "Removing file " << fileName.toStdString(); file.remove(); return true; } return false; } -mitk::Image::Pointer mitk::PythonService::CopyItkImageFromPython(const QString &varName) +mitk::Image::Pointer mitk::PythonService::CopyItkImageFromPython(const std::string &stdvarName) { + QString varName = QString::fromStdString( stdvarName ); mitk::Image::Pointer mitkImage; QString command; - QString fileName = GetTempImageName( QString::fromStdString( mitk::IOUtil::DEFAULTIMAGEEXTENSION ) ); + QString fileName = GetTempImageName( mitk::IOUtil::DEFAULTIMAGEEXTENSION ); MITK_DEBUG("PythonService") << "Saving temporary file with python itk code " << fileName.toStdString(); command.append( QString( "writer = itk.ImageFileWriter[ %1 ].New()\n").arg( varName ) ); command.append( QString( "writer.SetFileName( \"%1\" )\n").arg(fileName) ); command.append( QString( "writer.SetInput( %1 )\n").arg(varName) ); command.append( QString( "writer.Update()\n" ) ); MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); - this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); + this->Execute(command.toStdString(), IPythonService::MULTI_LINE_COMMAND ); try { MITK_DEBUG("PythonService") << "Loading temporary file " << fileName.toStdString() << " as MITK image"; mitkImage = mitk::IOUtil::LoadImage( fileName.toStdString() ); } catch(std::exception& e) { MITK_ERROR << e.what(); } QFile file(fileName); if( file.exists() ) { MITK_DEBUG("PythonService") << "Removing temporary file " << fileName.toStdString(); file.remove(); } return mitkImage; } -bool mitk::PythonService::CopyToPythonAsCvImage( mitk::Image* image, const QString& varName ) +bool mitk::PythonService::CopyToPythonAsCvImage( mitk::Image* image, const std::string& stdvarName ) { + QString varName = QString::fromStdString( stdvarName ); + bool convert = false; if(image->GetDimension() != 2) { MITK_ERROR << "Only 2D images allowed for OpenCV images"; return convert; } // try to save mitk image - QString fileName = this->GetTempImageName( QString::fromStdString(".bmp") ); + QString fileName = this->GetTempImageName( ".bmp" ); MITK_DEBUG("PythonService") << "Saving temporary file " << fileName.toStdString(); if( !mitk::IOUtil::SaveImage(image, fileName.toStdString()) ) { MITK_ERROR << "Temporary file " << fileName.toStdString() << " could not be created."; return convert; } QString command; command.append( QString("%1 = cv.LoadImage(\"%2\")\n") .arg( varName ).arg( fileName ) ); MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); - this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); + this->Execute(command.toStdString(), IPythonService::MULTI_LINE_COMMAND ); MITK_DEBUG("PythonService") << "Removing file " << fileName.toStdString(); QFile file(fileName); file.remove(); convert = true; return convert; } -mitk::Image::Pointer mitk::PythonService::CopyCvImageFromPython( const QString& varName ) +mitk::Image::Pointer mitk::PythonService::CopyCvImageFromPython( const std::string& stdvarName ) { + QString varName = QString::fromStdString( stdvarName ); mitk::Image::Pointer mitkImage; QString command; - QString fileName = GetTempImageName( QString::fromStdString( ".bmp" ) ); + QString fileName = GetTempImageName( ".bmp" ); MITK_DEBUG("PythonService") << "run python command to save image with opencv to " << fileName.toStdString(); command.append( QString( "cv.SaveImage(\"%1\", %2)\n").arg( fileName ).arg( varName ) ); MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); - this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); + this->Execute(command.toStdString(), IPythonService::MULTI_LINE_COMMAND ); try { MITK_DEBUG("PythonService") << "Loading temporary file " << fileName.toStdString() << " as MITK image"; mitkImage = mitk::IOUtil::LoadImage( fileName.toStdString() ); } catch(std::exception& e) { MITK_ERROR << e.what(); } QFile file(fileName); if( file.exists() ) { MITK_DEBUG("PythonService") << "Removing temporary file " << fileName.toStdString(); file.remove(); } return mitkImage; } ctkAbstractPythonManager *mitk::PythonService::GetPythonManager() { return &m_PythonManager; } -mitk::Surface::Pointer mitk::PythonService::CopyVtkPolyDataFromPython( const QString& varName ) +mitk::Surface::Pointer mitk::PythonService::CopyVtkPolyDataFromPython( const std::string& stdvarName ) { + QString varName = QString::fromStdString( stdvarName ); mitk::Surface::Pointer newSurface; QString command; QString fileName = GetTempImageName( ".stl" ); MITK_DEBUG("PythonService") << "run python command to save polydata with vtk to " << fileName.toStdString(); command = QString ( "vtkStlWriter = vtk.vtkSTLWriter()\n" "vtkStlWriter.SetInput(%1)\n" "vtkStlWriter.SetFileName(\"%2\")\n" "vtkStlWriter.Write()\n").arg(varName).arg(fileName); MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); - this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); + this->Execute(command.toStdString(), IPythonService::MULTI_LINE_COMMAND ); try { MITK_DEBUG("PythonService") << "Loading temporary file " << fileName.toStdString() << " as MITK Surface"; newSurface = mitk::IOUtil::LoadSurface( fileName.toStdString() ); } catch(std::exception& e) { MITK_ERROR << e.what(); } QFile file(fileName); if( file.exists() ) { MITK_DEBUG("PythonService") << "Removing temporary file " << fileName.toStdString(); file.remove(); } return newSurface; } -bool mitk::PythonService::CopyToPythonAsVtkPolyData( mitk::Surface* surface, const QString& varName ) +bool mitk::PythonService::CopyToPythonAsVtkPolyData( mitk::Surface* surface, const std::string& stdvarName ) { + QString varName = QString::fromStdString( stdvarName ); bool convert = false; // try to save mitk image QString fileName = this->GetTempImageName( ".stl" ); MITK_DEBUG("PythonService") << "Saving temporary file " << fileName.toStdString(); if( !mitk::IOUtil::SaveSurface( surface, fileName.toStdString() ) ) { MITK_ERROR << "Temporary file " << fileName.toStdString() << " could not be created."; return convert; } QString command; command.append( QString("vtkStlReader = vtk.vtkSTLReader()\n") ); command.append( QString("vtkStlReader.SetFileName(\"%1\")\n").arg( fileName ) ); command.append( QString("%1 = vtkStlReader.GetOutput()").arg( fileName ) ); MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); - this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); + this->Execute(command.toStdString(), IPythonService::MULTI_LINE_COMMAND ); MITK_DEBUG("PythonService") << "Removing file " << fileName.toStdString(); QFile file(fileName); file.remove(); convert = true; return convert; } bool mitk::PythonService::IsItkPythonWrappingAvailable() { - return m_ItkWrappingAvailable; //TODO + return m_ItkWrappingAvailable; } bool mitk::PythonService::IsOpenCvPythonWrappingAvailable() { - return m_OpenCVWrappingAvailable; //TODO + return m_OpenCVWrappingAvailable; } bool mitk::PythonService::IsVtkPythonWrappingAvailable() { - return m_VtkWrappingAvailable; //TODO + return m_VtkWrappingAvailable; } diff --git a/Modules/Python/mitkPythonService.h b/Modules/Python/mitkPythonService.h index 8428504ee6..73a4ad6e2a 100644 --- a/Modules/Python/mitkPythonService.h +++ b/Modules/Python/mitkPythonService.h @@ -1,94 +1,94 @@ /*=================================================================== 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 mitkPythonService_h #define mitkPythonService_h #include "mitkIPythonService.h" #include #include #include "mitkSurface.h" namespace mitk { /// /// implementation of the IPythonService using ctkabstractpythonmanager /// \see IPythonService class PythonService: public itk::LightObject, public mitk::IPythonService { public: /// /// instantiate python manager here PythonService(); /// /// empty implementation... ~PythonService(); /// /// \see IPythonService::Execute() - QVariant Execute( const QString& pythonCommand, int commandType = SINGLE_LINE_COMMAND ); + std::string Execute( const std::string& pythonCommand, int commandType = SINGLE_LINE_COMMAND ); /// /// \see IPythonService::GetVariableStack() - QList GetVariableStack() const; + std::vector GetVariableStack() const; /// /// \see IPythonService::AddPythonCommandObserver() void AddPythonCommandObserver( PythonCommandObserver* observer ); /// /// \see IPythonService::RemovePythonCommandObserver() void RemovePythonCommandObserver( PythonCommandObserver* observer ); /// /// \see IPythonService::NotifyObserver() - void NotifyObserver( const QString& command ); + void NotifyObserver( const std::string& command ); /// /// \see IPythonService::IsItkPythonWrappingAvailable() bool IsItkPythonWrappingAvailable(); /// /// \see IPythonService::CopyToPythonAsItkImage() - bool CopyToPythonAsItkImage( mitk::Image* image, const QString& varName ); + bool CopyToPythonAsItkImage( mitk::Image* image, const std::string& varName ); /// /// \see IPythonService::CopyItkImageFromPython() - mitk::Image::Pointer CopyItkImageFromPython( const QString& varName ); + mitk::Image::Pointer CopyItkImageFromPython( const std::string& varName ); /// /// \see IPythonService::IsOpenCvPythonWrappingAvailable() bool IsOpenCvPythonWrappingAvailable(); /// /// \see IPythonService::CopyToPythonAsCvImage() - bool CopyToPythonAsCvImage( mitk::Image* image, const QString& varName ); + bool CopyToPythonAsCvImage( mitk::Image* image, const std::string& varName ); /// /// \see IPythonService::CopyCvImageFromPython() - mitk::Image::Pointer CopyCvImageFromPython( const QString& varName ); + mitk::Image::Pointer CopyCvImageFromPython( const std::string& varName ); /// /// \see IPythonService::IsVtkPythonWrappingAvailable() bool IsVtkPythonWrappingAvailable(); /// /// \see IPythonService::CopyToPythonAsVtkPolyData() - bool CopyToPythonAsVtkPolyData( mitk::Surface* surface, const QString& varName ); + bool CopyToPythonAsVtkPolyData( mitk::Surface* surface, const std::string& varName ); /// /// \see IPythonService::CopyVtkPolyDataFromPython() - mitk::Surface::Pointer CopyVtkPolyDataFromPython( const QString& varName ); + mitk::Surface::Pointer CopyVtkPolyDataFromPython( const std::string& varName ); /// /// \return the ctk abstract python manager instance ctkAbstractPythonManager* GetPythonManager(); protected: - QString GetTempImageName(const QString &ext) const; + QString GetTempImageName(const std::string &ext) const; private: QList m_Observer; ctkAbstractPythonManager m_PythonManager; static const QString m_TmpImageName; bool m_ItkWrappingAvailable; bool m_OpenCVWrappingAvailable; bool m_VtkWrappingAvailable; }; } #endif