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