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
|
/* -*- Mode: C; c-basic-offset: 4 -*- */
/* Python plug-in for dia
* Copyright (C) 1999 James Henstridge
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <config.h>
#include "pydia-layer.h"
#include "pydia-object.h"
#include "pydia-cpoint.h"
#include "pydia-render.h"
#include <structmember.h> /* PyMemberDef */
PyObject *
PyDiaLayer_New(Layer *layer)
{
PyDiaLayer *self;
self = PyObject_NEW(PyDiaLayer, &PyDiaLayer_Type);
if (!self) return NULL;
self->layer = layer;
return (PyObject *)self;
}
static void
PyDiaLayer_Dealloc(PyDiaLayer *self)
{
PyObject_DEL(self);
}
static int
PyDiaLayer_Compare(PyDiaLayer *self, PyDiaLayer *other)
{
if (self->layer == other->layer) return 0;
if (self->layer > other->layer) return -1;
return 1;
}
static long
PyDiaLayer_Hash(PyDiaLayer *self)
{
return (long)self->layer;
}
static PyObject *
PyDiaLayer_Str(PyDiaLayer *self)
{
return PyString_FromString(self->layer->name);
}
/* methods here */
static PyObject *
PyDiaLayer_Destroy(PyDiaLayer *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":Layer.destroy"))
return NULL;
layer_destroy(self->layer);
self->layer = NULL; /* we need some error checking elsewhere */
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
PyDiaLayer_ObjectGetIndex(PyDiaLayer *self, PyObject *args)
{
PyDiaObject *obj;
if (!PyArg_ParseTuple(args, "O!:Layer.object_get_index",
&PyDiaObject_Type, &obj))
return NULL;
return PyInt_FromLong(layer_object_get_index(self->layer, obj->object));
}
static PyObject *
PyDiaLayer_AddObject(PyDiaLayer *self, PyObject *args)
{
PyDiaObject *obj;
int pos = -1;
if (!PyArg_ParseTuple(args, "O!|i:Layer.add_object",
&PyDiaObject_Type, &obj,
&pos))
return NULL;
if (pos != -1)
layer_add_object_at(self->layer, obj->object, pos);
else
layer_add_object(self->layer, obj->object);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
PyDiaLayer_RemoveObject(PyDiaLayer *self, PyObject *args)
{
PyDiaObject *obj;
if (!PyArg_ParseTuple(args, "O!:Layer.remove_object",
&PyDiaObject_Type, &obj))
return NULL;
layer_remove_object(self->layer, obj->object);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
PyDiaLayer_FindObjectsInRectangle(PyDiaLayer *self, PyObject *args)
{
Rectangle rect;
GList *list, *tmp;
PyObject *ret;
if (!PyArg_ParseTuple(args, "dddd:Layer.find_objects_in_rectange",
&rect.top, &rect.left, &rect.bottom, &rect.right))
return NULL;
list = layer_find_objects_in_rectangle(self->layer, &rect);
ret = PyList_New(0);
for (tmp = list; tmp; tmp = tmp->next)
PyList_Append(ret, PyDiaObject_New((DiaObject *)tmp->data));
g_list_free(list);
return ret;
}
static PyObject *
PyDiaLayer_FindClosestObject(PyDiaLayer *self, PyObject *args)
{
Point pos;
real maxdist;
DiaObject *obj;
if (!PyArg_ParseTuple(args, "ddd:Layer.find_closest_object",
&pos.x, &pos.y, &maxdist))
return NULL;
obj = layer_find_closest_object(self->layer, &pos, maxdist);
if (obj)
return PyDiaObject_New(obj);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
PyDiaLayer_FindClosestConnectionPoint(PyDiaLayer *self, PyObject *args)
{
ConnectionPoint *cpoint = NULL;
Point pos;
real dist;
PyObject *ret;
PyDiaObject *obj;
if (!PyArg_ParseTuple(args, "dd|O!:Layer.find_closest_connection_point",
&pos.x, &pos.y, PyDiaObject_Type, &obj))
return NULL;
dist = layer_find_closest_connectionpoint(self->layer, &cpoint, &pos, obj ? obj->object : NULL);
ret = PyTuple_New(2);
PyTuple_SetItem(ret, 0, PyFloat_FromDouble(dist));
if (cpoint)
PyTuple_SetItem(ret, 1, PyDiaConnectionPoint_New(cpoint));
else {
Py_INCREF(Py_None);
PyTuple_SetItem(ret, 1, Py_None);
}
return ret;
}
static PyObject *
PyDiaLayer_UpdateExtents(PyDiaLayer *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":Layer.update_extents"))
return NULL;
return PyInt_FromLong(layer_update_extents(self->layer));
}
static PyObject *
PyDiaLayer_Render(PyDiaLayer *self, PyObject *args)
{
PyObject* renderer;
DiaRenderer *wrapper;
Rectangle *update = NULL;
gboolean active = FALSE; /* could derive from layer->parent_diagram->active_layer
* but not sure if it's worth the effort. */
if (!PyArg_ParseTuple(args, "O:Layer.render", &renderer))
return NULL;
/* We need to create the PythonRenderer wrapper to provide the gobject interface.
* This could be done much more efficient if it would somehow be cached for the
* whole rendering pass ...
*/
wrapper = PyDia_new_renderer_wrapper (renderer);
layer_render (self->layer, wrapper, update,
NULL, /* no special object renderer */
NULL, /* no user data */
active);
g_object_unref (wrapper);
Py_INCREF(Py_None);
return Py_None;
}
/* missing functions:
* layer_add_objects
* layer_add_objects_first
* layer_remove_objects
* layer_replace_object_with_list
* layer_set_object_list
*/
static PyMethodDef PyDiaLayer_Methods[] = {
{"destroy", (PyCFunction)PyDiaLayer_Destroy, METH_VARARGS,
"Release the layer. Must not be called when already added to a diagram."},
{"object_get_index", (PyCFunction)PyDiaLayer_ObjectGetIndex, METH_VARARGS,
"object_get_index(Object: o) -> int."
" Returns the index of the object in the layers list of objects."},
{"add_object", (PyCFunction)PyDiaLayer_AddObject, METH_VARARGS,
"add_object(Object: o[, int: position]) -> None."
" Add the object to the layer at the top or the given position counting from bottom."},
{"remove_object", (PyCFunction)PyDiaLayer_RemoveObject, METH_VARARGS,
"remove_object(Object: o) -> None"
" Remove the object from the layer and delete it."},
{"find_objects_in_rectangle",
(PyCFunction)PyDiaLayer_FindObjectsInRectangle, METH_VARARGS,
"find_objects_in_rectangle(real: top, real left, real: bottom, real: right) -> Objects"
" Returns a list of objects found in the given rectangle."},
{"find_closest_object", (PyCFunction)PyDiaLayer_FindClosestObject, METH_VARARGS,
"find_closest_object(real: x, real: y, real: maxdist) -> Object."
" Find an object in the given maximum distance of the given point."},
{"find_closest_connection_point",
(PyCFunction)PyDiaLayer_FindClosestConnectionPoint, METH_VARARGS,
"find_closest_connectionpoint(real: x, real: y[, Object: o]) -> (real: dist, ConnectionPoint: cp)."
" Given a point and an optional object to exclude return the distance and the closest connection point or None."},
{"update_extents", (PyCFunction)PyDiaLayer_UpdateExtents, METH_VARARGS,
"update_extents() -> None. Force recaculation of the layer extents."},
{ "render", (PyCFunction)PyDiaLayer_Render, METH_VARARGS,
"render(dia.Renderer: r) -> None."
" Render the layer with the given renderer" },
{NULL, 0, 0, NULL}
};
#define T_INVALID -1 /* can't allow direct access due to pyobject->data indirection */
static PyMemberDef PyDiaLayer_Members[] = {
{ "extents", T_INVALID, 0, RESTRICTED|READONLY,
"Rectangle covering all object's bounding boxes." },
{ "name", T_INVALID, 0, RESTRICTED|READONLY,
"The name of the layer." },
{ "objects", T_INVALID, 0, RESTRICTED|READONLY,
"The list of objects in the layer." },
{ "visible", T_INVALID, 0, RESTRICTED|READONLY,
"The visibility of the layer." },
{ NULL }
};
static PyObject *
PyDiaLayer_GetAttr(PyDiaLayer *self, gchar *attr)
{
if (!strcmp(attr, "__members__"))
return Py_BuildValue("[ssss]", "extents", "name", "objects",
"visible");
else if (!strcmp(attr, "name"))
return PyString_FromString(self->layer->name);
else if (!strcmp(attr, "extents"))
return Py_BuildValue("(dddd)", self->layer->extents.top,
self->layer->extents.left,
self->layer->extents.bottom,
self->layer->extents.right);
else if (!strcmp(attr, "objects")) {
PyObject *ret;
GList *tmp;
gint i;
ret = PyTuple_New(g_list_length(self->layer->objects));
for (i = 0, tmp = self->layer->objects; tmp; i++, tmp = tmp->next)
PyTuple_SetItem(ret, i, PyDiaObject_New((DiaObject *)tmp->data));
return ret;
} else if (!strcmp(attr, "visible"))
return PyInt_FromLong(self->layer->visible);
return Py_FindMethod(PyDiaLayer_Methods, (PyObject *)self, attr);
}
PyTypeObject PyDiaLayer_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"dia.Layer",
sizeof(PyDiaLayer),
0,
(destructor)PyDiaLayer_Dealloc,
(printfunc)0,
(getattrfunc)PyDiaLayer_GetAttr,
(setattrfunc)0,
(cmpfunc)PyDiaLayer_Compare,
(reprfunc)0,
0,
0,
0,
(hashfunc)PyDiaLayer_Hash,
(ternaryfunc)0,
(reprfunc)PyDiaLayer_Str,
(getattrofunc)0,
(setattrofunc)0,
(PyBufferProcs *)0,
0L, /* Flags */
"A Layer is part of a Diagram and can contain objects.",
(traverseproc)0,
(inquiry)0,
(richcmpfunc)0,
0, /* tp_weakliszoffset */
(getiterfunc)0,
(iternextfunc)0,
PyDiaLayer_Methods, /* tp_methods */
PyDiaLayer_Members, /* tp_members */
0
};
|