2015-05-29 17:42:57 +00:00
|
|
|
#include "divepicturemodel.h"
|
|
|
|
#include "dive.h"
|
|
|
|
#include "metrics.h"
|
|
|
|
#include "divelist.h"
|
2015-11-06 18:39:59 +00:00
|
|
|
#include "imagedownloader.h"
|
2015-05-29 17:42:57 +00:00
|
|
|
|
|
|
|
#include <QtConcurrent>
|
|
|
|
|
2015-11-09 15:48:12 +00:00
|
|
|
extern QHash <QString, QImage > thumbnailCache;
|
|
|
|
|
|
|
|
|
2015-08-06 13:14:18 +00:00
|
|
|
SPixmap scaleImages(picturepointer picture)
|
2015-05-29 17:42:57 +00:00
|
|
|
{
|
|
|
|
SPixmap ret;
|
|
|
|
ret.first = picture;
|
2015-11-09 15:48:12 +00:00
|
|
|
if (thumbnailCache.contains(picture->filename) && !thumbnailCache.value(picture->filename).isNull()) {
|
|
|
|
ret.second = thumbnailCache.value(picture->filename);
|
2015-05-29 17:42:57 +00:00
|
|
|
} else {
|
|
|
|
int dim = defaultIconMetrics().sz_pic;
|
|
|
|
QImage p = SHashedImage(picture);
|
2015-10-03 13:47:37 +00:00
|
|
|
if(!p.isNull()) {
|
2015-05-29 17:42:57 +00:00
|
|
|
p = p.scaled(dim, dim, Qt::KeepAspectRatio);
|
2015-11-09 15:48:12 +00:00
|
|
|
thumbnailCache.insert(picture->filename, p);
|
2015-10-03 13:47:37 +00:00
|
|
|
}
|
2015-05-29 17:42:57 +00:00
|
|
|
ret.second = p;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DivePictureModel *DivePictureModel::instance()
|
|
|
|
{
|
|
|
|
static DivePictureModel *self = new DivePictureModel();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
DivePictureModel::DivePictureModel() : numberOfPictures(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DivePictureModel::updateDivePicturesWhenDone(QList<QFuture<void> > futures)
|
|
|
|
{
|
|
|
|
Q_FOREACH (QFuture<void> f, futures) {
|
|
|
|
f.waitForFinished();
|
|
|
|
}
|
|
|
|
updateDivePictures();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePictureModel::updateDivePictures()
|
|
|
|
{
|
|
|
|
if (numberOfPictures != 0) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, numberOfPictures - 1);
|
|
|
|
numberOfPictures = 0;
|
|
|
|
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) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stringPixmapCache.clear();
|
|
|
|
SPictureList pictures;
|
|
|
|
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);
|
|
|
|
Q_FOREACH (const SPixmap &pixmap, list)
|
|
|
|
stringPixmapCache[pixmap.first->filename].image = pixmap.second;
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), 0, numberOfPictures - 1);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
int DivePictureModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2016-03-08 05:26:23 +00:00
|
|
|
Q_UNUSED(parent);
|
2015-05-29 17:42:57 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DivePictureModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
QVariant ret;
|
|
|
|
if (!index.isValid())
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
QString key = stringPixmapCache.keys().at(index.row());
|
|
|
|
if (index.column() == 0) {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
ret = key;
|
|
|
|
break;
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
ret = stringPixmapCache[key].image;
|
|
|
|
break;
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
ret = QFileInfo(key).fileName();
|
|
|
|
break;
|
|
|
|
case Qt::DisplayPropertyRole:
|
|
|
|
ret = QFileInfo(key).filePath();
|
|
|
|
}
|
|
|
|
} else if (index.column() == 1) {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::UserRole:
|
|
|
|
ret = QVariant::fromValue((int)stringPixmapCache[key].offsetSeconds);
|
|
|
|
break;
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
ret = key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-20 19:03:53 +00:00
|
|
|
void DivePictureModel::removePicture(const QString &fileUrl, bool last)
|
2015-05-29 17:42:57 +00:00
|
|
|
{
|
|
|
|
dive_remove_picture(fileUrl.toUtf8().data());
|
2015-10-20 19:03:53 +00:00
|
|
|
if (last) {
|
|
|
|
copy_dive(current_dive, &displayed_dive);
|
|
|
|
updateDivePictures();
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
}
|
2015-05-29 17:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DivePictureModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2016-03-08 05:26:23 +00:00
|
|
|
Q_UNUSED(parent);
|
2015-05-29 17:42:57 +00:00
|
|
|
return numberOfPictures;
|
2015-08-06 13:14:18 +00:00
|
|
|
}
|