2014-05-30 14:38:27 -03:00
|
|
|
#include "divepicturewidget.h"
|
2014-05-30 15:16:00 -03:00
|
|
|
#include <dive.h>
|
2014-05-31 00:42:54 -03:00
|
|
|
#include <qtconcurrentmap.h>
|
2014-06-02 22:09:21 -03:00
|
|
|
#include <qdir.h>
|
2014-05-30 14:38:27 -03:00
|
|
|
|
|
|
|
void DivePictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QStyledItemDelegate::paint(painter, option, index);
|
|
|
|
}
|
|
|
|
|
2014-06-03 19:04:50 -03:00
|
|
|
DivePictureModel *DivePictureModel::instance()
|
|
|
|
{
|
2014-06-03 16:48:59 -07:00
|
|
|
static DivePictureModel *self = new DivePictureModel();
|
2014-06-03 19:04:50 -03:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
DivePictureModel::DivePictureModel()
|
2014-05-30 15:16:00 -03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-02 22:09:21 -03:00
|
|
|
typedef QPair<QString, QImage> SPixmap;
|
2014-05-31 00:42:54 -03:00
|
|
|
typedef QList<SPixmap> SPixmapList;
|
|
|
|
|
2014-06-03 16:48:59 -07:00
|
|
|
SPixmap scaleImages(const QString &s)
|
|
|
|
{
|
|
|
|
QImage p = QImage(s).scaled(128, 128, Qt::KeepAspectRatio);
|
2014-05-31 00:42:54 -03:00
|
|
|
SPixmap ret;
|
|
|
|
ret.first = s;
|
|
|
|
ret.second = p;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:16:00 -03:00
|
|
|
void DivePictureModel::updateDivePictures(int divenr)
|
|
|
|
{
|
2014-06-02 18:41:19 -03:00
|
|
|
if (numberOfPictures != 0) {
|
2014-06-03 16:48:59 -07:00
|
|
|
beginRemoveRows(QModelIndex(), 0, numberOfPictures - 1);
|
2014-06-02 18:41:19 -03:00
|
|
|
numberOfPictures = 0;
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
2014-05-30 15:16:00 -03:00
|
|
|
|
|
|
|
struct dive *d = get_dive(divenr);
|
2014-06-02 19:18:07 -03:00
|
|
|
numberOfPictures = dive_get_picture_count(d);
|
2014-06-02 18:41:19 -03:00
|
|
|
if (!d || numberOfPictures == 0) {
|
2014-05-30 15:16:00 -03:00
|
|
|
return;
|
2014-05-31 00:42:54 -03:00
|
|
|
}
|
2014-06-02 18:41:19 -03:00
|
|
|
|
2014-06-03 19:34:36 -03:00
|
|
|
stringPixmapCache.clear();
|
2014-06-02 18:41:19 -03:00
|
|
|
QStringList pictures;
|
2014-06-03 16:48:59 -07:00
|
|
|
FOR_EACH_PICTURE (d) {
|
2014-06-03 19:34:36 -03:00
|
|
|
stringPixmapCache[QString(picture->filename)].picture = picture;
|
2014-06-02 18:41:19 -03:00
|
|
|
pictures.push_back(QString(picture->filename));
|
2014-05-30 15:16:00 -03:00
|
|
|
}
|
|
|
|
|
2014-06-03 19:34:36 -03:00
|
|
|
|
2014-06-03 16:48:59 -07:00
|
|
|
SPixmapList retList = QtConcurrent::blockingMapped<SPixmapList>(pictures, scaleImages);
|
|
|
|
Q_FOREACH (const SPixmap &pixmap, retList)
|
2014-06-03 19:34:36 -03:00
|
|
|
stringPixmapCache[pixmap.first].image = pixmap.second;
|
2014-05-31 00:42:54 -03:00
|
|
|
|
2014-06-03 16:48:59 -07:00
|
|
|
beginInsertRows(QModelIndex(), 0, numberOfPictures - 1);
|
2014-05-30 15:16:00 -03:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2014-05-30 14:38:27 -03:00
|
|
|
int DivePictureModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2014-06-03 19:34:36 -03:00
|
|
|
return 2;
|
2014-05-30 14:38:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DivePictureModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2014-06-02 19:18:07 -03:00
|
|
|
QVariant ret;
|
|
|
|
if (!index.isValid())
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
QString key = stringPixmapCache.keys().at(index.row());
|
2014-06-03 16:48:59 -07: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-03 19:34:36 -03:00
|
|
|
}
|
2014-06-03 16:48:59 -07:00
|
|
|
} else if (index.column() == 1) {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::UserRole:
|
|
|
|
ret = QVariant::fromValue((void *)stringPixmapCache[key].picture);
|
2014-06-03 19:34:36 -03:00
|
|
|
}
|
2014-06-02 19:18:07 -03:00
|
|
|
}
|
2014-06-02 19:50:42 -03:00
|
|
|
return ret;
|
2014-05-30 14:38:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int DivePictureModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2014-05-30 15:16:00 -03:00
|
|
|
return numberOfPictures;
|
2014-05-30 14:38:27 -03:00
|
|
|
}
|
|
|
|
|
2014-06-03 16:48:59 -07:00
|
|
|
DivePictureWidget::DivePictureWidget(QWidget *parent) : QListView(parent)
|
2014-05-30 14:38:27 -03:00
|
|
|
{
|
|
|
|
}
|