electrum-bitcoinprivate/gui/kivy/uix/dialogs/choice_dialog.py

70 lines
2.0 KiB
Python
Raw Normal View History

from kivy.app import App
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.checkbox import CheckBox
from kivy.uix.label import Label
2015-12-16 02:53:37 -08:00
from kivy.uix.widget import Widget
Builder.load_string('''
<ChoiceDialog@Popup>
id: popup
title: ''
size_hint: 0.8, 0.8
pos_hint: {'top':0.9}
BoxLayout:
orientation: 'vertical'
Widget:
2015-12-16 02:53:37 -08:00
size_hint: 1, 0.1
ScrollView:
orientation: 'vertical'
size_hint: 1, 0.8
2015-12-16 02:53:37 -08:00
GridLayout:
row_default_height: '48dp'
orientation: 'vertical'
id: choices
cols: 2
2016-01-20 22:19:22 -08:00
size_hint: 1, None
BoxLayout:
orientation: 'horizontal'
2015-12-16 02:53:37 -08:00
size_hint: 1, 0.2
Button:
text: 'Cancel'
size_hint: 0.5, None
height: '48dp'
on_release: popup.dismiss()
Button:
text: 'OK'
size_hint: 0.5, None
height: '48dp'
on_release:
root.callback(popup.value)
popup.dismiss()
''')
class ChoiceDialog(Factory.Popup):
2015-12-16 02:53:37 -08:00
def __init__(self, title, choices, key, callback):
Factory.Popup.__init__(self)
2016-01-21 03:12:55 -08:00
if type(choices) is list:
choices = dict(map(lambda x: (x,x), choices))
2016-01-20 22:19:22 -08:00
layout = self.ids.choices
layout.bind(minimum_height=layout.setter('height'))
2016-01-21 03:12:55 -08:00
for k, v in sorted(choices.items()):
2015-12-16 02:53:37 -08:00
l = Label(text=v)
l.height = '48dp'
cb = CheckBox(group='choices')
cb.value = k
2015-12-16 02:53:37 -08:00
cb.height = '48dp'
def f(cb, x):
if x: self.value = cb.value
cb.bind(active=f)
2015-12-16 02:53:37 -08:00
if k == key:
cb.active = True
2016-01-20 22:19:22 -08:00
layout.add_widget(l)
layout.add_widget(cb)
layout.add_widget(Widget(size_hint_y=1))
self.callback = callback
self.title = title
2015-12-16 02:53:37 -08:00
self.value = key