home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / Onboard / OnboardCommon.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  16.3 KB  |  499 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from xml.dom import minidom
  5. import sys
  6. import gobject
  7. gobject.threads_init()
  8. import gtk
  9. import re
  10. import string
  11. import virtkey
  12. import gconf
  13. import gettext
  14. import os.path as os
  15. import gettext
  16. from gettext import gettext as _
  17. from Onboard.Keyboard import Keyboard
  18. from Onboard.Key import *
  19. from Onboard.Pane import Pane
  20. from Onboard.KbdWindow import KbdWindow
  21. import Onboard.utils as utils
  22. app = 'onboard'
  23. gettext.textdomain(app)
  24. gettext.bindtextdomain(app)
  25. DEFAULT_FONTSIZE = 25
  26.  
  27. class OnboardGtk(object):
  28.     """
  29.     This class is a mishmash of things that I didn't have time to refactor in to seperate classes.
  30.     It needs a lot of work.
  31.     The name comes from onboards original working name of simple onscreen keyboard.  
  32.     """
  33.     
  34.     def __init__(self):
  35.         self.mods = {
  36.             1: 0,
  37.             2: 0,
  38.             4: 0,
  39.             8: 0,
  40.             16: 0,
  41.             32: 0,
  42.             64: 0,
  43.             128: 0 }
  44.         self.SOK_INSTALL_DIR = '/usr/share/onboard'
  45.         sys.path.append(os.path.join(self.SOK_INSTALL_DIR, 'scripts'))
  46.         self.vk = virtkey.virtkey()
  47.         self.gconfClient = gconf.client_get_default()
  48.         self.gconfClient.add_dir('/apps/sok', gconf.CLIENT_PRELOAD_NONE)
  49.         filename = self.gconfClient.get_string('/apps/sok/layout_filename')
  50.         if not filename or not os.path.exists(filename):
  51.             self.load_default_layout()
  52.         else:
  53.             self.load_layout(filename)
  54.         self.macros = self.gconfClient.get_list('/apps/sok/macros', gconf.VALUE_STRING)
  55.         self.window = KbdWindow(self)
  56.         self.window.set_keyboard(self.keyboard)
  57.         uiManager = gtk.UIManager()
  58.         actionGroup = gtk.ActionGroup('UIManagerExample')
  59.         actionGroup.add_actions([
  60.             ('Quit', gtk.STOCK_QUIT, _('_Quit'), None, _('Quit onBoard'), self.quit),
  61.             ('Settings', gtk.STOCK_PREFERENCES, _('_Settings'), None, _('Show settings'), self.cb_settings_item_clicked)])
  62.         uiManager.insert_action_group(actionGroup, 0)
  63.         uiManager.add_ui_from_string('<ui>\n                        <popup>\n                            <menuitem action="Settings"/>\n                            <menuitem action="Quit"/>\n                        </popup>\n                    </ui>')
  64.         trayMenu = uiManager.get_widget('/ui/popup')
  65.         
  66.         try:
  67.             self.statusIcon = gtk.status_icon_new_from_file('%s/onboard.svg' % self.SOK_INSTALL_DIR)
  68.             self.statusIcon.connect('activate', self.cb_status_icon_clicked)
  69.             self.statusIcon.connect('popup-menu', self.cb_status_icon_menu, trayMenu)
  70.             if not self.gconfClient.get_bool('/apps/sok/trayicon'):
  71.                 self.hide_status_icon()
  72.             else:
  73.                 self.show_status_icon()
  74.         except AttributeError:
  75.             print _('You need pygtk 2.10 or above for the system tray icon')
  76.  
  77.         self.window.hidden = False
  78.         self.window.show_all()
  79.         self.gconfClient.notify_add('/apps/sok/sizeX', self.window.do_set_size)
  80.         self.gconfClient.notify_add('/apps/sok/layout_filename', self.do_set_layout)
  81.         self.gconfClient.notify_add('/apps/sok/macros', self.do_change_macros)
  82.         self.gconfClient.notify_add('/apps/sok/scanning_interval', self.do_change_scanningInterval)
  83.         self.gconfClient.notify_add('/apps/sok/scanning', self.do_change_scanning)
  84.         self.gconfClient.notify_add('/apps/sok/trayicon', self.do_set_trayicon)
  85.         scanning = self.gconfClient.get_bool('/apps/sok/scanning')
  86.         if scanning:
  87.             self.scanning = scanning
  88.             self.keyboard.reset_scan()
  89.         else:
  90.             self.scanning = False
  91.         scanningInterval = self.gconfClient.get_int('/apps/sok/scanning_interval')
  92.         if scanningInterval:
  93.             self.scanningInterval = scanningInterval
  94.         else:
  95.             self.gconfClient.set_int('/apps/sok/scanning_interval', 750)
  96.         gtk.main()
  97.         self.clean()
  98.  
  99.     
  100.     def cb_settings_item_clicked(self, widget):
  101.         '''
  102.         Callback called when setting button clicked in the trayicon menu. 
  103.         '''
  104.         utils.run_script('sokSettings', self)
  105.  
  106.     
  107.     def cb_status_icon_menu(self, status_icon, button, activate_time, trayMenu):
  108.         '''
  109.         Callback called when trayicon right clicked.  Produces menu.
  110.         '''
  111.         trayMenu.popup(None, None, gtk.status_icon_position_menu, button, activate_time, status_icon)
  112.  
  113.     
  114.     def do_set_trayicon(self, cxion_id = None, entry = None, user_data = None, thing = None):
  115.         '''
  116.         Callback called when gconf detects that the gconf key specifying 
  117.         whether the trayicon should be shown or not is changed.
  118.         '''
  119.         if self.gconfClient.get_bool('/apps/sok/trayicon'):
  120.             self.show_status_icon()
  121.         else:
  122.             self.hide_status_icon()
  123.  
  124.     
  125.     def show_status_icon(self):
  126.         '''
  127.         Shows the status icon.  When it is shown we set a wm hint so that
  128.         onboard does not appear in the taskbar.
  129.         '''
  130.         self.statusIcon.set_visible(True)
  131.         self.window.set_property('skip-taskbar-hint', True)
  132.  
  133.     
  134.     def hide_status_icon(self):
  135.         '''
  136.         The opposite of the above.
  137.         '''
  138.         self.statusIcon.set_visible(False)
  139.         self.window.set_property('skip-taskbar-hint', False)
  140.  
  141.     
  142.     def cb_status_icon_clicked(self, widget):
  143.         '''
  144.         Callback called when trayicon clicked.
  145.         Toggles whether onboard window visibile or not.
  146.  
  147.         TODO would be nice if appeared to iconify to taskbar
  148.         '''
  149.         if self.window.hidden:
  150.             self.window.deiconify()
  151.             self.window.hidden = False
  152.         else:
  153.             self.window.iconify()
  154.             self.window.hidden = True
  155.  
  156.     
  157.     def unstick(self):
  158.         for key in self.keyboard.basePane.keys.values():
  159.             if key.on:
  160.                 self.keyboard.release_key(key)
  161.                 continue
  162.         
  163.  
  164.     
  165.     def clean(self):
  166.         self.unstick()
  167.         self.window.hide()
  168.  
  169.     
  170.     def quit(self, widget = None):
  171.         self.clean()
  172.         gtk.main_quit()
  173.  
  174.     
  175.     def do_change_scanning(self, cxion_id, entry, user_data, thing):
  176.         self.scanning = self.gconfClient.get_bool('/apps/sok/scanning')
  177.         self.keyboard.reset_scan()
  178.  
  179.     
  180.     def do_change_scanningInterval(self, cxion_id, entry, user_data, thing):
  181.         self.scanningInterval = self.gconfClient.get_int('/apps/sok/scanningInterval')
  182.  
  183.     
  184.     def do_change_macros(self, client, cxion_id, entry, user_data):
  185.         self.macros = self.gconfClient.get_list('/apps/sok/macros', gconf.VALUE_STRING)
  186.  
  187.     
  188.     def do_set_layout(self, client, cxion_id, entry, user_data):
  189.         self.unstick()
  190.         filename = self.gconfClient.get_string('/apps/sok/layout_filename')
  191.         if os.path.exists(filename):
  192.             self.load_layout(filename)
  193.             self.window.set_keyboard(self.keyboard)
  194.         else:
  195.             self.load_default_layout()
  196.         self.window.set_keyboard(self.keyboard)
  197.  
  198.     
  199.     def hexstring_to_float(self, hexString):
  200.         return float(string.atoi(hexString, 16))
  201.  
  202.     
  203.     def get_sections_keys(self, section, keys, pane, xOffset, yOffset):
  204.         '''gets keys for a specified sections from the XServer.'''
  205.         rows = self.vk.layout_get_keys(section)
  206.         for row in rows:
  207.             for key in row:
  208.                 shape = key['shape']
  209.                 name = key['name'].strip(chr(0))
  210.                 if name in utils.modDic:
  211.                     nkey = RectKey(pane, float(shape[0] + xOffset), float(shape[1] + yOffset), float(shape[2]), float(shape[3]), (0.95, 0.9, 0.85, 1))
  212.                     props = utils.modDic[name]
  213.                     actions = ('', '', '', props[1], '')
  214.                     labels = (props[0], '', '', '', '')
  215.                     sticky = True
  216.                 else:
  217.                     actions = ('', key['keysym'], '', '', '', '')
  218.                     if name in utils.otherDic:
  219.                         nkey = RectKey(pane, float(shape[0] + xOffset), float(shape[1] + yOffset), float(shape[2]), float(shape[3]), (0.85, 0.8, 0.65, 1))
  220.                         labels = (utils.otherDic[name], '', '', '', '')
  221.                     else:
  222.                         nkey = RectKey(pane, float(shape[0] + xOffset), float(shape[1] + yOffset), float(shape[2]), float(shape[3]), (0.9, 0.85, 0.7, 1))
  223.                         labDic = key['labels']
  224.                         labels = (labDic[0], labDic[2], labDic[1], labDic[3], labDic[4])
  225.                     sticky = False
  226.                 nkey.set_properties(actions, labels, sticky, 0, 0)
  227.                 keys[name] = nkey
  228.             
  229.         
  230.  
  231.     
  232.     def load_default_layout(self):
  233.         panes = []
  234.         sizeA = self.vk.layout_get_section_size('Alpha')
  235.         sizeK = self.vk.layout_get_section_size('Keypad')
  236.         sizeE = self.vk.layout_get_section_size('Editing')
  237.         sizeF = (294, 94)
  238.         listX = [
  239.             sizeA[0],
  240.             sizeE[0] + sizeK[0] + 20 + 125,
  241.             sizeF[0]]
  242.         listY = [
  243.             sizeA[1] + 1,
  244.             sizeE[1] + 3,
  245.             sizeK[1] + 3,
  246.             64,
  247.             sizeF[1]]
  248.         listX.sort()
  249.         listY.sort()
  250.         sizeX = listX[len(listX) - 1]
  251.         sizeY = listY[len(listY) - 1]
  252.         keys = { }
  253.         pane = Pane(self, 'Alpha', keys, None, float(sizeX), float(sizeY), [
  254.             0,
  255.             0,
  256.             0,
  257.             0.3], DEFAULT_FONTSIZE)
  258.         panes.append(pane)
  259.         self.get_sections_keys('Alpha', keys, pane, 0, 0)
  260.         keys = { }
  261.         pane = Pane(self, 'Editing', keys, None, float(sizeX), float(sizeY), [
  262.             0.3,
  263.             0.3,
  264.             0.7,
  265.             0.3], DEFAULT_FONTSIZE)
  266.         panes.append(pane)
  267.         self.get_sections_keys('Editing', keys, pane, 0, 2)
  268.         self.get_sections_keys('Keypad', keys, pane, sizeE[0] + 20, 2)
  269.         for r in range(3):
  270.             for c in range(3):
  271.                 n = c + r * 3
  272.                 mkey = RectKey(pane, sizeE[0] + sizeK[0] + 45 + c * 30, 7 + r * 28, 25, 24, (0.5, 0.5, 0.8, 1))
  273.                 mkey.set_properties(('', '', '', '', '%d' % n), (_('Snippit\n%d') % n, '', '', '', ''), False, 0, 0)
  274.                 keys['m%d' % n] = mkey
  275.             
  276.         
  277.         keys = { }
  278.         pane = Pane(self, 'Functions', keys, None, float(sizeX), float(sizeY), [
  279.             0.6,
  280.             0.3,
  281.             0.7,
  282.             0.3], DEFAULT_FONTSIZE)
  283.         panes.append(pane)
  284.         y = 0
  285.         for n in range(len(utils.funcKeys)):
  286.             if n >= 8:
  287.                 y = 27
  288.                 m = n - 8
  289.             else:
  290.                 m = n
  291.             fkey = RectKey(pane, 5 + m * 30, 5 + y, 25, 24, (0.5, 0.5, 0.8, 1))
  292.             fkey.set_properties(('', utils.funcKeys[n][1], '', ''), (utils.funcKeys[n][0], '', '', '', ''), False, 0, 0)
  293.             keys[utils.funcKeys[n][0]] = fkey
  294.         
  295.         settingsKey = RectKey(pane, 5, 61, 60, 30, (0.95, 0.5, 0.5, 1))
  296.         settingsKey.set_properties(('', '', '', '', '', 'sokSettings'), (_('Settings'), '', '', '', ''), False, 0, 0)
  297.         keys['settings'] = settingsKey
  298.         switchingKey = RectKey(pane, 70, 61, 60, 30, (0.95, 0.5, 0.5, 1))
  299.         switchingKey.set_properties(('', '', '', '', '', 'switchButtons'), (_('Switch\nButtons'), '', '', '', ''), False, 0, 0)
  300.         keys['switchButtons'] = switchingKey
  301.         basePane = panes[0]
  302.         otherPanes = panes[1:]
  303.         self.keyboard = Keyboard(self, basePane, otherPanes)
  304.         for pane in panes:
  305.             pane.set_DrawingArea(self.keyboard)
  306.         
  307.  
  308.     
  309.     def load_keys(self, doc, keys):
  310.         for key in doc.getElementsByTagName('key'):
  311.             
  312.             try:
  313.                 if key.attributes['id'].value in keys:
  314.                     actions = [
  315.                         '',
  316.                         '',
  317.                         '',
  318.                         '',
  319.                         '',
  320.                         '']
  321.                     if key.hasAttribute('char'):
  322.                         actions[0] = key.attributes['char'].value
  323.                     elif key.hasAttribute('keysym'):
  324.                         value = key.attributes['keysym'].value
  325.                         if value[1] == 'x':
  326.                             actions[1] = string.atoi(value, 16)
  327.                         else:
  328.                             actions[1] = string.atoi(value, 10)
  329.                     elif key.hasAttribute('press'):
  330.                         actions[2] = key.attributes['press'].value
  331.                     elif key.hasAttribute('modifier'):
  332.                         actions[3] = modifiers[key.attributes['modifier'].value]
  333.                     elif key.hasAttribute('macro'):
  334.                         actions[4] = key.attributes['macro'].value
  335.                     elif key.hasAttribute('script'):
  336.                         actions[5] = key.attributes['script'].value
  337.                     
  338.                     labels = [
  339.                         '',
  340.                         '',
  341.                         '',
  342.                         '',
  343.                         '']
  344.                     if key.hasAttribute('label'):
  345.                         labels[0] = key.attributes['label'].value
  346.                     
  347.                     if key.hasAttribute('cap_label'):
  348.                         labels[1] = key.attributes['cap_label'].value
  349.                     
  350.                     if key.hasAttribute('shift_label'):
  351.                         labels[2] = key.attributes['shift_label'].value
  352.                     
  353.                     if key.hasAttribute('altgr_label'):
  354.                         labels[3] = key.attributes['altgr_label'].value
  355.                     
  356.                     if key.hasAttribute('altgrNshift_label'):
  357.                         labels[4] = key.attributes['altgrNshift_label'].value
  358.                     
  359.                     if key.hasAttribute('font_offset_x'):
  360.                         offsetX = float(key.attributes['font_offset_x'].value)
  361.                     else:
  362.                         offsetX = 0
  363.                     if key.hasAttribute('font_offset_y'):
  364.                         offsetY = float(key.attributes['font_offset_y'].value)
  365.                     else:
  366.                         offsetY = 0
  367.                     stickyString = key.attributes['sticky'].value
  368.                     if stickyString == 'true':
  369.                         sticky = True
  370.                     else:
  371.                         sticky = False
  372.                     keys[key.attributes['id'].value].set_properties(actions, labels, sticky, offsetX, offsetY)
  373.             continue
  374.             except KeyError:
  375.                 strerror = None
  376.                 print 'key missing id'
  377.                 continue
  378.             
  379.  
  380.         
  381.  
  382.     
  383.     def load_layout(self, kblang):
  384.         kbfolder = os.path.dirname(kblang)
  385.         f = open(kblang)
  386.         langdoc = minidom.parse(f).documentElement
  387.         f.close()
  388.         panes = []
  389.         for paneXML in langdoc.getElementsByTagName('pane'):
  390.             
  391.             try:
  392.                 path = '%s/%s' % (kbfolder, paneXML.attributes['filename'].value)
  393.                 f = open(path)
  394.                 
  395.                 try:
  396.                     svgdoc = minidom.parse(f).documentElement
  397.                     keys = { }
  398.                     
  399.                     try:
  400.                         viewPortSizeX = float(svgdoc.attributes['width'].value)
  401.                         viewPortSizeY = float(svgdoc.attributes['height'].value)
  402.                     except ValueError:
  403.                         print 'Units for canvas height and width must be px.  In the svg file this corresponds with having no units after the height and width'
  404.  
  405.                     paneBackground = [
  406.                         0,
  407.                         0,
  408.                         0,
  409.                         0]
  410.                     if paneXML.hasAttribute('backgroundRed'):
  411.                         paneBackground[0] = paneXML.attributes['backgroundRed'].value
  412.                     
  413.                     if paneXML.hasAttribute('backgroundGreen'):
  414.                         paneBackground[1] = paneXML.attributes['backgroundGreen'].value
  415.                     
  416.                     if paneXML.hasAttribute('backgroundBlue'):
  417.                         paneBackground[2] = paneXML.attributes['backgroundBlue'].value
  418.                     
  419.                     if paneXML.hasAttribute('backgroundAlpha'):
  420.                         paneBackground[3] = paneXML.attributes['backgroundAlpha'].value
  421.                     
  422.                     columns = []
  423.                     if paneXML.hasAttribute('font'):
  424.                         fontSize = string.atoi(paneXML.attributes['font'].value)
  425.                     else:
  426.                         fontSize = DEFAULT_FONTSIZE
  427.                     pane = Pane(self, paneXML.attributes['id'].value, keys, columns, viewPortSizeX, viewPortSizeY, paneBackground, fontSize)
  428.                     for rect in svgdoc.getElementsByTagName('rect'):
  429.                         id = rect.attributes['id'].value
  430.                         styleString = rect.attributes['style'].value
  431.                         result = re.search('(fill:#\\d?\\D?\\d?\\D?\\d?\\D?\\d?\\D?\\d?\\D?\\d?\\D?;)', styleString).groups()[0]
  432.                         rgba = [
  433.                             self.hexstring_to_float(result[6:8]) / 255,
  434.                             self.hexstring_to_float(result[8:10]) / 255,
  435.                             self.hexstring_to_float(result[10:12]) / 255,
  436.                             1]
  437.                         keys[id] = RectKey(pane, float(rect.attributes['x'].value), float(rect.attributes['y'].value), float(rect.attributes['width'].value), float(rect.attributes['height'].value), rgba)
  438.                     
  439.                     for path in svgdoc.getElementsByTagName('path'):
  440.                         id = path.attributes['id'].value
  441.                         styleString = rect.attributes['style'].value
  442.                         result = re.search('(fill:#\\d?\\D?\\d?\\D?\\d?\\D?\\d?\\D?\\d?\\D?\\d?\\D?;)', styleString).groups()[0]
  443.                         rgba = (self.hexstring_to_float(result[6:8]) / 255, self.hexstring_to_float(result[8:10]) / 255, self.hexstring_to_float(result[10:12]) / 255, 1)
  444.                         dList = path.attributes['d'].value.split(' ')
  445.                         dList = dList[1:-2]
  446.                         coordList = []
  447.                         for d in dList:
  448.                             l = d.split(',')
  449.                             for p in l:
  450.                                 if len(p) == 1:
  451.                                     coordList.append(p)
  452.                                     continue
  453.                                 coordList.append(float(p))
  454.                             
  455.                         
  456.                         keys[id] = LineKey(pane, coordList, rgba)
  457.                     
  458.                     svgdoc.unlink()
  459.                     self.load_keys(langdoc, keys)
  460.                     
  461.                     try:
  462.                         for columnXML in paneXML.getElementsByTagName('column'):
  463.                             column = []
  464.                             columns.append(column)
  465.                             for scanKey in columnXML.getElementsByTagName('scankey'):
  466.                                 column.append(keys[scanKey.attributes['id'].value])
  467.                             
  468.                     except KeyError:
  469.                         strerror = None
  470.                         print 'require %s key, appears in scanning only' % strerror
  471.  
  472.                     panes.append(pane)
  473.                 except KeyError:
  474.                     strerror = None
  475.                     print _('require %s') % strerror
  476.  
  477.                 f.close()
  478.             continue
  479.             except KeyError:
  480.                 print _('require filename in pane')
  481.                 continue
  482.             
  483.  
  484.         
  485.         langdoc.unlink()
  486.         basePane = panes[0]
  487.         otherPanes = panes[1:]
  488.         self.keyboard = Keyboard(self, basePane, otherPanes)
  489.         for pane in panes:
  490.             pane.set_DrawingArea(self.keyboard)
  491.         
  492.  
  493.  
  494. if __name__ == '__main__':
  495.     s = Sok()
  496.     gtk.main()
  497.     s.clean()
  498.  
  499.