2019-12-24 13:19:35 +03:00
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
2019-12-25 20:15:21 +03:00
|
|
|
from gi.repository import Gtk, GObject
|
|
|
|
from mods.files import choose_diagnosis_win_file, diagnosis_row_ui_str
|
2019-12-26 15:20:50 +03:00
|
|
|
from mods.utils import ConditionalFilter, enable_widget, disable_widget
|
2019-12-25 20:15:21 +03:00
|
|
|
from mods.db import DiagnosisCategory, Diagnosis, DiagnosisIndex, db
|
|
|
|
import re
|
2019-12-24 13:19:35 +03:00
|
|
|
|
2019-12-25 20:15:21 +03:00
|
|
|
class DiagnosisFilter(ConditionalFilter):
|
2019-12-26 15:20:50 +03:00
|
|
|
cat = None
|
|
|
|
def filter(self, query):
|
2019-12-25 20:15:21 +03:00
|
|
|
if query != self.fstr:
|
|
|
|
self.fstr = query
|
2019-12-26 15:20:50 +03:00
|
|
|
self.ids = list(map(lambda x:x.id, self.search_func(self.cat, query)))
|
2019-12-25 20:15:21 +03:00
|
|
|
return self.ids
|
2019-12-24 13:19:35 +03:00
|
|
|
|
2019-12-25 20:15:21 +03:00
|
|
|
class DiagnosisRow(Gtk.ListBoxRow):
|
|
|
|
@GObject.Property
|
|
|
|
def db_id(self):
|
|
|
|
return self._db_id
|
|
|
|
@db_id.setter
|
|
|
|
def db_id_setter(self, value):
|
|
|
|
self._db_id = value
|
|
|
|
|
|
|
|
def cats_subcats(cat):
|
|
|
|
cats = [cat]
|
|
|
|
def append_cat(cat_obj):
|
|
|
|
cats.append(cat_obj)
|
|
|
|
for c in cat_obj.subcat:
|
|
|
|
append_cat(c)
|
|
|
|
for c in cat.subcat:
|
|
|
|
append_cat(c)
|
|
|
|
return cats
|
|
|
|
|
|
|
|
def get_categories(parent=None):
|
|
|
|
with db.atomic():
|
|
|
|
return DiagnosisCategory.select().where(DiagnosisCategory.parent == parent)
|
2019-12-26 15:20:50 +03:00
|
|
|
def get_reception_diagnosises(cat=None):
|
2019-12-25 20:15:21 +03:00
|
|
|
with db.atomic():
|
|
|
|
if cat:
|
2019-12-26 15:20:50 +03:00
|
|
|
d = list(Diagnosis.select().where(Diagnosis.category == cat).order_by(Diagnosis.code))
|
2019-12-25 20:15:21 +03:00
|
|
|
return d
|
|
|
|
else:
|
|
|
|
return Diagnosis.select()
|
|
|
|
def get_category(cat_id):
|
|
|
|
with db.atomic():
|
|
|
|
return DiagnosisCategory.get_by_id(cat_id)
|
2019-12-26 15:20:50 +03:00
|
|
|
def get_giagnosis(diag_id):
|
|
|
|
with db.atomic():
|
|
|
|
return Diagnosis.get_by_id(diag_id)
|
2019-12-25 20:15:21 +03:00
|
|
|
def search_diagnosises(cat, q):
|
|
|
|
q = re.sub(r'\s+', ' ', q).strip()
|
|
|
|
q = ' '.join([ f'{x}*' for x in q.split(' ')])
|
|
|
|
with db.atomic():
|
|
|
|
return (Diagnosis.select()
|
|
|
|
.join(
|
|
|
|
DiagnosisIndex,
|
|
|
|
on=(Diagnosis.id == DiagnosisIndex.rowid))
|
2019-12-26 15:20:50 +03:00
|
|
|
.where((Diagnosis.category == cat) & (DiagnosisIndex.match(q)))
|
2019-12-25 20:15:21 +03:00
|
|
|
.order_by(DiagnosisIndex.bm25()))
|
|
|
|
diagnosis_filter = DiagnosisFilter(search_diagnosises)
|
|
|
|
########################################################
|
|
|
|
def build_diagnosis_row(diagnosis):
|
|
|
|
b = Gtk.Builder()
|
|
|
|
b.add_from_string(diagnosis_row_ui_str)
|
|
|
|
win = b.get_object('win')
|
|
|
|
box = b.get_object('diagnosis_box')
|
|
|
|
b.get_object('title').set_markup(f'<span size="x-large">{diagnosis.code:<8} {diagnosis.title}</span>')
|
|
|
|
win.remove(win.get_children()[0])
|
|
|
|
row = DiagnosisRow()
|
|
|
|
row.props.db_id = diagnosis.id
|
|
|
|
row.add(box)
|
|
|
|
return row
|
2019-12-24 13:19:35 +03:00
|
|
|
|
2019-12-26 15:20:50 +03:00
|
|
|
def create_choose_diagnosis_win(reception_b):
|
2019-12-24 13:19:35 +03:00
|
|
|
b = Gtk.Builder()
|
2019-12-26 15:20:50 +03:00
|
|
|
rd_list = reception_b.get_object('diagnosis_list')
|
2019-12-25 20:15:21 +03:00
|
|
|
class ChooseDiagnosisWinHandler:
|
|
|
|
def cat_selection_changed(self, cat_selection):
|
|
|
|
sel_iter = cat_selection.get_selected()[1]
|
|
|
|
model = cat_selection.get_tree_view().get_model()
|
|
|
|
category = get_category(model.get_value(sel_iter, 1))
|
2019-12-26 15:20:50 +03:00
|
|
|
diagnosis_filter.cat = category
|
|
|
|
diagnosis_list.unselect_all()
|
|
|
|
self.diagnosis_row_unselected()
|
2019-12-25 20:15:21 +03:00
|
|
|
self.redraw_diagnosis_list(category)
|
|
|
|
def redraw_diagnosis_list(self, cat):
|
|
|
|
for c in diagnosis_list.get_children():
|
|
|
|
diagnosis_list.remove(c)
|
2019-12-26 15:20:50 +03:00
|
|
|
for d in get_reception_diagnosises(cat):
|
2019-12-25 20:15:21 +03:00
|
|
|
diagnosis_list.add(build_diagnosis_row(d))
|
|
|
|
diagnosis_list.show_all()
|
2019-12-26 15:20:50 +03:00
|
|
|
def diagnosis_row_selected(self, *a):
|
|
|
|
enable_widget([accept_button])
|
|
|
|
def diagnosis_row_unselected(self, *a):
|
|
|
|
disable_widget([accept_button])
|
|
|
|
def diagnosis_filter_changed(self, filter_widget):
|
|
|
|
diagnosis_list.unselect_all()
|
|
|
|
self.diagnosis_row_unselected()
|
|
|
|
diagnosis_list.invalidate_filter()
|
|
|
|
def submit(self, button):
|
|
|
|
from mods.receptions import build_reception_diagnosis_row
|
|
|
|
exist_rows = list(map(lambda x: x.props.db_id, rd_list.get_children()))
|
|
|
|
rows = diagnosis_list.get_selected_rows()
|
|
|
|
for row in rows:
|
|
|
|
if row.props.db_id not in exist_rows:
|
|
|
|
diagnosis = get_giagnosis(row.props.db_id)
|
|
|
|
rd_list.add(build_reception_diagnosis_row(diagnosis))
|
|
|
|
rd_list.show_all()
|
|
|
|
w.destroy()
|
2019-12-24 13:19:35 +03:00
|
|
|
b.add_from_file(choose_diagnosis_win_file)
|
|
|
|
b.connect_signals(ChooseDiagnosisWinHandler())
|
2019-12-25 20:15:21 +03:00
|
|
|
cat_liststore = b.get_object('category_list')
|
|
|
|
def insert_cat(cat, parent=None):
|
|
|
|
cur_parent = cat_liststore.append(parent, [cat.title, cat.id])
|
|
|
|
for c in get_categories(cat):
|
|
|
|
insert_cat(c, cur_parent)
|
|
|
|
for c in get_categories():
|
|
|
|
insert_cat(c)
|
|
|
|
diagnosis_list = b.get_object('diagnosis_list')
|
2019-12-26 15:20:50 +03:00
|
|
|
di_filter = b.get_object('diagnosis_filter')
|
|
|
|
accept_button = b.get_object('accept_button')
|
|
|
|
#####
|
|
|
|
def diagnosis_filter_func(row):
|
|
|
|
fstr = di_filter.get_text().strip()
|
|
|
|
if not fstr:
|
|
|
|
diagnosis_filter.reset()
|
|
|
|
return True
|
|
|
|
return row.props.db_id in diagnosis_filter.filter(fstr)
|
|
|
|
#####
|
|
|
|
diagnosis_list.set_filter_func(diagnosis_filter_func)
|
2019-12-24 13:19:35 +03:00
|
|
|
w = b.get_object('choose_diagnosis_window')
|
|
|
|
return w
|