Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkPythonTextEditor.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 "QmitkPythonTextEditor.h"
18 
19 #include <QList>
20 #include <QFileInfo>
21 #include <QUrl>
22 #include <QToolBar>
23 #include <QGridLayout>
24 #include <QAction>
25 #include <QIcon>
26 #include <QMimeData>
27 #include <QFileDialog>
28 
29 #include <usModuleContext.h>
30 #include <usServiceReference.h>
31 #include <mitkDataNode.h>
32 #include <usGetModuleContext.h>
33 #include <mitkIPythonService.h>
35 
36 struct QmitkPythonTextEditorData
37 {
38  QString m_FilePath;
39 
40  QAction* m_LoadScript;
41  QAction* m_SaveScript;
42  QAction* m_RunScript;
43  QToolBar* m_Toolbar;
44 
45  QTextEdit* m_Content;
46 
47  QGridLayout* m_Layout;
48  mitk::IPythonService* m_PythonService;
50 
51  QString m_FileName;
52 };
53 
55  :QWidget(parent), d(new QmitkPythonTextEditorData)
56 {
57  us::ModuleContext* context = us::GetModuleContext();
58  d->m_PythonServiceRef = context->GetServiceReference<mitk::IPythonService>();
59  d->m_PythonService = context->GetService<mitk::IPythonService>( d->m_PythonServiceRef );
60 
61  d->m_LoadScript = new QAction(this);
62  d->m_LoadScript->setToolTip("Load script from disk.");
63  d->m_LoadScript->setObjectName(QString::fromUtf8("LoadScript"));
64  QIcon icon2;
65  icon2.addFile(QString::fromUtf8(":/mitkPython/document-open.png"), QSize(), QIcon::Normal, QIcon::Off);
66  d->m_LoadScript->setIcon(icon2);
67 
68  d->m_SaveScript = new QAction(this);
69  d->m_SaveScript->setToolTip("Save script to disk.");
70  d->m_SaveScript->setObjectName(QString::fromUtf8("SaveScript"));
71  QIcon icon3;
72  icon3.addFile(QString::fromUtf8(":/mitkPython/document-save.png"), QSize(), QIcon::Normal, QIcon::Off);
73  d->m_SaveScript->setIcon(icon3);
74 
75  d->m_RunScript = new QAction(this);
76  d->m_RunScript->setToolTip("Run the current script.");
77  d->m_RunScript->setObjectName(QString::fromUtf8("RunScript"));
78  QIcon icon4;
79  icon4.addFile(QString::fromUtf8(":/mitkPython/media-playback-start.png"), QSize(), QIcon::Normal, QIcon::Off);
80  d->m_RunScript->setIcon(icon4);
81 
82  d->m_Toolbar = new QToolBar;
83  d->m_Toolbar->addAction( d->m_LoadScript );
84  d->m_Toolbar->addAction( d->m_SaveScript );
85  d->m_Toolbar->addAction( d->m_RunScript );
86 
87  d->m_Content = new QTextEdit(this);
88  d->m_Content->setObjectName(QString::fromUtf8("Content"));
89 
91  new QmitkPythonScriptEditorHighlighter( d->m_Content->document() );
92 
93  d->m_Layout = new QGridLayout;
94  d->m_Layout->addWidget( d->m_Toolbar, 0, 0, 1, 1 );
95  d->m_Layout->addWidget( d->m_Content, 1, 0, 1, 1 );
96  d->m_Layout->setContentsMargins(2,2,2,2);
97 
98  this->setLayout(d->m_Layout);
99  QMetaObject::connectSlotsByName(this);
100 }
101 
103 {
104  us::ModuleContext* context = us::GetModuleContext();
105  context->UngetService( d->m_PythonServiceRef );
106 
107  delete d;
108 }
109 
110 void QmitkPythonTextEditor::dragEnterEvent(QDragEnterEvent *event)
111 {
112  event->accept();
113 }
114 
115 void QmitkPythonTextEditor::dropEvent(QDropEvent *event)
116 {
117  QList<QUrl> urls = event->mimeData()->urls();
118  for(int i = 0; i < urls.size(); i++)
119  {
120  this->Paste( urls[i].toString() );
121  }
122 }
123 /*
124 bool QmitkPythonTextEditor::canInsertFromMimeData( const QMimeData * ) const
125 {
126  return true;
127 }
128 */
129 
130 void QmitkPythonTextEditor::Paste(const QString &command)
131 {
132  if( this->isVisible() )
133  {
134  d->m_Content->insertPlainText(command + "\n");
135  }
136 }
137 
138 
140 {
141  QFile file(filename);
142  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
143  {
144  MITK_ERROR << "Could not open file " << filename.toStdString();
145  return NULL;
146  }
147 
148  QByteArray total;
149  QByteArray line;
150  while (!file.atEnd())
151  {
152  line = file.read(1024);
153  total.append(line);
154  }
155 
156  return QString(total);
157 }
158 
160 {
161  d->m_FileName = QFileDialog::getSaveFileName(this,tr("Save File"), d->m_FileName,tr("*.py"));
162  if( d->m_FileName.compare("") != 0)
163  {
164  std::ofstream myfile;
165  myfile.open(d->m_FileName.toLocal8Bit().data());
166  myfile << d->m_Content->toPlainText().toLocal8Bit().data();
167  myfile.close();
168  }
169 }
170 
172 {
173  d->m_FileName = QFileDialog::getOpenFileName( this, "Load Script", d->m_FileName, tr("*.py"));
174  if( !d->m_FileName.isEmpty() )
175  {
176  QString contents = this->ReadFile( d->m_FileName );
177  d->m_Content->setText(contents);
178  }
179 }
180 
182 {
183  if( !d->m_PythonService )
184  {
185  MITK_ERROR << "Python service not available.";
186  return;
187  }
188 
189  d->m_PythonService->Execute( d->m_Content->toPlainText().toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND );
190 }
void dropEvent(QDropEvent *event)
void Paste(const QString &command)
static char * line
Definition: svm.cpp:2884
void on_LoadScript_triggered(bool checked=false)
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static const int MULTI_LINE_COMMAND
QmitkPythonTextEditor(QWidget *parent=0)
static const std::string filename
void on_SaveScript_triggered(bool checked=false)
QString ReadFile(const QString &filename)
A script highlighter for Python Scripts.
void dragEnterEvent(QDragEnterEvent *event)
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void on_RunScript_triggered(bool checked=false)