~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/App/DocumentObserverPython.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam C. Powell, IV
  • Date: 2010-01-11 08:48:33 UTC
  • mfrom: (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100111084833-4g9vgdqbkw8u34zb
Tags: 0.9.2646.5-1
* New upstream version (closes: #561696).
* Added swig to Build-Depends (closes: #563523, #563772).
* Removed python-opencv from Build-Depends and Recommends (closes: #560768).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net>     *
 
3
 *                                                                         *
 
4
 *   This file is part of the FreeCAD CAx development system.              *
 
5
 *                                                                         *
 
6
 *   This library is free software; you can redistribute it and/or         *
 
7
 *   modify it under the terms of the GNU Library General Public           *
 
8
 *   License as published by the Free Software Foundation; either          *
 
9
 *   version 2 of the License, or (at your option) any later version.      *
 
10
 *                                                                         *
 
11
 *   This library  is distributed in the hope that it will be useful,      *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU Library General Public License for more details.                  *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU Library General Public     *
 
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
 
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 
20
 *                                                                         *
 
21
 ***************************************************************************/
 
22
 
 
23
 
 
24
#include "PreCompiled.h"
 
25
 
 
26
#ifndef _PreComp_
 
27
#endif
 
28
 
 
29
#include "Application.h"
 
30
#include "Document.h"
 
31
#include "DocumentObject.h"
 
32
#include "DocumentObserverPython.h"
 
33
#include <Base/Interpreter.h>
 
34
#include <Base/Console.h>
 
35
 
 
36
using namespace App;
 
37
 
 
38
std::vector<DocumentObserverPython*> DocumentObserverPython::_instances;
 
39
 
 
40
void DocumentObserverPython::addObserver(const Py::Object& obj)
 
41
{
 
42
    _instances.push_back(new DocumentObserverPython(obj));
 
43
}
 
44
 
 
45
void DocumentObserverPython::removeObserver(const Py::Object& obj)
 
46
{
 
47
    DocumentObserverPython* obs=0;
 
48
    for (std::vector<DocumentObserverPython*>::iterator it =
 
49
        _instances.begin(); it != _instances.end(); ++it) {
 
50
        if ((*it)->inst == obj) {
 
51
            obs = *it;
 
52
            _instances.erase(it);
 
53
            break;
 
54
        }
 
55
    }
 
56
 
 
57
    delete obs;
 
58
}
 
59
 
 
60
DocumentObserverPython::DocumentObserverPython(const Py::Object& obj) : inst(obj)
 
61
{
 
62
    this->connectApplicationCreatedDocument = App::GetApplication().signalNewDocument.connect(boost::bind
 
63
        (&DocumentObserverPython::slotCreatedDocument, this, _1));
 
64
    this->connectApplicationDeletedDocument = App::GetApplication().signalDeleteDocument.connect(boost::bind
 
65
        (&DocumentObserverPython::slotDeletedDocument, this, _1));
 
66
    this->connectApplicationRelabelDocument = App::GetApplication().signalRelabelDocument.connect(boost::bind
 
67
        (&DocumentObserverPython::slotRelabelDocument, this, _1));
 
68
    this->connectApplicationActivateDocument = App::GetApplication().signalActiveDocument.connect(boost::bind
 
69
        (&DocumentObserverPython::slotActivateDocument, this, _1));
 
70
 
 
71
    this->connectDocumentCreatedObject = App::GetApplication().signalNewObject.connect(boost::bind
 
72
        (&DocumentObserverPython::slotCreatedObject, this, _1));
 
73
    this->connectDocumentDeletedObject = App::GetApplication().signalDeletedObject.connect(boost::bind
 
74
        (&DocumentObserverPython::slotDeletedObject, this, _1));
 
75
    this->connectDocumentChangedObject = App::GetApplication().signalChangedObject.connect(boost::bind
 
76
        (&DocumentObserverPython::slotChangedObject, this, _1, _2));
 
77
}
 
78
 
 
79
DocumentObserverPython::~DocumentObserverPython()
 
80
{
 
81
    this->connectApplicationCreatedDocument.disconnect();
 
82
    this->connectApplicationDeletedDocument.disconnect();
 
83
    this->connectApplicationRelabelDocument.disconnect();
 
84
    this->connectApplicationActivateDocument.disconnect();
 
85
 
 
86
    this->connectDocumentCreatedObject.disconnect();
 
87
    this->connectDocumentDeletedObject.disconnect();
 
88
    this->connectDocumentChangedObject.disconnect();
 
89
}
 
90
 
 
91
void DocumentObserverPython::slotCreatedDocument(const App::Document& Doc)
 
92
{
 
93
    Base::PyGILStateLocker lock;
 
94
    try {
 
95
        if (this->inst.hasAttr(std::string("slotCreatedDocument"))) {
 
96
            Py::Callable method(this->inst.getAttr(std::string("slotCreatedDocument")));
 
97
            Py::Tuple args(1);
 
98
            args.setItem(0, Py::Object(const_cast<App::Document&>(Doc).getPyObject(), true));
 
99
            method.apply(args);
 
100
        }
 
101
    }
 
102
    catch (Py::Exception&) {
 
103
        Base::PyException e; // extract the Python error text
 
104
        Base::Console().Error("%s\n", e.what());
 
105
    }
 
106
}
 
107
 
 
108
void DocumentObserverPython::slotDeletedDocument(const App::Document& Doc)
 
109
{
 
110
    Base::PyGILStateLocker lock;
 
111
    try {
 
112
        if (this->inst.hasAttr(std::string("slotDeletedDocument"))) {
 
113
            Py::Callable method(this->inst.getAttr(std::string("slotDeletedDocument")));
 
114
            Py::Tuple args(1);
 
115
            args.setItem(0, Py::Object(const_cast<App::Document&>(Doc).getPyObject(), true));
 
116
            method.apply(args);
 
117
        }
 
118
    }
 
119
    catch (Py::Exception&) {
 
120
        Base::PyException e; // extract the Python error text
 
121
        Base::Console().Error("%s\n", e.what());
 
122
    }
 
123
}
 
124
 
 
125
void DocumentObserverPython::slotRelabelDocument(const App::Document& Doc)
 
126
{
 
127
    Base::PyGILStateLocker lock;
 
128
    try {
 
129
        if (this->inst.hasAttr(std::string("slotRelabelDocument"))) {
 
130
            Py::Callable method(this->inst.getAttr(std::string("slotRelabelDocument")));
 
131
            Py::Tuple args(1);
 
132
            args.setItem(0, Py::Object(const_cast<App::Document&>(Doc).getPyObject(), true));
 
133
            method.apply(args);
 
134
        }
 
135
    }
 
136
    catch (Py::Exception&) {
 
137
        Base::PyException e; // extract the Python error text
 
138
        Base::Console().Error("%s\n", e.what());
 
139
    }
 
140
}
 
141
 
 
142
void DocumentObserverPython::slotActivateDocument(const App::Document& Doc)
 
143
{
 
144
    Base::PyGILStateLocker lock;
 
145
    try {
 
146
        if (this->inst.hasAttr(std::string("slotActivateDocument"))) {
 
147
            Py::Callable method(this->inst.getAttr(std::string("slotActivateDocument")));
 
148
            Py::Tuple args(1);
 
149
            args.setItem(0, Py::Object(const_cast<App::Document&>(Doc).getPyObject(), true));
 
150
            method.apply(args);
 
151
        }
 
152
    }
 
153
    catch (Py::Exception&) {
 
154
        Base::PyException e; // extract the Python error text
 
155
        Base::Console().Error("%s\n", e.what());
 
156
    }
 
157
}
 
158
 
 
159
void DocumentObserverPython::slotCreatedObject(const App::DocumentObject& Obj)
 
160
{
 
161
    Base::PyGILStateLocker lock;
 
162
    try {
 
163
        if (this->inst.hasAttr(std::string("slotCreatedObject"))) {
 
164
            Py::Callable method(this->inst.getAttr(std::string("slotCreatedObject")));
 
165
            Py::Tuple args(1);
 
166
            args.setItem(0, Py::Object(const_cast<App::DocumentObject&>(Obj).getPyObject(), true));
 
167
            method.apply(args);
 
168
        }
 
169
    }
 
170
    catch (Py::Exception&) {
 
171
        Base::PyException e; // extract the Python error text
 
172
        Base::Console().Error("%s\n", e.what());
 
173
    }
 
174
}
 
175
 
 
176
void DocumentObserverPython::slotDeletedObject(const App::DocumentObject& Obj)
 
177
{
 
178
    Base::PyGILStateLocker lock;
 
179
    try {
 
180
        if (this->inst.hasAttr(std::string("slotDeletedObject"))) {
 
181
            Py::Callable method(this->inst.getAttr(std::string("slotDeletedObject")));
 
182
            Py::Tuple args(1);
 
183
            args.setItem(0, Py::Object(const_cast<App::DocumentObject&>(Obj).getPyObject(), true));
 
184
            method.apply(args);
 
185
        }
 
186
    }
 
187
    catch (Py::Exception&) {
 
188
        Base::PyException e; // extract the Python error text
 
189
        Base::Console().Error("%s\n", e.what());
 
190
    }
 
191
}
 
192
 
 
193
void DocumentObserverPython::slotChangedObject(const App::DocumentObject& Obj,
 
194
                                               const App::Property& Prop)
 
195
{
 
196
    Base::PyGILStateLocker lock;
 
197
    try {
 
198
        if (this->inst.hasAttr(std::string("slotChangedObject"))) {
 
199
            Py::Callable method(this->inst.getAttr(std::string("slotChangedObject")));
 
200
            Py::Tuple args(2);
 
201
            args.setItem(0, Py::Object(const_cast<App::DocumentObject&>(Obj).getPyObject(), true));
 
202
            std::string prop_name = Obj.getName(&Prop);
 
203
            args.setItem(1, Py::String(prop_name));
 
204
            method.apply(args);
 
205
        }
 
206
    }
 
207
    catch (Py::Exception&) {
 
208
        Base::PyException e; // extract the Python error text
 
209
        Base::Console().Error("%s\n", e.what());
 
210
    }
 
211
}