Commit cd3c4e27 authored by Suyash Garg's avatar Suyash Garg Committed by Marinus Schraal

songsview: Cleanup PEP-8 & PEP-257

Adhere to PEP-8 and PEP-257:
 * stop using CamelCase for variables and functions
 * indentation fixes
 * mark variables private
 * use treemodel shorthands
 * use docstrings
 * use of some utils functions
 * other cleanups

https://bugzilla.gnome.org/show_bug.cgi?id=773769
parent 4c7f565f
......@@ -26,7 +26,6 @@ from gettext import gettext as _
from gi.repository import Gd, GLib, Gtk, Pango
from gnomemusic import log
from gnomemusic.albumartcache import ArtSize
from gnomemusic.grilo import grilo
from gnomemusic.player import DiscoveryStatus
from gnomemusic.views.baseview import BaseView
......@@ -34,28 +33,41 @@ import gnomemusic.utils as utils
class SongsView(BaseView):
"""Main view of all songs sorted artistwise
Consists all songs along with songname, star, length, artist
and the album name.
"""
def __repr__(self):
return '<SongsView>'
@log
def __init__(self, window, player):
BaseView.__init__(self, 'songs', _("Songs"), window, Gd.MainViewType.LIST)
self._items = {}
self.isStarred = None
self.iter_to_clean = None
self.view.get_generic_view().get_style_context()\
.add_class('songs-list')
self._iconHeight = 32
self._iconWidth = 32
"""Initialize
:param GtkWidget window: The main window
:param player: The main player object
"""
BaseView.__init__(self, 'songs', _("Songs"),
window, Gd.MainViewType.LIST)
self._offset = 0
self._iter_to_clean = None
view_style = self.view.get_generic_view().get_style_context()
view_style.add_class('songs-list')
view_style.remove_class('content-view')
self._add_list_renderers()
self.view.get_generic_view().get_style_context().remove_class('content-view')
self.player = player
self.player.connect('playlist-item-changed', self.update_model)
@log
def _on_changes_pending(self, data=None):
if (self._init and self.header_bar._selectionMode is False):
if (self._init
and not self.header_bar._selectionMode):
self.model.clear()
self._offset = 0
GLib.idle_add(self.populate)
......@@ -63,7 +75,8 @@ class SongsView(BaseView):
@log
def _on_selection_mode_changed(self, widget, data=None):
if self.header_bar._selectionMode is False and grilo.changes_pending['Songs'] is True:
if (not self.header_bar._selectionMode
and grilo.changes_pending['Songs']):
self._on_changes_pending()
@log
......@@ -73,46 +86,58 @@ class SongsView(BaseView):
return
try:
_iter = self.model.get_iter(path)
except TypeError:
itr = self.model.get_iter(path)
except ValueError as err :
logger.warn("Error: {}, {}".format(err.__class__, err))
return
if self.model.get_value(_iter, 8) != self.errorIconName:
self.player.set_playlist('Songs', None, self.model, _iter, 5, 11)
self.player.set_playing(True)
if self.model[itr][8] != self.errorIconName:
self.player.set_playlist('Songs', None, self.model, itr, 5, 11)
self.player.set_playing(True)
@log
def update_model(self, player, playlist, currentIter):
if self.iter_to_clean:
self.model.set_value(self.iter_to_clean, 10, False)
def update_model(self, player, playlist, current_iter):
"""Updates model when the track changes
:param player: The main player object
:param playlist: The current playlist object
:param current_iter: Iter of the current displayed tracks
"""
if self._iter_to_clean:
self.model[self._iter_to_clean][10] = False
if playlist != self.model:
return False
self.model.set_value(currentIter, 10, True)
path = self.model.get_path(currentIter)
self.model[current_iter][10] = True
path = self.model.get_path(current_iter)
self.view.get_generic_view().scroll_to_path(path)
if self.model.get_value(currentIter, 8) != self.errorIconName:
self.iter_to_clean = currentIter.copy()
if self.model[current_iter][8] != self.errorIconName:
self._iter_to_clean = current_iter.copy()
return False
def _add_item(self, source, param, item, remaining=0, data=None):
"""Adds track item to the model"""
self.window.notification.set_timeout(0)
if not item:
if remaining == 0:
self.view.set_model(self.model)
self.window.notification.dismiss()
self.view.show()
if not item and not remaining:
self.view.set_model(self.model)
self.window.notification.dismiss()
self.view.show()
return
self._offset += 1
item.set_title(utils.get_media_title(item))
artist = utils.get_artist_name(item)
if item.get_url() is None:
if not item.get_url():
return
self.model.insert_with_valuesv(
-1,
[2, 3, 5, 9],
[utils.get_media_title(item),
artist, item, bool(item.get_lyrics())])
self.model.insert_with_valuesv(-1, [2, 3, 5, 9], [
utils.get_media_title(item),
artist,
item,
bool(item.get_lyrics())
])
# TODO: change "bool(item.get_lyrics())" --> item.get_favourite() once query works properly
@log
......@@ -130,41 +155,26 @@ class SongsView(BaseView):
column_now_playing.set_fixed_width(48)
column_now_playing.pack_start(now_playing_symbol_renderer, False)
column_now_playing.set_cell_data_func(now_playing_symbol_renderer,
self._on_list_widget_icon_render, None)
self._on_list_widget_icon_render,
None)
list_widget.insert_column(column_now_playing, 0)
title_renderer = Gtk.CellRendererText(
xpad=0,
xalign=0.0,
yalign=0.5,
height=48,
width=300,
ellipsize=Pango.EllipsizeMode.END
)
xpad=0, xalign=0.0, yalign=0.5, height=48, width=300,
ellipsize=Pango.EllipsizeMode.END)
list_widget.add_renderer(title_renderer,
self._on_list_widget_title_render, None)
cols[0].add_attribute(title_renderer, 'text', 2)
self.star_handler.add_star_renderers(list_widget, cols)
duration_renderer = Gd.StyledTextRenderer(
xpad=32,
xalign=1.0
)
duration_renderer = Gd.StyledTextRenderer(xpad=32, xalign=1.0)
duration_renderer.add_class('dim-label')
col = Gtk.TreeViewColumn()
col.pack_start(duration_renderer, False)
col.set_cell_data_func(duration_renderer,
self._on_list_widget_duration_render, None)
list_widget.append_column(col)
artist_renderer = Gd.StyledTextRenderer(
xpad=32,
width=300,
ellipsize=Pango.EllipsizeMode.END
)
xpad=32, width=300, ellipsize=Pango.EllipsizeMode.END)
artist_renderer.add_class('dim-label')
col = Gtk.TreeViewColumn()
......@@ -176,44 +186,40 @@ class SongsView(BaseView):
list_widget.append_column(col)
type_renderer = Gd.StyledTextRenderer(
xpad=32,
width=300,
ellipsize=Pango.EllipsizeMode.END
)
xpad=32, width=300, ellipsize=Pango.EllipsizeMode.END)
type_renderer.add_class('dim-label')
col.pack_end(type_renderer, True)
col.set_cell_data_func(type_renderer,
self._on_list_widget_type_render, None)
col.set_cell_data_func(type_renderer, self._on_list_widget_type_render,
None)
def _on_list_widget_title_render(self, col, cell, model, _iter, data):
def _on_list_widget_title_render(self, col, cell, model, itr, data):
pass
def _on_list_widget_duration_render(self, col, cell, model, _iter, data):
item = model.get_value(_iter, 5)
def _on_list_widget_duration_render(self, col, cell, model, itr, data):
item = model[itr][5]
if item:
seconds = item.get_duration()
minutes = seconds // 60
seconds %= 60
cell.set_property('text', '%i:%02i' % (minutes, seconds))
track_time = utils.seconds_to_string(seconds)
cell.set_property('text', '{}'.format(track_time))
def _on_list_widget_artist_render(self, col, cell, model, _iter, data):
def _on_list_widget_artist_render(self, col, cell, model, itr, data):
pass
def _on_list_widget_type_render(self, coll, cell, model, _iter, data):
item = model.get_value(_iter, 5)
def _on_list_widget_type_render(self, coll, cell, model, itr, data):
item = model[itr][5]
if item:
cell.set_property('text', utils.get_album_title(item))
def _on_list_widget_icon_render(self, col, cell, model, _iter, data):
if not self.player.currentTrackUri:
def _on_list_widget_icon_render(self, col, cell, model, itr, data):
track_uri = self.player.currentTrackUri
if not track_uri:
cell.set_visible(False)
return
if model.get_value(_iter, 11) == DiscoveryStatus.FAILED:
if model[itr][11] == DiscoveryStatus.FAILED:
cell.set_property('icon-name', self.errorIconName)
cell.set_visible(True)
elif model.get_value(_iter, 5).get_url() == self.player.currentTrackUri:
elif model[itr][5].get_url() == track_uri:
cell.set_property('icon-name', self.nowPlayingIconName)
cell.set_visible(True)
else:
......@@ -221,12 +227,19 @@ class SongsView(BaseView):
@log
def populate(self):
"""Populates the view"""
self._init = True
self.window._init_loading_notification()
grilo.populate_songs(self._offset, self._add_item)
if grilo.tracker:
self.window._init_loading_notification()
GLib.idle_add(grilo.populate_songs, self._offset, self._add_item)
@log
def get_selected_tracks(self, callback):
callback([self.model.get_value(self.model.get_iter(path), 5)
for path in self.view.get_selection()])
"""Returns a list of selected tracks
In this view this will be the all the songs selected
:returns: All selected songs
:rtype: A list of tracks
"""
callback([self.model[self.model.get_iter(path)][5]
for path in self.view.get_selection()])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment