File: gmFormWidgets.py

package info (click to toggle)
gnumed-client 1.8.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 37,672 kB
  • sloc: python: 126,459; javascript: 6,113; sh: 1,192; xml: 36; makefile: 33
file content (1083 lines) | stat: -rw-r--r-- 36,924 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
"""GNUmed form/letter handling widgets."""

#================================================================
__author__ = "Karsten Hilbert <Karsten.Hilbert@gmx.net>"
__license__ = "GPL v2 or later"


import os.path
import sys
import logging
import shutil


import wx


if __name__ == '__main__':
	sys.path.insert(0, '../../')
from Gnumed.pycommon import gmI18N
from Gnumed.pycommon import gmTools
from Gnumed.pycommon import gmDispatcher
from Gnumed.pycommon import gmPrinting
from Gnumed.pycommon import gmDateTime
from Gnumed.pycommon import gmShellAPI
from Gnumed.pycommon import gmMimeLib
from Gnumed.pycommon import gmCfg2

from Gnumed.business import gmForms
from Gnumed.business import gmPerson
from Gnumed.business import gmExternalCare
from Gnumed.business import gmPraxis

from Gnumed.wxpython import gmGuiHelpers
from Gnumed.wxpython import gmListWidgets
from Gnumed.wxpython import gmMacro
from Gnumed.wxpython import gmEditArea
from Gnumed.wxpython.gmDocumentWidgets import save_files_as_new_document


_log = logging.getLogger('gm.ui')

_ID_FORM_DISPOSAL_PRINT, \
_ID_FORM_DISPOSAL_REMOTE_PRINT, \
_ID_FORM_DISPOSAL_EXPORT_ONLY, \
_ID_FORM_DISPOSAL_ARCHIVE_ONLY = range(4)

#============================================================
# generic form generation and handling convenience functions
#------------------------------------------------------------
def print_doc_from_template(parent=None, jobtype=None, episode=None, edit_form=None):

	form = generate_form_from_template (
		parent = parent,
		excluded_template_types = [
			'gnuplot script',
			'visual progress note',
			'invoice'
		],
		edit = edit_form			# default None = respect template setting
	)
	if form is None:
		return False

	if form in [True, False]:	# returned by special OOo/LO handling
		return form

	if episode is None:
		epi_name = 'administrative'
	else:
		epi_name = episode['description']
	return act_on_generated_forms (
		parent = parent,
		forms = [form],
		jobtype = jobtype,
		episode_name = epi_name,
		review_copy_as_normal = True
	)

#------------------------------------------------------------
# eventually this should become superfluous when there's a
# standard engine wrapper around OOo
def print_doc_from_ooo_template(template=None):

	# export template to file
	filename = template.save_to_file()
	if filename is None:
		gmGuiHelpers.gm_show_error (
			_(	'Error exporting form template\n'
				'\n'
				' "%s" (%s)'
			) % (template['name_long'], template['external_version']),
			_('Letter template export')
		)
		return False

	try:
		doc = gmForms.cOOoLetter(template_file = filename, instance_type = template['instance_type'])
	except ImportError:
		gmGuiHelpers.gm_show_error (
			_('Cannot connect to OpenOffice.\n\n'
			  'The UNO bridge module for Python\n'
			  'is not installed.'
			),
			_('Letter writer')
		)
		return False

	if not doc.open_in_ooo():
		gmGuiHelpers.gm_show_error (
			_('Cannot connect to OpenOffice.\n'
			  '\n'
			  'You may want to increase the option\n'
			  '\n'
			  ' <%s>'
			) % _('OOo startup time'),
			_('Letter writer')
		)
		try: os.remove(filename)
		except Exception: pass
		return False

	doc.show(False)
	ph_handler = gmMacro.gmPlaceholderHandler()
	doc.replace_placeholders(handler = ph_handler)

	filename = filename.replace('.ott', '.odt').replace('-FormTemplate-', '-FormInstance-')
	doc.save_in_ooo(filename = filename)

	doc.show(True)

	return True

#------------------------------------------------------------
def generate_form_from_template(parent=None, template_types=None, edit=None, template=None, excluded_template_types=None):
	"""If <edit> is None it will honor the template setting."""

	if parent is None:
		parent = wx.GetApp().GetTopWindow()

	# 1) get template to use
	if template is None:
		template = manage_form_templates (
			parent = parent,
			active_only = True,
			template_types = template_types,
			excluded_types = excluded_template_types
		)
		if template is None:
			gmDispatcher.send(signal = 'statustext', msg = _('No document template selected.'), beep = False)
			return None

	if template['engine'] == 'O':
		return print_doc_from_ooo_template(template = template)

	wx.BeginBusyCursor()

	# 2) process template
	try:
		form = template.instantiate()
	except KeyError:
		_log.exception('cannot instantiate document template [%s]', template)
		gmGuiHelpers.gm_show_error (
			aMessage = _('Invalid document template [%s - %s (%s)]') % (name, ver, template['engine']),
			aTitle = _('Generating document from template')
		)
		wx.EndBusyCursor()
		return None
	ph = gmMacro.gmPlaceholderHandler()
	#ph.debug = True
	form.substitute_placeholders(data_source = ph)
	if edit is None:
		if form.template['edit_after_substitution']:
			edit = True
		else:
			edit = False
	if edit:
		wx.EndBusyCursor()
		form.edit()
		wx.BeginBusyCursor()

	# 3) generate output
	pdf_name = form.generate_output()
	wx.EndBusyCursor()
	if pdf_name is not None:
		return form

	gmGuiHelpers.gm_show_error (
		aMessage = _('Error generating document printout.'),
		aTitle = _('Generating document printout')
	)
	return None

#------------------------------------------------------------
def act_on_generated_forms(parent=None, forms=None, jobtype=None, episode_name=None, progress_note=None, review_copy_as_normal=False):
	"""This function assumes that .generate_output() has already been called on each form.

	It operates on the active patient.
	"""
	if len(forms) == 0:
		return True

	no_of_printables = 0
	for form in forms:
		no_of_printables += len(form.final_output_filenames)

	if no_of_printables == 0:
		return True

	soap_lines = []

	#-----------------------------
	def save_soap(soap=None):
		if episode_name is None:
			return
		if soap.strip() == '':
			return
		pat = gmPerson.gmCurrentPatient()
		emr = pat.emr
		epi = emr.add_episode(episode_name = episode_name, is_open = False)
		emr.add_clin_narrative (
			soap_cat = None,
			note = soap,
			episode = epi
		)

	#-----------------------------
	def archive_forms(episode_name=None, comment=None):
		if episode_name is None:
			epi = None				# will ask for episode further down
		else:
			pat = gmPerson.gmCurrentPatient()
			emr = pat.emr
			epi = emr.add_episode(episode_name = episode_name, is_open = False)
		for form in forms:
			files2import = []
			files2import.extend(form.final_output_filenames)
			files2import.extend(form.re_editable_filenames)
			if len(files2import) == 0:
				continue
			save_files_as_new_document (
				parent = parent,
				filenames = files2import,
				document_type = form.template['instance_type'],
				unlock_patient = False,
				episode = epi,
				review_as_normal = review_copy_as_normal,
				reference = None,
				pk_org_unit = gmPraxis.gmCurrentPraxisBranch()['pk_org_unit'],
				comment = comment,
				date_generated = gmDateTime.pydt_now_here()
			)
		return True

	#-----------------------------
	def print_forms():
		# anything to do ?
		files2print = []
		form_names = []
		for form in forms:
			files2print.extend(form.final_output_filenames)
			form_names.append('%s (%s)' % (form.template['name_long'], form.template['external_version']))
		if len(files2print) == 0:
			return True
		# print
		_cfg = gmCfg2.gmCfgData()
		printed = gmPrinting.print_files(filenames = files2print, jobtype = jobtype, verbose = _cfg.get(option = 'debug'))
		if not printed:
			gmGuiHelpers.gm_show_error (
				aMessage = _('Error printing documents.'),
				aTitle = _('Printing [%s]') % jobtype
			)
			return False
		soap_lines.append(_('Printed: %s') % ', '.join(form_names))
		return True

	#-----------------------------
	def export_forms(remote_print=False):
		pat = gmPerson.gmCurrentPatient()
		return pat.export_area.add_forms(forms = forms, designation = gmTools.bool2subst(remote_print, 'print', None, None))

	#-----------------------------
	if parent is None:
		parent = wx.GetApp().GetTopWindow()

	if jobtype is None:
		jobtype = 'generic_document'

	dlg = cFormDisposalDlg(parent, -1)
	dlg.forms = forms
	dlg.progress_note = progress_note
	dlg.episode_name = episode_name
	action_code = dlg.ShowModal()

	if action_code == wx.ID_CANCEL:
		dlg.DestroyLater()
		return True

	forms = dlg._LCTRL_forms.get_item_data()
	if len(forms) == 0:
		dlg.DestroyLater()
		return True

	progress_note = dlg.progress_note
	episode_name = dlg._PRW_episode.GetValue().strip()
	if episode_name == '':
		episode_name = None
	also_export = dlg._CHBOX_export.GetValue()
	dlg.DestroyLater()

	if action_code == _ID_FORM_DISPOSAL_ARCHIVE_ONLY:
		success = archive_forms(episode_name = episode_name, comment = progress_note)
		if not success:
			return False
		if progress_note != '':
			soap_lines.insert(0, progress_note)
		if len(soap_lines) > 0:
			save_soap(soap = '\n'.join(soap_lines))
		return True

	if action_code == _ID_FORM_DISPOSAL_EXPORT_ONLY:
		success = export_forms()
		if not success:
			return False
		if progress_note != '':
			soap_lines.insert(0, progress_note)
		if len(soap_lines) > 0:
			save_soap(soap = '\n'.join(soap_lines))
		return True

	success = False
	if action_code == _ID_FORM_DISPOSAL_PRINT:
		success = print_forms()
		if episode_name is not None:
			archive_forms(episode_name = episode_name, comment = progress_note)
		if also_export:
			export_forms()

	elif action_code == _ID_FORM_DISPOSAL_REMOTE_PRINT:
		success = export_forms(remote_print = True)
		if episode_name is not None:
			archive_forms(episode_name = episode_name, comment = progress_note)

	if not success:
		return False

	if progress_note != '':
		soap_lines.insert(0, progress_note)
	if len(soap_lines) > 0:
		save_soap(soap = '\n'.join(soap_lines))

	return True

#============================================================
from Gnumed.wxGladeWidgets import wxgFormDisposalDlg

class cFormDisposalDlg(wxgFormDisposalDlg.wxgFormDisposalDlg):

	def __init__(self, *args, **kwargs):

		wxgFormDisposalDlg.wxgFormDisposalDlg.__init__(self, *args, **kwargs)

		self.__init_ui()

	#--------------------------------------------------------
	# properties
	#--------------------------------------------------------
	def _set_msg(self, msg):
		self._LBL_msg.SetLabel(msg)
		self._LBL_msg.Refresh()

	message = property(lambda x:x, _set_msg)

	#--------------------------------------------------------
	def _set_forms(self, forms):
		items = [ f.template['name_long'] for f in forms ]
		self._LCTRL_forms.set_string_items(items)
		self._LCTRL_forms.set_data(forms)

	forms = property(lambda x:x, _set_forms)

	#--------------------------------------------------------
	def _get_note(self):
		return self._TCTRL_soap.GetValue().strip()

	def _set_note(self, note):
		if note is None:
			note = ''
		self._TCTRL_soap.SetValue(note)

	progress_note = property(_get_note, _set_note)

	#--------------------------------------------------------
	def _get_episode_name(self):
		return self._PRW_episode.GetValue().strip()

	def _set_episode_name(self, episode_name):
		if episode_name is None:
			episode_name = ''
		self._PRW_episode.SetValue(episode_name)

	episode_name = property(_get_episode_name, _set_episode_name)

	#--------------------------------------------------------
	# internal helpers
	#--------------------------------------------------------
	def __init_ui(self):
		self._LCTRL_forms.set_columns([_('Form')])
		#self._CHBOX_export.SetValue(False)
	#--------------------------------------------------------
	# event handlers
	#--------------------------------------------------------
	def _on_print_button_pressed(self, event):
		self.EndModal(_ID_FORM_DISPOSAL_PRINT)
	#--------------------------------------------------------
	def _on_remote_print_button_pressed(self, event):
		self.EndModal(_ID_FORM_DISPOSAL_REMOTE_PRINT)
	#--------------------------------------------------------
	def _on_export_button_pressed(self, event):
		self.EndModal(_ID_FORM_DISPOSAL_EXPORT_ONLY)
	#--------------------------------------------------------
	def _on_archive_button_pressed(self, event):
		self.EndModal(_ID_FORM_DISPOSAL_ARCHIVE_ONLY)
	#--------------------------------------------------------
	def _on_show_forms_button_pressed(self, event):
		event.Skip()
		if self._LCTRL_forms.ItemCount == 0:
			return
		forms2show = self._LCTRL_forms.get_selected_item_data()
		if len(forms2show) == 0:
			data = self._LCTRL_forms.get_item_data(item_idx = 0)
			if data is None:
				return
			forms2show = [data]
		if len(forms2show) == 0:
			return
		for form in forms2show:
			for filename in form.final_output_filenames:
				gmMimeLib.call_viewer_on_file(filename, block = True)
	#--------------------------------------------------------
	def _on_delete_forms_button_pressed(self, event):
		print("Event handler '_on_delete_forms_button_pressed' not implemented!")
		event.Skip()

#============================================================
# form template management
#------------------------------------------------------------
def edit_template(parent=None, template=None, single_entry=False):
	ea = cFormTemplateEAPnl(parent, -1)
	ea.data = template
	ea.mode = gmTools.coalesce(template, 'new', 'edit')
	dlg = gmEditArea.cGenericEditAreaDlg2(parent, -1, edit_area = ea, single_entry = single_entry)
	dlg.SetTitle(gmTools.coalesce(template, _('Adding new form template'), _('Editing form template')))
	if dlg.ShowModal() == wx.ID_OK:
		dlg.DestroyLater()
		return True
	dlg.DestroyLater()
	return False

#------------------------------------------------------------
def manage_form_templates(parent=None, template_types=None, active_only=False, excluded_types=None, msg=None):

	if parent is None:
		parent = wx.GetApp().GetTopWindow()

	#-------------------------
	def edit(template=None):
		return edit_template(parent = parent, template = template)
	#-------------------------
	def delete(template):
		delete = gmGuiHelpers.gm_show_question (
			aTitle = _('Deleting form template.'),
			aMessage = _(
				'Are you sure you want to delete\n'
				'the following form template ?\n\n'
				' "%s (%s)"\n\n'
				'You can only delete templates which\n'
				'have not yet been used to generate\n'
				'any forms from.'
			) % (template['name_long'], template['external_version'])
		)
		if delete:
			# FIXME: make this a priviledged operation ?
			gmForms.delete_form_template(template = template)
			return True
		return False
	#-------------------------
	def refresh(lctrl):
		templates = gmForms.get_form_templates(active_only = active_only, template_types = template_types, excluded_types = excluded_types)
		lctrl.set_string_items(items = [ [t['name_long'], t['external_version'], gmForms.form_engine_names[t['engine']]] for t in templates ])
		lctrl.set_data(data = templates)
	#-------------------------
	template = gmListWidgets.get_choices_from_list (
		parent = parent,
		msg = msg,
		caption = _('Select letter or form template.'),
		columns = [_('Template'), _('Version'), _('Type')],
		edit_callback = edit,
		new_callback = edit,
		delete_callback = delete,
		refresh_callback = refresh,
		single_selection = True
	)

	return template

#------------------------------------------------------------
from Gnumed.wxGladeWidgets import wxgFormTemplateEditAreaPnl

class cFormTemplateEAPnl(wxgFormTemplateEditAreaPnl.wxgFormTemplateEditAreaPnl, gmEditArea.cGenericEditAreaMixin):

	def __init__(self, *args, **kwargs):

		try:
			data = kwargs['template']
			del kwargs['template']
		except KeyError:
			data = None

		wxgFormTemplateEditAreaPnl.wxgFormTemplateEditAreaPnl.__init__(self, *args, **kwargs)
		gmEditArea.cGenericEditAreaMixin.__init__(self)

		self.full_filename = None

		self.mode = 'new'
		self.data = data
		if data is not None:
			self.mode = 'edit'

		self.__init_ui()
	#----------------------------------------------------------------
	def __init_ui(self):
		self._PRW_name_long.matcher = gmForms.cFormTemplateNameLong_MatchProvider()
		self._PRW_name_short.matcher = gmForms.cFormTemplateNameShort_MatchProvider()
		self._PRW_template_type.matcher = gmForms.cFormTemplateType_MatchProvider()
	#----------------------------------------------------------------
	# generic Edit Area mixin API
	#----------------------------------------------------------------
	def _valid_for_save(self):

		validity = True

		# self._TCTRL_filename
		self.display_tctrl_as_valid(tctrl = self._TCTRL_filename, valid = True)
		fname = self._TCTRL_filename.GetValue().strip()
		# 1) new template: file must exist
		if self.data is None:
			try:
				open(fname, 'r').close()
			except Exception:
				validity = False
				self.display_tctrl_as_valid(tctrl = self._TCTRL_filename, valid = False)
				self.StatusText = _('You must select a template file before saving.')
				self._TCTRL_filename.SetFocus()
		# 2) existing template
		# - empty = no change
		# - does not exist: name change in DB field
		# - does exist: reload from filesystem

		# self._PRW_instance_type
		if self._PRW_instance_type.GetValue().strip() == '':
			validity = False
			self._PRW_instance_type.display_as_valid(False)
			self.StatusText = _('You must enter a type for documents created with this template.')
			self._PRW_instance_type.SetFocus()
		else:
			self._PRW_instance_type.display_as_valid(True)

		# self._PRW_template_type
		if self._PRW_template_type.GetData() is None:
			validity = False
			self._PRW_template_type.display_as_valid(False)
			self.StatusText = _('You must enter a type for this template.')
			self._PRW_template_type.SetFocus()
		else:
			self._PRW_template_type.display_as_valid(True)

		# self._TCTRL_external_version
		if self._TCTRL_external_version.GetValue().strip() == '':
			validity = False
			self.display_tctrl_as_valid(tctrl = self._TCTRL_external_version, valid = False)
			self.StatusText = _('You must enter a version for this template.')
			self._TCTRL_external_version.SetFocus()
		else:
			self.display_tctrl_as_valid(tctrl = self._TCTRL_external_version, valid = True)

		# self._PRW_name_short
		if self._PRW_name_short.GetValue().strip() == '':
			validity = False
			self._PRW_name_short.display_as_valid(False)
			self.StatusText = _('Missing short name for template.')
			self._PRW_name_short.SetFocus()
		else:
			self._PRW_name_short.display_as_valid(True)

		# self._PRW_name_long
		if self._PRW_name_long.GetValue().strip() == '':
			validity = False
			self._PRW_name_long.display_as_valid(False)
			self.StatusText = _('Missing long name for template.')
			self._PRW_name_long.SetFocus()
		else:
			self._PRW_name_long.display_as_valid(True)

		return validity
	#----------------------------------------------------------------
	def _save_as_new(self):
		data = gmForms.create_form_template (
			template_type = self._PRW_template_type.GetData(),
			name_short = self._PRW_name_short.GetValue().strip(),
			name_long = self._PRW_name_long.GetValue().strip()
		)
		data['external_version'] = self._TCTRL_external_version.GetValue()
		data['instance_type'] = self._PRW_instance_type.GetValue().strip()
		data['filename'] = os.path.split(self._TCTRL_filename.GetValue().strip())[1]
		data['in_use'] = self._CHBOX_active.GetValue()
		data['edit_after_substitution'] = self._CHBOX_editable.GetValue()
		data['engine'] = gmForms.form_engine_abbrevs[self._CH_engine.GetSelection()]
		data.save()

		data.update_template_from_file(filename = self._TCTRL_filename.GetValue().strip())

		self.data = data
		return True
	#----------------------------------------------------------------
	def _save_as_update(self):
		self.data['pk_template_type'] = self._PRW_template_type.GetData()
		self.data['name_short'] = self._PRW_name_short.GetValue().strip()
		self.data['name_long'] = self._PRW_name_long.GetValue().strip()
		self.data['external_version'] = self._TCTRL_external_version.GetValue()
		tmp = self._PRW_instance_type.GetValue().strip()
		if tmp not in [self.data['instance_type'], self.data['l10n_instance_type']]:
			self.data['instance_type'] = tmp
		tmp = os.path.split(self._TCTRL_filename.GetValue().strip())[1]
		if tmp != '':
			self.data['filename'] = tmp
		self.data['in_use'] = self._CHBOX_active.GetValue()
		self.data['edit_after_substitution'] = self._CHBOX_editable.GetValue()
		self.data['engine'] = gmForms.form_engine_abbrevs[self._CH_engine.GetSelection()]
		self.data.save()

		fname = self._TCTRL_filename.GetValue().strip()
		try:
			open(fname, 'r').close()
			self.data.update_template_from_file(filename = fname)
		except Exception:
			pass # filename column already updated

		return True
	#----------------------------------------------------------------
	def _refresh_as_new(self):
		self._PRW_name_long.SetText('')
		self._PRW_name_short.SetText('')
		self._TCTRL_external_version.SetValue('')
		self._PRW_template_type.SetText('')
		self._PRW_instance_type.SetText('')
		self._TCTRL_filename.SetValue('')
		self._CH_engine.SetSelection(0)
		self._CHBOX_active.SetValue(True)
		self._CHBOX_editable.SetValue(True)
		self._LBL_status.SetLabel('')
		self._BTN_export.Enable(False)

		self._PRW_name_long.SetFocus()
	#----------------------------------------------------------------
	def _refresh_as_new_from_existing(self):
		self._refresh_as_new()
	#----------------------------------------------------------------
	def _refresh_from_existing(self):
		self._PRW_name_long.SetText(self.data['name_long'])
		self._PRW_name_short.SetText(self.data['name_short'])
		self._TCTRL_external_version.SetValue(self.data['external_version'])
		self._PRW_template_type.SetText(self.data['l10n_template_type'], data = self.data['pk_template_type'])
		self._PRW_instance_type.SetText(self.data['l10n_instance_type'], data = self.data['instance_type'])
		self._TCTRL_filename.SetValue(self.data['filename'])
		self._CH_engine.SetSelection(gmForms.form_engine_abbrevs.index(self.data['engine']))
		self._CHBOX_active.SetValue(self.data['in_use'])
		self._CHBOX_editable.SetValue(self.data['edit_after_substitution'])
		self._LBL_status.SetLabel(_('last modified %s by %s, internal revision [%s]') % (
			gmDateTime.pydt_strftime(self.data['last_modified'], '%Y %B %d'),
			self.data['modified_by'],
			gmTools.coalesce(self.data['gnumed_revision'], '?')
		))

		self._TCTRL_filename.Enable(True)
		self._BTN_load.Enable(True)
		self._BTN_export.Enable(True)

		self._BTN_load.SetFocus()
	#----------------------------------------------------------------
	# event handlers
	#----------------------------------------------------------------
	def _on_load_button_pressed(self, event):
		engine_abbrev = gmForms.form_engine_abbrevs[self._CH_engine.GetSelection()]

		wildcards = []
		try:
			wildcards.append('%s (%s)|%s' % (
				gmForms.form_engine_names[engine_abbrev],
				gmForms.form_engine_template_wildcards[engine_abbrev],
				gmForms.form_engine_template_wildcards[engine_abbrev]
			))
		except KeyError:
			pass
		wildcards.append("%s (*)|*" % _('all files'))
		wildcards.append("%s (*.*)|*.*" % _('all files (Windows)'))

		dlg = wx.FileDialog (
			parent = self,
			message = _('Choose a form template file'),
			defaultDir = os.path.expanduser(os.path.join('~', 'gnumed')),
			defaultFile = '',
			wildcard = '|'.join(wildcards),
			style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
		)
		result = dlg.ShowModal()
		if result != wx.ID_CANCEL:
			self._TCTRL_filename.SetValue(dlg.GetPath())
		dlg.DestroyLater()

		event.Skip()
	#----------------------------------------------------------------
	def _on_export_button_pressed(self, event):
		if self.data is None:
			return

		engine_abbrev = gmForms.form_engine_abbrevs[self._CH_engine.GetSelection()]

		wildcards = []
		try:
			wildcards.append('%s (%s)|%s' % (
				gmForms.form_engine_names[engine_abbrev],
				gmForms.form_engine_template_wildcards[engine_abbrev],
				gmForms.form_engine_template_wildcards[engine_abbrev]
			))
		except KeyError:
			pass
		wildcards.append("%s (*)|*" % _('all files'))
		wildcards.append("%s (*.*)|*.*" % _('all files (Windows)'))

		dlg = wx.FileDialog (
			parent = self,
			message = _('Enter a filename to save the template to'),
			defaultDir = os.path.expanduser(os.path.join('~', 'gnumed')),
			defaultFile = '',
			wildcard = '|'.join(wildcards),
			style = wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
		)
		result = dlg.ShowModal()
		if result != wx.ID_CANCEL:
			fname = dlg.GetPath()
			self.data.save_to_file(filename = fname)
		dlg.DestroyLater()

		event.Skip()

#============================================================
from Gnumed.wxGladeWidgets import wxgReceiverSelectionDlg

class cReceiverSelectionDlg(wxgReceiverSelectionDlg.wxgReceiverSelectionDlg):

	def __init__(self, *args, **kwargs):
		wxgReceiverSelectionDlg.wxgReceiverSelectionDlg.__init__(self, *args, **kwargs)
		self.__patient = None
		self.__init_ui()
		self.__register_interests()

	#------------------------------------------------------------
	def __init_ui(self):
		if self.__patient is None:
			return

		self._LCTRL_candidates.set_columns([_('Receiver'), _('Details')])
		self._LCTRL_candidates.set_resize_column()
		self._LCTRL_candidates.item_tooltip_callback = self._get_candidate_tooltip
		self.__populate_candidates_list()

		self._LCTRL_addresses.set_resize_column()
		self._LCTRL_addresses.item_tooltip_callback = self._get_address_tooltip
		self._LCTRL_addresses.activate_callback = self._on_address_activated_in_list
		adrs = self.__patient.get_addresses()
		self.__populate_address_list(addresses = adrs)

		self._TCTRL_final_name.SetValue(self.__patient['description'].strip())

		self.Layout()

	#------------------------------------------------------------
	def __register_interests(self):
		self._TCTRL_final_name.add_callback_on_modified(callback = self._on_final_name_modified)
		self._PRW_other_address.add_callback_on_selection(self._on_address_selected_in_PRW)
		self._PRW_org_unit.add_callback_on_set_focus(self._on_entering_org_unit_PRW)
		self._PRW_org_unit.add_callback_on_selection(self._on_org_unit_selected_in_PRW)

	#------------------------------------------------------------
	def __populate_candidates_list(self):

		list_items = [[_('Patient'), self.__patient['description_gender'].strip()]]
		list_data = [(self.__patient['description'].strip(), self.__patient.get_addresses(), '', None)]

		candidate_type = _('Emergency contact')
		if self.__patient['emergency_contact'] is not None:
			name = self.__patient['emergency_contact'].strip()
			list_items.append([candidate_type, name])
			list_data.append((name, [], '', None))
		contact = self.__patient.emergency_contact_in_database
		if contact is not None:
			list_items.append([candidate_type, contact['description_gender']])
			list_data.append((contact['description'].strip(), contact.get_addresses(), '', None))

		candidate_type = _('Primary doctor')
		prov = self.__patient.primary_provider
		if prov is not None:
			ident = prov.identity
			list_items.append([candidate_type, '%s: %s' % (prov['short_alias'], ident['description_gender'])])
			list_data.append((ident['description'].strip(), ident.get_addresses(), _('in-praxis primary provider'), None))

		candidate_type = _('This praxis')
		branches = gmPraxis.get_praxis_branches(order_by = 'branch')
		for branch in branches:
			adr = branch.address
			if adr is None:
				continue
			list_items.append([candidate_type, '%s @ %s' % (branch['branch'], branch['praxis'])])
			list_data.append(('%s @ %s' % (branch['branch'], branch['praxis']), [adr], branch.format(), None))
		del branches

		candidate_type = _('External care')
		cares = gmExternalCare.get_external_care_items(pk_identity = self.__patient.ID)
		for care in cares:
			details = '%s%s@%s (%s)' % (
				gmTools.coalesce(care['provider'], '', '%s: '),
				care['unit'],
				care['organization'],
				care['issue']
			)
			name = ('%s%s' % (
				gmTools.coalesce(care['provider'], '', '%s, '),
				'%s @ %s' % (care['unit'], care['organization'])
			)).strip()
			org_unit = care.org_unit
			adr = org_unit.address
			if adr is None:
				addresses = []
			else:
				addresses = [adr]
			list_items.append([candidate_type, details])
			tt = '\n'.join(care.format(with_health_issue = True, with_address = True, with_comms = True))
			list_data.append((name, addresses, tt, org_unit))
		del cares

		emr = self.__patient.emr

		candidate_type = _('Hospital stay')
		depts = emr.get_attended_hospitals_as_org_units()
		for dept in depts:
			adr = dept.address
			if adr is None:
				continue
			list_items.append([candidate_type, '%s @ %s' % (dept['unit'], dept['organization'])])
			list_data.append(('%s @ %s' % (dept['unit'], dept['organization']), [adr], '\n'.join(dept.format(with_comms = True)), dept))
		del depts

		candidate_type = _('Procedure')
		proc_locs = emr.get_procedure_locations_as_org_units()
		for proc_loc in proc_locs:
			adr = proc_loc.address
			if adr is None:
				continue
			list_items.append([candidate_type, '%s @ %s' % (proc_loc['unit'], proc_loc['organization'])])
			list_data.append(('%s @ %s' % (proc_loc['unit'], proc_loc['organization']), [adr], '\n'.join(proc_loc.format(with_comms = True)), proc_loc))
		del proc_locs

		candidate_type = _('Lab')
		labs = emr.get_labs_as_org_units()
		for lab in labs:
			adr = lab.address
			if adr is None:
				continue
			list_items.append([candidate_type, '%s @ %s' % (lab['unit'], lab['organization'])])
			list_data.append(('%s @ %s' % (lab['unit'], lab['organization']), [adr], '\n'.join(lab.format(with_comms = True)), lab))
		del labs

		candidate_type = _('Bill receiver')
		bills = self.__patient.bills
		adrs_seen = []
		for bill in bills:
			if bill['pk_receiver_address'] in adrs_seen:
				continue
			adr = bill.address
			if adr is None:
				continue
			adrs_seen.append(bill['pk_receiver_address'])
			details = '%s%s' % (bill['invoice_id'], gmDateTime.pydt_strftime(dt = bill['close_date'], format = ' (%Y %b %d)', none_str = ''))
			list_items.append([candidate_type, details])
			list_data.append(('', [adr], '\n'.join(adr.format()), None))

		candidate_type = _('Document')
		doc_folder = self.__patient.document_folder
		doc_units = doc_folder.all_document_org_units
		for doc_unit in doc_units:
			adr = doc_unit.address
			if adr is None:
				continue
			list_items.append([candidate_type, '%s @ %s' % (doc_unit['unit'], doc_unit['organization'])])
			list_data.append(('%s @ %s' % (doc_unit['unit'], doc_unit['organization']), [adr], '\n'.join(doc_unit.format(with_comms = True)), doc_unit))
		del doc_units

		self._LCTRL_candidates.set_string_items(list_items)
		self._LCTRL_candidates.set_column_widths()
		self._LCTRL_candidates.set_data(list_data)

	#------------------------------------------------------------
	def _get_candidate_tooltip(self, data):
		if data is None:
			return ''
		name, addresses, tt, unit = data
		return tt

	#------------------------------------------------------------
	def __update_address_info(self, adr):
		if adr is None:
			self._LBL_address_details.SetLabel('')
			self._LBL_final_country.SetLabel('')
			self._LBL_final_region.SetLabel('')
			self._LBL_final_zip.SetLabel('')
			self._LBL_final_location.SetLabel('')
			self._LBL_final_street.SetLabel('')
			self._LBL_final_number.SetLabel('')
			self.Layout()
			return
		self._LBL_address_details.SetLabel('\n'.join(adr.format()))
		self._LBL_final_country.SetLabel(adr['l10n_country'])
		self._LBL_final_region.SetLabel(adr['l10n_region'])
		self._LBL_final_zip.SetLabel(adr['postcode'])
		self._LBL_final_location.SetLabel('%s%s' % (adr['urb'], gmTools.coalesce(adr['suburb'], '', ' - %s')))
		self._LBL_final_street.SetLabel(adr['street'])
		self._LBL_final_number.SetLabel('%s%s' % (adr['number'], gmTools.coalesce(adr['subunit'], '', ' %s')))
		self.Layout()

	#------------------------------------------------------------
	def __populate_address_list(self, addresses=None):
		self._LCTRL_addresses.Enable()
		cols = [_(u'Address')]
		list_items = []
		for a in addresses:
			try:
				list_items.append([a['l10n_address_type'], a.format(single_line = True, verbose = False, show_type = False)])
				cols = [_('Type'), _('Address')]
			except KeyError:
				list_items.append([a.format(single_line = True, verbose = False, show_type = False)])
				cols = [_('Address')]

		self._LCTRL_addresses.set_columns(cols)
		self._LCTRL_addresses.set_string_items(list_items)
		self._LCTRL_candidates.set_column_widths()
		self._LCTRL_addresses.set_data(addresses)
		self._PRW_other_address.SetText(value = '', data = None)
		self.__update_address_info(None)

	#------------------------------------------------------------
	def _get_address_tooltip(self, adr):
		return '\n'.join(adr.format(show_type = True))

	#------------------------------------------------------------
	#------------------------------------------------------------
	def _on_final_name_modified(self):
		self._LBL_final_name.SetLabel(self._TCTRL_final_name.Value)

	#------------------------------------------------------------
	def _on_address_selected_in_PRW(self, address):
		self.__update_address_info(self._PRW_other_address.GetData(as_instance = True))

	#------------------------------------------------------------
	def _on_entering_org_unit_PRW(self):
		self._LCTRL_addresses.Disable()

	#------------------------------------------------------------
	def _on_org_unit_selected_in_PRW(self, unit):
		if unit is None:
			self._LCTRL_addresses.remove_items_safely(max_tries = 3)
			self._PRW_other_address.SetText(value = '', data = None)
			self.__update_address_info(None)
			self._TCTRL_org_unit_details.SetValue('')
			return

		unit = self._PRW_org_unit.GetData(as_instance = True)
		adr = unit.address
		if adr is None:
			self._LCTRL_addresses.remove_items_safely(max_tries = 3)
			self._PRW_other_address.SetText(value = '', data = None)
			self.__update_address_info(None)
		else:
			self.__populate_address_list(addresses = [adr])
			self._PRW_other_address.SetData(data = adr['pk_address'])
			self.__update_address_info(adr)

		name = '%s @ %s' % (unit['unit'], unit['organization'])
		self._TCTRL_final_name.SetValue(name)
		self._TCTRL_org_unit_details.SetValue('\n'.join(unit.format(with_comms = True)))
		self.Layout()

	#------------------------------------------------------------
	#------------------------------------------------------------
	def _on_candidate_selected(self, event):
		event.Skip()
		name, addresses, tt, unit = self._LCTRL_candidates.get_selected_item_data(only_one = True)
		self.__populate_address_list(addresses = addresses)
		if unit is None:
			self._PRW_org_unit.SetText(value = '', data = None)
			self._TCTRL_org_unit_details.SetValue('')
		else:
			self._PRW_org_unit.SetData(data = unit['pk_org_unit'])
			self._TCTRL_org_unit_details.SetValue('\n'.join(unit.format(with_comms = True)))
		self._TCTRL_final_name.SetValue(name.strip())
		self._LBL_final_name.SetLabel(name.strip())

	#------------------------------------------------------------
	def _on_address_activated_in_list(self, evt):
		evt.Skip()
		adr = self._LCTRL_addresses.get_selected_item_data(only_one = True)
		self._PRW_other_address.address = adr
		self.__update_address_info(adr)

	#------------------------------------------------------------
	#------------------------------------------------------------
	def _on_manage_addresses_button_pressed(self, event):
		event.Skip()
		manage_addresses(parent = self)

	#------------------------------------------------------------
	def _on_manage_orgs_button_pressed(self, event):
		event.Skip()
		from Gnumed.wxpython.gmOrganizationWidgets import manage_orgs
		manage_orgs(parent = self, no_parent = False)

	#------------------------------------------------------------
	def _on_ok_button_pressed(self, event):
		if self._TCTRL_final_name.GetValue().strip() == '':
			return False
		if self._PRW_other_address.address is None:
			return False
		event.Skip()
		self.EndModal(wx.ID_OK)

	#------------------------------------------------------------
	def _set_patient(self, patient):
		self.__patient = patient
		self.__init_ui()

	patient = property(lambda x:x, _set_patient)

	#------------------------------------------------------------
	def _get_name(self):
		return self._TCTRL_final_name.GetValue().strip()

	name = property(_get_name, lambda x:x)

	#------------------------------------------------------------
	def _get_address(self):
		return self._PRW_other_address.address

	address = property(_get_address, lambda x:x)

#============================================================
# main
#------------------------------------------------------------
if __name__ == '__main__':

	gmI18N.activate_locale()
	gmI18N.install_domain(domain = 'gnumed')

	#----------------------------------------
	def test_cFormTemplateEAPnl():
		app = wx.PyWidgetTester(size = (400, 300))
		pnl = cFormTemplateEAPnl(app.frame, -1, template = gmForms.cFormTemplate(aPK_obj=4))
		app.frame.Show(True)
		app.MainLoop()
		return
	#----------------------------------------
	if (len(sys.argv) > 1) and (sys.argv[1] == 'test'):
		test_cFormTemplateEAPnl()

#============================================================