← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~googol/openlp/thumb-creation into lp:openlp

 

Andreas Preikschat has proposed merging lp:~googol/openlp/thumb-creation into lp:openlp.

Requested reviews:
  OpenLP Core (openlp-core)

For more details, see:
https://code.launchpad.net/~googol/openlp/thumb-creation/+merge/77794

Hello,

I moved the create_thumb and validate_thumb functions to core/lib/init, thus to be able to use them (from) everywhere.
-- 
https://code.launchpad.net/~googol/openlp/thumb-creation/+merge/77794
Your team OpenLP Core is requested to review the proposed merge of lp:~googol/openlp/thumb-creation into lp:openlp.
=== modified file 'openlp/core/lib/__init__.py'
--- openlp/core/lib/__init__.py	2011-09-20 17:53:09 +0000
+++ openlp/core/lib/__init__.py	2011-10-01 16:33:26 +0000
@@ -144,6 +144,59 @@
     # convert to base64 encoding so does not get missed!
     return byte_array.toBase64()
 
+def create_thumb(image_path, thumb_path, return_icon=True, size=None):
+    """
+    Create a thumbnail from the given image path and depending on
+    ``return_icon`` it returns an icon from this thumb.
+
+    ``image_path``
+        The image file to create the icon from.
+
+    ``thumb_path``
+        The filename to save the thumbnail to.
+
+    ``return_icon``
+        States if an icon should be build and returned from the thumb. Defaults
+        to ``True``.
+
+    ``size``
+        Allows to state a own size to use. Defaults to ``None``, which means
+        that a default height of 88 is used.
+    """
+    ext = os.path.splitext(thumb_path)[1].lower()
+    reader = QtGui.QImageReader(image_path)
+    if size is None:
+        ratio = float(reader.size().width()) / float(reader.size().height())
+        reader.setScaledSize(QtCore.QSize(int(ratio * 88), 88))
+    else:
+        reader.setScaledSize(size)
+    thumb = reader.read()
+    thumb.save(thumb_path, ext[1:])
+    if not return_icon:
+        return
+    if os.path.exists(thumb_path):
+        return build_icon(unicode(thumb_path))
+    # Fallback for files with animation support.
+    return build_icon(unicode(image_path))
+
+def validate_thumb(file_path, thumb_path):
+    """
+    Validates whether an file's thumb still exists and if is up to date.
+    **Note**, you must **not** call this function, before checking the
+    existence of the file.
+
+    ``file_path``
+        The path to the file. The file **must** exist!
+
+    ``thumb_path``
+        The path to the thumb.
+    """
+    if not os.path.exists(unicode(thumb_path)):
+        return False
+    image_date = os.stat(unicode(file_path)).st_mtime
+    thumb_date = os.stat(unicode(thumb_path)).st_mtime
+    return image_date <= thumb_date
+
 def resize_image(image_path, width, height, background=u'#000000'):
     """
     Resize an image to fit on the current screen.
@@ -158,7 +211,7 @@
         The new image height.
 
     ``background``
-        The background colour defaults to black.
+        The background colour. Defaults to black.
 
     DO NOT REMOVE THE DEFAULT BACKGROUND VALUE!
     """

=== modified file 'openlp/core/lib/mediamanageritem.py'
--- openlp/core/lib/mediamanageritem.py	2011-09-17 08:28:33 +0000
+++ openlp/core/lib/mediamanageritem.py	2011-10-01 16:33:26 +0000
@@ -425,44 +425,6 @@
             count += 1
         return filelist
 
-    def validate(self, image, thumb):
-        """
-        Validates whether an image still exists and, if it does, is the
-        thumbnail representation of the image up to date.
-        """
-        if not os.path.exists(unicode(image)):
-            return False
-        if os.path.exists(thumb):
-            imageDate = os.stat(unicode(image)).st_mtime
-            thumbDate = os.stat(unicode(thumb)).st_mtime
-            # If image has been updated rebuild icon
-            if imageDate > thumbDate:
-                self.iconFromFile(image, thumb)
-        else:
-            self.iconFromFile(image, thumb)
-        return True
-
-    def iconFromFile(self, image_path, thumb_path):
-        """
-        Create a thumbnail icon from a given image.
-
-        ``image_path``
-            The image file to create the icon from.
-
-        ``thumb_path``
-            The filename to save the thumbnail to.
-        """
-        ext = os.path.splitext(thumb_path)[1].lower()
-        reader = QtGui.QImageReader(image_path)
-        ratio = float(reader.size().width()) / float(reader.size().height())
-        reader.setScaledSize(QtCore.QSize(int(ratio * 88), 88))
-        thumb = reader.read()
-        thumb.save(thumb_path, ext[1:])
-        if os.path.exists(thumb_path):
-            return build_icon(unicode(thumb_path))
-        # Fallback for files with animation support.
-        return build_icon(unicode(image_path))
-
     def loadList(self, list):
         raise NotImplementedError(u'MediaManagerItem.loadList needs to be '
             u'defined by the plugin')

=== modified file 'openlp/core/ui/slidecontroller.py'
--- openlp/core/ui/slidecontroller.py	2011-09-15 16:48:42 +0000
+++ openlp/core/ui/slidecontroller.py	2011-10-01 16:33:26 +0000
@@ -669,14 +669,14 @@
                 label.setMargin(4)
                 label.setScaledContents(True)
                 if self.serviceItem.is_command():
-                    image = QtGui.QImage(frame[u'image'])
+                    label.setPixmap(QtGui.QPixmap(frame[u'image']))
                 else:
                     # If current slide set background to image
                     if framenumber == slideno:
                         self.serviceItem.bg_image_bytes = \
                             self.imageManager.get_image_bytes(frame[u'title'])
                     image = self.imageManager.get_image(frame[u'title'])
-                label.setPixmap(QtGui.QPixmap.fromImage(image))
+                    label.setPixmap(QtGui.QPixmap.fromImage(image))
                 self.previewListWidget.setCellWidget(framenumber, 0, label)
                 slideHeight = width * self.parent().renderer.screen_ratio
                 row += 1

=== modified file 'openlp/core/ui/thememanager.py'
--- openlp/core/ui/thememanager.py	2011-08-21 05:33:07 +0000
+++ openlp/core/ui/thememanager.py	2011-10-01 16:33:26 +0000
@@ -36,7 +36,7 @@
 
 from openlp.core.lib import OpenLPToolbar, get_text_file_string, build_icon, \
     Receiver, SettingsManager, translate, check_item_selected, \
-    check_directory_exists
+    check_directory_exists, create_thumb, validate_thumb
 from openlp.core.lib.theme import ThemeXML, BackgroundType, VerticalType, \
     BackgroundGradientType
 from openlp.core.lib.ui import UiStrings, critical_error_message_box, \
@@ -359,7 +359,7 @@
             The theme to delete.
         """
         self.themelist.remove(theme)
-        thumb = theme + u'.png'
+        thumb = u'%s.png' % theme
         delete_file(os.path.join(self.path, thumb))
         delete_file(os.path.join(self.thumbPath, thumb))
         try:
@@ -473,15 +473,12 @@
                     name = textName
                 thumb = os.path.join(self.thumbPath, u'%s.png' % textName)
                 item_name = QtGui.QListWidgetItem(name)
-                if os.path.exists(thumb):
+                if validate_thumb(theme, thumb):
                     icon = build_icon(thumb)
                 else:
-                    icon = build_icon(theme)
-                    pixmap = icon.pixmap(QtCore.QSize(88, 50))
-                    pixmap.save(thumb, u'png')
+                    icon = create_thumb(theme, thumb)
                 item_name.setIcon(icon)
-                item_name.setData(QtCore.Qt.UserRole,
-                    QtCore.QVariant(textName))
+                item_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(textName))
                 self.themeListWidget.addItem(item_name)
                 self.themelist.append(textName)
         self._pushThemes()
@@ -658,9 +655,7 @@
             os.unlink(samplepathname)
         frame.save(samplepathname, u'png')
         thumb = os.path.join(self.thumbPath, u'%s.png' % name)
-        icon = build_icon(frame)
-        pixmap = icon.pixmap(QtCore.QSize(88, 50))
-        pixmap.save(thumb, u'png')
+        create_thumb(samplepathname, thumb, False)
         log.debug(u'Theme image written to %s', samplepathname)
 
     def updatePreviewImages(self):

=== modified file 'openlp/plugins/images/lib/mediaitem.py'
--- openlp/plugins/images/lib/mediaitem.py	2011-09-14 20:27:30 +0000
+++ openlp/plugins/images/lib/mediaitem.py	2011-10-01 16:33:26 +0000
@@ -33,7 +33,7 @@
 
 from openlp.core.lib import MediaManagerItem, build_icon, ItemCapabilities, \
     SettingsManager, translate, check_item_selected, check_directory_exists, \
-    Receiver
+    Receiver, create_thumb, validate_thumb
 from openlp.core.lib.ui import UiStrings, critical_error_message_box
 from openlp.core.utils import AppLocation, delete_file, get_images_filter
 
@@ -127,13 +127,13 @@
                 self.plugin.formparent.incrementProgressBar()
             filename = os.path.split(unicode(imageFile))[1]
             thumb = os.path.join(self.servicePath, filename)
-            if os.path.exists(thumb):
-                if self.validate(imageFile, thumb):
+            if not os.path.exists(imageFile):
+                icon = build_icon(u':/general/general_delete.png')
+            else:
+                if validate_thumb(imageFile, thumb):
                     icon = build_icon(thumb)
                 else:
-                    icon = build_icon(u':/general/general_delete.png')
-            else:
-                icon = self.iconFromFile(imageFile, thumb)
+                    icon = create_thumb(imageFile, thumb)
             item_name = QtGui.QListWidgetItem(filename)
             item_name.setIcon(icon)
             item_name.setToolTip(imageFile)

=== modified file 'openlp/plugins/presentations/lib/mediaitem.py'
--- openlp/plugins/presentations/lib/mediaitem.py	2011-09-20 16:24:07 +0000
+++ openlp/plugins/presentations/lib/mediaitem.py	2011-10-01 16:33:26 +0000
@@ -32,7 +32,8 @@
 from PyQt4 import QtCore, QtGui
 
 from openlp.core.lib import MediaManagerItem, build_icon, SettingsManager, \
-    translate, check_item_selected, Receiver, ItemCapabilities
+    translate, check_item_selected, Receiver, ItemCapabilities, create_thumb, \
+    validate_thumb
 from openlp.core.lib.ui import UiStrings, critical_error_message_box, \
     media_item_combo_box
 from openlp.plugins.presentations.lib import MessageListener
@@ -193,10 +194,13 @@
                     doc.load_presentation()
                     preview = doc.get_thumbnail_path(1, True)
                 doc.close_presentation()
-                if preview and self.validate(preview, thumb):
-                    icon = build_icon(thumb)
+                if not (preview and os.path.exists(preview)):
+                    icon = build_icon(u':/general/general_delete.png')
                 else:
-                    icon = build_icon(u':/general/general_delete.png')
+                    if validate_thumb(preview, thumb):
+                        icon = build_icon(thumb)
+                    else:
+                        icon = create_thumb(preview, thumb)
             else:
                 if initialLoad:
                     icon = build_icon(u':/general/general_delete.png')

=== modified file 'openlp/plugins/presentations/lib/presentationcontroller.py'
--- openlp/plugins/presentations/lib/presentationcontroller.py	2011-06-12 16:02:52 +0000
+++ openlp/plugins/presentations/lib/presentationcontroller.py	2011-10-01 16:33:26 +0000
@@ -31,7 +31,8 @@
 
 from PyQt4 import QtCore
 
-from openlp.core.lib import Receiver, resize_image
+from openlp.core.lib import Receiver, check_directory_exists, create_thumb, \
+    resize_image, validate_thumb
 from openlp.core.utils import AppLocation
 
 log = logging.getLogger(__name__)
@@ -97,8 +98,7 @@
         self.slidenumber = 0
         self.controller = controller
         self.filepath = name
-        if not os.path.isdir(self.get_thumbnail_folder()):
-            os.mkdir(self.get_thumbnail_folder())
+        check_directory_exists(self.get_thumbnail_folder())
 
     def load_presentation(self):
         """
@@ -145,15 +145,13 @@
 
     def check_thumbnails(self):
         """
-        Returns true if the thumbnail images look to exist and are more
-        recent than the powerpoint
+        Returns ``True`` if the thumbnail images exist and are more recent than
+        the powerpoint file.
         """
         lastimage = self.get_thumbnail_path(self.get_slide_count(), True)
         if not (lastimage and os.path.isfile(lastimage)):
             return False
-        imgdate = os.stat(lastimage).st_mtime
-        pptdate = os.stat(self.filepath).st_mtime
-        return imgdate >= pptdate
+        return validate_thumb(self.filepath, lastimage)
 
     def close_presentation(self):
         """
@@ -246,8 +244,8 @@
         if self.check_thumbnails():
             return
         if os.path.isfile(file):
-            img = resize_image(file, 320, 240)
-            img.save(self.get_thumbnail_path(idx, False))
+            thumb_path = self.get_thumbnail_path(idx, False)
+            create_thumb(file, thumb_path, False, QtCore.QSize(320, 240))
 
     def get_thumbnail_path(self, slide_no, check_exists):
         """
@@ -387,10 +385,8 @@
             AppLocation.get_section_data_path(self.settings_section),
             u'thumbnails')
         self.thumbnail_prefix = u'slide'
-        if not os.path.isdir(self.thumbnail_folder):
-            os.makedirs(self.thumbnail_folder)
-        if not os.path.isdir(self.temp_folder):
-            os.makedirs(self.temp_folder)
+        check_directory_exists(self.thumbnail_folder)
+        check_directory_exists(self.temp_folder)
 
     def enabled(self):
         """


Follow ups