Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkCtkPythonShell.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 
17 #include "QmitkCtkPythonShell.h"
18 
19 #include <ctkAbstractPythonManager.h>
20 #include <QDragEnterEvent>
21 #include <QDropEvent>
22 #include <QMimeData>
23 #include <QUrl>
24 #include "mitkIPythonService.h"
25 #include <usModuleContext.h>
26 #include <usServiceReference.h>
27 #include <usGetModuleContext.h>
28 
29 struct QmitkCtkPythonShellData
30 {
31  mitk::IPythonService* m_PythonService;
33 };
34 
36  : ctkPythonConsole(parent), d( new QmitkCtkPythonShellData )
37 {
38  MITK_DEBUG("QmitkCtkPythonShell") << "retrieving IPythonService";
39  us::ModuleContext* context = us::GetModuleContext();
40  d->m_PythonServiceRef = context->GetServiceReference<mitk::IPythonService>();
41  d->m_PythonService = dynamic_cast<mitk::IPythonService*> ( context->GetService<mitk::IPythonService>(d->m_PythonServiceRef) );
42 
43  MITK_DEBUG("QmitkCtkPythonShell") << "checking IPythonService";
44  Q_ASSERT( d->m_PythonService );
45 
46  MITK_DEBUG("QmitkCtkPythonShell") << "initialize m_PythonService";
47  this->initialize( d->m_PythonService->GetPythonManager() );
48 
49  MITK_DEBUG("QmitkCtkPythonShell") << "m_PythonService initialized";
51 }
52 
54 {
55  us::ModuleContext* context = us::GetModuleContext();
56  context->UngetService( d->m_PythonServiceRef );
57  delete d;
58 }
59 
60 void QmitkCtkPythonShell::dragEnterEvent(QDragEnterEvent *event)
61 {
62  event->accept();
63 }
64 void QmitkCtkPythonShell::dropEvent(QDropEvent *event)
65 {
66  QList<QUrl> urls = event->mimeData()->urls();
67  for(int i = 0; i < urls.size(); i++)
68  {
69  d->m_PythonService->Execute( urls[i].toString().toStdString(), mitk::IPythonService::SINGLE_LINE_COMMAND );
70  }
71 }
72 
73 bool QmitkCtkPythonShell::canInsertFromMimeData( const QMimeData *source ) const
74 {
75  return true;
76 }
77 
78 void QmitkCtkPythonShell::executeCommand(const QString& command)
79 {
80  MITK_DEBUG("QmitkCtkPythonShell") << "executing command " << command.toStdString();
81  d->m_PythonService->Execute(command.toStdString(),mitk::IPythonService::MULTI_LINE_COMMAND);
82  d->m_PythonService->NotifyObserver(command.toStdString());
83 }
84 
85 void QmitkCtkPythonShell::Paste(const QString &command)
86 {
87  if( this->isVisible() )
88  {
89  this->exec( command );
90  //this->executeCommand( command );
91  }
92 }
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
static const int MULTI_LINE_COMMAND
static const int SINGLE_LINE_COMMAND
void executeCommand(const QString &command)
bool canInsertFromMimeData(const QMimeData *source) const
void dragEnterEvent(QDragEnterEvent *event)
static std::string ForceLoadModule()
void dropEvent(QDropEvent *event)
void Paste(const QString &command)
QmitkCtkPythonShell(QWidget *parent=0)
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.