home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / onboard / sok.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  14.9 KB  |  470 lines

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