def widget_for_column_class(column_class): # Try a standard field if column_class in WIDGETS: return WIDGETS[column_class] # Try the enum wildcard (we don't want to name all the known enums in the widget registration) if column_class in Enum.all_known(): return WIDGETS["Enum"] raise RuntimeError(f"No widget to edit a field of type {column_class}")
def get_sound(entity): if entity.discriminator == "Shop": return "shop" elif entity.discriminator == "Land": return "land" elif entity.discriminator == "Amenity" and entity.value_of_field( "type") == Enum.with_name("AmenityType").value_for_name( "waste_basket"): return "trashcan" else: return None
def _handle_enter_into(self, sender, enters): base_group = None if enters.is_road_like: # Simulate that all the currently interesting roads just became interesting. for road in self._interesting_roads: self._maybe_spawn_crossing_sound_for_road(enters, road) if enters.value_of_field("type") == Enum.with_name( "RoadType").value_for_name("path"): base_group = "steps_path" else: base_group = "steps_road" if base_group: count = sound().get_group_size(base_group) group = "%s.%02d" % (base_group, random.randint(1, count)) else: group = DEFAULT_STEPS_GROUP self._groups_map[sender][enters] = group
def format_field_value(field_value, field_type, template_type=TemplateType.long): if field_type in known_enums: try: if isinstance(field_value, str): log.warning( "Field value %s of the enum %s expected to be an int.", field_value, field_type) return underscored_to_words(field_value) return underscored_to_words( Enum.with_name(field_type).name_for_value( field_value)) or field_value except ValueError: pass try: metadata = EntityMetadata.for_discriminator(field_type) except KeyError: return field_value return describe_nested_object(field_value, metadata, template_type)
def is_interesting(entity): # For our purposes, the following discriminators are not interesting. not_interesting_discriminators = { "Crossing", "Addressable", "Barrier", "PowerLine", "NoExit", "Entrance", "RailWay", "Tree", "Steps" } if entity.discriminator in not_interesting_discriminators: return False # Filter out uninteresting buildings if entity.discriminator == "Building" and entity_has_none_of_these_fields( entity, "name", "amenity", "education", "artwork_type", "garden_type", "seamark_type", "diplomatic", "landuse", "historic_type", "industrial_type", "man_made", "leisure_type", "emergency"): return False # Filter out house gardens - we might consider making them at least sound in the future, though if entity.discriminator == "Garden" and entity.value_of_field( "garden_type") == Enum.with_name("GardenType").value_for_name( "residential"): return False # Filter out sidewalks if we want to avoid them if config().navigation.try_avoid_sidewalks and is_footway(entity): return False return True
import builtins from osm_db import EntityMetadata, Enum import jinja2 import logging from PySide2.QtCore import QLocale from .services import config OPPOSITE_TURN_THRESHOLD = 175 class TemplateType(enum.Enum): long = 1 short = 2 known_enums = Enum.all_known() jinja2_env = jinja2.Environment(loader=jinja2.BaseLoader()) template_cache = defaultdict(dict) locale = QLocale() log = logging.getLogger(__name__) def get_template_string(metadata, template_type): return metadata.short_display_template if template_type is TemplateType.short else metadata.long_display_template # If we add a third type, we'll have to change this oneliner to something with more lines. def get_template(metadata, template_type): if template_type not in template_cache[metadata.discriminator]: template = get_template_string(metadata, template_type) template_cache[metadata.discriminator][
def get_value_widget(parent, column): choice = QComboBox(parent, editable=False) enum = Enum.with_name(column.type_name) for name in enum.members.keys(): choice.addItem(underscored_to_words(name)) return choice
def is_footway(road): return road.value_of_field("type") == Enum.with_name( "RoadType").value_for_name("footway")