~ubuntu-branches/ubuntu/utopic/pida/utopic

« back to all changes in this revision

Viewing changes to .pc/0001-pida-optionsmanager-NoneTypeFix.diff/pida/services/optionsmanager/optionsmanager.py

  • Committer: Package Import Robot
  • Author(s): Philipp Kaluza
  • Date: 2011-07-29 23:46:20 UTC
  • mfrom: (0.1.6 sid)
  • Revision ID: package-import@ubuntu.com-20110729234620-zka1d28kv0jeunix
Tags: 0.5.1-6
* merge NMUs; thanks to Marek Kubica and Sandro Tosi.
* remove python-gtkhtml2 completely; its gone for good (closes: #549033)
* move to 3.0 (quilt) source format
* update maintainer's email address
* transition from python-central to dh_python2 (closes: #616937)
* patches/0000-call-vim.gnome.patch:
  - "gvim" is too generic a name now, we need to depend on vim-gnome
    (thanks to Moritz Muehlenhoff for the idea; closes: #582524)
  - also, depend on vim-gnome
* patches/0001-pida-optionsmanager-NoneTypeFix.diff:
  - optionsmanager: do the prefill only of current is not None
    (thanks to Nick Daly; closes: #519152)
* fix some linitan warnings
* bump debhelper compat level to 7 (still allowing lucid builds)
* update Standards-Version to 3.9.2.0 (no changes needed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*- 
 
2
 
 
3
# Copyright (c) 2007 The PIDA Project
 
4
 
 
5
#Permission is hereby granted, free of charge, to any person obtaining a copy
 
6
#of this software and associated documentation files (the "Software"), to deal
 
7
#in the Software without restriction, including without limitation the rights
 
8
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
#copies of the Software, and to permit persons to whom the Software is
 
10
#furnished to do so, subject to the following conditions:
 
11
 
 
12
#The above copyright notice and this permission notice shall be included in
 
13
#all copies or substantial portions of the Software.
 
14
 
 
15
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
16
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
17
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
18
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
19
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
20
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
21
#SOFTWARE.
 
22
 
 
23
from textwrap import wrap
 
24
 
 
25
import gtk
 
26
 
 
27
 
 
28
# PIDA Imports
 
29
from pida.core.service import Service
 
30
from pida.core.features import FeaturesConfig
 
31
from pida.core.commands import CommandsConfig
 
32
from pida.core.events import EventsConfig
 
33
from pida.core.actions import ActionsConfig
 
34
from pida.core.actions import TYPE_NORMAL, TYPE_MENUTOOL, TYPE_RADIO, TYPE_TOGGLE
 
35
 
 
36
from pida.ui.views import PidaGladeView
 
37
from pida.ui.widgets import get_widget_for_type
 
38
 
 
39
from kiwi import ValueUnset
 
40
 
 
41
# locale
 
42
from pida.core.locale import Locale
 
43
locale = Locale('optionsmanager')
 
44
_ = locale.gettext
 
45
 
 
46
def service_sort_func(s1, s2):
 
47
    return cmp(s1.get_label(), s2.get_label())
 
48
 
 
49
def options_sort_func(o, o1):
 
50
    return cmp(o1.name, o2.name)
 
51
 
 
52
 
 
53
class PidaOptionsView(PidaGladeView):
 
54
 
 
55
    gladefile = 'options-editor'
 
56
    locale = locale
 
57
    label_text = 'Preferences'
 
58
 
 
59
    icon_name = 'gnome-settings'
 
60
 
 
61
    def create_ui(self):
 
62
        self.current = None
 
63
        self.refresh_ui()
 
64
 
 
65
    def clear_ui(self):
 
66
        while self.options_book.get_n_pages():
 
67
            self.options_book.remove_page(-1)
 
68
        self._services_display = []
 
69
        self._service_pages = {}
 
70
 
 
71
    def refresh_ui(self):
 
72
        current = self.current
 
73
        self.clear_ui()
 
74
        self._services = []
 
75
        for svc in self.svc.boss.get_services():
 
76
            if len(svc.get_options()):
 
77
                self._services.append(svc)
 
78
                self._services_display.append(
 
79
                    (svc.get_label(), svc),
 
80
                )
 
81
        self._services.sort(service_sort_func)
 
82
        self._tips = gtk.Tooltips()
 
83
        self.service_combo.prefill(self._services_display)
 
84
        if current is not None:
 
85
            try:
 
86
                self.service_combo.update(current)
 
87
            except KeyError:
 
88
                self.service_combo.update(self.current)
 
89
                
 
90
 
 
91
    def _add_service(self, svc):
 
92
        self._service_pages[svc.servicename] = self.options_book.get_n_pages()
 
93
        self.options_book.append_page(self._create_page(svc))
 
94
        self.options_book.show_all()
 
95
        
 
96
    def _create_page(self, svc):
 
97
        mainvb = gtk.VBox(spacing=0)
 
98
        mainvb.set_border_width(6)
 
99
        label = gtk.Label()
 
100
        label.set_markup('<big><b>%s</b></big>' % svc.get_label())
 
101
        label.set_alignment(0, 0.5)
 
102
        mainvb.pack_start(label, expand=False)
 
103
        optvb = gtk.VBox()
 
104
        optsw = gtk.ScrolledWindow()
 
105
        optsw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
 
106
        optvb.set_border_width(6)
 
107
        optsw.add_with_viewport(optvb)
 
108
        mainvb.pack_start(optsw)
 
109
        labelsizer = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)
 
110
        widgetsizer = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)
 
111
        options = list(svc.get_options().iter_options())
 
112
        options.sort()
 
113
        for opt in options:
 
114
            vb = gtk.VBox(spacing=2)
 
115
            vb.set_border_width(6)
 
116
            eb = gtk.EventBox()
 
117
            eb.add(vb)
 
118
            optvb.pack_start(eb, expand=False)
 
119
            hb = gtk.HBox(spacing=6)
 
120
            vb.pack_start(hb)
 
121
            optlabel = gtk.Label()
 
122
            optlabel.set_text('\n'.join(wrap(opt.label, 20)))
 
123
            optlabel.set_alignment(0, 0)
 
124
            labelsizer.add_widget(optlabel)
 
125
            hb.pack_start(optlabel, expand=False)
 
126
            optwidget = get_widget_for_type(opt.rtype)
 
127
            widgetsizer.add_widget(optwidget)
 
128
            hb.pack_start(optwidget, expand=True)
 
129
            value = opt.get_value()
 
130
            optwidget.update(value)
 
131
            optwidget.connect('content-changed', self._on_option_changed, opt)
 
132
            opt.add_notify(self._on_option_changed_elsewhere, optwidget)
 
133
            self._tips.set_tip(eb, opt.doc)
 
134
        return mainvb
 
135
 
 
136
    def on_service_combo__content_changed(self, cmb):
 
137
        self.current = svc = self.service_combo.read()
 
138
        if not svc.servicename in self._service_pages:
 
139
            self._add_service(svc)
 
140
        pagenum = self._service_pages[svc.servicename]
 
141
        self.options_book.set_current_page(pagenum)
 
142
 
 
143
    def _on_option_changed(self, widget, option):
 
144
        widgval = widget.read()
 
145
        optval = option.get_value()
 
146
        # various hacks
 
147
        if widgval is None:
 
148
            return
 
149
        if widgval == ValueUnset:
 
150
            widgval = ''
 
151
        if widgval != optval:
 
152
            option.set_value(widgval)
 
153
 
 
154
    def _on_option_changed_elsewhere(self, client, id, entry, (option, widget)):
 
155
        widgval = widget.read()
 
156
        optval = option.get_value()
 
157
        if optval != widgval:
 
158
            widget.update(option.get_value())
 
159
 
 
160
    def can_be_closed(self):
 
161
        self.svc.get_action('show_options').set_active(False)
 
162
 
 
163
 
 
164
class OptionsActions(ActionsConfig):
 
165
 
 
166
    def create_actions(self):
 
167
        self.create_action(
 
168
            'show_options',
 
169
            TYPE_TOGGLE,
 
170
            _('Edit Preferences'),
 
171
            _('Edit the PIDA preferences'),
 
172
            'properties',
 
173
            self.on_show_options,
 
174
            '<Shift><Control>asciitilde'
 
175
        )
 
176
 
 
177
    def on_show_options(self, action):
 
178
        if action.get_active():
 
179
            self.svc.show_options()
 
180
        else:
 
181
            self.svc.hide_options()
 
182
 
 
183
class OptionsEvents(EventsConfig):
 
184
 
 
185
    def subscribe_foreign_events(self):
 
186
        self.subscribe_foreign_event('plugins', 'plugin_started',
 
187
                                     self.plugin_changed)
 
188
        self.subscribe_foreign_event('plugins', 'plugin_stopped',
 
189
                                     self.plugin_changed)
 
190
 
 
191
    def plugin_changed(self, plugin):
 
192
        if len(plugin.get_options()):
 
193
            self.svc.refresh_view()
 
194
 
 
195
# Service class
 
196
class Optionsmanager(Service):
 
197
    """Describe your Service Here""" 
 
198
 
 
199
    actions_config = OptionsActions
 
200
    events_config = OptionsEvents
 
201
 
 
202
    def start(self):
 
203
        self._view = PidaOptionsView(self)
 
204
 
 
205
    def show_options(self):
 
206
        self.boss.cmd('window', 'add_view', paned='Plugin', view=self._view)
 
207
 
 
208
    def hide_options(self):
 
209
        self.boss.cmd('window', 'remove_view', view=self._view)
 
210
 
 
211
    def refresh_view(self):
 
212
        self._view.refresh_ui()
 
213
 
 
214
# Required Service attribute for service loading
 
215
Service = Optionsmanager
 
216
 
 
217
 
 
218
 
 
219
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: