feature/session #27

Merged
Shikiryu merged 7 commits from feature/session into main 2023-06-02 12:24:15 +02:00
4 changed files with 110 additions and 9 deletions
Showing only changes of commit 25314fde08 - Show all commits

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ theme.py
!/dist/drawingtraining !/dist/drawingtraining
/*.spec /*.spec
/build/ /build/
/config.json

49
src/entity/config.py Normal file
View File

@ -0,0 +1,49 @@
import json
import os
from pathlib import Path
from configparser import *
import typing
class Config(object):
_CONFIG_FILE: typing.Optional[str] = None
_CONFIG: typing.Optional[dict] = None
def __init__(self, config_file=None):
if config_file is None:
config_file = "config.json"
# Check that specified config file exists
if not os.path.exists(config_file):
Path(config_file).touch()
with open(config_file, 'w') as configfile:
json.dump({"session": []}, configfile)
Config._CONFIG_FILE = config_file
with open(config_file, 'r') as f:
Config._CONFIG = json.load(f)
@staticmethod
def save():
with open(Config._CONFIG_FILE, 'w') as configfile:
json.dump(Config._CONFIG, configfile)
@staticmethod
def get_config_file() -> str:
return Config._CONFIG_FILE
@staticmethod
def get_required_env_var(env_var: str) -> str:
if env_var not in os.environ:
raise Exception(f"Please set the {env_var} environment variable")
return os.environ[env_var]
@staticmethod
def get_required_config_var(config_var: str) -> str:
assert Config._CONFIG
if config_var not in Config._CONFIG:
raise Exception(f"Please set the {config_var} variable in the config file {Config._CONFIG_FILE}")
return Config._CONFIG[config_var]

View File

@ -1,6 +1,8 @@
from tkinter import * from tkinter import *
from tkinter import filedialog from tkinter import filedialog
from pathlib import Path from pathlib import Path
from src.entity.config import Config
from src.util import Util from src.util import Util
from functools import partial from functools import partial
from src.window.image import ImageWindow from src.window.image import ImageWindow
@ -10,6 +12,7 @@ from src.window.session import SessionWindow
class App: class App:
def __init__(self, root): def __init__(self, root):
super().__init__() super().__init__()
self.config = Config()
self.session_window = None self.session_window = None
self.image_window = None self.image_window = None
self.root = root self.root = root

View File

@ -1,11 +1,16 @@
from functools import partial from functools import partial
from tkinter import * from tkinter import *
from src.entity.config import Config
from src.entity.session.drawing import DrawingElement from src.entity.session.drawing import DrawingElement
from src.util import Util
class SessionWindow: class SessionWindow:
def __init__(self, app): def __init__(self, app):
self.total_row_time_value_label = None
self.down_button = None
self.up_button = None
self.timer_input = None self.timer_input = None
self.number_of_drawings_input = None self.number_of_drawings_input = None
self.selected_element_index = -1 self.selected_element_index = -1
@ -32,17 +37,30 @@ class SessionWindow:
def update_session_list(self): def update_session_list(self):
self.reset_list() self.reset_list()
for i, session in enumerate(self.list_in_session): for i, session in enumerate(self.list_in_session):
bg = "blue" if i == self.selected_element_index else "grey85"
new_session = Button(self.left_column, new_session = Button(self.left_column,
text=session.number_of_drawings + " drawings of "+session.timer+"s each", text=session.number_of_drawings + " drawings of " + session.timer + "s each",
command=partial(self.edit_element, i) command=partial(self.edit_element, i),
bg=bg
) )
new_session.grid(row=i, column=0) new_session.grid(row=i, column=0)
self.add_button = Button(self.left_column, text="+", command=self.add_element) button_frame = Frame(self.left_column)
self.add_button.grid() self.add_button = Button(button_frame, text="+", command=self.add_element)
self.add_button.pack(side=LEFT, padx=0, pady=0)
self.delete_button = Button(self.left_column, text="-", command=self.remove_element) self.delete_button = Button(button_frame, text="-", command=self.remove_element)
self.delete_button.grid() self.delete_button.pack(side=LEFT, padx=0, pady=0)
self.up_button = Button(button_frame, text="^", command=self.up_element)
self.up_button.pack(side=LEFT, padx=0, pady=0)
self.down_button = Button(button_frame, text="v", command=self.down_element)
self.down_button.pack(side=LEFT, padx=0, pady=0)
self.save_session_button = Button(button_frame, text="v", command=self.save_session)
self.save_session_button.pack(side=LEFT, padx=0, pady=0)
button_frame.grid()
def reset_element(self): def reset_element(self):
for widget in self.right_column.winfo_children(): for widget in self.right_column.winfo_children():
@ -53,9 +71,11 @@ class SessionWindow:
widget.destroy() widget.destroy()
def update_element(self, index=None): def update_element(self, index=None):
self.reset_element()
if index is not None: if index is not None:
self.selected_element_index = index self.selected_element_index = index
element = self.list_in_session[self.selected_element_index] element = self.list_in_session[self.selected_element_index]
self.update_session_list()
else: else:
element = DrawingElement() element = DrawingElement()
@ -64,24 +84,32 @@ class SessionWindow:
self.number_of_drawings_input = Entry(self.right_column) self.number_of_drawings_input = Entry(self.right_column)
self.number_of_drawings_input.insert(INSERT, element.number_of_drawings) self.number_of_drawings_input.insert(INSERT, element.number_of_drawings)
self.number_of_drawings_input.bind("<KeyRelease>", lambda e: self.update_local_timer(e))
self.number_of_drawings_input.pack() self.number_of_drawings_input.pack()
self.number_of_drawings_input.focus_set()
timer_label = Label(self.right_column, text="Time per drawing in seconds") timer_label = Label(self.right_column, text="Time per drawing in seconds")
timer_label.pack() timer_label.pack()
self.timer_input = Entry(self.right_column) self.timer_input = Entry(self.right_column)
self.timer_input.insert(INSERT, element.timer) self.timer_input.insert(INSERT, element.timer)
self.timer_input.bind("<KeyRelease>", lambda e: self.update_local_timer(e))
self.timer_input.pack() self.timer_input.pack()
row_time = IntVar()
total_row_time_label = Label(self.right_column, text="Total row time") total_row_time_label = Label(self.right_column, text="Total row time")
total_row_time_label.pack() total_row_time_label.pack()
total_row_time_value_label = Label(self.right_column, textvariable=row_time) self.total_row_time_value_label = Label(self.right_column, text="")
total_row_time_value_label.pack() self.total_row_time_value_label.pack()
self.update_local_timer()
save_button = Button(self.right_column, text="Save", command=self.save) save_button = Button(self.right_column, text="Save", command=self.save)
save_button.pack() save_button.pack()
def update_local_timer(self, event=None):
self.total_row_time_value_label.config(
text=Util.format_seconds(int(self.timer_input.get()) * int(self.number_of_drawings_input.get()))
)
def remove_element(self): def remove_element(self):
self.list_in_session.pop(self.selected_element_index) self.list_in_session.pop(self.selected_element_index)
self.selected_element_index = -1 self.selected_element_index = -1
@ -91,8 +119,22 @@ class SessionWindow:
self.update_element(i) self.update_element(i)
def add_element(self): def add_element(self):
self.selected_element_index = -1
self.update_session_list()
self.update_element() self.update_element()
def up_element(self):
self.list_in_session.insert(self.selected_element_index - 1,
self.list_in_session.pop(self.selected_element_index))
self.selected_element_index -= 1
self.update_session_list()
def down_element(self):
self.list_in_session.insert(self.selected_element_index + 1,
self.list_in_session.pop(self.selected_element_index))
self.selected_element_index += 1
self.update_session_list()
def save(self): def save(self):
element = DrawingElement() if self.selected_element_index < 0 \ element = DrawingElement() if self.selected_element_index < 0 \
else self.list_in_session[self.selected_element_index] else self.list_in_session[self.selected_element_index]
@ -111,3 +153,9 @@ class SessionWindow:
def save_on_closing(self): def save_on_closing(self):
# TODO save # TODO save
self.window.destroy() self.window.destroy()
def save_session(self):
for element in self.list_in_session:
Config._CONFIG['session'].append(element)
Config.save()
self.window.destroy()