summaryrefslogtreecommitdiffstats
path: root/libpyside/destroylistener.cpp
blob: 600d922c332a3c1d6daa001a3d04821b6b40b664 (plain)
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
#include "destroylistener.h"

#include <Python.h>
#include <QObject>
#include <shiboken.h>
#include <QDebug>
#include <QMutex>

PySide::DestroyListener* PySide::DestroyListener::m_instance = 0;

namespace PySide
{

struct DestroyListenerPrivate
{
    static bool m_destroyed;
};


DestroyListener* DestroyListener::instance()
{
    if (!m_instance)
        m_instance = new DestroyListener(0);
    return m_instance;
}

void DestroyListener::destroy()
{
    if (m_instance) {
        m_instance->disconnect();
        delete m_instance;
        m_instance = 0;
    }
}

void DestroyListener::listen(QObject *obj)
{
    SbkObject* wrapper = Shiboken::BindingManager::instance().retrieveWrapper(obj);
    if (!wrapper) // avoid problem with multiple inheritance
        return;

    if (Py_IsInitialized() == 0)
        onObjectDestroyed(obj);
    else
        QObject::connect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(onObjectDestroyed(QObject*)), Qt::DirectConnection);
}

void DestroyListener::onObjectDestroyed(QObject* obj)
{
    SbkObject* wrapper = Shiboken::BindingManager::instance().retrieveWrapper(obj);
    if (wrapper) //make sure the object exists before destroy
        Shiboken::Object::destroy(wrapper, obj);
}

DestroyListener::DestroyListener(QObject *parent)
    : QObject(parent)
{
    m_d = new DestroyListenerPrivate();
}

DestroyListener::~DestroyListener()
{
    delete m_d;
}

}//namespace