emmental/emmental/entry.py

69 lines
2.0 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""Customized Gtk.Entries for easier development."""
from gi.repository import Gtk
from gi.repository import GObject
from . import format
class Filter(Gtk.SearchEntry):
"""A Gtk.Entry that returns a filter query."""
def __init__(self, what: str, **kwargs):
"""Set up the FilterEntry."""
super().__init__(placeholder_text=f"type to filter {what}", **kwargs)
def get_placeholder_text(self) -> str:
"""Get the entry's placeholder-text."""
return self.get_property("placeholder-text")
def get_query(self) -> str | None:
"""Get the query string for the entered text."""
return format.search(self.get_text())
class ValueBase(Gtk.Entry):
"""Base class for value entries."""
def __init__(self, input_purpose: Gtk.InputPurpose, value, **kwargs):
"""Initialize a ValueBase Entry."""
super().__init__(input_purpose=input_purpose,
value=value, text=str(value), **kwargs)
self.connect("notify::value", self.__notify_value)
def __notify_value(self, entry: Gtk.Entry, param) -> None:
self.set_text(str(self.value))
def do_activate(self) -> None:
"""Handle the activate signal."""
self.value = type(self.value)(self.get_text())
class Integer(ValueBase):
"""Entry for Integers."""
value = GObject.Property(type=int)
def __init__(self, value: int = 0, **kwargs):
"""Initialize an Integer Entry."""
super().__init__(Gtk.InputPurpose.DIGITS, value, **kwargs)
class Float(ValueBase):
"""Entry for Floats."""
value = GObject.Property(type=float)
def __init__(self, value: float = 0.0, **kwargs):
"""Initialize a Float Entry."""
super().__init__(Gtk.InputPurpose.NUMBER, value, **kwargs)
class String(ValueBase):
"""Entry for Strings."""
value = GObject.Property(type=str)
def __init__(self, value: str = "", **kwargs):
"""Initialize a String Entry."""
super().__init__(Gtk.InputPurpose.FREE_FORM, value, **kwargs)