From 8f7335ff56c42f7df550649ad0c75ea2f4ee8d92 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 13 Nov 2007 17:56:46 +0000 Subject: [PATCH] Enforce a maximum size for cover display widgets. --- src/libprs500/__init__.py | 23 +++++++++++++++++++ src/libprs500/ebooks/lrf/html/convert_from.py | 16 ++----------- src/libprs500/gui2/status.py | 11 ++++++--- src/libprs500/gui2/widgets.py | 9 ++++++-- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/libprs500/__init__.py b/src/libprs500/__init__.py index e4727864e5..687dfc17c0 100644 --- a/src/libprs500/__init__.py +++ b/src/libprs500/__init__.py @@ -20,6 +20,7 @@ __appname__ = 'libprs500' import sys, os, logging, mechanize, locale, cStringIO from gettext import GNUTranslations +from math import floor iswindows = 'win32' in sys.platform.lower() isosx = 'darwin' in sys.platform.lower() @@ -84,6 +85,28 @@ def browser(): opener.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; i686 Linux; en_US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4')] return opener +def fit_image(width, height, pwidth, pheight): + ''' + Fit image in box of width pwidth and height pheight. + @param width: Width of image + @param height: Height of image + @param pwidth: Width of box + @param pheight: Height of box + @return: scaled, new_width, new_height. scaled is True iff new_widdth and/or new_height is different from width or height. + ''' + scaled = height > pheight or width > pwidth + if height > pheight: + corrf = pheight/float(height) + width, height = floor(corrf*width), pheight + if width > pwidth: + corrf = pwidth/float(width) + width, height = pwidth, floor(corrf*height) + if height > pheight: + corrf = pheight/float(height) + width, height = floor(corrf*width), pheight + + return scaled, int(width), int(height) + def set_translator(): # To test different translations invoke as # LC_ALL=de_DE.utf8 program diff --git a/src/libprs500/ebooks/lrf/html/convert_from.py b/src/libprs500/ebooks/lrf/html/convert_from.py index 4405c2a53e..5fcdbad15e 100644 --- a/src/libprs500/ebooks/lrf/html/convert_from.py +++ b/src/libprs500/ebooks/lrf/html/convert_from.py @@ -42,7 +42,7 @@ from libprs500.ebooks.lrf import Book from libprs500.ebooks.lrf import option_parser as lrf_option_parser from libprs500.ebooks import ConversionError from libprs500.ebooks.lrf.html.table import Table -from libprs500 import filename_to_utf8, setup_cli_handlers, __appname__ +from libprs500 import filename_to_utf8, setup_cli_handlers, __appname__, fit_image from libprs500.ptempfile import PersistentTemporaryFile from libprs500.ebooks.metadata.opf import OPFReader from libprs500.devices.interface import Device @@ -64,19 +64,7 @@ def munge_paths(basepath, url): path = os.path.join(os.path.dirname(basepath), path) return os.path.normpath(path), fragment -def fit_image(width, height, pwidth, pheight): - scaled = height > pheight or width > pwidth - if height > pheight: - corrf = pheight/float(height) - width, height = floor(corrf*width), pheight - if width > pwidth: - corrf = pwidth/float(width) - width, height = pwidth, floor(corrf*height) - if height > pheight: - corrf = pheight/float(height) - width, height = floor(corrf*width), pheight - - return scaled, int(width), int(height) + class HTMLConverter(object): diff --git a/src/libprs500/gui2/status.py b/src/libprs500/gui2/status.py index 1ebe411dd1..ffe1799556 100644 --- a/src/libprs500/gui2/status.py +++ b/src/libprs500/gui2/status.py @@ -17,6 +17,7 @@ import textwrap from PyQt4.QtGui import QStatusBar, QMovie, QLabel, QFrame, QHBoxLayout, QPixmap, \ QVBoxLayout, QSizePolicy from PyQt4.QtCore import Qt, QSize +from libprs500 import fit_image from libprs500.gui2 import qstring_to_unicode class BookInfoDisplay(QFrame): @@ -29,13 +30,17 @@ class BookInfoDisplay(QFrame): self.__class__.HEIGHT, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) - self.setPixmap(self.default_pixmap) - self.setMaximumHeight(self.HEIGHT) - self.setMaximumWidth(self.WIDTH) self.setScaledContents(True) + self.setPixmap(self.default_pixmap) + def setPixmap(self, pixmap): + width, height = fit_image(pixmap.width(), pixmap.height(), + self.WIDTH, self.HEIGHT)[1:] + self.setMaximumHeight(height) + self.setMaximumWidth(width) QLabel.setPixmap(self, pixmap) + aspect_ratio = pixmap.width()/float(pixmap.height()) self.setMaximumWidth(int(aspect_ratio*self.HEIGHT)) diff --git a/src/libprs500/gui2/widgets.py b/src/libprs500/gui2/widgets.py index 68dda1570b..c368d672f3 100644 --- a/src/libprs500/gui2/widgets.py +++ b/src/libprs500/gui2/widgets.py @@ -19,13 +19,18 @@ from PyQt4.QtGui import QListView, QIcon, QFont, QLabel from PyQt4.QtCore import QAbstractListModel, QVariant, Qt, QSize, SIGNAL, QObject from libprs500.gui2 import human_readable, NONE +from libprs500 import fit_image class ImageView(QLabel): + MAX_WIDTH = 400 + MAX_HEIGHT = 300 + def setPixmap(self, pixmap): QLabel.setPixmap(self, pixmap) - self.setMaximumWidth(pixmap.width()) - self.setMaximumHeight(pixmap.height()) + width, height = fit_image(pixmap.width(), pixmap.height(), self.MAX_WIDTH, self.MAX_HEIGHT)[1:] + self.setMaximumWidth(width) + self.setMaximumHeight(height) class LocationModel(QAbstractListModel): def __init__(self, parent):