Refactor check_item_selected

This commit is contained in:
Jon Tibble 2010-06-24 16:50:40 +01:00
parent ca7f3eb599
commit 2c42aa0289
8 changed files with 39 additions and 30 deletions

View File

@ -159,6 +159,22 @@ def resize_image(image, width, height):
painter.drawImage((width - realw) / 2, (height - realh) / 2, preview) painter.drawImage((width - realw) / 2, (height - realh) / 2, preview)
return new_image return new_image
def check_item_selected(list_widget, message):
"""
Check if a list item is selected so an action may be performed on it
``list_widget``
The list to check for selected items
``message``
The message to give the user if no item is selected
"""
if not list_widget.selectedIndexes():
QtGui.QMessageBox.information(self,
translate('MediaManagerItem', 'No Items Selected'), message)
return False
return True
class ThemeLevel(object): class ThemeLevel(object):
""" """

View File

@ -343,19 +343,6 @@ class MediaManagerItem(QtGui.QWidget):
""" """
pass pass
def checkItemSelected(self, message):
"""
Check if a list item is selected so an action may be performed on it
``message``
The message to give the user if no item is selected
"""
if not self.ListView.selectedIndexes():
QtGui.QMessageBox.information(self,
translate('MediaManagerItem', 'No Items Selected'), message)
return False
return True
def onFileClick(self): def onFileClick(self):
files = QtGui.QFileDialog.getOpenFileNames( files = QtGui.QFileDialog.getOpenFileNames(
self, self.OnNewPrompt, self, self.OnNewPrompt,

View File

@ -35,7 +35,7 @@ from openlp.core.ui import AmendThemeForm
from openlp.core.theme import Theme from openlp.core.theme import Theme
from openlp.core.lib import OpenLPToolbar, context_menu_action, \ from openlp.core.lib import OpenLPToolbar, context_menu_action, \
ThemeXML, str_to_bool, get_text_file_string, build_icon, Receiver, \ ThemeXML, str_to_bool, get_text_file_string, build_icon, Receiver, \
context_menu_separator, SettingsManager, translate context_menu_separator, SettingsManager, translate, check_item_selected
from openlp.core.utils import AppLocation, get_filesystem_encoding from openlp.core.utils import AppLocation, get_filesystem_encoding
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -182,8 +182,9 @@ class ThemeManager(QtGui.QWidget):
Loads the settings for the theme that is to be edited and launches the Loads the settings for the theme that is to be edited and launches the
theme editing form so the user can make their changes. theme editing form so the user can make their changes.
""" """
if check_item_selected(self.ThemeListWidget, translate('ThemeManager',
'You must select a theme to edit.')):
item = self.ThemeListWidget.currentItem() item = self.ThemeListWidget.currentItem()
if item:
theme = self.getThemeData( theme = self.getThemeData(
unicode(item.data(QtCore.Qt.UserRole).toString())) unicode(item.data(QtCore.Qt.UserRole).toString()))
self.amendThemeForm.loadTheme(theme) self.amendThemeForm.loadTheme(theme)
@ -198,8 +199,9 @@ class ThemeManager(QtGui.QWidget):
self.global_theme = unicode(QtCore.QSettings().value( self.global_theme = unicode(QtCore.QSettings().value(
self.settingsSection + u'/global theme', self.settingsSection + u'/global theme',
QtCore.QVariant(u'')).toString()) QtCore.QVariant(u'')).toString())
if check_item_selected(self.ThemeListWidget, translate('ThemeManager',
'You must select a theme to delete.')):
item = self.ThemeListWidget.currentItem() item = self.ThemeListWidget.currentItem()
if item:
theme = unicode(item.text()) theme = unicode(item.text())
# should be the same unless default # should be the same unless default
if theme != unicode(item.data(QtCore.Qt.UserRole).toString()): if theme != unicode(item.data(QtCore.Qt.UserRole).toString()):

View File

@ -28,7 +28,7 @@ import logging
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from openlp.core.lib import MediaManagerItem, SongXMLParser, BaseListWithDnD, \ from openlp.core.lib import MediaManagerItem, SongXMLParser, BaseListWithDnD, \
Receiver, ItemCapabilities, translate Receiver, ItemCapabilities, translate, check_item_selected
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -118,7 +118,8 @@ class CustomMediaItem(MediaManagerItem):
""" """
Edit a custom item Edit a custom item
""" """
if self.checkItemSelected(translate('CustomPlugin.MediaItem', if check_item_selected(self.ListView,
translate('CustomPlugin.MediaItem',
'You must select an item to edit.')): 'You must select an item to edit.')):
item = self.ListView.currentItem() item = self.ListView.currentItem()
item_id = (item.data(QtCore.Qt.UserRole)).toInt()[0] item_id = (item.data(QtCore.Qt.UserRole)).toInt()[0]
@ -130,7 +131,8 @@ class CustomMediaItem(MediaManagerItem):
""" """
Remove a custom item from the list and database Remove a custom item from the list and database
""" """
if self.checkItemSelected(translate('CustomPlugin.MediaItem', if check_item_selected(self.ListView,
translate('CustomPlugin.MediaItem',
'You must select an item to delete.')): 'You must select an item to delete.')):
item = self.ListView.currentItem() item = self.ListView.currentItem()
item_id = (item.data(QtCore.Qt.UserRole)).toInt()[0] item_id = (item.data(QtCore.Qt.UserRole)).toInt()[0]

View File

@ -29,7 +29,8 @@ import os
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from openlp.core.lib import MediaManagerItem, BaseListWithDnD, build_icon, \ from openlp.core.lib import MediaManagerItem, BaseListWithDnD, build_icon, \
context_menu_action, ItemCapabilities, SettingsManager, translate context_menu_action, ItemCapabilities, SettingsManager, translate, \
check_item_selected
from openlp.core.utils import AppLocation, get_images_filter from openlp.core.utils import AppLocation, get_images_filter
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -116,7 +117,7 @@ class ImageMediaItem(MediaManagerItem):
""" """
Remove an image item from the list Remove an image item from the list
""" """
if self.checkItemSelected(translate('ImagePlugin.MediaItem', if check_item_selected(self.ListView, translate('ImagePlugin.MediaItem',
'You must select an item to delete.')): 'You must select an item to delete.')):
items = self.ListView.selectedIndexes() items = self.ListView.selectedIndexes()
for item in items: for item in items:

View File

@ -29,7 +29,7 @@ import os
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from openlp.core.lib import MediaManagerItem, BaseListWithDnD, build_icon, \ from openlp.core.lib import MediaManagerItem, BaseListWithDnD, build_icon, \
ItemCapabilities, SettingsManager, translate ItemCapabilities, SettingsManager, translate, check_item_selected
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -141,7 +141,7 @@ class MediaMediaItem(MediaManagerItem):
""" """
Remove a media item from the list Remove a media item from the list
""" """
if self.checkItemSelected(translate('MediaPlugin.MediaItem', if check_item_selected(self.ListView, translate('MediaPlugin.MediaItem',
'You must select an item to delete.')): 'You must select an item to delete.')):
item = self.ListView.currentItem() item = self.ListView.currentItem()
row = self.ListView.row(item) row = self.ListView.row(item)

View File

@ -29,7 +29,7 @@ import os
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from openlp.core.lib import MediaManagerItem, BaseListWithDnD, build_icon, \ from openlp.core.lib import MediaManagerItem, BaseListWithDnD, build_icon, \
SettingsManager, translate SettingsManager, translate, check_item_selected
from openlp.core.utils import AppLocation from openlp.core.utils import AppLocation
from openlp.plugins.presentations.lib import MessageListener from openlp.plugins.presentations.lib import MessageListener
@ -177,7 +177,8 @@ class PresentationMediaItem(MediaManagerItem):
""" """
Remove a presentation item from the list Remove a presentation item from the list
""" """
if self.checkItemSelected(translate('PresentationPlugin.MediaItem', if check_item_selected(self.ListView,
translate('PresentationPlugin.MediaItem',
'You must select an item to delete.')): 'You must select an item to delete.')):
item = self.ListView.currentItem() item = self.ListView.currentItem()
row = self.ListView.row(item) row = self.ListView.row(item)

View File

@ -28,7 +28,7 @@ import logging
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from openlp.core.lib import MediaManagerItem, SongXMLParser, \ from openlp.core.lib import MediaManagerItem, SongXMLParser, \
BaseListWithDnD, Receiver, ItemCapabilities, translate BaseListWithDnD, Receiver, ItemCapabilities, translate, check_item_selected
from openlp.plugins.songs.forms import EditSongForm, SongMaintenanceForm, \ from openlp.plugins.songs.forms import EditSongForm, SongMaintenanceForm, \
ImportWizardForm ImportWizardForm
@ -279,7 +279,7 @@ class SongMediaItem(MediaManagerItem):
""" """
Edit a song Edit a song
""" """
if self.checkItemSelected(translate('SongsPlugin.MediaItem', if check_item_selected(self.ListView, translate('SongsPlugin.MediaItem',
'You must select an item to edit.')): 'You must select an item to edit.')):
item = self.ListView.currentItem() item = self.ListView.currentItem()
item_id = (item.data(QtCore.Qt.UserRole)).toInt()[0] item_id = (item.data(QtCore.Qt.UserRole)).toInt()[0]
@ -290,7 +290,7 @@ class SongMediaItem(MediaManagerItem):
""" """
Remove a song from the list and database Remove a song from the list and database
""" """
if self.checkItemSelected(translate('SongsPlugin.MediaItem', if check_item_selected(self.ListView, translate('SongsPlugin.MediaItem',
'You must select an item to delete.')): 'You must select an item to delete.')):
items = self.ListView.selectedIndexes() items = self.ListView.selectedIndexes()
if len(items) == 1: if len(items) == 1: