forked from Kitware/ParaView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpqPythonScriptEditor.cxx
389 lines (341 loc) · 12.4 KB
/
pqPythonScriptEditor.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*=========================================================================
Program: ParaView
Module: pqPythonScriptEditor.cxx
Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
All rights reserved.
ParaView is a free software; you can redistribute it and/or modify it
under the terms of the ParaView license version 1.2.
See License_v1.2.txt for the full ParaView license.
A copy of this license can be obtained by contacting
Kitware Inc.
28 Corporate Drive
Clifton Park, NY 12065
USA
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================*/
#include "pqPythonScriptEditor.h"
#include "pqApplicationCore.h"
#include "pqCoreUtilities.h"
#include "pqFileDialog.h"
#include "pqPythonManager.h"
#include "pqPythonSyntaxHighlighter.h"
#include "pqSettings.h"
#include <QAction>
#include <QApplication>
#include <QCloseEvent>
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QScrollBar>
#include <QStatusBar>
#include <QTextEdit>
#include <QTextStream>
#include <vtkPythonInterpreter.h>
//-----------------------------------------------------------------------------
pqPythonScriptEditor::pqPythonScriptEditor(QWidget* p)
: Superclass(p)
{
this->pythonManager = NULL;
this->TextEdit = new QTextEdit;
this->TextEdit->setTabStopWidth(4);
this->setCentralWidget(this->TextEdit);
this->createActions();
this->createMenus();
this->createStatusBar();
this->DefaultSaveDirectory = QDir::homePath();
this->setCurrentFile("");
this->connect(
this->TextEdit->document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
this->resize(300, 450);
pqApplicationCore::instance()->settings()->restoreState("PythonScriptEditor", *this);
vtkPythonInterpreter::Initialize();
this->SyntaxHighlighter = new pqPythonSyntaxHighlighter(this->TextEdit, this);
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::closeEvent(QCloseEvent* e)
{
if (this->maybeSave())
{
this->TextEdit->clear();
this->TextEdit->document()->setModified(false);
this->setWindowModified(false);
e->accept();
pqApplicationCore::instance()->settings()->saveState(*this, "PythonScriptEditor");
}
else
{
e->ignore();
}
}
//-----------------------------------------------------------------------------
bool pqPythonScriptEditor::newFile()
{
if (this->maybeSave())
{
this->TextEdit->clear();
this->setCurrentFile("");
return true;
}
return false;
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::open()
{
if (this->maybeSave())
{
QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty())
{
this->loadFile(fileName);
}
}
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::open(const QString& fileName)
{
if (this->maybeSave())
{
if (!fileName.isEmpty())
{
this->loadFile(fileName);
}
}
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::setSaveDialogDefaultDirectory(const QString& dir)
{
this->DefaultSaveDirectory = dir;
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::setPythonManager(pqPythonManager* manager)
{
this->pythonManager = manager;
this->saveAsMacroAct->setEnabled(manager);
}
//-----------------------------------------------------------------------------
bool pqPythonScriptEditor::save()
{
if (this->CurrentFile.isEmpty())
{
return this->saveAs();
}
else
{
return this->saveFile(this->CurrentFile);
}
}
//-----------------------------------------------------------------------------
bool pqPythonScriptEditor::saveAsMacro()
{
QString userMacroDir = pqCoreUtilities::getParaViewUserDirectory() + "/Macros";
QDir existCheck(userMacroDir);
if (!existCheck.exists() && !existCheck.mkpath(userMacroDir))
{
qWarning() << "Could not create user Macro directory:" << userMacroDir;
return false;
}
QString fileName = pqFileDialog::getSaveFileName(
NULL, this, tr("Save Macro"), userMacroDir, tr("Python script (*.py)"));
if (!fileName.isEmpty() && this->saveFile(fileName))
{
if (pythonManager)
{
pythonManager->updateMacroList();
}
return true;
}
return false;
}
//-----------------------------------------------------------------------------
bool pqPythonScriptEditor::saveAs()
{
QString fileName = pqFileDialog::getSaveFileName(
NULL, this, tr("Save File"), this->DefaultSaveDirectory, tr("Python script (*.py)"));
if (fileName.isEmpty())
{
return false;
}
if (!fileName.endsWith(".py"))
{
fileName.append(".py");
}
return this->saveFile(fileName);
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::documentWasModified()
{
this->setWindowModified(this->TextEdit->document()->isModified());
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::createActions()
{
this->newAct = new QAction(tr("&New"), this);
this->newAct->setShortcut(tr("Ctrl+N"));
this->newAct->setStatusTip(tr("Create a new file"));
this->connect(this->newAct, SIGNAL(triggered()), this, SLOT(newFile()));
this->openAct = new QAction(tr("&Open..."), this);
this->openAct->setShortcut(tr("Ctrl+O"));
this->openAct->setStatusTip(tr("Open an existing file"));
this->connect(this->openAct, SIGNAL(triggered()), this, SLOT(open()));
this->saveAct = new QAction(tr("&Save"), this);
this->saveAct->setShortcut(tr("Ctrl+S"));
this->saveAct->setStatusTip(tr("Save the document to disk"));
this->connect(this->saveAct, SIGNAL(triggered()), this, SLOT(save()));
this->saveAsAct = new QAction(tr("Save &As..."), this);
this->saveAsAct->setStatusTip(tr("Save the document under a new name"));
this->connect(this->saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
this->saveAsMacroAct = new QAction(tr("Save As &Macro..."), this);
this->saveAsMacroAct->setStatusTip(tr("Save the document as a Macro"));
this->connect(this->saveAsMacroAct, SIGNAL(triggered()), this, SLOT(saveAsMacro()));
this->exitAct = new QAction(tr("C&lose"), this);
this->exitAct->setShortcut(tr("Ctrl+W"));
this->exitAct->setStatusTip(tr("Close the script editor"));
this->connect(this->exitAct, SIGNAL(triggered()), this, SLOT(close()));
this->cutAct = new QAction(tr("Cu&t"), this);
this->cutAct->setShortcut(tr("Ctrl+X"));
this->cutAct->setStatusTip(tr("Cut the current selection's contents to the "
"clipboard"));
this->connect(this->cutAct, SIGNAL(triggered()), this->TextEdit, SLOT(cut()));
this->copyAct = new QAction(tr("&Copy"), this);
this->copyAct->setShortcut(tr("Ctrl+C"));
this->copyAct->setStatusTip(tr("Copy the current selection's contents to the "
"clipboard"));
this->connect(this->copyAct, SIGNAL(triggered()), this->TextEdit, SLOT(copy()));
this->pasteAct = new QAction(tr("&Paste"), this);
this->pasteAct->setShortcut(tr("Ctrl+V"));
this->pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
"selection"));
this->connect(this->pasteAct, SIGNAL(triggered()), this->TextEdit, SLOT(paste()));
this->saveAsMacroAct->setEnabled(false);
this->cutAct->setEnabled(false);
this->copyAct->setEnabled(false);
this->connect(this->TextEdit, SIGNAL(copyAvailable(bool)), this->cutAct, SLOT(setEnabled(bool)));
this->connect(this->TextEdit, SIGNAL(copyAvailable(bool)), this->copyAct, SLOT(setEnabled(bool)));
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::createMenus()
{
this->menuBar()->setObjectName("PythonScriptEditorMenuBar");
this->fileMenu = menuBar()->addMenu(tr("&File"));
this->fileMenu->setObjectName("File");
this->fileMenu->addAction(this->newAct);
this->fileMenu->addAction(this->openAct);
this->fileMenu->addAction(this->saveAct);
this->fileMenu->addAction(this->saveAsAct);
this->fileMenu->addAction(this->saveAsMacroAct);
this->fileMenu->addSeparator();
this->fileMenu->addAction(this->exitAct);
this->editMenu = menuBar()->addMenu(tr("&Edit"));
this->editMenu->setObjectName("Edit");
this->editMenu->addAction(this->cutAct);
this->editMenu->addAction(this->copyAct);
this->editMenu->addAction(this->pasteAct);
this->menuBar()->addSeparator();
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::createStatusBar()
{
this->statusBar()->showMessage(tr("Ready"));
}
//-----------------------------------------------------------------------------
bool pqPythonScriptEditor::maybeSave()
{
if (this->TextEdit->document()->isModified())
{
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("Script Editor"), tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (ret == QMessageBox::Save)
{
return this->save();
}
else if (ret == QMessageBox::Cancel)
{
return false;
}
}
return true;
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::setText(const QString& text)
{
this->TextEdit->setPlainText(text);
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::loadFile(const QString& fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::warning(this, tr("Script Editor"),
tr("Cannot read file %1:\n%2.").arg(fileName).arg(file.errorString()));
return;
}
QTextStream in(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
this->TextEdit->setPlainText(in.readAll());
QApplication::restoreOverrideCursor();
this->setCurrentFile(fileName);
this->statusBar()->showMessage(tr("File loaded"), 2000);
}
//-----------------------------------------------------------------------------
bool pqPythonScriptEditor::saveFile(const QString& fileName)
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::warning(
this, tr("Sorry!"), tr("Cannot write file %1:\n%2.").arg(fileName).arg(file.errorString()));
return false;
}
QTextStream out(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
out << this->TextEdit->toPlainText();
QApplication::restoreOverrideCursor();
this->setCurrentFile(fileName);
this->statusBar()->showMessage(tr("File saved"), 2000);
emit this->fileSaved();
return true;
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::setCurrentFile(const QString& fileName)
{
this->CurrentFile = fileName;
this->TextEdit->document()->setModified(false);
this->setWindowModified(false);
QString shownName;
if (this->CurrentFile.isEmpty())
{
shownName = "untitled.py";
}
else
{
shownName = strippedName(this->CurrentFile);
}
this->setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Script Editor")));
}
//-----------------------------------------------------------------------------
QString pqPythonScriptEditor::strippedName(const QString& fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
//-----------------------------------------------------------------------------
void pqPythonScriptEditor::scrollToBottom()
{
this->TextEdit->verticalScrollBar()->setValue(this->TextEdit->verticalScrollBar()->maximum());
}