widgethandler.py 5.11 KB
Newer Older
1 2 3 4 5 6 7
#coding=utf-8
#########################################################
# 							#
# This is BashStyle-NG  				#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3		#
#							#
8
# Copyright 2007 - 2018 Christopher Bratusek		#
9 10 11
#							#
#########################################################

12
MODULES = [ 'os', 'sys' ]
13 14 15 16 17 18 19 20 21 22 23 24

FAILED = []

for module in MODULES:
	try:
		globals()[module] = __import__(module)
	except ImportError:
		FAILED.append(module)

try:
	from gi.repository import Gtk
except ImportError:
25
	FAILED.append(_("Gtk (from gi.repository)"))
26 27

if FAILED:
28
    print(_("The following modules failed to import: %s") % (" ".join(FAILED)))
29 30
    sys.exit(1)

31
DATADIR = os.getenv('BSNG_DATADIR')
32
blacklist = ['\'', '\"']
33
gtkbuilder = Gtk.Builder()
34
gtkbuilder.set_translation_domain("bashstyle")
35
gtkbuilder.add_from_file(DATADIR + "/bashstyle-ng/ui/bashstyle.ui")
36 37 38

class WidgetHandler(object):
		####################### metafuncs for handling widgets ###########################
39 40 41 42 43
		def __init__(self, cfo, udc, fdc):
			self.config = cfo
			self.userdefault = udc
			self.factorydefault = fdc

44 45 46 47 48 49 50
		def SwapDictionary(self, original_dict):
		    try:
			    iteritems = original_dict.iteritems
		    except AttributeError:
			    iteritems = original_dict.items
		    return dict([(v, k) for (k, v) in iteritems()])

51
		def InitWidget(self, widget, group, setting, type, dict):
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
			# known widget types:
			#	text		GtkTextEntry
			#	int		GtkSpinButton
			#	bool		GtkToggleButton / GtkRadioButton
			#	switch		GtkSwitch
			#	combo		GtkComboBox
			#	button		GtkButton
			#	label		GtkLabel
			#	cpb_button	Custom Prompt Builder GtkButton
			#	cpb_combo	Custom Prompt Builder GtkComboBox

			# required parameters:
			#	for text, int, bool, switch:
			#		widget, group, setting, type, None
			#	for combo:
			#		widget, group, setting, type, dict
			#	for button:
			#		widget, action, actionarg, type, None
			#	for labels:
			#		widget, None, label, type, None
			#	for cpb_button:
			#		widget, pc_text, ps1_text, type, action
			#	for cpb_combo:
			#		widget, pc_dict, ps1_dict, type action
76

77
			def LoadWidget():
78
				object = gtkbuilder.get_object("%s" % widget)
79
				return object
80

81
			def LoadValue():
82
				if type == "text":
83
					object.set_text("%s" % self.config["%s" % group]["%s" % setting])
84
				elif type == "int":
85
					object.set_value(self.config["%s" % group].as_int("%s" % setting))
86
				elif type == "bool":
87
					object.set_active(self.config["%s" % group].as_bool("%s" % setting))
88 89
				elif type == "switch":
					object.set_active(self.config["%s" % group].as_bool("%s" % setting))
90
				elif type == "combo":
91
					object.set_active(self.SwapDictionary(dict)[self.config["%s" % group]["%s" % setting]])
92 93
				elif  type == "label":
					object.set_label("%s" % setting)
94 95
				elif type == "cpb_combo":
					object.set_active(0)
96

97
			def ConnectSignals():
98
				if type == "text":
99
					object.connect("insert-text", emit_text)
100
					object.connect("icon-press", revert_option, type, group, setting)
101
					object.connect("changed", set_option, None, type, None, group, setting)
102
				elif type == "int":
103
					object.connect("value-changed", set_option, None, type, None, group, setting)
104
					object.connect("icon-press", revert_option, type, group, setting)
105
				elif type == "bool":
106
					object.connect("toggled", set_option, None, type, None, group, setting)
107 108
				elif type == "switch":
					object.connect("notify::active", set_option, type, None, group, setting)
109
				elif type == "combo":
110
					object.connect("changed", set_option, None, type, dict, group, setting)
111 112
				elif type == "button":
					object.connect("clicked", group, setting)
113 114 115 116
				elif type == "cpb_button":
					object.connect("clicked", dict, group, setting)
				elif type == "cpb_combo":
					object.connect("changed", dict, group, setting)
117

118 119 120
			def revert_option(widget, pos, event, type, widget_group, widget_setting):
				if type == "text" or type == "int":
					if pos == Gtk.EntryIconPosition.SECONDARY:
121
						opt = self.factorydefault["%s" % widget_group]["%s" % widget_setting]
122
					else:
123 124
						opt = self.userdefault["%s" % widget_group]["%s" % widget_setting]
					self.config["%s" % widget_group]["%s" % widget_setting] = opt
125
					if type == "text":
126
						widget.set_text("%s" % self.config["%s" % widget_group]["%s" % widget_setting])
127
					elif type == "int":
128
						widget.set_value(self.config["%s" % widget_group].as_int("%s" % widget_setting))
129

130
			def set_option(widget, data, type, dict, widget_group, widget_setting):
131
				if type == "text":
132
					self.config["%s" % widget_group]["%s" % widget_setting] = widget.get_text()
133
				elif type == "int":
134
					self.config["%s" % widget_group]["%s" % widget_setting] = widget.get_value_as_int()
135
				elif type == "bool":
136
					self.config["%s" % widget_group]["%s" % widget_setting] = widget.get_active()
137 138
				elif type == "switch":
					self.config["%s" % widget_group]["%s" % widget_setting] = widget.get_active()
139
				elif type == "combo":
140
					self.config["%s" % widget_group]["%s" % widget_setting] =  dict[widget.get_active()]
141 142 143 144

			def emit_text(widget, text, *args):
				if text in blacklist:
					widget.emit_stop_by_name('insert-text')
145

146 147
			object = LoadWidget()
			LoadValue()
148
			ConnectSignals()
149
			return object