Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __version__ = '1.0.0'
- # kivy imports
- from kivy.app import App
- from kivy.metrics import dp
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.anchorlayout import AnchorLayout
- from kivy.uix.relativelayout import RelativeLayout
- from kivy.uix.gridlayout import GridLayout
- from kivy.uix.widget import Widget
- from kivy.uix.scrollview import ScrollView
- from kivy.storage.jsonstore import JsonStore
- from kivy.factory import Factory
- from kivy.properties import ObjectProperty, ListProperty, BooleanProperty, StringProperty, NumericProperty, VariableListProperty
- from kivy.animation import Animation
- from kivy.graphics.vertex_instructions import Line
- from kivy.graphics import Color, Rectangle
- from functools import partial
- from collections import deque
- # Material Design
- # from kivymd.bottomsheet import MDListBottomSheet, MDGridBottomSheet
- # from kivymd.button import MDIconButton, MDFlatButton
- # from kivymd.label import MDLabel
- # from kivymd.list import ILeftBody, ILeftBodyTouch, IRightBodyTouch, MDList, OneLineListItem
- # from kivymd.selectioncontrols import MDCheckbox
- from kivymd.theming import ThemeManager
- import kivymd.snackbar as Snackbar
- from kivymd.navigationdrawer import NavigationDrawerIconButton
- from kivymd.navigationdrawer import NavigationDrawer
- from kivymd.menu import MDDropdownMenu
- from custom_toolbar import Toolbar
- from kivymd.button import MDFlatButton
- from kivymd.dialog import MDDialog
- from kivymd.label import MDLabel
- import kivymd.material_resources as m_res
- from Screens import *
- from license import license
- __author__ = 'Andre'
- # Global Vars
- default_language = 'English'
- # ['Pink', 'Blue', 'Indigo', 'BlueGrey', 'Brown', 'LightBlue', 'Purple', 'Grey', 'Yellow', 'LightGreen', 'DeepOrange',
- # 'Green', 'Red', 'Teal', 'Orange', 'Cyan', 'Amber', 'DeepPurple', 'Lime']
- theme_color = 'BlueGrey'
- class Snackbars(Widget):
- finished = BooleanProperty(False)
- def __init__(self, **kwargs):
- super(Snackbars, self).__init__(**kwargs)
- self.queue = deque()
- self.playing = False
- def make(self, text, button_text=None, button_callback=None, duration=3, change_to_home=False):
- if button_text is not None and button_callback is not None:
- self.queue.append(Snackbar._SnackbarWidget(text=text,
- button_text=button_text,
- button_callback=button_callback,
- duration=duration))
- else:
- self.queue.append(Snackbar._SnackbarWidget(text=text,
- duration=duration))
- if change_to_home:
- self.queue[0].bind(top=self.get_anim_finished)
- self._play_next()
- def _play_next(self, dying_widget=None):
- # global playing
- if (dying_widget or not self.playing) and len(self.queue) > 0:
- self.playing = True
- self.queue.popleft().begin()
- elif len(self.queue) == 0:
- self.playing = False
- def get_anim_finished(self, instance, value):
- if value == 0:
- self.finished = True
- class FutsalRoot(BoxLayout):
- theme_style = StringProperty() # hack because theme_cls.theme_style isn't a bindable property
- # language = StringProperty()
- def __init__(self, **kwargs):
- super(FutsalRoot, self).__init__(**kwargs)
- Clock.schedule_once(self.my_init)
- def my_init(self, dt):
- if not app.store.exists('theme'):
- app.store.put('theme', style='Light')
- # if not app.store.exists('languages'):
- # app.store.put('languages', lang=default_language)
- self.theme_style = app.theme_cls.theme_style = app.store.get('theme')['style']
- self.bind(theme_style=self.write_store_style)
- # self.language = app.store.get('languages')['lang']
- # self.bind(language=self.write_store_language)
- if self.theme_style == 'Dark':
- app.theme_color = [1, 1, 1, 1]
- else:
- app.theme_color = app.theme_color = [0, 0, 0, 1]
- def change_theme(self):
- if app.theme_cls.theme_style == 'Light':
- app.theme_cls.theme_style = 'Dark'
- app.theme_color = [1, 1, 1, 1]
- # self.theme_color = [1, 1, 1, 1]
- else:
- app.theme_cls.theme_style = 'Light'
- self.theme_color = app.theme_color = [0, 0, 0, 1]
- self.theme_style = app.theme_cls.theme_style
- # def show_dialog(self):
- # self.content = BoxLayout(orientation='vertical')
- # languages = ["English", "Portuguese"]
- # for c in languages:
- # if c == self.language:
- # color = app.theme_cls.primary_color
- # else:
- # color = app.theme_color
- # print color
- # #child_width = content.width
- # a = MDFlatButton(
- # text=c,
- # size_hint=(1, None),
- # height=dp(36),
- # #width=3*dp(48),
- # #text_color=color,
- # background_color=app.theme_cls.bg_light
- # )
- # a.ids['_label'].halign = 'left'
- # a.ids['_label'].text_color = color
- # a.bind(on_release=partial(self.change_language, a.text))
- # a.bind(on_press=self.change_label_color)
- # .setter self.content.add_widget(a)
- #
- # #content.bind(size=content.ml.setter('text_size'))
- # self.dialog = MDDialog(title="Pick a language",
- # content=self.content,
- # size_hint=(.8, None),
- # height=dp(200),
- # auto_dismiss=False)
- #
- # # self.dialog.add_action_button("Dismiss",
- # # action=partial(self.change_language, 'pt')) #self.dialog.dismiss())
- # self.dialog.open()
- # def change_label_color(self, instance):
- # instance.ids['_label'].text_color = app.theme_cls.primary_color
- # print [c for c in self.content.walk(restrict=True) if c != instance and isinstance(c, MDFlatButton)]
- # #c.ids['_label'].text_color.setter(app.theme_color)
- #
- # def change_language(self, arg, instance):
- # self.language = arg
- # print self.language
- # print instance.parent.width
- # print instance.width
- # self.dialog.dismiss()
- def write_store_style(self, instance, arg):
- app.store.put('theme', style=arg)
- # def write_store_language(self, instance, arg):
- # app.store.put('languages', lang=arg)
- # self.theme_color = [0, 0, 0, 1]
- # self.ids['sm'].ids['record_screen_id'].ids['test_record'].ids['graphlayout'].graph.tick_color = app.theme_color
- # for elem in self.graphs:
- # #elem.graph.tick_color = app.theme_color
- # elem.graph.border_color = app.theme_color
- # elem.graph.label_options['color'] = app.theme_color
- # elem.graph._redraw_all()
- # print elem.graph.label_options['color']
- # print len(self.graphs)
- # print self.ids['sm'].ids['register_screen_id'].ids
- # print self.ids['sm'].ids['register_screen_id'].ids['t_registerform'].ids['t_name'].foreground_color
- # print self.ids['sm'].ids['record_screen_id'].ids['test_record'].ids['graphlayout'].graph.tick_color
- class FutsalNavDrawer(NavigationDrawer):
- pass
- class NavDrawerChangeTheme(NavigationDrawerIconButton):
- pass
- class NavDrawerChangeLanguage(NavigationDrawerIconButton):
- pass
- class MainToolBar(Toolbar):
- dots_menu = ObjectProperty()
- tabs_layout = ObjectProperty()
- tab1 = ObjectProperty()
- tab2 = ObjectProperty()
- sm = ObjectProperty()
- def __init__(self, **kwargs):
- super(MainToolBar, self).__init__(**kwargs)
- Clock.schedule_once(self.my_init, -1)
- def my_init(self, dt):
- self.dots_menu = DotsMenu()
- self.id = 'toolbar'
- self.title = 'Futsal World'
- self.left_action_items = [['menu', lambda x: app.nav_drawer.toggle()]]
- self.right_action_items = [['more-vert', lambda x: self.dots_menu.open(self.ids.right_actions)]]
- self.size_hint_y = .2
- # self.tab1.text = 'News'
- # self.tab1.text_color = (1, 1, 1, 1) # white color
- # self.tab1.bind(on_release=self.change_screen)
- # self.tab2.text = 'Livescores'
- # self.tab2.text_color = (1, 1, 1, 1) # white color
- # self.tab2.bind(on_release=self.change_screen)
- #print self.tabs_layout.x
- #self.tabs_layout.canvas.after.get_group('rectangle')[0].size = (self.tab1.width, dp(10))
- #app.nav_drawer.bind(center_x=self.test)
- # with self.tabs_layout.canvas.after:
- # Color(1, 0, 0, 1) # set the colour to red
- # self.tabs_layout.rect = Rectangle(pos=(0, 500),
- # size=(self.tab1.width,
- # dp(4)))
- def change_screen(self, instance):
- print instance.parent
- #self.move_rect(instance)
- if instance.name == 'tab1':
- app.root.ids['sm'].change_screen('News')
- if instance.name == 'tab2':
- if not app.root.ids['sm'].has_screen('Livescores'):
- app.root.ids['sm'].add_widget(LiveScoresScreen())
- app.root.ids['sm'].change_screen('Livescores')
- else:
- app.root.ids['sm'].change_screen('Livescores')
- def move_rect(self, instance):
- animation = Animation(size=(instance.width, dp(4)), duration=.4)
- animation &= Animation(pos=(instance.x, instance.y), duration=.4)
- animation.start(self.tabs_layout.canvas.after.get_group('rectangle')[0])
- class ShowLicense(MDDialog):
- link_label = ObjectProperty()
- def __init__(self, **kwargs):
- super(ShowLicense, self).__init__(**kwargs)
- self.title = "License Information"
- self.link_label.text = license
- #self.content.bind(size=self.link_label.setter('text_size'))
- def custom_open(self, menu):
- menu.dismiss()
- self.open()
- class DotsMenu(MDDropdownMenu):
- def __init__(self, **kwargs):
- super(DotsMenu, self).__init__(**kwargs)
- self.items = [
- {'viewclass': 'MDFlatButton',
- 'text': 'Legal'},
- {'viewclass': 'MDFlatButton',
- 'text': 'Licenses',
- 'on_release': lambda: self.menu_license.custom_open(self)},
- {'viewclass': 'MDFlatButton',
- 'text': 'Settings',
- 'on_release': lambda: app.open_settings()}
- ]
- self.hor_growth = 'left'
- self.ver_growth = 'down'
- self.width_mult = 3
- self.menu_license = ShowLicense()
- self.control_size = (app.root.width - dp(3), app.root.top - dp(3))
- app.root.bind(width=self.adapt_control_width)
- app.root.bind(height=self.adapt_control_height)
- def adapt_control_width(self, x, y):
- self.control_size = (app.root.width - dp(3), self.control_size[1])
- self.ids['md_menu'].right = self.control_size[0]
- def adapt_control_height(self, x, y):
- self.control_size = (self.control_size[0], app.root.top - dp(3))
- self.ids['md_menu'].top = self.control_size[1]
- # Over-ride Open Behaviour
- def open(self, *largs):
- Window.add_widget(self)
- Clock.schedule_once(lambda x: self.display_menu(largs[0]), -1)
- def display_menu(self, caller):
- # We need to pick a starting point, see how big we need to be,
- # and where to grow to.
- # ---ESTABLISH INITIAL TARGET SIZE ESTIMATE---
- target_width = self.width_mult * m_res.STANDARD_INCREMENT
- # If we're wider than the Window...
- if target_width > Window.width:
- # ...reduce our multiplier to max allowed.
- target_width = int(
- Window.width / m_res.STANDARD_INCREMENT) * m_res.STANDARD_INCREMENT
- target_height = sum([dp(48) for i in self.items])
- # If we're over max_height...
- if self.max_height > 0 and target_height > self.max_height:
- target_height = self.max_height
- # ---ESTABLISH VERTICAL GROWTH DIRECTION---
- if self.ver_growth is not None:
- ver_growth = self.ver_growth
- else:
- # If there's enough space below us:
- if target_height <= self.control_size[1] - self.border_margin:
- ver_growth = 'down'
- # if there's enough space above us:
- elif target_height < Window.height - self.control_size[1] - self.border_margin:
- ver_growth = 'up'
- # otherwise, let's pick the one with more space and adjust ourselves
- else:
- # if there's more space below us:
- if self.control_size[1] >= Window.height - self.control_size[1]:
- ver_growth = 'down'
- target_height = self.control_size[1] - self.border_margin
- # if there's more space above us:
- else:
- ver_growth = 'up'
- target_height = Window.height - self.control_size[1] - self.border_margin
- if self.hor_growth is not None:
- hor_growth = self.hor_growth
- else:
- # If there's enough space to the right:
- if target_width <= Window.width - self.control_size[0] - self.border_margin:
- hor_growth = 'right'
- # if there's enough space to the left:
- elif target_width < self.control_size[0] - self.border_margin:
- hor_growth = 'left'
- # otherwise, let's pick the one with more space and adjust ourselves
- else:
- # if there's more space to the right:
- if Window.width - self.control_size[0] >= self.control_size[0]:
- hor_growth = 'right'
- target_width = Window.width - self.control_size[0] - self.border_margin
- # if there's more space to the left:
- else:
- hor_growth = 'left'
- target_width = self.control_size[0] - self.border_margin
- if ver_growth == 'down':
- tar_y = self.control_size[1] - target_height
- else: # should always be 'up'
- tar_y = self.control_size[1]
- if hor_growth == 'right':
- tar_x = self.control_size[0]
- else: # should always be 'left'
- tar_x = self.control_size[0] - target_width
- anim = Animation(x=tar_x, y=tar_y,
- width=target_width, height=target_height,
- duration=.3, transition='out_quint')
- menu = self.ids['md_menu']
- menu.pos = self.control_size
- anim.start(menu)
- class FutsalApp(App):
- theme_color = ListProperty([])
- theme_cls = ThemeManager()
- store = ObjectProperty()
- nav_drawer = ObjectProperty()
- def build(self):
- self.title = 'Futsal World'
- # self.theme_color = [0, 0, 0, 1]
- self.store = JsonStore('details.json')
- self.root = FutsalRoot()
- self.nav_drawer = FutsalNavDrawer()
- self.theme_cls.primary_palette = theme_color
- return self.root
- # Window.clearcolor = (1, 1, 1, 1)
- # return ScreenManagement()
- def on_pause(self):
- # Here you can save data if needed
- return True
- def on_resume(self):
- # Here you can check if any data needs replacing (usually nothing)
- pass
- # print self.ids
- def show_snackbar(self, type, text, change_to_home=False):
- if type == 'simple':
- a = Snackbars()
- a.make(text, change_to_home=change_to_home)
- if change_to_home:
- a.bind(finished=self.change_homescreen)
- elif type == 'button':
- Snackbar.make(text, button_text="with a button!",
- button_callback=lambda *args: 2)
- elif type == 'verylong':
- Snackbar.make(text,
- button_text="Hello world")
- def change_homescreen(self, instance, arg):
- if arg:
- self.root.ids['sm'].change_screen('HomeScreen')
- if __name__ == '__main__':
- app = FutsalApp()
- app.run()
Add Comment
Please, Sign In to add comment