SMILX  1.01
milxQtPythonPlugin.cpp
1 /*=========================================================================
2  The Software is copyright (c) Commonwealth Scientific and Industrial Research Organisation (CSIRO)
3  ABN 41 687 119 230.
4  All rights reserved.
5 
6  Licensed under the CSIRO BSD 3-Clause License
7  You may not use this file except in compliance with the License.
8  You may obtain a copy of the License in the file LICENSE.md or at
9 
10  https://stash.csiro.au/projects/SMILI/repos/smili/browse/license.txt
11 
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 =========================================================================*/
18 #include "milxQtPythonPlugin.h"
19 
20 #include <qplugin.h>
21 
23 {
25  MainWindow = qobject_cast<milxQtMain *>(theParent);
26 
27  threaded = false;
28  dockable = true;
29  consoleWindow = true;
30  extension = false;
31  pluginName = "Python";
32  dataName = "";
33 
34  setupPython();
35 
36  dock = new QDockWidget(tr("Python Console"));
37  dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
38  dock->setWidget(pyConsole);
39 
40  //Syntax Highlight
42  highlighter->rehighlight();
43 
44  createConnections();
45 }
46 
47 milxQtPythonPlugin::~milxQtPythonPlugin()
48 {
49  if(isRunning() && threaded)
50  quit();
51  cerr << "Python Plugin Destroyed." << endl;
52 }
53 
55 {
56  return pluginName;
57 }
58 
60 {
61  QString openPythonExt = "";
62 
63  return openPythonExt;
64 }
65 
67 {
68  QStringList exts;
69 
70  return exts;
71 }
72 
74 {
75  QStringList exts;
76 
77  return exts;
78 }
79 
81 {
82  QString savePythonExt = "";
83 
84  return savePythonExt;
85 }
86 
87 void milxQtPythonPlugin::SetInputCollection(vtkPolyDataCollection* collection, QStringList &filenames)
88 {
89 
90 }
91 
92 void milxQtPythonPlugin::open(QString filename)
93 {
94 
95 }
96 
97 void milxQtPythonPlugin::save(QString filename)
98 {
99 
100 }
101 
103 {
104  return NULL;
105 } //No image result
106 
108 {
109  return NULL;
110 } //No image result
111 
113 {
114  return NULL;
115 } //No image result
116 
118 {
119  pyConsole->show();
120 
121  return dock;
122 }
123 
125 {
126  if(pluginWindow(window) == 0)
127  return false;
128  else
129  return true;
130 }
131 
133 {
134  if(window)
135  return qobject_cast<milxQtPythonConsole *>(window);
136  return 0;
137 }
138 
140 {
141  QMutexLocker locker(&mutex); //Lock memory
142 
144  dock->show();
145 
146  exec();
147 }
148 
150 {
151  PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
152 // PythonQt_QtAll::init();
153  mainContext = PythonQt::self()->getMainModule();
154 
156 // pyConsole->resize(512,64);
157  pyConsole->setWindowTitle("Python");
158 // pyConsole->setMaximumHeight(consoleMinHeight);
159 
160  // make the object in to the python shell under the name "MainWindow"
161  mainContext.addObject("MainWindow", MainWindow);
162  milxQtFile *fileIO = new milxQtFile(dock);
164  mainContext.addObject("milxQtFile", fileIO); //Add file to python env
165  mainContext.addObject("milxQtUnifiedWindow", unified); //Add file to python env
166  mainContext.addObject("qApp", qApp); //Add app pointer to python env
167 }
168 
169 void milxQtPythonPlugin::createConnections()
170 {
171  QObject::connect(dock, SIGNAL(topLevelChanged(bool)), this, SLOT(resizeForFloat(bool)));
172  QObject::connect(MainWindow, SIGNAL(displayed(milxQtRenderWindow*)), this, SLOT(addObject(milxQtRenderWindow*)));
173  QObject::connect(MainWindow, SIGNAL(displayed(milxQtImage*)), this, SLOT(addObject(milxQtImage*)));
174  QObject::connect(MainWindow, SIGNAL(displayed(milxQtModel*)), this, SLOT(addObject(milxQtModel*)));
175 }
176 
177 void milxQtPythonPlugin::resizeForFloat(bool floating)
178 {
179  if(floating)
180  {
181  pyConsole->resize(512,128);
182  pyConsole->setMaximumHeight(QWIDGETSIZE_MAX);
183  }
184  else
185  {
186  pyConsole->resize(256,consoleMinHeight);
187  pyConsole->setMaximumHeight(consoleMinHeight);
188  }
189 }
190 
191 Q_EXPORT_PLUGIN2(PythonPlugin, milxQtPythonPluginFactory);
192 
193 //Syntax class implementation
194 milxQtPythonSyntaxHighlighter::milxQtPythonSyntaxHighlighter(QTextEdit *textEdit) : QSyntaxHighlighter(textEdit)
195 {
196  // Reserved keywords in Python 2.4
197  keywords << "\\band\\b" << "\\bassert\bb" << "\\bbreak\\b" << "\\bclass\\b" << "\\bcontinue\\b" << "\\bdef\\b"
198  << "\\bdel\\b" << "\\belif\\b" << "\\belse\\b" << "\\bexcept\\b" << "\\bexec\\b" << "\\bfinally\\b"
199  << "\\bfor\\b" << "\\bfrom\\b" << "\\bglobal\\b" << "\\bif\\b" << "\\bimport\\b" << "\\bin\\b"
200  << "\\bis\\b" << "\\blambda\\b" << "\\bnot\\b" << "\\bor\\b" << "\\bpass\\b" << "\\bprint\\b" << "\\braise\\b"
201  << "\\breturn\\b" << "\\btry\\b" << "\\bwhile\\b" << "\\byield\\b";
202 
203  HighlightingRule rule;
204 
205  keywordFormat.setForeground(Qt::darkBlue);
206  keywordFormat.setFontWeight(QFont::Bold);
207 
208  multiLineCommentFormat.setForeground(Qt::darkRed);
209  multiLineEscapeFormat.setForeground(Qt::black);
210 
211 // commentStartExpression = QRegExp("'''");
212 // commentEndExpression = QRegExp("'''");
213  commentStartExpression = QRegExp("/\\*");
214  commentEndExpression = QRegExp("\\*/");
215  escapeStartExpression = QRegExp("/\\@");
216  escapeEndExpression = QRegExp("\\@/");
217 
219  foreach (const QString &pattern, keywords)
220  {
221  rule.pattern = QRegExp(pattern);
222  rule.format = keywordFormat;
223  highlightingRules.append(rule);
224  }
225 
227  quotationFormat.setForeground(Qt::magenta);
228  rule.pattern = QRegExp("\".*\"");
229  rule.format = quotationFormat;
230  highlightingRules.append(rule);
231 
233  singleQuotationFormat.setForeground(Qt::darkYellow);
234  rule.pattern = QRegExp("'.*'");
235  rule.format = singleQuotationFormat;
236  highlightingRules.append(rule);
237 
239  singleLineCommentFormat.setForeground(Qt::darkRed);
240  rule.pattern = QRegExp("#[^\n]*");
241  rule.format = singleLineCommentFormat;
242  highlightingRules.append(rule);
243 }
244 
246 {
247  foreach (const HighlightingRule &rule, highlightingRules)
248  {
249  QRegExp expression(rule.pattern);
250  int index = expression.indexIn(text);
251  while (index >= 0)
252  {
253  int length = expression.matchedLength();
254  setFormat(index, length, rule.format);
255  index = expression.indexIn(text, index + length);
256  }
257  }
258  setCurrentBlockState(0);
259 
261  int startIndex = 0;
262  if (previousBlockState() != 1)
263  startIndex = commentStartExpression.indexIn(text);
264 
265  while (startIndex >= 0)
266  {
267  int endIndex = commentEndExpression.indexIn(text, startIndex);
268  int commentLength;
269 
270  if (endIndex == -1)
271  {
272  setCurrentBlockState(1);
273  commentLength = text.length() - startIndex;
274  }
275  else
276  commentLength = endIndex - startIndex + commentEndExpression.matchedLength();
277 
278  setFormat(startIndex, commentLength, multiLineCommentFormat);
279  startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
280  }
281 
283  startIndex = 0;
284  if (previousBlockState() != 2)
285  startIndex = escapeStartExpression.indexIn(text);
286 
287  while (startIndex >= 0)
288  {
289  int endIndex = escapeEndExpression.indexIn(text, startIndex);
290  int commentLength;
291 
292  if (endIndex == -1)
293  {
294  setCurrentBlockState(2);
295  commentLength = text.length() - startIndex;
296  }
297  else
298  commentLength = endIndex - startIndex + escapeEndExpression.matchedLength();
299 
300  setFormat(startIndex, commentLength, multiLineEscapeFormat);
301  startIndex = escapeStartExpression.indexIn(text, startIndex + commentLength);
302  }
303 }
QPointer< milxQtPythonConsole > pyConsole
Python Console widget.
QStringList openExtensions()
Get a list of supported file format extensions. [Implement this in your plugin].
bool threaded
Threaded plugin?
void SetInputCollection(vtkPolyDataCollection *collection, QStringList &filenames)
Pass a collection to internal plugin class. [Implement this in your plugin].
QString saveFileSupport()
Get the file support string for saving (extension wildcard list). [Implement this in your plugin]...
This class represents the MILX Qt Render Window Display object using QVTK.
milxQtModel * modelResult()
Get the model result. The result can then be displayed in milxQtMain etc. [Implement this in your plu...
bool consoleWindow
console window?
This class represents the MILX Qt Image Display object using VTK.
Definition: milxQtImage.h:118
milxQtRenderWindow * genericResult()
Get the generic result, which is a milxQtRenderWindow. The result can then be displayed in milxQtMain...
bool extension
Extension rather than a plugin?
This class represents the MILX Qt File I/O object using VTK/ITK/Qt.
Definition: milxQtFile.h:60
void highlightBlock(const QString &text)
This class represents the MILX Qt Model/Mesh Display object using VTK.
Definition: milxQtModel.h:115
void save(QString filename)
Save the result as a file using the plugin. [Implement this in your plugin].
bool dockable
Dockable plugin?
Syntax highlighting for Python to be applied to the console (such as milxQtPythonConsole).
The class maintains a state and render of multiple display objects (such as a milxQtModel or milxQtIm...
void open(QString filename)
Open the file using the plugin. [Implement this in your plugin].
The interface for any plugins that can be made for milxQtMain.
milxQtPythonConsole * pluginWindow(QWidget *window)
QStringList saveExtensions()
Get a list of supported file format extensions. [Implement this in your plugin].
QPointer< QDockWidget > dock
Dock widget.
QString openFileSupport()
Get the file support string for opening (extension wildcard list). [Implement this in your plugin]...
milxQtPythonPlugin(QObject *theParent=0)
milxQtPythonSyntaxHighlighter(QTextEdit *textEdit)
QString name()
Get the Name of the plugin. [Implement this in your plugin].
This class represents the MILX Qt Main Window object using Qt.
Definition: milxQtMain.h:85
QDockWidget * dockWidget()
Return the dock widget (if one is provided by plugin). [Implement this in your plugin].
A simple console for python scripting with some syntax highlighting.
PythonQtObjectPtr mainContext
Python Context to Python Intepreter.
milxQtImage * imageResult()
Get the image result. The result can then be displayed in milxQtMain etc.[Implement this in your plug...
bool isPluginWindow(QWidget *window)
Is the window provided a plugin generated window? In this case a milxQtShapeModel window...