fixed ticket 502 and another fix

bzr-revno: 1981
This commit is contained in:
Andreas Preikschat 2012-06-04 12:39:38 +02:00
commit d16e64f11a
2 changed files with 67 additions and 53 deletions

View File

@ -707,7 +707,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
text = unicode(self.songBookComboBox.currentText()) text = unicode(self.songBookComboBox.currentText())
if item == 0 and text: if item == 0 and text:
temp_song_book = text temp_song_book = text
self.mediaitem.songMaintenanceForm.exec_() self.mediaitem.songMaintenanceForm.exec_(True)
self.loadAuthors() self.loadAuthors()
self.loadBooks() self.loadBooks()
self.loadTopics() self.loadTopics()
@ -865,12 +865,16 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
for row in xrange(self.authorsListView.count()): for row in xrange(self.authorsListView.count()):
item = self.authorsListView.item(row) item = self.authorsListView.item(row)
authorId = (item.data(QtCore.Qt.UserRole)).toInt()[0] authorId = (item.data(QtCore.Qt.UserRole)).toInt()[0]
self.song.authors.append(self.manager.get_object(Author, authorId)) author = self.manager.get_object(Author, authorId)
if author is not None:
self.song.authors.append(author)
self.song.topics = [] self.song.topics = []
for row in xrange(self.topicsListView.count()): for row in xrange(self.topicsListView.count()):
item = self.topicsListView.item(row) item = self.topicsListView.item(row)
topicId = (item.data(QtCore.Qt.UserRole)).toInt()[0] topicId = (item.data(QtCore.Qt.UserRole)).toInt()[0]
self.song.topics.append(self.manager.get_object(Topic, topicId)) topic = self.manager.get_object(Topic, topicId)
if topic is not None:
self.song.topics.append(topic)
# Save the song here because we need a valid id for the audio files. # Save the song here because we need a valid id for the audio files.
clean_song(self.manager, self.song) clean_song(self.manager, self.song)
self.manager.save_object(self.song) self.manager.save_object(self.song)

View File

@ -87,7 +87,15 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
QtCore.SIGNAL(u'currentRowChanged(int)'), QtCore.SIGNAL(u'currentRowChanged(int)'),
self.onBooksListRowChanged) self.onBooksListRowChanged)
def exec_(self): def exec_(self, fromSongEdit=False):
"""
Show the dialog.
``fromSongEdit``
Indicates if the maintenance dialog has been opened from song edit
or from the media manager. Defaults to **False**.
"""
self.fromSongEdit = fromSongEdit
self.typeListWidget.setCurrentRow(0) self.typeListWidget.setCurrentRow(0)
self.resetAuthors() self.resetAuthors()
self.resetTopics() self.resetTopics()
@ -103,20 +111,20 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
else: else:
return -1 return -1
def _deleteItem(self, item_class, list_widget, reset_func, dlg_title, def _deleteItem(self, itemClass, listWidget, resetFunc, dlgTitle,
del_text, err_text): del_text, err_text):
item_id = self._getCurrentItemId(list_widget) item_id = self._getCurrentItemId(listWidget)
if item_id != -1: if item_id != -1:
item = self.manager.get_object(item_class, item_id) item = self.manager.get_object(itemClass, item_id)
if item and not item.songs: if item and not item.songs:
if critical_error_message_box(dlg_title, del_text, self, if critical_error_message_box(dlgTitle, del_text, self,
True) == QtGui.QMessageBox.Yes: True) == QtGui.QMessageBox.Yes:
self.manager.delete_object(item_class, item.id) self.manager.delete_object(itemClass, item.id)
reset_func() resetFunc()
else: else:
critical_error_message_box(dlg_title, err_text) critical_error_message_box(dlgTitle, err_text)
else: else:
critical_error_message_box(dlg_title, UiStrings().NISs) critical_error_message_box(dlgTitle, UiStrings().NISs)
def resetAuthors(self): def resetAuthors(self):
""" """
@ -157,34 +165,34 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
book_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(book.id)) book_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(book.id))
self.booksListWidget.addItem(book_name) self.booksListWidget.addItem(book_name)
def checkAuthor(self, new_author, edit=False): def checkAuthor(self, newAuthor, edit=False):
""" """
Returns *False* if the given Author already exists, otherwise *True*. Returns *False* if the given Author already exists, otherwise *True*.
""" """
authors = self.manager.get_all_objects(Author, authors = self.manager.get_all_objects(Author,
and_(Author.first_name == new_author.first_name, and_(Author.first_name == newAuthor.first_name,
Author.last_name == new_author.last_name, Author.last_name == newAuthor.last_name,
Author.display_name == new_author.display_name)) Author.display_name == newAuthor.display_name))
return self.__checkObject(authors, new_author, edit) return self.__checkObject(authors, newAuthor, edit)
def checkTopic(self, new_topic, edit=False): def checkTopic(self, newTopic, edit=False):
""" """
Returns *False* if the given Topic already exists, otherwise *True*. Returns *False* if the given Topic already exists, otherwise *True*.
""" """
topics = self.manager.get_all_objects(Topic, topics = self.manager.get_all_objects(Topic,
Topic.name == new_topic.name) Topic.name == newTopic.name)
return self.__checkObject(topics, new_topic, edit) return self.__checkObject(topics, newTopic, edit)
def checkBook(self, new_book, edit=False): def checkBook(self, newBook, edit=False):
""" """
Returns *False* if the given Topic already exists, otherwise *True*. Returns *False* if the given Topic already exists, otherwise *True*.
""" """
books = self.manager.get_all_objects(Book, books = self.manager.get_all_objects(Book,
and_(Book.name == new_book.name, and_(Book.name == newBook.name,
Book.publisher == new_book.publisher)) Book.publisher == newBook.publisher))
return self.__checkObject(books, new_book, edit) return self.__checkObject(books, newBook, edit)
def __checkObject(self, objects, new_object, edit): def __checkObject(self, objects, newObject, edit):
""" """
Utility method to check for an existing object. Utility method to check for an existing object.
@ -196,7 +204,7 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
# not return False when nothing has changed. # not return False when nothing has changed.
if edit: if edit:
for object in objects: for object in objects:
if object.id != new_object.id: if object.id != newObject.id:
return False return False
return True return True
else: else:
@ -275,6 +283,7 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
if self.checkAuthor(author, True): if self.checkAuthor(author, True):
if self.manager.save_object(author): if self.manager.save_object(author):
self.resetAuthors() self.resetAuthors()
if not self.fromSongEdit:
Receiver.send_message(u'songs_load_list') Receiver.send_message(u'songs_load_list')
else: else:
critical_error_message_box( critical_error_message_box(
@ -373,75 +382,76 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
Receiver.send_message(u'cursor_busy') Receiver.send_message(u'cursor_busy')
merge(dbObject) merge(dbObject)
reset() reset()
if not self.fromSongEdit:
Receiver.send_message(u'songs_load_list') Receiver.send_message(u'songs_load_list')
Receiver.send_message(u'cursor_normal') Receiver.send_message(u'cursor_normal')
def mergeAuthors(self, old_author): def mergeAuthors(self, oldAuthor):
""" """
Merges two authors into one author. Merges two authors into one author.
``old_author`` ``oldAuthor``
The object, which was edited, that will be deleted The object, which was edited, that will be deleted
""" """
# Find the duplicate. # Find the duplicate.
existing_author = self.manager.get_object_filtered(Author, existing_author = self.manager.get_object_filtered(Author,
and_(Author.first_name == old_author.first_name, and_(Author.first_name == oldAuthor.first_name,
Author.last_name == old_author.last_name, Author.last_name == oldAuthor.last_name,
Author.display_name == old_author.display_name, Author.display_name == oldAuthor.display_name,
Author.id != old_author.id)) Author.id != oldAuthor.id))
# Find the songs, which have the old_author as author. # Find the songs, which have the oldAuthor as author.
songs = self.manager.get_all_objects(Song, songs = self.manager.get_all_objects(Song,
Song.authors.contains(old_author)) Song.authors.contains(oldAuthor))
for song in songs: for song in songs:
# We check if the song has already existing_author as author. If # We check if the song has already existing_author as author. If
# that is not the case we add it. # that is not the case we add it.
if existing_author not in song.authors: if existing_author not in song.authors:
song.authors.append(existing_author) song.authors.append(existing_author)
song.authors.remove(old_author) song.authors.remove(oldAuthor)
self.manager.save_object(song) self.manager.save_object(song)
self.manager.delete_object(Author, old_author.id) self.manager.delete_object(Author, oldAuthor.id)
def mergeTopics(self, old_topic): def mergeTopics(self, oldTopic):
""" """
Merges two topics into one topic. Merges two topics into one topic.
``old_topic`` ``oldTopic``
The object, which was edited, that will be deleted The object, which was edited, that will be deleted
""" """
# Find the duplicate. # Find the duplicate.
existing_topic = self.manager.get_object_filtered(Topic, existing_topic = self.manager.get_object_filtered(Topic,
and_(Topic.name == old_topic.name, Topic.id != old_topic.id)) and_(Topic.name == oldTopic.name, Topic.id != oldTopic.id))
# Find the songs, which have the old_topic as topic. # Find the songs, which have the oldTopic as topic.
songs = self.manager.get_all_objects(Song, songs = self.manager.get_all_objects(Song,
Song.topics.contains(old_topic)) Song.topics.contains(oldTopic))
for song in songs: for song in songs:
# We check if the song has already existing_topic as topic. If that # We check if the song has already existing_topic as topic. If that
# is not the case we add it. # is not the case we add it.
if existing_topic not in song.topics: if existing_topic not in song.topics:
song.topics.append(existing_topic) song.topics.append(existing_topic)
song.topics.remove(old_topic) song.topics.remove(oldTopic)
self.manager.save_object(song) self.manager.save_object(song)
self.manager.delete_object(Topic, old_topic.id) self.manager.delete_object(Topic, oldTopic.id)
def mergeBooks(self, old_book): def mergeBooks(self, oldBook):
""" """
Merges two books into one book. Merges two books into one book.
``old_book`` ``oldBook``
The object, which was edited, that will be deleted The object, which was edited, that will be deleted
""" """
# Find the duplicate. # Find the duplicate.
existing_book = self.manager.get_object_filtered(Book, existing_book = self.manager.get_object_filtered(Book,
and_(Book.name == old_book.name, and_(Book.name == oldBook.name,
Book.publisher == old_book.publisher, Book.publisher == oldBook.publisher,
Book.id != old_book.id)) Book.id != oldBook.id))
# Find the songs, which have the old_book as book. # Find the songs, which have the oldBook as book.
songs = self.manager.get_all_objects(Song, songs = self.manager.get_all_objects(Song,
Song.song_book_id == old_book.id) Song.song_book_id == oldBook.id)
for song in songs: for song in songs:
song.song_book_id = existing_book.id song.song_book_id = existing_book.id
self.manager.save_object(song) self.manager.save_object(song)
self.manager.delete_object(Book, old_book.id) self.manager.delete_object(Book, oldBook.id)
def onAuthorDeleteButtonClicked(self): def onAuthorDeleteButtonClicked(self):
""" """