From 268f6361b57551ccc4128356efb72c07fa399e19 Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Sat, 14 Mar 2009 07:08:15 +0000 Subject: [PATCH] Fix up quote problems in SongXMLHandler Add MVC to Custom Add Update to Custom Remove xml1.py as has served it's purpose bzr-revno: 416 --- openlp/core/lib/songxmlhandler.py | 16 ++-- openlp/plugins/custom/forms/editcustomform.py | 42 ++++++---- openlp/plugins/custom/lib/manager.py | 11 ++- openlp/plugins/custom/lib/mediaitem.py | 28 ++++--- openlp/plugins/custom/lib/textlistdata.py | 4 +- xml1.py | 83 ------------------- 6 files changed, 61 insertions(+), 123 deletions(-) delete mode 100644 xml1.py diff --git a/openlp/core/lib/songxmlhandler.py b/openlp/core/lib/songxmlhandler.py index 51fb9af83..a8210d0e7 100644 --- a/openlp/core/lib/songxmlhandler.py +++ b/openlp/core/lib/songxmlhandler.py @@ -18,14 +18,14 @@ class SongXMLBuilder(): def new_document(self): # Create the base element - self.song = self.song_xml.createElement("song") + self.song = self.song_xml.createElement(u'song') self.song_xml.appendChild(self.song) - self.song.setAttribute("version", "1.0") + self.song.setAttribute(u'version', u'1.0') def add_lyrics_to_song(self): # Create the main element - self.lyrics = self.song_xml.createElement("lyrics") - self.lyrics.setAttribute("language", "en") + self.lyrics = self.song_xml.createElement(u'lyrics') + self.lyrics.setAttribute(u'language', u'en') self.song.appendChild(self.lyrics) def add_verse_to_lyrics(self, type, number, content): @@ -34,9 +34,9 @@ class SongXMLBuilder(): number - number of item eg verse 1 content - the text to be stored """ - verse = self.song_xml.createElement("verse") - verse.setAttribute("type", type) - verse.setAttribute('label', number) + verse = self.song_xml.createElement(u'verse') + verse.setAttribute(u'type', type) + verse.setAttribute(u'label', number) self.lyrics.appendChild(verse) # add data as a CDATA section @@ -60,7 +60,7 @@ class SongXMLParser(): iter=self.song_xml.getiterator() verse_list = [] for element in iter: - if element.tag == 'verse': + if element.tag == u'verse': verse_list.append([element.attrib, element.text]) return verse_list diff --git a/openlp/plugins/custom/forms/editcustomform.py b/openlp/plugins/custom/forms/editcustomform.py index 57e5c27c1..6f3a059b0 100644 --- a/openlp/plugins/custom/forms/editcustomform.py +++ b/openlp/plugins/custom/forms/editcustomform.py @@ -20,7 +20,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA from PyQt4 import Qt, QtCore, QtGui from editcustomdialog import Ui_customEditDialog -from openlp.core.lib import SongXMLBuilder +from openlp.core.lib import SongXMLBuilder, SongXMLParser from openlp.plugins.custom.lib.models import CustomSlide class EditCustomForm(QtGui.QDialog, Ui_customEditDialog): @@ -54,6 +54,25 @@ class EditCustomForm(QtGui.QDialog, Ui_customEditDialog): self.initialise() self.VerseListView.setAlternatingRowColors(True) #self.savebutton = self.ButtonBox.button(QtGui.QDialogButtonBox.Save) + + def initialise(self): + self.valid = True + self.DeleteButton.setEnabled(False) + self.EditButton.setEnabled(False) + self.SaveButton.setEnabled(False) + self.VerseTextEdit.clear() + self.VerseListView.clear() + + def loadCustom(self, id): + self.customSlide = self.custommanager.get_custom(id) + self.TitleEdit.setText(self.customSlide.title) + self.CreditEdit.setText(self.customSlide.title) + + songXML=SongXMLParser(self.customSlide.text) + verseList = songXML.get_verses() + for verse in verseList: + self.VerseListView.addItem(verse[1]) + self.validate() def accept(self): self.validate() @@ -66,11 +85,12 @@ class EditCustomForm(QtGui.QDialog, Ui_customEditDialog): sxml.add_verse_to_lyrics(u'custom', str(count), str(self.VerseListView.item(i).text())) count += 1 sxml.dump_xml() - customSlide = CustomSlide() - customSlide.title = unicode(self.TitleEdit.displayText()) - customSlide.text = unicode(sxml.extract_xml()) - customSlide.credits = unicode(self.CreditEdit.displayText()) - self.custommanager.save_slide(customSlide) + if self.customSlide == None: + self.customSlide = CustomSlide() + self.customSlide.title = unicode(self.TitleEdit.displayText()) + self.customSlide.text = unicode(sxml.extract_xml()) + self.customSlide.credits = unicode(self.CreditEdit.displayText()) + self.custommanager.save_slide(self.customSlide) self.close() def rejected(self): @@ -134,16 +154,6 @@ class EditCustomForm(QtGui.QDialog, Ui_customEditDialog): if invalid == 1: self.valid = False - def initialise(self): - self.valid = True - self.DeleteButton.setEnabled(False) - self.EditButton.setEnabled(False) - self.SaveButton.setEnabled(False) -# list = self.songmanager.get_authors() -# self.AuthorsSelectionComboItem.clear() -# for i in list: -# self.AuthorsSelectionComboItem.addItem( i.display_name) - def loadCustomItem(self, id): pass #self.item = self.songmanager.get_song(id) diff --git a/openlp/plugins/custom/lib/manager.py b/openlp/plugins/custom/lib/manager.py index fc28a226a..e8dcf9e36 100644 --- a/openlp/plugins/custom/lib/manager.py +++ b/openlp/plugins/custom/lib/manager.py @@ -75,7 +75,7 @@ class CustomManager(): Returns the details of a song """ if id is None: - return CustomeSlide() + return CustomSlide() else: return self.session.query(CustomSlide).get(id) @@ -92,6 +92,15 @@ class CustomManager(): except: log.debug('Custom Slide failed') return False + + def get_custom(self, id=None): + """ + Returns the details of a Custom Slide + """ + if id is None: + return CustomSlide() + else: + return self.session.query(CustomSlide).get(id) # # def delete_song(self, song): # try: diff --git a/openlp/plugins/custom/lib/mediaitem.py b/openlp/plugins/custom/lib/mediaitem.py index 800b62555..f58a0b956 100644 --- a/openlp/plugins/custom/lib/mediaitem.py +++ b/openlp/plugins/custom/lib/mediaitem.py @@ -21,10 +21,11 @@ import logging from PyQt4 import QtCore, QtGui +from openlp.core import translate from openlp.core.lib import MediaManagerItem from openlp.core.resources import * -#from openlp.plugins.custom.lib import TextItemData +from openlp.plugins.custom.lib import TextListData class CustomMediaItem(MediaManagerItem): @@ -95,8 +96,8 @@ class CustomMediaItem(MediaManagerItem): self.CustomListView = QtGui.QListView() self.CustomListView.setAlternatingRowColors(True) -# self.CustomListData = TextListData() -# self.CustomListView.setModel(self.CustomListData) + self.CustomListData = TextListData() + self.CustomListView.setModel(self.CustomListData) self.PageLayout.addWidget(self.CustomListView) @@ -110,9 +111,12 @@ class CustomMediaItem(MediaManagerItem): QtCore.QObject.connect(self.CustomListView, QtCore.SIGNAL("itemPressed(QTableWidgetItem * item)"), self.onCustomSelected) -# #define and add the context menu + #define and add the context menu self.CustomListView.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) -# + self.CustomListView.addAction(self.contextMenuAction(self.CustomListView, + ':/custom/custom_edit.png', translate('CustomMediaItem', u'&Edit Custom'), + self.onCustomEditClick)) + self.CustomListView.addAction(self.contextMenuSeparator(self.CustomListView)) self.CustomListView.addAction(self.contextMenuAction( self.CustomListView, ':/system/system_preview.png', "&Preview Custom", self.onCustomPreviewClick)) @@ -128,9 +132,7 @@ class CustomMediaItem(MediaManagerItem): def loadCustomList(self, list): for CustomSlide in list: - print CustomSlide.title -# for CustomSlide in list: -# self.CustomListData.addRow(CustomSlide.id,CustomSlide.title) + self.CustomListData.addRow(CustomSlide.id,CustomSlide.title) def onClearTextButtonClick(self): """ @@ -154,12 +156,14 @@ class CustomMediaItem(MediaManagerItem): def onCustomNewClick(self): self.parent.edit_custom_form.exec_() + self.initialise() def onCustomEditClick(self): - current_row = self.CustomListView.currentRow() - id = int(self.CustomListView.item(current_row, 0).text()) - self.edit_Custom_form.loadCustom(id) - self.edit_Custom_form.exec_() + indexes = self.CustomListView.selectedIndexes() + for index in indexes: + self.parent.edit_custom_form.loadCustom(self.CustomListData.getId(index)) + self.parent.edit_custom_form.exec_() + self.initialise() def onCustomDeleteClick(self): pass diff --git a/openlp/plugins/custom/lib/textlistdata.py b/openlp/plugins/custom/lib/textlistdata.py index 094a9e981..70f4634f6 100644 --- a/openlp/plugins/custom/lib/textlistdata.py +++ b/openlp/plugins/custom/lib/textlistdata.py @@ -38,8 +38,6 @@ class TextListData(QAbstractListModel): if row > len(self.items): # if the last row is selected and deleted, we then get called with an empty row! return QVariant() if role==Qt.DisplayRole: - retval= self.items[row][2] - elif role == Qt.DecorationRole: retval= self.items[row][1] elif role == Qt.ToolTipRole: retval= self.items[row][0] @@ -57,7 +55,7 @@ class TextListData(QAbstractListModel): def getId(self, index): row = index.row() - return self.item[row][0] + return self.items[row][0] if __name__=="__main__": sxml=TextListData() diff --git a/xml1.py b/xml1.py deleted file mode 100644 index 5406451d1..000000000 --- a/xml1.py +++ /dev/null @@ -1,83 +0,0 @@ -from xml.dom.minidom import Document -from xml.etree.ElementTree import ElementTree, XML, dump -""" - - - - - - - - - -""" -class SongXMLBuilder(): - def __init__(self): - # Create the minidom document - self.song_xml = Document() - - def new_document(self): - # Create the base element - self.song = self.song_xml.createElement(u'song') - self.song_xml.appendChild(self.song) - self.song.setAttribute(u'version', u'1.0') - - def add_lyrics_to_song(self): - # Create the main element - self.lyrics = self.song_xml.createElement(u'lyrics') - self.lyrics.setAttribute(u'language', u'en') - self.song.appendChild(self.lyrics) - - def add_verse_to_lyrics(self, type, label, content): - """ - type - type of verse (Chorus, Verse , Bridge, Custom etc - label - label of item eg verse 1 - content - the text to be stored - """ - verse = self.song_xml.createElement(u'verse') - verse.setAttribute(u'type', type) - verse.setAttribute(u'label', label) - self.lyrics.appendChild(verse) - - # add data as a CDATA section - cds = self.song_xml.createCDATASection(content) - verse.appendChild(cds) - - def dump_xml(self): - # Debugging aid to see what we have - print self.song_xml.toprettyxml(indent=u' ') - - def extract_xml(self): - # Print our newly created XML - return self.song_xml.toxml() - -class SongXMLParser(): - def __init__(self, xml): - self.song_xml = ElementTree(element=XML(xml)) - - def get_verses(self): - #return a list of verse's and attributes - iter=self.song_xml.getiterator() - verse_list = [] - for element in iter: - if element.tag == u'verse': - verse_list.append([element.attrib, element.text]) - return verse_list - - def dump_xml(self): - # Debugging aid to see what we have - print dump(self.song_xml) - -if __name__ == '__main__': - sxml=SongXMLBuilder() - sxml.new_document() - sxml.add_lyrics_to_song() - sxml.add_verse_to_lyrics(u'chorus', u'1', u'The is\n is \nsome\n text') - sxml.add_verse_to_lyrics(u'verse', u'2', u'The is\n is \nmore\n text') - sxml.dump_xml() - x1 = sxml.extract_xml() - print x1 - print "================================" - spra=SongXMLParser(x1) - vl = spra.get_verses() - print vl