From 3244b6577dde5b76c5164da15e437338222c5478 Mon Sep 17 00:00:00 2001 From: 9and3r <9and3r@gmail.com> Date: Mon, 4 Aug 2014 11:50:12 +0200 Subject: [PATCH] Code cleanup --- docs/conf.py | 9 +- mopidy_touchscreen/list_view.py | 3 +- mopidy_touchscreen/main_screen.py | 25 ++-- mopidy_touchscreen/screen_manager.py | 18 +-- mopidy_touchscreen/touch_text_manager.py | 153 ----------------------- mopidy_touchscreen/tracklist.py | 6 +- 6 files changed, 35 insertions(+), 179 deletions(-) delete mode 100644 mopidy_touchscreen/touch_text_manager.py diff --git a/docs/conf.py b/docs/conf.py index 0aa3075..7f9c92c 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -37,7 +37,7 @@ templates_path = ['_templates'] source_suffix = '.rst' # The encoding of source files. -#source_encoding = 'utf-8-sig' +# source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' @@ -196,7 +196,8 @@ latex_elements = {} # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - ('index', 'Mopidy-Touchscreen.tex', u'Mopidy-Touchscreen Documentation', + ('index', 'Mopidy-Touchscreen.tex', + u'Mopidy-Touchscreen Documentation', u'9and3r', 'manual'), ] @@ -241,8 +242,8 @@ man_pages = [ # dir menu entry, description, category) texinfo_documents = [ ('index', 'Mopidy-Touchscreen', u'Mopidy-Touchscreen Documentation', - u'9and3r', 'Mopidy-Touchscreen', 'One line description of project.', - 'Miscellaneous'), + u'9and3r', 'Mopidy-Touchscreen', 'One line description of project.', + 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. diff --git a/mopidy_touchscreen/list_view.py b/mopidy_touchscreen/list_view.py index 770d907..78751f3 100644 --- a/mopidy_touchscreen/list_view.py +++ b/mopidy_touchscreen/list_view.py @@ -1,7 +1,8 @@ import logging -from .screen_objects import ScrollBar, ScreenObjectsManager, TouchAndTextItem +from .screen_objects import ScreenObjectsManager, TouchAndTextItem, ScrollBar from .touch_manager import TouchManager + logger = logging.getLogger(__name__) diff --git a/mopidy_touchscreen/main_screen.py b/mopidy_touchscreen/main_screen.py index e62fe4a..df880fa 100644 --- a/mopidy_touchscreen/main_screen.py +++ b/mopidy_touchscreen/main_screen.py @@ -7,8 +7,10 @@ import urllib2 import json import pygame import logging + from .touch_manager import TouchManager -from .screen_objects import ScreenObjectsManager, Progressbar, TouchAndTextItem, TextItem +from .screen_objects import ScreenObjectsManager, Progressbar, \ + TouchAndTextItem, TextItem logger = logging.getLogger(__name__) @@ -37,7 +39,8 @@ class MainScreen(): if self.image is not None: screen.blit(self.image, ( self.base_size / 2, self.base_size + self.base_size / 2)) - self.touch_text_manager.get_touch_object("time_progress").set_value( + self.touch_text_manager.get_touch_object( + "time_progress").set_value( self.core.playback.time_position.get() / 1000) self.touch_text_manager.get_touch_object("time_progress").set_text( time.strftime('%M:%S', time.gmtime( @@ -75,7 +78,7 @@ class MainScreen(): (x, self.base_size * 4), (width, self.size[1])) self.touch_text_manager.set_object("artist_name", label) - #Previous track button + # Previous track button button = TouchAndTextItem(self.fonts['icon'], u"\ue61c", (0, self.base_size * 6), None) self.touch_text_manager.set_touch_object("previous", button) @@ -93,7 +96,8 @@ class MainScreen(): 0)) + "/" + time.strftime('%M:%S', time.gmtime(0)), (size_1, self.base_size * 6), - (self.size[0] - size_1 - size_2, self.base_size), + ( + self.size[0] - size_1 - size_2, self.base_size), track.length / 1000, False) self.touch_text_manager.set_touch_object("time_progress", progress) @@ -133,13 +137,14 @@ class MainScreen(): def download_image(self, artist_index): if artist_index < len(self.artists): try: - safe_artist = urllib.quote_plus(self.artists[artist_index].name) + safe_artist = urllib.quote_plus( + self.artists[artist_index].name) safe_album = urllib.quote_plus( MainScreen.get_track_album_name(self.track)) url = "http://ws.audioscrobbler.com/2.0/?" params = "method=album.getinfo&" + \ - "api_key=59a04c6a73fb99d6e8996e01db306829&artist=" \ - + safe_artist + "&album=" + safe_album + "&format=json" + "api_key=59a04c6a73fb99d6e8996e01db306829&artist=" \ + + safe_artist + "&album=" + safe_album + "&format=json" response = urllib2.urlopen(url + params) data = json.load(response) image = data['album']['image'][-1]['#text'] @@ -225,13 +230,15 @@ class MainScreen(): volume = self.core.playback.volume.get() + 10 if volume > 100: volume = 100 - self.manager.backend.tell({'action': 'volume', 'value': volume}) + self.manager.backend.tell( + {'action': 'volume', 'value': volume}) self.manager.volume_changed(volume) elif event.direction == TouchManager.down: volume = self.core.playback.volume.get() - 10 if volume < 0: volume = 0 - self.manager.backend.tell({'action': 'volume', 'value': volume}) + self.manager.backend.tell( + {'action': 'volume', 'value': volume}) self.manager.volume_changed(volume) @staticmethod diff --git a/mopidy_touchscreen/screen_manager.py b/mopidy_touchscreen/screen_manager.py index 1bb92fe..611feeb 100644 --- a/mopidy_touchscreen/screen_manager.py +++ b/mopidy_touchscreen/screen_manager.py @@ -61,50 +61,50 @@ class ScreenManager(): self.screen_objects_manager.set_touch_object("repeat", button) x = button.get_right_pos() - #Repeat + # Single button = TouchAndTextItem(self.fonts['base'], " 1 ", (x, 0), None) self.screen_objects_manager.set_touch_object("single", button) x = button.get_right_pos() - #Internet + # Internet button = TouchAndTextItem(self.fonts['icon'], u"\ue602 ", (x, 0), None) self.screen_objects_manager.set_touch_object("internet", button) x = button.get_right_pos() - #Mute + # Mute button = TouchAndTextItem(self.fonts['icon'], u"\ue61f ", (x, 0), None) self.screen_objects_manager.set_touch_object("mute", button) x = button.get_right_pos() - #Volume + # Volume progress = Progressbar(self.fonts['base'], "100", (x, 0), (self.size[0] - x, self.base_size), 100, True) self.screen_objects_manager.set_touch_object("volume", progress) progress.set_value(self.core.playback.volume.get()) - #Menu buttons + # Menu buttons button_size = (self.size[0] / 5, self.base_size) - #Main button + # Main button button = TouchAndTextItem(self.fonts['icon'], u" \ue600", (0, self.base_size * 7), button_size) self.screen_objects_manager.set_touch_object("menu_0", button) x = button.get_right_pos() - #Tracklist button + # Tracklist button button = TouchAndTextItem(self.fonts['icon'], u" \ue60d", (x, self.base_size * 7), button_size) self.screen_objects_manager.set_touch_object("menu_1", button) x = button.get_right_pos() - #Library button + # Library button button = TouchAndTextItem(self.fonts['icon'], u" \ue604", (x, self.base_size * 7), button_size) self.screen_objects_manager.set_touch_object("menu_2", button) x = button.get_right_pos() - #Playlist button + # Playlist button button = TouchAndTextItem(self.fonts['icon'], u" \ue605", (x, self.base_size * 7), button_size) self.screen_objects_manager.set_touch_object("menu_3", button) diff --git a/mopidy_touchscreen/touch_text_manager.py b/mopidy_touchscreen/touch_text_manager.py deleted file mode 100644 index 8b3aa18..0000000 --- a/mopidy_touchscreen/touch_text_manager.py +++ /dev/null @@ -1,153 +0,0 @@ -import pygame -import logging - -logger = logging.getLogger(__name__) - - -class TouchTextManager(): - def __init__(self, size, base_size): - self.size = size - self.base_size = base_size - self.touch_objects = {} - self.text_objects = {} - - def add_object(self, key, text, pos, pos2, color): - self.text_objects[key] = TextItem(text, pos, pos2, color, - self.base_size) - - def get_object(self, key): - return self.text_objects[key] - - def add_touch_object(self, key, text, pos, color): - self.touch_objects['key'] = TouchAndTextItem(text, pos, color, - self.base_size) - - def get_touch_object(self, key): - return self.touch_objects['key'] - - def add_progressbar(self, key, pos, pos2, max): - self.touch_objects['key'] = Progressbar(pos, pos2, max) - - def render(self, surface): - for key in self.text_objects: - self.text_objects[key].update() - self.text_objects[key].render(surface) - for key in self.touch_objects: - self.touch_objects[key].render(surface) - - -class BaseItem(): - def __init__(self, pos, pos2): - self.pos = pos - self.pos2 = pos2 - self.size = [0, 0] - self.size[0] = self.pos2[0] - self.pos[0] - self.size[1] = self.pos2[1] - self.pos[1] - self.rect = pygame.Rect(0, 0, self.size[0], self.size[1]) - - -class TextItem(BaseItem): - def __init__(self, text, pos, pos2, color, text_size): - if pos2 is not None: - BaseItem.__init__(self, pos, pos2) - self.text_size = text_size - self.font = pygame.font.SysFont("arial", text_size) - self.text = text - self.color = color - self.box = self.font.render(text, True, self.color) - if pos2 is not None: - if self.pos[0] + self.box.get_rect().width > pos2[0]: - self.fit_horizontal = False - self.text = self.text + " " - self.original_text = self.text - self.step = 0 - else: - self.fit_horizontal = True - if self.pos[1] + self.box.get_rect().height > pos2[1]: - self.fit_vertical = False - else: - self.fit_vertical = True - else: - BaseItem.__init__(self, pos, (pos[0] + self.box.get_rect().width, - pos[1] + self.box.get_rect().height)) - self.fit_horizontal = True - self.fit_vertical = True - - - def update(self): - if not self.fit_horizontal: - if self.text == self.original_text: - if self.step > 90: - self.step = 0 - new_text = self.text[1:] - new_text = new_text + self.text[:1] - self.text = new_text - else: - self.step = self.step + 1 - elif self.step > 5: - self.step = 0 - new_text = self.text[1:] - new_text = new_text + self.text[:1] - self.text = new_text - else: - self.step = self.step + 1 - - def render(self, surface): - if self.fit_horizontal: - self.box - else: - self.box = self.font.render(self.text, True, self.color) - surface.blit(self.box, self.pos, area=self.rect) - - def set_text(self, text): - self.__init__(text, self.pos, self.pos2, self.color, self.text_size) - - -class TouchObject(BaseItem): - def __init__(self, pos, pos2): - BaseItem.__init__(self, pos, pos2) - self.active = False - self.background_box = pygame.Surface(self.size) - self.background_box.fill((0, 128, 255)) - - def render(self, surface): - surface.blit(self.background_box, self.pos) - - -class TouchAndTextItem(TouchObject, TextItem): - def __init__(self, text, pos, color, text_size): - TextItem.__init__(self, text, pos, None, color, text_size) - TouchObject.__init__(self, pos, self.pos2) - - def update(self): - TextItem.update() - - def render(self, surface): - TouchObject.render(self, surface) - TextItem.render(self, surface) - - -class Progressbar(BaseItem): - def __init__(self, pos, pos2, max): - BaseItem.__init__(self, pos, pos2) - logger.error(pos2) - self.value = 0 - self.max = max - self.back_color = (0, 0, 0) - self.main_color = (255, 255, 255) - self.surface = pygame.Surface(self.size) - self.surface.fill(self.back_color) - - def update(self): - pass - - def render(self, surface): - surface.blit(self.surface, self.pos) - - def set_value(self, value): - self.value = value - self.surface.fill(self.back_color) - pos_pixel = value * self.size[0] / self.max - rect = pygame.Rect(0, 0, pos_pixel, self.size[1]) - self.surface.fill(self.main_color, rect) - diff --git a/mopidy_touchscreen/tracklist.py b/mopidy_touchscreen/tracklist.py index 126fdc9..71e63a0 100644 --- a/mopidy_touchscreen/tracklist.py +++ b/mopidy_touchscreen/tracklist.py @@ -8,8 +8,8 @@ class Tracklist(): self.base_size = base_size self.manager = manager self.list_view = ListView((0, self.base_size), ( - self.size[0], self.size[1] - 2 * self.base_size), - self.base_size, manager.fonts['base']) + self.size[0], self.size[1] - 2 * self.base_size), + self.base_size, manager.fonts['base']) self.tracks = [] self.tracks_strings = [] self.update_list() @@ -36,4 +36,4 @@ class Tracklist(): def track_started(self, track): self.list_view.set_selected( - [self.manager.core.tracklist.index(track).get()]) \ No newline at end of file + [self.manager.core.tracklist.index(track).get()])