166 lines
6.0 KiB
Python
166 lines
6.0 KiB
Python
import io
|
|
import tkinter as tk
|
|
from tkinter import Frame, Canvas, ttk, PhotoImage
|
|
|
|
from PIL import ImageChops
|
|
|
|
from ..config import DISPLAY_TYPES
|
|
from . import NumberView, CircleView, TimerView
|
|
|
|
class MainView(tk.Frame):
|
|
def __init__(self, parent,
|
|
tare_command=None,
|
|
calibrate_command=None,
|
|
**kwargs):
|
|
super().__init__(parent, **kwargs)
|
|
self.views = []
|
|
self.timer_view = None # Timer view is always active
|
|
self.tare_command = tare_command
|
|
self.calibrate_command = calibrate_command
|
|
|
|
# Button press detection variables
|
|
self.left_press_start = None
|
|
self.right_press_start = None
|
|
self.long_press_threshold = 1000 # 1 second in milliseconds
|
|
self.left_press_job = None
|
|
self.right_press_job = None
|
|
|
|
self.actions = Frame(self)
|
|
self.actions.pack()
|
|
self.calibrate_button = ttk.Button(self.actions, text="Calibrate", command=calibrate_command)
|
|
self.calibrate_button.pack()
|
|
|
|
# self.tare_button = ttk.Button(self.actions, text="Tare", command=tare_command)
|
|
# self.tare_button.pack()
|
|
|
|
self.im_size = (168, 144)
|
|
self.center = (168 // 2, 144 // 2)
|
|
|
|
# Create timer view that's always active
|
|
self.timer_view = TimerView(self.actions, self.im_size, self.center)
|
|
|
|
self.canvas = Canvas(self, width=168, height=144, background='white',
|
|
highlightthickness=1, highlightbackground="black")
|
|
self.canvas.pack()
|
|
|
|
self.device_frame = Frame(self.actions)
|
|
self.left_button = ttk.Button(self.device_frame, text="Left")
|
|
# Bind mouse events for press detection
|
|
self.left_button.bind("<Button-1>", self._left_button_press_start)
|
|
self.left_button.bind("<ButtonRelease-1>", self._left_button_press_end)
|
|
self.left_button.pack(side="left")
|
|
|
|
self.right_button = ttk.Button(self.device_frame, text="Right")
|
|
# Bind mouse events for press detection
|
|
self.right_button.bind("<Button-1>", self._right_button_press_start)
|
|
self.right_button.bind("<ButtonRelease-1>", self._right_button_press_end)
|
|
self.right_button.pack(side="right")
|
|
self.device_frame.pack()
|
|
|
|
|
|
################ VIEW MANAGEMENT ################
|
|
|
|
def update_views(self, selected_types: DISPLAY_TYPES):
|
|
# Clear only the selectable views, not the timer
|
|
for v in self.views:
|
|
if v.ui is not None:
|
|
v.ui.destroy()
|
|
self.views.clear()
|
|
|
|
if selected_types & DISPLAY_TYPES.NUMBER:
|
|
number_view = NumberView(self.actions, self.im_size, self.center)
|
|
self.views.append(number_view)
|
|
|
|
if selected_types & DISPLAY_TYPES.CIRCLE:
|
|
circle_view = CircleView(self.actions, self.im_size, self.center)
|
|
self.views.append(circle_view)
|
|
|
|
|
|
def refresh(self, weight: float):
|
|
ims = []
|
|
|
|
# Always include timer view
|
|
if self.timer_view:
|
|
timer_im = self.timer_view.update_weight(weight)
|
|
ims.append(timer_im)
|
|
|
|
# Add other selected views
|
|
for view in self.views:
|
|
im = view.update_weight(weight)
|
|
ims.append(im)
|
|
|
|
self.canvas.delete("all")
|
|
# Combine images by logical_and
|
|
if ims:
|
|
combined_im = ims[0]
|
|
for im in ims[1:]:
|
|
combined_im = ImageChops.invert(ImageChops.logical_xor(combined_im, im))
|
|
|
|
# Convert PIL image to bytes
|
|
buffer = io.BytesIO()
|
|
combined_im.save(buffer, format='PNG')
|
|
buffer.seek(0)
|
|
|
|
# Load into PhotoImage and display on canvas
|
|
self.photo = PhotoImage(data=buffer.getvalue())
|
|
self.canvas.create_image(0, 0, anchor="nw", image=self.photo)
|
|
|
|
|
|
|
|
|
|
|
|
########### BUTTON PRESS HANDLING ###########
|
|
|
|
def _left_button_press_start(self, event):
|
|
"""Handle left button press start"""
|
|
self.left_press_start = self.after_idle(lambda: None) # Get current time reference
|
|
# Schedule long press detection
|
|
self.left_press_job = self.after(self.long_press_threshold, self._left_long_press_detected)
|
|
|
|
def _left_button_press_end(self, event):
|
|
"""Handle left button press end"""
|
|
if self.left_press_job:
|
|
self.after_cancel(self.left_press_job)
|
|
self.left_press_job = None
|
|
# If we get here, it was a short press
|
|
self.left_button_press()
|
|
self.left_press_start = None
|
|
|
|
def _left_long_press_detected(self):
|
|
"""Called when long press threshold is reached for left button"""
|
|
self.left_press_job = None
|
|
self.left_button_long_press()
|
|
|
|
def _right_button_press_start(self, event):
|
|
"""Handle right button press start"""
|
|
self.right_press_start = self.after_idle(lambda: None) # Get current time reference
|
|
# Schedule long press detection
|
|
self.right_press_job = self.after(self.long_press_threshold, self._right_long_press_detected)
|
|
|
|
def _right_button_press_end(self, event):
|
|
"""Handle right button press end"""
|
|
if self.right_press_job:
|
|
self.after_cancel(self.right_press_job)
|
|
self.right_press_job = None
|
|
# If we get here, it was a short press
|
|
self.right_button_press()
|
|
self.right_press_start = None
|
|
|
|
def _right_long_press_detected(self):
|
|
"""Called when long press threshold is reached for right button"""
|
|
self.right_press_job = None
|
|
self.right_button_long_press()
|
|
|
|
def left_button_press(self):
|
|
self.tare_command()
|
|
|
|
def right_button_press(self):
|
|
self.timer_view.toggle_timer()
|
|
|
|
def left_button_long_press(self):
|
|
"""Handle long press on left button"""
|
|
print("Left button long press") # Replace with your logic
|
|
pass
|
|
|
|
def right_button_long_press(self):
|
|
self.timer_view.reset_timer() |