1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# Copyright (C) 2012 Phoenix87 <phoenix1987@gmail.com>
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU General Public License version 3, as published
6
# by the Free Software Foundation.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
# PURPOSE. See the GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License along
14
# with this program. If not, see <http://www.gnu.org/licenses/>.
17
"""this dialog adjusts values in the preferences dictionary
20
in module preferences: defaults[key] has a value
21
self.builder.get_object(key) is a suitable widget to adjust value
22
widget_methods[key] provides method names for the widget
23
each widget calls set_preference(...) when it has adjusted value
28
logger = logging.getLogger('gtumbler_lib')
30
from . helpers import get_builder, show_uri, get_help_uri
31
from . preferences import preferences
33
class PreferencesDialog(gtk.Dialog):
34
__gtype_name__ = "PreferencesDialog"
37
"""Special static method that's automatically called by Python when
38
constructing a new instance of this class.
40
Returns a fully instantiated PreferencesDialog object.
42
builder = get_builder('PreferencesGtumblerDialog')
43
new_object = builder.get_object("preferences_gtumbler_dialog")
44
new_object.finish_initializing(builder)
47
def finish_initializing(self, builder):
48
"""Called while initializing this instance in __new__
50
finish_initalizing should be called after parsing the ui definition
51
and creating a PreferencesDialog object with it in order to
52
finish initializing the start of the new PerferencesGtumblerDialog
55
Put your initialization code in here and leave __init__ undefined.
58
# Get a reference to the builder and set up the signals.
59
self.builder = builder
60
self.ui = builder.get_ui(self, True)
62
# code for other initialization actions should be added here
63
self.widget_methods = []
65
def set_widgets_from_preferences(self):
66
''' these widgets show values in the preferences dictionary '''
67
for key in preferences.keys():
68
self.set_widget_from_preference(key)
70
def set_widget_from_preference(self, key):
71
'''set widget value from item in preferences'''
73
value = preferences.get(key)
74
widget = self.builder.get_object(key)
76
# this preference is not adjustable by this dialog
77
# for example: window and dialog geometries
78
logger.debug('no widget for preference: %s' % key)
81
logger.debug('set_widget_from_preference: %s' % key)
83
write_method_name = self.widget_methods[key][1]
85
logger.warn('%s not in widget_methods' % key)
89
method = getattr(widget, write_method_name)
90
except AttributeError:
91
logger.warn("""'%s' does not have a '%s' method.
92
Please edit 'widget_methods' in %s"""
93
% (key, write_method_name, self.__gtype_name__))
97
widget.connect(self.widget_methods[key][2], self.set_preference)
99
logger.warn("""'%s' unknown signal name '%s'
100
Please edit 'widget_methods' in %s"""
101
% (key, self.widget_methods[key][2], self.__gtype_name__))
105
def get_key_for_widget(self, widget):
107
for key_try in preferences.keys():
108
obj = self.builder.get_object(key_try)
113
def set_preference(self, widget, data=None):
114
'''set a preference from a widget'''
115
key = self.get_key_for_widget(widget)
117
logger.warn('''This widget will not write to a preference.
118
The preference must already exist so add this widget's name
119
to default_preferences in your main function''')
122
# set_widget_from_preference is called first
123
# so no KeyError test is needed here
124
read_method_name = self.widget_methods[key][0]
127
read_method = getattr(widget, read_method_name)
128
except AttributeError:
129
logger.warn("""'%s' does not have a '%s' method.
130
Please edit 'widget_methods' in %s"""
131
% (key, read_method_name, self.__gtype_name__))
135
logger.debug('set_preference: %s = %s' % (key, str(value)))
136
preferences[key] = value
138
def on_btn_close_clicked(self, widget, data=None):
141
def on_btn_help_clicked(self, widget, data=None):
142
show_uri(self, "ghelp:%s" % get_help_uri('preferences'))