From 2cc18a8fb448f62adc1be25270141bff00475b33 Mon Sep 17 00:00:00 2001 From: Martin Zibricky Date: Fri, 12 Oct 2012 13:55:06 +0200 Subject: [PATCH] Bug #687638: use locale aware comparison for songs by default. --- openlp/core/utils/__init__.py | 4 ++-- openlp/plugins/songs/forms/songexportform.py | 3 +-- openlp/plugins/songs/lib/db.py | 10 +++++++++- openlp/plugins/songs/lib/mediaitem.py | 7 ++----- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index 957fde829..764cab06a 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -490,11 +490,11 @@ def format_time(text, local_time): def locale_compare(string1, string2): """ - Compares two strings according to the current QLocale settings. + Compares two strings according to the current locale settings. As any other compare function, returns a negative, or a positive value, or 0, depending on whether string1 collates before or after string2 or - is equal to it. + is equal to it. Comparison is case insensitive. """ # Function locale.strcol() from standard Python library does not work # properly on Windows and probably somewhere else. diff --git a/openlp/plugins/songs/forms/songexportform.py b/openlp/plugins/songs/forms/songexportform.py index c483c91b6..15239f9c2 100644 --- a/openlp/plugins/songs/forms/songexportform.py +++ b/openlp/plugins/songs/forms/songexportform.py @@ -29,7 +29,6 @@ The :mod:`songexportform` module provides the wizard for exporting songs to the OpenLyrics format. """ -import locale import logging from PyQt4 import QtCore, QtGui @@ -252,7 +251,7 @@ class SongExportForm(OpenLPWizard): # Load the list of songs. Receiver.send_message(u'cursor_busy') songs = self.plugin.manager.get_all_objects(Song) - songs.sort(cmp=locale.strcoll, key=lambda song: song.title.lower()) + songs.sort() for song in songs: # No need to export temporary songs. if song.temporary: diff --git a/openlp/plugins/songs/lib/db.py b/openlp/plugins/songs/lib/db.py index d79d177fd..0d404a235 100644 --- a/openlp/plugins/songs/lib/db.py +++ b/openlp/plugins/songs/lib/db.py @@ -35,6 +35,7 @@ from sqlalchemy.orm import mapper, relation from sqlalchemy.sql.expression import func from openlp.core.lib.db import BaseModel, init_db +from openlp.core.utils import locale_compare class Author(BaseModel): """ @@ -63,7 +64,14 @@ class Song(BaseModel): """ Song model """ - pass + # By default sort the songs by its title considering language specific + # characters. + def __lt__(self, other): + r = locale_compare(self.title, other.title) + return True if r < 0 else False + + def __eq__(self, other): + return 0 == locale_compare(self.title, other.title) class Topic(BaseModel): diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index e338a11d9..0808bcba0 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -39,7 +39,7 @@ from openlp.core.lib import MediaManagerItem, Receiver, ItemCapabilities, \ check_directory_exists from openlp.core.lib.ui import UiStrings, create_widget_action from openlp.core.lib.settings import Settings -from openlp.core.utils import AppLocation, locale_compare +from openlp.core.utils import AppLocation from openlp.plugins.songs.forms import EditSongForm, SongMaintenanceForm, \ SongImportForm, SongExportForm from openlp.plugins.songs.lib import OpenLyrics, SongXML, VerseType, \ @@ -259,10 +259,7 @@ class SongMediaItem(MediaManagerItem): log.debug(u'display results Song') self.saveAutoSelectId() self.listView.clear() - # Sort the songs by its title considering language specific characters. - # lower() is needed for windows! - searchresults.sort( - cmp=locale_compare, key=lambda song: song.title) + searchresults.sort() for song in searchresults: # Do not display temporary songs if song.temporary: