From 42a75e545e70e86358c2c2ab2eb85f8922c10fb1 Mon Sep 17 00:00:00 2001 From: Arjan Schrijver Date: Tue, 29 Jan 2013 14:21:07 +0100 Subject: [PATCH] Implemented drag&drop from external directly into an image group --- openlp/core/lib/mediamanageritem.py | 15 ++++--- openlp/core/lib/treewidgetwithdnd.py | 2 +- openlp/plugins/custom/lib/mediaitem.py | 2 +- openlp/plugins/images/lib/mediaitem.py | 43 +++++++++++-------- openlp/plugins/media/lib/mediaitem.py | 2 +- openlp/plugins/presentations/lib/mediaitem.py | 4 +- 6 files changed, 39 insertions(+), 29 deletions(-) diff --git a/openlp/core/lib/mediamanageritem.py b/openlp/core/lib/mediamanageritem.py index 540011f68..7708c1ce8 100644 --- a/openlp/core/lib/mediamanageritem.py +++ b/openlp/core/lib/mediamanageritem.py @@ -335,7 +335,7 @@ class MediaManagerItem(QtGui.QWidget): self.validateAndLoad(files) Receiver.send_message(u'cursor_normal') - def loadFile(self, files): + def loadFile(self, data): """ Turn file from Drag and Drop into an array so the Validate code can run it. @@ -344,7 +344,7 @@ class MediaManagerItem(QtGui.QWidget): """ new_files = [] error_shown = False - for file in files: + for file in data['files']: type = file.split(u'.')[-1] if type.lower() not in self.onNewFileMasks: if not error_shown: @@ -354,7 +354,7 @@ class MediaManagerItem(QtGui.QWidget): else: new_files.append(file) if new_files: - self.validateAndLoad(new_files) + self.validateAndLoad(new_files, data['target']) def dndMoveInternal(self, target): """ @@ -362,7 +362,7 @@ class MediaManagerItem(QtGui.QWidget): """ pass - def validateAndLoad(self, files): + def validateAndLoad(self, files, target_group=None): """ Process a list for files either from the File Dialog or from Drag and Drop @@ -385,8 +385,9 @@ class MediaManagerItem(QtGui.QWidget): files_added = True full_list.append(file) if full_list and files_added: - self.listView.clear() - self.loadList(full_list) + if target_group is None: + self.listView.clear() + self.loadList(full_list, target_group) last_dir = os.path.split(unicode(files[0]))[0] Settings().setValue(self.settingsSection + u'/last directory', last_dir) if duplicates_found: @@ -415,7 +416,7 @@ class MediaManagerItem(QtGui.QWidget): count += 1 return file_list - def loadList(self, list): + def loadList(self, list, target_group): raise NotImplementedError(u'MediaManagerItem.loadList needs to be defined by the plugin') def onNewClick(self): diff --git a/openlp/core/lib/treewidgetwithdnd.py b/openlp/core/lib/treewidgetwithdnd.py index dffb2a428..b1c6acd97 100644 --- a/openlp/core/lib/treewidgetwithdnd.py +++ b/openlp/core/lib/treewidgetwithdnd.py @@ -120,7 +120,7 @@ class TreeWidgetWithDnD(QtGui.QTreeWidget): listing = os.listdir(localFile) for file in listing: files.append(os.path.join(localFile, file)) - Receiver.send_message(u'%s_dnd' % self.mimeDataText, files) + Receiver.send_message(u'%s_dnd' % self.mimeDataText, {'files':files, 'target':self.itemAt(event.pos())}) elif self.allowInternalDnD: event.setDropAction(QtCore.Qt.CopyAction) event.accept() diff --git a/openlp/plugins/custom/lib/mediaitem.py b/openlp/plugins/custom/lib/mediaitem.py index 7006ad561..c7e8c6d50 100644 --- a/openlp/plugins/custom/lib/mediaitem.py +++ b/openlp/plugins/custom/lib/mediaitem.py @@ -99,7 +99,7 @@ class CustomMediaItem(MediaManagerItem): self.searchTextEdit.setCurrentSearchType(Settings().value( u'%s/last search type' % self.settingsSection)) self.config_updated() - def loadList(self, custom_slides): + def loadList(self, custom_slides, target_group=None): # Sort out what custom we want to select after loading the list. self.saveAutoSelectId() self.listView.clear() diff --git a/openlp/plugins/images/lib/mediaitem.py b/openlp/plugins/images/lib/mediaitem.py index 61b3e003b..0f44deff5 100644 --- a/openlp/plugins/images/lib/mediaitem.py +++ b/openlp/plugins/images/lib/mediaitem.py @@ -239,26 +239,35 @@ class ImageMediaItem(MediaManagerItem): self.main_window.finishedProgressBar() Receiver.send_message(u'cursor_normal') - def loadList(self, images, initialLoad=False): + def loadList(self, images, target_group=None, initialLoad=False): """ Add new images to the database. This method is called when adding images using the Add button or DnD. """ - # Ask which group the images should be saved in - self.fillGroupsComboBox(self.choosegroupform.groupComboBox, showTopLevelGroup=False) - if self.choosegroupform.exec_(): - group_id = self.choosegroupform.groupComboBox.itemData( - self.choosegroupform.groupComboBox.currentIndex(), QtCore.Qt.UserRole) - # Save the new images in the database - for filename in images: - if type(filename) is not str and type(filename) is not unicode: - continue - log.debug(u'Adding new image: %s', filename) - imageFile = ImageFilenames() - imageFile.group_id = group_id - imageFile.filename = unicode(filename) - success = self.manager.save_object(imageFile) - self.loadFullList(self.manager.get_all_objects(ImageFilenames, order_by_ref=ImageFilenames.filename), - initialLoad) + if target_group is None: + # Ask which group the images should be saved in + self.fillGroupsComboBox(self.choosegroupform.groupComboBox, showTopLevelGroup=False) + if self.choosegroupform.exec_(): + group_id = self.choosegroupform.groupComboBox.itemData( + self.choosegroupform.groupComboBox.currentIndex(), QtCore.Qt.UserRole) + parent_group = self.manager.get_object_filtered(ImageGroups, ImageGroups.id == group_id) + else: + parent_group = target_group.data(0, QtCore.Qt.UserRole) + if isinstance(parent_group, ImageFilenames): + parent_group = target_group.parent().data(0, QtCore.Qt.UserRole) + # If no valid parent group is found, do nothing + if not isinstance(parent_group, ImageGroups): + return + # Save the new images in the database + for filename in images: + if type(filename) is not str and type(filename) is not unicode: + continue + log.debug(u'Adding new image: %s', filename) + imageFile = ImageFilenames() + imageFile.group_id = parent_group.id + imageFile.filename = unicode(filename) + success = self.manager.save_object(imageFile) + self.loadFullList(self.manager.get_all_objects(ImageFilenames, order_by_ref=ImageFilenames.filename), + initialLoad) def dndMoveInternal(self, target): """ diff --git a/openlp/plugins/media/lib/mediaitem.py b/openlp/plugins/media/lib/mediaitem.py index 4fa94e2a2..3cce9ea03 100644 --- a/openlp/plugins/media/lib/mediaitem.py +++ b/openlp/plugins/media/lib/mediaitem.py @@ -256,7 +256,7 @@ class MediaMediaItem(MediaManagerItem): self.listView.takeTopLevelItem(row) Settings().setValue(self.settingsSection + u'/media files', self.getFileList()) - def loadList(self, media): + def loadList(self, media, target_group=None): # Sort the media by its filename considering language specific # characters. media.sort(cmp=locale_compare, key=lambda filename: os.path.split(unicode(filename))[1]) diff --git a/openlp/plugins/presentations/lib/mediaitem.py b/openlp/plugins/presentations/lib/mediaitem.py index cd2628cf1..2dab04c47 100644 --- a/openlp/plugins/presentations/lib/mediaitem.py +++ b/openlp/plugins/presentations/lib/mediaitem.py @@ -121,7 +121,7 @@ class PresentationMediaItem(MediaManagerItem): """ self.listView.setIconSize(QtCore.QSize(88, 50)) files = Settings().value(self.settingsSection + u'/presentations files') - self.loadList(files, True) + self.loadList(files, initialLoad=True) self.populateDisplayTypes() def populateDisplayTypes(self): @@ -142,7 +142,7 @@ class PresentationMediaItem(MediaManagerItem): else: self.presentationWidget.hide() - def loadList(self, files, initialLoad=False): + def loadList(self, files, target_group=None, initialLoad=False): """ Add presentations into the media manager This is called both on initial load of the plugin to populate with