moves data, updates gui

This commit is contained in:
2020-04-03 14:57:17 +02:00
parent f56b41f168
commit 01caa47fd1
3 changed files with 124 additions and 97 deletions

68
xerus/data.py Normal file
View File

@@ -0,0 +1,68 @@
# -*- coding:utf-8 -*-
def test_data():
return { "Henri" :
{ "Santé": { "23 mars 2020": [] },
"Comportement": { "23 mars 2020": [] },
"Quotidien": {
"23 mars 2020": [
( "Matin", "Bien mangé ce matin. Sorti faire un tour pour voir les poules" ),
( "Après-midi", "Rien de particulier" ),
],
"24 mars 2020": [
( "Matin", "Bien mangé ce matin. Sorti faire un tour pour voir les poules", ),
( "Après-midi", "Rien de particulier" ),
],
"25 mars 2020": [
( "Matin", "Bien mangé ce matin. Sorti faire un tour pour voir les poules" ),
( "Après-midi", "Rien de particulier" ),
],
},
"Infos": { "23 mars 2020": [] },
"Activité": { "23 mars 2020": [] },
},
"Étienne":
{ "Santé": { "23 mars 2020": [] },
"Comportement": { "23 mars 2020": [] },
"Quotidien": { "23 mars 2020": [] },
"Infos": { "23 mars 2020": [] },
"Activité": { "23 mars 2020": [] },
},
"Julia" :
{ "Santé": { "23 mars 2020": [] },
"Comportement": { "23 mars 2020": [] },
"Quotidien": { "23 mars 2020": [] },
"Infos": { "23 mars 2020": [] },
"Activité": { "23 mars 2020": [] },
},
}
class Notes(dict):
""" Store of notes grouped by subject and category """
def __init__(self, source):
self._categories = None
super().__init__([data for data in test_data().items()])
def subjects(self):
""" Returns list of subjects """
return list(self.keys())
def categories(self):
""" Returns list of categories """
if not self._categories:
self._categories = ["Santé", "Comportement", "Quotidien", "Infos", "Activité"]
return self._categories
def get(self, subject_idx, category_idx, date_range=None):
""" Filter notes by subject, category and date """
subject = list(self.subjects())[subject_idx]
# TODO: deduplicate categories by using a tuple
category = list(self.categories())[category_idx]
notes = self[subject][category]
if date_range is not None:
# TODO: filter by date
pass
return notes

View File

@@ -3,6 +3,8 @@
import tkinter as tk import tkinter as tk
from tkinter import ttk from tkinter import ttk
from .data import Notes
def new_frame_horiz(parent): def new_frame_horiz(parent):
frame = tk.Frame(parent) frame = tk.Frame(parent)
frame.pack(fill=tk.X, expand=True) frame.pack(fill=tk.X, expand=True)
@@ -11,60 +13,23 @@ def new_frame_horiz(parent):
def render_notes(notes, text_area): def render_notes(notes, text_area):
# Text # Text
for note in notes: for date, notes in notes.items():
text_area.insert(tk.INSERT, f"{note[0]}. ", "t") text_area.insert(tk.INSERT, f"{date}\n\n", "d")
text_area.insert(tk.INSERT, f"{note[1]}\n") for note in notes:
text_area.insert(tk.INSERT, "\n") text_area.insert(tk.INSERT, f"{note[0]}. ", "t")
text_area.insert(tk.INSERT, f"{note[1]}\n")
text_area.insert(tk.INSERT, "\n")
DATA = { "Henri" :
{ "Santé": { "23 mars 2020": [] },
"Comportement": { "23 mars 2020": [] },
"Quotidien": {
"23 mars 2020": [
( "Matin", "Bien mangé ce matin. Sorti faire un tour pour voir les poules" ),
( "Après-midi", "Rien de particulier" ),
],
"24 mars 2020": [
( "Matin", "Bien mangé ce matin. Sorti faire un tour pour voir les poules", ),
( "Après-midi", "Rien de particulier" ),
],
"25 mars 2020": [
( "Matin", "Bien mangé ce matin. Sorti faire un tour pour voir les poules" ),
( "Après-midi", "Rien de particulier" ),
],
},
"Infos": { "23 mars 2020": [] },
"Activité": { "23 mars 2020": [] },
},
"Étienne":
{ "Santé": { "23 mars 2020": [] },
"Comportement": { "23 mars 2020": [] },
"Quotidien": { "23 mars 2020": [] },
"Infos": { "23 mars 2020": [] },
"Activité": { "23 mars 2020": [] },
},
"Julia" :
{ "Santé": { "23 mars 2020": [] },
"Comportement": { "23 mars 2020": [] },
"Quotidien": { "23 mars 2020": [] },
"Infos": { "23 mars 2020": [] },
"Activité": { "23 mars 2020": [] },
},
}
class App(tk.Frame): class App(tk.Frame):
def __init__(self, master=None): def __init__(self, master=None):
super().__init__(master) super().__init__(master)
self.current_category = 0 self.pack(fill=tk.BOTH, expand=True)
self.current_subject = None
self.pack()
def run(self): def run(self):
self.subjects = ["Henri", "Étienne", "Julia"] self.notes = Notes(None)
self.categories = ["Santé", "Comportement", "Quotidien", "Infos", "Activité"]
self._create_widgets() self._create_widgets()
self.mainloop() self.mainloop()
@@ -77,50 +42,60 @@ class App(tk.Frame):
def _set_date_range(self): def _set_date_range(self):
pass pass
def _refresh_text(self, event):
self.text.config(state=tk.NORMAL)
self.text.delete(1.0, tk.END)
render_notes( self.notes.get(self.subjectChoice.current(), self.categoryChoice.current())
, self.text)
self.text.config(state=tk.DISABLED)
def _create_widgets(self): def _create_widgets(self):
def combo_with_buttons(label, values):
""" Builds a combo with cycling buttons, and returns the combobox """
def cycle_choice(cbox, reversed=False):
def do_it():
idx = cbox.current()
max_idx = len(cbox["values"]) - 1
if reversed:
idx = idx - 1 if idx > 0 else max_idx
else:
idx = idx + 1 if idx < max_idx else 0
cbox.current(idx)
self._refresh_text(None)
return do_it
# Build
frame = new_frame_horiz(self.header)
label = tk.Label(frame, text=label + " : ")
label.pack(side=tk.LEFT)
cbox = ttk.Combobox(frame, values=values, state="readonly")
prev_button = tk.Button(frame, text="<", command=cycle_choice(cbox, reversed=True))
next_button = tk.Button(frame, text=">", command=cycle_choice(cbox))
prev_button.pack(side=tk.LEFT)
cbox.pack(side=tk.LEFT, fill=tk.X, expand=True)
next_button.pack(side=tk.LEFT)
# Configure
cbox.current(0)
cbox.bind("<<ComboboxSelected>>", self._refresh_text)
return cbox
# Subject and Category selection
self.header = tk.Frame(self) self.header = tk.Frame(self)
self.header.pack(side=tk.TOP, fill=tk.X, expand=True) self.header.pack(side=tk.TOP, fill=tk.X)
self.subjectChoice = combo_with_buttons("Résident", values=self.notes.subjects())
# Subjects choice list self.categoryChoice = combo_with_buttons("Catégorie", values=self.notes.categories())
subjectFrame = new_frame_horiz(self.header)
subjectLabel = tk.Label(subjectFrame, text="Résident")
subjectLabel.pack(side=tk.LEFT)
subjectChoice = ttk.Combobox(subjectFrame, values=self.subjects)
subjectChoice.pack(side=tk.RIGHT, expand=True, fill=tk.X)
# Category choice list
categoryFrame = new_frame_horiz(self.header)
categoryLabel = tk.Label(categoryFrame, text="Catégorie")
categoryLabel.pack(side=tk.LEFT)
categoryChoice = ttk.Combobox(categoryFrame, values=self.categories)
categoryChoice.current(0)
toLeft = tk.Button(categoryFrame, text="<")
toRight = tk.Button(categoryFrame, text=">")
toRight.pack(side=tk.RIGHT)
categoryChoice.pack(side=tk.RIGHT, fill=tk.X, expand=True)
toLeft.pack(side=tk.RIGHT)
# Text view # Text view
textView = tk.Frame(self) textView = tk.Frame(self)
textView.pack() textView.pack(fill=tk.BOTH, expand=True)
scrollbar = tk.Scrollbar(textView) scrollbar = tk.Scrollbar(textView)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y) scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
text = tk.Text(textView, yscrollcommand=scrollbar.set) self.text = tk.Text(textView, yscrollcommand=scrollbar.set)
scrollbar.config(command=text.yview) self.text.tag_config("d", underline=1) # Dates
text.tag_config("d", underline=1) # Dates self.text.tag_config("t", foreground="grey") # Times
text.tag_config("t", foreground="grey") # Times self.text.pack(fill=tk.BOTH, expand=True)
text.pack() scrollbar.config(command=self.text.yview)
self._refresh_text(None)
current_subject = 0
current_category = 0
for date, notes in DATA[self.subjects[current_subject]][self.categories[current_category]].items():
# Date header
text.insert(tk.INSERT, f"{date}\n\n", "d")
render_notes(notes, text)
text.config(state=tk.DISABLED)

View File

@@ -1,16 +0,0 @@
# -*- coding: utf-8 -*-
from enum import Enum
Category = Enum('Category', 'SANTE COMPORTEMENT QUOTIDIEN ACTIVITE INFOS')
@dataclass
class Note:
""" A chunk of text related to a subject and a category
It contains the content of a cell in xls table
"""
subject: str
category: int
text: str