Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
QmitkPythonVariableStackTableView.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 #include <usModuleContext.h>
19 #include <usServiceReference.h>
20 #include <usGetModuleContext.h>
21 #include <mitkRenderingManager.h>
22 #include <mitkSurface.h>
23 #include <vtkPolyData.h>
24 
26  :QTableView(parent)
27 {
28  m_TableModel = new QmitkPythonVariableStackTableModel(parent);
29  m_TableModel->CommandExecuted("");
30 
31  this->setSelectionBehavior( QAbstractItemView::SelectRows );
32  this->setAlternatingRowColors(true);
33  this->setDropIndicatorShown(true);
34  this->setAcceptDrops(true);
35  this->setModel( m_TableModel );
36 
37  us::ModuleContext* context = us::GetModuleContext();
38  us::ServiceReference<mitk::IPythonService> serviceRef = context->GetServiceReference<mitk::IPythonService>();
39  m_PythonService = context->GetService<mitk::IPythonService>(serviceRef);
40 
41  connect( this, SIGNAL(doubleClicked ( const QModelIndex& )), this, SLOT( OnVariableStackDoubleClicked(const QModelIndex&) ) );
42 }
43 
45 {
46 }
47 
49 {
50  m_DataStorage = _DataStorage;
51 }
52 
54 {
55  if( m_DataStorage.IsNull() || m_PythonService == 0 )
56  {
57  MITK_ERROR << "QmitkPythonVariableStackTableView not configured correctly. Quit";
58  return;
59  }
60 
61  int row = index.row();
62  std::vector<mitk::PythonVariable> variableStack = m_TableModel->GetVariableStack();
63  {
64  MITK_DEBUG("QmitkPythonVariableStackTableView") << "row " << row;
65  MITK_DEBUG("QmitkPythonVariableStackTableView") << "variableStack.size(): " << variableStack.size();
66  }
67 
68  QString varName = QString::fromStdString( variableStack.at(row).m_Name );
69  QString type = QString::fromStdString( variableStack.at(row).m_Type );
70  QString value = QString::fromStdString( variableStack.at(row).m_Value );
71 
72  {
73  MITK_DEBUG("QmitkPythonVariableStackTableView") << "varName: " << varName.toStdString();
74  MITK_DEBUG("QmitkPythonVariableStackTableView") << "type: " << type.toStdString();
75  }
76 
77  mitk::Image::Pointer mitkImage;
78  mitk::Surface::Pointer mitkSurface;
79 
80  if( type.startsWith("Image") )
81  {
82  mitkImage = m_PythonService->CopySimpleItkImageFromPython(varName.toStdString());
83  }
84  else if( type.startsWith("numpy.ndarray") )
85  {
86  mitkImage = m_PythonService->CopyCvImageFromPython(varName.toStdString());
87  }
88  else if( value.startsWith("(vtkCommonDataModelPython.vtkPolyData)") )
89  {
90  mitkSurface = m_PythonService->CopyVtkPolyDataFromPython(varName.toStdString());
91  }
92 
93  std::string nodeName = varName.toStdString();
94  mitk::DataNode::Pointer node = m_DataStorage->GetNamedNode(nodeName);
95 
96  // only create data node if it does not exist
97  if ( node.IsNull() )
98  {
99  node = mitk::DataNode::New();
100  node->SetName ( nodeName );
101  m_DataStorage->Add(node);
102  }
103 
104  if( mitkImage.IsNotNull() )
105  {
106  node->SetData( mitkImage );
107  }
108  else if( mitkSurface.IsNotNull() )
109  {
110  node->SetData( mitkSurface );
111  // init renderwindow geometry
112  mitk::RenderingManager::GetInstance()->InitializeViews(mitkSurface->GetGeometry());
113  }
114 
116 }
virtual bool InitializeViews(const BaseGeometry *geometry, RequestType type=REQUEST_UPDATE_ALL, bool preserveRoughOrientationInWorldSpace=false)
virtual mitk::Surface::Pointer CopyVtkPolyDataFromPython(const std::string &varName)=0
Data management class that handles 'was created by' relations.
#define MITK_ERROR
Definition: mitkLogMacros.h:24
std::vector< mitk::PythonVariable > GetVariableStack() const
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
void CommandExecuted(const std::string &pythonCommand)
void SetDataStorage(mitk::DataStorage *_DataStorage)
virtual mitk::Image::Pointer CopySimpleItkImageFromPython(const std::string &varName)=0
static Pointer New()
static RenderingManager * GetInstance()
virtual mitk::Image::Pointer CopyCvImageFromPython(const std::string &varName)=0
void OnVariableStackDoubleClicked(const QModelIndex &index)
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)