-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWellLitGUI.py
173 lines (133 loc) · 4.82 KB
/
WellLitGUI.py
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
#!/usr/bin/env python3
# Joana Cabrera
# 3/15/2020
# revised
# Andrew Cote
# 6/30/2020
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
import kivy
import json, os
kivy.require('1.11.1')
from kivy.app import App
from kivy.config import Config
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
# noinspection ProblematicWhitespace
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.metrics import sp
from kivy.properties import ObjectProperty, StringProperty
from .plateLighting import PlateLighting
from abc import ABC, abstractmethod
class WellLitWidget(FloatLayout):
status = StringProperty('')
def __init__(self, **kwargs):
super(WellLitWidget, self).__init__(**kwargs)
self._popup = None
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down=self._on_keyboard_up)
Config.set('kivy', 'exit_on_escape', 0)
def updateLights(self):
"""
To be called after every user action, updating the current WellPlots by accessing self.ids.___plate
"""
pass
def _keyboard_closed(self):
self._keyboard.unbind(on_key_up=self._on_keyboard_up)
self._keyboard = None
def _on_keyboard_up(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'esc' or keycode[1] == 'q':
self.showPopup('Are you sure you want to exit?', 'Confirm exit', func=self.quit)
def log(self, msg):
self.status = msg
def dismiss_popup(self):
self._popup.dismiss()
def showPopup(self, error, title: str, func=None):
self._popup = WellLitPopup()
self._popup.title = title
self._popup.show(error.__str__(), func=func)
def reset_plates(self, config_path):
self.ids.source_plate.initialize(config_path)
self.ids.dest_plate.initialize(config_path)
def resetAll(self):
self.ids.notificationLabel.font_size = 20
def quit(self, _):
App.get_running_app().stop()
class WellPlot(BoxLayout):
'''
Acts as a Kivy GUI block which contains a matplotlib figure as its only content.
matplotlib figure resizes with BoxLayout resizing.
Owns a PlateLighting object
'''
shape = ObjectProperty(None)
def __init__(self, **kwargs):
super(WellPlot, self).__init__(**kwargs)
self.pl = None
def initialize(self, config_path):
# load configs
with open(config_path) as json_file:
configs = json.load(json_file)
self.num_wells = configs['num_wells']
if self.type == 'source_plate':
A1_X = configs[self.num_wells]["A1_X_source"]
A1_Y = configs[self.num_wells]["A1_Y_source"]
if self.type == 'dest_plate':
A1_X = configs[self.num_wells]["A1_X_dest"]
A1_Y = configs[self.num_wells]["A1_Y_dest"]
size_param = configs[self.num_wells]["size_param"]
well_spacing = configs[self.num_wells]["well_spacing"]
# set up PlateLighting object
self.pl = PlateLighting(A1_X, A1_Y, self.shape, size_param, well_spacing, self.num_wells)
self.add_widget(FigureCanvasKivyAgg(figure=self.pl.fig))
def on_touch_down(self, touch):
# this method keeps the app from crashing if the plot is clicked on
pass
class ConfirmPopup(Popup):
def __init__(self, txt_file_path=None):
super(ConfirmPopup, self).__init__()
self.pos_hint={'left': 1}
self.txt_file_path = txt_file_path
def show(self, text):
content = BoxLayout(orientation='vertical')
popup_lb = Label(text=text, font_size=25)
content.add_widget(popup_lb)
button_box = BoxLayout(orientation='horizontal', size_hint=(1, .4))
content.add_widget(button_box)
# configure yes button
self.yes_button = Button(text='yes')
button_box.add_widget(self.yes_button)
self.yes_button.bind(on_press=self.yes_callback)
self.yes_button.bind(on_press=self.dismiss)
# configure no button
no_button = Button(text='no')
button_box.add_widget(no_button)
no_button.bind(on_press=self.dismiss)
self.content = content
self.open()
def yes_callback(self, *args):
if self.txt_file_path:
txt_file = open(self.txt_file_path, "w")
txt_file.close()
class WellLitPopup(Popup):
def __init__(self):
super(WellLitPopup, self).__init__()
def show(self, error_str, func=None):
content = BoxLayout(orientation='vertical')
popup_lb = Label(text=error_str, font_size=25, text_size=(sp(450), sp(800)), halign='center', valign='center')
content.add_widget(popup_lb)
if func is not None:
confirm_button = Button(text='Confirm', size_hint=(0.5, 0.4))
content.add_widget(confirm_button)
confirm_button.bind(on_press=func)
confirm_button.bind(on_press=self.dismiss)
close_button = Button(text='Cancel', size_hint=(0.5, .4))
else:
close_button = Button(text='Close', size_hint=(0.5, .4))
content.add_widget(close_button)
close_button.bind(on_press=self.dismiss)
self.content = content
self.open()