← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~m2j/openlp/cleanups into lp:openlp

 

Meinert Jordan has proposed merging lp:~m2j/openlp/cleanups into lp:openlp.

Requested reviews:
  OpenLP Core (openlp-core)

For more details, see:
https://code.launchpad.net/~m2j/openlp/cleanups/+merge/94692

This are some cleanups. 

There were five methods to create a action in OpenLP, so I thought: Why not add a sixth method. ;)
Well, I was a bit surprised of the freeze as I was just about merging my editior into the OpenLP source tree. Therefore I didn't had the chance yet to go through the sources and replace the current action creation methods where it makes sense.

Some paths are handled through Pythons os.path instead of Qts methods (we use os.path at most places of the code so it is more consistent)
The path seperator in zip files is always a slash (/): http://www.pkware.com/documents/casestudies/APPNOTE.TXT
-- 
https://code.launchpad.net/~m2j/openlp/cleanups/+merge/94692
Your team OpenLP Core is requested to review the proposed merge of lp:~m2j/openlp/cleanups into lp:openlp.
=== modified file 'openlp/core/lib/ui.py'
--- openlp/core/lib/ui.py	2011-12-27 10:33:55 +0000
+++ openlp/core/lib/ui.py	2012-02-26 21:28:17 +0000
@@ -280,101 +280,104 @@
         QtCore.SIGNAL(u'clicked()'), parent.onDownButtonClicked)
     return up_button, down_button
 
-def base_action(parent, name, category=None):
+def create_action(parent, name, text=u'', icon=None, tooltip=u'', statustip=u'',
+    checked=None, enabled=True, visible=True, shortcuts=None,
+    context=QtCore.Qt.WindowShortcut, category=None, trigger=None):
     """
-    Return the most basic action with the object name set.
+    Return an action with the object name set and the given parameters.
+    Parameters with default values are not set in case they stay unchanged.
+
+    ``parent``
+        A QtCore.QObject or ``None`` for the actions parent.
+
+    ``name``
+        A string which is set as object name.
+
+    ``text``
+        A string for the action text.
+
+    ``icon``
+        Either a QIcon, a resource string, or a file location string for the
+        action icon.
+
+    ``tooltip``
+        A string for the action tool tip.
+
+    ``statustip``
+        A string for the action status tip.
+
+    ``checked``
+        Either ``None`` for a not checkable action or bool for the state.
+
+    ``enabled``
+        False in case the action should be disabled.
+
+    ``visible``
+        False in case the action should be hidden.
+
+    ``shortcuts``
+        A QList<QKeySequence> (or a list of strings) which are set as shortcuts.
+
+    ``context``
+        A context for the shortcut execution (only will be set if ``shortcuts``
+        is not None)
 
     ``category``
         The category the action should be listed in the shortcut dialog. If you
         not wish, that this action is added to the shortcut dialog, then do not
         state any.
+
+    ``trigger``
+       A slot which is connected to the actions ``triggered()`` slot.
     """
     action = QtGui.QAction(parent)
     action.setObjectName(name)
+    if text:
+        action.setText(text)
+    if icon is not None:
+        action.setIcon(build_icon(icon))
+    if tooltip:
+        action.setToolTip(tooltip)
+    if statustip:
+        action.setStatusTip(statustip)
+    if checked is not None:
+        action.setCheckable(True)
+        action.setChecked(checked)
+    if not enabled:
+        action.setEnabled(enabled)
+    if not visible:
+        action.setVisible(visible)
+    if shortcuts is not None:
+        action.setShortcuts(shortcuts)
+        action.setShortcutContext(context)
     if category is not None:
         action_list = ActionList.get_instance()
         action_list.add_action(action, category)
+    if trigger is not None:
+        QtCore.QObject.connect(action, QtCore.SIGNAL(u'triggered(bool)'),
+            trigger)
     return action
 
+def base_action(parent, name, category=None):
+    return create_action(parent, name, category=category)
+
 def checkable_action(parent, name, checked=None, category=None):
-    """
-    Return a standard action with the checkable attribute set.
-    """
-    action = base_action(parent, name, category)
-    action.setCheckable(True)
-    if checked is not None:
-        action.setChecked(checked)
-    return action
+    return create_action(parent, name, checked=bool(checked), category=category)
 
 def icon_action(parent, name, icon, checked=None, category=None):
-    """
-    Return a standard action with an icon.
-    """
-    if checked is not None:
-        action = checkable_action(parent, name, checked, category)
-    else:
-        action = base_action(parent, name, category)
-    action.setIcon(build_icon(icon))
-    return action
+    return create_action(parent, name, icon=icon, checked=checked,
+        category=category)
 
 def shortcut_action(parent, name, shortcuts, function, icon=None, checked=None,
     category=None, context=QtCore.Qt.WindowShortcut):
-    """
-    Return a shortcut enabled action.
-    """
-    action = QtGui.QAction(parent)
-    action.setObjectName(name)
-    if icon is not None:
-        action.setIcon(build_icon(icon))
-    if checked is not None:
-        action.setCheckable(True)
-        action.setChecked(checked)
-    if shortcuts:
-        action.setShortcuts(shortcuts)
-        action.setShortcutContext(context)
-    action_list = ActionList.get_instance()
-    action_list.add_action(action, category)
-    QtCore.QObject.connect(action, QtCore.SIGNAL(u'triggered(bool)'), function)
-    return action
+    return create_action(parent, name, icon=icon, checked=checked,
+        shortcuts=shortcuts, context=context, category=category,
+        trigger=function)
 
 def context_menu_action(base, icon, text, slot, shortcuts=None, category=None,
     context=QtCore.Qt.WidgetShortcut):
-    """
-    Utility method to help build context menus.
-
-    ``base``
-        The parent menu to add this menu item to
-
-    ``icon``
-        An icon for this action
-
-    ``text``
-        The text to display for this action
-
-    ``slot``
-        The code to run when this action is triggered
-
-    ``shortcuts``
-        The action's shortcuts.
-
-    ``category``
-        The category the shortcut should be listed in the shortcut dialog. If
-        left to ``None``, then the action will be hidden in the shortcut dialog.
-
-    ``context``
-        The context the shortcut is valid.
-    """
-    action = QtGui.QAction(text, base)
-    if icon:
-        action.setIcon(build_icon(icon))
-    QtCore.QObject.connect(action, QtCore.SIGNAL(u'triggered(bool)'), slot)
-    if shortcuts is not None:
-        action.setShortcuts(shortcuts)
-        action.setShortcutContext(context)
-        action_list = ActionList.get_instance()
-        action_list.add_action(action)
-    base.addAction(action)
-    return action
+    return create_action(parent=base, name=u'', icon=icon, text=text,
+        trigger=slot, shortcuts=shortcuts, category=category, context=context)
 
 def context_menu(base, icon, text):
     """

=== modified file 'openlp/core/ui/exceptionform.py'
--- openlp/core/ui/exceptionform.py	2012-01-18 13:50:06 +0000
+++ openlp/core/ui/exceptionform.py	2012-02-26 21:28:17 +0000
@@ -150,7 +150,7 @@
             translate('OpenLP.ExceptionForm',
             'Text files (*.txt *.log *.text)'))
         if filename:
-            filename = unicode(QtCore.QDir.toNativeSeparators(filename))
+            filename = unicode(filename).replace(u'/', os.path.sep)
             SettingsManager.set_last_dir(self.settingsSection, os.path.dirname(
                 filename))
             report_text = report_text % self._createReport()

=== modified file 'openlp/core/ui/mainwindow.py'
--- openlp/core/ui/mainwindow.py	2012-02-06 17:35:41 +0000
+++ openlp/core/ui/mainwindow.py	2012-02-26 21:28:17 +0000
@@ -1378,14 +1378,14 @@
         recentFileCount = QtCore.QSettings().value(
             u'advanced/recent file count', QtCore.QVariant(4)).toInt()[0]
         existingRecentFiles = [recentFile for recentFile in self.recentFiles
-            if QtCore.QFile.exists(recentFile)]
+            if os.path.isfile(unicode(recentFile))]
         recentFilesToDisplay = existingRecentFiles[0:recentFileCount]
         self.recentFilesMenu.clear()
         for fileId, filename in enumerate(recentFilesToDisplay):
             log.debug('Recent file name: %s', filename)
             action =  base_action(self, u'')
-            action.setText(u'&%d %s' %
-                (fileId + 1, QtCore.QFileInfo(filename).fileName()))
+            action.setText(u'&%d %s' % (fileId + 1,
+                os.path.splitext(os.path.basename(unicode(filename)))[0]))
             action.setData(QtCore.QVariant(filename))
             self.connect(action, QtCore.SIGNAL(u'triggered()'),
                 self.serviceManagerContents.onRecentServiceClicked)

=== modified file 'openlp/core/ui/servicemanager.py'
--- openlp/core/ui/servicemanager.py	2012-02-07 22:07:24 +0000
+++ openlp/core/ui/servicemanager.py	2012-02-26 21:28:17 +0000
@@ -683,7 +683,7 @@
                         'File is not a valid service.\n'
                         'The content encoding is not UTF-8.'))
                     continue
-                osfile = unicode(QtCore.QDir.toNativeSeparators(ucsfile))
+                osfile = ucsfile.replace(u'/', os.path.sep)
                 if not osfile.startswith(u'audio'):
                     osfile = os.path.split(osfile)[1]
                 log.debug(u'Extract file: %s', osfile)

=== modified file 'openlp/core/ui/thememanager.py'
--- openlp/core/ui/thememanager.py	2012-01-18 13:50:06 +0000
+++ openlp/core/ui/thememanager.py	2012-02-26 21:28:17 +0000
@@ -544,7 +544,7 @@
                         log.exception(u'Theme file contains non utf-8 filename'
                             u' "%s"' % name.decode(u'utf-8', u'replace'))
                         raise Exception(u'validation')
-                    uname = unicode(QtCore.QDir.toNativeSeparators(uname))
+                    uname = uname.replace(u'/', os.path.sep)
                     splitname = uname.split(os.path.sep)
                     if splitname[-1] == u'' or len(splitname) == 1:
                         # is directory or preview file


Follow ups