SHARE
TWEET

Untitled

a guest May 20th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2.  
  3. """
  4. Dialog
  5. ======
  6.  
  7. Copyright (c) 2015 Andrés Rodríguez and KivyMD contributors -
  8.     KivyMD library up to version 0.1.2
  9. Copyright (c) 2019 Ivanov Yuri and KivyMD contributors -
  10.     KivyMD library version 0.1.3 and higher
  11.  
  12. For suggestions and questions:
  13. <kivydevelopment@gmail.com>
  14.  
  15. This file is distributed under the terms of the same license,
  16. as the Kivy framework.
  17.  
  18. `Material Design spec, Dialogs <https://material.io/design/components/dialogs.html>`_
  19.  
  20. Example
  21. -------
  22.  
  23. from kivy.app import App
  24. from kivy.lang import Builder
  25. from kivy.factory import Factory
  26. from kivy.utils import get_hex_from_color
  27.  
  28. from kivymd.dialog import MDInputDialog, MDDialog
  29. from kivymd.theming import ThemeManager
  30.  
  31.  
  32. Builder.load_string('''
  33. #:import MDToolbar kivymd.toolbar.MDToolbar
  34. #:import MDRectangleFlatButton kivymd.button.MDRectangleFlatButton
  35.  
  36.  
  37. <ExampleDialogs@BoxLayout>
  38.     orientation: 'vertical'
  39.     spacing: dp(5)
  40.  
  41.     MDToolbar:
  42.         id: toolbar
  43.         title: app.title
  44.         left_action_items: [['menu', lambda x: None]]
  45.         elevation: 10
  46.         md_bg_color: app.theme_cls.primary_color
  47.  
  48.     FloatLayout:
  49.         MDRectangleFlatButton:
  50.             text: "Open input dialog"
  51.             pos_hint: {'center_x': .5, 'center_y': .7}
  52.             opposite_colors: True
  53.             on_release: app.show_example_input_dialog()
  54.  
  55.         MDRectangleFlatButton:
  56.             text: "Open Ok Cancel dialog"
  57.             pos_hint: {'center_x': .5, 'center_y': .5}
  58.             opposite_colors: True
  59.             on_release: app.show_example_okcancel_dialog()
  60. ''')
  61.  
  62.  
  63. class Example(App):
  64.     theme_cls = ThemeManager()
  65.     theme_cls.primary_palette = 'Teal'
  66.     title = "Dialogs"
  67.  
  68.     def build(self):
  69.         return Factory.ExampleDialogs()
  70.  
  71.     def show_example_input_dialog(self):
  72.         dialog = MDInputDialog(
  73.             title='Title', hint_text='Hint text', size_hint=(.8, .4),
  74.             text_button_ok='Yes', events_callback=lambda x: None)
  75.         dialog.open()
  76.  
  77.     def show_example_okcancel_dialog(self):
  78.         dialog = MDDialog(
  79.             title='Title', size_hint=(.8, .3), text_button_ok='Yes',
  80.             text="Your [color=%s][b]text[/b][/color] dialog" % get_hex_from_color(
  81.                 self.theme_cls.primary_color),
  82.             text_button_cancel='Cancel', events_callback=lambda x: None)
  83.         dialog.open()
  84.  
  85.  
  86. Example().run()
  87. """
  88.  
  89. from kivy.clock import Clock
  90. from kivy.lang import Builder
  91. from kivy.properties import StringProperty, ObjectProperty, BooleanProperty
  92. from kivy.metrics import dp
  93. from kivy.uix.anchorlayout import AnchorLayout
  94. from kivy.uix.boxlayout import BoxLayout
  95. from kivy.uix.modalview import ModalView
  96. from kivy.uix.textinput import TextInput
  97.  
  98. from kivymd.cards import MDCard
  99. from kivymd.textfields import MDTextField, MDTextFieldRect
  100. from kivymd.theming import ThemableBehavior
  101. from kivymd.button import MDFlatButton, MDRaisedButton, MDTextButton
  102. from kivymd import images_path
  103. from kivymd.material_resources import DEVICE_IOS
  104.  
  105.  
  106. Builder.load_string('''
  107. #:import images_path kivymd.images_path
  108. #:import MDSeparator kivymd.cards.MDSeparator
  109.  
  110.  
  111. <ContentInputDialog>
  112.     orientation: 'vertical'
  113.     padding: dp(15)
  114.     spacing: dp(10)
  115.  
  116.     MDLabel:
  117.         font_style: 'H6'
  118.         text: root.title
  119.         halign: 'left' if not root.device_ios else 'center'
  120.  
  121.     BoxLayout:
  122.         id: box_input
  123.         size_hint: 1, None
  124.  
  125.     Widget:
  126.     Widget:
  127.  
  128.     MDSeparator:
  129.         id: sep
  130.  
  131.     BoxLayout:
  132.         id: box_buttons
  133.         size_hint_y: None
  134.         height: dp(20)
  135.         padding: dp(20), 0, dp(20), 0
  136.  
  137.  
  138. <ContentMDDialog>
  139.     orientation: 'vertical'
  140.     padding: dp(15)
  141.     spacing: dp(10)
  142.  
  143.     text_button_ok: ''
  144.     text_button_cancel: ''
  145.  
  146.     MDLabel:
  147.         id: title
  148.         text: root.title
  149.         font_style: 'H6'
  150.         halign: 'left' if not root.device_ios else 'center'
  151.         valign: 'top'
  152.         size_hint_y: None
  153.         text_size: self.width, None
  154.         height: self.texture_size[1]
  155.  
  156.     ScrollView:
  157.         id: scroll
  158.         size_hint_y: None
  159.         height:
  160.             root.height - (box_buttons.height + title.height + dp(48)\
  161.             + sep.height)
  162.  
  163.         canvas:
  164.             Rectangle:
  165.                 pos: self.pos
  166.                 size: self.size
  167.                 #source: '{}dialog_in_fade.png'.format(images_path)
  168.                 source: '{}transparent.png'.format(images_path)
  169.  
  170.         MDLabel:
  171.             text: '\\n' + root.text + '\\n'
  172.             size_hint_y: None
  173.             height: self.texture_size[1]
  174.             valign: 'top'
  175.             halign: 'left' if not root.device_ios else 'center'
  176.             markup: True
  177.  
  178.     MDSeparator:
  179.         id: sep
  180.  
  181.     BoxLayout:
  182.         id: box_buttons
  183.         size_hint_y: None
  184.         height: dp(20)
  185.         padding: dp(20), 0, dp(20), 0
  186. ''')
  187.  
  188. if DEVICE_IOS:
  189.     Heir = BoxLayout
  190. else:
  191.     Heir = MDCard
  192.  
  193.  
  194. # FIXME: Not work themes for iOS.
  195. class BaseDialog(ThemableBehavior, ModalView):
  196.  
  197.     def set_content(self, instance_content_dialog):
  198.         def _events_callback(result_press):
  199.             self.dismiss()
  200.             if result_press:
  201.                 self.events_callback(result_press, self)
  202.  
  203.         if self.device_ios:  # create buttons for iOS
  204.             self.background = self._background
  205.  
  206.             if instance_content_dialog.__class__ is ContentInputDialog:
  207.                 self.text_field = MDTextFieldRect(
  208.                     pos_hint={'center_x': .5},
  209.                     size_hint=(1, None), multiline=False, height=dp(33),
  210.                     cursor_color=self.theme_cls.primary_color,
  211.                     hint_text=instance_content_dialog.hint_text)
  212.                 instance_content_dialog.ids.box_input.height = dp(33)
  213.                 instance_content_dialog.ids.box_input.add_widget(
  214.                     self.text_field)
  215.  
  216.             if self.text_button_cancel != '':
  217.                 anchor = 'left'
  218.             else:
  219.                 anchor = 'center'
  220.             box_button_ok = AnchorLayout(anchor_x=anchor)
  221.             box_button_ok.add_widget(
  222.                 MDTextButton(
  223.                     text=self.text_button_ok, font_size='18sp',
  224.                     on_release=lambda x: _events_callback(
  225.                         self.text_button_ok)))
  226.             instance_content_dialog.ids.box_buttons.add_widget(box_button_ok)
  227.  
  228.             if self.text_button_cancel != '':
  229.                 box_button_ok.anchor_x = 'left'
  230.                 box_button_cancel = AnchorLayout(anchor_x='right')
  231.                 box_button_cancel.add_widget(
  232.                     MDTextButton(
  233.                         text=self.text_button_cancel, font_size='18sp',
  234.                         on_release=lambda x: _events_callback(
  235.                             self.text_button_cancel)))
  236.                 instance_content_dialog.ids.box_buttons.add_widget(
  237.                     box_button_cancel)
  238.  
  239.         else:  # create buttons for Android
  240.             if instance_content_dialog.__class__ is ContentInputDialog:
  241.                 self.text_field = MDTextField(
  242.                     size_hint=(1, None), height=dp(48),
  243.                     hint_text=instance_content_dialog.hint_text)
  244.                 instance_content_dialog.ids.box_input.height = dp(48)
  245.                 instance_content_dialog.ids.box_input.add_widget(
  246.                     self.text_field)
  247.                 instance_content_dialog.ids.box_buttons.remove_widget(
  248.                     instance_content_dialog.ids.sep)
  249.  
  250.             box_buttons = AnchorLayout(
  251.                 anchor_x='right', size_hint_y=None, height=dp(30))
  252.             box = BoxLayout(size_hint_x=None, spacing=dp(5))
  253.             box.bind(minimum_width=box.setter('width'))
  254.             button_ok = MDRaisedButton(
  255.                 text=self.text_button_ok,
  256.                 on_release=lambda x: _events_callback(self.text_button_ok))
  257.             box.add_widget(button_ok)
  258.  
  259.             if self.text_button_cancel != '':
  260.                 button_cancel = MDFlatButton(
  261.                     text=self.text_button_cancel,
  262.                     theme_text_color='Custom',
  263.                     text_color=self.theme_cls.primary_color,
  264.                     on_release=lambda x: _events_callback(
  265.                         self.text_button_cancel))
  266.                 box.add_widget(button_cancel)
  267.  
  268.             box_buttons.add_widget(box)
  269.             instance_content_dialog.ids.box_buttons.add_widget(box_buttons)
  270.             instance_content_dialog.ids.box_buttons.height = button_ok.height
  271.             instance_content_dialog.remove_widget(
  272.                 instance_content_dialog.ids.sep)
  273.  
  274.  
  275. class MDInputDialog(BaseDialog):
  276.     title = StringProperty('Title')
  277.     hint_text = StringProperty()
  278.     text_button_ok = StringProperty('Ok')
  279.     text_button_cancel = StringProperty('Cancel')
  280.     events_callback = ObjectProperty()
  281.     _background = StringProperty('{}ios_bg_mod.png'.format(images_path))
  282.  
  283.     def __init__(self, **kwargs):
  284.         super(MDInputDialog, self).__init__(**kwargs)
  285.  
  286.         self.content_dialog = ContentInputDialog(
  287.             title=self.title,
  288.             hint_text=self.hint_text,
  289.             text_button_ok=self.text_button_ok,
  290.             text_button_cancel=self.text_button_cancel,
  291.             device_ios=self.device_ios)
  292.         self.add_widget(self.content_dialog)
  293.         self.set_content(self.content_dialog)
  294.         Clock.schedule_once(self.set_field_focus, .5)
  295.  
  296.     def set_field_focus(self, interval):
  297.         self.text_field.focus = True
  298.  
  299.  
  300. class MDDialog(BaseDialog):
  301.     title = StringProperty('Title')
  302.     text = StringProperty('Text dialog')
  303.     text_button_cancel = StringProperty()
  304.     text_button_ok = StringProperty('Ok')
  305.     events_callback = ObjectProperty()
  306.     _background = StringProperty('{}ios_bg_mod.png'.format(images_path))
  307.  
  308.     def __init__(self, **kwargs):
  309.         super(MDDialog, self).__init__(**kwargs)
  310.         content_dialog = ContentMDDialog(
  311.             title=self.title, text=self.text,
  312.             text_button_ok=self.text_button_ok,
  313.             text_button_cancel=self.text_button_cancel,
  314.             device_ios=self.device_ios)
  315.         self.add_widget(content_dialog)
  316.         self.set_content(content_dialog)
  317.  
  318.  
  319. class ContentInputDialog(Heir):
  320.     title = StringProperty()
  321.     hint_text = StringProperty()
  322.     text_button_ok = StringProperty()
  323.     text_button_cancel = StringProperty()
  324.     device_ios = BooleanProperty()
  325.  
  326.  
  327. class ContentMDDialog(Heir):
  328.     title = StringProperty()
  329.     text = StringProperty()
  330.     text_button_cancel = StringProperty()
  331.     text_button_ok = StringProperty()
  332.     device_ios = BooleanProperty()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top