Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 #include <usModuleContext.h>
15 #include <usServiceReference.h>
16 #include <usGetModuleContext.h>
17 #include <mitkRenderingManager.h>
18 #include <mitkSurface.h>
19 #include <vtkPolyData.h>
20 
22  :QTableView(parent)
23 {
24  m_TableModel = new QmitkPythonVariableStackTableModel(parent);
25  m_TableModel->CommandExecuted("");
26 
27  this->setSelectionBehavior( QAbstractItemView::SelectRows );
28  this->setAlternatingRowColors(true);
29  this->setDropIndicatorShown(true);
30  this->setAcceptDrops(true);
31  this->setModel( m_TableModel );
32 
33  us::ModuleContext* context = us::GetModuleContext();
34  us::ServiceReference<mitk::IPythonService> serviceRef = context->GetServiceReference<mitk::IPythonService>();
35  m_PythonService = context->GetService<mitk::IPythonService>(serviceRef);
36 
37  connect( this, SIGNAL(doubleClicked ( const QModelIndex& )), this, SLOT( OnVariableStackDoubleClicked(const QModelIndex&) ) );
38 }
39 
41 {
42 }
43 
45 {
46  m_DataStorage = _DataStorage;
47 }
48 
50 {
51  if( m_DataStorage.IsNull() || m_PythonService == nullptr )
52  {
53  MITK_ERROR << "QmitkPythonVariableStackTableView not configured correctly. Quit";
54  return;
55  }
56 
57  int row = index.row();
58  std::vector<mitk::PythonVariable> variableStack = m_TableModel->GetVariableStack();
59  {
60  MITK_DEBUG("QmitkPythonVariableStackTableView") << "row " << row;
61  MITK_DEBUG("QmitkPythonVariableStackTableView") << "variableStack.size(): " << variableStack.size();
62  }
63 
64  QString varName = QString::fromStdString( variableStack.at(row).m_Name );
65  QString type = QString::fromStdString( variableStack.at(row).m_Type );
66  QString value = QString::fromStdString( variableStack.at(row).m_Value );
67 
68  {
69  MITK_DEBUG("QmitkPythonVariableStackTableView") << "varName: " << varName.toStdString();
70  MITK_DEBUG("QmitkPythonVariableStackTableView") << "type: " << type.toStdString();
71  }
72 
73  mitk::Image::Pointer mitkImage;
74  mitk::Surface::Pointer mitkSurface;
75 
76  if( type.startsWith("Image") )
77  {
78  mitkImage = m_PythonService->CopySimpleItkImageFromPython(varName.toStdString());
79  }
80  else if( type.startsWith("numpy.ndarray") )
81  {
82  mitkImage = m_PythonService->CopyCvImageFromPython(varName.toStdString());
83  }
84  else if( value.startsWith("(vtkCommonDataModelPython.vtkPolyData)") )
85  {
86  mitkSurface = m_PythonService->CopyVtkPolyDataFromPython(varName.toStdString());
87  }
88 
89  std::string nodeName = varName.toStdString();
90  mitk::DataNode::Pointer node = m_DataStorage->GetNamedNode(nodeName);
91 
92  // only create data node if it does not exist
93  if ( node.IsNull() )
94  {
95  node = mitk::DataNode::New();
96  node->SetName ( nodeName );
97  m_DataStorage->Add(node);
98  }
99 
100  if( mitkImage.IsNotNull() )
101  {
102  node->SetData( mitkImage );
103  }
104  else if( mitkSurface.IsNotNull() )
105  {
106  node->SetData( mitkSurface );
107  // init renderwindow geometry
108  mitk::RenderingManager::GetInstance()->InitializeViews(mitkSurface->GetGeometry());
109  }
110 
112 }
virtual bool InitializeViews(const BaseGeometry *geometry, RequestType type=REQUEST_UPDATE_ALL, bool preserveRoughOrientationInWorldSpace=false)
void CommandExecuted(const std::string &pythonCommand) override
virtual mitk::Surface::Pointer CopyVtkPolyDataFromPython(const std::string &varName)=0
Data management class that handles &#39;was created by&#39; relations.
#define MITK_ERROR
Definition: mitkLogMacros.h:20
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
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)
std::vector< mitk::PythonVariable > GetVariableStack() const
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)