23 #include <QGridLayout>
27 #include <QFileDialog>
36 struct QmitkPythonTextEditorData
40 QAction* m_LoadScript;
41 QAction* m_SaveScript;
47 QGridLayout* m_Layout;
55 :QWidget(parent), d(new QmitkPythonTextEditorData)
61 d->m_LoadScript =
new QAction(
this);
62 d->m_LoadScript->setToolTip(
"Load script from disk.");
63 d->m_LoadScript->setObjectName(QString::fromUtf8(
"LoadScript"));
65 icon2.addFile(QString::fromUtf8(
":/mitkPython/document-open.png"), QSize(), QIcon::Normal, QIcon::Off);
66 d->m_LoadScript->setIcon(icon2);
68 d->m_SaveScript =
new QAction(
this);
69 d->m_SaveScript->setToolTip(
"Save script to disk.");
70 d->m_SaveScript->setObjectName(QString::fromUtf8(
"SaveScript"));
72 icon3.addFile(QString::fromUtf8(
":/mitkPython/document-save.png"), QSize(), QIcon::Normal, QIcon::Off);
73 d->m_SaveScript->setIcon(icon3);
75 d->m_RunScript =
new QAction(
this);
76 d->m_RunScript->setToolTip(
"Run the current script.");
77 d->m_RunScript->setObjectName(QString::fromUtf8(
"RunScript"));
79 icon4.addFile(QString::fromUtf8(
":/mitkPython/media-playback-start.png"), QSize(), QIcon::Normal, QIcon::Off);
80 d->m_RunScript->setIcon(icon4);
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 );
87 d->m_Content =
new QTextEdit(
this);
88 d->m_Content->setObjectName(QString::fromUtf8(
"Content"));
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);
98 this->setLayout(d->m_Layout);
99 QMetaObject::connectSlotsByName(
this);
105 context->UngetService( d->m_PythonServiceRef );
117 QList<QUrl> urls =
event->mimeData()->urls();
118 for(
int i = 0; i < urls.size(); i++)
120 this->
Paste( urls[i].toString() );
132 if( this->isVisible() )
134 d->m_Content->insertPlainText(command +
"\n");
141 QFile file(filename);
142 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
144 MITK_ERROR <<
"Could not open file " << filename.toStdString();
150 while (!file.atEnd())
152 line = file.read(1024);
156 return QString(total);
161 d->m_FileName = QFileDialog::getSaveFileName(
this,tr(
"Save File"), d->m_FileName,tr(
"*.py"));
162 if( d->m_FileName.compare(
"") != 0)
164 std::ofstream myfile;
165 myfile.open(d->m_FileName.toLocal8Bit().data());
166 myfile << d->m_Content->toPlainText().toLocal8Bit().data();
173 d->m_FileName = QFileDialog::getOpenFileName(
this,
"Load Script", d->m_FileName, tr(
"*.py"));
174 if( !d->m_FileName.isEmpty() )
176 QString contents = this->
ReadFile( d->m_FileName );
177 d->m_Content->setText(contents);
183 if( !d->m_PythonService )
185 MITK_ERROR <<
"Python service not available.";
void dropEvent(QDropEvent *event)
void Paste(const QString &command)
void on_LoadScript_triggered(bool checked=false)
virtual ~QmitkPythonTextEditor()
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)