Wiresheet: left-drag rectangle selection, group move, Ctrl+A select all
Rectangle selection moves to the left button on bare sheet (Ctrl adds); right-click is the menu again. Dragging a selected box by its title bar moves the whole selection as one undoable step. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1344,6 +1344,24 @@ def select_link_on_open_sheet(link_elem):
|
||||
app_state.set_status("That link is not painted on this sheet.", warn=True)
|
||||
|
||||
|
||||
def select_all_boxes(event=None):
|
||||
"""Ctrl+A: selects every box on the open wiresheet."""
|
||||
# Bound application-wide like Del, so a text field keeps its own select-all.
|
||||
if editor_panel_showing():
|
||||
return None
|
||||
focused = app_state.window.focus_get()
|
||||
if focused is not None:
|
||||
if focused.winfo_toplevel() is not app_state.window:
|
||||
return None
|
||||
if focused.winfo_class() in TEXT_ENTRY_CLASSES:
|
||||
return None
|
||||
wiresheet.deselect_link() # links and boxes are never selected together
|
||||
select_canvas_boxes(list(app_state.canvas_comp_map))
|
||||
count = len(app_state.selected_canvas_tags)
|
||||
app_state.set_status(f"{count} component{'s' if count != 1 else ''} selected.")
|
||||
return "break"
|
||||
|
||||
|
||||
def delete_selected_component(event=None):
|
||||
"""Deletes the highlighted canvas component (or selected link) and all its descendants recursively."""
|
||||
# Del is bound application-wide, so it also reaches a Properties field being typed in
|
||||
@@ -1532,7 +1550,16 @@ def on_canvas_click(event):
|
||||
_toggle_canvas_box(group_tag)
|
||||
return
|
||||
|
||||
select_canvas_boxes([])
|
||||
# Pressing one of several selected boxes keeps them all selected, so a drag moves
|
||||
# them together; a press that never moves narrows the selection to it on release.
|
||||
keep_group = (group_tag and not wire_tag and group_tag in app_state.selected_canvas_tags
|
||||
and len(app_state.selected_canvas_tags) > 1)
|
||||
app_state.drag_data["group"] = []
|
||||
app_state.drag_data["moved"] = False
|
||||
app_state.drag_data["collapse_to"] = group_tag if keep_group else None
|
||||
|
||||
if not keep_group:
|
||||
select_canvas_boxes([])
|
||||
if wire_tag:
|
||||
wiresheet.select_link(wire_tag)
|
||||
return
|
||||
@@ -1541,20 +1568,26 @@ def on_canvas_click(event):
|
||||
app_state.drag_data["tag"] = group_tag
|
||||
app_state.drag_data["x"] = cx
|
||||
app_state.drag_data["y"] = cy
|
||||
select_canvas_boxes([group_tag])
|
||||
if keep_group:
|
||||
# Del and the menus still act on the box pressed last.
|
||||
app_state.selected_canvas_tag = group_tag
|
||||
else:
|
||||
select_canvas_boxes([group_tag])
|
||||
|
||||
# Where the box starts out. The drag is measured as an offset from this pair
|
||||
# Where each moving box starts out. The drag is measured as an offset from these
|
||||
# rather than accumulated per motion event, so repeated snapping cannot drift.
|
||||
origin = wiresheet.get_box_origin(group_tag)
|
||||
app_state.drag_data["box_x"] = origin[0] if origin else None
|
||||
app_state.drag_data["box_y"] = origin[1] if origin else None
|
||||
|
||||
# Snapshot the pre-drag meta so the move can be pushed as one undoable step.
|
||||
dragged = app_state.canvas_comp_map.get(group_tag)
|
||||
meta_prop = dragged.find("prop[@name='meta']") if dragged is not None else None
|
||||
app_state.drag_data["meta_before"] = meta_prop.get("val") if meta_prop is not None else None
|
||||
|
||||
# Bring the clicked box clear of any neighbour it happens to sit under.
|
||||
# Each entry also snapshots the pre-drag meta, so the move is one undoable step.
|
||||
moving = list(app_state.selected_canvas_tags) if keep_group else [group_tag]
|
||||
for tag in moving:
|
||||
origin = wiresheet.get_box_origin(tag)
|
||||
comp = app_state.canvas_comp_map.get(tag)
|
||||
meta_prop = comp.find("prop[@name='meta']") if comp is not None else None
|
||||
app_state.drag_data["group"].append({
|
||||
"tag": tag, "origin": origin,
|
||||
"meta_before": meta_prop.get("val") if meta_prop is not None else None,
|
||||
})
|
||||
# Bring the moving boxes clear of any neighbour they happen to sit under.
|
||||
canvas.tag_raise(tag)
|
||||
canvas.tag_raise(group_tag)
|
||||
wiresheet.redraw_links()
|
||||
|
||||
@@ -2071,18 +2104,17 @@ def on_canvas_drag(event):
|
||||
return
|
||||
|
||||
canvas = app_state.canvas
|
||||
group_tag = app_state.drag_data["tag"]
|
||||
cx = canvas.canvasx(event.x)
|
||||
cy = canvas.canvasy(event.y)
|
||||
group = app_state.drag_data.get("group") or []
|
||||
|
||||
box_x = app_state.drag_data.get("box_x")
|
||||
origin = wiresheet.get_box_origin(group_tag)
|
||||
|
||||
if box_x is None or origin is None:
|
||||
# Nothing to align against, so the box just follows the pointer one to one.
|
||||
canvas.move(group_tag, cx - app_state.drag_data["x"], cy - app_state.drag_data["y"])
|
||||
if not group or any(entry["origin"] is None for entry in group):
|
||||
# Nothing to align against, so the boxes just follow the pointer one to one.
|
||||
for tag in [entry["tag"] for entry in group] or [app_state.drag_data["tag"]]:
|
||||
canvas.move(tag, cx - app_state.drag_data["x"], cy - app_state.drag_data["y"])
|
||||
app_state.drag_data["x"] = cx
|
||||
app_state.drag_data["y"] = cy
|
||||
app_state.drag_data["moved"] = True
|
||||
wiresheet.redraw_links()
|
||||
return
|
||||
|
||||
@@ -2091,40 +2123,66 @@ def on_canvas_drag(event):
|
||||
# Canvas coordinates carry the sheet's zoom, so the step and the margin do too.
|
||||
step = app_state.meta_scale * app_state.zoom
|
||||
margin = wiresheet.layout_margin(app_state.meta_scale) * app_state.zoom
|
||||
|
||||
# Follow the pointer, but land the box on a step every step of the way, and keep it
|
||||
# out of the margin strip along the top and left canvas edges.
|
||||
wanted_x = box_x + (cx - app_state.drag_data["x"])
|
||||
wanted_y = app_state.drag_data["box_y"] + (cy - app_state.drag_data["y"])
|
||||
# The box's corner also stays on the sheet, the furthest meta can store it.
|
||||
# A box's corner also stays on the sheet, the furthest meta can store it.
|
||||
limit = wiresheet.META_COORD_MAX * step
|
||||
target_x = min(limit, max(margin, round(wanted_x / step) * step))
|
||||
target_y = min(limit, max(margin, round(wanted_y / step) * step))
|
||||
|
||||
shift_x = target_x - origin[0]
|
||||
shift_y = target_y - origin[1]
|
||||
# Follow the pointer, but keep every moving box out of the margin strip along the
|
||||
# top and left canvas edges and on the sheet -- the whole group stops together, so
|
||||
# the boxes keep their places relative to one another.
|
||||
dx = cx - app_state.drag_data["x"]
|
||||
dy = cy - app_state.drag_data["y"]
|
||||
xs = [entry["origin"][0] for entry in group]
|
||||
ys = [entry["origin"][1] for entry in group]
|
||||
dx = min(limit - max(xs), max(margin - min(xs), dx))
|
||||
dy = min(limit - max(ys), max(margin - min(ys), dy))
|
||||
|
||||
if shift_x or shift_y:
|
||||
canvas.move(group_tag, shift_x, shift_y)
|
||||
# Wires are not part of the dragged group, so they are re-routed against the
|
||||
shifted = False
|
||||
for entry in group:
|
||||
# Land each box on a step every step of the way.
|
||||
start_x, start_y = entry["origin"]
|
||||
target_x = min(limit, max(margin, round((start_x + dx) / step) * step))
|
||||
target_y = min(limit, max(margin, round((start_y + dy) / step) * step))
|
||||
origin = wiresheet.get_box_origin(entry["tag"])
|
||||
if origin is None:
|
||||
continue
|
||||
shift_x = target_x - origin[0]
|
||||
shift_y = target_y - origin[1]
|
||||
if shift_x or shift_y:
|
||||
canvas.move(entry["tag"], shift_x, shift_y)
|
||||
shifted = True
|
||||
|
||||
if shifted:
|
||||
app_state.drag_data["moved"] = True
|
||||
# Wires are not part of the dragged groups, so they are re-routed against the
|
||||
# new positions.
|
||||
wiresheet.redraw_links()
|
||||
|
||||
|
||||
def on_canvas_release(event):
|
||||
collapse_to = app_state.drag_data.pop("collapse_to", None)
|
||||
if collapse_to and not app_state.drag_data.get("moved"):
|
||||
# A click that never moved on one of several selected boxes picks it alone.
|
||||
select_canvas_boxes([collapse_to])
|
||||
|
||||
if not app_state.drag_data["tag"]:
|
||||
return
|
||||
|
||||
canvas = app_state.canvas
|
||||
step = app_state.meta_scale * app_state.zoom
|
||||
group_tag = app_state.drag_data["tag"]
|
||||
origin = wiresheet.get_box_origin(group_tag)
|
||||
margin = wiresheet.layout_margin(app_state.meta_scale) * app_state.zoom
|
||||
limit = wiresheet.META_COORD_MAX * step
|
||||
group = app_state.drag_data.get("group") or [
|
||||
{"tag": app_state.drag_data["tag"], "meta_before": None}]
|
||||
|
||||
if origin is not None:
|
||||
changes = []
|
||||
failed = []
|
||||
for entry in group:
|
||||
group_tag = entry["tag"]
|
||||
origin = wiresheet.get_box_origin(group_tag)
|
||||
if origin is None:
|
||||
continue
|
||||
current_x, current_y = origin
|
||||
|
||||
margin = wiresheet.layout_margin(app_state.meta_scale) * app_state.zoom
|
||||
limit = wiresheet.META_COORD_MAX * step
|
||||
snapped_x = min(limit, max(margin, round(current_x / step) * step))
|
||||
snapped_y = min(limit, max(margin, round(current_y / step) * step))
|
||||
|
||||
@@ -2139,40 +2197,47 @@ def on_canvas_release(event):
|
||||
raw_y = min(wiresheet.META_COORD_MAX, int(round(snapped_y / step)))
|
||||
|
||||
comp_element = app_state.canvas_comp_map.get(group_tag)
|
||||
if comp_element is not None:
|
||||
meta_str = None
|
||||
orig_prop = comp_element.find("prop[@name='meta']")
|
||||
if orig_prop is not None:
|
||||
meta_str = orig_prop.get("val")
|
||||
if comp_element is None:
|
||||
continue
|
||||
meta_str = None
|
||||
orig_prop = comp_element.find("prop[@name='meta']")
|
||||
if orig_prop is not None:
|
||||
meta_str = orig_prop.get("val")
|
||||
|
||||
orig_x, orig_y, _, _, _, _, _, _ = read_meta_property(meta_str)
|
||||
if orig_x != raw_x or orig_y != raw_y:
|
||||
meta_before = app_state.drag_data.get("meta_before", meta_str)
|
||||
update_meta_property(comp_element, meta_str, raw_x, raw_y)
|
||||
if app_state.live_mode:
|
||||
new_meta = comp_element.find("prop[@name='meta']")
|
||||
if new_meta is None or not live_hooks.live_set_prop_value(
|
||||
comp_element, "meta", new_meta.get("val", "0")):
|
||||
# Back where the device still has it, so the sheet tells the truth.
|
||||
if meta_str is None:
|
||||
remove_prop(comp_element, "meta")
|
||||
else:
|
||||
set_prop_value(comp_element, "meta", meta_str)
|
||||
app_state.drag_data["tag"] = None
|
||||
wiresheet.render_current_sheet()
|
||||
live_hooks.warn_failure(
|
||||
"Move", f"{comp_element.get('name', 'The component')} was not "
|
||||
"moved on the device")
|
||||
return
|
||||
command_class = (undo.LiveMetaMoveCommand if app_state.live_mode
|
||||
else undo.MetaMoveCommand)
|
||||
undo.push(command_class(
|
||||
[(comp_element, meta_before, raw_x, raw_y)],
|
||||
label=f"move {comp_element.get('name', 'component')}"
|
||||
))
|
||||
app_state.mark_dirty()
|
||||
orig_x, orig_y, _, _, _, _, _, _ = read_meta_property(meta_str)
|
||||
if orig_x == raw_x and orig_y == raw_y:
|
||||
continue
|
||||
meta_before = entry.get("meta_before") or meta_str
|
||||
update_meta_property(comp_element, meta_str, raw_x, raw_y)
|
||||
if app_state.live_mode:
|
||||
new_meta = comp_element.find("prop[@name='meta']")
|
||||
if new_meta is None or not live_hooks.live_set_prop_value(
|
||||
comp_element, "meta", new_meta.get("val", "0")):
|
||||
# Back where the device still has it, so the sheet tells the truth.
|
||||
if meta_str is None:
|
||||
remove_prop(comp_element, "meta")
|
||||
else:
|
||||
set_prop_value(comp_element, "meta", meta_str)
|
||||
failed.append(comp_element.get("name", "?"))
|
||||
continue
|
||||
changes.append((comp_element, meta_before, raw_x, raw_y))
|
||||
|
||||
app_state.drag_data["tag"] = None
|
||||
app_state.drag_data["group"] = []
|
||||
|
||||
if changes:
|
||||
command_class = (undo.LiveMetaMoveCommand if app_state.live_mode
|
||||
else undo.MetaMoveCommand)
|
||||
label = (f"move {changes[0][0].get('name', 'component')}" if len(changes) == 1
|
||||
else f"move {len(changes)} components")
|
||||
undo.push(command_class(changes, label=label))
|
||||
app_state.mark_dirty()
|
||||
|
||||
if failed:
|
||||
wiresheet.render_current_sheet()
|
||||
live_hooks.warn_failure("Move", f"{', '.join(failed)} not moved on the device")
|
||||
return
|
||||
|
||||
wiresheet.redraw_links()
|
||||
|
||||
|
||||
@@ -3249,6 +3314,8 @@ SHORTCUTS = [
|
||||
("9", do_zoom_fit),
|
||||
("KP_9", do_zoom_fit),
|
||||
("p", wiresheet.print_sheet),
|
||||
("a", select_all_boxes),
|
||||
("A", select_all_boxes),
|
||||
]
|
||||
for key, handler in SHORTCUTS:
|
||||
window.bind_all(f"<Command-{key}>", handler)
|
||||
|
||||
Reference in New Issue
Block a user