[gimp] pygimp: Creates the "gimp.Item" class



commit 3de6cc5f74f5bb3b06e908d399a141f073e23380
Author: João S. O. Bueno <gwidion gmail com>
Date:   Mon May 16 01:34:18 2011 -0400

    pygimp: Creates the "gimp.Item" class
    
    Creates a gimp.Item class and change the hyerarchy so that
    Python Vectors, Layers and Drawable classes inherit from it.
    Still not working properly, as PDB calls returns raw integer
    ID's instead of proper Python objects.

 plug-ins/pygimp/Makefile.am       |    1 +
 plug-ins/pygimp/gimpmodule.c      |    5 ++
 plug-ins/pygimp/pygimp-api.h      |    7 ++-
 plug-ins/pygimp/pygimp-drawable.c |    2 +-
 plug-ins/pygimp/pygimp-item.c     |  137 +++++++++++++++++++++++++++++++++++++
 plug-ins/pygimp/pygimp-pdb.c      |   23 ++++++-
 plug-ins/pygimp/pygimp-vectors.c  |    2 +-
 plug-ins/pygimp/pygimp.h          |    4 +
 8 files changed, 177 insertions(+), 4 deletions(-)
---
diff --git a/plug-ins/pygimp/Makefile.am b/plug-ins/pygimp/Makefile.am
index 97f53fe..c436ce2 100644
--- a/plug-ins/pygimp/Makefile.am
+++ b/plug-ins/pygimp/Makefile.am
@@ -32,6 +32,7 @@ pygimp_LTLIBRARIES = gimp.la _gimpenums.la gimpcolor.la _gimpui.la \
 
 gimp_la_SOURCES = \
 	gimpmodule.c		\
+	pygimp-item.c		\
 	pygimp-display.c	\
 	pygimp-drawable.c	\
 	pygimp-image.c		\
diff --git a/plug-ins/pygimp/gimpmodule.c b/plug-ins/pygimp/gimpmodule.c
index 7fe283d..38fb39a 100644
--- a/plug-ins/pygimp/gimpmodule.c
+++ b/plug-ins/pygimp/gimpmodule.c
@@ -1764,6 +1764,8 @@ static struct _PyGimp_Functions pygimp_api_functions = {
     pygimp_image_new,
     &PyGimpDisplay_Type,
     pygimp_display_new,
+    &PyGimpItem_Type,
+    pygimp_item_new,
     &PyGimpDrawable_Type,
     pygimp_drawable_new,
     &PyGimpLayer_Type,
@@ -1891,6 +1893,9 @@ initgimp(void)
     Py_INCREF(&PyGimpImage_Type);
     PyModule_AddObject(m, "Image", (PyObject *)&PyGimpImage_Type);
 
+    Py_INCREF(&PyGimpItem_Type);
+    PyModule_AddObject(m, "Item", (PyObject *)&PyGimpItem_Type);
+
     Py_INCREF(&PyGimpDrawable_Type);
     PyModule_AddObject(m, "Drawable", (PyObject *)&PyGimpDrawable_Type);
 
diff --git a/plug-ins/pygimp/pygimp-api.h b/plug-ins/pygimp/pygimp-api.h
index 2a94740..9128a07 100644
--- a/plug-ins/pygimp/pygimp-api.h
+++ b/plug-ins/pygimp/pygimp-api.h
@@ -26,7 +26,7 @@
 typedef struct {
     PyObject_HEAD
     gint32 ID;
-} PyGimpImage;
+} PyGimpImage, PyGimpItem;
 
 typedef struct {
     PyObject_HEAD
@@ -51,6 +51,9 @@ struct _PyGimp_Functions {
     PyTypeObject *Display_Type;
     PyObject *(* display_new)(gint32 ID);
 
+    PyTypeObject *Item_Type;
+    PyObject *(* item_new)(gint32 ID);
+
     PyTypeObject *Drawable_Type;
     PyObject *(* drawable_new)(GimpDrawable *drawable, gint32 ID);
 
@@ -78,6 +81,8 @@ struct _PyGimp_Functions *_PyGimp_API;
 #define pygimp_image_new        (_PyGimp_API->image_new)
 #define PyGimpDisplay_Type      (_PyGimp_API->Display_Type)
 #define pygimp_display_new      (_PyGimp_API->display_new)
+#define PyGimpItem_Type         (_PyGimp_API->Item_Type)
+#define pygimp_item_new         (_PyGimp_API->item_new)
 #define PyGimpDrawable_Type     (_PyGimp_API->Drawable_Type)
 #define pygimp_drawable_new     (_PyGimp_API->drawable_new)
 #define PyGimpLayer_Type        (_PyGimp_API->Layer_Type)
diff --git a/plug-ins/pygimp/pygimp-drawable.c b/plug-ins/pygimp/pygimp-drawable.c
index b21dc49..577b7dc 100644
--- a/plug-ins/pygimp/pygimp-drawable.c
+++ b/plug-ins/pygimp/pygimp-drawable.c
@@ -1301,7 +1301,7 @@ PyTypeObject PyGimpDrawable_Type = {
     drw_methods,			/* tp_methods */
     0,					/* tp_members */
     drw_getsets,			/* tp_getset */
-    (PyTypeObject *)0,			/* tp_base */
+    &PyGimpItem_Type,			/* tp_base */
     (PyObject *)0,			/* tp_dict */
     0,					/* tp_descr_get */
     0,					/* tp_descr_set */
diff --git a/plug-ins/pygimp/pygimp-item.c b/plug-ins/pygimp/pygimp-item.c
new file mode 100644
index 0000000..9a41d35
--- /dev/null
+++ b/plug-ins/pygimp/pygimp-item.c
@@ -0,0 +1,137 @@
+/* -*- Mode: C; c-basic-offset: 4 -*-
+ * Gimp-Python - allows the writing of Gimp plugins in Python.
+ * Copyright (C) 1997-2002  James Henstridge <james daa com au>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#define NO_IMPORT_PYGOBJECT
+#include <pygobject.h>
+
+#include "pygimp.h"
+
+#define NO_IMPORT_PYGIMPCOLOR
+#include "pygimpcolor-api.h"
+
+#include <glib-object.h>
+
+
+static PyMethodDef item_methods[] = {
+    {NULL,              NULL}           /* sentinel */
+};
+
+static PyObject *
+item_get_parent(PyGimpLayer *self, void *closure)
+{
+    /*  Not implemented yet */
+    return NULL;
+}
+
+static PyGetSetDef item_getsets[] = {
+    { "parent", (getter)item_get_parent, (setter)0 },
+    { NULL, (getter)0, (setter)0 }
+};
+
+
+static void
+item_dealloc(PyGimpItem *self)
+{
+    PyObject_DEL(self);
+}
+
+static PyObject *
+item_repr(PyGimpItem *self)
+{
+    PyObject *s;
+
+    s = PyString_FromFormat("<gimp.Item '%s'>", self->ID);
+
+    return s;
+}
+
+static int
+item_init(PyGimpLayer *self, PyObject *args, PyObject *kwargs)
+{
+    return -1;
+}
+
+
+
+PyTypeObject PyGimpItem_Type = {
+    PyObject_HEAD_INIT(NULL)
+    0,                                  /* ob_size */
+    "gimp.Item",                        /* tp_name */
+    sizeof(PyGimpItem),                 /* tp_basicsize */
+    0,                                  /* tp_itemsize */
+    /* methods */
+    (destructor)item_dealloc,            /* tp_dealloc */
+    (printfunc)0,                       /* tp_print */
+    (getattrfunc)0,                     /* tp_getattr */
+    (setattrfunc)0,                     /* tp_setattr */
+    (cmpfunc)0,                         /* tp_compare */
+    (reprfunc)item_repr,                /* tp_repr */
+    0,                                  /* tp_as_number */
+    0,                                  /* tp_as_sequence */
+    0,                                  /* tp_as_mapping */
+    (hashfunc)0,                        /* tp_hash */
+    (ternaryfunc)0,                     /* tp_call */
+    (reprfunc)0,                        /* tp_str */
+    (getattrofunc)0,                    /* tp_getattro */
+    (setattrofunc)0,                    /* tp_setattro */
+    0,                                  /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT,                 /* tp_flags */
+    NULL, /* Documentation string */
+    (traverseproc)0,                    /* tp_traverse */
+    (inquiry)0,                         /* tp_clear */
+    (richcmpfunc)0,                     /* tp_richcompare */
+    0,                                  /* tp_weaklistoffset */
+    (getiterfunc)0,                     /* tp_iter */
+    (iternextfunc)0,                    /* tp_iternext */
+    item_methods,                       /* tp_methods */
+    0,                                  /* tp_members */
+    item_getsets,                       /* tp_getset */
+    (PyTypeObject *)0,                   /* tp_base */
+    (PyObject *)0,                      /* tp_dict */
+    0,                                  /* tp_descr_get */
+    0,                                  /* tp_descr_set */
+    0,                                  /* tp_dictoffset */
+    (initproc)item_init,                /* tp_init */
+    (allocfunc)0,                       /* tp_alloc */
+    (newfunc)0,                         /* tp_new */
+};
+
+
+PyObject *
+pygimp_item_new(gint32 ID)
+{
+    PyGimpItem *self;
+
+    if (!gimp_item_is_valid(ID)) {
+        Py_INCREF(Py_None);
+        return Py_None;
+    }
+
+    self = PyObject_NEW(PyGimpItem, &PyGimpItem_Type);
+
+    if (self == NULL)
+        return NULL;
+
+    self->ID = ID;
+
+    return (PyObject *)self;
+}
\ No newline at end of file
diff --git a/plug-ins/pygimp/pygimp-pdb.c b/plug-ins/pygimp/pygimp-pdb.c
index 84995d0..fe0584f 100644
--- a/plug-ins/pygimp/pygimp-pdb.c
+++ b/plug-ins/pygimp/pygimp-pdb.c
@@ -33,7 +33,7 @@
 #include <glib-object.h>
 
 #ifndef PG_DEBUG
-# define PG_DEBUG 0
+# define PG_DEBUG 2
 #endif
 
 /* ----------------------------------------------------- */
@@ -73,6 +73,7 @@ pygimp_param_print(int nparams, GimpParam *params)
     int i;
 
     for (i = 0; i < nparams; i++) {
+	g_printf("param_print: type: %d, PDB_ITEM: %d\n",  params[i].type, GIMP_PDB_ITEM);
 	switch(params[i].type) {
 	case GIMP_PDB_INT32:
 	    g_printerr("%i. int %i\n", i,
@@ -126,6 +127,8 @@ pygimp_param_to_tuple(int nparams, const GimpParam *params)
     args = PyTuple_New(nparams);
     for (i = 0; i < nparams && params[i].type != GIMP_PDB_END; i++) {
 	PyObject *value = NULL;
+	
+	g_printf("param_to_tuple: type: %d, PDB_ITEM: %d\n",  params[i].type, GIMP_PDB_ITEM);
 
 	switch(params[i].type) {
 	case GIMP_PDB_INT32:
@@ -261,6 +264,9 @@ pygimp_param_to_tuple(int nparams, const GimpParam *params)
 	case GIMP_PDB_COLOR:
 	    value = pygimp_rgb_new(&params[i].data.d_color);
 	    break;
+	/*
+	GIMP_PDB_REGION is deprecated in libgimpbase/gimpbaseenums.h
+	and conflicts with GIMP_PDB_ITEM
 	case GIMP_PDB_REGION:
 	    value = Py_BuildValue("(iiii)",
 				  (int) params[i].data.d_region.x,
@@ -268,6 +274,7 @@ pygimp_param_to_tuple(int nparams, const GimpParam *params)
 				  (int) params[i].data.d_region.width,
 				  (int) params[i].data.d_region.height);
 	    break;
+	*/
 	case GIMP_PDB_DISPLAY:
 	    value = pygimp_display_new(params[i].data.d_display);
 	    break;
@@ -280,6 +287,9 @@ pygimp_param_to_tuple(int nparams, const GimpParam *params)
 	case GIMP_PDB_CHANNEL:
 	    value = pygimp_channel_new(params[i].data.d_channel);
 	    break;
+	case GIMP_PDB_ITEM:
+	    value = PyInt_FromLong(params[i].data.d_item);
+	    break;
 	case GIMP_PDB_DRAWABLE:
 	    value = pygimp_drawable_new(NULL, params[i].data.d_drawable);
 	    break;
@@ -371,6 +381,7 @@ pygimp_param_from_tuple(PyObject *args, const GimpParamDef *ptype, int nparams)
 	}
     for (i = 1; i <= nparams; i++) {
 	item = PyTuple_GetItem(tuple, i-1);
+	g_printf("param_from_tuple: type: %d, PDB_ITEM: %d\n",  ptype[i-1].type, GIMP_PDB_ITEM);
 	switch (ptype[i-1].type) {
 	case GIMP_PDB_INT32:
 	    check((x = PyNumber_Int(item)) == NULL);
@@ -483,6 +494,7 @@ pygimp_param_from_tuple(PyObject *args, const GimpParamDef *ptype, int nparams)
                 ret[i].data.d_color = rgb;
 	    }
 	    break;
+/*
 	case GIMP_PDB_REGION:
 	    check(!PySequence_Check(item) ||
 		  PySequence_Length(item) < 4);
@@ -497,6 +509,7 @@ pygimp_param_from_tuple(PyObject *args, const GimpParamDef *ptype, int nparams)
 	    ret[i].data.d_region.width = PyInt_AsLong(w);
 	    ret[i].data.d_region.height = PyInt_AsLong(h);
 	    break;
+*/
 	case GIMP_PDB_DISPLAY:
             if (item == Py_None) {
                 ret[i].data.d_display = -1;
@@ -529,6 +542,14 @@ pygimp_param_from_tuple(PyObject *args, const GimpParamDef *ptype, int nparams)
 	    check(!pygimp_channel_check(item));
 	    ret[i].data.d_channel = ((PyGimpChannel *)item)->ID;
 	    break;
+	case GIMP_PDB_ITEM:
+	    if (item == Py_None) {
+		ret[i].data.d_channel = -1;
+		break;
+	    }
+	    check(!pygimp_item_check(item));
+	    ret[i].data.d_item = ((PyGimpItem *)item)->ID;
+	    break;
 	case GIMP_PDB_DRAWABLE:
 	    if (item == Py_None) {
 		ret[i].data.d_channel = -1;
diff --git a/plug-ins/pygimp/pygimp-vectors.c b/plug-ins/pygimp/pygimp-vectors.c
index d85556a..c1558aa 100644
--- a/plug-ins/pygimp/pygimp-vectors.c
+++ b/plug-ins/pygimp/pygimp-vectors.c
@@ -969,7 +969,7 @@ PyTypeObject PyGimpVectors_Type = {
     vectors_methods,                    /* tp_methods */
     0,                                  /* tp_members */
     vectors_getsets,                    /* tp_getset */
-    (PyTypeObject *)0,                  /* tp_base */
+    &PyGimpItem_Type,                  /* tp_base */
     (PyObject *)0,                      /* tp_dict */
     0,                                  /* tp_descr_get */
     0,                                  /* tp_descr_set */
diff --git a/plug-ins/pygimp/pygimp.h b/plug-ins/pygimp/pygimp.h
index dee7d5e..e4989d5 100644
--- a/plug-ins/pygimp/pygimp.h
+++ b/plug-ins/pygimp/pygimp.h
@@ -64,6 +64,10 @@ extern PyTypeObject PyGimpDisplay_Type;
 #define pygimp_display_check(v) (PyObject_TypeCheck(v, &PyGimpDisplay_Type))
 PyObject *pygimp_display_new(gint32 ID);
 
+extern PyTypeObject PyGimpItem_Type;
+#define pygimp_item_check(v) (PyObject_TypeCheck(v, &PyGimpItem_Type))
+PyObject *pygimp_item_new(gint32 ID);
+
 extern PyTypeObject PyGimpDrawable_Type;
 #define pygimp_drawable_check(v) (PyObject_TypeCheck(v, &PyGimpDrawable_Type))
 PyObject *pygimp_drawable_new(GimpDrawable *drawable, gint32 ID);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]