Back to index

scribus-ng  1.3.4.dfsg+svn20071115
Functions
cmdgetsetprop.h File Reference
#include "cmdvar.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

QObject * getQObjectFromPyArg (PyObject *arg)
 Return a pointer to the QObject that the PyObject* args refers to.
PyObject * wrapQObject (QObject *obj)
 Return a PyCObject containing the passed QObject pointer.
PyObject * convert_QStringList_to_PyListObject (QStrList origlist)
 Convert a QStringList to a Python list of str objects.
PyObject * convert_QObjectList_to_PyListObject (QObjectList *origlist)
 Convert a QObjectList to a Python list of PyCObjects.
const char * getpropertytype (QObject *obj, const char *propname, bool includesuper=true)
 return the name of the C type of the specified property
 PyDoc_STRVAR (scribus_propertyctype__doc__, QT_TR_NOOP("getPropertyCType(object, property, includesuper=True)\n\ \n\ Returns the name of the C type of `property' of `object'. See getProperty()\n\ for details of arguments.\n\ \n\ If `includesuper' is true, search inherited properties too.\n\ "))
 Get name of C type of property of object.
PyObject * scribus_propertyctype (PyObject *, PyObject *args, PyObject *kw)
 PyDoc_STRVAR (scribus_getpropertynames__doc__, QT_TR_NOOP("getPropertyNames(object, includesuper=True)\n\ \n\ Return a list of property names supported by `object'.\n\ If `includesuper' is true, return properties supported\n\ by parent classes as well.\n\ "))
 Return a list of the property names of the passed object.
PyObject * scribus_getpropertynames (PyObject *self, PyObject *args, PyObject *kw)
 PyDoc_STRVAR (scribus_getproperty__doc__, QT_TR_NOOP("getProperty(object, property)\n\ \n\ Return the value of the property `property' of the passed `object'.\n\ \n\ The `object' argument may be a string, in which case the named PageItem\n\ is searched for. It may also be a PyCObject, which may point to any\n\ C++ QObject instance.\n\ \n\ The `property' argument must be a string, and is the name of the property\n\ to look up on `object'.\n\ \n\ The return value varies depending on the type of the property.\n\ "))
 Generic Python getter for Qt object properties.
PyObject * scribus_getproperty (PyObject *, PyObject *args, PyObject *kw)
 PyDoc_STRVAR (scribus_setproperty__doc__, QT_TR_NOOP("setProperty(object, property, value)\n\ \n\ Set `property' of `object' to `value'. If `value' cannot be converted to a type\n\ compatible with the type of `property', an exception is raised. An exception may\n\ also be raised if the underlying setter fails.\n\ \n\ See getProperty() for more information.\n\ "))
 Generic setter for Qt object properties.
PyObject * scribus_setproperty (PyObject *, PyObject *args, PyObject *kw)
 PyDoc_STRVAR (scribus_getchildren__doc__, QT_TR_NOOP("getChildren(object, ofclass=None, ofname=None, regexpmatch=False, recursive=True)\n\ \n\ Return a list of children of `object', possibly restricted to children\n\ of class named `ofclass' or children named `ofname'. If `recursive' is true,\n\ search recursively through children, grandchildren, etc.\n\ \n\ See QObject::children() in the Qt docs for more information.\n\ "))
 Return a list of children of the passed object.
PyObject * scribus_getchildren (PyObject *self, PyObject *args, PyObject *kw)
 PyDoc_STRVAR (scribus_getchild__doc__, QT_TR_NOOP("getChild(object, childname, ofclass=None, recursive=True)\n\ \n\ Return the first child of `object' named `childname', possibly restricting\n\ the search to children of type name `ofclass'. If `recursive' is true,\n\ search recursively through children, grandchildren, etc.\n\ "))
 Return a wrapped PyObject* for the named child, if found.
PyObject * scribus_getchild (PyObject *self, PyObject *args, PyObject *kw)

Function Documentation

PyObject* convert_QObjectList_to_PyListObject ( QObjectList *  origlist)

Convert a QObjectList to a Python list of PyCObjects.

Attention:
may return NULL with an exception set

Definition at line 127 of file cmdgetsetprop.cpp.

{
       PyObject* resultList = PyList_New(0);
       if (!resultList)
              return NULL;

       PyObject* objPtr = NULL;
       // Loop over the objects in the list and add them to the python
       // list wrapped in PyCObjects .
       for ( origlist->first(); origlist->current(); origlist->next() )
       {
              // Wrap up the object pointer
              objPtr = wrapQObject(origlist->current());
              if (!objPtr)
              {
                     // Failed to wrap the object. An exception is already set.
                     Py_DECREF(resultList);
                     return NULL;
              }
              // and add it to the list
              if (PyList_Append(resultList, (PyObject*)objPtr) == -1)
                     return NULL;
       }
       return resultList;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PyObject* convert_QStringList_to_PyListObject ( QStrList  origlist)

Convert a QStringList to a Python list of str objects.

Attention:
may return NULL with an exception set
const char* getpropertytype ( QObject *  obj,
const char *  propname,
bool  includesuper = true 
)

return the name of the C type of the specified property

Look up `propname' on `obj', searching through parent classes if `includsuper' is true. Get and return the name of the C type of the property.

If the property cannot be found, a null string is returned. NO EXCEPTION IS SET.

See also:
scribus_propertyctype()

Definition at line 50 of file cmdgetsetprop.cpp.

{
       QMetaObject* objmeta = obj->metaObject();
       int i = objmeta->findProperty(propname, includesuper);
       if (i == -1)
              return NULL;
       const QMetaProperty* propmeta = objmeta->property(i, includesuper);
       if (propmeta == NULL)
              return NULL;
       const char* type = propmeta->type();
       assert(type);
       return type;
}

Here is the caller graph for this function:

QObject* getQObjectFromPyArg ( PyObject *  arg)

Return a pointer to the QObject that the PyObject* args refers to.

This function looks up the PyObject* argument and tries to return a corresponding QObject.

If the argument is a PyCObject, it will return the contained pointer. If the pointer is NULL an exception is set before returning.

If the argument is a string, a PageItem by that name will be looked for and, if found, returned. If no such object could be found, an exception is set and NULL is returned.

If any other type is specified, an exception is set and NULL is returned.

Attention:
may return NULL with an exception set
See also:
wrapQObject()

Definition at line 14 of file cmdgetsetprop.cpp.

{
       if (PyString_Check(arg))
              // It's a string. Look for a pageItem by that name. Do NOT accept a
              // selection.
              return getPageItemByName(QString::fromUtf8(PyString_AsString(arg)));
       else if (PyCObject_Check(arg))
       {
              // It's a PyCObject, ie a wrapped pointer. Check it's not NULL
              // and return it.
              // FIXME: Try to check that its a pointer to a QObject instance
              QObject* tempObject = (QObject*)PyCObject_AsVoidPtr(arg);
              if (!tempObject)
              {
                     PyErr_SetString(PyExc_TypeError, "INTERNAL: Passed NULL PyCObject");
                     return NULL;
              }
              else
                     return tempObject;
       }
       else
       {
              // It's not a type we know what to do with
              PyErr_SetString(PyExc_TypeError,
                     QObject::tr("Argument must be page item name, or PyCObject instance"));
              return NULL;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

PyDoc_STRVAR ( scribus_propertyctype__doc__  ,
QT_TR_NOOP("getPropertyCType(object, property, includesuper=True)\n\\n\Returns the name of the C type of `property' of `object'. See getProperty()\n\for details of arguments.\n\\n\If `includesuper' is true, search inherited properties too.\n\")   
)

Get name of C type of property of object.

Returns:
Python string object containing name of C type of property.
See also:
getpropertytype(), scribus_getproperty(), scribus_setproperty()
PyDoc_STRVAR ( scribus_getpropertynames__doc__  ,
QT_TR_NOOP("getPropertyNames(object, includesuper=True)\n\\n\Return a list of property names supported by `object'.\n\If `includesuper' is true, return properties supported\n\by parent classes as well.\n\")   
)

Return a list of the property names of the passed object.

See the Python docstring for details on this function.

See also:
QMetaObject::propertyNames(), scribus_getproperty(), scribus_setproperty()
PyDoc_STRVAR ( scribus_getproperty__doc__  ,
QT_TR_NOOP("getProperty(object, property)\n\\n\Return the value of the property `property' of the passed `object'.\n\\n\The `object' argument may be a string, in which case the named PageItem\n\is searched for. It may also be a PyCObject, which may point to any\n\C++ QObject instance.\n\\n\The `property' argument must be a string, and is the name of the property\n\to look up on `object'.\n\\n\The return value varies depending on the type of the property.\n\")   
)

Generic Python getter for Qt object properties.

This getter uses the Qt property interface (see the Qt object model documentation) to provide a generic getter for any property of a supported type. It finds the closest Python type match to the property's C++ type, converts the property value to the Python type, and returns it.

If an unsupported type is encountered, TypeError will be raised.

See the Python docstring for details of this function's Python interface.

See also:
scribus_setproperty(), QObject::property(), QMetaObject. scribus_propertyctype()
PyDoc_STRVAR ( scribus_setproperty__doc__  ,
QT_TR_NOOP("setProperty(object, property, value)\n\\n\Set `property' of `object' to `value'. If `value' cannot be converted to a type\n\compatible with the type of `property', an exception is raised. An exception may\n\also be raised if the underlying setter fails.\n\\n\See getProperty() for more information.\n\")   
)

Generic setter for Qt object properties.

This setter uses the Qt property intercace to provide a generic setter for any property of a supported type. It checks the Property's C type and attempts to convert the passed Python object to a compatible type, then call the object's property setter method with the converted value.

If an unsupported type is encountered, TypeError will be raised. If the passed object can't be converted to a compatible type, TypeError will be raised.

See the Python docstring for details of this function's Python interface.

See also:
scribus_getproperty(), QObject::property(), QMetaObject, scribus_propertyctype()
PyDoc_STRVAR ( scribus_getchildren__doc__  ,
QT_TR_NOOP("getChildren(object, ofclass=None, ofname=None, regexpmatch=False, recursive=True)\n\\n\Return a list of children of `object', possibly restricted to children\n\of class named `ofclass' or children named `ofname'. If `recursive' is true,\n\search recursively through children, grandchildren, etc.\n\\n\See QObject::children() in the Qt docs for more information.\n\")   
)

Return a list of children of the passed object.

See the Python docstring for details on this function.

See also:
QObject::children(), scribus_getchild()
PyDoc_STRVAR ( scribus_getchild__doc__  ,
QT_TR_NOOP("getChild(object, childname, ofclass=None, recursive=True)\n\\n\Return the first child of `object' named `childname', possibly restricting\n\the search to children of type name `ofclass'. If `recursive' is true,\n\search recursively through children, grandchildren, etc.\n\")   
)

Return a wrapped PyObject* for the named child, if found.

See the Python docstring for details on this function.

See also:
QObject::child(), scribus_children()
PyObject* scribus_getchild ( PyObject *  self,
PyObject *  args,
PyObject *  kw 
)

Definition at line 189 of file cmdgetsetprop.cpp.

{
       PyObject* objArg = NULL;
       char* childname = NULL;
       char* ofclass = NULL;
       bool recursive = true;
       char* kwnames[] = {const_cast<char*>("object"),
                                      const_cast<char*>("childname"),
                                      const_cast<char*>("ofclass"),
                                      const_cast<char*>("recursive"),
                                      NULL};
       if (!PyArg_ParseTupleAndKeywords(args, kw, "Oes|esi", kwnames,
                            &objArg, "ascii", &childname, "ascii", &ofclass, &recursive))
              return NULL;

       // Get the QObject* the object argument refers to
       QObject* obj = getQObjectFromPyArg(objArg);
       if (!obj)
              return NULL;
       objArg = NULL; // no need to decref, it's borrowed

       // Search for the child, possibly restricting the search to children
       // of a particular type, and possibly recursively searching through
       // grandchildren etc.
       QObject* child = obj->child(childname, ofclass, recursive);
       if (child == NULL)
       {
              PyErr_SetString(PyExc_KeyError, QObject::tr("Child not found"));
              return NULL;
       }

       return wrapQObject(child);
}

Here is the call graph for this function:

PyObject* scribus_getchildren ( PyObject *  self,
PyObject *  args,
PyObject *  kw 
)

Definition at line 155 of file cmdgetsetprop.cpp.

{
       PyObject* objArg = NULL;
       char* ofclass = NULL;
       char* ofname = NULL;
       int recursive = 0;
       int regexpmatch = 0;
       char* kwnames[] = {const_cast<char*>("object"),
                                      const_cast<char*>("ofclass"),
                                      const_cast<char*>("ofname"),
                                      const_cast<char*>("regexpmatch"),
                                      const_cast<char*>("recursive"),
                                      NULL};
       if (!PyArg_ParseTupleAndKeywords(args, kw, "O|esesii", kwnames,
                            &objArg, "ascii", &ofclass, "ascii", &ofname, &regexpmatch, &recursive))
              return NULL;

       // Get the QObject* the object argument refers to
       QObject* obj = getQObjectFromPyArg(objArg);
       if (!obj)
              return NULL;
       objArg = NULL; // no need to decref, it's borrowed

       // Our job is to return a Python list containing the children of this
       // widget (as PyCObjects).
       QObjectList* children = obj->queryList(ofclass, ofname, regexpmatch, recursive);
       PyObject* itemlist = convert_QObjectList_to_PyListObject(children);
       delete children;
       return itemlist;
}

Here is the call graph for this function:

PyObject* scribus_getproperty ( PyObject *  ,
PyObject *  args,
PyObject *  kw 
)

Definition at line 251 of file cmdgetsetprop.cpp.

{
       PyObject* objArg = NULL;
       char* propertyName = NULL;
       char* kwargs[] = {const_cast<char*>("object"),
                                     const_cast<char*>("property"),
                                     NULL};
       if (!PyArg_ParseTupleAndKeywords(args, kw, "Oes", kwargs,
                            &objArg, "ascii", &propertyName))
              return NULL;

       // Get the QObject* the object argument refers to
       QObject* obj = getQObjectFromPyArg(objArg);
       if (!obj)
              return NULL;
       objArg = NULL; // no need to decref, it's borrowed

       // Get the QMetaProperty for the property, so we can check
       // if it's a set/enum and do name/value translation.
       QMetaObject* objmeta = obj->metaObject();
       int i = objmeta->findProperty(propertyName, true);
       if (i == -1)
       {
              PyErr_SetString(PyExc_ValueError,
                            QObject::tr("Property not found"));
              return NULL;
       }
       const QMetaProperty* propmeta = objmeta->property(i, true);
       assert(propmeta);

       // Get the property value as a variant type
       QVariant prop = obj->property(propertyName);

       // Convert the property to an instance of the closest matching Python type.
       PyObject* resultobj = NULL;
       // NUMERIC TYPES
       if (prop.type() == QVariant::Int)
              resultobj = PyLong_FromLong(prop.asInt());
       else if (prop.type() == QVariant::Double)
              resultobj = PyFloat_FromDouble(prop.asDouble());
       // BOOLEAN
       else if (prop.type() == QVariant::Bool)
              resultobj = PyBool_FromLong(prop.asBool());
       // STRING TYPES
       else if (prop.type() == QVariant::CString)
              resultobj = PyString_FromString(prop.asCString().data());
       else if (prop.type() == QVariant::String)
              resultobj = PyString_FromString(prop.asString().utf8().data());
       // HIGHER ORDER TYPES
       else if (prop.type() == QVariant::Point)
       {
              // Return a QPoint as an (x,y) tuple.
              QPoint pt = prop.asPoint();
              return Py_BuildValue("(ii)", pt.x(), pt.y());
       }
       else if (prop.type() == QVariant::Rect)
       {
              // Return a QRect as an (x,y,width,height) tuple.
              // FIXME: We should really construct and return an object that
              // matches the API of QRect and has properties to keep
              // left/top/right/bottom and x/y/width/height in sync.
              QRect r = prop.asRect();
              return Py_BuildValue("(iiii)", r.x(), r.y(), r.width(), r.height());
       }
       else if (prop.type() == QVariant::StringList)
              return convert_QStringList_to_PyListObject(prop.asStringList());
       // UNHANDLED TYPE
       else
       {
              PyErr_SetString(PyExc_TypeError,
                            QObject::tr("Couldn't convert result type '%1'.").arg(prop.typeName()) );
              return NULL;
       }

       // Return the resulting Python object
       if (resultobj == NULL)
       {
              // An exception was set while assigning to resultobj
              assert(PyErr_Occurred());
              return NULL;
       }
       else
              return resultobj;
}

Here is the call graph for this function:

PyObject* scribus_getpropertynames ( PyObject *  self,
PyObject *  args,
PyObject *  kw 
)

Definition at line 224 of file cmdgetsetprop.cpp.

{
       PyObject* objArg = NULL;
       int includesuper = 1;
       char* kwargs[] = {const_cast<char*>("object"),
                                     const_cast<char*>("includesuper"),
                                     NULL};
       if (!PyArg_ParseTupleAndKeywords(args, kw, "O|i", kwargs,
                            &objArg, &includesuper))
              return NULL;

       // Get the QObject* the object argument refers to
       QObject* obj = getQObjectFromPyArg(objArg);
       if (!obj)
              return NULL;
       objArg = NULL; // no need to decref, it's borrowed

       // Retrive the object's meta object so we can query it
       QMetaObject* objmeta = obj->metaObject();
       assert(objmeta);

       // Return the list of properties
       QStrList propertyNames = objmeta->propertyNames(includesuper);
       return convert_QStrList_to_PyListObject(propertyNames);
}

Here is the call graph for this function:

PyObject* scribus_propertyctype ( PyObject *  ,
PyObject *  args,
PyObject *  kw 
)

Definition at line 65 of file cmdgetsetprop.cpp.

{
       PyObject* objArg = NULL;
       char* propertyname = NULL;
       int includesuper = 1;
       char* kwargs[] = {const_cast<char*>("object"),
                                     const_cast<char*>("property"),
                                     const_cast<char*>("includesuper"),
                                     NULL};
       if (!PyArg_ParseTupleAndKeywords(args, kw, "Oes|i", kwargs,
                            &objArg, "ascii", &propertyname, &includesuper))
              return NULL;

       // Get the QObject* the object argument refers to
       QObject* obj = getQObjectFromPyArg(objArg);
       if (!obj)
              return NULL;
       objArg = NULL; // no need to decref, it's borrowed

       // Look up the property and retrive its type information
       const char* type = getpropertytype( (QObject*)obj, propertyname, includesuper);
       if (type == NULL)
       {
              PyErr_SetString(PyExc_KeyError, QObject::tr("Property not found"));
              return NULL;
       }
       return PyString_FromString(type);
}

Here is the call graph for this function:

PyObject* scribus_setproperty ( PyObject *  ,
PyObject *  args,
PyObject *  kw 
)

Definition at line 338 of file cmdgetsetprop.cpp.

{
       PyObject* objArg = NULL;
       char* propertyName = NULL;
       PyObject* objValue = NULL;
       char* kwargs[] = {const_cast<char*>("object"),
                                     const_cast<char*>("property"),
                                     const_cast<char*>("value"),
                                     NULL};
       if (!PyArg_ParseTupleAndKeywords(args, kw, "OesO", kwargs,
                            &objArg, "ascii", &propertyName, &objValue))
              return NULL;

       // We're going to hang on to the value object for a while, so
       // claim a reference to it.
       Py_INCREF(objValue);

       // Get the QObject* the object argument refers to
       QObject* obj = getQObjectFromPyArg(objArg);
       if (!obj)
              return NULL;
       objArg = NULL; // no need to decref, it's borrowed

       const QString propertyType = getpropertytype(obj, propertyName, true);

       // Did we know how to convert the value argument to the right type?
       bool matched = false;
       // Did the set call succceed?
       bool success = false;

       // Check the C++ type of the property, and try to convert the passed
       // PyObject to something sensible looking for it.
       // FIXME: handle enums/sets
       // NUMERIC TYPES
       // These are unfortuately a TOTAL PITA because of the multitude of
       // C and Python numeric types. TODO This needs to be broken out into a subroutine.
       if (propertyType == "bool")
       {
              matched = true;
              if (objValue == Py_False)
                     success = obj->setProperty(propertyName, 0);
              else if (objValue == Py_True)
                     success = obj->setProperty(propertyName, 1);
              else if (PyInt_Check(objValue))
                     success = obj->setProperty(propertyName, PyInt_AsLong(objValue) == 0);
              else if (PyLong_Check(objValue))
                     success = obj->setProperty(propertyName, PyLong_AsLong(objValue) == 0);
              else
                     matched = false;
       }
       else if (propertyType == "int")
       {
              matched = true;
              if (objValue == Py_False)
                     success = obj->setProperty(propertyName, 0);
              else if (objValue == Py_True)
                     success = obj->setProperty(propertyName, 1);
              else if (PyInt_Check(objValue))
                     success = obj->setProperty(propertyName, (int)PyInt_AsLong(objValue));
              else if (PyLong_Check(objValue))
                     success = obj->setProperty(propertyName, (int)PyLong_AsLong(objValue));
              else
                     matched = false;
       }
       else if (propertyType == "double")
       {
              matched = true;
              // FIXME: handle int, long  and bool too
              if (PyFloat_Check(objValue))
                     success = obj->setProperty(propertyName, PyFloat_AsDouble(objValue));
              else
                     matched = false;
       }
       // STRING TYPES
       else if (propertyType == "QString")
       {
              matched = true;
              if (PyString_Check(objValue))
                     success = obj->setProperty(propertyName, QString::fromUtf8(PyString_AsString(objValue)));
              else if (PyUnicode_Check(objValue))
              {
                     // Get a pointer to the internal buffer of the Py_Unicode object, which is UCS2 formatted
                     const unsigned short * ucs2Data = (const unsigned short *)PyUnicode_AS_UNICODE(objValue);
                     // and make a new QString from it (the string is copied)
                     success = obj->setProperty(propertyName, QString::fromUcs2(ucs2Data));
              }
              else
                     matched = false;
       }
       else if (propertyType == "QCString")
       {
              matched = true;
              if (PyString_Check(objValue))
              {
                     // FIXME: should raise an exception instead of mangling the string when
                     // out of charset chars present.
                     QString utfString = QString::fromUtf8(PyString_AsString(objValue));
                     success = obj->setProperty(propertyName, utfString.ascii());
              }
              else if (PyUnicode_Check(objValue))
              {
                     // Get a pointer to the internal buffer of the Py_Unicode object, which is UCS2 formatted
                     const unsigned short * ucs2Data = (const unsigned short *)PyUnicode_AS_UNICODE(objValue);
                     // and make a new QString from it (the string is copied)
                     success = obj->setProperty(propertyName, QString::fromUcs2(ucs2Data).ascii());
              }
              else
                     matched = false;
       }
       // HIGHER ORDER TYPES
       // ... which I can't be stuffed supporting yet. FIXME.
       else
       {
              Py_DECREF(objValue);
              PyErr_SetString(PyExc_TypeError,
                            QObject::tr("Property type '%1' not supported").arg(propertyType));
              return NULL;
       }

       // If `matched' is false, we recognised the C type but weren't able to
       // convert the passed Python value to anything suitable.
       if (!matched)
       {
              // Get a string representation of the object
              PyObject* objRepr = PyObject_Repr(objValue);
              Py_DECREF(objValue); // We're done with it now
              if (!objRepr)
                     return NULL;
              // Extract the repr() string
              QString reprString = QString::fromUtf8(PyString_AsString(objRepr));
              Py_DECREF(objRepr);

              // And return an error
              PyErr_SetString(PyExc_TypeError,
                            QObject::tr("Couldn't convert '%1' to property type '%2'")
                            .arg(reprString).arg(propertyType));
              return NULL;
       }

       // `success' is the return value of the setProperty() call
       if (!success)
       {
              Py_DECREF(objValue);
              PyErr_SetString(PyExc_ValueError,
                            QObject::tr("Types matched, but setting property failed."));
              return NULL;
       }

       Py_DECREF(objValue);
//     Py_INCREF(Py_None);
//     return Py_None;
       Py_RETURN_NONE;
}

Here is the call graph for this function:

PyObject* wrapQObject ( QObject *  obj)

Return a PyCObject containing the passed QObject pointer.

See also:
getQObjectFromPyArg()
Attention:
may return NULL with an exception set

Definition at line 44 of file cmdgetsetprop.cpp.

{
       return PyCObject_FromVoidPtr((void*)obj, NULL);
}

Here is the caller graph for this function: