<s> from decimal import Decimal <EOL> import sys <EOL> import re <EOL> import os <EOL> import xml . sax . handler <EOL> import traceback <EOL> from muntjac . api import * <EOL> from mjextras import * <EOL> class TreeBuilder ( xml . sax . handler . ContentHandler ) : <EOL> def __init__ ( self , parent ) : <EOL> self . stack = [ ] <EOL> self . root = DataNode ( ) <EOL> self . current = self . root <EOL> self . text_parts = [ ] <EOL> self . parent = parent <EOL> def startElement ( self , name , attrs ) : <EOL> self . stack . append ( ( self . current , self . text_parts ) ) <EOL> self . current = DataNode ( ) <EOL> self . text_parts = [ ] <EOL> for k , v in attrs . items ( ) : <EOL> self . current . _add_xml_attr ( self . parent . _name_mangle ( k ) , v ) <EOL> def endElement ( self , name ) : <EOL> text = '<STR_LIT>' . join ( self . text_parts ) . strip ( ) <EOL> if text : <EOL> self . current . data = text <EOL> if self . current . _attrs : <EOL> obj = self . current <EOL> else : <EOL> obj = text or '<STR_LIT>' <EOL> self . current , self . text_parts = self . stack . pop ( ) <EOL> self . current . _add_xml_attr ( self . parent . _name_mangle ( name ) , obj ) <EOL> def characters ( self , content ) : <EOL> self . text_parts . append ( content ) <EOL> class DataNode ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _attrs = { } <EOL> self . data = None <EOL> def __len__ ( self ) : <EOL> return <NUM_LIT:1> <EOL> def __getitem__ ( self , key ) : <EOL> if isinstance ( key , basestring ) : <EOL> return self . _attrs . get ( key , None ) <EOL> else : <EOL> return [ self ] [ key ] <EOL> def __contains__ ( self , name ) : <EOL> return self . _attrs . has_key ( name ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . _attrs or self . data ) <EOL> def __getattr__ ( self , name ) : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> raise AttributeError ( name ) <EOL> return self . _attrs . get ( name , None ) <EOL> def _add_xml_attr ( self , name , value ) : <EOL> if name in self . _attrs : <EOL> children = self . _attrs [ name ] <EOL> if not isinstance ( children , list ) : <EOL> children = [ children ] <EOL> self . _attrs [ name ] = children <EOL> children . append ( value ) <EOL> else : <EOL> self . _attrs [ name ] = value <EOL> def __str__ ( self ) : <EOL> return self . data or '<STR_LIT>' <EOL> def __repr__ ( self ) : <EOL> items = sorted ( self . _attrs . items ( ) ) <EOL> if self . data : <EOL> items . append ( ( '<STR_LIT:data>' , self . data ) ) <EOL> return u'<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( [ u'<STR_LIT>' % ( k , repr ( v ) ) for k , v in items ] ) <EOL> class xml2obj ( object ) : <EOL> def __init__ ( self , src ) : <EOL> self . src = src <EOL> self . non_id_char = re . compile ( '<STR_LIT>' ) <EOL> def _name_mangle ( self , name ) : <EOL> return self . non_id_char . sub ( '<STR_LIT:_>' , name ) <EOL> def build ( self ) : <EOL> builder = TreeBuilder ( self ) <EOL> if isinstance ( self . src , basestring ) : <EOL> xml . sax . parseString ( self . src , builder ) <EOL> else : <EOL> xml . sax . parse ( self . src , builder ) <EOL> return builder . root . _attrs . values ( ) [ <NUM_LIT:0> ] <EOL> def cfkey ( theobj , thevar ) : <EOL> try : <EOL> e = getattr ( theobj , thevar ) <EOL> if e == "<STR_LIT>" : return False <EOL> else : return True <EOL> except : <EOL> return False <EOL> class MuntJacWindow ( object ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> class QT2Muntjac ( object ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def translateUI ( self , filename ) : <EOL> IN = open ( filename , "<STR_LIT:r>" ) . read ( ) <EOL> g = xml2obj ( IN ) . build ( ) <EOL> centralwidget = g . widget <EOL> mainWidget = aWidget ( None , centralwidget , None , True ) <EOL> self . window = mainWidget . mainwidget <EOL> self . ui = mainWidget . mainUI <EOL> class aWidget ( object ) : <EOL> def __init__ ( self , parent , widget , mainUI = None , main = False ) : <EOL> if mainUI == None : <EOL> self . mainUI = MuntJacWindow ( ) <EOL> else : <EOL> self . mainUI = mainUI <EOL> self . main = main <EOL> self . parent = parent <EOL> self . widget = widget <EOL> self . cls = widget [ '<STR_LIT:class>' ] <EOL> self . mainwidget = None <EOL> self . isLayout = False <EOL> if self . main == False : <EOL> if self . parent . wHandleSize == False : <EOL> self . wHandleSize = False <EOL> else : <EOL> self . wHandleSize = True <EOL> else : <EOL> self . wHandleSize = False <EOL> found , geometry = self . getProperty ( "<STR_LIT>" ) <EOL> if found : <EOL> self . geometry = geometry <EOL> else : <EOL> self . geometry = None <EOL> self . makeWidget ( ) <EOL> self . makeChildren ( ) <EOL> def makeWidget ( self ) : <EOL> if not self . main : <EOL> if self . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> if self . widget . layout != None : <EOL> if self . widget . layout [ "<STR_LIT:class>" ] == "<STR_LIT>" : <EOL> self . mainUI . __dict__ [ self . widget . layout . name ] = VerticalLayout ( ) <EOL> self . thiswidget = self . mainUI . __dict__ [ self . widget . layout . name ] <EOL> self . wHandleSize = False <EOL> self . isLayout = True <EOL> elif self . widget . layout [ "<STR_LIT:class>" ] == "<STR_LIT>" : <EOL> self . mainUI . __dict__ [ self . widget . layout . name ] = HorizontalLayout ( ) <EOL> self . thiswidget = self . mainUI . __dict__ [ self . widget . layout . name ] <EOL> self . wHandleSize = False <EOL> self . isLayout = True <EOL> elif self . widget . layout == None : <EOL> self . mainUI . __dict__ [ self . widget . name ] = AbsoluteLayout ( ) <EOL> self . thiswidget = self . mainUI . __dict__ [ self . widget . name ] <EOL> self . wHandleSize = True <EOL> if self . parent . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> self . parent . mainwidget = self . thiswidget <EOL> self . mainUI . width = self . parent . geometry . rect . width <EOL> self . mainUI . height = self . parent . geometry . rect . height <EOL> elif self . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> self . mainUI . __dict__ [ self . widget . name ] = StackedSheet ( ) <EOL> self . thiswidget = self . mainUI . __dict__ [ self . widget . name ] <EOL> elif self . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> title = str ( self . getTitle ( ) ) <EOL> self . mainUI . __dict__ [ self . widget . name + '<STR_LIT>' ] = Panel ( title ) <EOL> self . thiswidget = self . mainUI . __dict__ [ self . widget . name + '<STR_LIT>' ] <EOL> self . mainUI . __dict__ [ self . widget . name ] = AbsoluteLayout ( ) <EOL> self . thiswidget . setContent ( self . mainUI . __dict__ [ self . widget . name ] ) <EOL> elif self . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> text = str ( self . getText ( ) ) <EOL> self . mainUI . __dict__ [ self . widget . name ] = Label ( text ) <EOL> self . thiswidget = self . mainUI . __dict__ [ self . widget . name ] <EOL> elif self . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> text = str ( self . getText ( ) ) <EOL> self . mainUI . __dict__ [ self . widget . name ] = TextField ( "<STR_LIT>" ) <EOL> self . thiswidget = self . mainUI . __dict__ [ self . widget . name ] <EOL> self . thiswidget . setValue ( text ) <EOL> elif self . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> text = str ( self . getText ( ) ) <EOL> self . mainUI . __dict__ [ self . widget . name ] = Button ( text ) <EOL> self . thiswidget = self . mainUI . __dict__ [ self . widget . name ] <EOL> elif self . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> html = str ( self . getHtml ( ) ) <EOL> self . mainUI . __dict__ [ self . widget . name ] = TextArea ( ) <EOL> self . thiswidget = self . mainUI . __dict__ [ self . widget . name ] <EOL> self . thiswidget . setValue ( html ) <EOL> elif self . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> self . mainUI . __dict__ [ self . widget . name ] = TabSheet ( ) <EOL> self . thiswidget = self . mainUI . __dict__ [ self . widget . name ] <EOL> elif self . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" or self . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> self . mainUI . __dict__ [ self . widget . name ] = Table ( ) <EOL> self . thiswidget = self . mainUI . __dict__ [ self . widget . name ] <EOL> elif self . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> self . mainUI . __dict__ [ self . widget . name ] = CheckBox ( ) <EOL> self . thiswidget = self . mainUI . __dict__ [ self . widget . name ] <EOL> else : <EOL> print "<STR_LIT>" , self . widget [ '<STR_LIT:class>' ] <EOL> self . wHandleSize = False <EOL> return <EOL> if self . wHandleSize : <EOL> self . handleSize ( ) <EOL> if self . parent . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> if self . parent . widget . layout != None : <EOL> pnname = self . parent . widget . layout . name <EOL> pnclass = self . parent . widget . layout [ '<STR_LIT:class>' ] <EOL> else : <EOL> pnname = self . parent . widget . name <EOL> pnclass = self . parent . widget [ '<STR_LIT:class>' ] <EOL> if self . widget . layout != None : <EOL> thname = self . widget . layout . name <EOL> thclass = self . widget . layout [ '<STR_LIT:class>' ] <EOL> else : <EOL> thname = self . widget . name <EOL> thclass = self . widget [ '<STR_LIT:class>' ] <EOL> else : <EOL> pnname = self . parent . widget . name <EOL> pnclass = self . parent . widget [ '<STR_LIT:class>' ] <EOL> thname = self . widget . name <EOL> thclass = self . widget [ '<STR_LIT:class>' ] <EOL> if pnclass == "<STR_LIT>" or pnclass == "<STR_LIT>" or pnclass == "<STR_LIT>" or pnclass == "<STR_LIT>" : <EOL> if pnclass != "<STR_LIT>" : <EOL> if self . parent . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" or self . parent . widget [ '<STR_LIT:class>' ] == "<STR_LIT>" : <EOL> self . parent . thiswidget . addTab ( self . thiswidget ) <EOL> if thclass == "<STR_LIT>" : <EOL> self . thiswidget = self . mainUI . __dict__ [ thname ] <EOL> else : <EOL> self . parent . thiswidget . addComponent ( self . thiswidget ) <EOL> if thclass == "<STR_LIT>" : <EOL> self . thiswidget = self . mainUI . __dict__ [ thname ] <EOL> else : <EOL> if pnclass != "<STR_LIT>" and pnclass != "<STR_LIT>" and pnclass != "<STR_LIT>" : <EOL> if self . parent . geometry != None : <EOL> print "<STR_LIT>" , thname , "<STR_LIT:to>" , pnname , "<STR_LIT>" , pnclass , "<STR_LIT>" , type ( self . parent . thiswidget ) <EOL> xpos = "<STR_LIT>" % ( ( ( float ( self . geometry . rect . x ) + <NUM_LIT> ) / ( float ( self . parent . geometry . rect . width ) + <NUM_LIT> ) ) * <NUM_LIT:100> ) <EOL> ypos = "<STR_LIT>" % ( ( ( float ( self . geometry . rect . y ) + <NUM_LIT> ) / ( float ( self . parent . geometry . rect . height ) + <NUM_LIT> ) ) * <NUM_LIT:100> ) <EOL> self . parent . thiswidget . addComponent ( self . thiswidget , <EOL> "<STR_LIT>" + str ( ypos ) + "<STR_LIT>" + str ( xpos ) + "<STR_LIT:%>" ) <EOL> else : <EOL> print "<STR_LIT>" , thname , "<STR_LIT:to>" , pnname , "<STR_LIT>" , pnclass , "<STR_LIT>" , type ( self . parent . thiswidget ) <EOL> self . parent . thiswidget . addComponent ( self . thiswidget , <EOL> "<STR_LIT>" + str ( self . geometry . rect . y ) + "<STR_LIT>" + str ( self . geometry . rect . x ) + "<STR_LIT:%>" ) <EOL> if thclass == "<STR_LIT>" : <EOL> self . thiswidget = self . mainUI . __dict__ [ thname ] <EOL> def handleSize ( self ) : <EOL> if self . geometry == None : <EOL> self . geometry = self . parent . geometry <EOL> wdth = "<STR_LIT>" % ( ( float ( self . parent . geometry . rect . width ) / float ( self . parent . geometry . rect . width ) ) * <NUM_LIT:100> ) <EOL> hgth = "<STR_LIT>" % ( ( float ( self . parent . geometry . rect . height ) / float ( self . parent . geometry . rect . height ) ) * <NUM_LIT:100> ) <EOL> if self . widget . name == "<STR_LIT>" : <EOL> print self . widget . name , wdth , hgth <EOL> if self . widget [ '<STR_LIT:class>' ] != "<STR_LIT>" : <EOL> self . thiswidget . setHeight ( str ( hgth ) + "<STR_LIT:%>" ) <EOL> else : <EOL> self . thiswidget . setHeight ( str ( self . parent . geometry . rect . height ) + "<STR_LIT>" ) <EOL> self . thiswidget . setWidth ( str ( wdth ) + "<STR_LIT:%>" ) <EOL> else : <EOL> wdth = "<STR_LIT>" % ( ( float ( self . geometry . rect . width ) / ( float ( self . parent . geometry . rect . width ) - float ( self . geometry . rect . x ) ) ) * <NUM_LIT:100> ) <EOL> hgth = "<STR_LIT>" % ( ( float ( self . geometry . rect . height ) / ( float ( self . parent . geometry . rect . height ) - float ( self . geometry . rect . y ) ) ) * <NUM_LIT:100> ) <EOL> if self . widget . name == "<STR_LIT>" : <EOL> print self . widget . name , wdth , hgth <EOL> if self . widget [ '<STR_LIT:class>' ] != "<STR_LIT>" : <EOL> self . thiswidget . setHeight ( str ( hgth ) + "<STR_LIT:%>" ) <EOL> self . thiswidget . setWidth ( str ( wdth ) + "<STR_LIT:%>" ) <EOL> def getTitle ( self ) : <EOL> found , title = self . getProperty ( "<STR_LIT:title>" ) <EOL> if found : title = title . string <EOL> else : title = "<STR_LIT>" <EOL> return title <EOL> def getText ( self ) : <EOL> found , thetext = self . getProperty ( "<STR_LIT:text>" ) <EOL> if found : thetext = thetext . string <EOL> else : thetext = "<STR_LIT>" <EOL> return thetext <EOL> def getHtml ( self ) : <EOL> found , html = self . getProperty ( "<STR_LIT:html>" ) <EOL> if found : html = html . string <EOL> else : html = "<STR_LIT>" <EOL> return html <EOL> def makeChildren ( self ) : <EOL> if self . isLayout : <EOL> if not self . widget . layout . item is None : <EOL> for awidget in self . widget . layout . item : <EOL> aWidget ( self , awidget . widget , self . mainUI ) <EOL> else : <EOL> if not self . widget . widget is None : <EOL> for awidget in self . widget . widget : <EOL> aWidget ( self , awidget , self . mainUI ) <EOL> if self . main : <EOL> pass <EOL> def getProperty ( self , name , ref = "<STR_LIT:string>" ) : <EOL> widget = self . widget <EOL> if cfkey ( widget , "<STR_LIT>" ) : <EOL> if type ( widget . property ) is list : <EOL> for prop in widget . property : <EOL> if prop . name == name : <EOL> return True , prop <EOL> return False , "<STR_LIT>" <EOL> else : <EOL> try : <EOL> if widget . property . name == name : <EOL> return True , widget . property <EOL> else : <EOL> return False , "<STR_LIT>" <EOL> except : <EOL> return False , "<STR_LIT>" <EOL> else : <EOL> return False , "<STR_LIT>" <EOL> def getSProperty ( self , widget , name , ref = "<STR_LIT:string>" ) : <EOL> if cfkey ( widget , "<STR_LIT>" ) : <EOL> if type ( widget . property ) is list : <EOL> for prop in widget . property : <EOL> if prop . name == name : <EOL> ret = str ( getattr ( prop , ref ) ) <EOL> return ret <EOL> return "<STR_LIT>" <EOL> else : <EOL> try : <EOL> if widget . property . name == name : <EOL> ret = str ( getattr ( widget . property , ref ) ) <EOL> return ret <EOL> else : <EOL> return "<STR_LIT>" <EOL> except : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> conv = QT2Muntjac ( ) <EOL> conv . translateUI ( '<STR_LIT>' ) </s>
