From 4d1dfc89066e8dbccf7e5049686895de6fcb32d2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Fri, 29 Jul 2016 01:04:24 +0200 Subject: [PATCH] Lock widget modifications and reuse labels in actionlist/playlist --- music_sampler/app_blocks/actionlist.py | 68 ++++++++++++------ music_sampler/app_blocks/playlist.py | 97 ++++++++++++++++++-------- music_sampler/music_sampler.kv | 24 ++++++- 3 files changed, 137 insertions(+), 52 deletions(-) diff --git a/music_sampler/app_blocks/actionlist.py b/music_sampler/app_blocks/actionlist.py index 70fe3f4..51f4b82 100644 --- a/music_sampler/app_blocks/actionlist.py +++ b/music_sampler/app_blocks/actionlist.py @@ -2,13 +2,14 @@ from kivy.uix.label import Label from kivy.uix.stacklayout import StackLayout from kivy.uix.relativelayout import RelativeLayout from kivy.properties import ListProperty, StringProperty - -import math +from ..lock import Lock __all__ = ["ActionList", "ActionListIcons", "ActionListIcon", "ActionListDescriptions", "ActionListDescription"] +actionlist_lock = Lock("playlist") + class ActionList(RelativeLayout): action_title = StringProperty("") action_list = ListProperty([]) @@ -25,7 +26,8 @@ class ActionList(RelativeLayout): else: icon = " " action_list.append([icon, action]) - self.action_list = action_list + with actionlist_lock: + self.action_list = action_list class ActionListIcons(StackLayout): def __init__(self, **kwargs): @@ -36,13 +38,19 @@ class ActionListIcons(StackLayout): parent.bind(action_list=self.update_actionlist_icons) def update_actionlist_icons(self, instance, actionlist): - for icon in self.icons: - self.remove_widget(icon) - self.icons = [] - for icon, description in actionlist: - icon_label = ActionListIcon(text=icon) - self.add_widget(icon_label) - self.icons.append(icon_label) + icons_length = len(self.icons) + index = -1 + for index, [icon, description] in enumerate(actionlist): + if index >= icons_length: + icon_label = ActionListIcon(text=icon) + self.add_widget(icon_label) + self.icons.append(icon_label) + else: + self.icons[index].text = icon + + if index+1 < icons_length: + self.clear_widgets(children=self.icons[index+1:icons_length]) + del(self.icons[index+1:icons_length]) class ActionListIcon(Label): def __init__(self, text='', **kwargs): @@ -51,8 +59,14 @@ class ActionListIcon(Label): def on_parent(self, instance, parent): if parent is not None: - self.font_size = math.ceil(2 * math.sqrt(parent.parent.parent.key_size)) - self.height = parent.parent.labels_height + parent.bind(font_size=self.update_font_size) + parent.bind(labels_height=self.update_height) + + def update_height(self, instance, height): + self.height = height + + def update_font_size(self, instance, font_size): + self.font_size = font_size class ActionListDescriptions(StackLayout): def __init__(self, **kwargs): @@ -63,13 +77,20 @@ class ActionListDescriptions(StackLayout): parent.bind(action_list=self.update_actionlist_descriptions) def update_actionlist_descriptions(self, instance, actionlist): - for description in self.descriptions: - self.remove_widget(description) - self.descriptions = [] - for icon, description in actionlist: - description_label = ActionListDescription(text=description) - self.add_widget(description_label) - self.descriptions.append(description_label) + descriptions_length = len(self.descriptions) + index = -1 + for index, [icon, description] in enumerate(actionlist): + if index >= descriptions_length: + description_label = ActionListDescription(text=description) + self.add_widget(description_label) + self.descriptions.append(description_label) + else: + self.descriptions[index].text = description + + if index+1 < descriptions_length: + self.clear_widgets( + children=self.descriptions[index+1:descriptions_length]) + del(self.descriptions[index+1:descriptions_length]) class ActionListDescription(Label): def __init__(self, text='', **kwargs): @@ -78,7 +99,12 @@ class ActionListDescription(Label): def on_parent(self, instance, parent): if parent is not None: - self.font_size = math.ceil(2 * math.sqrt(parent.parent.parent.key_size)) - self.height = parent.parent.labels_height + parent.bind(font_size=self.update_font_size) + parent.bind(labels_height=self.update_height) + + def update_height(self, instance, height): + self.height = height + def update_font_size(self, instance, font_size): + self.font_size = font_size diff --git a/music_sampler/app_blocks/playlist.py b/music_sampler/app_blocks/playlist.py index b704d4d..5894995 100644 --- a/music_sampler/app_blocks/playlist.py +++ b/music_sampler/app_blocks/playlist.py @@ -4,14 +4,15 @@ from kivy.uix.relativelayout import RelativeLayout from kivy.properties import ListProperty from kivy.clock import Clock from ..helpers import duration_to_min_sec - -import math +from ..lock import Lock __all__ = ["PlayList", "PlayListIcons", "PlayListIcon", "PlayListNames", "PlayListName", "PlayListTimes", "PlayListTime"] +playlist_lock = Lock("playlist") + class PlayList(RelativeLayout): playlist = ListProperty([]) @@ -37,7 +38,8 @@ class PlayList(RelativeLayout): playlist.append(["⏸", music_file.name, time_info, False]) else: playlist.append(["⏵", music_file.name, time_info, True]) - self.playlist = playlist + with playlist_lock: + self.playlist = playlist class PlayListIcons(StackLayout): @@ -49,13 +51,19 @@ class PlayListIcons(StackLayout): parent.bind(playlist=self.update_playlist_icons) def update_playlist_icons(self, instance, playlist): - for icon in self.icons: - self.remove_widget(icon) - self.icons = [] - for icon, filename, time_info, playing in playlist: - icon_label = PlayListIcon(text=icon) - self.add_widget(icon_label) - self.icons.append(icon_label) + icons_length = len(self.icons) + index = -1 + for index, [icon, filename, time_info, playing] in enumerate(playlist): + if index >= icons_length: + icon_label = PlayListIcon(text=icon) + self.add_widget(icon_label) + self.icons.append(icon_label) + else: + self.icons[index].text = icon + + if index+1 < icons_length: + self.clear_widgets(children=self.icons[index+1:icons_length]) + del(self.icons[index+1:icons_length]) class PlayListIcon(Label): def __init__(self, text='', **kwargs): @@ -64,9 +72,14 @@ class PlayListIcon(Label): def on_parent(self, instance, parent): if parent is not None: - self.font_size = math.ceil(2 * math.sqrt(parent.parent.parent.key_size)) - self.height = parent.parent.labels_height + parent.bind(font_size=self.update_font_size) + parent.bind(labels_height=self.update_height) + + def update_height(self, instance, height): + self.height = height + def update_font_size(self, instance, font_size): + self.font_size = font_size class PlayListNames(StackLayout): def __init__(self, **kwargs): @@ -77,13 +90,19 @@ class PlayListNames(StackLayout): parent.bind(playlist=self.update_playlist_names) def update_playlist_names(self, instance, playlist): - for name in self.names: - self.remove_widget(name) - self.names = [] - for icon, filename, time_info, playing in playlist: - name_label = PlayListName(text=filename, is_playing=playing) - self.add_widget(name_label) - self.names.append(name_label) + names_length = len(self.names) + index = -1 + for index, [icon, filename, time_info, playing] in enumerate(playlist): + if index >= names_length: + name_label = PlayListName(text=filename, is_playing=playing) + self.add_widget(name_label) + self.names.append(name_label) + else: + self.names[index].text = filename + + if index+1 < names_length: + self.clear_widgets(children=self.names[index+1:names_length]) + del(self.names[index+1:names_length]) class PlayListName(Label): def __init__(self, text='', is_playing=False, **kwargs): @@ -93,8 +112,14 @@ class PlayListName(Label): def on_parent(self, instance, parent): if parent is not None: - self.font_size = math.ceil(2 * math.sqrt(parent.parent.parent.key_size)) - self.height = parent.parent.labels_height + parent.bind(font_size=self.update_font_size) + parent.bind(labels_height=self.update_height) + + def update_height(self, instance, height): + self.height = height + + def update_font_size(self, instance, font_size): + self.font_size = font_size class PlayListTimes(StackLayout): def __init__(self, **kwargs): @@ -105,13 +130,19 @@ class PlayListTimes(StackLayout): parent.bind(playlist=self.update_playlist_times) def update_playlist_times(self, instance, playlist): - for time in self.times: - self.remove_widget(time) - self.times = [] - for icon, filename, time_info, playing in playlist: - time_label = PlayListTime(text=time_info) - self.add_widget(time_label) - self.times.append(time_label) + times_length = len(self.times) + index = -1 + for index, [icon, filename, time_info, playing] in enumerate(playlist): + if index >= times_length: + time_label = PlayListTime(text=time_info) + self.add_widget(time_label) + self.times.append(time_label) + else: + self.times[index].text = time_info + + if index+1 < times_length: + self.clear_widgets(children=self.times[index+1:times_length]) + del(self.times[index+1:times_length]) class PlayListTime(Label): def __init__(self, text='', **kwargs): @@ -120,6 +151,12 @@ class PlayListTime(Label): def on_parent(self, instance, parent): if parent is not None: - self.font_size = math.ceil(2 * math.sqrt(parent.parent.parent.key_size)) - self.height = parent.parent.labels_height + parent.bind(font_size=self.update_font_size) + parent.bind(labels_height=self.update_height) + + def update_height(self, instance, height): + self.height = height + + def update_font_size(self, instance, font_size): + self.font_size = font_size diff --git a/music_sampler/music_sampler.kv b/music_sampler/music_sampler.kv index 4a91896..ffc2797 100644 --- a/music_sampler/music_sampler.kv +++ b/music_sampler/music_sampler.kv @@ -204,6 +204,7 @@ : size_hint: None, None labels_height: self.parent.max_height or 1 + font_size: math.ceil(2 * math.sqrt(self.parent.key_size or 10)) canvas: Color: rgba: 250./255, 250./255, 250./255, 1 @@ -214,7 +215,7 @@ id: action_list_title font_name: "Ubuntu" bold: True - font_size: math.ceil(2 * math.sqrt(self.parent.parent.key_size or 10)) + font_size: self.parent.font_size color: 0, 0, 0, 1 text: self.parent.action_title text_size: None, self.parent.height @@ -223,15 +224,21 @@ size_hint: None, None size: self.texture_size[0], self.parent.height ActionListIcons: + font_size: self.parent.font_size + labels_height: self.parent.labels_height orientation: 'lr-tb' size_hint: 0.02, 0.9 pos_hint: { 'x': 0, 'top': 0.9 } ActionListDescriptions: + font_size: self.parent.font_size + labels_height: self.parent.labels_height orientation: 'lr-tb' size_hint: 0.98, 0.9 pos_hint: { 'x': 0.02, 'top': 0.9 } : + font_size: self.parent and self.parent.font_size or 10 + height: self.parent and self.parent.labels_height or 0 font_name: "Symbola" color: 0, 0, 0, 1 text_size: None, None @@ -240,6 +247,8 @@ width: self.texture_size[0] : + font_size: self.parent and self.parent.font_size or 10 + height: self.parent and self.parent.labels_height or 0 font_name: "Ubuntu" color: 0, 0, 0, 1 text_size: self.width, None @@ -252,6 +261,7 @@ : size_hint: None, None labels_height: self.parent.max_height or 1 + font_size: math.ceil(2 * math.sqrt(self.parent.key_size or 10)) canvas: Color: rgba: 250./255, 250./255, 250./255, 1 @@ -259,19 +269,27 @@ pos: 0, 0 size: self.width, self.height PlayListIcons: + font_size: self.parent.font_size + labels_height: self.parent.labels_height orientation: 'lr-tb' size_hint: 0.05, 1 pos_hints: { 'x': 0, 'top': 0 } PlayListNames: + font_size: self.parent.font_size + labels_height: self.parent.labels_height orientation: 'lr-tb' pos_hint: { 'x': 0.05, 'bottom': 0 } size_hint: 0.65, 1 PlayListTimes: + font_size: self.parent.font_size + labels_height: self.parent.labels_height orientation: 'lr-tb' pos_hint: { 'x': 0.7, 'bottom': 0 } size_hint: 0.30, 1 : + font_size: self.parent and self.parent.font_size or 10 + height: self.parent and self.parent.labels_height or 0 font_name: "Symbola" color: 0, 0, 0, 1 text_size: None, None @@ -280,6 +298,8 @@ width: self.texture_size[0] : + font_size: self.parent and self.parent.font_size or 10 + height: self.parent and self.parent.labels_height or 0 font_name: "Ubuntu" color: 0, 0, 0, 1 text_size: self.width, None @@ -290,6 +310,8 @@ size_hint: 1, None : + font_size: self.parent and self.parent.font_size or 10 + height: self.parent and self.parent.labels_height or 0 canvas.before: Color: rgba: 250./255, 250./255, 250./255, 1 -- 2.41.0