Skip to content

Refactor: modernize helper_methods + port Mainsail layer formulas to jobStatusPage#208

Merged
HugoCLSC merged 13 commits into
devfrom
refactor/helper-methods
Jul 1, 2026
Merged

Refactor: modernize helper_methods + port Mainsail layer formulas to jobStatusPage#208
HugoCLSC merged 13 commits into
devfrom
refactor/helper-methods

Conversation

@gmmcosta15

@gmmcosta15 gmmcosta15 commented Apr 13, 2026

Copy link
Copy Markdown
Collaborator

Description

  • Feature
  • Bug fix
  • Code refactor
  • Documentation

Refactors helper_methods.py and jobStatusPage.py, and adds a full unit test suite for JobStatusWidget.

jobStatusPage.py

  • _VALID_STATES / _INVALID_STATES -> frozenset class constants (avoid per-call realloc)
  • on_gcode_move_update - 7 individual guards collapsed into one compound or guard
  • on_print_stats_update - if/if -> if/elif chains (mutually exclusive branches)
  • _post_event helper - DRY up duplicated QApplication.postEvent pattern
  • _load_thumbnails - manual loop -> walrus list comprehension
  • Removed no-op QPixmap.scaled() to identical dimensions
  • Removed spurious self.show() on thumbnail collapse

helper_methods.py

  • Mainsail layer formula extracted to calculate_current_layer / calculate_max_layers
  • jobStatusPage updated to call the new helpers

tests/widgets/test_job_status_page_unit.py - 40 unit tests

  • TestOnPrintStart (4), TestHandlePrintState (7), TestOnPrintStatsUpdate (6)
  • TestOnGcodeMoveUpdate (8), TestVirtualSdcardUpdate (3)
  • TestPauseResumePrint (6), TestHandleCancel (4), TestOnFileInfo (5)

tests/widgets/conftest.py

  • Ensures BlocksScreen/ on sys.path for lib.* imports
  • Clears empty lib.* namespace stubs registered by the network conftest so widget tests can coexist in the full make test-fast run

Motivation

The refactors reduce duplicated code and allocation overhead in the hottest print-progress paths. The test suite documents the expected slot behaviour and will catch regressions in the state machine, signal wiring, and layer-calculation logic.

Tests

Widget tests alone: 40 passed

@gmmcosta15 gmmcosta15 requested a review from HugoCLSC April 13, 2026 09:41
@gmmcosta15 gmmcosta15 self-assigned this Apr 13, 2026
@gmmcosta15 gmmcosta15 marked this pull request as ready for review April 13, 2026 09:41
@gmmcosta15 gmmcosta15 marked this pull request as draft April 13, 2026 12:59
@gmmcosta15 gmmcosta15 marked this pull request as draft April 13, 2026 12:59
- 40 unit tests across 8 test classes covering all public slots: on_print_start, _handle_print_state, on_print_stats_update, on_gcode_move_update, virtual_sdcard_update, pause_resume_print, handleCancel, on_fileinfo
- tests/widgets/conftest.py: fix cross-suite isolation (clears empty lib.* stubs registered by network conftest, ensures BlocksScreen/on sys.path)
- jobStatusPage: frozenset class constants, compound guard, if/elif chains, _post_event helper, walrus list comprehension, removed no-op QPixmap.scaled, removed spurious show() on collapse
@gmmcosta15 gmmcosta15 marked this pull request as ready for review April 13, 2026 16:42
@gmmcosta15 gmmcosta15 marked this pull request as draft April 13, 2026 16:43
@gmmcosta15 gmmcosta15 marked this pull request as ready for review April 27, 2026 15:09
@HugoCLSC HugoCLSC merged commit 137c1a2 into dev Jul 1, 2026
8 checks passed
@HugoCLSC HugoCLSC deleted the refactor/helper-methods branch July 1, 2026 13:49
HugoCLSC added a commit that referenced this pull request Jul 3, 2026
…slots (#258)

# Description

Select the type:

- [ ] Feature
- [x] Bug fix
- [ ] Code refactor
- [ ] Documentation

Follow-up hardening for the `jobStatusPage` layer logic introduced in
**#208**. Fixes a field-observed bug where the current-layer counter
jumped whenever the toolhead raised Z (travel moves / Z-hop) to reach
another region of the plate, and aligns the layer-source priority with
Mainsail's getters.

- Reject transient Z rises: the current layer now commits an advance
only once Z has settled (confirmed across two samples) and never
regresses, so Z-hop and travel moves no longer bump the layer number.
- Respect reported totals: a Klipper-reported
`print_stats.info.total_layer` (or `current_file.layer_count`) is no
longer overwritten by the geometry estimate, matching Mainsail's
`getPrintMaxLayers` priority.
- `on_flowguard_update`: renamed params that shadowed the `str`/`dict`
builtins, added type hints, and applied each key independently so a
partial delta no longer raises `KeyError`.
- `handleCancel`: replaced the disconnect+reconnect dance with a single
`UniqueConnection` (guarded) connect.
- `showEvent`: now calls `super().showEvent()`.
- Simplified `toggle_thumbnail_expansion`, merged the duplicate
valid-state branch in `_handle_print_state`, and tidied the print-stats
dispatch.

# Motivation

The Z-based layer fallback read the instantaneous
`gcode_move.gcode_position[2]`, so any non-print Z motion (Z-hop, travel
to a taller area) was interpreted as a layer change and the counter
flickered. Since `gcode_position` is published on essentially every
move, a two-sample "Z settled" filter reliably distinguishes a real
layer advance from a transient hop while keeping the counter monotonic.
The layer-source priority was also corrected to match Mainsail so a
reported total layer count is never clobbered by the geometry estimate.

# Tests

New/updated cases: transient Z-hop does not bump the layer, layer never
regresses, reported `total_layer` not overridden by estimate, flowguard
partial vs full payload, plus the two-sample settle behavior.

Co-authored-by: Hugo Costa <hugo.santos.costa@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants