URI: 
       tchoice_dialog.py - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
       tchoice_dialog.py (2574B)
       ---
            1 from kivy.app import App
            2 from kivy.factory import Factory
            3 from kivy.properties import ObjectProperty
            4 from kivy.lang import Builder
            5 from kivy.uix.checkbox import CheckBox
            6 from kivy.uix.label import Label
            7 from kivy.uix.widget import Widget
            8 
            9 Builder.load_string('''
           10 <ChoiceDialog@Popup>
           11     id: popup
           12     title: ''
           13     description: ''
           14     size_hint: 0.8, 0.8
           15     pos_hint: {'top':0.9}
           16     BoxLayout:
           17         orientation: 'vertical'
           18         Label:
           19             size_hint: 1, None
           20             text: root.description
           21             halign: 'left'
           22             text_size: self.width, None
           23             size: self.texture_size
           24         ScrollView:
           25             orientation: 'vertical'
           26             size_hint: 1, 0.8
           27             GridLayout:
           28                 row_default_height: '48dp'
           29                 id: choices
           30                 cols: 2
           31                 size_hint: 1, None
           32         BoxLayout:
           33             orientation: 'horizontal'
           34             size_hint: 1, 0.2
           35             Button:
           36                 text: 'Cancel'
           37                 size_hint: 0.5, None
           38                 height: '48dp'
           39                 on_release: popup.dismiss()
           40             Button:
           41                 text: 'OK'
           42                 size_hint: 0.5, None
           43                 height: '48dp'
           44                 on_release:
           45                     root.callback(popup.value)
           46                     popup.dismiss()
           47 ''')
           48 
           49 class ChoiceDialog(Factory.Popup):
           50 
           51     def __init__(self, title, choices, key, callback, *, description='', keep_choice_order=False):
           52         Factory.Popup.__init__(self)
           53         self.description = description
           54         if keep_choice_order:
           55             orig_index = {choice: i for (i, choice) in enumerate(choices)}
           56             sort_key = lambda x: orig_index[x[0]]
           57         else:
           58             sort_key = lambda x: x
           59         if type(choices) is list:
           60             choices = dict(map(lambda x: (x,x), choices))
           61         layout = self.ids.choices
           62         layout.bind(minimum_height=layout.setter('height'))
           63         for k, v in sorted(choices.items(), key=sort_key):
           64             l = Label(text=v)
           65             l.height = '48dp'
           66             l.size_hint_x = 4
           67             cb = CheckBox(group='choices')
           68             cb.value = k
           69             cb.height = '48dp'
           70             cb.size_hint_x = 1
           71             def f(cb, x):
           72                 if x: self.value = cb.value
           73             cb.bind(active=f)
           74             if k == key:
           75                 cb.active = True
           76             layout.add_widget(l)
           77             layout.add_widget(cb)
           78         layout.add_widget(Widget(size_hint_y=1))
           79         self.callback = callback
           80         self.title = title
           81         self.value = key