/usr/share/pyshared/freevo/dialog/widgets.py is in python-freevo 1.9.2b2-4.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | # -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# dialog definition and rendering module for livepause osd
# -----------------------------------------------------------------------
# $Id: widgets.py 11836 2011-02-18 21:58:19Z adam $
#
# Notes:
# Todo:
#
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2002 Krister Lagerstrom, et al.
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ----------------------------------------------------------------------- */
"""
Module containing the Widget model classes.
A widget is something that the user can navigate to and can have one of the following states:
- active = The user has navigate to this widget and it has focues.
- normal = The widget doesn't have focus but is still enabled and can be navigated to.
- disabled = The widget is not navigatable to.
- invisible = The widget is not drawn.
"""
import kaa
from event import *
class WidgetModel(object):
"""
The base class for all widgets.
@ivar parent: The dialog or widget that contains this one.
@ivar active: Whether this widget has focus.
@ivar enabled: Whether this widget is navigable.
@ivar visible: Whether this widget is drawn.
@ivar signals: Dictionary of signals emitted by this widget. The base class emits 2 signals:
- activated = The widget has recieved focus.
- deactivated = The widget has lost focus.
"""
def __init__(self):
self.parent = None
self.active = False
self.enabled = True
self.visible = True
self.signals = kaa.Signals()
self.signals['activated'] = kaa.Signal()
self.signals['deactivated'] = kaa.Signal()
def set_parent(self, parent):
"""
Set the parent of this widget, either a dialog or another widget.
@param parent: The parent dialog or widget.
"""
self.parent = parent
def redraw(self):
"""
Request that the dialog containing this widget is redrawn.
"""
if self.parent:
self.parent.redraw()
def set_active(self, active):
"""
Actitivate/Deactivate this widget.
@param active: Whether this widget has gained the focus or not.
"""
if self.active != active:
self.active = active
if active:
self.signals['activated'].emit(self)
else:
self.signals['deactivated'].emit(self)
def handle_event(self, event):
"""
Process the event supplied.
@param event: kaa.Event to process.
@return: True if the event was consumed, False otherwise.
@rtype: boolean
"""
return False
def get_state(self):
"""
Returns a text string describing the state of the model.
Used by the skin module when determine how to draw the widget.
@return: A string describing the current state of the widget.
"""
if self.visible:
if self.enabled:
if self.active:
return 'active'
else:
return 'normal'
else:
return 'disabled'
return 'invisible'
class ButtonModel(WidgetModel):
"""
Widget model representing a simple button
This model emits the following signals in addition to the base Widget signals:
- pressed = The button has been pressed.
"""
def __init__(self, text, icon=None):
"""
Create a new instance.
@param text: Text to display in the button.
@param icon: Optional image filename to be displayed.
"""
super(ButtonModel, self).__init__()
self.text = text
self.icon = icon
self.pressed = False
self.signals['pressed'] = kaa.Signal()
self.pressed_timer = kaa.OneShotTimer(self.__unpress)
def handle_event(self, event):
if event == 'INPUT_ENTER':
self.press()
return True
return super(ButtonModel, self).handle_event(event)
def handle_mouse_event(self, event):
if event == MOUSE_BTN_PRESS:
self.press(False)
elif event == MOUSE_BTN_RELEASE:
self.__unpress()
def press(self, keyboard=True):
"""
Press the button.
"""
self.pressed = True
self.redraw()
if keyboard:
self.pressed_timer.start(0.2)
def __unpress(self):
"""
Called by the timer to set the state to unpressed and emit the pressed signal.
"""
self.pressed = False
self.redraw()
self.signals['pressed'].emit(self)
def get_state(self):
state = super(ButtonModel, self).get_state()
if state == 'active' and self.pressed:
return 'pressed'
return state
def __repr__(self):
return 'ButtonModel(%r, icon=%r)' % (self.text, self.icon)
class ToggleButtonModel(ButtonModel):
"""
A button that can be in 2 states, selected or unselected.
This class can be used in combination with the ToggleButtonGroup to
implement radio buttons.
This model emits the following signals in addition to the base Widget signals:
- toggled = The selected state of the button has changed.
@ivar selected: Whether this button is selected.
"""
def __init__(self, text):
"""
Create a new instance.
@param text: The text to display along with the state of the button.
"""
super(ToggleButtonModel, self).__init__(text)
self.selected = False
self.group = None
self.signals['toggled'] = kaa.Signal()
def set_selected(self, selected):
"""
Set whether this button is selected or not. If the state of the button
changes this method emits the toggled signal.
@param selected: True if the button should be selected, False otherwise.
"""
if selected != self.selected:
# Don't allow selected button in a group to be deselected.
if self.group and self.group.selected_button == self and not selected:
return
self.selected = selected
self.signals['toggled'].emit(self, selected)
self.redraw()
def press(self):
self.set_selected(not self.selected)
def get_state(self):
state = super(ToggleButtonModel, self).get_state()
if state in ('normal', 'active', 'disabled'):
if self.selected:
state += '_selected'
else:
state += '_unselected'
return state
def __repr__(self):
return 'ToggleButtonModel(%r)' % self.text
class ToggleButtonGroup(object):
"""
This class is used to group ToggleButtonModels together so that when one is
selected all the others in the group are unselected.
This class emits the selection_changed signal when the selected button
changes.
@ivar buttons: List of buttons managed by this group.
@ivar selected_button: The currently selected button.
@ivar signals: Dictionary of signals emitted by this class.
"""
def __init__(self):
"""
Create a new instance with initially no buttons.
"""
self.buttons = []
self.selected_button = None
self.signals = kaa.Signals()
self.signals['selection_changed'] = kaa.Signal()
def add_button(self, button):
"""
Add a button to the group of buttons.
@param button: ToggleButtonModel to add to the group.
"""
self.buttons.append(button)
button.group = self
button.signals['toggled'].connect(self.__button_toggled)
if self.selected_button is None:
button.set_selected(True)
def remove_button(self, button):
"""
Remove a button from the group of buttons.
@param button: ToggleButtonModel to remove from the group.
"""
self.buttons.remove(button)
button.signals['toggled'].disconnect(self.__button_toggled)
def __button_toggled(self, from_button, selected):
"""
Internal function to monitor the state of the buttons and ensure only 1
is selected.
Emits the selected_changed signal when the selection changes.
"""
if selected:
self.selected_button = from_button
for button in self.buttons:
if from_button != button and button.selected:
button.set_selected(False)
self.signals['selection_changed'].emit(self, from_button)
class MenuModel(WidgetModel):
"""
Widget that displays a vertical list of selectable options.
This class emits the selection_changed signal when the selected button
changes.
@ivar items: List of menu items managed by this menu.
"""
def __init__(self, items=None):
WidgetModel.__init__(self)
self.items = []
self.page = None
self.offset = 0
self.active_item = 0
self.items_per_page = 0
self.more_up = False
self.more_down = False
self.signals['selection_changed'] = kaa.Signal()
if items:
for item in items:
item.parent = self
self.items.append(item)
def handle_event(self, event):
if event == 'INPUT_ENTER':
self.items[self.active_item].press()
return True
elif event == 'INPUT_UP':
if self.active_item > 0:
self.items[self.active_item].active = False
self.active_item -= 1
if self.active_item < self.offset:
self.offset = self.active_item
self.__update_page()
self.items[self.active_item].active = True
self.signals['selection_changed'].emit(self, self.items[self.active_item])
self.redraw()
return True
elif event == 'INPUT_DOWN':
if self.active_item < len(self.items) - 1:
self.items[self.active_item].active = False
self.active_item += 1
if self.active_item >= self.offset + self.items_per_page:
self.offset = self.active_item - (self.items_per_page - 1)
self.__update_page()
self.items[self.active_item].active = True
self.signals['selection_changed'].emit(self, self.items[self.active_item])
self.redraw()
return True
elif event == 'INPUT_LEFT':
if self.active_item > 0:
self.items[self.active_item].active = False
if self.active_item != self.offset:
self.active_item = self.offset
else:
self.active_item -= 1
self.offset = self.active_item - (self.items_per_page - 1)
if self.offset < 0:
self.offset = 0
self.__update_page()
self.items[self.active_item].active = True
self.signals['selection_changed'].emit(self, self.items[self.active_item])
self.redraw()
return True
elif event == 'INPUT_RIGHT':
if self.active_item < len(self.items) - 1:
self.items[self.active_item].active = False
if self.active_item != self.offset + self.items_per_page - 1:
self.active_item = self.offset + self.items_per_page - 1
else:
self.active_item += 1
self.offset = self.active_item - (self.items_per_page - 1)
self.__update_page()
if self.active_item >= len(self.items):
self.active_item = len(self.items) - 1
self.items[self.active_item].active = True
self.signals['selection_changed'].emit(self, self.items[self.active_item])
self.redraw()
return True
return super(MenuModel, self).handle_event(event)
def handle_mouse_event(self, event):
if event == MOUSE_MOTION:
y = event.pos[1] - self.position[1]
size_per_item = self.size[1] / self.items_per_page
scroll_height = size_per_item / 4
if y <= scroll_height and self.offset >= 1:
self.offset -= 1
self.__update_page()
self.redraw()
if y >= (self.size[1] - scroll_height) and (self.offset + self.items_per_page) < len(self.items):
self.offset += 1
self.__update_page()
self.redraw()
idx = (y / size_per_item) + self.offset
self.items[self.active_item].active = False
self.active_item = idx
if self.active_item >= len(self.items):
self.active_item = len(self.items) - 1
self.items[self.active_item].active = True
self.redraw()
if event in (MOUSE_BTN_PRESS, MOUSE_BTN_RELEASE):
self.items[self.active_item].handle_mouse_event(event)
def activate_item(self, item):
"""
Activate the specified menu item and redraw the dialog.
@param item: MenuItemModel to be activated.
"""
try:
idx = self.items.index(item)
self.items[self.active_item].active = False
self.active_item = idx
self.items[self.active_item].active = True
self.redraw()
except ValueError:
pass
def add(self, item):
"""
Adds a new menu item to this menu.
@param item: The MenuItem to add to this menu.
"""
item.parent = self
self.items.append(item)
if self.items_per_page:
self.__update_page()
self.redraw()
def remove(self, item):
"""
Remove a menu item from this menu.
@param item: The MenuItem to remove.
"""
item.parent = None
idx = self.items.index(item)
if idx <= self.offset:
self.offset -= 1
if idx <= self.active_item:
self.active_item -= 1
self.items.remove(item)
if self.items_per_page:
self.__update_page()
self.redraw()
def remove_all(self):
"""
Remove all items from this menu.
"""
for item in self.items:
item.parent = None
self.items = []
self.offset = 0
self.active_item = 0
if self.items_per_page:
self.__update_page()
self.redraw()
def layout(self, items_per_page, position, size):
"""
Called by the skin to layout this menu.
@param items_per_page: The number of items that are to be displayed on a page.
"""
self.position = position
self.size = size
self.offset = 0
self.active_item = 0
self.items_per_page = items_per_page
self.items[0].set_active(True)
for item in self.items[1:]:
item.set_active(False)
self.__update_page()
def __update_page(self):
"""
Internal method to create the active page of menu items.
"""
self.page = []
count = 0
while count + self.offset < len(self.items) and count < self.items_per_page:
self.page.append(self.items[self.offset + count])
count += 1
self.more_down = self.offset + count < len(self.items)
self.more_up = self.offset > 0
def get_page_item(self, index):
"""
Get a menu item on the active page.
@param index: The index of the item to retrieve from the page.
@return: A MenuItemModel or None if there is no model at the specific index.
"""
if index >= len(self.page):
return None
return self.page[index]
def get_active_item(self):
"""
Retrieve the active MenuItemModel.
@return: the active MenuItemModel.
"""
return self.items[self.active_item]
def __repr__(self):
return 'MenuModel(items=%r)' % self.items
class MenuItemModel(ButtonModel):
"""
Simple item to be displayed in a menu.
"""
def __init__(self, text, icon=None):
"""
Creates a new instance.
@param text: Text to be displayed by the item.
@param icon: Optional image filename to display.
"""
super(MenuItemModel, self).__init__(text, icon)
def set_active(self, active):
pass
def get_state(self):
state = super(MenuItemModel, self).get_state()
if state == 'normal' and self == self.parent.get_active_item():
return 'highlighted'
return state
def __repr__(self):
return 'MenuItemModel(%r, icon=%r)' % (self.text, self.icon)
class ToggleMenuItemModel(MenuItemModel):
"""
Menu item that can either be selected or unselected.
"""
def __init__(self, text):
"""
Creates a new instance.
@param text: Text to be displayed by the item.
"""
super(ToggleMenuItemModel, self).__init__(text)
self.selected = False
self.signals['toggled'] = kaa.Signal()
def set_selected(self, selected):
"""
Set whether this
"""
if selected != self.selected:
self.selected = selected
self.signals['toggled'].emit(self, selected)
self.redraw()
def press(self, keyboard=True):
self.set_selected(not self.selected)
def get_state(self):
state = super(ToggleMenuItemModel, self).get_state()
if state in ('normal', 'active', 'disabled', 'highlighted'):
if self.selected:
state += '_selected'
else:
state += '_unselected'
return state
def __repr__(self):
return 'ToggleMenuItemModel(%r)' % self.text
|