Module albow.widgets.ListBox
Source code
from typing import Tuple
import logging
from pygame import Surface
from pygame import Rect
from pygame.event import Event
from albow.core.ui.Widget import Widget
from albow.containers.PaletteView import PaletteView
LISTBOX_DEFAULT_ROWS: int = 10
LISTBOX_COLUMNS: int = 1
LISTBOX_SCROLLS: bool = True
class ListBox(PaletteView):
"""
"""
client: Widget = None
selection: int = None
items = []
selectAction = None
def __init__(self, theClient: Widget, theItems: list,
nrows: int = LISTBOX_DEFAULT_ROWS, ncols: int = LISTBOX_COLUMNS, selectAction=None, **kwargs):
assert isinstance(theItems, list), "Wrong type for input list"
self.client = theClient
self.items = theItems
self.selectAction = selectAction
font = self.predict_font(kwargs)
h = font.get_linesize()
d = 2 * self.predict(kwargs, 'margin')
longestTextLine: str = self._getLongestTextLine()
lineDimension: Tuple[int, int] = font.size(longestTextLine)
listBoxWidth: int = lineDimension[0]
cellSize = (listBoxWidth - d, h)
self.border_width = 1 #
self.margin = 5 # Before constructor to allow proper sizing 2.7.4
self.highlight_style = 'reverse' #
super().__init__(cell_size=cellSize, nrows=nrows, ncols=ncols, scrolling=LISTBOX_SCROLLS, **kwargs)
self.client = theClient
self.items = theItems
self.selectAction = selectAction
self.logger = logging.getLogger(__name__)
#
# The 4 methods below implement the PaletteView abstract methods
#
def num_items(self) -> int:
return len(self.items)
def draw_item(self, theSurface: Surface, theItemNumber: int, theRect: Rect):
# self.logger.info("draw_item %s ", theRect.size)
color = self.fg_color
buf = self.font.render(self.items[theItemNumber], True, color)
theSurface.blit(buf, theRect)
def click_item(self, theItemNumber: int, theEvent: Event):
self.logger.debug("click_item: %s", theItemNumber)
self.selection = theItemNumber
if self.selectAction is not None:
self.selectAction(self.items[self.selection])
def item_is_selected(self, theItemNumber: int) -> bool:
ans: bool = theItemNumber == self.selection
return ans
def _getLongestTextLine(self) -> str:
retStr: str = ""
for currLine in self.items:
if len(currLine) > len(retStr):
retStr = currLine
return retStr
Classes
class ListBox (theClient, theItems, nrows=10, ncols=1, selectAction=None, **kwargs)-
Initializes the palette view with the specified cell_size, and a rect sized for displaying nrows rows and ncols columns of items. If scrolling is true, controls will be displayed for scrolling the view.
Args
cell_size- A tuple that specifies the cell size (width, height)
nrows-
The # of rows
ncols-
The # of columns
scrolling- True to display scroll bars, else false
**kwds:
Source code
class ListBox(PaletteView): """ """ client: Widget = None selection: int = None items = [] selectAction = None def __init__(self, theClient: Widget, theItems: list, nrows: int = LISTBOX_DEFAULT_ROWS, ncols: int = LISTBOX_COLUMNS, selectAction=None, **kwargs): assert isinstance(theItems, list), "Wrong type for input list" self.client = theClient self.items = theItems self.selectAction = selectAction font = self.predict_font(kwargs) h = font.get_linesize() d = 2 * self.predict(kwargs, 'margin') longestTextLine: str = self._getLongestTextLine() lineDimension: Tuple[int, int] = font.size(longestTextLine) listBoxWidth: int = lineDimension[0] cellSize = (listBoxWidth - d, h) self.border_width = 1 # self.margin = 5 # Before constructor to allow proper sizing 2.7.4 self.highlight_style = 'reverse' # super().__init__(cell_size=cellSize, nrows=nrows, ncols=ncols, scrolling=LISTBOX_SCROLLS, **kwargs) self.client = theClient self.items = theItems self.selectAction = selectAction self.logger = logging.getLogger(__name__) # # The 4 methods below implement the PaletteView abstract methods # def num_items(self) -> int: return len(self.items) def draw_item(self, theSurface: Surface, theItemNumber: int, theRect: Rect): # self.logger.info("draw_item %s ", theRect.size) color = self.fg_color buf = self.font.render(self.items[theItemNumber], True, color) theSurface.blit(buf, theRect) def click_item(self, theItemNumber: int, theEvent: Event): self.logger.debug("click_item: %s", theItemNumber) self.selection = theItemNumber if self.selectAction is not None: self.selectAction(self.items[self.selection]) def item_is_selected(self, theItemNumber: int) -> bool: ans: bool = theItemNumber == self.selection return ans def _getLongestTextLine(self) -> str: retStr: str = "" for currLine in self.items: if len(currLine) > len(retStr): retStr = currLine return retStrAncestors
Class variables
var clientvar itemsvar selectActionvar selection
Methods
def draw_item(self, theSurface, theItemNumber, theRect)-
Source code
def draw_item(self, theSurface: Surface, theItemNumber: int, theRect: Rect): # self.logger.info("draw_item %s ", theRect.size) color = self.fg_color buf = self.font.render(self.items[theItemNumber], True, color) theSurface.blit(buf, theRect) def num_items(self)-
Source code
def num_items(self) -> int: return len(self.items)
Inherited members
PaletteView:addadd_anchoradd_centeredanchorattention_lostaugment_mouse_eventbg_colorbg_imageborder_colorborder_widthcall_handlercall_parent_handlercell_rectclick_itemdefer_drawingdismissdrawdraw_item_and_highlightdraw_overdraw_posthighlightdraw_prehighlightfg_colorfocusfocus_switchfontget_cursorget_focusget_margin_rectget_rootget_top_widgetget_visibleglobal_to_localhas_focushighlight_styleinheritedinvalidateis_gl_containeritem_is_selectedkey_downkey_uplocal_to_globalmarginmenu_barnum_colsnum_rowsparentparent_resizedpresentrectrelative_moderemoveremove_anchorresizedscale_bgscroll_button_colorscroll_button_sizesel_colorsel_widthset_parentset_size_for_texttab_stopvisible