×

Discussion Board

Results 1 to 3 of 3

Thread: Page builder

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    24

    Page builder

    Hi!

    i've found this:

    This library shows a working example of image scrolling. This library create an over sized image to display text. The image is blit to a canvas object and the scrolling factor can be controlled via the Up and Down Softkeys.
    [CODE]
    import appuifw, e32
    from key_codes import EScancodeUpArrow, EScancodeDownArrow
    from graphics import Image
    try:
    from akntextutils import wrap_text_to_array
    except:
    import sys
    sys.exit("akntextutils module isn't installed!")

    ## Keyboard handler class.
    class Keyboard( object ):
    ## The constructor
    # @param self The object pointer
    # @param aOnevent An optional function that can be called on events
    def __init__( self, aOnevent=lambda:None ):
    ## Keboard state dictionnary
    self._keyboard_state = { }
    ## Key down dictionnary
    self._downs = { }
    ## User defined callback function run when an event occures
    self._onevent = aOnevent

    ## Event handler
    # @param self The object pointer
    # @param aEvent Event detected
    def handle_event( self, aEvent ):
    if aEvent['type'] == appuifw.EEventKeyDown:
    code = aEvent['scancode']
    if not self.is_down( code ):
    self._downs[code] = self._downs.get( code, 0 ) + 1
    self._keyboard_state
    Code:
     = 1
            elif aEvent['type'] == appuifw.EEventKeyUp:
                self._keyboard_state[aEvent['scancode']] = 0
            self._onevent( )
    
        ## Detects if the given keycode key is down
        #  @param self The object pointer
        #  @param aScancode key code
        def is_down( self, aScancode ):
            return self._keyboard_state.get( aScancode, 0 )
    
        ## Returns true if the given keycode key has been pressed
        #  @param self The object pointer
        #  @param aScancode Key code
        def pressed( self, aScancode ):
            if self._downs.get( aScancode, 0 ):
                self._downs[aScancode] -= 1
                return True
            return False
    
    
    ## Base class from making scrollable pages.
    class PageBuilder( object, appuifw.Canvas):
        ## Background color for the list
        _iBackgroundColor = 0xffffff
        ## Default text font
        _iDefaultFont = 'dense'
        ## Over sized image to be scrolled
        _iOSImage = None
        ## Lines to print on the image
        _iLines = None
        ## Line space
        _iLineSpace = 13
        ## When True, elements are drawn
        _iReady = False
        ## Scrollbar cursor outline color
        _iScrollbarCursorOutlineColor = 0x0000ff
        ## Scrollbar cursor fill color
        _iScrollbarCursorFillColor = 0xc3d9ff
        ## Scrollbar outline color
        _iScrollbarOutlineColor = 0x000000
        ## Scrolling factor
        _iYFactor = 0
        ## Text x origin in pixel
        _iXText = 2
        ## Text x origin in pixel
        _iYText = _iLineSpace
            
        ## The constructor
        #  @param self The object pointer
        #  @param aList List containing text to display
        #  @param aScrollY True by default; show the scrollbar or not
        def __init__( self, aList, aScrollY=True ):
            ## If True, the scrollbar will be displayed
            self._iScrollY = aScrollY
            ## Keyboard handler instance. self._eventCallback given will be called 
            #  everytime a keyboard event will happen. That way it filters the
            #  useless events.
            self._iKeyboard = Keyboard( self._eventCallback )
            appuifw.Canvas.__init__( self, 
                                     self._redrawCallback,
                                     self._iKeyboard.handle_event )
    
            appuifw.app.body = self 
            self._formatData( aList )
            self._createOversizedImage( )
            self._iReady = True
    
        ## Displays the text on the application body
        #  @param self The object pointer
        def show( self ):
            appuifw.app.body = self
    
        ## Set new content for the text object
        #  @param self The object pointer
        #  @param aLong_str_array Array containing the long string to print
        def set( self, aLong_str_array ):
            self._formatData( aLong_str_array )
            self._redrawCallback( )
    
        ## Key down, scrolls down
        #  @param self The object pointer
        def _down( self ):
            if ( ( self.size[1] ) + ( self._iYFactor + 1 ) * self._iLineSpace ) < \
                 ( self._iOSImage.size[1] + self._iLineSpace ):
                self._iYFactor += 1
                self._redrawCallback( )
    
        ## Draw the image
        #  @param self The object pointer
        def _drawImage( self ):
            self.blit(self._iOSImage ,target=( 0, 
                      -self._iYFactor * self._iLineSpace ) )
    
        ## Draw the scroll bar on the right
        #  @param self The object pointer
        def _drawScrollbar( self ):
            if self._iScrollY:
                height = ( self._iOSImage.size[1] - self.size[1] ) / \
                           self._iLineSpace
                if ( self._iOSImage.size[1] - self.size[1] ) % \
                     self._iLineSpace != 0:
                    height += 1
                height += 1    # +1 ince we start counting from 0
                height = self.size[1] / height
                y = self._iYFactor * height
                self.line( ( self.size[0] - 2, 0 ,self.size[0] - 2, self.size[1] ), 
                           # vertical right side scroll bar
                           outline=self._iScrollbarOutlineColor )  
                self.rectangle( ( self.size[0] - 3, y, ( self.size[0] ), y + height ),
                                 outline=self._iScrollbarCursorOutlineColor,               
                                 # scroll rectangle
                                 fill=self._iScrollbarCursorFillColor) 
        ## Event callback method for the canvas
        #  @param self The object pointer
        def _eventCallback( self ):
            if self._iKeyboard.pressed( EScancodeUpArrow ):
                self._up( )
            elif self._iKeyboard.pressed( EScancodeDownArrow ):
                self._down( )
    
        ## Redraw callback method for teh canvas
        #  @param self The object pointer
        #  @param aRect Attribute value sent by the Canvas object
        def _redrawCallback( self, aRect=None):
            self.clear( self._iBackgroundColor )
            if self._iReady:
                self._drawImage()
                self._drawScrollbar()
    
        ## Key up, scrolls up
        #  @param self The object pointer
        def _up( self ):
            if self._iYFactor > 0:
                self._iYFactor -= 1
                self._redrawCallback( )
    
    
        ## Generates array of line to be drawn
        #  @param self The object pointer
        #  @param aLong_str_array Array containing the long string to print
        def _formatData(self, aLong_str_array):
            temp = []
            i = 0
            for long_str in aLong_str_array:
                # long_str is a long string to be wrapped
                lines = wrap_text_to_array(long_str, 
                                           self._iDefaultFont, 
                                           self.size[0]-5)
                for line in lines:
                   temp.append(line)
                   i += 1
                # finally set the private lines variable
                self._iLines = temp
    
        ## Create an oversized image
        #  @param self The object pointer
        def _createOversizedImage( self ):
            x, y = self._iXText, self._iYText
            # calculate the height (head space + img height + 1 line + x line for
            # the text)
            height = y + ( len( self._iLines ) * self._iLineSpace ) + \
                     self._iLineSpace
            # create the main image
            self._iOSImage = Image.new( ( self.size[0], height ) )
            # add the text on it
            for line in self._iLines:
                self._iOSImage.text( ( x, y ), line, font=self._iDefaultFont )
                y += self._iLineSpace
    Code:
    
    
    and the example usage is this:
    Code:
    # Create active object
    SCRIPT_LOCK = e32.Ao_lock( )
     
    def __exit__( ):
        SCRIPT_LOCK.signal( )
     
    appuifw.app.exit_key_handler = __exit__
    appuifw.app.title= u'PageBuilder base class'
     
    text = [u'iLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do' \
    'eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad' \
    'minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex '\
    'ea commodo consequat. Duis aute irure dolor in reprehenderit in '\
    'voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur '\
    'sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt '\
    'mollit anim id est laborum.' ]
     
    PageBuilder( text ).show( )
     
    SCRIPT_LOCK.wait( )
    but i can't use it!it gives me a error!...
    where is the problem?some one could help me?

    THK
    Last edited by giogiogio; 2007-08-25 at 20:22.

  2. #2
    Super Contributor
    Join Date
    May 2006
    Location
    Oulu, Finland
    Posts
    622

    Re: Page builder

    Hi giogiogio,

    First of all, please use my version, using binding instead of the keyboard class and able to resize when changing the screen size.

    Code:
    ####################################################
    #  Author: LEFEVRE Damien
    #  Version: 0.2
    #  Decription: Text page builder
    #  Copyright 2006-2007 LEFEVRE Damien contact[at]lfdm[dot]net
    #  License: Apache2
    ####################################################
    import appuifw #@UnresolvedImport
    from key_codes import EKeyUpArrow, EKeyDownArrow, EKeySelect #@UnresolvedImport
    from graphics import Image #@UnresolvedImport
    try:
        from akntextutils import wrap_text_to_array #@UnresolvedImport
    except:
        import sys
        sys.exit("akntextutils module isn't installed!")
    
    ## @brief Base class from making scrollable pages.
    #
    #  PageBuild build a non-editable page of text wrapped inside the screen width. The
    #  page can be scrolled vertically.
    #  This UI control handle the screen resizing i.e: dimentions, protrait, landscape
    #  for the phone supporting this feature.
    #
    #
    #  @b Snippet:
    #  @code
    # import appuifw, e32 
    # from org.pbol.UI.pageMaker import PageMaker
    #
    # # Create active object
    # SCRIPT_LOCK = e32.Ao_lock( )
    #  
    # def __exit__( ):
    #     SCRIPT_LOCK.signal( )
    # 
    # 
    # def portrait():
    #     appuifw.app.orientation='portrait'
    # 
    # def landscape():
    #     appuifw.app.orientation='landscape'
    # 
    # appuifw.app.exit_key_handler = __exit__
    # appuifw.app.title = u'PageBuilder base class'
    # appuifw.app.menu = [(u'Portrait', portrait),
    #                     (u'Landscape', landscape)]
    # 
    # text = [u'iLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do' \
    # 'eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad' \
    # 'minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex '\
    # 'ea commodo consequat. Duis aute irure dolor in reprehenderit in '\
    # 'voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur '\
    # 'sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt '\
    # 'mollit anim id est laborum.' ]
    # 
    # PageMaker( text ).show( )
    # 
    # SCRIPT_LOCK.wait( )
    #  @endcode
    class PageMaker( object, appuifw.Canvas):
        
        ## Background color for the list
        _iBackgroundColor = 0xffffff
        
        ## Default text font
        _iDefaultFont = 'dense'
        
        ## Over sized image to be scrolled
        _iOSImage = None
        
        ## Lines to print on the image
        _iLines = []
        
        ## Line space
        _iLineSpace = 20
        
        ## When True, elements are drawn
        _iReady = False
        
        ## Scrollbar cursor outline color
        _iScrollbarCursorOutlineColor = 0x0000ff
        
        ## Scrollbar cursor fill color
        _iScrollbarCursorFillColor = 0xc3d9ff
        
        ## Scrollbar outline color
        _iScrollbarOutlineColor = 0x000000
        
        ## Scrolling factor
        _iYFactor = 0
        
        ## Text x origin in pixel
        _iXText = 2
        
        ## Text x origin in pixel
        _iYText = _iLineSpace
        
            
        ## @brief The constructor
        #  @param self The object pointer
        #  @param aList List containing text to display
        #  @param aScrollY True by default; show the scrollbar or not
        def __init__( self, aList=[], aScrollY=True ):
            ## If True, the scrollbar will be displayed
            self._iScrollY = aScrollY
            ## Keyboard handler instance. self._eventCallback given will be called 
            #  everytime a keyboard event will happen. That way it filters the
            #  useless events.
            appuifw.Canvas.__init__( self, 
                                     self._redrawCallback,
                                     resize_callback = self._resizeCallback )
     
            appuifw.app.body = self 
            self.bind(EKeySelect, None)
            self.bind( EKeyUpArrow, lambda:self._up( ) )
            self.bind( EKeyDownArrow, lambda:self._down( ) )
            
            ## @brief Store the array of non wrapped string set at the construction or when
            #  calling the set method.
            self._iRawStringArray = aList
            self._iReady = True
     
     
        ## @brief Displays the text on the application body
        #  @param self The object pointer
        def show( self ):
            appuifw.app.body = self
     
     
        ## @brief Set new content for the text object
        #  @param self The object pointer
        #  @param aLong_str_array Array containing the long string to print
        def set( self, aLong_str_array ):
            self._iRawStringArray = aLong_str_array
            self._formatData( )
            self._redrawCallback( )
     
     
        ## @brief Key down, scrolls down
        #  @param self The object pointer
        def _down( self ):
            if ( ( self.size[1] ) + ( self._iYFactor + 1 ) * self._iLineSpace ) < \
                 ( self._iOSImage.size[1] + self._iLineSpace ):
                self._iYFactor += 1
                self._redrawCallback( )
     
     
        ## @brief Draw the image
        #  @param self The object pointer
        def _drawImage( self ):
            if self._iOSImage:
                self.blit(self._iOSImage ,target=( 0,
                          -self._iYFactor * self._iLineSpace ) )
    
    
        ## @brief Draw the scroll bar on the right
        #  @param self The object pointer
        def _drawScrollbar( self ):
            if self._iScrollY and self._iOSImage:
                height = ( self._iOSImage.size[1] - self.size[1] ) / \
                           self._iLineSpace
    
                # if height is negative, then the image is smaller than the cnaves
                if height < 0:
                    height = 0
    
                if ( self._iOSImage.size[1] - self.size[1] ) % \
                     self._iLineSpace != 0:
                    height += 1
    
                height += 1    # +1 ince we start counting from 0
                height = self.size[1] / height
                y = self._iYFactor * height
                
                height = self.size[1]
                if self._iOSImage.size[1] > self.size[1]:
                    height = self.size[1] - (self._iOSImage.size[1] - self.size[1] ) /self._iLineSpace
                    if ( self._iOSImage.size[1] - self.size[1] ) % \
                         self._iLineSpace != 0:
                        height -= 1                
                y = self._iYFactor
    
                self.line( ( self.size[0] - 2, 0 ,self.size[0] - 2, self.size[1] ),
                           # vertical right side scroll bar
                           outline=self._iScrollbarOutlineColor )
                self.rectangle( ( self.size[0] - 3, y, ( self.size[0] ), y + height ),
                                 outline=self._iScrollbarCursorOutlineColor,
                                 # scroll rectangle
                                 fill=self._iScrollbarCursorFillColor)
    
    
        ## @brief Redraw callback method for teh canvas
        #  @param self The object pointer
        #  @param aRect Attribute value sent by the Canvas object
        def _redrawCallback( self, aRect=None): #@UnusedVariable
            self.clear( self._iBackgroundColor )
            if self._iReady:
                self._drawImage()
                self._drawScrollbar()
     
     
        ## @brief Key up, scrolls up
        #  @param self The object pointer
        def _up( self ):
            if self._iYFactor > 0:
                self._iYFactor -= 1
                self._redrawCallback( )
    
    
        ## @brief Generates array of line to be drawn
        #  @param self The object pointer
        def _formatData(self):
    
            temp = []
            measures = self.measure_text(u"Tlpq", font=self._iDefaultFont, maxwidth=self.size[0]-5)
            ## @brief line space recalculated when the screen dimentions or orientation 
            #  change
            self._iLineSpace = abs(measures[0][1]) + abs(measures[0][3])
            for long_str in self._iRawStringArray:
                # long_str is a long string to be wrapped
                lines = wrap_text_to_array(long_str,
                                           self._iDefaultFont,
                                           self.size[0]-5)
                for line in lines:
                   temp.append(line)
                # finally set the private lines variable
                
                ## Store the lines of text wrapped at the screen width
                self._iLines = temp
    
    
        ## @brief Create an oversized image
        #  @param self The object pointer
        def _createOversizedImage( self ):
            x, y = self._iXText, self._iYText
            # calculate the height (head space + img height + 1 line + x line for
            # the text)
            height = y + ( len( self._iLines ) * self._iLineSpace ) + \
                     self._iLineSpace
            # create the main image
            self._iOSImage = Image.new( ( self.size[0], height ) )
            # add the text on it
            for line in self._iLines:
                self._iOSImage.text( ( x, y ), line, font=self._iDefaultFont )
                y += self._iLineSpace
    
    
        ## @brief Callback called when the screen size has changed. It can be due to 
        #  tilting the screen, changing apuifw.app.menu to 'normal', 'large', 'full'
        #  @param self The object pointer.
        #  @param aRect Default paramter sent with the callback
        def _resizeCallback(self, aRect=None): #@UnusedVariable
            try:
                self._formatData()
                self._createOversizedImage()
            except:
                pass
    but i can't use it!it gives me a error!...
    where is the problem?
    What exactly the error is?


    some one could help me?
    Sure there's plenty of guys out there

    LFD
    Devices:
    Nokia E61 3rd Edition - pys60 1.4.0

    Tips and modules:
    http://www.lfdm.net/thesis

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    24

    Re: Page builder

    ok!
    of course i'll use your version!!

    for the problem i've done.i have to use a list not a string!...

Similar Threads

  1. Replies: 0
    Last Post: 2005-02-17, 06:15
  2. File format Unknown when trying to view XHTML page on the Nokia6650
    By shawn.hines in forum Browsing and Mark-ups
    Replies: 0
    Last Post: 2004-05-04, 16:35

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×