Skip to content

Commit

Permalink
Progress on custom tables
Browse files Browse the repository at this point in the history
  • Loading branch information
ajkolenc committed Jan 10, 2025
1 parent f0ec775 commit 9c14d33
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 199 deletions.
48 changes: 14 additions & 34 deletions source/package/pathways_app/controls/actions_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from controls.metric_value import MetricValueCell
from controls.sortable_header import SortableHeader, SortMode
from controls.styled_button import StyledButton
from controls.styled_table import StyledTable
from controls.styled_table import StyledTable, TableCell, TableColumn

from adaptation_pathways.app.model.action import Action
from adaptation_pathways.app.model.pathways_project import PathwaysProject
Expand Down Expand Up @@ -114,36 +114,20 @@ def on_new_action(self, _):
self.update()

def update_table(self):
sort_mode = SortableHeader.get_sort_mode(self.project.action_sorting)
sort_key = self.project.action_sorting.sort_key

sortable_headers = [
SortableHeader(
sort_key="name",
name="Name",
sort_mode=SortMode.NONE if sort_key != "name" else sort_mode,
on_sort=self.on_sort_actions,
columns = [
TableColumn(
label="Icon",
),
TableColumn(
label="Name",
),
*(
SortableHeader(
sort_key=metric.id,
name=metric.name,
sort_mode=SortMode.NONE if sort_key is not metric.id else sort_mode,
on_sort=self.on_sort_actions,
TableColumn(
label=metric.name, key=metric.id, on_sort=self.on_sort_actions
)
for metric in self.project.all_metrics()
),
]

columns = [
ft.DataColumn(
label=ft.Text("Icon", expand=True),
),
*(
ft.DataColumn(label=header, numeric=header.sort_key != "name")
for header in sortable_headers
),
]
self.action_table.set_columns(columns)

self.update_rows()
Expand Down Expand Up @@ -234,15 +218,11 @@ def update_rows(self):
)

rows.append(
ft.DataRow(
[
ft.DataCell(self.create_icon_editor(action)),
EditableTextCell(action, "name", self.on_name_edited),
*metric_cells,
],
selected=action.id in self.project.selected_action_ids,
on_select_changed=lambda _, a=action: self.on_action_selected(a),
)
[
TableCell(self.create_icon_editor(action)),
EditableTextCell(action, "name", self.on_name_edited),
*metric_cells,
]
)

self.action_table.set_rows(rows)
9 changes: 5 additions & 4 deletions source/package/pathways_app/controls/editable_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

import flet as ft
import theme
from pathways_app.controls.styled_table import TableCell
from pyparsing import abstractmethod


class EditableCell(ft.DataCell, ABC):
class EditableCell(TableCell, ABC):
def __init__(
self,
display_control: ft.Control,
Expand All @@ -32,7 +33,7 @@ def __init__(

self.update_bg()

super().__init__(content=self.cell_content)
super().__init__(control=self.cell_content)

def toggle_editing(self, _):
self.is_editing = not self.is_editing
Expand All @@ -46,7 +47,7 @@ def toggle_editing(self, _):
self.input_content.visible = self.is_editing

self.update_bg()
self.update()
self.control.update()

if self.is_editing:
self.input_content.focus()
Expand All @@ -64,7 +65,7 @@ def update_bg(self):
def set_calculated(self, is_calculated):
self.is_calculated = is_calculated
self.update_bg()
self.update()
self.control.update()

@abstractmethod
def update_display(self):
Expand Down
39 changes: 17 additions & 22 deletions source/package/pathways_app/controls/metrics_panel.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Callable
# from typing import Callable

import flet as ft
from controls.editable_cell import EditableTextCell
from controls.header import SmallHeader
from controls.styled_button import StyledButton
from controls.styled_table import StyledTable
from controls.styled_table import StyledTable, TableCell, TableColumn
from controls.unit_cell import MetricUnitCell

from adaptation_pathways.app.model.metric import Metric
Expand All @@ -22,17 +22,17 @@ def __init__(self, project: PathwaysProject):

self.conditions_table = StyledTable(
columns=[
ft.DataColumn(label=ft.Text("Name")),
ft.DataColumn(label=ft.Text("Unit")),
TableColumn(label="Name"),
TableColumn(label="Unit"),
],
rows=[],
show_checkboxes=True,
)

self.criteria_table = StyledTable(
columns=[
ft.DataColumn(label=ft.Text("Name")),
ft.DataColumn(label=ft.Text("Unit")),
TableColumn(label="Name"),
TableColumn(label="Unit"),
],
rows=[],
show_checkboxes=True,
Expand Down Expand Up @@ -95,7 +95,6 @@ def redraw(self):
self.update()

def on_metric_updated(self, _):
print(self)
self.project.notify_conditions_changed()

def on_condition_selected(self, metric: Metric):
Expand Down Expand Up @@ -137,26 +136,22 @@ def on_delete_criteria(self, _):
def get_metric_row(
self,
metric: Metric,
selected_ids: set[str],
on_metric_selected: Callable[[Metric], None],
) -> ft.DataRow:
row = ft.DataRow(
[
EditableTextCell(metric, "name", self.on_metric_updated),
MetricUnitCell(metric, self.on_metric_updated),
],
selected=metric.id in selected_ids,
)
row.on_select_changed = lambda e: on_metric_selected(metric)
# selected_ids: set[str],
# on_metric_selected: Callable[[Metric], None],
) -> list[TableCell]:
row = [
EditableTextCell(metric, "name", self.on_metric_updated),
MetricUnitCell(metric, self.on_metric_updated),
]
return row

def update_metrics(self):
self.conditions_table.set_rows(
[
self.get_metric_row(
metric,
self.project.selected_condition_ids,
self.on_condition_selected,
# self.project.selected_condition_ids,
# self.on_condition_selected,
)
for metric in self.project.sorted_conditions
]
Expand All @@ -168,8 +163,8 @@ def update_metrics(self):
[
self.get_metric_row(
metric,
self.project.selected_criteria_ids,
self.on_criteria_selected,
# self.project.selected_criteria_ids,
# self.on_criteria_selected,
)
for metric in self.project.sorted_criteria
]
Expand Down
122 changes: 54 additions & 68 deletions source/package/pathways_app/controls/pathways_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from controls.metric_value import MetricValueCell
from controls.sortable_header import SortableHeader, SortMode
from controls.styled_button import StyledButton
from controls.styled_table import StyledTable
from controls.styled_table import StyledTable, TableCell, TableColumn

from adaptation_pathways.app.model.pathway import Pathway
from adaptation_pathways.app.model.pathways_project import PathwaysProject
from adaptation_pathways.app.model.sorting import SortTarget


class PathwaysPanel(ft.Column):
class PathwaysPanel(ft.Container):
def __init__(self, project: PathwaysProject):
self.project = project

Expand All @@ -26,26 +26,22 @@ def __init__(self, project: PathwaysProject):

super().__init__(
expand=True,
scroll=ft.ScrollMode.AUTO,
controls=[
ft.Column(
expand=False,
horizontal_alignment=ft.CrossAxisAlignment.STRETCH,
controls=[
ft.Row(
[
SectionHeader(
ft.icons.ACCOUNT_TREE_OUTLINED, "Pathways"
),
ft.Container(expand=True),
self.delete_pathways_button,
],
spacing=15,
),
self.pathway_table,
],
)
],
content=ft.Column(
expand=True,
horizontal_alignment=ft.CrossAxisAlignment.STRETCH,
controls=[
ft.Row(
[
SectionHeader(ft.icons.ACCOUNT_TREE_OUTLINED, "Pathways"),
ft.Container(expand=True),
self.delete_pathways_button,
],
spacing=15,
expand=False,
),
self.pathway_table,
],
),
)

def redraw(self):
Expand All @@ -72,8 +68,8 @@ def on_sort_table(self, header: SortableHeader):
self.project.notify_pathways_changed()

def update_table(self):
sorting = self.project.pathway_sorting
sort_mode = SortableHeader.get_sort_mode(sorting)
# sorting = self.project.pathway_sorting
# sort_mode = SortableHeader.get_sort_mode(sorting)

self.delete_pathways_button.visible = (
len(self.project.selected_pathway_ids) > 0
Expand All @@ -82,20 +78,13 @@ def update_table(self):

self.pathway_table.set_columns(
[
ft.DataColumn(ft.Text("Pathway")),
TableColumn(label="Pathway", expand=2),
*(
ft.DataColumn(
SortableHeader(
metric.id,
metric.name,
sort_mode=(
SortMode.NONE
if sorting.sort_key is not metric.id
else sort_mode
),
on_sort=self.on_sort_table,
),
numeric=True,
TableColumn(
label=metric.name,
key=metric.id,
on_sort=self.on_sort_table,
alignment=ft.alignment.center_right,
)
for metric in self.project.all_metrics()
),
Expand All @@ -111,8 +100,8 @@ def update_table(self):
# if len(path) > 1:
# path = path[1:]
row = self.get_pathway_row(pathway, path)
if pathway.id == self.project.root_pathway_id:
row.on_select_changed = None
# if pathway.id == self.project.root_pathway_id:
# row.on_select_changed = None
self.rows_by_pathway[pathway] = row
rows.append(row)

Expand Down Expand Up @@ -181,41 +170,38 @@ def get_pathway_row(self, pathway: Pathway, ancestors: list[Pathway]):
),
)

row = ft.DataRow(
[
ft.DataCell(
ft.Container(
expand=True,
content=ft.Row(
spacing=0,
controls=row_controls,
),
row = [
TableCell(
ft.Container(
expand=True,
content=ft.Row(
spacing=0,
controls=row_controls,
),
),
*(
MetricValueCell(
metric,
pathway.metric_data[metric.id],
on_finished_editing=self.on_metric_value_edited,
)
for metric in self.project.all_metrics()
),
],
selected=pathway.id in self.project.selected_pathway_ids,
)
),
*(
MetricValueCell(
metric,
pathway.metric_data[metric.id],
on_finished_editing=self.on_metric_value_edited,
)
for metric in self.project.all_metrics()
),
]

if pathway.parent_id is None:
row.color = "#EEEEEE"
# if pathway.parent_id is None:
# row.color = "#EEEEEE"

def on_select_changed(_):
if pathway.id in self.project.selected_pathway_ids:
self.project.selected_pathway_ids.remove(pathway.id)
else:
self.project.selected_pathway_ids.add(pathway.id)
# def on_select_changed(_):
# if pathway.id in self.project.selected_pathway_ids:
# self.project.selected_pathway_ids.remove(pathway.id)
# else:
# self.project.selected_pathway_ids.add(pathway.id)

self.project.notify_pathways_changed()
# self.project.notify_pathways_changed()

row.on_select_changed = on_select_changed
# row.on_select_changed = on_select_changed
return row

def extend_pathway(self, pathway: Pathway, action_id: str):
Expand Down
Loading

0 comments on commit 9c14d33

Please sign in to comment.