Tuesday, April 3, 2012

py gtk3+ popup qwerty keyboard with caps toggle functionality example




from gi.repository import Gtk, Gdk, GObject

import math


TRACKER_COLORS = [
    (0.0,0.0,1.0), #blue
    (0.0,0.75,0.0), #green
    (0.6,0.0,1.0), #purple
    (1.0,0.5,0), #orange
    (0.5,0.5,0.5), #grey
    (0.6,0.15,0.15), #maroonish
    (0,0.3,0.0), #dark green
    (0,0,0.5), #dark blue
    (0.5,0.35,0.1), #muddy
    (0.5,0.25,0), #dark orange
    ]

def Gtkcolor_to_rgb (c):
    return c.red/float(2**16),c.green/float(2**16),c.blue/float(2**16)

def overlay (color_1, color_2, method=1):
    return color_1[0]+color_2[0]*method,color_1[1]+color_2[1]*method,color_1[2]+color_2[2]*method

ERROR_HIGHLIGHT_COLOR = (1.0,0,0)

BASE_SIZE = 35 # The "normal" size of a box (in pixels)

BORDER_WIDTH = 9.0 # The size of space we leave for a box

BORDER_LINE_WIDTH = 4 # The size of the line we draw around a selected box

LITTLE_LINE_WIDTH = 0.25
NORMAL_LINE_WIDTH = 1 # The size of the line we draw around a box

SPACING_FACTOR = 40 # The size of a box compared (roughly) to the size
                    # of padding -- the larger this is, the smaller
                    # the spaces
SMALL_TO_BIG_FACTOR = 3 # The number of times wider than a small line a big line is.

LATIN_QWERTY_TH_LOWER = {1:'q',2:'w',3:'e',4:'r',5:'t', 6:'y', 7:'u',
                           8: 'i', 9:'o', 10:'p'}
LATIN_QWERTY_TM_LOWER = {1:'a', 2:'s', 3:'d', 4:'f', 5:'g', 6:'h',
                           7:'j', 8:'k', 9:'l'}
LATIN_QWERTY_TL_LOWER = {1:'z', 2:'x', 3: 'c', 4:'v', 5:'b', 6:'n',
                         7:'m', 8:'ABC'}
LATIN_QWERTY_TH_UPPER = {1:'Q',2:'W',3:'E',4:'R',5:'T', 6:'Y', 7:'U',
                           8: 'I', 9:'O', 10:'P'}
LATIN_QWERTY_TM_UPPER = {1:'A', 2:'S', 3:'D', 4:'F', 5:'G', 6:'H',
                           7:'J', 8:'K', 9:'L'}
LATIN_QWERTY_TL_UPPER = {1:'Z', 2:'X', 3: 'C', 4:'V', 5:'B', 6:'N',
                         7:'M', 8:'abc'}
LATIN_QKEY_LOWER = {1: LATIN_QWERTY_TH_LOWER, 2: LATIN_QWERTY_TM_LOWER,
                    3: LATIN_QWERTY_TL_LOWER}
LATIN_QKEY_UPPER = {1: LATIN_QWERTY_TH_UPPER, 2: LATIN_QWERTY_TM_UPPER,
                    3: LATIN_QWERTY_TL_UPPER}

class NumberSelector (Gtk.EventBox):

    __gsignals__ = {
        'changed':(GObject.SIGNAL_RUN_LAST,GObject.TYPE_NONE,()),
        }

    def modifykeys(self):
        if self.caseflg:
            Key_Layout = LATIN_QKEY_UPPER
        else:
            Key_Layout = LATIN_QKEY_LOWER
        for y in Key_Layout:
            for x in Key_Layout[y]:
                b = self.bdict[(y,x)]
                b.set_label(Key_Layout[y][x])
                
                self.bdict[(y,x)] = b
        self.show_all()        
                

    def setkeys(self):
        if self.caseflg:
            Key_Layout = LATIN_QKEY_UPPER
        else:
            Key_Layout = LATIN_QKEY_LOWER
            
        self.bdict = {}
        for y in Key_Layout:
            for x in Key_Layout[y]:
                b = Gtk.Button()
                l = Gtk.Label()
                if Key_Layout[y][x]==self.value:
                    l.set_markup('%s'%Key_Layout[y][x])
                else:
                    l.set_markup('%s'%Key_Layout[y][x])
                b.add(l)
                b.set_relief(Gtk.ReliefStyle.HALF)
                l = b.get_children()[0]
                b.set_border_width(0)
                l.set_padding(0,0)
                l.get_alignment()
                b.connect('clicked',self.alpha_clicked, (y,x))
                self.table.attach(b,x,x+1,y,y+1)
                self.bdict[(y,x)] = b
               
        
    
    def __init__ (self,default=None,upper=9):
        self.caseflg = 0
        self.value = default
        Gtk.EventBox.__init__(self)
        self.table = Gtk.Table()
        self.add(self.table)
        self.setkeys()

        self.connect('key-release-event',self.key_press_cb)
        self.show_all()

    def key_press_cb (self, w, e):
        txt = Gdk.keyval_name(e.keyval)
        if txt.isalpha():
            self.value = txt
            self.emit('changed')
        else:
            self.emit('changed')

    def alpha_clicked (self, button, val):
        
        if self.caseflg:
            Key_Layout = LATIN_QKEY_UPPER
            y,x = val
            aval = Key_Layout[y][x]
            
            
        else:
            Key_Layout = LATIN_QKEY_LOWER                
            y,x = val
            aval = Key_Layout[y][x]
                    
        
        if not aval == 'ABC' and not aval == 'abc':
            self.value = aval
            self.emit('changed')
                
      
        else:
            if aval == 'ABC':
                self.caseflg = 1
                self.modifykeys()
            else:
                self.caseflg = 0 
                self.modifykeys()
                
    def get_value (self):
        return self.value

    def set_value (self,val):
        self.value = val


w = Gtk.Window()
w.set_app_paintable(True)
w.set_type_hint(Gdk.WindowTypeHint.POPUP_MENU)
w.set_decorated(False)
ns = NumberSelector()
def number_changed_cb (b):
    w.destroy()
    print('value:', ns.get_value())
    Gtk.main_quit()

ns.connect('changed',number_changed_cb)
w.grab_focus()
w.connect('focus-out-event',lambda *args: w.destroy())

w.add(ns)
w.show_all()
        
Gtk.main()
r = w.get_allocation()

w.show()      


This is a pop up window for a QWERTY keyboard using latest gtk3+ libraries in python.  Albeit has resizing issues initially when toggling caps, this provides a structural framework for on screen pop up keyboard (e.g., something that might be similarly used in tablet based apps).

 

No comments:

Post a Comment

Oblivion

 Between the fascination of an upcoming pandemic ridden college football season, Taylor Swift, and Kim Kardashian, wildfires, crazier weathe...