Re: Defining custom GEnum subclasses



Στις 10-06-2011, ημέρα Παρ, και ώρα 15:09 -0400, ο/η John Palmieri
έγραψε:
> 
> ----- Original Message -----
> > Hi,
> > 
> > I'm wondering if it is possible to define a new GEnum type.
> > 
> > GObject.type_register() only registers subtypes of GObject
> > and whatever else I tried resulted in some error about a missing
> > '__enum_values__' attribute (which I don't know how to define).
> > 
> > Of course this is expected, as I suppose that the underlying
> > GObject system must be notified for the value list somehow.
> > 
> > I need the custom GEnum, to use it for defining custom properties.
> > 
> > 
> > The reason why I DO NEED a custom GEnum instead of an integer
> > for defining my properties, is that I want to expose and use
> > my custom widgets from the Glade designer, and I want to
> > be able to select the property by its values (with a combo box),
> > instead of just specifying an integer
> > 
> > Thanks in advance,
> > Costas
> > 
> 
> Looking at the code in gobject/pygenum.c, it doesn't look like you can actually construct a GEnumClass in Python since it requires a subclass of GEnumClass that aleady defines the size and limits of the enum.  It is all pretty bound to C and there doesn't look like GEnums can ever be constructed dynamically.
>   
> --
> John (J5) Palmieri
> Software Engineer
> Red Hat, Inc.

Thank you for your response, John.
I had some progress myself and I was just about to share it with the
list!

I guess you are right about not being possible to create new GEnums by
subclassing existing GEnums, since this would mean changing the already
defined GEnumValues

I was more thinking about some function like
GObject.register_enum(values)
though, that would create a new GEnumClass from scratch,
with 'values' being some GEnumValue array or something like that,

or to define some class with a dictionary '__enum_values__'
and have some
GObject.register_enum(<class>)
to trigger the registration



I have been experimenting towards this direction after my last post, by
creating a snippet (a custom time entry widget) in vala (and accessed it
from python). I noticed that my new GEnum (MyTime.MyTimeAccuracy) was
registered in the generated C code with something like:
...
static const GEnumValue values[] = {
  { MY_TIME_MY_TIME_ACCURACY_ACCURACY_MINUTE,
    "MY_TIME_MY_TIME_ACCURACY_ACCURACY_MINUTE", "accuracy-minute"},
  { MY_TIME_MY_TIME_ACCURACY_ACCURACY_SECOND,
    "MY_TIME_MY_TIME_ACCURACY_ACCURACY_SECOND", "accuracy-second"}, 
  { MY_TIME_MY_TIME_ACCURACY_ACCURACY_MICRO, 
    "MY_TIME_MY_TIME_ACCURACY_ACCURACY_MICRO", "accuracy-micro"},
  { 0, NULL, NULL}
};
GType my_time_my_time_accuracy_type_id = 
  g_enum_register_static ("MyTimeMyTimeAccuracy", values);
...
and a lot of pre-processor definitions...
(I'm not very familiar with Gtk in C, but I can send you the snippet if
you want)

Is this 'g_enum_register_static' or something equivalent wrapped in
python? 
In fact it is?

I have tried in python
GObject.enum_register_static('MyEnum', [[1, 'aaa', 'aaa'], [2, 'bbb',
'bbb']])
the function is found but I get some error:
TypeError: argument 1: Must be GObject.EnumValue, not list

If I do:
GObject.enum_register_static('MyEnum', GObject.EnumValue())
I get no errors, but of course, I need something more than a
single-empty GObject.EnumValue item to construct a custom GEnum!

Even so, I can construct but not customise a GObject.EnumValue

Any ideas?

Costas



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