2n2u

Untitled

Apr 11th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.45 KB | None | 0 0
  1. __version__ = '1.0.0'
  2. # kivy imports
  3. from kivy.app import App
  4. from kivy.metrics import dp
  5. from kivy.uix.boxlayout import BoxLayout
  6. from kivy.uix.anchorlayout import AnchorLayout
  7. from kivy.uix.relativelayout import RelativeLayout
  8. from kivy.uix.gridlayout import GridLayout
  9. from kivy.uix.widget import Widget
  10. from kivy.uix.scrollview import ScrollView
  11. from kivy.storage.jsonstore import JsonStore
  12. from kivy.factory import Factory
  13. from kivy.properties import ObjectProperty, ListProperty, BooleanProperty, StringProperty, NumericProperty, VariableListProperty
  14. from kivy.animation import Animation
  15. from kivy.graphics.vertex_instructions import Line
  16. from kivy.graphics import Color, Rectangle
  17. from functools import partial
  18. from collections import deque
  19.  
  20.  
  21. # Material Design
  22. # from kivymd.bottomsheet import MDListBottomSheet, MDGridBottomSheet
  23. # from kivymd.button import MDIconButton, MDFlatButton
  24. # from kivymd.label import MDLabel
  25. # from kivymd.list import ILeftBody, ILeftBodyTouch, IRightBodyTouch, MDList, OneLineListItem
  26. # from kivymd.selectioncontrols import MDCheckbox
  27. from kivymd.theming import ThemeManager
  28. import kivymd.snackbar as Snackbar
  29. from kivymd.navigationdrawer import NavigationDrawerIconButton
  30. from kivymd.navigationdrawer import NavigationDrawer
  31. from kivymd.menu import MDDropdownMenu
  32. from custom_toolbar import Toolbar
  33. from kivymd.button import MDFlatButton
  34. from kivymd.dialog import MDDialog
  35. from kivymd.label import MDLabel
  36. import kivymd.material_resources as m_res
  37. from Screens import *
  38. from license import license
  39.  
  40. __author__ = 'Andre'
  41.  
  42. # Global Vars
  43. default_language = 'English'
  44. # ['Pink', 'Blue', 'Indigo', 'BlueGrey', 'Brown', 'LightBlue', 'Purple', 'Grey', 'Yellow', 'LightGreen', 'DeepOrange',
  45. # 'Green', 'Red', 'Teal', 'Orange', 'Cyan', 'Amber', 'DeepPurple', 'Lime']
  46. theme_color = 'BlueGrey'
  47.  
  48.  
  49. class Snackbars(Widget):
  50.     finished = BooleanProperty(False)
  51.  
  52.     def __init__(self, **kwargs):
  53.         super(Snackbars, self).__init__(**kwargs)
  54.         self.queue = deque()
  55.         self.playing = False
  56.  
  57.     def make(self, text, button_text=None, button_callback=None, duration=3, change_to_home=False):
  58.         if button_text is not None and button_callback is not None:
  59.             self.queue.append(Snackbar._SnackbarWidget(text=text,
  60.                                                        button_text=button_text,
  61.                                                        button_callback=button_callback,
  62.                                                        duration=duration))
  63.         else:
  64.             self.queue.append(Snackbar._SnackbarWidget(text=text,
  65.                                                        duration=duration))
  66.         if change_to_home:
  67.             self.queue[0].bind(top=self.get_anim_finished)
  68.         self._play_next()
  69.  
  70.     def _play_next(self, dying_widget=None):
  71.         # global playing
  72.         if (dying_widget or not self.playing) and len(self.queue) > 0:
  73.             self.playing = True
  74.             self.queue.popleft().begin()
  75.         elif len(self.queue) == 0:
  76.             self.playing = False
  77.  
  78.     def get_anim_finished(self, instance, value):
  79.         if value == 0:
  80.             self.finished = True
  81.  
  82.  
  83. class FutsalRoot(BoxLayout):
  84.     theme_style = StringProperty()  # hack because theme_cls.theme_style isn't a bindable property
  85.  
  86.     # language = StringProperty()
  87.  
  88.     def __init__(self, **kwargs):
  89.         super(FutsalRoot, self).__init__(**kwargs)
  90.         Clock.schedule_once(self.my_init)
  91.  
  92.     def my_init(self, dt):
  93.         if not app.store.exists('theme'):
  94.             app.store.put('theme', style='Light')
  95.         # if not app.store.exists('languages'):
  96.         #     app.store.put('languages', lang=default_language)
  97.         self.theme_style = app.theme_cls.theme_style = app.store.get('theme')['style']
  98.         self.bind(theme_style=self.write_store_style)
  99.         # self.language = app.store.get('languages')['lang']
  100.         # self.bind(language=self.write_store_language)
  101.  
  102.         if self.theme_style == 'Dark':
  103.             app.theme_color = [1, 1, 1, 1]
  104.         else:
  105.             app.theme_color = app.theme_color = [0, 0, 0, 1]
  106.  
  107.     def change_theme(self):
  108.         if app.theme_cls.theme_style == 'Light':
  109.             app.theme_cls.theme_style = 'Dark'
  110.             app.theme_color = [1, 1, 1, 1]
  111.             # self.theme_color = [1, 1, 1, 1]
  112.         else:
  113.             app.theme_cls.theme_style = 'Light'
  114.             self.theme_color = app.theme_color = [0, 0, 0, 1]
  115.         self.theme_style = app.theme_cls.theme_style
  116.  
  117.     # def show_dialog(self):
  118.     #     self.content = BoxLayout(orientation='vertical')
  119.     #     languages = ["English", "Portuguese"]
  120.     #     for c in languages:
  121.     #         if c == self.language:
  122.     #             color = app.theme_cls.primary_color
  123.     #         else:
  124.     #             color = app.theme_color
  125.     #         print color
  126.     #         #child_width = content.width
  127.     #         a = MDFlatButton(
  128.     #                 text=c,
  129.     #                 size_hint=(1, None),
  130.     #                 height=dp(36),
  131.     #                 #width=3*dp(48),
  132.     #                 #text_color=color,
  133.     #                 background_color=app.theme_cls.bg_light
  134.     #             )
  135.     #         a.ids['_label'].halign = 'left'
  136.     #         a.ids['_label'].text_color = color
  137.     #         a.bind(on_release=partial(self.change_language, a.text))
  138.     #         a.bind(on_press=self.change_label_color)
  139.     #     .setter    self.content.add_widget(a)
  140.     #
  141.     #     #content.bind(size=content.ml.setter('text_size'))
  142.     #     self.dialog = MDDialog(title="Pick a language",
  143.     #                             content=self.content,
  144.     #                             size_hint=(.8, None),
  145.     #                             height=dp(200),
  146.     #                             auto_dismiss=False)
  147.     #
  148.     #     # self.dialog.add_action_button("Dismiss",
  149.     #     #                                 action=partial(self.change_language, 'pt')) #self.dialog.dismiss())
  150.     #     self.dialog.open()
  151.  
  152.     # def change_label_color(self, instance):
  153.     #     instance.ids['_label'].text_color = app.theme_cls.primary_color
  154.     #     print [c for c in self.content.walk(restrict=True) if c != instance and isinstance(c, MDFlatButton)]
  155.     #     #c.ids['_label'].text_color.setter(app.theme_color)
  156.     #
  157.     # def change_language(self, arg, instance):
  158.     #     self.language = arg
  159.     #     print self.language
  160.     #     print instance.parent.width
  161.     #     print instance.width
  162.  
  163.     #     self.dialog.dismiss()
  164.  
  165.     def write_store_style(self, instance, arg):
  166.         app.store.put('theme', style=arg)
  167.  
  168.         # def write_store_language(self, instance, arg):
  169.         #     app.store.put('languages', lang=arg)
  170.  
  171.         # self.theme_color = [0, 0, 0, 1]
  172.         # self.ids['sm'].ids['record_screen_id'].ids['test_record'].ids['graphlayout'].graph.tick_color = app.theme_color
  173.         # for elem in self.graphs:
  174.         #     #elem.graph.tick_color = app.theme_color
  175.         #     elem.graph.border_color = app.theme_color
  176.         #     elem.graph.label_options['color'] = app.theme_color
  177.         # elem.graph._redraw_all()
  178.         # print elem.graph.label_options['color']
  179.         # print len(self.graphs)
  180.         # print self.ids['sm'].ids['register_screen_id'].ids
  181.         # print self.ids['sm'].ids['register_screen_id'].ids['t_registerform'].ids['t_name'].foreground_color
  182.         # print self.ids['sm'].ids['record_screen_id'].ids['test_record'].ids['graphlayout'].graph.tick_color
  183.  
  184.  
  185. class FutsalNavDrawer(NavigationDrawer):
  186.     pass
  187.  
  188.  
  189. class NavDrawerChangeTheme(NavigationDrawerIconButton):
  190.     pass
  191.  
  192.  
  193. class NavDrawerChangeLanguage(NavigationDrawerIconButton):
  194.     pass
  195.  
  196.  
  197. class MainToolBar(Toolbar):
  198.     dots_menu = ObjectProperty()
  199.     tabs_layout = ObjectProperty()
  200.     tab1 = ObjectProperty()
  201.     tab2 = ObjectProperty()
  202.     sm = ObjectProperty()
  203.  
  204.     def __init__(self, **kwargs):
  205.         super(MainToolBar, self).__init__(**kwargs)
  206.         Clock.schedule_once(self.my_init, -1)
  207.  
  208.     def my_init(self, dt):
  209.         self.dots_menu = DotsMenu()
  210.         self.id = 'toolbar'
  211.         self.title = 'Futsal World'
  212.         self.left_action_items = [['menu', lambda x: app.nav_drawer.toggle()]]
  213.         self.right_action_items = [['more-vert', lambda x: self.dots_menu.open(self.ids.right_actions)]]
  214.         self.size_hint_y = .2
  215.         # self.tab1.text = 'News'
  216.         # self.tab1.text_color = (1, 1, 1, 1)  # white color
  217.         # self.tab1.bind(on_release=self.change_screen)
  218.         # self.tab2.text = 'Livescores'
  219.         # self.tab2.text_color = (1, 1, 1, 1)  # white color
  220.         # self.tab2.bind(on_release=self.change_screen)
  221.         #print self.tabs_layout.x
  222.         #self.tabs_layout.canvas.after.get_group('rectangle')[0].size = (self.tab1.width, dp(10))
  223.         #app.nav_drawer.bind(center_x=self.test)
  224.  
  225.  
  226.         # with self.tabs_layout.canvas.after:
  227.         #     Color(1, 0, 0, 1)  # set the colour to red
  228.         #     self.tabs_layout.rect = Rectangle(pos=(0, 500),
  229.         #                           size=(self.tab1.width,
  230.         #                                 dp(4)))
  231.  
  232.  
  233.     def change_screen(self, instance):
  234.         print instance.parent
  235.         #self.move_rect(instance)
  236.         if instance.name == 'tab1':
  237.             app.root.ids['sm'].change_screen('News')
  238.         if instance.name == 'tab2':
  239.             if not app.root.ids['sm'].has_screen('Livescores'):
  240.                 app.root.ids['sm'].add_widget(LiveScoresScreen())
  241.                 app.root.ids['sm'].change_screen('Livescores')
  242.             else:
  243.                 app.root.ids['sm'].change_screen('Livescores')
  244.  
  245.     def move_rect(self, instance):
  246.         animation = Animation(size=(instance.width, dp(4)), duration=.4)
  247.         animation &= Animation(pos=(instance.x, instance.y), duration=.4)
  248.         animation.start(self.tabs_layout.canvas.after.get_group('rectangle')[0])
  249.  
  250.  
  251. class ShowLicense(MDDialog):
  252.     link_label = ObjectProperty()
  253.  
  254.     def __init__(self, **kwargs):
  255.         super(ShowLicense, self).__init__(**kwargs)
  256.         self.title = "License Information"
  257.         self.link_label.text = license
  258.         #self.content.bind(size=self.link_label.setter('text_size'))
  259.  
  260.     def custom_open(self, menu):
  261.         menu.dismiss()
  262.         self.open()
  263.  
  264.  
  265. class DotsMenu(MDDropdownMenu):
  266.     def __init__(self, **kwargs):
  267.         super(DotsMenu, self).__init__(**kwargs)
  268.         self.items = [
  269.             {'viewclass': 'MDFlatButton',
  270.              'text': 'Legal'},
  271.             {'viewclass': 'MDFlatButton',
  272.              'text': 'Licenses',
  273.              'on_release': lambda: self.menu_license.custom_open(self)},
  274.             {'viewclass': 'MDFlatButton',
  275.              'text': 'Settings',
  276.              'on_release': lambda: app.open_settings()}
  277.         ]
  278.         self.hor_growth = 'left'
  279.         self.ver_growth = 'down'
  280.         self.width_mult = 3
  281.         self.menu_license = ShowLicense()
  282.         self.control_size = (app.root.width - dp(3), app.root.top - dp(3))
  283.         app.root.bind(width=self.adapt_control_width)
  284.         app.root.bind(height=self.adapt_control_height)
  285.  
  286.     def adapt_control_width(self, x, y):
  287.         self.control_size = (app.root.width - dp(3), self.control_size[1])
  288.         self.ids['md_menu'].right = self.control_size[0]
  289.  
  290.     def adapt_control_height(self, x, y):
  291.         self.control_size = (self.control_size[0], app.root.top - dp(3))
  292.         self.ids['md_menu'].top = self.control_size[1]
  293.  
  294.     # Over-ride Open Behaviour
  295.     def open(self, *largs):
  296.         Window.add_widget(self)
  297.         Clock.schedule_once(lambda x: self.display_menu(largs[0]), -1)
  298.  
  299.     def display_menu(self, caller):
  300.         # We need to pick a starting point, see how big we need to be,
  301.         # and where to grow to.
  302.  
  303.         # ---ESTABLISH INITIAL TARGET SIZE ESTIMATE---
  304.         target_width = self.width_mult * m_res.STANDARD_INCREMENT
  305.         # If we're wider than the Window...
  306.         if target_width > Window.width:
  307.             # ...reduce our multiplier to max allowed.
  308.             target_width = int(
  309.                 Window.width / m_res.STANDARD_INCREMENT) * m_res.STANDARD_INCREMENT
  310.  
  311.         target_height = sum([dp(48) for i in self.items])
  312.         # If we're over max_height...
  313.         if self.max_height > 0 and target_height > self.max_height:
  314.             target_height = self.max_height
  315.  
  316.         # ---ESTABLISH VERTICAL GROWTH DIRECTION---
  317.         if self.ver_growth is not None:
  318.             ver_growth = self.ver_growth
  319.         else:
  320.             # If there's enough space below us:
  321.             if target_height <= self.control_size[1] - self.border_margin:
  322.                 ver_growth = 'down'
  323.             # if there's enough space above us:
  324.             elif target_height < Window.height - self.control_size[1] - self.border_margin:
  325.                 ver_growth = 'up'
  326.             # otherwise, let's pick the one with more space and adjust ourselves
  327.             else:
  328.                 # if there's more space below us:
  329.                 if self.control_size[1] >= Window.height - self.control_size[1]:
  330.                     ver_growth = 'down'
  331.                     target_height = self.control_size[1] - self.border_margin
  332.                 # if there's more space above us:
  333.                 else:
  334.                     ver_growth = 'up'
  335.                     target_height = Window.height - self.control_size[1] - self.border_margin
  336.  
  337.         if self.hor_growth is not None:
  338.             hor_growth = self.hor_growth
  339.         else:
  340.             # If there's enough space to the right:
  341.             if target_width <= Window.width - self.control_size[0] - self.border_margin:
  342.                 hor_growth = 'right'
  343.             # if there's enough space to the left:
  344.             elif target_width < self.control_size[0] - self.border_margin:
  345.                 hor_growth = 'left'
  346.             # otherwise, let's pick the one with more space and adjust ourselves
  347.             else:
  348.                 # if there's more space to the right:
  349.                 if Window.width - self.control_size[0] >= self.control_size[0]:
  350.                     hor_growth = 'right'
  351.                     target_width = Window.width - self.control_size[0] - self.border_margin
  352.                 # if there's more space to the left:
  353.                 else:
  354.                     hor_growth = 'left'
  355.                     target_width = self.control_size[0] - self.border_margin
  356.  
  357.         if ver_growth == 'down':
  358.             tar_y = self.control_size[1] - target_height
  359.         else:  # should always be 'up'
  360.             tar_y = self.control_size[1]
  361.  
  362.         if hor_growth == 'right':
  363.             tar_x = self.control_size[0]
  364.         else:  # should always be 'left'
  365.             tar_x = self.control_size[0] - target_width
  366.         anim = Animation(x=tar_x, y=tar_y,
  367.                          width=target_width, height=target_height,
  368.                          duration=.3, transition='out_quint')
  369.         menu = self.ids['md_menu']
  370.         menu.pos = self.control_size
  371.         anim.start(menu)
  372.  
  373.  
  374. class FutsalApp(App):
  375.     theme_color = ListProperty([])
  376.     theme_cls = ThemeManager()
  377.     store = ObjectProperty()
  378.     nav_drawer = ObjectProperty()
  379.  
  380.     def build(self):
  381.         self.title = 'Futsal World'
  382.         # self.theme_color = [0, 0, 0, 1]
  383.         self.store = JsonStore('details.json')
  384.         self.root = FutsalRoot()
  385.         self.nav_drawer = FutsalNavDrawer()
  386.         self.theme_cls.primary_palette = theme_color
  387.         return self.root
  388.  
  389.     # Window.clearcolor = (1, 1, 1, 1)
  390.     # return ScreenManagement()
  391.  
  392.     def on_pause(self):
  393.         # Here you can save data if needed
  394.         return True
  395.  
  396.     def on_resume(self):
  397.         # Here you can check if any data needs replacing (usually nothing)
  398.         pass
  399.  
  400.         # print self.ids
  401.  
  402.     def show_snackbar(self, type, text, change_to_home=False):
  403.         if type == 'simple':
  404.             a = Snackbars()
  405.             a.make(text, change_to_home=change_to_home)
  406.             if change_to_home:
  407.                 a.bind(finished=self.change_homescreen)
  408.  
  409.         elif type == 'button':
  410.             Snackbar.make(text, button_text="with a button!",
  411.                           button_callback=lambda *args: 2)
  412.         elif type == 'verylong':
  413.             Snackbar.make(text,
  414.                           button_text="Hello world")
  415.  
  416.     def change_homescreen(self, instance, arg):
  417.         if arg:
  418.             self.root.ids['sm'].change_screen('HomeScreen')
  419.  
  420. if __name__ == '__main__':
  421.     app = FutsalApp()
  422.     app.run()
Add Comment
Please, Sign In to add comment