1
# -*- coding: utf-8 -*-
3
# Copyright (c) 2007 The PIDA Project
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:
12
#The above copyright notice and this permission notice shall be included in
13
#all copies or substantial portions of the Software.
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
23
from textwrap import wrap
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
36
from pida.ui.views import PidaGladeView
37
from pida.ui.widgets import get_widget_for_type
39
from kiwi import ValueUnset
42
from pida.core.locale import Locale
43
locale = Locale('optionsmanager')
46
def service_sort_func(s1, s2):
47
return cmp(s1.get_label(), s2.get_label())
49
def options_sort_func(o, o1):
50
return cmp(o1.name, o2.name)
53
class PidaOptionsView(PidaGladeView):
55
gladefile = 'options-editor'
57
label_text = 'Preferences'
59
icon_name = 'gnome-settings'
66
while self.options_book.get_n_pages():
67
self.options_book.remove_page(-1)
68
self._services_display = []
69
self._service_pages = {}
72
current = self.current
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),
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:
86
self.service_combo.update(current)
88
self.service_combo.update(self.current)
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()
96
def _create_page(self, svc):
97
mainvb = gtk.VBox(spacing=0)
98
mainvb.set_border_width(6)
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)
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())
114
vb = gtk.VBox(spacing=2)
115
vb.set_border_width(6)
118
optvb.pack_start(eb, expand=False)
119
hb = gtk.HBox(spacing=6)
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)
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)
143
def _on_option_changed(self, widget, option):
144
widgval = widget.read()
145
optval = option.get_value()
149
if widgval == ValueUnset:
151
if widgval != optval:
152
option.set_value(widgval)
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())
160
def can_be_closed(self):
161
self.svc.get_action('show_options').set_active(False)
164
class OptionsActions(ActionsConfig):
166
def create_actions(self):
170
_('Edit Preferences'),
171
_('Edit the PIDA preferences'),
173
self.on_show_options,
174
'<Shift><Control>asciitilde'
177
def on_show_options(self, action):
178
if action.get_active():
179
self.svc.show_options()
181
self.svc.hide_options()
183
class OptionsEvents(EventsConfig):
185
def subscribe_foreign_events(self):
186
self.subscribe_foreign_event('plugins', 'plugin_started',
188
self.subscribe_foreign_event('plugins', 'plugin_stopped',
191
def plugin_changed(self, plugin):
192
if len(plugin.get_options()):
193
self.svc.refresh_view()
196
class Optionsmanager(Service):
197
"""Describe your Service Here"""
199
actions_config = OptionsActions
200
events_config = OptionsEvents
203
self._view = PidaOptionsView(self)
205
def show_options(self):
206
self.boss.cmd('window', 'add_view', paned='Plugin', view=self._view)
208
def hide_options(self):
209
self.boss.cmd('window', 'remove_view', view=self._view)
211
def refresh_view(self):
212
self._view.refresh_ui()
214
# Required Service attribute for service loading
215
Service = Optionsmanager
219
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: