home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2306 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  15.6 KB  |  411 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import logging
  5. import sys
  6. from PyQt4.uic.exceptions import UnsupportedPropertyError
  7. from PyQt4.uic.icon_cache import IconCache
  8. if sys.hexversion >= 50331648:
  9.     from PyQt4.uic.port_v3.ascii_upper import ascii_upper
  10. else:
  11.     from PyQt4.uic.port_v2.ascii_upper import ascii_upper
  12. logger = logging.getLogger(__name__)
  13. DEBUG = logger.debug
  14. QtCore = None
  15. QtGui = None
  16.  
  17. def int_list(prop):
  18.     return [ int(child.text) for child in prop ]
  19.  
  20.  
  21. def float_list(prop):
  22.     return [ float(child.text) for child in prop ]
  23.  
  24.  
  25. bool_ = lambda v: v == 'true'
  26.  
  27. def needsWidget(func):
  28.     func.needsWidget = True
  29.     return func
  30.  
  31.  
  32. class Properties(object):
  33.     
  34.     def __init__(self, factory, QtCore_mod, QtGui_mod):
  35.         global QtGui, QtCore
  36.         QtGui = QtGui_mod
  37.         QtCore = QtCore_mod
  38.         self.factory = factory
  39.         self.reset()
  40.  
  41.     
  42.     def reset(self):
  43.         self.buddies = []
  44.         self.delayed_props = []
  45.         self.icon_cache = IconCache(self.factory, QtGui)
  46.  
  47.     
  48.     def _pyEnumMember(self, cpp_name):
  49.         
  50.         try:
  51.             (prefix, membername) = cpp_name.split('::')
  52.             DEBUG(membername)
  53.             if prefix == 'Qt':
  54.                 return getattr(QtCore.Qt, membername)
  55.             return getattr(getattr(QtGui, prefix), membername)
  56.         except ValueError:
  57.             pass
  58.  
  59.         
  60.         try:
  61.             return getattr(QtCore.Qt, cpp_name)
  62.         except AttributeError:
  63.             return getattr(getattr(QtGui, self.wclass), cpp_name)
  64.  
  65.  
  66.     
  67.     def _set(self, prop):
  68.         expr = [ self._pyEnumMember(v) for v in prop.text.split('|') ]
  69.         value = expr[0]
  70.         for v in expr[1:]:
  71.             value |= v
  72.         
  73.         return value
  74.  
  75.     
  76.     def _enum(self, prop):
  77.         return self._pyEnumMember(prop.text)
  78.  
  79.     
  80.     def _number(self, prop):
  81.         return int(prop.text)
  82.  
  83.     _uInt = _longLong = _uLongLong = _number
  84.     
  85.     def _double(self, prop):
  86.         return float(prop.text)
  87.  
  88.     
  89.     def _bool(self, prop):
  90.         return prop.text == 'true'
  91.  
  92.     
  93.     def _stringlist(self, prop):
  94.         return [ self._string(p, notr = 'true') for p in prop ]
  95.  
  96.     
  97.     def _string(self, prop, notr = None):
  98.         if prop.get('notr', notr) == 'true':
  99.             return self._cstring(prop)
  100.         if prop.text is None:
  101.             return ''
  102.         return QtGui.QApplication.translate(self.uiname, prop.text, None, QtGui.QApplication.UnicodeUTF8)
  103.  
  104.     _char = _string
  105.     
  106.     def _cstring(self, prop):
  107.         return str(prop.text)
  108.  
  109.     
  110.     def _color(self, prop):
  111.         args = int_list(prop)
  112.         alpha = int(prop.get('alpha', '255'))
  113.         if alpha != 255:
  114.             args.append(alpha)
  115.         
  116.         return QtGui.QColor(*args)
  117.  
  118.     
  119.     def _point(self, prop):
  120.         return QtCore.QPoint(*int_list(prop))
  121.  
  122.     
  123.     def _pointf(self, prop):
  124.         return QtCore.QPointF(*float_list(prop))
  125.  
  126.     
  127.     def _rect(self, prop):
  128.         return QtCore.QRect(*int_list(prop))
  129.  
  130.     
  131.     def _rectf(self, prop):
  132.         return QtCore.QRectF(*float_list(prop))
  133.  
  134.     
  135.     def _size(self, prop):
  136.         return QtCore.QSize(*int_list(prop))
  137.  
  138.     
  139.     def _sizef(self, prop):
  140.         return QtCore.QSizeF(*float_list(prop))
  141.  
  142.     
  143.     def _pixmap(self, prop):
  144.         if prop.text:
  145.             return QtGui.QPixmap(prop.text.replace('\\', '\\\\'))
  146.  
  147.     
  148.     def _iconset(self, prop):
  149.         return self.icon_cache.get_icon(prop)
  150.  
  151.     
  152.     def _url(self, prop):
  153.         return QtCore.QUrl(prop[0].text)
  154.  
  155.     
  156.     def _locale(self, prop):
  157.         lang = getattr(QtCore.QLocale, prop.attrib['language'])
  158.         country = getattr(QtCore.QLocale, prop.attrib['country'])
  159.         return QtCore.QLocale(lang, country)
  160.  
  161.     
  162.     def _cursor(self, prop):
  163.         return QtGui.QCursor(QtCore.Qt.CursorShape(int(prop.text)))
  164.  
  165.     
  166.     def _date(self, prop):
  167.         return QtCore.QDate(*int_list(prop))
  168.  
  169.     
  170.     def _datetime(self, prop):
  171.         args = int_list(prop)
  172.         return QtCore.QDateTime(QtCore.QDate(*args[-3:]), QtCore.QTime(*args[:-3]))
  173.  
  174.     
  175.     def _time(self, prop):
  176.         return QtCore.QTime(*int_list(prop))
  177.  
  178.     
  179.     def _gradient(self, prop):
  180.         name = 'gradient'
  181.         gtype = prop.get('type', '')
  182.         if gtype == 'LinearGradient':
  183.             startx = float(prop.get('startx'))
  184.             starty = float(prop.get('starty'))
  185.             endx = float(prop.get('endx'))
  186.             endy = float(prop.get('endy'))
  187.             gradient = self.factory.createQObject('QLinearGradient', name, (startx, starty, endx, endy), is_attribute = False)
  188.         elif gtype == 'ConicalGradient':
  189.             centralx = float(prop.get('centralx'))
  190.             centraly = float(prop.get('centraly'))
  191.             angle = float(prop.get('angle'))
  192.             gradient = self.factory.createQObject('QConicalGradient', name, (centralx, centraly, angle), is_attribute = False)
  193.         elif gtype == 'RadialGradient':
  194.             centralx = float(prop.get('centralx'))
  195.             centraly = float(prop.get('centraly'))
  196.             radius = float(prop.get('radius'))
  197.             focalx = float(prop.get('focalx'))
  198.             focaly = float(prop.get('focaly'))
  199.             gradient = self.factory.createQObject('QRadialGradient', name, (centralx, centraly, radius, focalx, focaly), is_attribute = False)
  200.         else:
  201.             raise UnsupportedPropertyError(prop.tag)
  202.         spread = (gtype == 'LinearGradient').get('spread')
  203.         if spread:
  204.             gradient.setSpread(getattr(QtGui.QGradient, spread))
  205.         
  206.         cmode = prop.get('coordinatemode')
  207.         if cmode:
  208.             gradient.setCoordinateMode(getattr(QtGui.QGradient, cmode))
  209.         
  210.         for gstop in prop:
  211.             if gstop.tag != 'gradientstop':
  212.                 raise UnsupportedPropertyError(gstop.tag)
  213.             gstop.tag != 'gradientstop'
  214.             position = float(gstop.get('position'))
  215.             color = self._color(gstop[0])
  216.             gradient.setColorAt(position, color)
  217.         
  218.         return name
  219.  
  220.     
  221.     def _palette(self, prop):
  222.         palette = self.factory.createQObject('QPalette', 'palette', (), is_attribute = False)
  223.         for palette_elem in prop:
  224.             sub_palette = getattr(QtGui.QPalette, palette_elem.tag.title())
  225.             for role, color in enumerate(palette_elem):
  226.                 if color.tag == 'color':
  227.                     palette.setColor(sub_palette, QtGui.QPalette.ColorRole(role), self._color(color))
  228.                     continue
  229.                 if color.tag == 'colorrole':
  230.                     role = getattr(QtGui.QPalette, color.get('role'))
  231.                     brushstyle = color[0].get('brushstyle')
  232.                     if brushstyle in ('LinearGradientPattern', 'ConicalGradientPattern', 'RadialGradientPattern'):
  233.                         gradient = self._gradient(color[0][0])
  234.                         brush = self.factory.createQObject('QBrush', 'brush', (gradient,), is_attribute = False)
  235.                     else:
  236.                         color = self._color(color[0][0])
  237.                         brush = self.factory.createQObject('QBrush', 'brush', (color,), is_attribute = False)
  238.                         brushstyle = getattr(QtCore.Qt, brushstyle)
  239.                         brush.setStyle(brushstyle)
  240.                     palette.setBrush(sub_palette, role, brush)
  241.                     continue
  242.                 raise UnsupportedPropertyError(color.tag)
  243.             
  244.         
  245.         return palette
  246.  
  247.     
  248.     def _sizepolicy(self, prop, widget):
  249.         values = [ int(child.text) for child in prop ]
  250.         sizePolicy = self.factory.createQObject('QSizePolicy', 'sizePolicy', (hsizetype, vsizetype), is_attribute = False)
  251.         sizePolicy.setHorizontalStretch(horstretch)
  252.         sizePolicy.setVerticalStretch(verstretch)
  253.         sizePolicy.setHeightForWidth(widget.sizePolicy().hasHeightForWidth())
  254.         return sizePolicy
  255.  
  256.     _sizepolicy = needsWidget(_sizepolicy)
  257.     _font_attributes = (('Family', str), ('PointSize', int), ('Weight', int), ('Italic', bool_), ('Underline', bool_), ('StrikeOut', bool_), ('Bold', bool_))
  258.     
  259.     def _font(self, prop):
  260.         newfont = self.factory.createQObject('QFont', 'font', (), is_attribute = False)
  261.         for attr, converter in self._font_attributes:
  262.             v = prop.findtext('./%s' % (attr.lower(),))
  263.             if v is None:
  264.                 continue
  265.             
  266.             getattr(newfont, 'set%s' % (attr,))(converter(v))
  267.         
  268.         return newfont
  269.  
  270.     
  271.     def _cursorShape(self, prop):
  272.         return getattr(QtCore.Qt, prop.text)
  273.  
  274.     
  275.     def convert(self, prop, widget = None):
  276.         
  277.         try:
  278.             func = getattr(self, '_' + prop[0].tag)
  279.         except AttributeError:
  280.             raise UnsupportedPropertyError(prop[0].tag)
  281.  
  282.         args = { }
  283.         if getattr(func, 'needsWidget', False):
  284.             args['widget'] = widget
  285.         
  286.         return func(prop[0], **args)
  287.  
  288.     
  289.     def _getChild(self, elem_tag, elem, name, default = None):
  290.         for prop in elem.findall(elem_tag):
  291.             if prop.attrib['name'] == name:
  292.                 return self.convert(prop)
  293.         else:
  294.             return default
  295.         return prop.attrib['name'] == name
  296.  
  297.     
  298.     def getProperty(self, elem, name, default = None):
  299.         return self._getChild('property', elem, name, default)
  300.  
  301.     
  302.     def getAttribute(self, elem, name, default = None):
  303.         return self._getChild('attribute', elem, name, default)
  304.  
  305.     
  306.     def setProperties(self, widget, elem):
  307.         
  308.         try:
  309.             self.wclass = elem.attrib['class']
  310.         except KeyError:
  311.             pass
  312.  
  313.         for prop in elem.findall('property'):
  314.             prop_name = prop.attrib['name']
  315.             DEBUG('setting property %s' % (prop_name,))
  316.             
  317.             try:
  318.                 stdset = bool(int(prop.attrib['stdset']))
  319.             except KeyError:
  320.                 stdset = True
  321.  
  322.             if not stdset:
  323.                 self._setViaSetProperty(widget, prop)
  324.                 continue
  325.             if hasattr(self, prop_name):
  326.                 getattr(self, prop_name)(widget, prop)
  327.                 continue
  328.             prop_value = self.convert(prop, widget)
  329.             if prop_value is not None:
  330.                 getattr(widget, 'set%s%s' % (ascii_upper(prop_name[0]), prop_name[1:]))(prop_value)
  331.                 continue
  332.         
  333.  
  334.     
  335.     def _delay(self, widget, prop):
  336.         prop_value = self.convert(prop)
  337.         if prop_value is not None:
  338.             prop_name = prop.attrib['name']
  339.             self.delayed_props.append((getattr(widget, 'set%s%s' % (ascii_upper(prop_name[0]), prop_name[1:])), prop_value))
  340.         
  341.  
  342.     
  343.     def _setViaSetProperty(self, widget, prop):
  344.         prop_value = self.convert(prop)
  345.         if prop_value is not None:
  346.             widget.setProperty(prop.attrib['name'], prop_value)
  347.         
  348.  
  349.     
  350.     def _ignore(self, widget, prop):
  351.         pass
  352.  
  353.     currentIndex = _delay
  354.     currentRow = _delay
  355.     showDropIndicator = _setViaSetProperty
  356.     intValue = _setViaSetProperty
  357.     value = _setViaSetProperty
  358.     objectName = _ignore
  359.     leftMargin = _ignore
  360.     topMargin = _ignore
  361.     rightMargin = _ignore
  362.     bottomMargin = _ignore
  363.     horizontalSpacing = _ignore
  364.     verticalSpacing = _ignore
  365.     
  366.     def buddy(self, widget, prop):
  367.         buddy_name = prop[0].text
  368.         if buddy_name:
  369.             self.buddies.append((widget, buddy_name))
  370.         
  371.  
  372.     
  373.     def geometry(self, widget, prop):
  374.         if widget.objectName() == self.uiname:
  375.             geom = int_list(prop[0])
  376.             widget.resize(geom[2], geom[3])
  377.         else:
  378.             widget.setGeometry(self._rect(prop[0]))
  379.  
  380.     
  381.     def orientation(self, widget, prop):
  382.         if widget.metaObject().className() == 'QFrame':
  383.             widget.setFrameShape({
  384.                 'Qt::Horizontal': QtGui.QFrame.HLine,
  385.                 'Qt::Vertical': QtGui.QFrame.VLine }[prop[0].text])
  386.             widget.setFrameShadow(QtGui.QFrame.Sunken)
  387.         else:
  388.             widget.setOrientation(self._enum(prop[0]))
  389.  
  390.     
  391.     def isWrapping(self, widget, prop):
  392.         widget.setWrapping(self.convert(prop))
  393.  
  394.     
  395.     def pyuicContentsMargins(self, widget, prop):
  396.         widget.setContentsMargins(*int_list(prop))
  397.  
  398.     
  399.     def pyuicSpacing(self, widget, prop):
  400.         (horiz, vert) = int_list(prop)
  401.         if horiz == vert:
  402.             widget.setSpacing(horiz)
  403.         elif horiz >= 0:
  404.             widget.setHorizontalSpacing(horiz)
  405.         
  406.         if vert >= 0:
  407.             widget.setVerticalSpacing(vert)
  408.         
  409.  
  410.  
  411.