mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Move DivePictureModel to qt-models
This class will surely be used on the mobile version, and it was very tangled inside divepicturewidget. Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
37bd82f623
commit
ffc0c8ee99
11 changed files with 181 additions and 156 deletions
|
@ -263,6 +263,7 @@ set(SUBSURFACE_MODELS_LIB_SRCS
|
||||||
qt-models/divecomputerextradatamodel.cpp
|
qt-models/divecomputerextradatamodel.cpp
|
||||||
qt-models/completionmodels.cpp
|
qt-models/completionmodels.cpp
|
||||||
qt-models/profileprintmodel.cpp
|
qt-models/profileprintmodel.cpp
|
||||||
|
qt-models/divepicturemodel.cpp
|
||||||
)
|
)
|
||||||
source_group("Subsurface Models" FILES ${SUBSURFACE_MODELS})
|
source_group("Subsurface Models" FILES ${SUBSURFACE_MODELS})
|
||||||
|
|
||||||
|
|
129
qt-models/divepicturemodel.cpp
Normal file
129
qt-models/divepicturemodel.cpp
Normal file
|
@ -0,0 +1,129 @@
|
||||||
|
#include "divepicturemodel.h"
|
||||||
|
#include "dive.h"
|
||||||
|
#include "metrics.h"
|
||||||
|
#include "divelist.h"
|
||||||
|
|
||||||
|
#include <QtConcurrent>
|
||||||
|
|
||||||
|
typedef QList<struct picture *> SPictureList;
|
||||||
|
typedef struct picture *picturepointer;
|
||||||
|
typedef QPair<picturepointer, QImage> SPixmap;
|
||||||
|
|
||||||
|
|
||||||
|
static SPixmap scaleImages(picturepointer picture)
|
||||||
|
{
|
||||||
|
static QHash <QString, QImage > cache;
|
||||||
|
SPixmap ret;
|
||||||
|
ret.first = picture;
|
||||||
|
if (cache.contains(picture->filename) && !cache.value(picture->filename).isNull()) {
|
||||||
|
ret.second = cache.value(picture->filename);
|
||||||
|
} else {
|
||||||
|
int dim = defaultIconMetrics().sz_pic;
|
||||||
|
QImage p = SHashedImage(picture);
|
||||||
|
if(!p.isNull())
|
||||||
|
p = p.scaled(dim, dim, Qt::KeepAspectRatio);
|
||||||
|
cache.insert(picture->filename, p);
|
||||||
|
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
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DivePictureModel::removePicture(const QString &fileUrl)
|
||||||
|
{
|
||||||
|
dive_remove_picture(fileUrl.toUtf8().data());
|
||||||
|
copy_dive(current_dive, &displayed_dive);
|
||||||
|
updateDivePictures();
|
||||||
|
mark_divelist_changed(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
int DivePictureModel::rowCount(const QModelIndex &parent) const
|
||||||
|
{
|
||||||
|
return numberOfPictures;
|
||||||
|
}
|
41
qt-models/divepicturemodel.h
Normal file
41
qt-models/divepicturemodel.h
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
#ifndef DIVEPICTUREMODEL_H
|
||||||
|
#define DIVEPICTUREMODEL_H
|
||||||
|
|
||||||
|
#include <QAbstractTableModel>
|
||||||
|
#include <QImage>
|
||||||
|
#include <QFuture>
|
||||||
|
|
||||||
|
typedef QPair<QString, QByteArray> SHashedFilename;
|
||||||
|
|
||||||
|
class SHashedImage : public QImage {
|
||||||
|
public:
|
||||||
|
SHashedImage(struct picture *picture);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct PhotoHelper {
|
||||||
|
QImage image;
|
||||||
|
int offsetSeconds;
|
||||||
|
};
|
||||||
|
|
||||||
|
class DivePictureModel : public QAbstractTableModel {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
static DivePictureModel *instance();
|
||||||
|
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
||||||
|
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
||||||
|
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
||||||
|
void updateDivePictures();
|
||||||
|
void updateDivePicturesWhenDone(QList<QFuture<void> >);
|
||||||
|
void removePicture(const QString& fileUrl);
|
||||||
|
|
||||||
|
private:
|
||||||
|
DivePictureModel();
|
||||||
|
int numberOfPictures;
|
||||||
|
// Currently, load the images on the fly
|
||||||
|
// Later, use a thread to load the images
|
||||||
|
// Later, save the thumbnails so we don't need to reopen every time.
|
||||||
|
QHash<QString, PhotoHelper> stringPixmapCache;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -20,7 +20,8 @@
|
||||||
#include "qthelper.h"
|
#include "qthelper.h"
|
||||||
#include "undocommands.h"
|
#include "undocommands.h"
|
||||||
#include "divelistview.h"
|
#include "divelistview.h"
|
||||||
#include "models.h"
|
#include "divepicturemodel.h"
|
||||||
|
#include "metrics.h"
|
||||||
|
|
||||||
// # Date Rtg Dpth Dur Tmp Wght Suit Cyl Gas SAC OTU CNS Loc
|
// # Date Rtg Dpth Dur Tmp Wght Suit Cyl Gas SAC OTU CNS Loc
|
||||||
static int defaultWidth[] = { 70, 140, 90, 50, 50, 50, 50, 70, 50, 50, 70, 50, 50, 500};
|
static int defaultWidth[] = { 70, 140, 90, 50, 50, 50, 50, 70, 50, 50, 70, 50, 50, 500};
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "divepicturewidget.h"
|
#include "divepicturewidget.h"
|
||||||
|
#include "divepicturemodel.h"
|
||||||
#include "metrics.h"
|
#include "metrics.h"
|
||||||
#include "dive.h"
|
#include "dive.h"
|
||||||
#include "divelist.h"
|
#include "divelist.h"
|
||||||
|
@ -14,7 +15,7 @@
|
||||||
#include <qthelper.h>
|
#include <qthelper.h>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
|
|
||||||
void loadPicuture(struct picture *picture)
|
void loadPicture(struct picture *picture)
|
||||||
{
|
{
|
||||||
ImageDownloader download(picture);
|
ImageDownloader download(picture);
|
||||||
download.load();
|
download.load();
|
||||||
|
@ -31,7 +32,7 @@ SHashedImage::SHashedImage(struct picture *picture) : QImage()
|
||||||
if (!isNull()) {
|
if (!isNull()) {
|
||||||
QtConcurrent::run(updateHash, picture);
|
QtConcurrent::run(updateHash, picture);
|
||||||
} else {
|
} else {
|
||||||
QtConcurrent::run(loadPicuture, picture);
|
QtConcurrent::run(loadPicture, picture);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
QByteArray hash = hashFile(url.toLocalFile());
|
QByteArray hash = hashFile(url.toLocalFile());
|
||||||
|
@ -87,126 +88,6 @@ void ImageDownloader::saveImage(QNetworkReply *reply)
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
DivePictureModel *DivePictureModel::instance()
|
|
||||||
{
|
|
||||||
static DivePictureModel *self = new DivePictureModel();
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
DivePictureModel::DivePictureModel() : numberOfPictures(0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct picture *picturepointer;
|
|
||||||
typedef QPair<picturepointer, QImage> SPixmap;
|
|
||||||
typedef QList<struct picture *> SPictureList;
|
|
||||||
|
|
||||||
SPixmap scaleImages(picturepointer picture)
|
|
||||||
{
|
|
||||||
static QHash <QString, QImage > cache;
|
|
||||||
SPixmap ret;
|
|
||||||
ret.first = picture;
|
|
||||||
if (cache.contains(picture->filename) && !cache.value(picture->filename).isNull()) {
|
|
||||||
ret.second = cache.value(picture->filename);
|
|
||||||
} else {
|
|
||||||
int dim = defaultIconMetrics().sz_pic;
|
|
||||||
QImage p = SHashedImage(picture);
|
|
||||||
if(!p.isNull())
|
|
||||||
p = p.scaled(dim, dim, Qt::KeepAspectRatio);
|
|
||||||
cache.insert(picture->filename, p);
|
|
||||||
ret.second = p;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DivePictureModel::removePicture(const QString &fileUrl)
|
|
||||||
{
|
|
||||||
dive_remove_picture(fileUrl.toUtf8().data());
|
|
||||||
copy_dive(current_dive, &displayed_dive);
|
|
||||||
updateDivePictures();
|
|
||||||
mark_divelist_changed(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
int DivePictureModel::rowCount(const QModelIndex &parent) const
|
|
||||||
{
|
|
||||||
return numberOfPictures;
|
|
||||||
}
|
|
||||||
|
|
||||||
DivePictureWidget::DivePictureWidget(QWidget *parent) : QListView(parent)
|
DivePictureWidget::DivePictureWidget(QWidget *parent) : QListView(parent)
|
||||||
{
|
{
|
||||||
connect(this, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(doubleClicked(const QModelIndex &)));
|
connect(this, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(doubleClicked(const QModelIndex &)));
|
||||||
|
|
|
@ -7,18 +7,6 @@
|
||||||
#include <QFuture>
|
#include <QFuture>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
|
|
||||||
typedef QPair<QString, QByteArray> SHashedFilename;
|
|
||||||
|
|
||||||
struct PhotoHelper {
|
|
||||||
QImage image;
|
|
||||||
int offsetSeconds;
|
|
||||||
};
|
|
||||||
|
|
||||||
class SHashedImage : public QImage {
|
|
||||||
public:
|
|
||||||
SHashedImage(struct picture *picture);
|
|
||||||
};
|
|
||||||
|
|
||||||
class ImageDownloader : public QObject {
|
class ImageDownloader : public QObject {
|
||||||
Q_OBJECT;
|
Q_OBJECT;
|
||||||
public:
|
public:
|
||||||
|
@ -31,26 +19,6 @@ private slots:
|
||||||
void saveImage(QNetworkReply *reply);
|
void saveImage(QNetworkReply *reply);
|
||||||
};
|
};
|
||||||
|
|
||||||
class DivePictureModel : public QAbstractTableModel {
|
|
||||||
Q_OBJECT
|
|
||||||
public:
|
|
||||||
static DivePictureModel *instance();
|
|
||||||
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
|
||||||
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
|
||||||
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
|
||||||
void updateDivePictures();
|
|
||||||
void updateDivePicturesWhenDone(QList<QFuture<void> >);
|
|
||||||
void removePicture(const QString& fileUrl);
|
|
||||||
|
|
||||||
private:
|
|
||||||
DivePictureModel();
|
|
||||||
int numberOfPictures;
|
|
||||||
// Currently, load the images on the fly
|
|
||||||
// Later, use a thread to load the images
|
|
||||||
// Later, save the thumbnails so we don't need to reopen every time.
|
|
||||||
QHash<QString, PhotoHelper> stringPixmapCache;
|
|
||||||
};
|
|
||||||
|
|
||||||
class DivePictureWidget : public QListView {
|
class DivePictureWidget : public QListView {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
#include "divesitehelpers.h"
|
#include "divesitehelpers.h"
|
||||||
#include "cylindermodel.h"
|
#include "cylindermodel.h"
|
||||||
#include "weightmodel.h"
|
#include "weightmodel.h"
|
||||||
|
#include "divepicturemodel.h"
|
||||||
#include "divecomputerextradatamodel.h"
|
#include "divecomputerextradatamodel.h"
|
||||||
|
|
||||||
#if defined(FBSUPPORT)
|
#if defined(FBSUPPORT)
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
class WeightModel;
|
class WeightModel;
|
||||||
class CylindersModel;
|
class CylindersModel;
|
||||||
class ExtraDataModel;
|
class ExtraDataModel;
|
||||||
|
class DivePictureModel;
|
||||||
class QCompleter;
|
class QCompleter;
|
||||||
|
|
||||||
struct Completers {
|
struct Completers {
|
||||||
|
|
|
@ -42,6 +42,7 @@
|
||||||
#ifndef NO_USERMANUAL
|
#ifndef NO_USERMANUAL
|
||||||
#include "usermanual.h"
|
#include "usermanual.h"
|
||||||
#endif
|
#endif
|
||||||
|
#include "divepicturemodel.h"
|
||||||
#include <QNetworkProxy>
|
#include <QNetworkProxy>
|
||||||
#include <QUndoStack>
|
#include <QUndoStack>
|
||||||
#include <qthelper.h>
|
#include <qthelper.h>
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include "divepixmapitem.h"
|
#include "divepixmapitem.h"
|
||||||
#include "animationfunctions.h"
|
#include "animationfunctions.h"
|
||||||
#include <divepicturewidget.h>
|
#include "divepicturemodel.h"
|
||||||
#include <preferences.h>
|
#include <preferences.h>
|
||||||
|
|
||||||
#include <QDesktopServices>
|
#include <QDesktopServices>
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include "divepicturewidget.h"
|
#include "divepicturewidget.h"
|
||||||
#include "diveplannermodel.h"
|
#include "diveplannermodel.h"
|
||||||
#include "models.h"
|
#include "models.h"
|
||||||
|
#include "divepicturemodel.h"
|
||||||
#include "maintab.h"
|
#include "maintab.h"
|
||||||
#include "diveplanner.h"
|
#include "diveplanner.h"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue