2014-05-30 17:38:27 +00:00
|
|
|
#include "divepicturewidget.h"
|
2014-10-19 14:15:19 +00:00
|
|
|
#include "metrics.h"
|
2015-01-17 09:43:52 +00:00
|
|
|
#include "dive.h"
|
|
|
|
#include "divelist.h"
|
2014-06-26 17:01:31 +00:00
|
|
|
#include <QtConcurrentMap>
|
|
|
|
#include <QDir>
|
2014-05-30 17:38:27 +00:00
|
|
|
|
2014-06-03 22:04:50 +00:00
|
|
|
DivePictureModel *DivePictureModel::instance()
|
|
|
|
{
|
2014-06-03 23:48:59 +00:00
|
|
|
static DivePictureModel *self = new DivePictureModel();
|
2014-06-03 22:04:50 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-06-13 14:11:06 +00:00
|
|
|
DivePictureModel::DivePictureModel() : numberOfPictures(0)
|
2014-05-30 18:16:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-03 01:09:21 +00:00
|
|
|
typedef QPair<QString, QImage> SPixmap;
|
2014-05-31 03:42:54 +00:00
|
|
|
typedef QList<SPixmap> SPixmapList;
|
|
|
|
|
2014-06-03 23:48:59 +00:00
|
|
|
SPixmap scaleImages(const QString &s)
|
|
|
|
{
|
2014-07-01 16:02:10 +00:00
|
|
|
static QHash <QString, QImage > cache;
|
2014-05-31 03:42:54 +00:00
|
|
|
SPixmap ret;
|
|
|
|
ret.first = s;
|
2014-07-01 16:02:10 +00:00
|
|
|
if (cache.contains(s)) {
|
|
|
|
ret.second = cache.value(s);
|
|
|
|
} else {
|
2014-10-19 14:15:19 +00:00
|
|
|
int dim = defaultIconMetrics().sz_pic;
|
|
|
|
QImage p = QImage(s).scaled(dim, dim, Qt::KeepAspectRatio);
|
2014-07-01 16:02:10 +00:00
|
|
|
cache.insert(s, p);
|
|
|
|
ret.second = p;
|
|
|
|
}
|
2014-05-31 03:42:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-02 20:58:06 +00:00
|
|
|
void DivePictureModel::updateDivePictures()
|
2014-05-30 18:16:00 +00:00
|
|
|
{
|
2014-06-02 21:41:19 +00:00
|
|
|
if (numberOfPictures != 0) {
|
2014-06-03 23:48:59 +00:00
|
|
|
beginRemoveRows(QModelIndex(), 0, numberOfPictures - 1);
|
2014-06-02 21:41:19 +00:00
|
|
|
numberOfPictures = 0;
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
2014-05-30 18:16:00 +00:00
|
|
|
|
2014-12-10 04:15:52 +00:00
|
|
|
// if the dive_table is empty, ignore the displayed_dive
|
|
|
|
numberOfPictures = dive_table.nr == 0 ? 0 : dive_get_picture_count(&displayed_dive);
|
2014-07-02 20:58:06 +00:00
|
|
|
if (numberOfPictures == 0) {
|
2014-05-30 18:16:00 +00:00
|
|
|
return;
|
2014-05-31 03:42:54 +00:00
|
|
|
}
|
2014-06-02 21:41:19 +00:00
|
|
|
|
2014-06-03 22:34:36 +00:00
|
|
|
stringPixmapCache.clear();
|
2014-06-02 21:41:19 +00:00
|
|
|
QStringList pictures;
|
2014-10-28 10:13:07 +00:00
|
|
|
FOR_EACH_PICTURE_NON_PTR(displayed_dive) {
|
2014-08-05 19:37:14 +00:00
|
|
|
stringPixmapCache[QString(picture->filename)].offsetSeconds = picture->offset.seconds;
|
2014-06-02 21:41:19 +00:00
|
|
|
pictures.push_back(QString(picture->filename));
|
2014-05-30 18:16:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 17:43:20 +00:00
|
|
|
Q_FOREACH (const SPixmap &pixmap, QtConcurrent::blockingMapped<SPixmapList>(pictures, scaleImages))
|
2014-06-03 22:34:36 +00:00
|
|
|
stringPixmapCache[pixmap.first].image = pixmap.second;
|
2014-05-31 03:42:54 +00:00
|
|
|
|
2014-06-03 23:48:59 +00:00
|
|
|
beginInsertRows(QModelIndex(), 0, numberOfPictures - 1);
|
2014-05-30 18:16:00 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2014-05-30 17:38:27 +00:00
|
|
|
int DivePictureModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2014-06-03 22:34:36 +00:00
|
|
|
return 2;
|
2014-05-30 17:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DivePictureModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2014-06-02 22:18:07 +00:00
|
|
|
QVariant ret;
|
|
|
|
if (!index.isValid())
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
QString key = stringPixmapCache.keys().at(index.row());
|
2014-06-03 23:48:59 +00:00
|
|
|
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();
|
2014-06-27 12:16:55 +00:00
|
|
|
break;
|
|
|
|
case Qt::DisplayPropertyRole:
|
|
|
|
ret = QFileInfo(key).filePath();
|
2014-06-03 22:34:36 +00:00
|
|
|
}
|
2014-06-03 23:48:59 +00:00
|
|
|
} else if (index.column() == 1) {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::UserRole:
|
2014-08-05 19:37:14 +00:00
|
|
|
ret = QVariant::fromValue((int)stringPixmapCache[key].offsetSeconds);
|
2014-07-30 02:03:32 +00:00
|
|
|
break;
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
ret = key;
|
2014-06-03 22:34:36 +00:00
|
|
|
}
|
2014-06-02 22:18:07 +00:00
|
|
|
}
|
2014-06-02 22:50:42 +00:00
|
|
|
return ret;
|
2014-05-30 17:38:27 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 02:03:32 +00:00
|
|
|
void DivePictureModel::removePicture(const QString &fileUrl)
|
|
|
|
{
|
2014-08-05 19:37:14 +00:00
|
|
|
dive_remove_picture(fileUrl.toUtf8().data());
|
2014-07-30 02:03:32 +00:00
|
|
|
copy_dive(current_dive, &displayed_dive);
|
|
|
|
updateDivePictures();
|
2014-07-30 21:19:34 +00:00
|
|
|
mark_divelist_changed(true);
|
2014-07-30 02:03:32 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 17:38:27 +00:00
|
|
|
int DivePictureModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2014-05-30 18:16:00 +00:00
|
|
|
return numberOfPictures;
|
2014-05-30 17:38:27 +00:00
|
|
|
}
|
|
|
|
|
2014-06-03 23:48:59 +00:00
|
|
|
DivePictureWidget::DivePictureWidget(QWidget *parent) : QListView(parent)
|
2014-05-30 17:38:27 +00:00
|
|
|
{
|
2014-06-27 12:16:55 +00:00
|
|
|
connect(this, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(doubleClicked(const QModelIndex &)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePictureWidget::doubleClicked(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
QString filePath = model()->data(index, Qt::DisplayPropertyRole).toString();
|
|
|
|
emit photoDoubleClicked(filePath);
|
2014-05-30 17:38:27 +00:00
|
|
|
}
|