import gi import os gi.require_version('Gtk', '3.0') from gi.repository import Gtk, GObject, Gdk from mods.db import db, Patient, Reception, Catalog, List from mods.settings import s_get_reception_list, s_set_reception_list from mods.catalogs import add_catalog, search_catalogs from mods.patients import build_patient_row, patient_sort_func, patient_filter_func_factory, create_new_patient_win, create_open_patient_win from mods.receptions import create_new_reception_win, build_reception_row from mods.lists import create_open_list_win from mods.files import list_row_ui_str from mods.utils import show_msg, ConditionalFilter, enable_widget, disable_widget from datetime import datetime, timedelta import peewee import re # Variables resource_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'res') ui_dir = os.path.join(resource_dir, 'ui') main_win_file = os.path.join(ui_dir, 'main_win.glade') reception_list_settings_win_file = os.path.join(ui_dir, 'reception_list_settings.glade') new_catalog_win_file = os.path.join(ui_dir, 'new_catalog_win.glade') catalog_row_file = os.path.join(ui_dir, 'catalog_list_row.glade') with open(catalog_row_file, 'r') as f: catalog_row_ui_str = f.read() ############ builder = Gtk.Builder() def get_reception_timelist(rec_date): s = s_get_reception_list() dstart = datetime(rec_date.year, rec_date.month, rec_date.day, s.day_start[0], s.day_start[1]) dend = datetime(rec_date.year, rec_date.month, rec_date.day, s.day_end[0], s.day_end[1]) work_day_minutes_total = (dend.hour - dstart.hour) * 60 + dend.minute - dstart.minute shift_minutes_range = range(0, work_day_minutes_total, s.interval) return [dstart + timedelta(minutes=x) for x in shift_minutes_range] catalog_filter = ConditionalFilter(search_catalogs) class CatalogRow(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 @GObject.Property def name(self): return self._name @name.setter def name_setter(self, value): self._name = value class ListRow(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 @GObject.Property def name(self): return self._name @name.setter def name_setter(self, value): self._name = value class MainWinHandler: def main_win_close(self, *args): Gtk.main_quit() def set_reception_list_today(self, *a): cal = builder.get_object('reception_cal') d = datetime.now() cal.select_month(d.month - 1, d.year) cal.select_day(d.day) redraw_reception_list(d) def change_reception_list_day(self, *a): cal = builder.get_object('reception_cal') cal_date = cal.get_date() d = datetime(cal_date.year, cal_date.month + 1, cal_date.day) redraw_reception_list(d) def show_reception_list_settings_win(self, button): reception_list_settings_win = create_reception_list_settings_win() reception_list_settings_win.show_all() def show_new_reception_win(self, button): new_reception_win = create_new_reception_win(builder) new_reception_win.show_all() def show_new_patient_win(self, button): new_patient_win = create_new_patient_win(patient_list) new_patient_win.show_all() def show_open_patient_win(self, button): pl = builder.get_object('patient_list') row = pl.get_selected_row() open_patient_win = create_open_patient_win(row.props.db_id, patient_list) open_patient_win.show_all() def show_open_list_win(self, button): ll = builder.get_object('list_list') row = ll.get_selected_row() open_list_win = create_open_list_win(row.props.db_id) open_list_win.show_all() def show_new_catalog_win(self, button): new_catalog_win = create_new_catalog_win() new_catalog_win.show_all() def reception_list_selected(self, *a): rl = builder.get_object('reception_list') new_button = builder.get_object('new_reception_button') edit_button = builder.get_object('edit_reception_button') row = rl.get_selected_row() if row.props.scheduled: enable_widget([edit_button]) disable_widget([new_button]) else: enable_widget([new_button]) disable_widget([edit_button]) def patient_list_selected(self, *a): button = builder.get_object('patient_open_button') enable_widget([button]) def patient_list_unselected(self, *a): button = builder.get_object('patient_open_button') disable_widget([button]) def patient_filter_changed(self, filter_widget): pl = builder.get_object('patient_list') pl.unselect_all() self.patient_list_unselected() pl.invalidate_filter() def list_list_selected(self, *a): open_button = builder.get_object('list_open_button') enable_widget([open_button]) def catalog_list_selected(self, *a): open_button = builder.get_object('catalog_open_button') rename_button = builder.get_object('catalog_rename_button') enable_widget([open_button, rename_button]) def catalog_list_unselected(self, *a): open_button = builder.get_object('catalog_open_button') rename_button = builder.get_object('catalog_rename_button') disable_widget([open_button, rename_button]) def catalog_filter_changed(self, filter_widget): cl = builder.get_object('catalog_list') cl.unselect_all() self.catalog_list_unselected() cl.invalidate_filter() def build_list_row(list_o): b = Gtk.Builder() b.add_from_string(list_row_ui_str) win = b.get_object('win') box = b.get_object('list_box') b.get_object('name').set_text(list_o.name) row = ListRow() row.props.db_id = list_o.id row.name = list_o.name win.remove(win.get_children()[0]) row.add(box) return row def build_catalog_row(catalog): b = Gtk.Builder() b.add_from_string(catalog_row_ui_str) win = b.get_object('win') box = b.get_object('catalog_box') b.get_object('name').set_text(catalog.name) row = CatalogRow() row.props.db_id = catalog.id row.name = catalog.name win.remove(win.get_children()[0]) row.add(box) return row def list_sort_func(row1, row2, *a): text1 = row1.props.name text2 = row2.props.name return (text1 > text2) - (text1 < text2) def catalog_sort_func(row1, row2, *a): text1 = row1.props.name text2 = row2.props.name return (text1 > text2) - (text1 < text2) def catalog_filter_func(row): fstr = builder.get_object('catalog_filter').get_text().strip() if not fstr: catalog_filter.reset() return True return row.props.db_id in catalog_filter.filter(fstr) def redraw_reception_list(selected_date): reception_timelist = get_reception_timelist(selected_date) reception_list = builder.get_object('reception_list') for c in reception_list.get_children(): reception_list.remove(c) for d in reception_timelist: reception_list.add(build_reception_row(d)) reception_list.show_all() builder.add_from_file(main_win_file) builder.connect_signals(MainWinHandler()) ##### redraw_reception_list(datetime.now()) ##### patient_list = builder.get_object('patient_list') patient_list.set_sort_func(patient_sort_func) patient_list.set_filter_func(patient_filter_func_factory(builder)) with db.atomic(): for p in Patient.select(): patient_list.add(build_patient_row(p)) ##### list_list = builder.get_object('list_list') list_list.set_sort_func(list_sort_func) with db.atomic(): for l in List.select(): list_list.add(build_list_row(l)) ##### catalog_list = builder.get_object('catalog_list') catalog_list.set_sort_func(catalog_sort_func) catalog_list.set_filter_func(catalog_filter_func) with db.atomic(): for c in Catalog.select(): catalog_list.add(build_catalog_row(c)) def create_reception_list_settings_win(): b = Gtk.Builder() class ReceptionListSettingsHandler: def save_settings(self, *a): start_hour = int(b.get_object('start_hour').get_value()) start_minute = int(b.get_object('start_minute').get_value()) end_hour = int(b.get_object('end_hour').get_value()) end_minute = int(b.get_object('end_minute').get_value()) interval = int(b.get_object('interval').get_value()) s_set_reception_list([start_hour, start_minute], [end_hour, end_minute], interval) redraw_reception_list(datetime.now()) b.get_object('reception_list_settings_win').close() b.add_from_file(reception_list_settings_win_file) b.connect_signals(ReceptionListSettingsHandler()) s = s_get_reception_list() b.get_object('start_hour').set_value(s.day_start[0]) b.get_object('start_minute').set_value(s.day_start[1]) b.get_object('end_hour').set_value(s.day_end[0]) b.get_object('end_minute').set_value(s.day_end[1]) b.get_object('interval').set_value(s.interval) w = b.get_object('reception_list_settings_win') return w def create_new_catalog_win(): b = Gtk.Builder() class NewCatalogHandler: def save_catalog(self, *a): catname = re.sub(r'\s+', ' ', b.get_object('catalog_name').get_text()).strip() if not len(catname): return show_msg('Не указано имя справочника', 'Данное поле должно быть заполнено', level='warn') try: catalog = add_catalog(catname) except peewee.IntegrityError: return show_msg('Данный справочник уже существует', 'Справочник с указанным названием уже есть с базе данных', level='warn') row = build_catalog_row(catalog) catalog_list.add(row) catalog_list.select_row(row) catalog_list.show_all() b.get_object('new_catalog_window').close() b.add_from_file(new_catalog_win_file) b.connect_signals(NewCatalogHandler()) w = b.get_object('new_catalog_window') return w main_win = builder.get_object('main_window') main_win.show_all() Gtk.main()