Fix sorting in song export

This commit is contained in:
Jonathan Corwin 2012-12-08 21:18:45 +00:00
parent 0485e967ad
commit ac440d9185
3 changed files with 41 additions and 45 deletions

View File

@ -34,11 +34,11 @@ import logging
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from openlp.core.lib import build_icon, Receiver, SettingsManager, translate, \ from openlp.core.lib import build_icon, Receiver, translate, \
create_separated_list create_separated_list
from openlp.core.lib.ui import UiStrings, critical_error_message_box from openlp.core.lib.ui import UiStrings, critical_error_message_box
from openlp.core.ui.wizard import OpenLPWizard, WizardStrings from openlp.core.ui.wizard import OpenLPWizard, WizardStrings
from openlp.core.utils import locale_direct_compare from openlp.plugins.songs.lib import natcmp
from openlp.plugins.songs.lib.db import Song from openlp.plugins.songs.lib.db import Song
from openlp.plugins.songs.lib.openlyricsexport import OpenLyricsExport from openlp.plugins.songs.lib.openlyricsexport import OpenLyricsExport
@ -253,8 +253,7 @@ class SongExportForm(OpenLPWizard):
# Load the list of songs. # Load the list of songs.
Receiver.send_message(u'cursor_busy') Receiver.send_message(u'cursor_busy')
songs = self.plugin.manager.get_all_objects(Song) songs = self.plugin.manager.get_all_objects(Song)
songs.sort( songs.sort(cmp=natcmp, key=lambda song: song.sort_key)
cmp=locale_direct_compare, key=lambda song: song.sort_string)
for song in songs: for song in songs:
# No need to export temporary songs. # No need to export temporary songs.
if song.temporary: if song.temporary:

View File

@ -28,10 +28,10 @@
############################################################################### ###############################################################################
import re import re
from PyQt4 import QtGui from PyQt4 import QtGui, QtCore
from openlp.core.lib import translate from openlp.core.lib import translate
from openlp.core.utils import CONTROL_CHARS from openlp.core.utils import CONTROL_CHARS, locale_direct_compare
from db import Author from db import Author
from ui import SongStrings from ui import SongStrings
@ -594,6 +594,40 @@ def strip_rtf(text, default_encoding=None):
text = u''.join(out) text = u''.join(out)
return text, default_encoding return text, default_encoding
def natcmp(a, b):
"""
Natural string comparison which mimics the behaviour of Python's internal
cmp function.
"""
if len(a) <= len(b):
for i, key in enumerate(a):
if isinstance(key, int) and isinstance(b[i], int):
result = cmp(key, b[i])
elif isinstance(key, int) and not isinstance(b[i], int):
result = locale_direct_compare(QtCore.QString(str(key)), b[i])
elif not isinstance(key, int) and isinstance(b[i], int):
result = locale_direct_compare(key, QtCore.QString(str(b[i])))
else:
result = locale_direct_compare(key, b[i])
if result != 0:
return result
if len(a) == len(b):
return 0
else:
return -1
else:
for i, key in enumerate(b):
if isinstance(a[i], int) and isinstance(key, int):
result = cmp(a[i], key)
elif isinstance(a[i], int) and not isinstance(key, int):
result = locale_direct_compare(QtCore.QString(str(a[i])), key)
elif not isinstance(a[i], int) and isinstance(key, int):
result = locale_direct_compare(a[i], QtCore.QString(str(key)))
else:
result = locale_direct_compare(a[i], key)
if result != 0:
return result
return 1
from xml import OpenLyrics, SongXML from xml import OpenLyrics, SongXML
from songstab import SongsTab from songstab import SongsTab

View File

@ -40,54 +40,17 @@ from openlp.core.lib import MediaManagerItem, Receiver, ItemCapabilities, \
check_directory_exists check_directory_exists
from openlp.core.lib.ui import UiStrings, create_widget_action from openlp.core.lib.ui import UiStrings, create_widget_action
from openlp.core.lib.settings import Settings from openlp.core.lib.settings import Settings
from openlp.core.utils import AppLocation, locale_direct_compare from openlp.core.utils import AppLocation
from openlp.plugins.songs.forms import EditSongForm, SongMaintenanceForm, \ from openlp.plugins.songs.forms import EditSongForm, SongMaintenanceForm, \
SongImportForm, SongExportForm SongImportForm, SongExportForm
from openlp.plugins.songs.lib import OpenLyrics, SongXML, VerseType, \ from openlp.plugins.songs.lib import OpenLyrics, SongXML, VerseType, \
clean_string clean_string, natcmp
from openlp.plugins.songs.lib.db import Author, Song, Book, MediaFile from openlp.plugins.songs.lib.db import Author, Song, Book, MediaFile
from openlp.plugins.songs.lib.ui import SongStrings from openlp.plugins.songs.lib.ui import SongStrings
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def natcmp(a, b):
"""
Natural string comparison which mimics the behaviour of Python's internal
cmp function.
"""
log.debug('a: %s; b: %s', a, b)
if len(a) <= len(b):
for i, key in enumerate(a):
if isinstance(key, int) and isinstance(b[i], int):
result = cmp(key, b[i])
elif isinstance(key, int) and not isinstance(b[i], int):
result = locale_direct_compare(QtCore.QString(str(key)), b[i])
elif not isinstance(key, int) and isinstance(b[i], int):
result = locale_direct_compare(key, QtCore.QString(str(b[i])))
else:
result = locale_direct_compare(key, b[i])
if result != 0:
return result
if len(a) == len(b):
return 0
else:
return -1
else:
for i, key in enumerate(b):
if isinstance(a[i], int) and isinstance(key, int):
result = cmp(a[i], key)
elif isinstance(a[i], int) and not isinstance(key, int):
result = locale_direct_compare(QtCore.QString(str(a[i])), key)
elif not isinstance(a[i], int) and isinstance(key, int):
result = locale_direct_compare(a[i], QtCore.QString(str(key)))
else:
result = locale_direct_compare(a[i], key)
if result != 0:
return result
return 1
class SongSearch(object): class SongSearch(object):
""" """
An enumeration for song search methods. An enumeration for song search methods.