/
MainDialog.py
158 lines (132 loc) · 4.94 KB
/
MainDialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# PLaunch - Shortcuts management tool, base on pyton/pygtk/pyxlib
#
# Copyright (C) 2008 Volans <volansw@gmail.com>
#
# PLaunch is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 2 of the License, or (at your
# option) any later version.
#
# PLaunch is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------------
import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade as glade
import gobject
import sys
import subprocess
from ShortCutsLoader import ShortCutsLoader
GLADE = sys.path[0] + '/data/PLaunch.glade'
SHORTCUTS_CONF = sys.path[0] + '/data/ShortCutList.txt'
(COLUMN_NUM, COLUMN_SHORTCUT, COLUMN_DISCRIPTION) = range(3)
reload_scm = True
class MainDialog:
'''MainDialog for user input, show shortcuts...
'''
def __init__(self):
self.xml = glade.XML(GLADE)
self.win = self.xml.get_widget('PLaunch')
self.xml.signal_autoconnect(self)
self.win.connect("delete_event", self.on_close_main)
self.win.set_keep_above(True)
self.__load_scm()
self.list = self.__create_list()
self.tv = self.__create_treeview()
self.__update_tree()
def __load_scm(self):
global reload_scm
if reload_scm == True:
self.scm = ShortCutsLoader(file(SHORTCUTS_CONF).readlines()[1:])
reload_scm = False
def __create_list(self):
list = gtk.ListStore(gobject.TYPE_UINT,
gobject.TYPE_STRING,
gobject.TYPE_STRING,
)
return list
def __create_treeview(self):
tv = self.xml.get_widget('shortcutTree')
selection = tv.get_selection()
selection.set_mode(gtk.SELECTION_BROWSE)
renderer = gtk.CellRendererText()
col = gtk.TreeViewColumn(u'Number',
renderer,
text=COLUMN_NUM)
tv.append_column(col)
renderer = gtk.CellRendererText()
col = gtk.TreeViewColumn(u'Shortcuts',
renderer,
text=COLUMN_SHORTCUT)
tv.append_column(col)
renderer = gtk.CellRendererText()
col = gtk.TreeViewColumn(u'Discription',
renderer,
text=COLUMN_DISCRIPTION)
tv.append_column(col)
return tv
def __update_tree(self):
'''Updated tree view of shortcuts after user input modification.
'''
self.__load_scm()
self.list.clear()
for key, cmd in self.scm.shortcutmatch.items():
self.list.set(self.list.append(),
0, key,
1, cmd,
2, self.scm.shortcutspool[cmd][1]
)
self.tv.set_model(self.list)
selection = self.tv.get_selection()
selection.select_path(0)
def on_select_shortcut(self, tv, column_number, column):
'''Get command from command pool.
Create a new process to run this command.
'''
self.__load_scm()
command = self.scm.shortcutspool[self.scm.shortcutmatch[column_number[0]]][0]
subprocess.Popen(command, shell=True)
# clear input
entryinput = self.xml.get_widget('userInput')
entryinput.set_text('')
self.on_close_main(None)
return True
def on_userInput_key_press_event(self, widget, key):
'''Catch the Enter key
'''
if key.keyval == 65293: # Enter
self.on_select_shortcut(None, (0,), None)
elif key.keyval == 65307: # Esc
entryinput = self.xml.get_widget('userInput')
entryinput.set_text('')
self.on_close_main(None)
def on_userInput_changed(self, widget, data=None):
input = widget.get_text()
self.scm.flush_input(input)
self.__update_tree()
def on_close_main(self, widget, data=None):
self.win.hide()
return True
def open_main(self, time=0):
self.__update_tree()
evttime = time if time else gtk.get_current_event_time()
self.win.stick()
self.win.present_with_time(time)
self.win.window.focus(timestamp=evttime)
if __name__ == '__main__':
class Test(MainDialog):
def on_close_main(self, widget, data=None):
gtk.main_quit()
test = Test()
test.win.show_all()
test.win.present()
gtk.main()