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

« back to all changes in this revision

Viewing changes to src/App/PropertyContainerPyImp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
Import upstream version 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) J�rgen Riegel          (juergen.riegel@web.de) 2007     *
 
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
# include <sstream>
 
28
#endif
 
29
 
 
30
#include "PropertyContainer.h"
 
31
#include "Property.h"
 
32
 
 
33
// inclution of the generated files (generated out of PropertyContainerPy.xml)
 
34
#include "PropertyContainerPy.h"
 
35
#include "PropertyContainerPy.cpp"
 
36
 
 
37
using namespace App;
 
38
 
 
39
// returns a string which represent the object e.g. when printed in python
 
40
const char *PropertyContainerPy::representation(void) const
 
41
{
 
42
    return "<property container>";
 
43
}
 
44
 
 
45
PyObject*  PropertyContainerPy::getPropertyByName(PyObject *args)
 
46
{
 
47
    char *pstr;
 
48
    if (!PyArg_ParseTuple(args, "s", &pstr))     // convert args: Python->C
 
49
        return NULL;                             // NULL triggers exception
 
50
    App::Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr);
 
51
    if (prop) {
 
52
        return prop->getPyObject();
 
53
    }
 
54
    else {
 
55
        PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr);
 
56
        return NULL;
 
57
    }
 
58
}
 
59
 
 
60
PyObject*  PropertyContainerPy::getTypeOfProperty(PyObject *args)
 
61
{
 
62
    Py::List ret;
 
63
    char *pstr;
 
64
    if (!PyArg_ParseTuple(args, "s", &pstr))     // convert args: Python->C
 
65
        return NULL;                             // NULL triggers exception
 
66
 
 
67
    short Type =  getPropertyContainerPtr()->getPropertyType(pstr);
 
68
 
 
69
    if (Type & Prop_Hidden)
 
70
        ret.append(Py::String("Hidden"));
 
71
    if (Type & Prop_ReadOnly)
 
72
        ret.append(Py::String("ReadOnly"));
 
73
    if (Type & Prop_Output)
 
74
        ret.append(Py::String("Output"));
 
75
    if (Type & Prop_Transient)
 
76
        ret.append(Py::String("Transient"));
 
77
 
 
78
    return Py::new_reference_to(ret);
 
79
}
 
80
 
 
81
PyObject*  PropertyContainerPy::getGroupOfProperty(PyObject *args)
 
82
{
 
83
    char *pstr;
 
84
    if (!PyArg_ParseTuple(args, "s", &pstr))     // convert args: Python->C
 
85
        return NULL;                             // NULL triggers exception
 
86
 
 
87
    const char* Group = getPropertyContainerPtr()->getPropertyGroup(pstr);
 
88
    if (Group)
 
89
        return Py::new_reference_to(Py::String(Group));
 
90
    else
 
91
        return Py::new_reference_to(Py::String(""));
 
92
}
 
93
 
 
94
PyObject*  PropertyContainerPy::getDocumentationOfProperty(PyObject *args)
 
95
{
 
96
    char *pstr;
 
97
    if (!PyArg_ParseTuple(args, "s", &pstr))     // convert args: Python->C
 
98
        return NULL;                             // NULL triggers exception
 
99
 
 
100
    const char* Group = getPropertyContainerPtr()->getPropertyDocumentation(pstr);
 
101
    if (Group)
 
102
        return Py::new_reference_to(Py::String(Group));
 
103
    else
 
104
        return Py::new_reference_to(Py::String(""));
 
105
}
 
106
 
 
107
Py::List PropertyContainerPy::getPropertiesList(void) const
 
108
{
 
109
    Py::List ret;
 
110
    std::map<std::string,Property*> Map;
 
111
 
 
112
    getPropertyContainerPtr()->getPropertyMap(Map);
 
113
 
 
114
    for (std::map<std::string,Property*>::const_iterator It=Map.begin();It!=Map.end();++It)
 
115
        ret.append(Py::String(It->first));
 
116
 
 
117
    return ret;
 
118
}
 
119
 
 
120
PyObject *PropertyContainerPy::getCustomAttributes(const char* attr) const
 
121
{
 
122
    // search in PropertyList
 
123
    Property *prop = getPropertyContainerPtr()->getPropertyByName(attr);
 
124
    if (prop) {
 
125
        PyObject* pyobj = prop->getPyObject();
 
126
        if (!pyobj && PyErr_Occurred()) {
 
127
            // the Python exception is already set
 
128
            throw Py::Exception();
 
129
        }
 
130
        return pyobj;
 
131
    }
 
132
    else if (Base::streq(attr, "__dict__")) {
 
133
        // get the properties to the C++ PropertyContainer class
 
134
        std::map<std::string,App::Property*> Map;
 
135
        getPropertyContainerPtr()->getPropertyMap(Map);
 
136
        PyObject *dict = PyDict_New();
 
137
        if (dict) {
 
138
            for ( std::map<std::string,App::Property*>::iterator it = Map.begin(); it != Map.end(); ++it )
 
139
                PyDict_SetItem(dict, PyString_FromString(it->first.c_str()), PyString_FromString(""));
 
140
            if (PyErr_Occurred()) {
 
141
                Py_DECREF(dict);
 
142
                dict = NULL;
 
143
            }
 
144
        }
 
145
        return dict;
 
146
    }
 
147
 
 
148
    return 0;
 
149
}
 
150
 
 
151
int PropertyContainerPy::setCustomAttributes(const char* attr, PyObject *obj)
 
152
{
 
153
    // search in PropertyList
 
154
    Property *prop = getPropertyContainerPtr()->getPropertyByName(attr);
 
155
    if (prop) {
 
156
        // Read-only attributes must not be set over its Python interface
 
157
        short Type =  getPropertyContainerPtr()->getPropertyType(prop);
 
158
        if (Type & Prop_ReadOnly) {
 
159
            std::stringstream s;
 
160
            s << "Object attribute '" << attr << "' is read-only";
 
161
            throw Py::AttributeError(s.str());
 
162
        }
 
163
 
 
164
        prop->setPyObject(obj);
 
165
        return 1;
 
166
    }
 
167
 
 
168
    return 0;
 
169
}