From 01caa47fd1a4295c6327fc470d4e8ebae31a08f0 Mon Sep 17 00:00:00 2001 From: Artus Date: Fri, 3 Apr 2020 14:57:17 +0200 Subject: [PATCH] moves data, updates gui --- xerus/data.py | 68 ++++++++++++++++++++++++ xerus/gui.py | 137 ++++++++++++++++++++----------------------------- xerus/notes.py | 16 ------ 3 files changed, 124 insertions(+), 97 deletions(-) create mode 100644 xerus/data.py delete mode 100644 xerus/notes.py diff --git a/xerus/data.py b/xerus/data.py new file mode 100644 index 0000000..530850e --- /dev/null +++ b/xerus/data.py @@ -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 + diff --git a/xerus/gui.py b/xerus/gui.py index 3065943..0f46db1 100644 --- a/xerus/gui.py +++ b/xerus/gui.py @@ -3,6 +3,8 @@ import tkinter as tk from tkinter import ttk +from .data import Notes + def new_frame_horiz(parent): frame = tk.Frame(parent) frame.pack(fill=tk.X, expand=True) @@ -11,60 +13,23 @@ def new_frame_horiz(parent): def render_notes(notes, text_area): # Text - for note in notes: - text_area.insert(tk.INSERT, f"{note[0]}. ", "t") - text_area.insert(tk.INSERT, f"{note[1]}\n") - text_area.insert(tk.INSERT, "\n") + for date, notes in notes.items(): + text_area.insert(tk.INSERT, f"{date}\n\n", "d") + for note in notes: + 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): def __init__(self, master=None): super().__init__(master) - self.current_category = 0 - self.current_subject = None - self.pack() + self.pack(fill=tk.BOTH, expand=True) def run(self): - self.subjects = ["Henri", "Étienne", "Julia"] - self.categories = ["Santé", "Comportement", "Quotidien", "Infos", "Activité"] + self.notes = Notes(None) self._create_widgets() self.mainloop() @@ -77,50 +42,60 @@ class App(tk.Frame): def _set_date_range(self): 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 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("<>", self._refresh_text) + return cbox + + # Subject and Category selection self.header = tk.Frame(self) - self.header.pack(side=tk.TOP, fill=tk.X, expand=True) - - # Subjects choice list - 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) - - + self.header.pack(side=tk.TOP, fill=tk.X) + self.subjectChoice = combo_with_buttons("Résident", values=self.notes.subjects()) + self.categoryChoice = combo_with_buttons("Catégorie", values=self.notes.categories()) # Text view textView = tk.Frame(self) - textView.pack() + textView.pack(fill=tk.BOTH, expand=True) scrollbar = tk.Scrollbar(textView) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) - text = tk.Text(textView, yscrollcommand=scrollbar.set) - scrollbar.config(command=text.yview) - text.tag_config("d", underline=1) # Dates - text.tag_config("t", foreground="grey") # Times - text.pack() + self.text = tk.Text(textView, yscrollcommand=scrollbar.set) + self.text.tag_config("d", underline=1) # Dates + self.text.tag_config("t", foreground="grey") # Times + self.text.pack(fill=tk.BOTH, expand=True) + 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) diff --git a/xerus/notes.py b/xerus/notes.py deleted file mode 100644 index 281b69e..0000000 --- a/xerus/notes.py +++ /dev/null @@ -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 -