Implemented recursive group deleting and fixed a bug in adding new images into a group

This commit is contained in:
Arjan Schrijver 2013-01-25 15:22:52 +01:00
parent 3c3b7bb294
commit 140250576b

View File

@ -115,6 +115,19 @@ class ImageMediaItem(MediaManagerItem):
self.resetAction = self.toolbar.addToolbarAction(u'resetAction', self.resetAction = self.toolbar.addToolbarAction(u'resetAction',
icon=u':/system/system_close.png', visible=False, triggers=self.onResetClick) icon=u':/system/system_close.png', visible=False, triggers=self.onResetClick)
def recursivelyDeleteGroup(self, image_group):
"""
Recursively deletes a group and all groups and images in it
"""
images = self.manager.get_all_objects(ImageFilenames, ImageFilenames.group_id == image_group.id)
for image in images:
delete_file(os.path.join(self.servicePath, os.path.split(image.filename)[1]))
self.manager.delete_object(ImageFilenames, image.id)
image_groups = self.manager.get_all_objects(ImageGroups, ImageGroups.parent_id == image_group.id)
for group in image_groups:
self.recursivelyDeleteGroup(group)
self.manager.delete_object(ImageGroups, group.id)
def onDeleteClick(self): def onDeleteClick(self):
""" """
Remove an image item from the list Remove an image item from the list
@ -129,11 +142,24 @@ class ImageMediaItem(MediaManagerItem):
for row_item in item_list: for row_item in item_list:
row_item = self.listView.topLevelItem(row) row_item = self.listView.topLevelItem(row)
if row_item: if row_item:
item_data = row_item.data(0, QtCore.Qt.UserRole)
if isinstance(item_data, ImageFilenames):
delete_file(os.path.join(self.servicePath, row_item.text(0))) delete_file(os.path.join(self.servicePath, row_item.text(0)))
row_item.parent().removeChild(row_item) row_item.parent().removeChild(row_item)
self.main_window.incrementProgressBar()
self.manager.delete_object(ImageFilenames, row_item.data(0, QtCore.Qt.UserRole).id) self.manager.delete_object(ImageFilenames, row_item.data(0, QtCore.Qt.UserRole).id)
SettingsManager.setValue(self.settingsSection + u'/images files', self.getFileList()) elif isinstance(item_data, ImageGroups):
if QtGui.QMessageBox.question(self.listView.parent(),
translate('ImagePlugin.MediaItem', 'Remove group'),
translate('ImagePlugin.MediaItem',
'Are you sure you want to remove "%s" and everything in it?') % item_data.group_name,
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)) == QtGui.QMessageBox.Yes:
self.recursivelyDeleteGroup(item_data)
self.manager.delete_object(ImageGroups, row_item.data(0, QtCore.Qt.UserRole).id)
if item_data.parent_id is 0:
self.listView.takeTopLevelItem(self.listView.indexOfTopLevelItem(row_item))
else:
row_item.parent().removeChild(row_item)
self.main_window.incrementProgressBar()
self.main_window.finishedProgressBar() self.main_window.finishedProgressBar()
Receiver.send_message(u'cursor_normal') Receiver.send_message(u'cursor_normal')
self.listView.blockSignals(False) self.listView.blockSignals(False)
@ -147,6 +173,7 @@ class ImageMediaItem(MediaManagerItem):
for image_group in image_groups: for image_group in image_groups:
group = QtGui.QTreeWidgetItem() group = QtGui.QTreeWidgetItem()
group.setText(0, image_group.group_name) group.setText(0, image_group.group_name)
group.setData(0, QtCore.Qt.UserRole, image_group)
if parentGroupId is 0: if parentGroupId is 0:
self.listView.addTopLevelItem(group) self.listView.addTopLevelItem(group)
else: else:
@ -224,8 +251,9 @@ class ImageMediaItem(MediaManagerItem):
self.choosegroupform.groupComboBox.currentIndex(), QtCore.Qt.UserRole) self.choosegroupform.groupComboBox.currentIndex(), QtCore.Qt.UserRole)
# Save the new images in the database # Save the new images in the database
for filename in images: for filename in images:
if filename is None: if type(filename) is not str and type(filename) is not unicode:
continue continue
log.debug(u'Adding new image: %s', filename)
imageFile = ImageFilenames() imageFile = ImageFilenames()
imageFile.group_id = group_id imageFile.group_id = group_id
imageFile.filename = unicode(filename) imageFile.filename = unicode(filename)
@ -323,7 +351,8 @@ class ImageMediaItem(MediaManagerItem):
group_name=self.addgroupform.nameEdit.text()) group_name=self.addgroupform.nameEdit.text())
if self.checkGroupName(new_group): if self.checkGroupName(new_group):
if self.manager.save_object(new_group): if self.manager.save_object(new_group):
self.loadList([]) self.loadFullList(self.manager.get_all_objects(ImageFilenames,
order_by_ref=ImageFilenames.filename))
self.fillGroupsComboBox(self.addgroupform.parentGroupComboBox) self.fillGroupsComboBox(self.addgroupform.parentGroupComboBox)
else: else:
critical_error_message_box( critical_error_message_box(