Menu

[r28]: / src / PythonQt.cpp  Maximize  Restore  History

Download this file

944 lines (849 with data), 30.9 kB

  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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
/*
*
* Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* Further, this software is distributed without any warranty that it is
* free of the rightful claim of any third person regarding infringement
* or the like. Any license provided herein, whether implied or
* otherwise, applies only to this software file. Patent licenses, if
* any, provided herein do not apply to combinations of this program with
* other software, or any other product whatsoever.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact information: MeVis Research GmbH, Universitaetsallee 29,
* 28359 Bremen, Germany or:
*
* http://www.mevis.de
*
*/
//----------------------------------------------------------------------------------
/*!
// \file PythonQt.cpp
// \author Florian Link
// \author Last changed by $Author: florian $
// \date 2006-05
*/
//----------------------------------------------------------------------------------
#include "PythonQt.h"
#include "PythonQtImporter.h"
#include "PythonQtClassInfo.h"
#include "PythonQtMethodInfo.h"
#include "PythonQtSignalReceiver.h"
#include "PythonQtConversion.h"
#include "PythonQtStdOut.h"
#include "PythonQtCppWrapperFactory.h"
#include "PythonQtVariants.h"
#include "PythonQtStdDecorators.h"
#include <pydebug.h>
PythonQt* PythonQt::_self = NULL;
void PythonQt::init(int flags)
{
if (!_self) {
_self = new PythonQt(flags);
}
PythonQt::self()->addDecorators(new PythonQtStdDecorators());
PythonQt::priv()->addVariantWrapper("QBitArray", new PythonQtQBitArrayWrapper);
PythonQt::priv()->addVariantWrapper("QDate", new PythonQtQDateWrapper);
PythonQt::priv()->addVariantWrapper("QTime", new PythonQtQTimeWrapper);
PythonQt::priv()->addVariantWrapper("QDateTime", new PythonQtQDateTimeWrapper);
PythonQt::priv()->addVariantWrapper("QUrl", new PythonQtQUrlWrapper);
PythonQt::priv()->addVariantWrapper("QLocale", new PythonQtQLocaleWrapper);
PythonQt::priv()->addVariantWrapper("QRect", new PythonQtQRectWrapper);
PythonQt::priv()->addVariantWrapper("QRectF", new PythonQtQRectFWrapper);
PythonQt::priv()->addVariantWrapper("QSize", new PythonQtQSizeWrapper);
PythonQt::priv()->addVariantWrapper("QSizeF", new PythonQtQSizeFWrapper);
PythonQt::priv()->addVariantWrapper("QLine", new PythonQtQLineWrapper);
PythonQt::priv()->addVariantWrapper("QLineF", new PythonQtQLineFWrapper);
PythonQt::priv()->addVariantWrapper("QPoint", new PythonQtQPointWrapper);
PythonQt::priv()->addVariantWrapper("QPointF", new PythonQtQPointFWrapper);
PythonQt::priv()->addVariantWrapper("QRegExp", new PythonQtQRegExpWrapper);
PythonQt::priv()->addVariantWrapper("QFont", new PythonQtQFontWrapper);
PythonQt::priv()->addVariantWrapper("QPixmap", new PythonQtQPixmapWrapper);
PythonQt::priv()->addVariantWrapper("QBrush", new PythonQtQBrushWrapper);
PythonQt::priv()->addVariantWrapper("QColor", new PythonQtQColorWrapper);
PythonQt::priv()->addVariantWrapper("QPalette", new PythonQtQPaletteWrapper);
PythonQt::priv()->addVariantWrapper("QIcon", new PythonQtQIconWrapper);
PythonQt::priv()->addVariantWrapper("QImage", new PythonQtQImageWrapper);
PythonQt::priv()->addVariantWrapper("QPolygon", new PythonQtQPolygonWrapper);
PythonQt::priv()->addVariantWrapper("QRegion", new PythonQtQRegionWrapper);
PythonQt::priv()->addVariantWrapper("QBitmap", new PythonQtQBitmapWrapper);
PythonQt::priv()->addVariantWrapper("QCursor", new PythonQtQCursorWrapper);
PythonQt::priv()->addVariantWrapper("QSizePolicy", new PythonQtQSizePolicyWrapper);
PythonQt::priv()->addVariantWrapper("QKeySequence", new PythonQtQKeySequenceWrapper);
PythonQt::priv()->addVariantWrapper("QPen", new PythonQtQPenWrapper);
PythonQt::priv()->addVariantWrapper("QTextLength", new PythonQtQTextLengthWrapper);
PythonQt::priv()->addVariantWrapper("QTextFormat", new PythonQtQTextFormatWrapper);
PythonQt::priv()->addVariantWrapper("QMatrix", new PythonQtQMatrixWrapper);
}
void PythonQt::cleanup()
{
if (_self) {
delete _self;
_self = NULL;
}
}
PythonQt::PythonQt(int flags)
{
_p = new PythonQtPrivate;
_p->_initFlags = flags;
_p->_PythonQtObjectPtr_metaId = qRegisterMetaType<PythonQtObjectPtr>("PythonQtObjectPtr");
Py_SetProgramName("PythonQt");
if (flags & IgnoreSiteModule) {
// this prevents the automatic importing of Python site files
Py_NoSiteFlag = 1;
}
Py_Initialize();
// add our own python object types for qt object slots
if (PyType_Ready(&PythonQtSlotFunction_Type) < 0) {
std::cerr << "could not initialize PythonQtSlotFunction_Type" << ", in " << __FILE__ << ":" << __LINE__ << std::endl;
}
Py_INCREF(&PythonQtSlotFunction_Type);
// add our own python object types for qt objects
if (PyType_Ready(&PythonQtWrapper_Type) < 0) {
std::cerr << "could not initialize PythonQtWrapper_Type" << ", in " << __FILE__ << ":" << __LINE__ << std::endl;
}
Py_INCREF(&PythonQtWrapper_Type);
// add our own python object types for qt objects
if (PyType_Ready(&PythonQtVariantWrapper_Type) < 0) {
std::cerr << "could not initialize PythonQtVariantWrapper_Type" << ", in " << __FILE__ << ":" << __LINE__ << std::endl;
}
Py_INCREF(&PythonQtVariantWrapper_Type);
// add our own python object types for qt objects
if (PyType_Ready(&PythonQtMetaObjectWrapper_Type) < 0) {
std::cerr << "could not initialize PythonQtMetaObjectWrapper_Type" << ", in " << __FILE__ << ":" << __LINE__ << std::endl;
}
Py_INCREF(&PythonQtMetaObjectWrapper_Type);
// add our own python object types for redirection of stdout
if (PyType_Ready(&PythonQtStdOutRedirectType) < 0) {
std::cerr << "could not initialize PythonQtStdOutRedirectType" << ", in " << __FILE__ << ":" << __LINE__ << std::endl;
}
Py_INCREF(&PythonQtStdOutRedirectType);
initPythonQtModule(flags & RedirectStdOut);
}
PythonQt::~PythonQt() {
delete _p;
_p = NULL;
}
PythonQtPrivate::~PythonQtPrivate() {
{
QHashIterator<QByteArray, PythonQtClassInfo *> i(_knownQtClasses);
while (i.hasNext()) {
delete i.next().value();
}
}
{
QHashIterator<QByteArray, PythonQtClassInfo *> i(_knownQtWrapperClasses);
while (i.hasNext()) {
delete i.next().value();
}
}
{
QHashIterator<int , QPair<PythonQtClassInfo*, QObject*> > i(_knownVariantWrappers);
while (i.hasNext()) {
delete i.next().value().first;
}
}
{
QHashIterator<QByteArray, PythonQtSlotInfo *> i(_constructorSlots);
while (i.hasNext()) {
delete i.next().value();
}
}
{
QHashIterator<QByteArray, PythonQtSlotInfo *> i(_destructorSlots);
while (i.hasNext()) {
delete i.next().value();
}
}
PythonQtConv::global_valueStorage.clear();
PythonQtConv::global_ptrStorage.clear();
PythonQtConv::global_variantStorage.clear();
PythonQtMethodInfo::cleanupCachedMethodInfos();
delete _qtNamespace;
}
PythonQtImportFileInterface* PythonQt::importInterface()
{
return _self->_p->_importInterface;
}
void PythonQt::registerClass(const QMetaObject* metaobject)
{
_p->registerClass(metaobject);
}
void PythonQtPrivate::registerClass(const QMetaObject* metaobject)
{
// we register all classes in the hierarchy
const QMetaObject* m = metaobject;
while (m) {
PythonQtClassInfo* info = _knownQtClasses.value(m->className());
if (!info) {
info = new PythonQtClassInfo(m);
_knownQtClasses.insert(m->className(), info);
PyModule_AddObject(_pythonQtModule, m->className(), (PyObject*)createNewPythonQtMetaObjectWrapper(info));
}
m = m->superClass();
}
}
bool PythonQtPrivate::isEnumType(const QMetaObject* meta, const QByteArray& name) {
int i = meta?meta->indexOfEnumerator(name.constData()):-1;
if (i!=-1) {
return true;
} else {
// look for scope
int scopePos = name.indexOf("::");
if (scopePos != -1) {
// slit into scope and enum name
QByteArray enumScope = name.mid(0,scopePos);
QByteArray enumName = name.mid(scopePos+2);
if (enumScope == "Qt") {
// special qt namespace case
return isEnumType(&staticQtMetaObject, enumName);
} else {
// look for known classes as scope
// TODO: Q_GADGETS are not yet handled
PythonQtClassInfo* info = _knownQtClasses.value(enumScope);
if (info) {
return isEnumType(info->metaObject(), enumName);
}
}
}
}
return false;
}
PyObject* PythonQtPrivate::wrapQObject(QObject* obj)
{
if (!obj) {
Py_INCREF(Py_None);
return Py_None;
}
PythonQtWrapper* wrap = _wrappedObjects.value(obj);
if (!wrap) {
// smuggling it in...
PythonQtClassInfo* classInfo = _knownQtClasses.value(obj->metaObject()->className());
if (!classInfo) {
registerClass(obj->metaObject());
classInfo = _knownQtClasses.value(obj->metaObject()->className());
}
wrap = createNewPythonQtWrapper(obj, classInfo);
// insert destroyed handler
connect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(wrappedObjectDestroyed(QObject*)));
// mlabDebugConst("MLABPython","new qobject wrapper added " << " " << wrap->_obj->className() << " " << wrap->_info->wrappedClassName().latin1());
} else {
Py_INCREF(wrap);
// mlabDebugConst("MLABPython","qobject wrapper reused " << wrap->_obj->className() << " " << wrap->_info->wrappedClassName().latin1());
}
return (PyObject*)wrap;
}
PyObject* PythonQtPrivate::wrapPtr(void* ptr, const QByteArray& name)
{
if (!ptr) {
Py_INCREF(Py_None);
return Py_None;
}
PythonQtWrapper* wrap = _wrappedObjects.value(ptr);
if (!wrap) {
PythonQtClassInfo* info = _knownQtClasses.value(name);
if (!info) {
// we do not know the metaobject yet, but we might know it by it's name:
if (_knownQObjectClassNames.find(name)!=_knownQObjectClassNames.end()) {
// yes, we know it, so we can convert to QObject
QObject* qptr = (QObject*)ptr;
registerClass(qptr->metaObject());
info = _knownQtClasses.value(qptr->metaObject()->className());
}
}
if (info) {
QObject* qptr = (QObject*)ptr;
// if the object is a derived object, we want to switch the class info to the one of the derived class:
if (name!=(qptr->metaObject()->className())) {
registerClass(qptr->metaObject());
info = _knownQtClasses.value(qptr->metaObject()->className());
}
wrap = createNewPythonQtWrapper(qptr, info);
// insert destroyed handler
connect(qptr, SIGNAL(destroyed(QObject*)), this, SLOT(wrappedObjectDestroyed(QObject*)));
// mlabDebugConst("MLABPython","new qobject wrapper added " << " " << wrap->_obj->className() << " " << wrap->_info->wrappedClassName().latin1());
} else {
// maybe it is a PyObject, which we can return directly
if (name == "PyObject") {
PyObject* p = (PyObject*)ptr;
Py_INCREF(p);
return p;
}
// not a known QObject, so try our wrapper factory:
QObject* wrapper = NULL;
for (int i=0; i<_cppWrapperFactories.size(); i++) {
wrapper = _cppWrapperFactories.at(i)->create(name, ptr);
if (wrapper) {
break;
}
}
PythonQtClassInfo* info = _knownQtWrapperClasses.value(name);
if (!info) {
info = new PythonQtClassInfo(wrapper?wrapper->metaObject():&QObject::staticQtMetaObject, name);
_knownQtWrapperClasses.insert(name, info);
PyModule_AddObject(_pythonQtModule, name, (PyObject*)createNewPythonQtMetaObjectWrapper(info));
} else {
if (wrapper && (info->metaObject() != wrapper->metaObject())) {
info->setMetaObject(wrapper->metaObject());
}
}
wrap = createNewPythonQtWrapper(wrapper, info, ptr);
// mlabDebugConst("MLABPython","new c++ wrapper added " << wrap->_wrappedPtr << " " << wrap->_obj->className() << " " << wrap->_info->wrappedClassName().latin1());
}
} else {
Py_INCREF(wrap);
//mlabDebugConst("MLABPython","c++ wrapper reused " << wrap->_wrappedPtr << " " << wrap->_obj->className() << " " << wrap->_info->wrappedClassName().latin1());
}
return (PyObject*)wrap;
}
void PythonQt::registerCPPClassNames(const QStringList& names)
{
foreach ( QString n, names) {
QByteArray name = n.toLatin1();
PythonQtClassInfo* info = _p->_knownQtWrapperClasses.value(name);
if (!info) {
info = new PythonQtClassInfo(&QObject::staticMetaObject, name);
_p->_knownQtWrapperClasses.insert(name, info);
PyModule_AddObject(_p->_pythonQtModule, name.data(), (PyObject*)_p->createNewPythonQtMetaObjectWrapper(info));
}
}
}
PythonQtWrapper* PythonQtPrivate::createNewPythonQtWrapper(QObject* obj, PythonQtClassInfo* info, void* wrappedPtr) {
PythonQtWrapper* result;
result = (PythonQtWrapper *)PythonQtWrapper_Type.tp_new(&PythonQtWrapper_Type,
NULL, NULL);
result->_obj = obj;
result->_info = info;
result->_wrappedPtr = wrappedPtr;
result->_ownedByPythonQt = false;
if (wrappedPtr) {
_wrappedObjects.insert(wrappedPtr, result);
} else {
_wrappedObjects.insert(obj, result);
}
return result;
}
PythonQtVariantWrapper* PythonQtPrivate::createNewPythonQtVariantWrapper(const QVariant& variant) {
PythonQtVariantWrapper* result;
result = (PythonQtVariantWrapper *)PythonQtVariantWrapper_Type.tp_new(&PythonQtVariantWrapper_Type,
NULL, NULL);
*result->_variant = variant;
QPair<PythonQtClassInfo*, QObject*> pair = _knownVariantWrappers.value(variant.userType());
result->_wrapper = pair.second;
result->_info = pair.first;
return result;
}
PythonQtMetaObjectWrapper* PythonQtPrivate::createNewPythonQtMetaObjectWrapper(PythonQtClassInfo* info) {
PythonQtMetaObjectWrapper* result;
result = (PythonQtMetaObjectWrapper *)PythonQtMetaObjectWrapper_Type.tp_new(&PythonQtMetaObjectWrapper_Type,
NULL, NULL);
result->_info = info;
return result;
}
PythonQtSignalReceiver* PythonQt::getSignalReceiver(QObject* obj)
{
PythonQtSignalReceiver* r = _p->_signalReceivers[obj];
if (!r) {
r = new PythonQtSignalReceiver(obj);
_p->_signalReceivers.insert(obj, r);
// insert destroyed handler
connect(obj, SIGNAL(destroyed(QObject*)), _p ,SLOT(destroyedSignalEmitter(QObject*)));
}
return r;
}
bool PythonQt::addSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname)
{
bool flag = false;
PythonQtObjectPtr callable = lookupCallable(module, objectname);
if (callable) {
PythonQtSignalReceiver* r = getSignalReceiver(obj);
flag = r->addSignalHandler(signal, callable);
if (!flag) {
// signal not found
}
} else {
// callable not found
}
return flag;
}
bool PythonQt::addSignalHandler(QObject* obj, const char* signal, PyObject* receiver)
{
bool flag = false;
PythonQtSignalReceiver* r = getSignalReceiver(obj);
if (r) {
flag = r->addSignalHandler(signal, receiver);
}
return flag;
}
bool PythonQt::removeSignalHandler(QObject* obj, const char* signal, PyObject* module, const QString& objectname)
{
bool flag = false;
PythonQtObjectPtr callable = lookupCallable(module, objectname);
if (callable) {
PythonQtSignalReceiver* r = _p->_signalReceivers[obj];
if (r) {
flag = r->removeSignalHandler(signal, callable);
}
} else {
// callable not found
}
return flag;
}
bool PythonQt::removeSignalHandler(QObject* obj, const char* signal, PyObject* receiver)
{
bool flag = false;
PythonQtSignalReceiver* r = _p->_signalReceivers[obj];
if (r) {
flag = r->removeSignalHandler(signal, receiver);
}
return flag;
}
PythonQtObjectPtr PythonQt::lookupCallable(PyObject* module, const QString& name)
{
PythonQtObjectPtr p = lookupObject(module, name);
if (p) {
if (PyCallable_Check(p)) {
return p;
}
}
PyErr_Clear();
return NULL;
}
PythonQtObjectPtr PythonQt::lookupObject(PyObject* module, const QString& name)
{
QStringList l = name.split('.');
PythonQtObjectPtr p = module;
PythonQtObjectPtr prev;
QString s;
QByteArray b;
for (QStringList::ConstIterator i = l.begin(); i!=l.end() && p; ++i) {
prev = p;
b = (*i).toLatin1();
p.setNewRef(PyObject_GetAttrString(p, b.data()));
}
PyErr_Clear();
return p;
}
PythonQtObjectPtr PythonQt::getMainModule() {
//both borrowed
PythonQtObjectPtr dict = PyImport_GetModuleDict();
return PyDict_GetItemString(dict, "__main__");
}
QVariant PythonQt::evalCode(PyObject* module, PyObject* pycode) {
QVariant result;
if (pycode) {
PyObject* r = PyEval_EvalCode((PyCodeObject*)pycode, PyModule_GetDict((PyObject*)module) , PyModule_GetDict((PyObject*)module));
if (r) {
result = PythonQtConv::PyObjToQVariant(r);
Py_DECREF(r);
} else {
handleError();
}
} else {
handleError();
}
return result;
}
QVariant PythonQt::evalScript(PyObject* module, const QString& script, int start)
{
QVariant result;
PythonQtObjectPtr p;
p.setNewRef(PyRun_String(script.toLatin1().data(), start, PyModule_GetDict(module), PyModule_GetDict(module)));
if (p) {
result = PythonQtConv::PyObjToQVariant(p);
} else {
handleError();
}
return result;
}
void PythonQt::evalFile(PyObject* module, const QString& filename)
{
PythonQtObjectPtr code = parseFile(filename);
if (code) {
evalCode(module, code);
} else {
handleError();
}
}
PythonQtObjectPtr PythonQt::parseFile(const QString& filename)
{
PythonQtObjectPtr p;
p.setNewRef(PythonQtImport::getCodeFromPyc(filename));
if (!p) {
handleError();
}
return p;
}
void PythonQt::addObject(PyObject* module, const QString& name, QObject* object)
{
PyModule_AddObject(module, name.toLatin1().data(), _p->wrapQObject(object));
}
void PythonQt::addVariable(PyObject* module, const QString& name, const QVariant& v)
{
PyModule_AddObject(module, name.toLatin1().data(), PythonQtConv::QVariantToPyObject(v));
}
void PythonQt::removeVariable(PyObject* module, const QString& name)
{
PyObject_DelAttrString(module, name.toLatin1().data());
}
QVariant PythonQt::getVariable(PyObject* module, const QString& objectname)
{
QVariant result;
PythonQtObjectPtr obj = lookupObject(module, objectname);
if (obj) {
result = PythonQtConv::PyObjToQVariant(obj);
}
return result;
}
QStringList PythonQt::introspection(PyObject* module, const QString& objectname, PythonQt::ObjectType type)
{
QStringList results;
PythonQtObjectPtr object;
if (objectname.isEmpty()) {
object = module;
} else {
object = lookupObject(module, objectname);
if (!object && type == CallOverloads) {
PyObject* dict = lookupObject(module, "__builtins__");
if (dict) {
object = PyDict_GetItemString(dict, objectname.toLatin1().constData());
}
}
}
if (object) {
if (type == CallOverloads) {
if (PythonQtSlotFunction_Check(object)) {
PythonQtSlotFunctionObject* o = (PythonQtSlotFunctionObject*)object.object();
PythonQtSlotInfo* info = o->m_ml;
while (info) {
results << info->fullSignature(info->isInstanceDecorator() || o->m_self->ob_type == &PythonQtVariantWrapper_Type);
info = info->nextInfo();
}
} else if (object->ob_type == &PythonQtMetaObjectWrapper_Type) {
PythonQtMetaObjectWrapper* o = (PythonQtMetaObjectWrapper*)object.object();
PythonQtSlotInfo* info = o->_info->constructors();
while (info) {
results << info->fullSignature(false);
info = info->nextInfo();
}
} else {
//TODO: use pydoc!
PyObject* doc = PyObject_GetAttrString(object, "__doc__");
if (doc) {
results << PyString_AsString(doc);
Py_DECREF(doc);
}
}
} else {
PyObject* keys = PyObject_Dir(object);
if (keys) {
int count = PyList_Size(keys);
PyObject* key;
PyObject* value;
QString keystr;
for (int i = 0;i<count;i++) {
key = PyList_GetItem(keys,i);
value = PyObject_GetAttr(object, key);
if (!value) continue;
keystr = PyString_AsString(key);
static const QString underscoreStr("__tmp");
if (!keystr.startsWith(underscoreStr)) {
switch (type) {
case Anything:
results << keystr;
break;
case Class:
if (value->ob_type == &PyClass_Type) {
results << keystr;
}
break;
case Variable:
if (value->ob_type != &PyClass_Type
&& value->ob_type != &PyCFunction_Type
&& value->ob_type != &PyFunction_Type
&& value->ob_type != &PyModule_Type
) {
results << keystr;
}
break;
case Function:
if (value->ob_type == &PyFunction_Type ||
value->ob_type == &PyMethod_Type
) {
results << keystr;
}
break;
case Module:
if (value->ob_type == &PyModule_Type) {
results << keystr;
}
break;
default:
std::cerr << "PythonQt: introspection: unknown case" << ", in " << __FILE__ << ":" << __LINE__ << std::endl;
}
}
Py_DECREF(value);
}
Py_DECREF(keys);
}
}
}
return results;
}
QVariant PythonQt::call(PyObject* module, const QString& name, const QVariantList& args)
{
QVariant r;
PythonQtObjectPtr callable = lookupCallable(module, name);
if (callable) {
PythonQtObjectPtr pargs;
int count = args.size();
if (count>0) {
pargs.setNewRef(PyTuple_New(count));
}
bool err = false;
// transform QVariants to Python
for (int i = 0; i < count; i++) {
PyObject* arg = PythonQtConv::QVariantToPyObject(args.at(i));
if (arg) {
// steals reference, no unref
PyTuple_SetItem(pargs, i,arg);
} else {
err = true;
break;
}
}
if (!err) {
PyErr_Clear();
PythonQtObjectPtr result;
result.setNewRef(PyObject_CallObject(callable, pargs));
if (result) {
// ok
r = PythonQtConv::PyObjToQVariant(result);
} else {
PythonQt::self()->handleError();
}
}
}
return r;
}
void PythonQt::addInstanceDecorators(QObject* o)
{
_p->addDecorators(o, true, false);
}
void PythonQt::addClassDecorators(QObject* o)
{
_p->addDecorators(o, false, true);
}
void PythonQt::addDecorators(QObject* o)
{
_p->addDecorators(o, true, true);
}
void PythonQt::registerQObjectClassNames(const QStringList& names)
{
_p->registerQObjectClassNames(names);
}
void PythonQt::setImporter(PythonQtImportFileInterface* importInterface)
{
static bool first = true;
if (first) {
first = false;
_p->_importInterface = importInterface;
PythonQtImport::init();
}
}
void PythonQt::setImporterIgnorePaths(const QStringList& paths)
{
_p->_importIgnorePaths = paths;
}
const QStringList& PythonQt::getImporterIgnorePaths()
{
return _p->_importIgnorePaths;
}
void PythonQt::addWrapperFactory(PythonQtCppWrapperFactory* factory)
{
_p->_cppWrapperFactories.append(factory);
}
void PythonQt::addConstructorHandler(PythonQtConstructorHandler* factory)
{
_p->_constructorHandlers.append(factory);
}
const QList<PythonQtConstructorHandler*>& PythonQt::constructorHandlers()
{
return _p->_constructorHandlers;
};
//---------------------------------------------------------------------------------------------------
PythonQtPrivate::PythonQtPrivate()
{
_importInterface = NULL;
}
void PythonQtPrivate::addDecorators(QObject* o, bool instanceDeco, bool classDeco)
{
o->setParent(this);
int numMethods = o->metaObject()->methodCount();
for (int i = 0; i < numMethods; i++) {
QMetaMethod m = o->metaObject()->method(i);
if ((m.methodType() == QMetaMethod::Method ||
m.methodType() == QMetaMethod::Slot) && m.access() == QMetaMethod::Public) {
const PythonQtMethodInfo* info = PythonQtMethodInfo::getCachedMethodInfo(m);
if (qstrncmp(m.signature(), "new_", 4)==0) {
if (!classDeco) continue;
// either it returns a * or a QVariant and the name starts with "new_"
bool isVariantReturn = info->parameters().at(0).typeId == PythonQtMethodInfo::Variant;
if ((info->parameters().at(0).isPointer || isVariantReturn)) {
QByteArray signature = m.signature();
QByteArray nameOfClass = signature.mid(4, signature.indexOf('(')-4);
PythonQtSlotInfo* prev = _constructorSlots.value(nameOfClass);
PythonQtSlotInfo* newSlot = new PythonQtSlotInfo(m, i, o, PythonQtSlotInfo::ClassDecorator);
if (prev) {
newSlot->setNextInfo(prev->nextInfo());
prev->setNextInfo(newSlot);
} else {
_constructorSlots.insert(nameOfClass, newSlot);
}
}
} else if (qstrncmp(m.signature(), "delete_", 7)==0) {
if (!classDeco) continue;
QByteArray signature = m.signature();
QByteArray nameOfClass = signature.mid(7, signature.indexOf('(')-7);
PythonQtSlotInfo* newSlot = new PythonQtSlotInfo(m, i, o, PythonQtSlotInfo::ClassDecorator);
_destructorSlots.insert(nameOfClass, newSlot);
} else if (qstrncmp(m.signature(), "static_", 7)==0) {
if (!classDeco) continue;
QByteArray signature = m.signature();
QByteArray nameOfClass = signature.mid(signature.indexOf('_')+1);
nameOfClass = nameOfClass.mid(0, nameOfClass.indexOf('_'));
PythonQtSlotInfo* slotCopy = new PythonQtSlotInfo(m, i, o, PythonQtSlotInfo::ClassDecorator);
_knownQtDecoratorSlots.insert(nameOfClass, slotCopy);
} else {
if (!instanceDeco) continue;
if (info->parameters().count()>1) {
PythonQtMethodInfo::ParameterInfo p = info->parameters().at(1);
if (p.isPointer) {
PythonQtSlotInfo* slotCopy = new PythonQtSlotInfo(m, i, o, PythonQtSlotInfo::InstanceDecorator);
_knownQtDecoratorSlots.insert(p.name, slotCopy);
}
}
}
}
}
}
void PythonQtPrivate::registerQObjectClassNames(const QStringList& names)
{
foreach(QString name, names) {
_knownQObjectClassNames.insert(name.toLatin1(), true);
}
}
QList<PythonQtSlotInfo*> PythonQtPrivate::getDecoratorSlots(const QByteArray& className)
{
return _knownQtDecoratorSlots.values(className);
}
void PythonQtPrivate::wrappedObjectDestroyed(QObject* obj)
{
// mlabDebugConst("MLABPython","PyWrapper QObject destroyed " << o << " " << o->name() << " " << o->className());
PythonQtWrapper* wrap = _wrappedObjects[obj];
if (wrap) {
_wrappedObjects.remove(obj);
// remove the pointer but keep the wrapper alive in python
wrap->_obj = NULL;
}
}
void PythonQtPrivate::destroyedSignalEmitter(QObject* obj)
{
_signalReceivers.take(obj);
}
bool PythonQt::handleError()
{
bool flag = false;
if (PyErr_Occurred()) {
// currently we just print the error and the stderr handler parses the errors
PyErr_Print();
/*
// EXTRA: the format of the ptype and ptraceback is not really documented, so I use PyErr_Print() above
PyObject *ptype;
PyObject *pvalue;
PyObject *ptraceback;
PyErr_Fetch( &ptype, &pvalue, &ptraceback);
Py_XDECREF(ptype);
Py_XDECREF(pvalue);
Py_XDECREF(ptraceback);
*/
PyErr_Clear();
flag = true;
}
return flag;
}
void PythonQt::overwriteSysPath(const QStringList& paths)
{
PythonQtObjectPtr sys;
sys.setNewRef(PyImport_ImportModule("sys"));
PyModule_AddObject(sys, "path", PythonQtConv::QStringListToPyList(paths));
}
void PythonQt::setModuleImportPath(PyObject* module, const QStringList& paths)
{
PyModule_AddObject(module, "__path__", PythonQtConv::QStringListToPyList(paths));
}
void PythonQt::stdOutRedirectCB(const QString& str)
{
emit PythonQt::self()->pythonStdOut(str);
}
void PythonQt::stdErrRedirectCB(const QString& str)
{
emit PythonQt::self()->pythonStdErr(str);
}
static PyMethodDef PythonQtMethods[] = {
{NULL, NULL, 0, NULL}
};
void PythonQt::initPythonQtModule(bool redirectStdOut)
{
_p->_pythonQtModule.setNewRef(Py_InitModule("PythonQt", PythonQtMethods));
_p->_qtNamespace = new PythonQtClassInfo(&staticQtMetaObject);
PyModule_AddObject(_p->_pythonQtModule, "Qt", (PyObject*)_p->createNewPythonQtMetaObjectWrapper(_p->_qtNamespace));
if (redirectStdOut) {
PythonQtObjectPtr sys;
PythonQtObjectPtr out;
PythonQtObjectPtr err;
sys.setNewRef(PyImport_ImportModule("sys"));
// create a redirection object for stdout and stderr
out = PythonQtStdOutRedirectType.tp_new(&PythonQtStdOutRedirectType,NULL, NULL);
((PythonQtStdOutRedirect*)out.object())->_cb = stdOutRedirectCB;
err = PythonQtStdOutRedirectType.tp_new(&PythonQtStdOutRedirectType,NULL, NULL);
((PythonQtStdOutRedirect*)err.object())->_cb = stdErrRedirectCB;
// replace the built in file objects with our own objects
PyModule_AddObject(sys, "stdout", out);
PyModule_AddObject(sys, "stderr", err);
}
}
void PythonQt::addVariantWrapper(const char* typeName, QObject* wrapper)
{
_p->addVariantWrapper(typeName, wrapper);
}
void PythonQtPrivate::addVariantWrapper(const char* typeName, QObject* wrapper)
{
int type = QMetaType::type(typeName);
PythonQtClassInfo* info = new PythonQtClassInfo(wrapper->metaObject(), typeName);
_knownVariantWrappers.insert(type, qMakePair(info, wrapper));
addDecorators(wrapper, false, true);
PyModule_AddObject(_pythonQtModule, typeName, (PyObject*)createNewPythonQtMetaObjectWrapper(info));
}
PyObject* PythonQt::helpCalled(PythonQtClassInfo* info)
{
if (_p->_initFlags & ExternalHelp) {
emit pythonHelpRequest(QByteArray(info->className()));
return Py_BuildValue("");
} else {
return PyString_FromString(info->help().toLatin1().data());
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.