Simplify DivePictureModel

The code of DivePictureModel used a QHash to keep track of thumbnails.
Not only was the code rather complex - it also had the consequence that
pictures are sorted according to the hash function, i.e. seemingly
random.

This commit replaces the QHash by a simple QList which keeps track
of thumbnails and some meta-data.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2017-12-10 00:07:46 +01:00 committed by Robert C. Helling
parent a07d3b7bfe
commit c73828d605
3 changed files with 35 additions and 59 deletions

View file

@ -9,22 +9,19 @@
extern QHash <QString, QImage> thumbnailCache; extern QHash <QString, QImage> thumbnailCache;
SPixmap scaleImages(picturepointer picture) void scaleImages(PictureEntry &entry)
{ {
SPixmap ret; if (thumbnailCache.contains(entry.filename) && !thumbnailCache.value(entry.filename).isNull()) {
ret.first = picture; entry.image = thumbnailCache.value(entry.filename);
if (thumbnailCache.contains(picture->filename) && !thumbnailCache.value(picture->filename).isNull()) {
ret.second = thumbnailCache.value(picture->filename);
} else { } else {
int dim = defaultIconMetrics().sz_pic; int dim = defaultIconMetrics().sz_pic;
QImage p = SHashedImage(picture); QImage p = SHashedImage(entry.picture);
if(!p.isNull()) { if(!p.isNull()) {
p = p.scaled(dim, dim, Qt::KeepAspectRatio); p = p.scaled(dim, dim, Qt::KeepAspectRatio);
thumbnailCache.insert(picture->filename, p); thumbnailCache.insert(entry.filename, p);
} }
ret.second = p; entry.image = p;
} }
return ret;
} }
DivePictureModel *DivePictureModel::instance() DivePictureModel *DivePictureModel::instance()
@ -33,7 +30,7 @@ DivePictureModel *DivePictureModel::instance()
return self; return self;
} }
DivePictureModel::DivePictureModel() : numberOfPictures(0) DivePictureModel::DivePictureModel()
{ {
} }
@ -47,30 +44,22 @@ void DivePictureModel::updateDivePicturesWhenDone(QList<QFuture<void>> futures)
void DivePictureModel::updateDivePictures() void DivePictureModel::updateDivePictures()
{ {
if (numberOfPictures != 0) { if (!pictures.isEmpty()) {
beginRemoveRows(QModelIndex(), 0, numberOfPictures - 1); beginRemoveRows(QModelIndex(), 0, pictures.count() - 1);
numberOfPictures = 0; pictures.clear();
endRemoveRows(); endRemoveRows();
} }
// if the dive_table is empty, ignore the displayed_dive // if the dive_table is empty, ignore the displayed_dive
numberOfPictures = dive_table.nr == 0 ? 0 : dive_get_picture_count(&displayed_dive); if (dive_table.nr == 0 || dive_get_picture_count(&displayed_dive) == 0)
if (numberOfPictures == 0) {
return; return;
}
stringPixmapCache.clear(); FOR_EACH_PICTURE_NON_PTR(displayed_dive)
SPictureList pictures; pictures.push_back({picture, picture->filename, QImage(), picture->offset.seconds});
FOR_EACH_PICTURE_NON_PTR(displayed_dive) {
stringPixmapCache[QString(picture->filename)].offsetSeconds = picture->offset.seconds;
pictures.push_back(picture);
}
QList<SPixmap> list = QtConcurrent::blockingMapped(pictures, scaleImages); QtConcurrent::blockingMap(pictures, scaleImages);
Q_FOREACH (const SPixmap &pixmap, list)
stringPixmapCache[pixmap.first->filename].image = pixmap.second;
beginInsertRows(QModelIndex(), 0, numberOfPictures - 1); beginInsertRows(QModelIndex(), 0, pictures.count() - 1);
endInsertRows(); endInsertRows();
} }
@ -86,28 +75,28 @@ QVariant DivePictureModel::data(const QModelIndex &index, int role) const
if (!index.isValid()) if (!index.isValid())
return ret; return ret;
QString key = stringPixmapCache.keys().at(index.row()); const PictureEntry &entry = pictures.at(index.row());
if (index.column() == 0) { if (index.column() == 0) {
switch (role) { switch (role) {
case Qt::ToolTipRole: case Qt::ToolTipRole:
ret = key; ret = entry.filename;
break; break;
case Qt::DecorationRole: case Qt::DecorationRole:
ret = stringPixmapCache[key].image; ret = entry.image;
break; break;
case Qt::DisplayRole: case Qt::DisplayRole:
ret = QFileInfo(key).fileName(); ret = QFileInfo(entry.filename).fileName();
break; break;
case Qt::DisplayPropertyRole: case Qt::DisplayPropertyRole:
ret = QFileInfo(key).filePath(); ret = QFileInfo(entry.filename).filePath();
} }
} else if (index.column() == 1) { } else if (index.column() == 1) {
switch (role) { switch (role) {
case Qt::UserRole: case Qt::UserRole:
ret = QVariant::fromValue((int)stringPixmapCache[key].offsetSeconds); ret = QVariant::fromValue(entry.offsetSeconds);
break; break;
case Qt::DisplayRole: case Qt::DisplayRole:
ret = key; ret = entry.filename;
} }
} }
return ret; return ret;
@ -126,5 +115,5 @@ void DivePictureModel::removePicture(const QString &fileUrl, bool last)
int DivePictureModel::rowCount(const QModelIndex &parent) const int DivePictureModel::rowCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent); Q_UNUSED(parent);
return numberOfPictures; return pictures.count();
} }

View file

@ -6,17 +6,15 @@
#include <QImage> #include <QImage>
#include <QFuture> #include <QFuture>
struct PhotoHelper { struct PictureEntry {
struct picture *picture;
QString filename;
QImage image; QImage image;
int offsetSeconds; int offsetSeconds;
}; };
typedef QList<struct picture *> SPictureList;
typedef struct picture *picturepointer;
typedef QPair<picturepointer, QImage> SPixmap;
// function that will scale the pixmap, used inside the QtConcurrent thread. // function that will scale the pixmap, used inside the QtConcurrent thread.
SPixmap scaleImages(picturepointer picture); void scaleImages(PictureEntry &entry);
class DivePictureModel : public QAbstractTableModel { class DivePictureModel : public QAbstractTableModel {
Q_OBJECT Q_OBJECT
@ -31,11 +29,10 @@ public:
protected: protected:
DivePictureModel(); DivePictureModel();
int numberOfPictures;
// Currently, load the images on the fly // Currently, load the images on the fly
// Later, use a thread to load the images // Later, use a thread to load the images
// Later, save the thumbnails so we don't need to reopen every time. // Later, save the thumbnails so we don't need to reopen every time.
QHash<QString, PhotoHelper> stringPixmapCache; QList<PictureEntry> pictures;
}; };
#endif #endif

View file

@ -17,30 +17,20 @@ DiveSitePicturesModel::DiveSitePicturesModel() {
void DiveSitePicturesModel::updateDivePictures() { void DiveSitePicturesModel::updateDivePictures() {
beginResetModel(); beginResetModel();
numberOfPictures = 0; pictures.clear();
endResetModel(); endResetModel();
const uint32_t ds_uuid = displayed_dive_site.uuid; const uint32_t ds_uuid = displayed_dive_site.uuid;
struct dive *d; struct dive *d;
int i; int i;
stringPixmapCache.clear(); for_each_dive (i, d)
SPictureList pictures; if (d->dive_site_uuid == ds_uuid && dive_get_picture_count(d))
FOR_EACH_PICTURE(d)
pictures.push_back({picture, picture->filename, QImage(), picture->offset.seconds});
for_each_dive (i, d) { QtConcurrent::blockingMap(pictures, scaleImages);
if (d->dive_site_uuid == ds_uuid && dive_get_picture_count(d)) {
FOR_EACH_PICTURE(d) {
stringPixmapCache[QString(picture->filename)].offsetSeconds = picture->offset.seconds;
pictures.push_back(picture);
}
}
}
QList<SPixmap> list = QtConcurrent::blockingMapped(pictures, scaleImages); beginInsertRows(QModelIndex(), 0, pictures.count() - 1);
Q_FOREACH (const SPixmap &pixmap, list)
stringPixmapCache[pixmap.first->filename].image = pixmap.second;
numberOfPictures = list.count();
beginInsertRows(QModelIndex(), 0, numberOfPictures - 1);
endInsertRows(); endInsertRows();
} }