diff --git a/Plugins/org.mitk.gui.qt.python/src/internal/QmitkPythonVariableStackTableModel.cpp b/Plugins/org.mitk.gui.qt.python/src/internal/QmitkPythonVariableStackTableModel.cpp index 9b3845d5dc..9fc1aa7fa9 100755 --- a/Plugins/org.mitk.gui.qt.python/src/internal/QmitkPythonVariableStackTableModel.cpp +++ b/Plugins/org.mitk.gui.qt.python/src/internal/QmitkPythonVariableStackTableModel.cpp @@ -1,281 +1,339 @@ /*=================================================================== 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 +#include "mitkPluginActivator.h" +#include + QmitkPythonVariableStackTableModel::QmitkPythonVariableStackTableModel(QObject *parent) :QAbstractTableModel(parent) { } QmitkPythonVariableStackTableModel::~QmitkPythonVariableStackTableModel() { } 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_INFO << "dropped MITK DataNode"; returnValue = true; QString arg = QString(data->data("application/x-mitk-datanodes").data()); QStringList listOfDataNodeAddressPointers = arg.split(","); QStringList::iterator slIter; 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()); + // save image + QString tmpFolder = QDir::tempPath(); + QDateTime dateTime = QDateTime::currentDateTime(); + QString fileName = tmpFolder + QDir::separator() + dateTime.toString("yy.MM.dd.hh.ss.zzz") + ".nrrd"; + + MITK_INFO << "Saving temporary file " << fileName.toStdString(); + if( !mitk::IOUtil::SaveImage(mitkImage, fileName.toStdString()) ) + { + MITK_ERROR << "Temporary file could not be created."; + } + + QString command; + command.append("import itk\n"); + command.append("dim = 3\n"); + command.append("pixelType = itk.UC\n"); + command.append("imageType = itk.Image[pixelType, dim]\n"); + command.append("readerType = itk.ImageFileReader[imageType]\n"); + command.append("reader = readerType.New()\n"); + command.append(QString("reader.SetFileName( \"%1\" )\n").arg(fileName)); + command.append("reader.Update()\n"); + command.append("itkImage = reader.GetOutput()\n"); + MITK_INFO << "Issuing python command " << command.toStdString(); + mitk::PluginActivator::GetPythonManager()->executeString(command, ctkAbstractPythonManager::FileInput ); + this->Update(); + + QFile file(fileName); + MITK_INFO << "Removing file " << fileName.toStdString(); + file.remove(); + + /* + if( mitkImage ) + { + MITK_INFO << "found MITK image"; + switch(mitkImage->GetDimension()) + { + case 2: + { + AccessFixedDimensionByItk( mitkImage, ItkImageProcessing, 2 ); break; + } + case 3: + { + AccessFixedDimensionByItk( mitkImage, ItkImageProcessing, 3 ); break; + } + case 4: + { + AccessFixedDimensionByItk( mitkImage, ItkImageProcessing, 4 ); break; + } + default: break; + } + } itk::SmartPointer * resultptr; resultptr = new itk::SmartPointer((itk::SmartPointer &)node); if(resultptr) { int i = 0; while(swig_types_initial[i] != 0) { if(swig_types_initial[i] == _swigt__p_itk__SmartPointerTmitk__DataNode_t) SWIGTYPE_p_itk__SmartPointerTmitk__DataNode_t = SWIG_TypeRegister(swig_types_initial[i]); i++; } PyObject * resultObj = SWIG_NewPointerObj((void*)(resultptr), SWIGTYPE_p_itk__SmartPointerTmitk__DataNode_t, 1); PyObject* dict = PyImport_GetModuleDict(); PyObject* object = PyDict_GetItemString(dict, "__main__"); if(object){ Py_INCREF(object); PyModule_AddObject(object, node->GetName().c_str(), resultObj); } setVariableStack(this->getAttributeList()); } + */ } - } 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) { QStringList item = m_VariableStack.at(index.row()); if(index.column() == 0) return item[0]; if(index.column() == 1) return item[1]; if(index.column() == 2) return item[2]; } } return QVariant(); } void QmitkPythonVariableStackTableModel::clear() { m_VariableStack.clear(); QAbstractTableModel::reset(); } void QmitkPythonVariableStackTableModel::setVariableStack(QList varStack) { m_VariableStack = varStack; QAbstractTableModel::reset(); } QMimeData * QmitkPythonVariableStackTableModel::mimeData(const QModelIndexList & indexes) const { return QAbstractTableModel::mimeData(indexes); /* QMimeData * ret = new QMimeData; QString dataNodeAddresses(""); for (int indexesCounter = 0; indexesCounter < indexes.size(); indexesCounter++) { QString name = m_VariableStack[indexes.at(indexesCounter).row()][0]; QString type = m_VariableStack[indexes.at(indexesCounter).row()][2]; if(type != "DataNode_PointerPtr") return NULL; mitk::DataNode* node; itk::SmartPointer *arg; PyObject * obj = this->getPyObjectString(&name); int i = 0; while(swig_types_initial[i] != 0) { if(swig_types_initial[i] == _swigt__p_itk__SmartPointerTmitk__DataNode_t) SWIGTYPE_p_itk__SmartPointerTmitk__DataNode_t = SWIG_TypeRegister(swig_types_initial[i]); i++; } if ((SWIG_ConvertPtr(obj,(void **)(&arg),SWIGTYPE_p_itk__SmartPointerTmitk__DataNode_t, SWIG_POINTER_EXCEPTION | 0)) == -1) return NULL; if(arg == NULL){ return NULL; } try { node = (mitk::DataNode *)((itk::SmartPointer const *)arg)->GetPointer(); } catch(std::exception &_e) { { std::cout << "Not a DataNode" << std::endl; } } long a = reinterpret_cast(node); if (dataNodeAddresses.size() != 0) { QTextStream(&dataNodeAddresses) << ","; } QTextStream(&dataNodeAddresses) << a; ret->setData("application/x-mitk-datanodes", QByteArray(dataNodeAddresses.toAscii())); } return ret; */ } 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; } Qt::DropActions QmitkPythonVariableStackTableModel::supportedDragActions() const { return Qt::CopyAction | Qt::MoveAction; } void QmitkPythonVariableStackTableModel::Update() { // std::cout << "QmitkPythonVariableStackTableModel::update()" << std::endl; this->setVariableStack(this->getAttributeList()); } QList QmitkPythonVariableStackTableModel::getAttributeList() { PyObject* dict = PyImport_GetModuleDict(); PyObject* object = PyDict_GetItemString(dict, "__main__"); PyObject* dirMain = PyObject_Dir(object); PyObject* tempObject; QList variableStack; if(dirMain) { QString attr, attrValue, attrType; variableStack.clear(); for(int i = 0; iob_type->tp_name; if(PyUnicode_Check(tempObject) || PyString_Check(tempObject)) attrValue = PyString_AsString(tempObject); else attrValue = ""; variableStack.push_back(QStringList() << attr << attrValue << attrType); } } return variableStack; } PyObject * QmitkPythonVariableStackTableModel::getPyObject(PyObject * object) { PyObject* dict = PyImport_GetModuleDict(); PyObject* main = PyDict_GetItemString(dict, "__main__"); return PyObject_GetAttr(main, object); } PyObject * QmitkPythonVariableStackTableModel::getPyObjectString(const QString &objectName) { PyObject* dict = PyImport_GetModuleDict(); PyObject* main = PyDict_GetItemString(dict, "__main__"); return PyObject_GetAttrString(main, objectName.toLocal8Bit().data()); } diff --git a/Plugins/org.mitk.gui.qt.python/src/internal/QmitkPythonVariableStackTableModel.h b/Plugins/org.mitk.gui.qt.python/src/internal/QmitkPythonVariableStackTableModel.h index 6a35ef358f..276b7b0cdc 100755 --- a/Plugins/org.mitk.gui.qt.python/src/internal/QmitkPythonVariableStackTableModel.h +++ b/Plugins/org.mitk.gui.qt.python/src/internal/QmitkPythonVariableStackTableModel.h @@ -1,66 +1,68 @@ /*=================================================================== 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 #include #include +#include /// /// 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 QmitkPythonVariableStackTableModel : public QAbstractTableModel { Q_OBJECT public: 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; void clear(); void setVariableStack(QList); QList getVariableStack(); QMimeData * mimeData(const QModelIndexList &) const; bool dropMimeData ( const QMimeData *, Qt::DropAction, int, int, const QModelIndex & ); Qt::DropActions supportedDropActions() const; Qt::DropActions supportedDragActions() const; public slots: void Update(); protected: QList getAttributeList(); PyObject* getPyObject(PyObject* object); PyObject* getPyObjectString(const QString& objectName); + private: QList m_VariableStack; }; #endif // QmitkPythonVariableStackTableModel_h diff --git a/Plugins/org.mitk.gui.qt.python/src/internal/QmitkPythonView.cpp b/Plugins/org.mitk.gui.qt.python/src/internal/QmitkPythonView.cpp index 5263f0dd8e..54f34c048f 100644 --- a/Plugins/org.mitk.gui.qt.python/src/internal/QmitkPythonView.cpp +++ b/Plugins/org.mitk.gui.qt.python/src/internal/QmitkPythonView.cpp @@ -1,68 +1,77 @@ /*=================================================================== 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 "QmitkPythonView.h" #include #include "QmitkCtkPythonShell.h" #include "mitkPluginActivator.h" #include "QmitkPythonVariableStackTableModel.h" const std::string QmitkPythonView::VIEW_ID = "org.mitk.views.python"; struct QmitkPythonViewData { // widget QmitkPythonVariableStackTableModel* m_VariableStackTableModel; QmitkCtkPythonShell* m_PythonShell; }; QmitkPythonView::QmitkPythonView() : d( new QmitkPythonViewData ) { d->m_PythonShell = 0; } QmitkPythonView::~QmitkPythonView() { delete d; } void QmitkPythonView::CreateQtPartControl(QWidget* parent) { d->m_VariableStackTableModel = new QmitkPythonVariableStackTableModel(parent); d->m_VariableStackTableModel->Update(); - QSplitter* splitter = new QSplitter; QTableView* variableStackView = new QTableView; + variableStackView->setSelectionBehavior( QAbstractItemView::SelectRows ); + variableStackView->setAlternatingRowColors(true); + variableStackView->setDragEnabled(true); + variableStackView->setDropIndicatorShown(true); + variableStackView->setAcceptDrops(true); variableStackView->setModel( d->m_VariableStackTableModel ); d->m_PythonShell = new QmitkCtkPythonShell; d->m_PythonShell->SetPythonManager( mitk::PluginActivator::GetPythonManager() ); + QList sizes; + sizes << 1 << 3; + QSplitter* splitter = new QSplitter; splitter->addWidget(variableStackView); splitter->addWidget(d->m_PythonShell); + splitter->setStretchFactor ( 0, 1 ); + splitter->setStretchFactor ( 1, 3 ); QGridLayout* layout = new QGridLayout; layout->addWidget( splitter, 0, 0 ); parent->setLayout(layout); connect( d->m_PythonShell, SIGNAL(newCommandExecuted()), d->m_VariableStackTableModel, SLOT(Update()) ); } void QmitkPythonView::SetFocus() { d->m_PythonShell->setFocus(); } diff --git a/Plugins/org.mitk.gui.qt.python/src/internal/mitkPluginActivator.cpp b/Plugins/org.mitk.gui.qt.python/src/internal/mitkPluginActivator.cpp index 5a897f6c56..7d5bca5dd3 100644 --- a/Plugins/org.mitk.gui.qt.python/src/internal/mitkPluginActivator.cpp +++ b/Plugins/org.mitk.gui.qt.python/src/internal/mitkPluginActivator.cpp @@ -1,67 +1,67 @@ /*=================================================================== 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 "mitkPluginActivator.h" #include #include "QmitkPythonView.h" #include "PythonPath.h" #include namespace mitk { void PluginActivator::start(ctkPluginContext* context) { - MITK_INFO << "registering python paths"; + MITK_DEBUG << "registering python paths"; QString itkLibDirs(PYTHONPATH_ITK_LIBRARY_DIRS); - MITK_INFO << "itkLibDirs" << itkLibDirs.toStdString(); + MITK_DEBUG << "itkLibDirs" << itkLibDirs.toStdString(); QString itkWrapItkDir(PYTHONPATH_WRAP_ITK_DIR); - MITK_INFO << "itkWrapItkDir" << itkWrapItkDir.toStdString(); + MITK_DEBUG << "itkWrapItkDir" << itkWrapItkDir.toStdString(); QString basecommand = "sys.path.append('%1');"; QString pythonCommand; if( ! itkWrapItkDir.isEmpty() ) { pythonCommand = basecommand.arg(itkLibDirs); - MITK_INFO << "issuing command " << pythonCommand.toStdString(); + MITK_DEBUG << "issuing command " << pythonCommand.toStdString(); GetPythonManager()->executeString(pythonCommand, ctkAbstractPythonManager::SingleInput ); pythonCommand = basecommand.arg(itkWrapItkDir); - MITK_INFO << "issuing command " << pythonCommand.toStdString(); + MITK_DEBUG << "issuing command " << pythonCommand.toStdString(); GetPythonManager()->executeString(pythonCommand, ctkAbstractPythonManager::SingleInput ); } else { MITK_WARN << "No wrapping directory set for WrapITK. ITK Python Wrapping not available!"; } BERRY_REGISTER_EXTENSION_CLASS(QmitkPythonView, context) } void PluginActivator::stop(ctkPluginContext* context) { Q_UNUSED(context) } ctkAbstractPythonManager *PluginActivator::GetPythonManager() { static ctkAbstractPythonManager PythonManager; return &PythonManager; } } Q_EXPORT_PLUGIN2(org_mitk_gui_qt_python, mitk::PluginActivator)