Exemplo n.º 1
0
def addPixelDataBaseClass(module):
    # wxPixelDataBase is the common base class of the other pixel data classes
    cls = ClassDef(
        name='wxPixelDataBase',
        items=[
            MethodDef(name='wxPixelDataBase',
                      isCtor=True,
                      protection='protected'),
            MethodDef(
                type='wxPoint',
                name='GetOrigin',
                isConst=True,
                briefDoc=
                "Return the origin of the area this pixel data represents."),
            MethodDef(
                type='int',
                name='GetWidth',
                isConst=True,
                briefDoc=
                "Return the width of the area this pixel data represents."),
            MethodDef(
                type='int',
                name='GetHeight',
                isConst=True,
                briefDoc=
                "Return the height of the area this pixel data represents."),
            MethodDef(
                type='wxSize',
                name='GetSize',
                isConst=True,
                briefDoc=
                "Return the size of the area this pixel data represents."),
            MethodDef(
                type='int',
                name='GetRowStride',
                isConst=True,
                briefDoc=
                "Returns the distance between the start of one row to the start of the next row."
            ),
        ])

    # TODO: Try to remember why I chose to do it this way instead of directly
    # returning an instance of the Iterator and giving it the methods needed
    # to be a Python iterator...

    # TODO: Determine how much of a performance difference not using the
    # PixelFacade class would make. Not using the __iter__ makes about 0.02
    # seconds difference per 100x100 bmp in samples/rawbmp/rawbmp1.py...

    cls.addPyMethod('__iter__',
                    '(self)',
                    doc="""\
            Create and return an iterator/generator object for traversing
            this pixel data object.
            """,
                    body="""\
            width  = self.GetWidth()
            height = self.GetHeight()
            pixels = self.GetPixels() # this is the C++ iterator

            # This class is a facade over the pixels object (using the one
            # in the enclosing scope) that only allows Get() and Set() to
            # be called.
            class PixelFacade(object):
                def Get(self):
                    return pixels.Get()
                def Set(self, *args, **kw):
                    return pixels.Set(*args, **kw)
                def __str__(self):
                    return str(self.Get())
                def __repr__(self):
                    return 'pixel(%d,%d): %s' % (x,y,self.Get())
                X = property(lambda self: x)
                Y = property(lambda self: y)

            import sys
            rangeFunc = range if sys.version_info >= (3,) else xrange

            pf = PixelFacade()
            for y in rangeFunc(height):
                pixels.MoveTo(self, 0, y)
                for x in rangeFunc(width):
                    # We always generate the same pf instance, but it
                    # accesses the pixels object which we use to iterate
                    # over the pixel buffer.
                    yield pf
                    pixels.nextPixel()
            """)

    module.addItem(cls)
Exemplo n.º 2
0
def addPixelDataBaseClass(module):
    # wxPixelDataBase is the common base class of the other pixel data classes
    cls = ClassDef(name='wxPixelDataBase', items=[
        MethodDef(
            name='wxPixelDataBase', isCtor=True, protection='protected'),
        MethodDef(
            type='wxPoint', name='GetOrigin',  isConst=True,
            briefDoc="Return the origin of the area this pixel data represents."),
        MethodDef(
            type='int', name='GetWidth', isConst=True, 
            briefDoc="Return the width of the area this pixel data represents."),
        MethodDef(
            type='int', name='GetHeight', isConst=True,
            briefDoc="Return the height of the area this pixel data represents."),
        MethodDef(
            type='wxSize', name='GetSize', isConst=True,
            briefDoc="Return the size of the area this pixel data represents."),
        MethodDef(
            type='int', name='GetRowStride', isConst=True,
            briefDoc="Returns the distance between the start of one row to the start of the next row."),
        ])
    
    # TODO: Try to remember why I chose to do it this way instead of directly
    # returning an instance of the Iterator and giving it the methods needed
    # to be a Python iterator...
    
    # TODO: Determine how much of a performance difference not using the
    # PixelFacade class would make. Not using the __iter__ makes about 0.02
    # seconds difference per 100x100 bmp in samples/rawbmp/rawbmp1.py...

    cls.addPyMethod('__iter__', '(self)',
        doc="""\
            Create and return an iterator/generator object for traversing 
            this pixel data object.
            """,        
        body="""\
            width  = self.GetWidth()
            height = self.GetHeight()
            pixels = self.GetPixels() # this is the C++ iterator
            
            # This class is a facade over the pixels object (using the one
            # in the enclosing scope) that only allows Get() and Set() to
            # be called.
            class PixelFacade(object):
                def Get(self):
                    return pixels.Get()
                def Set(self, *args, **kw):
                    return pixels.Set(*args, **kw)
                def __str__(self):
                    return str(self.Get())
                def __repr__(self):
                    return 'pixel(%d,%d): %s' % (x,y,self.Get())
                X = property(lambda self: x)
                Y = property(lambda self: y)
    
            import sys
            rangeFunc = range if sys.version_info >= (3,) else xrange
    
            pf = PixelFacade()        
            for y in rangeFunc(height):
                pixels.MoveTo(self, 0, y)
                for x in rangeFunc(width):
                    # We always generate the same pf instance, but it
                    # accesses the pixels object which we use to iterate
                    # over the pixel buffer.
                    yield pf    
                    pixels.nextPixel()
            """)
    
    module.addItem(cls)
Exemplo n.º 3
0
def run():
    # Parse the XML file(s) building a collection of Extractor objects
    module = etgtools.ModuleDef(PACKAGE, MODULE, NAME, DOCSTRING)
    etgtools.parseDoxyXML(module, ITEMS)

    #-----------------------------------------------------------------
    # Tweak the parsed meta objects in the module object as needed for
    # customizing the generated code and docstrings.

    c = module.find('wxIdManager')
    assert isinstance(c, etgtools.ClassDef)
    # no tweaks needed for this class

    # wxWindowIDRef is not documented (and probably rightly so) but we're going
    # to use it from Python anyway to help with preallocating IDs in a way that
    # allows them to be reused and be also be protected from conflicts from
    # other auto allocated IDs.

    # First, add defintions of the existing C++ class and its elements
    klass = ClassDef(
        name='wxWindowIDRef',
        bases=[],
        briefDoc="""\
            A wxWindowIDRef object wraps an ID value and marks it as being in-use until all references to that ID are gone.
            """,
        items=[
            MethodDef(name='wxWindowIDRef',
                      className='wxWindowIDRef',
                      isCtor=True,
                      briefDoc='Default constructor',
                      overloads=[
                          MethodDef(name='wxWindowIDRef',
                                    className='wxWindowIDRef',
                                    isCtor=True,
                                    briefDoc='Create reference from an ID',
                                    items=[ParamDef(type='int', name='id')]),
                          MethodDef(name='wxWindowIDRef',
                                    className='wxWindowIDRef',
                                    isCtor=True,
                                    briefDoc='Copy an ID reference',
                                    items=[
                                        ParamDef(type='const wxWindowIDRef&',
                                                 name='idref')
                                    ]),
                      ]),
            MethodDef(name='~wxWindowIDRef',
                      className='wxWindowIDRef',
                      isDtor=True),
            MethodDef(type='int', name='GetValue',
                      briefDoc='Get the ID value'),
        ])

    # Now tweak it a bit
    klass.addCppMethod(
        'int',
        'GetId',
        '()',
        doc=
        "Alias for GetValue allowing the IDRef to be passed as the source parameter to :meth:`wx.EvtHandler.Bind`.",
        body="""\
            return self->GetValue();
            """)

    klass.addCppMethod(
        'int',
        '__int__',
        '()',
        doc=
        "Alias for GetValue allowing the IDRef to be passed as the WindowID parameter when creating widgets or etc.",
        body="""\
            return self->GetValue();
            """)

    klass.addPyMethod('__repr__', '(self)',
                      'return "WindowIDRef: {}".format(self.GetId())')

    # and finish it up by adding it to the module
    module.addItem(klass)

    # Now, let's add a new Python function to the global scope that reserves an
    # ID (or range) and returns a ref object for it.
    module.addPyFunction('NewIdRef',
                         '(count=1)',
                         doc="""\
            Reserves a new Window ID (or range of WindowIDs) and returns a 
            :class:`wx.WindowIDRef` object (or list of them) that will help 
            manage the reservation of that ID.

            This function is intended to be a drop-in replacement of the old 
            and deprecated :func:`wx.NewId` function, with the added benefit 
            that the ID should never conflict with an in-use ID or other IDs 
            generated by this function.
            """,
                         body="""\
            if count == 1:
                return WindowIDRef(IdManager.ReserveId())
            else:
                start = IdManager.ReserveId(count)
                IDRefs = []
                for id in range(start, start+count):
                    IDRefs.append(WindowIDRef(id))
                return IDRefs
            """)

    #-----------------------------------------------------------------
    tools.doCommonTweaks(module)
    tools.runGenerators(module)
Exemplo n.º 4
0
def run():
    # Parse the XML file(s) building a collection of Extractor objects
    module = etgtools.ModuleDef(PACKAGE, MODULE, NAME, DOCSTRING)
    etgtools.parseDoxyXML(module, ITEMS)

    #-----------------------------------------------------------------
    # Tweak the parsed meta objects in the module object as needed for
    # customizing the generated code and docstrings.

    c = module.find('wxIdManager')
    assert isinstance(c, etgtools.ClassDef)
    # no tweaks needed for this class


    # wxWindowIDRef is not documented (and probably rightly so) but we're going
    # to use it from Python anyway to help with preallocating IDs in a way that
    # allows them to be reused and be also be protected from conflicts from
    # other auto allocated IDs.

    # First, add defintions of the existing C++ class and its elements
    klass = ClassDef(name='wxWindowIDRef', bases = [],
        briefDoc="""\
            A wxWindowIDRef object wraps an ID value and marks it as being in-use until all references to that ID are gone.
            """,
        items = [
            MethodDef(name='wxWindowIDRef', className='wxWindowIDRef', isCtor=True,
                briefDoc='Default constructor',
                overloads=[
                MethodDef(name='wxWindowIDRef', className='wxWindowIDRef', isCtor=True,
                    briefDoc='Create reference from an ID',
                    items=[ ParamDef(type='int', name='id') ]),
                
                MethodDef(name='wxWindowIDRef', className='wxWindowIDRef', isCtor=True,
                    briefDoc='Copy an ID reference',
                    items=[ ParamDef(type='const wxWindowIDRef&', name='idref') ]),
                ]),
            
            MethodDef(name='~wxWindowIDRef', className='wxWindowIDRef', isDtor=True),

            MethodDef(type='int', name='GetValue',
                briefDoc='Get the ID value'),
        ])

    # Now tweak it a bit
    klass.addCppMethod('int', 'GetId', '()',
        doc="Alias for GetValue allowing the IDRef to be passed as the source parameter to :meth:`wx.EvtHandler.Bind`.",
        body="""\
            return self->GetValue();
            """)

    klass.addCppMethod('int', '__int__', '()',
        doc="Alias for GetValue allowing the IDRef to be passed as the WindowID parameter when creating widgets or etc.",
        body="""\
            return self->GetValue();
            """)
    
    klass.addCppMethod('bool', '__eq__', '(wxWindowID id)', "return self->GetValue() == id;")
    klass.addCppMethod('bool', '__ne__', '(wxWindowID id)', "return self->GetValue() != id;")
    klass.addCppMethod('bool', '__lt__', '(wxWindowID id)', "return self->GetValue() < id;")
    klass.addCppMethod('bool', '__gt__', '(wxWindowID id)', "return self->GetValue() > id;")
    klass.addCppMethod('bool', '__le__', '(wxWindowID id)', "return self->GetValue() <= id;")
    klass.addCppMethod('bool', '__ge__', '(wxWindowID id)', "return self->GetValue() >= id;")

    klass.addPyMethod('__repr__', '(self)', 'return "WindowIDRef: {}".format(self.GetId())')
    klass.addPyMethod('__hash__', '(self)', 'return hash(self.GetValue())')


    # and finish it up by adding it to the module
    module.addItem(klass)

    # Now, let's add a new Python function to the global scope that reserves an 
    # ID (or range) and returns a ref object for it. 
    module.addPyFunction('NewIdRef', '(count=1)', 
        doc="""\
            Reserves a new Window ID (or range of WindowIDs) and returns a 
            :class:`wx.WindowIDRef` object (or list of them) that will help 
            manage the reservation of that ID.

            This function is intended to be a drop-in replacement of the old 
            and deprecated :func:`wx.NewId` function, with the added benefit 
            that the ID should never conflict with an in-use ID or other IDs 
            generated by this function.
            """,
        body="""\
            if count == 1:
                return WindowIDRef(IdManager.ReserveId())
            else:
                start = IdManager.ReserveId(count)
                IDRefs = []
                for id in range(start, start+count):
                    IDRefs.append(WindowIDRef(id))
                return IDRefs
            """)

    #-----------------------------------------------------------------
    tools.doCommonTweaks(module)
    tools.runGenerators(module)