Skip to content

Commit

Permalink
songlist columns: ~#rating -> ~rating. Make ~#rating a numeric column…
Browse files Browse the repository at this point in the history
… (Jan Path, me)

also enable config versioning and migrate old values; add some tests
  • Loading branch information
lazka committed Jul 30, 2014
1 parent 88854f2 commit 1735157
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
13 changes: 12 additions & 1 deletion quodlibet/quodlibet/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@


# global instance
_config = Config()
_config = Config(version=0)

options = _config.options
get = _config.get
Expand All @@ -164,6 +164,7 @@
write = _config.write
reset = _config.reset
add_section = _config.add_section
register_upgrade_function = _config.register_upgrade_function


def init(filename=None, initial=None):
Expand Down Expand Up @@ -272,6 +273,16 @@ def set_columns(vals, force=False):
print_d("No change in columns to write")


@register_upgrade_function
def _migrate_rating_column(config, old, new):
if old < 0:
columns = get_columns()[:]
for i, c in enumerate(columns):
if c == "~#rating":
columns[i] = "~rating"
set_columns(columns)


def cached_config():
pass

Expand Down
5 changes: 3 additions & 2 deletions quodlibet/quodlibet/qltk/prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Copyright 2004-2009 Joe Wreschnig, Michael Urman, Iñigo Serna,
# Steven Robertson
# 2011-2013 Nick Boultbee
# 2013 Christoph Reiter
# 2013 Christoph Reiter
# 2014 Jan Path
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
Expand Down Expand Up @@ -50,7 +51,7 @@ class SongList(Gtk.VBox):
("~basename", _("_Filename")),

("~#length", _("_Length")),
("~#rating", _("_Rating")),
("~rating", _("_Rating")),
("~#filesize", util.tag("~#filesize"))]

def __init__(self):
Expand Down
7 changes: 4 additions & 3 deletions quodlibet/quodlibet/qltk/songlist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2005 Joe Wreschnig
# 2012 Christoph Reiter
# 2011-2013 Nick Boultbee
# 2014 Jan Path
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
Expand Down Expand Up @@ -555,7 +556,7 @@ def __button_press(self, view, event, librarian):
return True
if event.window != self.get_bin_window():
return False
if col.header_name == "~#rating":
if col.header_name == "~rating":
if not config.getboolean("browsers", "rating_click"):
return

Expand All @@ -572,7 +573,7 @@ def __button_press(self, view, event, librarian):
rating = max(0.0, min(1.0, count * precision))
if (rating <= precision and
song("~#rating") == precision):
rating = 0
rating = 0.0
self.__set_rating(rating, [song], librarian)

def __set_rating(self, value, songs, librarian):
Expand Down Expand Up @@ -966,7 +967,7 @@ def add_header_toggle(menu, (header, tag), active, column=column):
menu.append(sep)

trackinfo = """title genre ~title~version ~#track
~#playcount ~#skipcount ~#rating ~#length""".split()
~#playcount ~#skipcount ~rating ~#length""".split()
peopleinfo = """artist ~people performer arranger author composer
conductor lyricist originalartist""".split()
albuminfo = """album ~album~discsubtitle labelid ~#disc ~#discs
Expand Down
14 changes: 10 additions & 4 deletions quodlibet/quodlibet/qltk/songlistcolumns.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2005 Joe Wreschnig
# 2012 Christoph Reiter
# 2011-2014 Nick Boultbee
# 2014 Jan Path
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
Expand Down Expand Up @@ -30,7 +31,7 @@ def create_songlist_column(t):
return LengthColumn()
elif t == "~#filesize":
return FilesizeColumn()
elif t in ["~rating", "~#rating"]:
elif t in ["~rating"]:
return RatingColumn()
elif t.startswith("~#"):
return NumericColumn(t)
Expand Down Expand Up @@ -109,13 +110,13 @@ def _cdf(self, column, cell, model, iter_, user_data):


class RatingColumn(TextColumn):
"""Render ~#rating directly
"""Render ~rating directly
(simplifies filtering, saves a function call).
"""

def __init__(self, *args, **kwargs):
super(RatingColumn, self).__init__("~#rating", *args, **kwargs)
super(RatingColumn, self).__init__("~rating", *args, **kwargs)
self.set_expand(False)
self.set_resizable(False)
width = self._cell_width(util.format_rating(1.0))
Expand Down Expand Up @@ -252,7 +253,12 @@ def _cdf(self, column, cell, model, iter_, user_data):
value = model.get_value(iter_).comma(self.header_name)
if not self._needs_update(value):
return
text = unicode(value)

if isinstance(value, float):
text = u"%.2f" % round(value, 2)
else:
text = unicode(value)

cell.set_property('text', text)
self._recalc_width(model.get_path(iter_), text)

Expand Down
1 change: 1 addition & 0 deletions quodlibet/quodlibet/util/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def register_upgrade_function(self, function):
# after read(), so upgrade now
if self._loaded_version is not None:
self._do_upgrade(function)
return function

def set_inital(self, section, option, value):
"""Set an initial value for an option.
Expand Down
13 changes: 11 additions & 2 deletions quodlibet/tests/test_qltk_songlistcolumns.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _render_column(self, column, **kwargs):
view = Gtk.TreeView()
model = ObjectStore()
view.set_model(model)
song = AudioFile({"~filename": "/dev/null"})
song = AudioFile({"~filename": "/dev/null", "~#rating": 0.6666})
song.update(kwargs)
model.append(row=[song])
view.append_column(column)
Expand All @@ -30,6 +30,10 @@ def _render_column(self, column, **kwargs):
with visible(view):
view.columns_autosize()

text = column.get_cells()[0].get_property("text")
self.assertIsNot(text, None)
return text

def test_date(self):
column = create_songlist_column("~#added")
self._render_column(column)
Expand All @@ -48,7 +52,12 @@ def test_filesize(self):

def test_rating(self):
column = create_songlist_column("~rating")
self._render_column(column)
text = self._render_column(column)
self.assertNotEqual(text, "0.67")

column = create_songlist_column("~#rating")
text = self._render_column(column)
self.assertEqual(text, "0.67")

def test_bitrate(self):
column = create_songlist_column("~#bitrate")
Expand Down

0 comments on commit 1735157

Please sign in to comment.