2014-05-30 17:38:27 +00:00
|
|
|
#include "divepicturewidget.h"
|
2014-05-30 18:16:00 +00:00
|
|
|
#include <dive.h>
|
2014-07-30 21:19:34 +00:00
|
|
|
#include <divelist.h>
|
2014-06-26 17:01:31 +00:00
|
|
|
#include <QtConcurrentMap>
|
|
|
|
#include <QDir>
|
2014-07-01 16:02:10 +00:00
|
|
|
#include <QHash>
|
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 {
|
|
|
|
QImage p = QImage(s).scaled(128, 128, Qt::KeepAspectRatio);
|
|
|
|
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-07-02 20:58:06 +00:00
|
|
|
numberOfPictures = dive_get_picture_count(&displayed_dive);
|
|
|
|
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-07-02 20:58:06 +00:00
|
|
|
FOR_EACH_PICTURE (&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
|
|
|
}
|