Changeset 33

User picture

Author: Steve Lee

(2010/01/23 17:27) Over 2 years ago

Added print opetion. re #15

Affected files

Updated trunk/config.py Download diff

3233
8
    
8
    
9
    def config_file(self):
9
    def config_file(self):
10
        #was os.path.expanduser("~/.dkeyrc") which is good for *nix
10
        #was os.path.expanduser("~/.dkeyrc") which is good for *nix
11
        cfgfile = ".dkeyrc"
11
        cfgfile = ".dkey"
12
        from win32com.shell import shell, shellcon
12
        from win32com.shell import shell, shellcon
13
        dir = shell.SHGetFolderPath(0,  # according to MSDN we should now use SHGetKnownFolderPath 
13
        dir = shell.SHGetFolderPath(0,  # according to MSDN we should now use SHGetKnownFolderPath 
14
                       shellcon.CSIDL_APPDATA,
14
                       shellcon.CSIDL_APPDATA,
...
...
24
    def __init__(self, main, defaults={}):
24
    def __init__(self, main, defaults={}):
25
        
25
        
26
        object.__init__(self)
26
        object.__init__(self)
27
        self.config = configobj.ConfigObj(self.config_file(),
27
        self.config = configobj.ConfigObj(self.config_file()+'rc',
28
                                          configspec="dkeyrc.val")
28
                                          configspec="dkeyrc.val")
29
        print "validation:", self.config.validate(validate.Validator())
29
        print "validation:", self.config.validate(validate.Validator())
30
        self.main = main
30
        self.main = main
...
...
47
            print "calling set_" + attrname
47
            print "calling set_" + attrname
48
            setter(value)
48
            setter(value)
49
    
49
    
50
    def callgetter(self, attrname):
51
        try:
52
            method = getattr(self.main, "get_" + attrname)
53
        except AttributeError:
54
            return self.config[attrname]
55
        else:
56
            return method()
57
50
    def __setattr__(self, attrname, value):
58
    def __setattr__(self, attrname, value):
51
        if "constructed" not in self.__dict__:
59
        if "constructed" not in self.__dict__:
52
            print "setting real attribute '%s'" % attrname
60
            print "setting real attribute '%s'" % attrname
...
...
55
            self.callsetter(attrname, value)  
63
            self.callsetter(attrname, value)  
56
            self.config[attrname] = value   # always, or at least when the
64
            self.config[attrname] = value   # always, or at least when the
57
                                            # getter is not defined
65
                                            # getter is not defined
58
59
    def __getattr__(self, attrname):
66
    def __getattr__(self, attrname):
60
        try:
67
        return self.callgetter(attrname)
61
            method = getattr(self.main, "get_" + attrname)
68
        
62
        except AttributeError:
63
            return self.config[attrname]
64
        else:
65
            return method()
66
67
    def loadattr(self, attrname):
69
    def loadattr(self, attrname):
68
        setattr(self, attrname, self.config[attrname])
70
        setattr(self, attrname, self.config[attrname])
69
71
70
    def load(self):
72
    def load(self):
71
        for attr in self.config:
73
        for attr in self.config:
72
            self.loadattr(attr)
74
            self.loadattr(attr)
75
            
76
        try:
77
            print_settings = gtk.print_settings_new_from_file(self.config_file()+'ps')
78
        except:
79
            print_settings = gtk.PrintSettings()
80
        setattr(self, 'print_settings', print_settings)
81
73
#         for attr in self.defaults:
82
#         for attr in self.defaults:
74
#             if attr not in self.config:
83
#             if attr not in self.config:
75
#                 self.callsetter(attr, self.defaults[attr])
84
#                 self.callsetter(attr, self.defaults[attr])
...
...
80
            self.config[attr] = getattr(self, attr)
89
            self.config[attr] = getattr(self, attr)
81
        self.config.write()
90
        self.config.write()
82
91
92
        print_settings = getattr(self, 'print_settings')
93
        print_settings.to_file(self.config_file()+'ps');
83
94
95
84
class DKeyConfig(Config):
96
class DKeyConfig(Config):
85
    posprefix = "_position"
97
    posprefix = "_position"
86
    coloursuffix = "_colour"
98
    coloursuffix = "_colour"

Updated trunk/dkey.py Download diff

3233
80
        self.iterhistory = []
80
        self.iterhistory = []
81
        self.pronounce = False
81
        self.pronounce = False
82
        self.placetext = False
82
        self.placetext = False
83
        
84
        self.print_page_setup = None
83
85
84
        # load layout data
86
        # load layout data
85
        gtk.rc_parse("dkeyrc.gtk")           
87
        gtk.rc_parse("dkeyrc.gtk")           
...
...
486
            self.setmarks(enditer, enditer, enditer)
488
            self.setmarks(enditer, enditer, enditer)
487
489
488
    def clearWindow(self):
490
    def clearWindow(self):
489
        print 'clear'
490
        startitr = self.textbuf.get_start_iter();
491
        startitr = self.textbuf.get_start_iter();
491
        self.textbuf.delete(startitr, self.textbuf.get_end_iter())
492
        self.textbuf.delete(startitr, self.textbuf.get_end_iter())
492
        self.setmarks(startitr, startitr, startitr)
493
        self.setmarks(startitr, startitr, startitr)
493
494
495
    def pageSetup(self):
496
        self.print_page_setup = gtk.print_run_page_setup_dialog(self.mainwindow, self.print_page_setup, self.print_settings)
497
498
    def printWindow(self):
499
        print_op = gtk.PrintOperation()
500
        print_op.set_n_pages(1)
501
502
        if self.print_settings != None:
503
            print_op.set_print_settings( self.print_settings)
504
        
505
        if self.print_page_setup != None:
506
            print_op.set_default_page_setup(self.print_page_setup)
507
        
508
        print_op.connect("draw_page", self.printText)
509
        res = print_op.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG, self.mainwindow)
510
        
511
        if True or res == gtkprint_OPERATION_RESULT_ERROR :
512
            error_dialog = gtk.MessageDialog(self.mainwindow,
513
                                                          gtk.DIALOG_DESTROY_WITH_PARENT,
514
                                                          gtk.MESSAGE_ERROR,
515
                                                          gtk.BUTTONS_CLOSE,
516
                                                          "Error printing text:\n")
517
            error_dialog.connect("response", lambda w,id: w.destroy())
518
            error_dialog.show()
519
            
520
        elif res == gtk.PRINT_OPERATION_RESULT_APPLY:
521
            self.print_settings = print_op.get_print_settings()
522
523
    def printText(self, operation=None, context=None, page_nr=None):
524
        layout = context.create_pango_layout()
525
        layout.set_width(int(context.get_width() * pango.SCALE))
526
        layout.set_justify(False)
527
        layout.set_alignment(pango.ALIGN_LEFT)
528
        layout.set_wrap(pango.WRAP_WORD_CHAR)
529
        layout.set_font_description(pango.FontDescription(self.config.printfontname))
530
        layout.set_text(self.getText())
531
        cr = context.get_cairo_context()
532
        cr.show_layout(layout)
533
534
    def getText(self):
535
        start, end = self.textbuf.get_start_iter(), self.textbuf.get_end_iter()
536
        text = self.textbuf.get_text(start, end)
537
        return text
538
494
    def settext(self, text):
539
    def settext(self, text):
495
        startiter, cursoriter, enditer = self.getiters()
540
        startiter, cursoriter, enditer = self.getiters()
496
        self.textbuf.delete(startiter, enditer)
541
        self.textbuf.delete(startiter, enditer)
...
...
676
        self.buttontable.set_row_spacings(spacing)
721
        self.buttontable.set_row_spacings(spacing)
677
        self.buttontable.set_col_spacings(spacing)        
722
        self.buttontable.set_col_spacings(spacing)        
678
723
724
    def set_print_settings(self, settings):
725
        self.print_settings = settings
726
        
727
    def get_print_settings(self):
728
        return self.print_settings
729
        
679
    # reactions to user actions
730
    # reactions to user actions
680
731
681
    def on_BackspaceButton_pressed(self, button):
732
    def on_BackspaceButton_pressed(self, button):
...
...
755
            self.on_BackspaceButton_pressed(None)
806
            self.on_BackspaceButton_pressed(None)
756
        elif action == 'clear':
807
        elif action == 'clear':
757
            self.clearWindow()
808
            self.clearWindow()
809
        elif action == 'print':
810
            self.printWindow()
811
        elif action == 'print_setup':
812
            self.pageSetup()
758
        else:
813
        else:
759
            pass
814
            pass
760
815
...
...
788
            self.acceptword(self.getcurrentword())
843
            self.acceptword(self.getcurrentword())
789
844
790
    def on_FontButton_pressed(self, button):
845
    def on_FontButton_pressed(self, button):
791
        dialog = gtk.FontSelectionDialog("Select Font")
846
        fontname = self.askFont("Select Font", self.config.fontname)
847
        if fontname:
848
            self.config.fontname = fontname
849
850
    def on_PrintFontButton_pressed(self, button):
851
        fontname = self.askFont("Select Print Font", self.config.printfontname)
852
        if fontname:
853
            self.config.printfontname = fontname
854
            
855
    def askFont(self, title, fontname):
856
        dialog = gtk.FontSelectionDialog(title)
792
        self.mainwindow.set_keep_above(False)  # must be after show on Windows
857
        self.mainwindow.set_keep_above(False)  # must be after show on Windows
793
        dialog.set_transient_for(self.mainwindow)
858
        dialog.set_transient_for(self.mainwindow)
794
859
795
        if self.config.fontname:
860
        if self.config.fontname:
796
            dialog.set_font_name(self.config.fontname)
861
            dialog.set_font_name(fontname)
797
862
798
        if dialog.run() == gtk.RESPONSE_OK:
863
        if dialog.run() == gtk.RESPONSE_OK:
799
            self.config.fontname = dialog.get_font_name()
864
            fontname = dialog.get_font_name()
865
        else:
866
            fontname = None
800
867
801
        dialog.destroy()
868
        dialog.destroy()
802
        self.mainwindow.set_keep_above(True)  # must be after show on Windows
869
        self.mainwindow.set_keep_above(True)  # must be after show on Windows
870
        
871
        return fontname
803
872
804
    def on_ColourButton_pressed(self, which):
873
    def on_ColourButton_pressed(self, which):
805
        dialog = gtk.ColorSelectionDialog("Select %s colour" % which)
874
        dialog = gtk.ColorSelectionDialog("Select %s colour" % which)

Updated trunk/dkeyrc.val Download diff

3233
1
fontname = string(default="Sans 14")
1
fontname = string(default="Sans 14")
2
printfontname = string(default="Sans 14")
2
text_colour = string(default="GTK")
3
text_colour = string(default="GTK")
3
bg_colour = string(default="GTK")
4
bg_colour = string(default="GTK")
4
sel_text_colour = string(default="GTK")
5
sel_text_colour = string(default="GTK")

Updated trunk/installer/Dkey.iss Download diff

3233
14
DefaultDirName={pf}\DKey
14
DefaultDirName={pf}\DKey
15
PrivilegesRequired=admin
15
PrivilegesRequired=admin
16
SetupIconFile=dkey.ico
16
SetupIconFile=dkey.ico
17
UninstallIconFile=dkey.ico
18
17
19
; max compression - slower, not recommended over around 100 MB
18
; max compression - slower, not recommended over around 100 MB
20
;SolidCompression=true
19
;SolidCompression=true

Updated trunk/keyhook.py Download diff

3233
1
1
2
# for now we use windows specific hooks until we see how to do in pygtk (pyxhook)
2
# for now we use windows specific hooks until we see how to do in pygtk (pyxhook)
3
3
4
keymap = { 'Numpad7': 'code2',
4
keymap = { 
5
           'Divide': 'print_setup',
6
           'Multiply': 'print',
7
           'Subtract': 'clear',
8
9
            'Numpad7': 'code2',
5
           'Numpad8': 'code3',
10
           'Numpad8': 'code3',
6
           'Numpad9': 'code4',
11
           'Numpad9': 'code4',
12
           
7
           'Numpad4': 'code5',
13
           'Numpad4': 'code5',
8
           'Numpad5': 'code6',
14
           'Numpad5': 'code6',
9
           'Numpad6': 'code7',
15
           'Numpad6': 'code7',
16
           
10
           'Numpad1': 'code8',
17
           'Numpad1': 'code8',
11
           'Numpad2': 'code9',
18
           'Numpad2': 'code9',
12
           'Numpad3': 'code1',
19
           'Numpad3': 'code1',
20
           
13
           'Numpad0': 'code0',
21
           'Numpad0': 'code0',
22
           'Decimal': 'del',
14
           'Add': 'next',
23
           'Add': 'next',
15
           'Decimal': 'del',
24
           
16
           'Subtract': 'clear',
25
           #'Return': '',
17
           'Return': '',
18
           }
26
           }
19
27
20
import pyHook
28
import pyHook

Updated trunk/layout.xml Download diff

3233
70
    <widget id="button14" title="Inject Text" 
70
    <widget id="button14" title="Inject Text" 
71
	    action="self.config.placetext = 'focus'" group="placetext" />
71
	    action="self.config.placetext = 'focus'" group="placetext" />
72
72
73
    <widget id="button22" title="" 
73
    <widget id="button22" title="Print Font..." 
74
	    action="" />
74
	    action="self.on_PrintFontButton_pressed(None)"/>
75
    <widget id="button23" title=""
75
    <widget id="button23" title=""
76
	    action="" />
76
	    action="" />
77
    <widget id="button24" title=""
77
    <widget id="button24" title=""