code standards

This commit is contained in:
Andreas Preikschat 2013-04-19 21:03:16 +02:00
parent dad243dd74
commit b03c3c6f4f
8 changed files with 60 additions and 65 deletions

View File

@ -27,6 +27,6 @@
# Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Temple Place, Suite 330, Boston, MA 02111-1307 USA #
############################################################################### ###############################################################################
""" """
The :mod:`images` module provides the Images plugin. The Images plugin The :mod:`images` module provides the Images plugin. The Images plugin provides the facility to display images from
provides the facility to display images from OpenLP. OpenLP.
""" """

View File

@ -27,20 +27,16 @@
# Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Temple Place, Suite 330, Boston, MA 02111-1307 USA #
############################################################################### ###############################################################################
""" """
Forms in OpenLP are made up of two classes. One class holds all the graphical Forms in OpenLP are made up of two classes. One class holds all the graphical elements, like buttons and lists, and the
elements, like buttons and lists, and the other class holds all the functional other class holds all the functional code, like slots and loading and saving.
code, like slots and loading and saving.
The first class, commonly known as the **Dialog** class, is typically named The first class, commonly known as the **Dialog** class, is typically named ``Ui_<name>Dialog``. It is a slightly
``Ui_<name>Dialog``. It is a slightly modified version of the class that the modified version of the class that the ``pyuic4`` command produces from Qt4's .ui file. Typical modifications will be
``pyuic4`` command produces from Qt4's .ui file. Typical modifications will be converting most strings from "" to u'' and using OpenLP's ``translate()`` function for translating strings.
converting most strings from "" to u'' and using OpenLP's ``translate()``
function for translating strings.
The second class, commonly known as the **Form** class, is typically named The second class, commonly known as the **Form** class, is typically named ``<name>Form``. This class is the one which
``<name>Form``. This class is the one which is instantiated and used. It uses is instantiated and used. It uses dual inheritance to inherit from (usually) QtGui.QDialog and the Ui class mentioned
dual inheritance to inherit from (usually) QtGui.QDialog and the Ui class above, like so::
mentioned above, like so::
class AuthorsForm(QtGui.QDialog, Ui_AuthorsDialog): class AuthorsForm(QtGui.QDialog, Ui_AuthorsDialog):
@ -48,9 +44,8 @@ mentioned above, like so::
QtGui.QDialog.__init__(self, parent) QtGui.QDialog.__init__(self, parent)
self.setupUi(self) self.setupUi(self)
This allows OpenLP to use ``self.object`` for all the GUI elements while keeping This allows OpenLP to use ``self.object`` for all the GUI elements while keeping them separate from the functionality,
them separate from the functionality, so that it is easier to recreate the GUI so that it is easier to recreate the GUI from the .ui files later if necessary.
from the .ui files later if necessary.
""" """
from addgroupform import AddGroupForm from addgroupform import AddGroupForm

View File

@ -47,16 +47,16 @@ class AddGroupForm(QtGui.QDialog, Ui_AddGroupDialog):
def exec_(self, clear=True, show_top_level_group=False, selected_group=None): def exec_(self, clear=True, show_top_level_group=False, selected_group=None):
""" """
Show the form Show the form,
``clear`` ``clear``
Set to False if the text input box should not be cleared when showing the dialog (default: True) Set to False if the text input box should not be cleared when showing the dialog (default: True),
``show_top_level_group`` ``show_top_level_group``
Set to True when "-- Top level group --" should be showed as first item (default: False) Set to True when "-- Top level group --" should be showed as first item (default: False),
``selected_group`` ``selected_group``
The ID of the group that should be selected by default when showing the dialog The ID of the group that should be selected by default when showing the dialog,
""" """
if clear: if clear:
self.name_edit.clear() self.name_edit.clear()
@ -72,7 +72,7 @@ class AddGroupForm(QtGui.QDialog, Ui_AddGroupDialog):
def accept(self): def accept(self):
""" """
Override the accept() method from QDialog to make sure something is entered in the text input box Override the accept() method from QDialog to make sure something is entered in the text input box.
""" """
if not self.name_edit.text(): if not self.name_edit.text():
critical_error_message_box(message=translate('ImagePlugin.AddGroupForm', critical_error_message_box(message=translate('ImagePlugin.AddGroupForm',

View File

@ -48,10 +48,10 @@ class ChooseGroupForm(QtGui.QDialog, Ui_ChooseGroupDialog):
Show the form Show the form
``selected_group`` ``selected_group``
The ID of the group that should be selected by default when showing the dialog The ID of the group that should be selected by default when showing the dialog.
""" """
if selected_group is not None: if selected_group is not None:
for i in range(self.group_combobox.count()): for index in range(self.group_combobox.count()):
if self.group_combobox.itemData(i) == selected_group: if self.group_combobox.itemData(index) == selected_group:
self.group_combobox.setCurrentIndex(i) self.group_combobox.setCurrentIndex(index)
return QtGui.QDialog.exec_(self) return QtGui.QDialog.exec_(self)

View File

@ -70,10 +70,10 @@ class ImagePlugin(Plugin):
def app_startup(self): def app_startup(self):
""" """
Perform tasks on application startup Perform tasks on application startup.
""" """
Plugin.app_startup(self) Plugin.app_startup(self)
# Convert old settings-based image list to the database # Convert old settings-based image list to the database.
files_from_config = Settings().get_files_from_config(self) files_from_config = Settings().get_files_from_config(self)
if files_from_config: if files_from_config:
log.debug(u'Importing images list from old config: %s' % files_from_config) log.debug(u'Importing images list from old config: %s' % files_from_config)
@ -93,7 +93,7 @@ class ImagePlugin(Plugin):
def set_plugin_text_strings(self): def set_plugin_text_strings(self):
""" """
Called to define all translatable texts of the plugin Called to define all translatable texts of the plugin.
""" """
## Name PluginList ## ## Name PluginList ##
self.text_strings[StringContent.Name] = { self.text_strings[StringContent.Name] = {
@ -117,8 +117,8 @@ class ImagePlugin(Plugin):
def config_update(self): def config_update(self):
""" """
Triggered by saving and changing the image border. Sets the images in image manager to require updates. Triggered by saving and changing the image border. Sets the images in image manager to require updates. Actual
Actual update is triggered by the last part of saving the config. update is triggered by the last part of saving the config.
""" """
log.info(u'Images config_update') log.info(u'Images config_update')
background = QtGui.QColor(Settings().value(self.settings_section + u'/background color')) background = QtGui.QColor(Settings().value(self.settings_section + u'/background color'))

View File

@ -27,7 +27,7 @@
# Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Temple Place, Suite 330, Boston, MA 02111-1307 USA #
############################################################################### ###############################################################################
""" """
The :mod:`db` module provides the database and schema that is the backend for the Images plugin The :mod:`db` module provides the database and schema that is the backend for the Images plugin.
""" """
from sqlalchemy import Column, ForeignKey, Table, types from sqlalchemy import Column, ForeignKey, Table, types
@ -38,14 +38,14 @@ from openlp.core.lib.db import BaseModel, init_db
class ImageGroups(BaseModel): class ImageGroups(BaseModel):
""" """
ImageGroups model ImageGroups model.
""" """
pass pass
class ImageFilenames(BaseModel): class ImageFilenames(BaseModel):
""" """
ImageFilenames model ImageFilenames model.
""" """
pass pass

View File

@ -40,6 +40,7 @@ from openlp.core.utils import AppLocation, delete_file, get_locale_key, get_imag
from openlp.plugins.images.forms import AddGroupForm, ChooseGroupForm from openlp.plugins.images.forms import AddGroupForm, ChooseGroupForm
from openlp.plugins.images.lib.db import ImageFilenames, ImageGroups from openlp.plugins.images.lib.db import ImageFilenames, ImageGroups
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -60,12 +61,11 @@ class ImageMediaItem(MediaManagerItem):
self.fill_groups_combobox(self.choose_group_form.group_combobox) self.fill_groups_combobox(self.choose_group_form.group_combobox)
self.fill_groups_combobox(self.add_group_form.parent_group_combobox) self.fill_groups_combobox(self.add_group_form.parent_group_combobox)
Registry().register_function(u'live_theme_changed', self.live_theme_changed) Registry().register_function(u'live_theme_changed', self.live_theme_changed)
# Allow DnD from the desktop # Allow DnD from the desktop.
self.list_view.activateDnD() self.list_view.activateDnD()
def retranslateUi(self): def retranslateUi(self):
self.on_new_prompt = translate('ImagePlugin.MediaItem', self.on_new_prompt = translate('ImagePlugin.MediaItem', 'Select Image(s)')
'Select Image(s)')
file_formats = get_images_filter() file_formats = get_images_filter()
self.on_new_file_masks = u'%s;;%s (*.*) (*)' % (file_formats, UiStrings().AllFiles) self.on_new_file_masks = u'%s;;%s (*.*) (*)' % (file_formats, UiStrings().AllFiles)
self.addGroupAction.setText(UiStrings().AddGroup) self.addGroupAction.setText(UiStrings().AddGroup)
@ -77,7 +77,7 @@ class ImageMediaItem(MediaManagerItem):
def required_icons(self): def required_icons(self):
""" """
Set which icons the media manager tab should show Set which icons the media manager tab should show.
""" """
MediaManagerItem.required_icons(self) MediaManagerItem.required_icons(self)
self.has_file_icon = True self.has_file_icon = True
@ -99,8 +99,8 @@ class ImageMediaItem(MediaManagerItem):
def add_list_view_to_toolbar(self): def add_list_view_to_toolbar(self):
""" """
Creates the main widget for listing items the media item is tracking. Creates the main widget for listing items the media item is tracking. This method overloads
This method overloads MediaManagerItem.add_list_view_to_toolbar MediaManagerItem.add_list_view_to_toolbar.
""" """
# Add the List widget # Add the List widget
self.list_view = TreeWidgetWithDnD(self, self.plugin.name) self.list_view = TreeWidgetWithDnD(self, self.plugin.name)
@ -159,21 +159,18 @@ class ImageMediaItem(MediaManagerItem):
def add_custom_context_actions(self): def add_custom_context_actions(self):
""" """
Add custom actions to the context menu Add custom actions to the context menu.
""" """
create_widget_action(self.list_view, separator=True) create_widget_action(self.list_view, separator=True)
create_widget_action(self.list_view, create_widget_action(self.list_view,
text=UiStrings().AddGroup, text=UiStrings().AddGroup, icon=u':/images/image_new_group.png', triggers=self.onAddGroupClick)
icon=u':/images/image_new_group.png',
triggers=self.onAddGroupClick)
create_widget_action(self.list_view, create_widget_action(self.list_view,
text=self.plugin.get_string(StringContent.Load)[u'tooltip'], text=self.plugin.get_string(StringContent.Load)[u'tooltip'],
icon=u':/general/general_open.png', icon=u':/general/general_open.png', triggers=self.on_file_click)
triggers=self.on_file_click)
def add_start_header_bar(self): def add_start_header_bar(self):
""" """
Add custom buttons to the start of the toolbar Add custom buttons to the start of the toolbar.
""" """
self.addGroupAction = self.toolbar.add_toolbar_action(u'addGroupAction', self.addGroupAction = self.toolbar.add_toolbar_action(u'addGroupAction',
icon=u':/images/image_new_group.png', triggers=self.onAddGroupClick) icon=u':/images/image_new_group.png', triggers=self.onAddGroupClick)
@ -189,10 +186,10 @@ class ImageMediaItem(MediaManagerItem):
def recursively_delete_group(self, image_group): def recursively_delete_group(self, image_group):
""" """
Recursively deletes a group and all groups and images in it Recursively deletes a group and all groups and images in it.
``image_group`` ``image_group``
The ImageGroups instance of the group that will be deleted The ImageGroups instance of the group that will be deleted.
""" """
images = self.manager.get_all_objects(ImageFilenames, ImageFilenames.group_id == image_group.id) images = self.manager.get_all_objects(ImageFilenames, ImageFilenames.group_id == image_group.id)
for image in images: for image in images:
@ -205,7 +202,7 @@ class ImageMediaItem(MediaManagerItem):
def on_delete_click(self): def on_delete_click(self):
""" """
Remove an image item from the list Remove an image item from the list.
""" """
# Turn off auto preview triggers. # Turn off auto preview triggers.
self.list_view.blockSignals(True) self.list_view.blockSignals(True)
@ -226,11 +223,11 @@ class ImageMediaItem(MediaManagerItem):
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)
elif isinstance(item_data, ImageGroups): elif isinstance(item_data, ImageGroups):
if QtGui.QMessageBox.question(self.list_view.parent(), if QtGui.QMessageBox.question(self.list_view.parent(),
translate('ImagePlugin.MediaItem', 'Remove group'), translate('ImagePlugin.MediaItem', 'Remove group'),
translate('ImagePlugin.MediaItem', translate('ImagePlugin.MediaItem',
'Are you sure you want to remove "%s" and everything in it?') % item_data.group_name, 'Are you sure you want to remove "%s" and everything in it?') % item_data.group_name,
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes | QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No)) == QtGui.QMessageBox.Yes: QtGui.QMessageBox.No)) == QtGui.QMessageBox.Yes:
self.recursively_delete_group(item_data) self.recursively_delete_group(item_data)
self.manager.delete_object(ImageGroups, row_item.data(0, QtCore.Qt.UserRole).id) self.manager.delete_object(ImageGroups, row_item.data(0, QtCore.Qt.UserRole).id)
if item_data.parent_id == 0: if item_data.parent_id == 0:
@ -246,13 +243,13 @@ class ImageMediaItem(MediaManagerItem):
def add_sub_groups(self, group_list, parent_group_id): def add_sub_groups(self, group_list, parent_group_id):
""" """
Recursively add subgroups to the given parent group in a QTreeWidget Recursively add subgroups to the given parent group in a QTreeWidget.
``group_list`` ``group_list``
The List object that contains all QTreeWidgetItems The List object that contains all QTreeWidgetItems.
``parent_group_id`` ``parent_group_id``
The ID of the group that will be added recursively The ID of the group that will be added recursively.
""" """
image_groups = self.manager.get_all_objects(ImageGroups, ImageGroups.parent_id == parent_group_id) image_groups = self.manager.get_all_objects(ImageGroups, ImageGroups.parent_id == parent_group_id)
image_groups.sort(key=lambda group_object: get_locale_key(group_object.group_name)) image_groups.sort(key=lambda group_object: get_locale_key(group_object.group_name))
@ -271,16 +268,16 @@ class ImageMediaItem(MediaManagerItem):
def fill_groups_combobox(self, combobox, parent_group_id=0, prefix=''): def fill_groups_combobox(self, combobox, parent_group_id=0, prefix=''):
""" """
Recursively add groups to the combobox in the 'Add group' dialog Recursively add groups to the combobox in the 'Add group' dialog.
``combobox`` ``combobox``
The QComboBox to add the options to The QComboBox to add the options to.
``parent_group_id`` ``parent_group_id``
The ID of the group that will be added The ID of the group that will be added.
``prefix`` ``prefix``
A string containing the prefix that will be added in front of the groupname for each level of the tree A string containing the prefix that will be added in front of the groupname for each level of the tree.
""" """
if parent_group_id == 0: if parent_group_id == 0:
combobox.clear() combobox.clear()
@ -293,13 +290,13 @@ class ImageMediaItem(MediaManagerItem):
def expand_group(self, group_id, root_item=None): def expand_group(self, group_id, root_item=None):
""" """
Expand groups in the widget recursively Expand groups in the widget recursively.
``group_id`` ``group_id``
The ID of the group that will be expanded The ID of the group that will be expanded.
``root_item`` ``root_item``
This option is only used for recursion purposes This option is only used for recursion purposes.
""" """
return_value = False return_value = False
if root_item is None: if root_item is None:

View File

@ -39,14 +39,17 @@ from openlp.core.ui import DisplayController, Display, DisplayControllerType
from openlp.core.ui.media import get_media_players, set_media_players from openlp.core.ui.media import get_media_players, set_media_players
from openlp.core.utils import AppLocation, get_locale_key from openlp.core.utils import AppLocation, get_locale_key
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
CLAPPERBOARD = u':/media/slidecontroller_multimedia.png' CLAPPERBOARD = u':/media/slidecontroller_multimedia.png'
VIDEO = build_icon(QtGui.QImage(u':/media/media_video.png')) VIDEO = build_icon(QtGui.QImage(u':/media/media_video.png'))
AUDIO = build_icon(QtGui.QImage(u':/media/media_audio.png')) AUDIO = build_icon(QtGui.QImage(u':/media/media_audio.png'))
DVDICON = build_icon(QtGui.QImage(u':/media/media_video.png')) DVDICON = build_icon(QtGui.QImage(u':/media/media_video.png'))
ERROR = build_icon(QtGui.QImage(u':/general/general_delete.png')) ERROR = build_icon(QtGui.QImage(u':/general/general_delete.png'))
class MediaMediaItem(MediaManagerItem): class MediaMediaItem(MediaManagerItem):
""" """
This is the custom media manager item for Media Slides. This is the custom media manager item for Media Slides.