From acbf83698b45c7e6021b21647410af7c93296f26 Mon Sep 17 00:00:00 2001 From: Akos Polster Date: Sat, 20 Nov 2010 00:26:44 +0100 Subject: [PATCH 1/1] Fix cover images. --- model/book.cpp | 67 ++++++++++++++++++++++++++++++++--------------------- model/book.h | 10 +++++++- model/library.cpp | 6 ++++- 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/model/book.cpp b/model/book.cpp index 56c88bc..89490d2 100644 --- a/model/book.cpp +++ b/model/book.cpp @@ -13,24 +13,7 @@ const int COVER_WIDTH = 53; const int COVER_HEIGHT = 59; - -static QImage makeCover(const QString &path) -{ - QPixmap src = QPixmap(path).scaled(COVER_WIDTH, COVER_HEIGHT, - Qt::KeepAspectRatio, Qt::SmoothTransformation); - QPixmap transparent(src.size()); - transparent.fill(Qt::transparent); - - QPainter p; - p.begin(&transparent); - p.setCompositionMode(QPainter::CompositionMode_Source); - p.drawPixmap((COVER_WIDTH - src.width()) / 2, - (COVER_HEIGHT - src.height()) / 2, src); - p.setCompositionMode(QPainter::CompositionMode_DestinationIn); - p.end(); - - return transparent.toImage(); -} +const int COVER_MAX = 512 * 1024; Book::Book(const QString &p, QObject *parent): QObject(parent), loaded(false) { @@ -39,7 +22,6 @@ Book::Book(const QString &p, QObject *parent): QObject(parent), loaded(false) QFileInfo info(p); mPath = info.absoluteFilePath(); title = info.baseName(); - cover = makeCover(":/icons/book.png"); mTempFile.open(); } } @@ -310,8 +292,7 @@ void Book::load() rights = data["rights"].toString(); mLastBookmark.part = data["lastpart"].toInt(); mLastBookmark.pos = data["lastpos"].toReal(); - cover = data["cover"].value().scaled(COVER_WIDTH, - COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + cover = data["cover"].value(); if (cover.isNull()) { cover = makeCover(":/icons/book.png"); } @@ -429,9 +410,8 @@ QString Book::name() } } return ret; - } else { - return path(); } + return path(); } QString Book::shortName() @@ -532,7 +512,6 @@ void Book::upgrade() TRACE; // Load book from old database (QSettings) - QSettings settings; QString key = "book/" + path() + "/"; title = settings.value(key + "title").toString(); @@ -546,10 +525,11 @@ void Book::upgrade() rights = settings.value(key + "rights").toString(); mLastBookmark.part = settings.value(key + "lastpart").toInt(); mLastBookmark.pos = settings.value(key + "lastpos").toReal(); - cover = settings.value(key + "cover").value().scaled(COVER_WIDTH, - COVER_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + cover = settings.value(key + "cover").value(); if (cover.isNull()) { cover = makeCover(":/icons/book.png"); + } else { + cover = makeCover(QPixmap::fromImage(cover)); } int size = settings.value(key + "bookmarks").toInt(); for (int i = 0; i < size; i++) { @@ -562,8 +542,10 @@ void Book::upgrade() mBookmarks.append(Bookmark(part, pos)); } - // Save book to new database + // Remove QSettings + settings.remove("book/" + path()); + // Save book to new database save(); } @@ -573,3 +555,34 @@ void Book::remove() close(); BookDb::instance()->remove(path()); } + +QImage Book::makeCover(const QString &fileName) +{ + TRACE; + qDebug() << fileName; + QFileInfo info(fileName); + if (info.isReadable() && (info.size() < COVER_MAX)) { + return makeCover(QPixmap(fileName)); + } + return makeCover(QPixmap(":/icons/book.png")); +} + +QImage Book::makeCover(const QPixmap &pixmap) +{ + TRACE; + QPixmap src = pixmap.scaled(COVER_WIDTH, COVER_HEIGHT, + Qt::KeepAspectRatio, Qt::SmoothTransformation); + QPixmap transparent(COVER_WIDTH, COVER_HEIGHT); + transparent.fill(Qt::transparent); + + QPainter p; + p.begin(&transparent); + p.setCompositionMode(QPainter::CompositionMode_Source); + p.drawPixmap((COVER_WIDTH - src.width()) / 2, + (COVER_HEIGHT - src.height()) / 2, src); + p.end(); + + return transparent.toImage(); +} + + diff --git a/model/book.h b/model/book.h index 07259a9..1da4209 100644 --- a/model/book.h +++ b/model/book.h @@ -4,11 +4,13 @@ #include #include #include -#include +#include #include #include #include +class QPixmap; + /** A book. */ class Book: public QObject { @@ -152,6 +154,12 @@ protected: /** Get location of OPS file in EPUB archive. */ QString opsPath(); + /** Make a cover image from a file. */ + QImage makeCover(const QString &fileName); + + /** Make a cover image from an pixmap. */ + QImage makeCover(const QPixmap &pixmap); + QString mPath; //< Path to EPUB file. Bookmark mLastBookmark; //< Last position read. QList mBookmarks; //< List of bookmarks. diff --git a/model/library.cpp b/model/library.cpp index 6d66a91..1a66639 100644 --- a/model/library.cpp +++ b/model/library.cpp @@ -47,7 +47,7 @@ QVariant Library::data(const QModelIndex &index, int role) const ret = mBooks[index.row()]->name(); break; case Qt::DecorationRole: - ret.setValue(mBooks[index.row()]->cover); + ret.setValue(mBooks[index.row()]->coverImage()); break; default: ; @@ -149,6 +149,7 @@ void Library::remove(const QModelIndex &index) void Library::remove(const QString &path) { + TRACE; remove(find(path)); } @@ -166,6 +167,7 @@ void Library::setNowReading(const QModelIndex &index) void Library::clear() { + TRACE; for (int i = 0; i < mBooks.size(); i++) { delete mBooks[i]; } @@ -175,6 +177,7 @@ void Library::clear() QModelIndex Library::find(QString path) const { + TRACE; if (path != "") { QString absolutePath = QFileInfo(path).absoluteFilePath(); for (int i = 0; i < mBooks.size(); i++) { @@ -188,6 +191,7 @@ QModelIndex Library::find(QString path) const QModelIndex Library::find(const Book *book) const { + TRACE; if (book) { for (int i = 0; i < mBooks.size(); i++) { if (book == mBooks[i]) { -- 1.7.9.5