From dff393b6a4df6ff4e04190487fc49e254d6aac5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20P=C3=B5ldaru?= Date: Thu, 17 Feb 2011 21:46:01 +0200 Subject: [PATCH] just another massive set of changes --- openlp/core/ui/slidecontroller.py | 15 ++-- openlp/plugins/songs/forms/editsongform.py | 87 +++++++++++-------- openlp/plugins/songs/forms/editversedialog.py | 28 +++--- openlp/plugins/songs/forms/editverseform.py | 46 +++++----- openlp/plugins/songs/lib/__init__.py | 86 ++++-------------- openlp/plugins/songs/lib/mediaitem.py | 16 ++-- openlp/plugins/songs/lib/xml.py | 5 +- 7 files changed, 122 insertions(+), 161 deletions(-) diff --git a/openlp/core/ui/slidecontroller.py b/openlp/core/ui/slidecontroller.py index 52626f24f..fda151c19 100644 --- a/openlp/core/ui/slidecontroller.py +++ b/openlp/core/ui/slidecontroller.py @@ -603,14 +603,15 @@ class SlideController(QtGui.QWidget): slideHeight = 0 if self.serviceItem.is_text(): if frame[u'verseTag']: - bits = frame[u'verseTag'].split(u':') - tag = u'%s\n%s' % (bits[0][0], bits[1][0:] ) - tag1 = u'%s%s' % (bits[0][0], bits[1][0:] ) - row = tag + # These tags are already translated. + versetag = frame[u'verseTag'] + versetag = u'%s%s' % (versetag[0].upper(), versetag[1:]) + twolineTag = u'%s\n%s' % (versetag[0], versetag[1:] ) + row = twolineTag if self.isLive: - if tag1 not in self.slideList: - self.slideList[tag1] = framenumber - self.songMenu.menu().addAction(tag1, + if versetag not in self.slideList: + self.slideList[versetag] = framenumber + self.songMenu.menu().addAction(versetag, self.onSongBarHandler) else: row += 1 diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index 7ff129de8..34cab3c74 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -230,11 +230,11 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): if self.song.verse_order: # we translate verse order translated = [] - for verse in self.song.verse_order.split(): - verseindex = VerseType.from_tag(verse[0]) - versetype = VerseType.Translations[verseindex][0] - versetag = verse[1:] - translated.append(u'%s%s' % (versetype, versetag)) + for versetag in self.song.verse_order.split(): + verseindex = VerseType.from_tag(versetag[0]) + versetype = VerseType.TranslatedTags[verseindex] + versenum = versetag[1:] + translated.append(u'%s%s' % (versetype.upper(), versenum)) self.verseOrderEdit.setText(u' '.join(translated)) else: self.verseOrderEdit.setText(u'') @@ -263,9 +263,8 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): for count, verse in enumerate(verseList): self.verseListWidget.setRowCount( self.verseListWidget.rowCount() + 1) - # this takes care of silently migrating from old or any markup - # if we entirely trusted the database, this should - # be unnecessary in the future + # This silently migrates from localized verse type markup. + # If we trusted the database, this would be unnecessary. vtype = verse[0][u'type'] index = None if len(vtype) > 1: @@ -277,7 +276,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): if index is None: index = VerseType.Other verse[0][u'type'] = VerseType.Tags[index] - variant = u'%s:%s' % (verse[0][u'type'], verse[0][u'label']) + variant = u'%s%s' % (verse[0][u'type'], verse[0][u'label']) item = QtGui.QTableWidgetItem(verse[1]) item.setData(QtCore.Qt.UserRole, QtCore.QVariant(variant)) self.verseListWidget.setItem(count, 0, item) @@ -318,10 +317,10 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): rowLabel = [] for row in range(0, self.verseListWidget.rowCount()): item = self.verseListWidget.item(row, 0) - data = unicode(item.data(QtCore.Qt.UserRole).toString()) - bit = data.split(u':') - bit[0] = VerseType.Translations[VerseType.from_tag(bit[0])][0] - rowTag = u'%s%s' % (bit[0], bit[1]) + versetag = unicode(item.data(QtCore.Qt.UserRole).toString()) + versetype = VerseType.TranslatedTags[ + VerseType.from_tag(versetag[0])].upper() + rowTag = u'%s%s' % (versetype, versetag[1:]) rowLabel.append(rowTag) self.verseListWidget.setVerticalHeaderLabels(rowLabel) @@ -442,10 +441,10 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): def onVerseAddButtonClicked(self): self.verse_form.setVerse(u'', True) if self.verse_form.exec_(): - afterText, verse, subVerse = self.verse_form.getVerse() - data = u'%s:%s' % (verse, subVerse) + afterText, versetype, versenum = self.verse_form.getVerse() + versetag = u'%s%s' % (versetype, versenum) item = QtGui.QTableWidgetItem(afterText) - item.setData(QtCore.Qt.UserRole, QtCore.QVariant(data)) + item.setData(QtCore.Qt.UserRole, QtCore.QVariant(versetag)) item.setText(afterText) self.verseListWidget.setRowCount( self.verseListWidget.rowCount() + 1) @@ -462,9 +461,9 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): verseId = unicode(item.data(QtCore.Qt.UserRole).toString()) self.verse_form.setVerse(tempText, True, verseId) if self.verse_form.exec_(): - afterText, verse, subVerse = self.verse_form.getVerse() - data = u'%s:%s' % (verse, subVerse) - item.setData(QtCore.Qt.UserRole, QtCore.QVariant(data)) + afterText, versetype, versenum = self.verse_form.getVerse() + versetag = u'%s%s' % (versetype, versenum) + item.setData(QtCore.Qt.UserRole, QtCore.QVariant(versetag)) item.setText(afterText) # number of lines has change so repaint the list moving the data if len(tempText.split(u'\n')) != len(afterText.split(u'\n')): @@ -489,8 +488,9 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): for row in range(0, self.verseListWidget.rowCount()): item = self.verseListWidget.item(row, 0) field = unicode(item.data(QtCore.Qt.UserRole).toString()) - versetype, versenum = field.split(u':') - versetype = VerseType.to_translated_string(versetype) + versetypeindex = VerseType.from_tag(field[0]) + versetype = VerseType.TranslatedNames[versetypeindex] + versenum = field[1:] verse_list += u'---[%s:%s]---\n' % (versetype, versenum) verse_list += item.text() verse_list += u'\n' @@ -510,15 +510,25 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): # handling carefully user inputted versetags separator = parts.find(u':') if separator >= 0: - verse = parts[0:separator].strip() - subVerse = parts[separator+1:].strip() + versetype = parts[0:separator].strip() + versenum = parts[separator+1:].strip() else: - verse = parts - verseIndex = VerseType.from_loose_input(verse) - verseType = VerseType.Tags[verseIndex] - if not len(subVerse): - subVerse = u'1' - variant = u'%s:%s' % (verseType, subVerse) + versetype = parts + versenum = u'1' + verseindex = \ + VerseType.from_loose_input(versetype) + if verseindex is None: + verseindex = VerseType.Verse + versetype = VerseType.Tags[verseindex] + # Later we need to handle v1a as well. + #regex = re.compile(r'(\d+\w.)') + regex = re.compile(r'\D*(\d+)\D*') + match = regex.match(versenum) + if match: + versenum = match.group(1) + else: + versenum = u'1' + variant = u'%s%s' % (versetype, versenum) else: if parts.endswith(u'\n'): parts = parts.rstrip(u'\n') @@ -585,7 +595,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): order.append(u'%s%s' % (versetag, versenum)) verses = [] verse_names = [] - for index in range (0, self.verseListWidget.rowCount()): + for index in range(0, self.verseListWidget.rowCount()): verse = self.verseListWidget.item(index, 0) verse = unicode(verse.data(QtCore.Qt.UserRole).toString()) if verse not in verse_names: @@ -609,9 +619,10 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): if verse not in order: self.songTabWidget.setCurrentIndex(0) self.verseOrderEdit.setFocus() - versetype, versenum = verse_names[count].split(u':') + versetype = verse_names[count][0] + versenum = verse_names[count][1:] verseindex = VerseType.from_tag(versetype) - versetype = VerseType.Translations[verseindex][0] + versetype = VerseType.TranslatedTags[verseindex].upper() versename = u'%s%s' % (versetype, versenum) answer = QtGui.QMessageBox.warning(self, translate('SongsPlugin.EditSongForm', 'Warning'), @@ -731,7 +742,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): versetag = VerseType.Tags[ VerseType.from_translated_tag(item[0])] versenum = item[1:].lower() - order.append(u'%s%s' % (versetag, versenum)) + order.append(u'%s%s' % (versetag, versenum)) self.song.verse_order = u' '.join(order) self.song.ccli_number = unicode(self.CCLNumberEdit.text()) self.song.song_number = unicode(self.songBookNumberEdit.text()) @@ -775,12 +786,14 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): for i in range(0, self.verseListWidget.rowCount()): item = self.verseListWidget.item(i, 0) verseId = unicode(item.data(QtCore.Qt.UserRole).toString()) - bits = verseId.split(u':') - sxml.add_verse_to_lyrics(bits[0], bits[1], unicode(item.text())) + versetype = verseId[0] + versenum = verseId[1:] + sxml.add_verse_to_lyrics(versetype, versenum, + unicode(item.text())) text = text + self.whitespace.sub(u' ', unicode(self.verseListWidget.item(i, 0).text())) + u' ' - if (bits[1] > u'1') and (bits[0][0] not in multiple): - multiple.append(bits[0][0]) + if (versenum > u'1') and (versetype not in multiple): + multiple.append(versetype) self.song.search_lyrics = text.lower() self.song.lyrics = unicode(sxml.extract_xml(), u'utf-8') for verse in multiple: diff --git a/openlp/plugins/songs/forms/editversedialog.py b/openlp/plugins/songs/forms/editversedialog.py index fe857e12a..64da3e89e 100644 --- a/openlp/plugins/songs/forms/editversedialog.py +++ b/openlp/plugins/songs/forms/editversedialog.py @@ -70,19 +70,19 @@ class Ui_EditVerseDialog(object): translate('SongsPlugin.EditVerseForm', 'Edit Verse')) self.verseTypeLabel.setText( translate('SongsPlugin.EditVerseForm', '&Verse type:')) - self.verseTypeComboBox.setItemText(0, - VerseType.Translations[VerseType.Verse]) - self.verseTypeComboBox.setItemText(1, - VerseType.Translations[VerseType.Chorus]) - self.verseTypeComboBox.setItemText(2, - VerseType.Translations[VerseType.Bridge]) - self.verseTypeComboBox.setItemText(3, - VerseType.Translations[VerseType.PreChorus]) - self.verseTypeComboBox.setItemText(4, - VerseType.Translations[VerseType.Intro]) - self.verseTypeComboBox.setItemText(5, - VerseType.Translations[VerseType.Ending]) - self.verseTypeComboBox.setItemText(6, - VerseType.Translations[VerseType.Other]) + self.verseTypeComboBox.setItemText(VerseType.Verse, + VerseType.TranslatedNames[VerseType.Verse]) + self.verseTypeComboBox.setItemText(VerseType.Chorus, + VerseType.TranslatedNames[VerseType.Chorus]) + self.verseTypeComboBox.setItemText(VerseType.Bridge, + VerseType.TranslatedNames[VerseType.Bridge]) + self.verseTypeComboBox.setItemText(VerseType.PreChorus, + VerseType.TranslatedNames[VerseType.PreChorus]) + self.verseTypeComboBox.setItemText(VerseType.Intro, + VerseType.TranslatedNames[VerseType.Intro]) + self.verseTypeComboBox.setItemText(VerseType.Ending, + VerseType.TranslatedNames[VerseType.Ending]) + self.verseTypeComboBox.setItemText(VerseType.Other, + VerseType.TranslatedNames[VerseType.Other]) self.insertButton.setText( translate('SongsPlugin.EditVerseForm', '&Insert')) diff --git a/openlp/plugins/songs/forms/editverseform.py b/openlp/plugins/songs/forms/editverseform.py index bb9d9e906..a8fe1302d 100644 --- a/openlp/plugins/songs/forms/editverseform.py +++ b/openlp/plugins/songs/forms/editverseform.py @@ -57,22 +57,23 @@ class EditVerseForm(QtGui.QDialog, Ui_EditVerseDialog): QtCore.QObject.connect(self.verseTypeComboBox, QtCore.SIGNAL(u'currentIndexChanged(int)'), self.onVerseTypeComboBoxChanged) - self.verse_regex = re.compile(r'---\[(.+):(.+)\]---') + self.verse_regex = re.compile(r'---\[(.+):\D*(\d+)\D*\]---') def contextMenu(self, point): item = self.serviceManagerList.itemAt(point) - def insertVerse(self, title, num=1): + def insertVerse(self, versetype, num=1): if self.verseTextEdit.textCursor().columnNumber() != 0: self.verseTextEdit.insertPlainText(u'\n') - self.verseTextEdit.insertPlainText(u'---[%s:%s]---\n' % (title, num)) + versetype = VerseType.TranslatedNames[VerseTag.from_tag(versetype)] + self.verseTextEdit.insertPlainText(u'---[%s:%s]---\n' % \ + (versetype, num)) self.verseTextEdit.setFocus() def onInsertButtonClicked(self): - verse_type = self.verseTypeComboBox.currentIndex() - if VerseType.to_translated_string(verse_type) is not None: - self.insertVerse(VerseType.to_translated_string(verse_type), - self.verseNumberBox.value()) + vtypeindex = self.verseTypeComboBox.currentIndex() + self.insertVerse(VerseType.Tags[vtypeindex], + self.verseNumberBox.value()) def onVerseTypeComboBoxChanged(self): """ @@ -81,7 +82,7 @@ class EditVerseForm(QtGui.QDialog, Ui_EditVerseDialog): """ position = self.verseTextEdit.textCursor().position() text = unicode(self.verseTextEdit.toPlainText()) - verse_type = VerseType.Translations[ + verse_type = VerseType.TranslatedNames[ self.verseTypeComboBox.currentIndex()] if not text: return @@ -98,11 +99,7 @@ class EditVerseForm(QtGui.QDialog, Ui_EditVerseDialog): if match: verse_type = match.group(1) verse_number = int(match.group(2)) - verse_type_index = VerseType.from_translated_string(verse_type) - if verse_type_index is None: - verse_type_index = VerseType.from_string(verse_type) - if verse_type_index is None: - verse_type_index = VerseType.from_tag(verse_type) + verse_type_index = VerseType.from_loose_input(verse_type) if verse_type_index is not None: self.verseNumberBox.setValue(verse_number) @@ -128,19 +125,20 @@ class EditVerseForm(QtGui.QDialog, Ui_EditVerseDialog): text = text[:position + 4] match = self.verse_regex.match(text) if match: - verse_type = match.group(1) - verse_number = int(match.group(2)) - verse_type_index = VerseType.from_loose_input(verse_type) - if verse_type_index is not None: - self.verseTypeComboBox.setCurrentIndex(verse_type_index) - self.verseNumberBox.setValue(verse_number) + versetype = match.group(1) + vtypeindex = VerseType.from_loose_input(versetype) + regex = re.compile(r'(\d+)') + versenum = int(match.group(2)) + if vtypeindex is not None: + self.verseTypeComboBox.setCurrentIndex(vtypeindex) + self.verseNumberBox.setValue(versenum) def setVerse(self, text, single=False, - tag=u'%s:1' % VerseType.tag(VerseType.Verse)): + tag=u'%s1' % VerseType.Tags[VerseType.Verse]): self.hasSingleVerse = single if single: - verse_type, verse_number = tag.split(u':') - verse_type_index = VerseType.from_tag(verse_type) + verse_type_index = VerseType.from_tag(tag[0]) + verse_number = tag[1:] if verse_type_index is not None: self.verseTypeComboBox.setCurrentIndex(verse_type_index) self.verseNumberBox.setValue(int(verse_number)) @@ -148,7 +146,7 @@ class EditVerseForm(QtGui.QDialog, Ui_EditVerseDialog): else: if not text: text = u'---[%s:1]---\n' % \ - VerseType.to_translated_string(VerseType.Verse) + VerseType.TranslatedNames[VerseType.Verse] self.verseTypeComboBox.setCurrentIndex(0) self.verseNumberBox.setValue(1) self.insertButton.setVisible(True) @@ -165,7 +163,7 @@ class EditVerseForm(QtGui.QDialog, Ui_EditVerseDialog): text = self.verseTextEdit.toPlainText() if not text.startsWith(u'---['): text = u'---[%s:1]---\n%s' % \ - (VerseType.to_translated_string(VerseType.Verse), text) + (VerseType.TranslatedNames[VerseType.Verse], text) return text def accept(self): diff --git a/openlp/plugins/songs/lib/__init__.py b/openlp/plugins/songs/lib/__init__.py index 7853f6c61..da3b97574 100644 --- a/openlp/plugins/songs/lib/__init__.py +++ b/openlp/plugins/songs/lib/__init__.py @@ -39,14 +39,7 @@ class VerseType(object): Intro = 4 Ending = 5 Other = 6 - Tags = [ - u'v', - u'c', - u'b', - u'p', - u'i', - u'e', - u'o'] + Names = [ u'Verse', u'Chorus', @@ -55,7 +48,11 @@ class VerseType(object): u'Intro', u'Ending', u'Other'] - Translations = [ + Tags = [] + for name in Names: + Tags.append(name[0].lower()) + + TranslatedNames = [ unicode(translate('SongsPlugin.VerseType', 'Verse')), unicode(translate('SongsPlugin.VerseType', 'Chorus')), unicode(translate('SongsPlugin.VerseType', 'Bridge')), @@ -63,60 +60,9 @@ class VerseType(object): unicode(translate('SongsPlugin.VerseType', 'Intro')), unicode(translate('SongsPlugin.VerseType', 'Ending')), unicode(translate('SongsPlugin.VerseType', 'Other'))] - - @staticmethod - def tag(verse_type): - """ - Return a string for a given VerseType tag - - ``verse_type`` - The verse type to return a string for - """ - if isinstance(verse_type, int): - try: - return VerseType.Tags[verse_type] - except: - return - elif verse_type[0].lower() in VerseType.Tags: - return verse_type[0].lower() - - @staticmethod - def to_string(verse_type): - """ - Return a string for a given VerseType Name - - ``verse_type`` - The type to return a string for - """ - if isinstance(verse_type, int): - try: - return VerseType.Names[verse_type] - except: - return - else: - verse_type = verse_type[0].lower() - for num, tag in enumerate(VerseType.Tags): - if verse_type == tag: - return VerseType.Names[num] - - @staticmethod - def to_translated_string(verse_type): - """ - Return a string for a given VerseType Name - - ``verse_type`` - The type to return a string for - """ - if isinstance(verse_type, int): - try: - return VerseType.Translations[verse_type] - except: - return - else: - verse_type = verse_type[0].lower() - for num, tag in enumerate(VerseType.Tags): - if verse_type == tag: - return VerseType.Translations[num] + TranslatedTags = [] + for name in TranslatedNames: + TranslatedTags.append(name[0].lower()) @staticmethod def from_tag(verse_type): @@ -140,9 +86,9 @@ class VerseType(object): The string to return a VerseType for """ verse_type = verse_type[0].lower() - for num, string in enumerate(VerseType.Translations): - if verse_type == string[0].lower(): - return num + for vtypeIndex, vtypeTag in enumerate(VerseType.TranslatedTags): + if verse_type == vtypeTag: + return vtypeIndex @staticmethod def from_string(verse_type): @@ -153,9 +99,9 @@ class VerseType(object): The string to return a VerseType for """ verse_type = verse_type.lower() - for num, string in enumerate(VerseType.Names): - if verse_type == string.lower(): - return num + for vtypeIndex, vtypeName in enumerate(VerseType.Names): + if verse_type == vtypeName.lower(): + return vtypeIndex @staticmethod def from_translated_string(verse_type): @@ -166,7 +112,7 @@ class VerseType(object): The string to return a VerseType for """ verse_type = verse_type.lower() - for num, translation in enumerate(VerseType.Translations): + for num, translation in enumerate(VerseType.TranslatedNames): if verse_type == translation.lower(): return num diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index e71939bb7..147450efd 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -346,8 +346,8 @@ class SongMediaItem(MediaManagerItem): # no verse list or only 1 space (in error) if not song.verse_order.strip(): for verse in verseList: - # we cannot use from_loose_input() here, because database - # is supposed to contain English lowercase singlechar tags + # We cannot use from_loose_input() here, because database + # is supposed to contain English lowercase singlechar tags. verse_type = verse[0][u'type'] verseIndex = None if len(verse_type) > 1: @@ -359,13 +359,12 @@ class SongMediaItem(MediaManagerItem): verseIndex = VerseType.from_tag(verse_type) if verseIndex is None: verseIndex = VerseType.Other - versetype = VerseType.Translations[verseIndex][0] - verseTag = u'%s:%s' % (versetype, verse[0][u'label']) + versetype = VerseType.TranslatedTags[verseIndex].upper() + verseTag = u'%s%s' % (versetype, verse[0][u'label']) service_item.add_from_text( verse[1][:30], unicode(verse[1]), verseTag) else: # Loop through the verse list and expand the song accordingly. - print song.verse_order for order in song.verse_order.lower().split(): if len(order) == 0: break @@ -373,9 +372,10 @@ class SongMediaItem(MediaManagerItem): if verse[0][u'type'][0] == order[0] and \ (verse[0][u'label'] == order[1:] or not order[1:]): verseindex = VerseType.from_tag(verse[0][u'type']) - versetype = VerseType.Translations[verseindex][0] - verseTag = u'%s:%s' % \ - (versetype, verse[0][u'label']) + versetype = VerseType.TranslatedTags[verseindex]\ + .upper() + verseTag = u'%s%s' % (versetype, + verse[0][u'label']) service_item.add_from_text( verse[1][:30], verse[1], verseTag) else: diff --git a/openlp/plugins/songs/lib/xml.py b/openlp/plugins/songs/lib/xml.py index b96e79961..bfae14db7 100644 --- a/openlp/plugins/songs/lib/xml.py +++ b/openlp/plugins/songs/lib/xml.py @@ -464,7 +464,10 @@ class OpenLyrics(object): text += u'\n' text += u'\n'.join([unicode(line) for line in lines.line]) verse_name = self._get(verse, u'name') - verse_type = unicode(VerseType.to_string(verse_name[0])) + verse_type_index = VerseType.from_tag(verse_name[0]) + if verse_type_index is None: + verse_type_index = VerseType.Other + verse_type = VerseType.Names[verse_type_index] verse_number = re.compile(u'[a-zA-Z]*').sub(u'', verse_name) verse_part = re.compile(u'[0-9]*').sub(u'', verse_name[1:]) # OpenLyrics allows e. g. "c", but we need "c1".