Layout rules for tidy and for dragging: - Boxes snap to whole meta steps live while dragged, not only on release. - The left-most column and top row clear the canvas edge by at least 30px, and neighbours by at least 30px, each rounded up to the next step. - Box widths round up to the grid so a right edge lands on a grid line. - Column gaps widen only where wires need lanes, so most stay at exactly 30px. - A sheet too wide for meta's 0..255 range is laid out at 2, then 3, then 4 grid squares per step. The multiplier is derived from the sheet's contents, so painting and tidying agree on it without recording anything in the file. The grid size in Preferences is untouched. Wire routing: - No wire may pass under a component. A link with no clear route is shown as knobs on both slots instead of a line hidden behind a box. - Vertical runs hug a box within 15px, never closer than 10px, and two runs sharing a gap stand 10px apart. - Horizontal runs of different links stay 5px apart. Runs from one output pin may share, since a fan-out is one signal. - Straight routes claim their rows first, as their segments are pinned to their slots and cannot move later. Knobs: - Wire colour with a small white centre when the far end is on this sheet, grey when it is on another, half and half when a slot carries both. - Selectable and deletable like a wire, and raised above the boxes so a click reaches them. - Selecting a same-sheet pair draws a red dashed line between the two knobs. Also: - get_box_bounds returns the rectangle actually drawn rather than canvas.bbox, which was inflated by the outline stroke and by selection. - Preferences offers to round the box width to a multiple of the grid. - The tidy coordinate-limit warning said to lower the grid size; raising it is what helps, since the reach is 255 * grid size. - Tidy Layout moves from its own menu into Edit. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
152 lines
5.7 KiB
Python
152 lines
5.7 KiB
Python
"""Shared configuration, runtime state and widget handles.
|
|
|
|
Every other module reaches this state as ``app_state.<name>`` rather than importing
|
|
the names directly. A plain ``from app_state import is_dirty`` would copy the value,
|
|
so later rebinding would only ever be seen by the module that did it.
|
|
"""
|
|
|
|
import configparser
|
|
import os
|
|
|
|
VERSION = "0.0.0.028"
|
|
PROPERTIES_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "editor.properties")
|
|
DEFAULT_GRID_SIZE = 10
|
|
DEFAULT_ADD_TIMESTAMP = True
|
|
|
|
# Purely a display choice, so it lives in editor.properties. The SAX file has nowhere to
|
|
# record it: meta stores a position and nothing about box size.
|
|
DEFAULT_BOX_WIDTH = 100
|
|
MIN_ALLOWED_BOX_WIDTH = 60
|
|
MAX_ALLOWED_BOX_WIDTH = 400
|
|
|
|
# Widget handles, filled in by main once the UI exists
|
|
window = None
|
|
canvas = None
|
|
tree = None
|
|
status_label = None # the window's bottom status bar
|
|
|
|
# Status bar tones. A hint that something needs attention later is worth a colour, so a
|
|
# placement that leaves the schema out of step is not read as a plain confirmation.
|
|
STATUS_INFO_COLOUR = "#333333"
|
|
STATUS_WARN_COLOUR = "#a04b00"
|
|
|
|
# Maps to hold tracking states
|
|
tree_element_map = {} # Treeview item ID -> ET.Element
|
|
canvas_comp_map = {} # Canvas item tag -> ET.Element
|
|
canvas_tag_by_element = {} # ET.Element -> Canvas unique tag string
|
|
component_slot_offsets = {} # Canvas unique tag -> {slot name: y offset from box top}
|
|
|
|
# File modification tracking state
|
|
is_dirty = False
|
|
current_file_path = None
|
|
active_components_list = [] # Tracks what components are actively painted
|
|
selected_canvas_tag = None # Tracks the currently clicked/highlighted canvas component tag
|
|
selected_tree_item_id = None # Tracks the currently right-clicked Treeview item
|
|
link_mark_element = None # Component marked as a link endpoint via "Link Mark"
|
|
selected_link_tag = None # Canvas tag of the currently selected (red-highlighted) link wire
|
|
unroutable_link_count = 0 # Links drawn as knobs because no wire could avoid a component
|
|
meta_scale = DEFAULT_GRID_SIZE # Pixels per meta coordinate step on the painted sheet
|
|
knob_link_ends = {} # wire tag -> (x1, y1, x2, y2) for a link shown as two knobs
|
|
canvas_link_map = {} # Canvas link tag -> ET.Element (<link>)
|
|
xml_root_element = None # Keep a persistent root reference to redraw tree layouts cleanly
|
|
current_sheet_parent = None # Element whose children are painted; None means the app root
|
|
|
|
# Drag-and-drop state variables
|
|
drag_data = {"x": 0, "y": 0, "tag": None}
|
|
|
|
# Global component slot registries compiled dynamically per file load
|
|
final_components_registry = {}
|
|
active_schema_kits = {} # kit name -> checksum, as the open file's <schema> declares them
|
|
|
|
# Kits a placed component needs that the file's <schema> does not declare yet, as
|
|
# kit name -> checksum resolved from Sedona home. Filled in when a palette component is
|
|
# placed and read back when saving, which is where the <schema> block is reconciled.
|
|
pending_schema_kits = {}
|
|
|
|
|
|
def load_preferences():
|
|
"""Loads application configurations from editor.properties or initializes defaults."""
|
|
config = configparser.ConfigParser()
|
|
grid_size = DEFAULT_GRID_SIZE
|
|
add_timestamp = DEFAULT_ADD_TIMESTAMP
|
|
box_width = DEFAULT_BOX_WIDTH
|
|
sedona_home = ""
|
|
|
|
if os.path.exists(PROPERTIES_FILE):
|
|
try:
|
|
config.read(PROPERTIES_FILE)
|
|
grid_size = config.getint("Editor", "grid_size", fallback=DEFAULT_GRID_SIZE)
|
|
add_timestamp = config.getboolean("Editor", "add_timestamp", fallback=DEFAULT_ADD_TIMESTAMP)
|
|
box_width = config.getint("Editor", "box_width", fallback=DEFAULT_BOX_WIDTH)
|
|
sedona_home = config.get("Editor", "sedona.home", fallback="").strip()
|
|
except Exception:
|
|
pass
|
|
else:
|
|
config["Editor"] = {
|
|
"sedona.home": "",
|
|
"grid_size": str(DEFAULT_GRID_SIZE),
|
|
"add_timestamp": str(DEFAULT_ADD_TIMESTAMP),
|
|
"box_width": str(DEFAULT_BOX_WIDTH)
|
|
}
|
|
try:
|
|
with open(PROPERTIES_FILE, "w") as configfile:
|
|
config.write(configfile)
|
|
except Exception:
|
|
pass
|
|
|
|
box_width = max(MIN_ALLOWED_BOX_WIDTH, min(MAX_ALLOWED_BOX_WIDTH, box_width))
|
|
return sedona_home, grid_size, add_timestamp, box_width
|
|
|
|
|
|
def save_preferences():
|
|
"""Writes the current preference values back to editor.properties."""
|
|
config = configparser.ConfigParser()
|
|
config["Editor"] = {
|
|
"sedona.home": SEDONA_HOME,
|
|
"grid_size": str(GRID_SIZE),
|
|
"add_timestamp": str(ADD_TIMESTAMP),
|
|
"box_width": str(BOX_WIDTH)
|
|
}
|
|
with open(PROPERTIES_FILE, "w") as configfile:
|
|
config.write(configfile)
|
|
|
|
|
|
def base_window_title():
|
|
"""The window title with no file context, which handlers extend with their own suffix."""
|
|
return f"Sedona SAX Offline Program Editor [v{VERSION}]"
|
|
|
|
|
|
def set_status(message, warn=False):
|
|
"""Posts a one-line note to the status bar. A no-op before the UI exists."""
|
|
if status_label is None:
|
|
return
|
|
status_label.config(
|
|
text=message,
|
|
foreground=STATUS_WARN_COLOUR if warn else STATUS_INFO_COLOUR
|
|
)
|
|
|
|
|
|
def clear_status():
|
|
"""Empties the status bar, for when the note no longer describes anything on screen."""
|
|
set_status("")
|
|
|
|
|
|
def mark_dirty():
|
|
"""Single funnel for 'the document changed', including the unsaved-state tint."""
|
|
global is_dirty
|
|
if not is_dirty:
|
|
is_dirty = True
|
|
if canvas is not None:
|
|
canvas.config(bg="#ffe6e6")
|
|
|
|
|
|
def mark_clean():
|
|
"""Clears the unsaved-state marker after a successful save or a fresh load."""
|
|
global is_dirty
|
|
is_dirty = False
|
|
if canvas is not None:
|
|
canvas.config(bg="white")
|
|
|
|
|
|
SEDONA_HOME, GRID_SIZE, ADD_TIMESTAMP, BOX_WIDTH = load_preferences()
|