From edf38c51f1f29d2f821714698810fbe81857a63b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 13 Jan 2010 15:33:01 -0700 Subject: [PATCH] Implement #4536 (Auto fit-to-view of cover display in details view) --- src/calibre/gui2/dialogs/book_info.py | 65 ++++++++++++++++-------- src/calibre/gui2/dialogs/book_info.ui | 72 +++++++++++++++------------ 2 files changed, 85 insertions(+), 52 deletions(-) diff --git a/src/calibre/gui2/dialogs/book_info.py b/src/calibre/gui2/dialogs/book_info.py index 72b305fd78..8e7d8ee178 100644 --- a/src/calibre/gui2/dialogs/book_info.py +++ b/src/calibre/gui2/dialogs/book_info.py @@ -7,34 +7,47 @@ __docformat__ = 'restructuredtext en' ''' import textwrap, os -from PyQt4.QtCore import QCoreApplication, SIGNAL, QModelIndex, QUrl +from PyQt4.QtCore import QCoreApplication, SIGNAL, QModelIndex, QUrl, QTimer, Qt from PyQt4.QtGui import QDialog, QPixmap, QGraphicsScene, QIcon, QDesktopServices from calibre.gui2.dialogs.book_info_ui import Ui_BookInfo +from calibre.gui2 import dynamic +from calibre import fit_image class BookInfo(QDialog, Ui_BookInfo): - + def __init__(self, parent, view, row): QDialog.__init__(self, parent) Ui_BookInfo.__init__(self) self.setupUi(self) + self.cover_pixmap = None desktop = QCoreApplication.instance().desktop() screen_height = desktop.availableGeometry().height() - 100 self.resize(self.size().width(), screen_height) - - + + self.view = view self.current_row = None + self.fit_cover.setChecked(dynamic.get('book_info_dialog_fit_cover', + False)) self.refresh(row) self.connect(self.view.selectionModel(), SIGNAL('currentChanged(QModelIndex,QModelIndex)'), self.slave) self.connect(self.next_button, SIGNAL('clicked()'), self.next) self.connect(self.previous_button, SIGNAL('clicked()'), self.previous) self.connect(self.text, SIGNAL('linkActivated(QString)'), self.open_book_path) - + self.fit_cover.stateChanged.connect(self.toggle_cover_fit) + self.cover.resizeEvent = self.cover_view_resized + + def toggle_cover_fit(self, state): + dynamic.set('book_info_dialog_fit_cover', self.fit_cover.isChecked()) + self.resize_cover() + + def cover_view_resized(self, event): + QTimer.singleShot(1, self.resize_cover) def slave(self, current, previous): row = current.row() self.refresh(row) - + def open_book_path(self, path): if os.sep in unicode(path): QDesktopServices.openUrl(QUrl('file:'+path)) @@ -43,41 +56,53 @@ class BookInfo(QDialog, Ui_BookInfo): path = self.view.model().db.format_abspath(self.current_row, format) if path is not None: QDesktopServices.openUrl(QUrl('file:'+path)) - - + + def next(self): row = self.view.currentIndex().row() ni = self.view.model().index(row+1, 0) if ni.isValid(): self.view.setCurrentIndex(ni) - + def previous(self): row = self.view.currentIndex().row() ni = self.view.model().index(row-1, 0) if ni.isValid(): self.view.setCurrentIndex(ni) - + + def resize_cover(self): + if self.cover_pixmap is None: + return + self.setWindowIcon(QIcon(self.cover_pixmap)) + self.scene = QGraphicsScene() + pixmap = self.cover_pixmap + if self.fit_cover.isChecked(): + scaled, new_width, new_height = fit_image(pixmap.width(), + pixmap.height(), self.cover.size().width()-10, + self.cover.size().height()-10) + if scaled: + pixmap = pixmap.scaled(new_width, new_height, + Qt.KeepAspectRatio, Qt.SmoothTransformation) + self.scene.addPixmap(pixmap) + self.cover.setScene(self.scene) + def refresh(self, row): if isinstance(row, QModelIndex): row = row.row() if row == self.current_row: return self.previous_button.setEnabled(False if row == 0 else True) - self.next_button.setEnabled(False if row == self.view.model().rowCount(QModelIndex())-1 else True) + self.next_button.setEnabled(False if row == self.view.model().rowCount(QModelIndex())-1 else True) self.current_row = row info = self.view.model().get_book_info(row) self.setWindowTitle(info[_('Title')]) self.title.setText(''+info.pop(_('Title'))) self.comments.setText(info.pop(_('Comments'), '')) - + cdata = info.pop('cover', '') - pixmap = QPixmap.fromImage(cdata) - self.setWindowIcon(QIcon(pixmap)) - - self.scene = QGraphicsScene() - self.scene.addPixmap(pixmap) - self.cover.setScene(self.scene) - + self.cover_pixmap = QPixmap.fromImage(cdata) + self.resize_cover() + rows = u'' self.text.setText('') self.data = info @@ -94,4 +119,4 @@ class BookInfo(QDialog, Ui_BookInfo): txt = info[key] txt = u'
\n'.join(textwrap.wrap(txt, 120)) rows += u'%s:%s'%(key, txt) - self.text.setText(u''+rows+'
') \ No newline at end of file + self.text.setText(u''+rows+'
') diff --git a/src/calibre/gui2/dialogs/book_info.ui b/src/calibre/gui2/dialogs/book_info.ui index 27e15f96a1..02dae12281 100644 --- a/src/calibre/gui2/dialogs/book_info.ui +++ b/src/calibre/gui2/dialogs/book_info.ui @@ -1,7 +1,8 @@ - + + BookInfo - - + + 0 0 @@ -9,70 +10,77 @@ 783 - + Dialog - - - - + + + + TextLabel - + Qt::AlignCenter - - + + - - + + - - + + TextLabel - + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - + true - - + + Comments - - - + + + - + + + Fit &cover to view + + + + + - - + + &Previous - - + + :/images/previous.svg:/images/previous.svg - - + + &Next - - + + :/images/next.svg:/images/next.svg @@ -84,7 +92,7 @@ - +