~phoenix1987/gtumbler/uas

« back to all changes in this revision

Viewing changes to gtumbler_lib/PreferencesDialog.py

  • Committer: Gabriele N. Tornetta
  • Date: 2012-06-25 16:18:04 UTC
  • Revision ID: phoenix1987@gmail.com-20120625161804-qjyy0b6wc8svt5kx
quickly saved

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
### BEGIN LICENSE
 
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.
 
7
 
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.
 
12
 
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/>.
 
15
### END LICENSE
 
16
 
 
17
"""this dialog adjusts values in the preferences dictionary
 
18
 
 
19
requirements:
 
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
 
24
"""
 
25
 
 
26
import gtk
 
27
import logging
 
28
logger = logging.getLogger('gtumbler_lib')
 
29
 
 
30
from . helpers import get_builder, show_uri, get_help_uri
 
31
from . preferences import preferences
 
32
 
 
33
class PreferencesDialog(gtk.Dialog):
 
34
    __gtype_name__ = "PreferencesDialog"
 
35
 
 
36
    def __new__(cls):
 
37
        """Special static method that's automatically called by Python when 
 
38
        constructing a new instance of this class.
 
39
        
 
40
        Returns a fully instantiated PreferencesDialog object.
 
41
        """
 
42
        builder = get_builder('PreferencesGtumblerDialog')
 
43
        new_object = builder.get_object("preferences_gtumbler_dialog")
 
44
        new_object.finish_initializing(builder)
 
45
        return new_object
 
46
 
 
47
    def finish_initializing(self, builder):
 
48
        """Called while initializing this instance in __new__
 
49
 
 
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
 
53
        instance.
 
54
        
 
55
        Put your initialization code in here and leave __init__ undefined.
 
56
        """
 
57
 
 
58
        # Get a reference to the builder and set up the signals.
 
59
        self.builder = builder
 
60
        self.ui = builder.get_ui(self, True)
 
61
 
 
62
        # code for other initialization actions should be added here
 
63
        self.widget_methods = []
 
64
 
 
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)
 
69
 
 
70
    def set_widget_from_preference(self, key):
 
71
        '''set widget value from item in preferences'''
 
72
                
 
73
        value = preferences.get(key)
 
74
        widget = self.builder.get_object(key)
 
75
        if widget is None:
 
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)
 
79
            return
 
80
                
 
81
        logger.debug('set_widget_from_preference: %s' % key)
 
82
        try:
 
83
            write_method_name = self.widget_methods[key][1]
 
84
        except KeyError:
 
85
            logger.warn('%s not in widget_methods' % key)
 
86
            return
 
87
 
 
88
        try:
 
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__))
 
94
            return
 
95
 
 
96
        try:
 
97
            widget.connect(self.widget_methods[key][2], self.set_preference)
 
98
        except TypeError:
 
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__))
 
102
 
 
103
        method(value)
 
104
 
 
105
    def get_key_for_widget(self, widget):
 
106
        key = None
 
107
        for key_try in preferences.keys():
 
108
            obj = self.builder.get_object(key_try)
 
109
            if obj == widget:
 
110
                key = key_try
 
111
        return key
 
112
 
 
113
    def set_preference(self, widget, data=None):
 
114
        '''set a preference from a widget'''
 
115
        key = self.get_key_for_widget(widget)
 
116
        if key is None:
 
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''')
 
120
            return
 
121
 
 
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]
 
125
 
 
126
        try:
 
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__))
 
132
            return
 
133
 
 
134
        value=read_method()
 
135
        logger.debug('set_preference: %s = %s' % (key, str(value)))
 
136
        preferences[key] = value
 
137
 
 
138
    def on_btn_close_clicked(self, widget, data=None):
 
139
        self.destroy()
 
140
 
 
141
    def on_btn_help_clicked(self, widget, data=None):
 
142
        show_uri(self, "ghelp:%s" % get_help_uri('preferences'))
 
143