25 #include <QGridLayout>
28 #include <QInputDialog>
30 #include <QMessageBox>
31 #include <QXmlStreamWriter>
32 #include <QXmlStreamReader>
34 #include <QFileDialog>
36 struct QmitkPythonSnippetsData
38 QString m_AutoSaveFileName;
39 QString m_SaveFileName;
41 QAction* m_PasteSnippet;
42 QAction* m_RemoveSnippet;
43 QAction* m_RenameSnippet;
44 QAction* m_AddSnippet;
45 QAction* m_RestoreDefaultSnippets;
46 QAction* m_LoadSnippets;
47 QAction* m_SaveSnippets;
55 QGridLayout* m_Layout;
65 : QWidget(parent), d(new QmitkPythonSnippetsData)
67 d->m_SaveFileName = QDir::currentPath();
68 d->m_AutoSaveFileName = _AutoSaveFileName;
75 d->m_PasteSnippet =
new QAction(
this);
76 d->m_PasteSnippet->setObjectName(QString::fromUtf8(
"PasteSnippet"));
78 icon.addFile(QString::fromUtf8(
":/mitkPython/edit-paste.png"), QSize(), QIcon::Normal, QIcon::Off);
79 d->m_PasteSnippet->setIcon(icon);
80 d->m_PasteSnippet->setToolTip(
"Paste snippet!");
81 d->m_PasteSnippet->setEnabled(
false);
83 d->m_RemoveSnippet =
new QAction(
this);
84 d->m_RemoveSnippet->setObjectName(QString::fromUtf8(
"RemoveSnippet"));
86 icon1.addFile(QString::fromUtf8(
":/mitkPython/edit-delete.png"), QSize(), QIcon::Normal, QIcon::Off);
87 d->m_RemoveSnippet->setIcon(icon1);
88 d->m_RemoveSnippet->setToolTip(
"Remove snippet.");
89 d->m_RemoveSnippet->setEnabled(
false);
91 d->m_RenameSnippet =
new QAction(
this);
92 d->m_RenameSnippet->setObjectName(QString::fromUtf8(
"RenameSnippet"));
94 icon2.addFile(QString::fromUtf8(
":/mitkPython/edit-find-replace.png"), QSize(), QIcon::Normal, QIcon::Off);
95 d->m_RenameSnippet->setIcon(icon2);
96 d->m_RenameSnippet->setToolTip(
"Rename snippet.");
97 d->m_RenameSnippet->setEnabled(
false);
99 d->m_AddSnippet =
new QAction(
this);
100 d->m_AddSnippet->setObjectName(QString::fromUtf8(
"AddSnippet"));
102 icon3.addFile(QString::fromUtf8(
":/mitkPython/document-new.png"), QSize(), QIcon::Normal, QIcon::Off);
103 d->m_AddSnippet->setIcon(icon3);
104 d->m_AddSnippet->setToolTip(
"Add snippet.");
106 d->m_RestoreDefaultSnippets =
new QAction(
this);
107 d->m_RestoreDefaultSnippets->setObjectName(QString::fromUtf8(
"RestoreDefaultSnippets"));
109 icon4.addFile(QString::fromUtf8(
":/mitkPython/edit-clear.png"), QSize(), QIcon::Normal, QIcon::Off);
110 d->m_RestoreDefaultSnippets->setIcon(icon4);
111 d->m_RestoreDefaultSnippets->setToolTip(
"Restore default snippets");
113 d->m_LoadSnippets =
new QAction(
this);
114 d->m_LoadSnippets->setToolTip(
"Load Snippets from disk.");
115 d->m_LoadSnippets->setObjectName(QString::fromUtf8(
"LoadSnippets"));
117 icon5.addFile(QString::fromUtf8(
":/mitkPython/document-open.png"), QSize(), QIcon::Normal, QIcon::Off);
118 d->m_LoadSnippets->setIcon(icon5);
120 d->m_SaveSnippets =
new QAction(
this);
121 d->m_SaveSnippets->setToolTip(
"Save Snippets to disk.");
122 d->m_SaveSnippets->setObjectName(QString::fromUtf8(
"SaveSnippets"));
124 icon6.addFile(QString::fromUtf8(
":/mitkPython/document-save.png"), QSize(), QIcon::Normal, QIcon::Off);
125 d->m_SaveSnippets->setIcon(icon6);
126 d->m_SaveSnippets->setEnabled(
false);
128 d->m_Toolbar =
new QToolBar;
129 d->m_Toolbar->addAction( d->m_PasteSnippet );
130 d->m_Toolbar->addAction( d->m_AddSnippet );
131 d->m_Toolbar->addAction( d->m_RemoveSnippet );
132 d->m_Toolbar->addAction( d->m_RenameSnippet );
133 d->m_Toolbar->addAction( d->m_RestoreDefaultSnippets );
134 d->m_Toolbar->addAction( d->m_SaveSnippets );
135 d->m_Toolbar->addAction( d->m_LoadSnippets );
137 d->m_Name =
new QComboBox;
138 d->m_Name->setObjectName(QString::fromUtf8(
"Name"));
140 d->m_Content =
new QTextEdit(
this);
141 d->m_Content->setObjectName(QString::fromUtf8(
"Content"));
142 d->m_Content->setEnabled(
false);
147 d->m_Layout =
new QGridLayout;
148 d->m_Layout->addWidget( d->m_Toolbar, 0, 0, 1, 1 );
149 d->m_Layout->addWidget( d->m_Name, 1, 0, 1, 1 );
150 d->m_Layout->addWidget( d->m_Content, 2, 0, 1, 1 );
151 d->m_Layout->setContentsMargins(2,2,2,2);
153 this->setLayout(d->m_Layout);
154 QMetaObject::connectSlotsByName(
this);
171 QString oldname = d->m_Name->currentText();
172 QString name = oldname;
176 name = QInputDialog::getText(
this,
177 tr(
"Add new snippet"),
178 tr(
"Name of snippet:"),
185 if ( d->m_Snippets.contains(name) )
187 QMessageBox::warning(
this,
188 tr(
"Duplicate name."),
189 tr(
"The entered name already exists. Enter another one or cancel the operation."),
195 QString tmpSnippet = d->m_Snippets[oldname];
196 d->m_Snippets.remove(oldname);
197 d->m_Snippets[name] = tmpSnippet;
213 QString name = QInputDialog::getText(
this,
214 tr(
"Add new snippet"),
215 tr(
"Name of snippet:"),
219 if (ok && !name.isEmpty())
221 MITK_DEBUG(
"QmitkPythonSnippets") <<
"creating unique name for " << name.toStdString();
224 MITK_DEBUG(
"QmitkPythonSnippets") <<
"creating snippet " << name.toStdString();
225 d->m_Snippets[name] =
"";
233 QString newName = name;
235 while( d->m_Snippets.contains(name) )
237 newName = name + QString(
"_%1").arg(i);
246 QString name = d->m_Name->currentText();
247 QString question = QString(
"Really remove Snippet %1?").arg(name);
248 int remove = QMessageBox::question(
this,
249 QString(
"Confirm removal"),
251 QMessageBox::Yes | QMessageBox::No,
254 if(
remove == QMessageBox::Yes ||
remove == QMessageBox::Ok )
256 d->m_Snippets.remove(name);
264 QString question = QString(
"Really restore default Snippets?");
265 int remove = QMessageBox::question(
this,
266 QString(
"Confirm restoring"),
268 QMessageBox::Yes | QMessageBox::No,
270 if(
remove == QMessageBox::Yes ||
remove == QMessageBox::Ok )
280 bool validSelection = i >= 0 ;
282 d->m_PasteSnippet->setEnabled(validSelection);
283 d->m_RemoveSnippet->setEnabled(validSelection);
284 d->m_RenameSnippet->setEnabled(validSelection);
285 d->m_Content->setEnabled(validSelection);
286 d->m_SaveSnippets->setEnabled(validSelection);
290 QString name = d->m_Name->currentText();
291 MITK_DEBUG(
"QmitkPythonSnippets") <<
"selected snippet " << name.toStdString();
292 d->m_Content->setText( d->m_Snippets[name] );
293 MITK_DEBUG(
"QmitkPythonSnippets") <<
"selected snippet content " << d->m_Snippets[name].toStdString();
299 MITK_DEBUG(
"QmitkPythonSnippets") <<
"saving to xml file " << filename.toStdString();
301 if( filename.isEmpty() )
303 MITK_WARN(
"QmitkPythonSnippets") <<
"empty auto save file path given. quit.";
307 QFile file(filename);
308 file.open(QIODevice::WriteOnly);
311 MITK_WARN(
"QmitkPythonSnippets") <<
"could not open file " << filename.toStdString() <<
" for writing";
314 QXmlStreamWriter xmlWriter(&file);
316 xmlWriter.setAutoFormatting(
true);
317 xmlWriter.writeStartDocument();
320 QStringMap::const_iterator it = d->m_Snippets.begin();
321 while( it != d->m_Snippets.end() )
326 MITK_DEBUG(
"QmitkPythonSnippets") <<
"writing item " << it.key().toStdString();
331 xmlWriter.writeAttribute(
"key", it.key() );
332 xmlWriter.writeAttribute(
"value", it.value() );
334 xmlWriter.writeEndElement();
339 xmlWriter.writeEndDocument();
344 MITK_DEBUG(
"QmitkPythonSnippets") <<
"SaveStringMap successful ";
351 MITK_DEBUG(
"QmitkPythonSnippets") <<
"loading from xml file " << filename.toStdString();
354 QXmlStreamReader xmlReader;
358 if( filename.startsWith(
":") )
360 QResource res( filename );
361 data = QByteArray( reinterpret_cast< const char* >( res.data() ), res.size() );
362 xmlReader.addData( data );
366 file.setFileName( filename );
367 if (!file.open(QFile::ReadOnly | QFile::Text))
369 MITK_ERROR <<
"Error: Cannot read file " << qPrintable(filename)
370 <<
": " << qPrintable(file.errorString());
373 xmlReader.setDevice(&file);
376 xmlReader.readNext();
378 while(!xmlReader.atEnd())
380 xmlReader.readNext();
384 QXmlStreamAttributes attributes = xmlReader.attributes();
387 if(attributes.hasAttribute(
"key"))
389 key = attributes.value(
"key").toString();
392 if(attributes.hasAttribute(
"value"))
394 value = attributes.value(
"value").toString();
399 MITK_DEBUG(
"QmitkPythonSnippets") <<
"loaded snippet " << key.toStdString();
400 MITK_DEBUG(
"QmitkPythonSnippets") <<
"value " << value.toStdString();
406 if (xmlReader.hasError())
409 << qPrintable(filename) <<
": "
410 << qPrintable(xmlReader.errorString());
413 else if (file.error() != QFile::NoError)
415 MITK_ERROR <<
"Error: Cannot read file " << qPrintable(filename)
416 <<
": " << qPrintable(file.errorString());
430 d->m_Content->clear();
432 MITK_DEBUG(
"QmitkPythonSnippets") <<
"size of snippets " << d->m_Snippets.size();
433 QStringMap::const_iterator it = d->m_Snippets.begin();
435 while( it != d->m_Snippets.end() )
437 MITK_DEBUG(
"QmitkPythonSnippets") <<
"adding item " << it.key().toStdString();
438 d->m_Name->addItem( it.key() );
442 int index = d->m_Name->findText( name );
445 MITK_DEBUG(
"QmitkPythonSnippets") <<
"selecting index " << index;
446 d->m_Name->setCurrentIndex(index);
453 if( d->m_Content->isEnabled() )
455 QString name = d->m_Name->currentText();
456 QString snippet = d->m_Content->toPlainText();
457 d->m_Snippets[name] = snippet;
460 MITK_DEBUG(
"QmitkPythonSnippets") <<
"SaveStringMap successful";
466 QString fileName = QFileDialog::getSaveFileName(
this,
"Save snippets", d->m_SaveFileName,
"XML files (*.xml)");
467 if( !fileName.isEmpty() )
469 d->m_SaveFileName = fileName;
476 QString fileName = QFileDialog::getOpenFileName(
this,
"Load snippets", d->m_SaveFileName,
"XML files (*.xml)");
478 if( !fileName.isEmpty() )
480 d->m_SaveFileName = fileName;
481 QString question = QString(
"Your current snippets will be overwritten. Proceed?");
482 int overwrite = QMessageBox::warning(
this,
483 QString(
"Confirm overwrite"),
485 QMessageBox::Yes | QMessageBox::No,
488 if( overwrite == QMessageBox::Yes )
491 this->
Update( d->m_Name->currentText() );
static const QString DEFAULT_SNIPPET_FILE
QMap< QString, QString > QStringMap
typedef for string map
void SaveStringMap(const QString &filename, const QStringMap &map) const
write string map to xml file
void on_LoadSnippets_triggered(bool checked=false)
ask for file, load snippets (do not replace)
void PasteCommandRequested(const QString &command)
this class whishes to paste sth command
void Update(const QString &name="")
void on_AddSnippet_triggered(bool checked=false)
ask for name, create snippet, call update()
static const QString SNIPPETS_ROOT_XML_ELEMENT_NAME
void on_Content_textChanged()
save changed snippet
void on_SaveSnippets_triggered(bool checked=false)
ask for file, save snippets
static const std::string filename
void on_PasteSnippet_triggered(bool checked=false)
emits PasteCommandRequested signal
void on_RenameSnippet_triggered(bool checked=false)
ask for name as long as it exists, call update()
void on_RestoreDefaultSnippets_triggered(bool checked=false)
call LoadStringMap with d->m_DefaultSnippetsAutoSaveFileName
QmitkPythonSnippets(const QString &_AutoSaveFileName="", QWidget *parent=0)
static bool LoadStringMap(const QString &filename, QStringMap &oldMap)
read string map from xml file
void on_Name_currentIndexChanged(int i)
update action state (enable/disable), update text box
A script highlighter for Python Scripts.
static const QString SNIPPETS_XML_ELEMENT_NAME
virtual ~QmitkPythonSnippets()
delete d pointer
MITKMATCHPOINTREGISTRATION_EXPORT ResultImageType::Pointer map(const InputImageType *input, const RegistrationType *registration, bool throwOnOutOfInputAreaError=false, const double &paddingValue=0, const ResultImageGeometryType *resultGeometry=NULL, bool throwOnMappingError=true, const double &errorValue=0, mitk::ImageMappingInterpolator::Type interpolatorType=mitk::ImageMappingInterpolator::Linear)
void on_RemoveSnippet_triggered(bool checked=false)
remove the current snippet, call update()
QString CreateUniqueName(const QString &name) const
creates a name which does not exist in the list