pqPythonEventSource.cxx 13.2 KB
Newer Older
Clinton Stimpson's avatar
 
Clinton Stimpson committed
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
/*=========================================================================

   Program: ParaView
   Module:    pqPythonEventSource.cxx

   Copyright (c) 2005,2006 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.1. 

   See License_v1.1.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.

=========================================================================*/

// python header first
Clinton Stimpson's avatar
   
Clinton Stimpson committed
34
35
// TODO:  Fix this so we don't depend on VTK
#include "vtkPython.h"
Clinton Stimpson's avatar
   
Clinton Stimpson committed
36
37
38
#ifndef PyMODINIT_FUNC
#define PyMODINIT_FUNC extern "C" void
#endif // PyMODINIT_FUNC
Clinton Stimpson's avatar
 
Clinton Stimpson committed
39
40
41
42

// self include
#include "pqPythonEventSource.h"

43
44
45
// system includes
#include <signal.h>

Clinton Stimpson's avatar
 
Clinton Stimpson committed
46
47
48
49
50
51
// Qt include
#include <QVariant>
#include <QFile>
#include <QtDebug>
#include <QCoreApplication>
#include <QEvent>
52
53
54
#include <QStringList>
#include <QThread>
#include <QApplication>
Clinton Stimpson's avatar
 
Clinton Stimpson committed
55
56
57

// Qt testing includes
#include "pqObjectNaming.h"
Clinton Stimpson's avatar
Clinton Stimpson committed
58
#include "pqWidgetEventPlayer.h"
Clinton Stimpson's avatar
   
Clinton Stimpson committed
59
#include "pqEventDispatcher.h"
Clinton Stimpson's avatar
 
Clinton Stimpson committed
60
61
62
63
64
65


// TODO not have a global instance pointer?
static pqPythonEventSource* Instance = NULL;
static QString PropertyObject;
static QString PropertyResult;
66
static QString PropertyValue;
67
static QStringList ObjectList;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87


static PyObject*
QtTesting_playCommand(PyObject* /*self*/, PyObject* args)
{
  // void QtTesting.playCommand('object', 'command', 'arguments')
  //   an exception is thrown in this fails
  
  const char* object = 0;
  const char* command = 0;
  const char* arguments = 0;

  if(!PyArg_ParseTuple(args, const_cast<char*>("sss"), &object, &command, &arguments))
    {
    PyErr_SetString(PyExc_TypeError, "bad arguments to playCommand()");
    return NULL;
    }

  if(Instance)
    {
Clinton Stimpson's avatar
   
Clinton Stimpson committed
88
89
90
91
92
    if(!Instance->postNextEvent(object, command, arguments))
      {
      PyErr_SetString(PyExc_AssertionError, "error processing event");
      return NULL;
      }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    }
  else
    {
    PyErr_SetString(PyExc_AssertionError, "pqPythonEventSource not defined");
    return NULL;
    }

  return Py_BuildValue(const_cast<char*>(""));
}

static PyObject*
QtTesting_getProperty(PyObject* /*self*/, PyObject* args)
{
  // string QtTesting.getProperty('object', 'property')
  //    returns the string value of the property
  
  const char* object = 0;
  const char* property = 0;

  if(!PyArg_ParseTuple(args, const_cast<char*>("ss"), &object, &property))
    {
    return NULL;
    }

117
118
119
120
121
122
  PropertyObject = object;
  PropertyResult = property;

  if(Instance && QThread::currentThread() != QApplication::instance()->thread())
    {
    QMetaObject::invokeMethod(Instance, "threadGetProperty", Qt::QueuedConnection);
123
    if(!Instance->waitForGUI())
Clinton Stimpson's avatar
   
Clinton Stimpson committed
124
125
126
127
      {
      PyErr_SetString(PyExc_ValueError, "error getting property");
      return NULL;
      }
128
129
130
131
132
133
    }
  else if(QThread::currentThread() == QApplication::instance()->thread())
    {
    PropertyResult = pqPythonEventSource::getProperty(PropertyObject, PropertyResult);
    }
  else
Clinton Stimpson's avatar
 
Clinton Stimpson committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    {
    PyErr_SetString(PyExc_AssertionError, "pqPythonEventSource not defined");
    return NULL;
    }

  if(PropertyObject == QString::null)
    {
    PyErr_SetString(PyExc_ValueError, "object not found");
    return NULL;
    }

  return Py_BuildValue(const_cast<char*>("s"), 
             PropertyResult.toAscii().data());
}

149
150
151
152
153
154
155
156
157
158
static PyObject*
QtTesting_setProperty(PyObject* /*self*/, PyObject* args)
{
  // string QtTesting.setProperty('object', 'property', 'value')
  //    returns the string value of the property
  
  const char* object = 0;
  const char* property = 0;
  const char* value = 0;

159
  if(!PyArg_ParseTuple(args, const_cast<char*>("sss"), &object, 
160
161
162
163
164
165
166
167
168
169
170
171
                                                      &property, &value))
    {
    return NULL;
    }

  PropertyObject = object;
  PropertyResult = property;
  PropertyValue = value;

  if(Instance && QThread::currentThread() != QApplication::instance()->thread())
    {
    QMetaObject::invokeMethod(Instance, "threadSetProperty", Qt::QueuedConnection);
172
    if(!Instance->waitForGUI())
Clinton Stimpson's avatar
   
Clinton Stimpson committed
173
174
175
176
      {
      PyErr_SetString(PyExc_ValueError, "error setting property");
      return NULL;
      }
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
    }
  else if(QThread::currentThread() == QApplication::instance()->thread())
    {
    pqPythonEventSource::setProperty(PropertyObject, 
                                     PropertyResult,
                                     PropertyValue);
    }
  else
    {
    PyErr_SetString(PyExc_AssertionError, "pqPythonEventSource not defined");
    return NULL;
    }

  if(PropertyObject == QString::null)
    {
    PyErr_SetString(PyExc_ValueError, "object not found");
    return NULL;
    }

  if(PropertyResult == QString::null)
    {
    PyErr_SetString(PyExc_ValueError, "property not found");
    return NULL;
    }

  return Py_BuildValue(const_cast<char*>("s"), "");
}

Clinton Stimpson's avatar
   
Clinton Stimpson committed
205
206
207
208
209
210
211
212
213
static PyObject*
QtTesting_getQtVersion(PyObject* /*self*/, PyObject* /*args*/)
{
  // string QtTesting.getQtVersion()
  //    returns the Qt version as a string
  
  return Py_BuildValue(const_cast<char*>("s"), qVersion());
}

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

static PyObject*
QtTesting_getChildren(PyObject* /*self*/, PyObject* args)
{
  // string QtTesting.getChildren('object')
  //    returns the a list of strings with object names
  
  const char* object = 0;

  if(!PyArg_ParseTuple(args, const_cast<char*>("s"), &object))
    {
    return NULL;
    }

  PropertyObject = object;
  ObjectList.clear();

  if(Instance && QThread::currentThread() != QApplication::instance()->thread())
    {
    QMetaObject::invokeMethod(Instance, "threadGetChildren", Qt::QueuedConnection);
234
    if(!Instance->waitForGUI())
Clinton Stimpson's avatar
   
Clinton Stimpson committed
235
236
237
238
      {
      PyErr_SetString(PyExc_ValueError, "error getting children");
      return NULL;
      }
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
    }
  else if(QThread::currentThread() == QApplication::instance()->thread())
    {
    ObjectList = pqPythonEventSource::getChildren(PropertyObject);
    }
  else
    {
    PyErr_SetString(PyExc_AssertionError, "pqPythonEventSource not defined");
    return NULL;
    }

  if(PropertyObject == QString::null)
    {
    PyErr_SetString(PyExc_ValueError, "object not found");
    return NULL;
    }

  QString objs = ObjectList.join(", ");
  QString ret = QString("[%1]").arg(objs);

  return Py_BuildValue(const_cast<char*>("s"), 
             ret.toAscii().data());
}

Clinton Stimpson's avatar
   
Clinton Stimpson committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
static PyObject*
QtTesting_invokeMethod(PyObject* /*self*/, PyObject* args)
{
  // string QtTesting.invokeMethod('object', 'method')
  //    calls a method and returns its value
  
  const char* object = 0;
  const char* method = 0;

  if(!PyArg_ParseTuple(args, const_cast<char*>("ss"), &object, &method))
    {
    return NULL;
    }

  PropertyObject = object;
  PropertyValue = method;
  PropertyResult = QString();

  if(Instance && QThread::currentThread() != QApplication::instance()->thread())
    {
    QMetaObject::invokeMethod(Instance, "threadInvokeMethod", Qt::QueuedConnection);
284
    if(!Instance->waitForGUI())
Clinton Stimpson's avatar
   
Clinton Stimpson committed
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
      {
      PyErr_SetString(PyExc_ValueError, "error invoking method");
      return NULL;
      }
    }
  else if(QThread::currentThread() == QApplication::instance()->thread())
    {
    PropertyResult = pqPythonEventSource::invokeMethod(PropertyObject,
                                                       PropertyValue);
    }
  else
    {
    PyErr_SetString(PyExc_AssertionError, "pqPythonEventSource not defined");
    return NULL;
    }

  if(PropertyObject == QString::null)
    {
    PyErr_SetString(PyExc_ValueError, "object not found");
    return NULL;
    }
  else if(PropertyValue == QString::null)
    {
    PyErr_SetString(PyExc_ValueError, "method not found");
    return NULL;
    }

  return Py_BuildValue(const_cast<char*>("s"), 
             PropertyResult.toAscii().data());
}
315

Clinton Stimpson's avatar
 
Clinton Stimpson committed
316
317
318
319
320
321
322
323
324
325
326
327
328
static PyMethodDef QtTestingMethods[] = {
  {
    const_cast<char*>("playCommand"), 
    QtTesting_playCommand,
    METH_VARARGS,
    const_cast<char*>("Play a test command.")
  },
  {
    const_cast<char*>("getProperty"),
    QtTesting_getProperty,
    METH_VARARGS,
    const_cast<char*>("Get a property of an object.")
  },
329
330
331
332
333
334
335
  {
    const_cast<char*>("setProperty"),
    QtTesting_setProperty,
    METH_VARARGS,
    const_cast<char*>("Set a property of an object.")
  },

Clinton Stimpson's avatar
   
Clinton Stimpson committed
336
337
338
339
340
341
  {
    const_cast<char*>("getQtVersion"),
    QtTesting_getQtVersion,
    METH_VARARGS,
    const_cast<char*>("Get the version of Qt being used.")
  },
342
343
344
345
346
347
  {
    const_cast<char*>("getChildren"),
    QtTesting_getChildren,
    METH_VARARGS,
    const_cast<char*>("Return a list of child objects.")
  },
Clinton Stimpson's avatar
   
Clinton Stimpson committed
348
349
350
351
352
353
  {
    const_cast<char*>("invokeMethod"),
    QtTesting_invokeMethod,
    METH_VARARGS,
    const_cast<char*>("Invoke a Qt slot with the signature \"QVariant foo()\".")
  },
Clinton Stimpson's avatar
 
Clinton Stimpson committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370

  {NULL, NULL, 0, NULL} // Sentinal
};

PyMODINIT_FUNC
initQtTesting(void)
{
  Py_InitModule(const_cast<char*>("QtTesting"), QtTestingMethods);
}


class pqPythonEventSource::pqInternal
{
public:
  QString FileName;
};

Clinton Stimpson's avatar
   
Clinton Stimpson committed
371
372
pqPythonEventSource::pqPythonEventSource(QObject* p)
  : pqThreadedEventSource(p)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
373
374
{
  this->Internal = new pqInternal;
375
376
377
378
379
  int initPy = Py_IsInitialized();
  if(!initPy)
    {
    // initialize python
    Py_Initialize();
380
#ifdef SIGINT
381
    signal(SIGINT, SIG_DFL);
382
#endif
383
384
385
386
    
    // add QtTesting to python's inittab, so it is
    // available to all interpreters
    PyImport_AppendInittab(const_cast<char*>("QtTesting"), initQtTesting);
387
    }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
}

pqPythonEventSource::~pqPythonEventSource()
{
  delete this->Internal;
}

void pqPythonEventSource::setContent(const QString& path)
{
  // start the python thread
  this->Internal->FileName = path;
  this->start();
}
  
402
QString pqPythonEventSource::getProperty(QString& object, const QString& prop)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
403
{
Clinton Stimpson's avatar
   
Clinton Stimpson committed
404
  // ensure other tasks have been completed
405
  pqEventDispatcher::processEventsAndWait(1);
406
  QVariant ret;
Clinton Stimpson's avatar
   
Clinton Stimpson committed
407

408
  QObject* qobject = pqObjectNaming::GetObject(object);
409
410
  if(!qobject)
    {
411
    object = QString::null;
412
413
    }
  else
Clinton Stimpson's avatar
 
Clinton Stimpson committed
414
    {
415
    ret = qobject->property(prop.toAscii().data()).toString();
Clinton Stimpson's avatar
 
Clinton Stimpson committed
416
    }
417
418
419
420
421

  return ret.toString();

}

422

423
424
425
void pqPythonEventSource::threadGetProperty()
{
  PropertyResult = this->getProperty(PropertyObject, PropertyResult);
Clinton Stimpson's avatar
   
Clinton Stimpson committed
426
  this->guiAcknowledge();
427
428
}

429
430
431
432
433

void pqPythonEventSource::setProperty(QString& object, QString& prop, 
                                      const QString& value)
{
  // ensure other tasks have been completed
434
  pqEventDispatcher::processEventsAndWait(1);
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
  QVariant ret;

  QObject* qobject = pqObjectNaming::GetObject(object);
  if(!qobject)
    {
    object = QString::null;
    }
  else
    {
    if(!qobject->setProperty(prop.toAscii().data(), value))
      {
      prop = QString::null;
      }
    }
}

void pqPythonEventSource::threadSetProperty()
{
  this->setProperty(PropertyObject, PropertyResult, PropertyValue);
Clinton Stimpson's avatar
   
Clinton Stimpson committed
454
  this->guiAcknowledge();
455
456
457
}


458
459
460
QStringList pqPythonEventSource::getChildren(QString& object)
{
  // ensure other tasks have been completed
461
  pqEventDispatcher::processEventsAndWait(1);
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
  QStringList ret;

  QObject* qobject = pqObjectNaming::GetObject(object);
  if(!qobject)
    {
    object = QString::null;
    }
  else
    {
    const QObjectList& children = qobject->children();
    foreach(QObject* child, children)
      {
      ret.append(pqObjectNaming::GetName(*child));
      }
    }
  return ret;
}


void pqPythonEventSource::threadGetChildren()
{
  ObjectList = this->getChildren(PropertyObject);
Clinton Stimpson's avatar
   
Clinton Stimpson committed
484
  this->guiAcknowledge();
Clinton Stimpson's avatar
 
Clinton Stimpson committed
485
486
487
488
}

void pqPythonEventSource::run()
{
489
490
491
492
493
494
  QFile file(this->Internal->FileName);
  if(!file.open(QFile::ReadOnly | QFile::Text))
    {
    printf("Unable to open python script\n");
    return;
    } 
Clinton Stimpson's avatar
   
Clinton Stimpson committed
495

496
497
  PyEval_InitThreads();
  PyEval_ReleaseLock();
498
499

  PyThreadState* curState = PyThreadState_Get();
500
  PyThreadState* threadState = Py_NewInterpreter();
Clinton Stimpson's avatar
 
Clinton Stimpson committed
501
502
  
  Instance = this;
503
  PyThreadState_Swap(threadState);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
504
505

  // finally run the script
506
507
  QByteArray wholeFile = file.readAll();
  int result = PyRun_SimpleString(wholeFile.data()) == 0 ? 0 : 1;
508

509
  PyThreadState_Swap(threadState);
510
  Py_EndInterpreter(threadState);
511
  PyThreadState_Swap(curState);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
512
  
Clinton Stimpson's avatar
   
Clinton Stimpson committed
513
  this->done(result);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
514
515
}

Clinton Stimpson's avatar
   
Clinton Stimpson committed
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
void pqPythonEventSource::threadInvokeMethod()
{
  PropertyResult = this->invokeMethod(PropertyObject, PropertyValue);
  this->guiAcknowledge();
}

QString pqPythonEventSource::invokeMethod(QString& object, QString& method)
{
  // ensure other tasks have been completed
  pqEventDispatcher::processEventsAndWait(1);
  QVariant ret;

  QObject* qobject = pqObjectNaming::GetObject(object);
  if(!qobject)
    {
    object = QString::null;
    }
  else
    {
    if(!QMetaObject::invokeMethod(qobject, method.toAscii().data(),
                                  Q_RETURN_ARG(QVariant, ret)))
      {
      method = QString::null;
      }
    }
  return ret.toString();
}