diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2017-12-10 00:07:46 +0100 |
---|---|---|
committer | Robert C. Helling <helling@atdotde.de> | 2017-12-10 15:00:53 +0100 |
commit | c73828d6055db664354ae5d1b2637d379294ef8a (patch) | |
tree | d750fbabc588d9fd672b4b58cef152ad138573b1 /qt-models/divepicturemodel.cpp | |
parent | a07d3b7bfe30f92df8d7e12d364db5aec0a18e46 (diff) | |
download | subsurface-c73828d6055db664354ae5d1b2637d379294ef8a.tar.gz |
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>
Diffstat (limited to 'qt-models/divepicturemodel.cpp')
-rw-r--r-- | qt-models/divepicturemodel.cpp | 57 |
1 files changed, 23 insertions, 34 deletions
diff --git a/qt-models/divepicturemodel.cpp b/qt-models/divepicturemodel.cpp index e5b40b748..d135e0faa 100644 --- a/qt-models/divepicturemodel.cpp +++ b/qt-models/divepicturemodel.cpp @@ -9,22 +9,19 @@ extern QHash <QString, QImage> thumbnailCache; -SPixmap scaleImages(picturepointer picture) +void scaleImages(PictureEntry &entry) { - SPixmap ret; - ret.first = picture; - if (thumbnailCache.contains(picture->filename) && !thumbnailCache.value(picture->filename).isNull()) { - ret.second = thumbnailCache.value(picture->filename); + if (thumbnailCache.contains(entry.filename) && !thumbnailCache.value(entry.filename).isNull()) { + entry.image = thumbnailCache.value(entry.filename); } else { int dim = defaultIconMetrics().sz_pic; - QImage p = SHashedImage(picture); + QImage p = SHashedImage(entry.picture); if(!p.isNull()) { 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() @@ -33,7 +30,7 @@ DivePictureModel *DivePictureModel::instance() return self; } -DivePictureModel::DivePictureModel() : numberOfPictures(0) +DivePictureModel::DivePictureModel() { } @@ -47,30 +44,22 @@ void DivePictureModel::updateDivePicturesWhenDone(QList<QFuture<void>> futures) void DivePictureModel::updateDivePictures() { - if (numberOfPictures != 0) { - beginRemoveRows(QModelIndex(), 0, numberOfPictures - 1); - numberOfPictures = 0; + if (!pictures.isEmpty()) { + beginRemoveRows(QModelIndex(), 0, pictures.count() - 1); + pictures.clear(); endRemoveRows(); } // if the dive_table is empty, ignore the displayed_dive - numberOfPictures = dive_table.nr == 0 ? 0 : dive_get_picture_count(&displayed_dive); - if (numberOfPictures == 0) { + if (dive_table.nr == 0 || dive_get_picture_count(&displayed_dive) == 0) return; - } - stringPixmapCache.clear(); - SPictureList pictures; - FOR_EACH_PICTURE_NON_PTR(displayed_dive) { - stringPixmapCache[QString(picture->filename)].offsetSeconds = picture->offset.seconds; - pictures.push_back(picture); - } + FOR_EACH_PICTURE_NON_PTR(displayed_dive) + pictures.push_back({picture, picture->filename, QImage(), picture->offset.seconds}); - QList<SPixmap> list = QtConcurrent::blockingMapped(pictures, scaleImages); - Q_FOREACH (const SPixmap &pixmap, list) - stringPixmapCache[pixmap.first->filename].image = pixmap.second; + QtConcurrent::blockingMap(pictures, scaleImages); - beginInsertRows(QModelIndex(), 0, numberOfPictures - 1); + beginInsertRows(QModelIndex(), 0, pictures.count() - 1); endInsertRows(); } @@ -86,28 +75,28 @@ QVariant DivePictureModel::data(const QModelIndex &index, int role) const if (!index.isValid()) return ret; - QString key = stringPixmapCache.keys().at(index.row()); + const PictureEntry &entry = pictures.at(index.row()); if (index.column() == 0) { switch (role) { case Qt::ToolTipRole: - ret = key; + ret = entry.filename; break; case Qt::DecorationRole: - ret = stringPixmapCache[key].image; + ret = entry.image; break; case Qt::DisplayRole: - ret = QFileInfo(key).fileName(); + ret = QFileInfo(entry.filename).fileName(); break; case Qt::DisplayPropertyRole: - ret = QFileInfo(key).filePath(); + ret = QFileInfo(entry.filename).filePath(); } } else if (index.column() == 1) { switch (role) { case Qt::UserRole: - ret = QVariant::fromValue((int)stringPixmapCache[key].offsetSeconds); + ret = QVariant::fromValue(entry.offsetSeconds); break; case Qt::DisplayRole: - ret = key; + ret = entry.filename; } } return ret; @@ -126,5 +115,5 @@ void DivePictureModel::removePicture(const QString &fileUrl, bool last) int DivePictureModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); - return numberOfPictures; + return pictures.count(); } |