summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/divepicturewidget.cpp
diff options
context:
space:
mode:
authorGravatar Tomaz Canabrava <tomaz.canabrava@intel.com>2015-09-03 14:20:19 -0300
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-10-30 10:36:49 -0700
commite49d6213ad129284a45d53c3fcdc03249e84efe2 (patch)
tree2946a666ab38af3375e7bb2b8c5dd887d4a7f9a1 /desktop-widgets/divepicturewidget.cpp
parent588abd019fb2ed3f607682f2b6c7fe86a7a5bb90 (diff)
downloadsubsurface-e49d6213ad129284a45d53c3fcdc03249e84efe2.tar.gz
Move qt-ui to desktop-widgets
Since we have now destkop and mobile versions, 'qt-ui' was a very poor name choice for a folder that contains only destkop-enabled widgets. Also, move the graphicsview-common.h/cpp to subsurface-core because it doesn't depend on qgraphicsview, it merely implements all the colors that we use throughout Subsurface, and we will use colors on both desktop and mobile versions Same thing applies for metrics.h/cpp Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'desktop-widgets/divepicturewidget.cpp')
-rw-r--r--desktop-widgets/divepicturewidget.cpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/desktop-widgets/divepicturewidget.cpp b/desktop-widgets/divepicturewidget.cpp
new file mode 100644
index 000000000..bed3d3bd1
--- /dev/null
+++ b/desktop-widgets/divepicturewidget.cpp
@@ -0,0 +1,100 @@
+#include "divepicturewidget.h"
+#include "divepicturemodel.h"
+#include "metrics.h"
+#include "dive.h"
+#include "divelist.h"
+#include <unistd.h>
+#include <QtConcurrentMap>
+#include <QtConcurrentRun>
+#include <QFuture>
+#include <QDir>
+#include <QCryptographicHash>
+#include <QNetworkAccessManager>
+#include <QNetworkReply>
+#include <mainwindow.h>
+#include <qthelper.h>
+#include <QStandardPaths>
+
+void loadPicture(struct picture *picture)
+{
+ ImageDownloader download(picture);
+ download.load();
+}
+
+SHashedImage::SHashedImage(struct picture *picture) : QImage()
+{
+ QUrl url = QUrl::fromUserInput(QString(picture->filename));
+ if(url.isLocalFile())
+ load(url.toLocalFile());
+ if (isNull()) {
+ // Hash lookup.
+ load(fileFromHash(picture->hash));
+ if (!isNull()) {
+ QtConcurrent::run(updateHash, picture);
+ } else {
+ QtConcurrent::run(loadPicture, picture);
+ }
+ } else {
+ QByteArray hash = hashFile(url.toLocalFile());
+ free(picture->hash);
+ picture->hash = strdup(hash.toHex().data());
+ }
+}
+
+ImageDownloader::ImageDownloader(struct picture *pic)
+{
+ picture = pic;
+}
+
+void ImageDownloader::load(){
+ QUrl url = QUrl::fromUserInput(QString(picture->filename));
+ if (url.isValid()) {
+ QEventLoop loop;
+ QNetworkRequest request(url);
+ connect(&manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(saveImage(QNetworkReply *)));
+ QNetworkReply *reply = manager.get(request);
+ while (reply->isRunning()) {
+ loop.processEvents();
+ sleep(1);
+ }
+ }
+
+}
+
+void ImageDownloader::saveImage(QNetworkReply *reply)
+{
+ QByteArray imageData = reply->readAll();
+ QImage image = QImage();
+ image.loadFromData(imageData);
+ if (image.isNull())
+ return;
+ QCryptographicHash hash(QCryptographicHash::Sha1);
+ hash.addData(imageData);
+ QString path = QStandardPaths::standardLocations(QStandardPaths::CacheLocation).first();
+ QDir dir(path);
+ if (!dir.exists())
+ dir.mkpath(path);
+ QFile imageFile(path.append("/").append(hash.result().toHex()));
+ if (imageFile.open(QIODevice::WriteOnly)) {
+ QDataStream stream(&imageFile);
+ stream.writeRawData(imageData.data(), imageData.length());
+ imageFile.waitForBytesWritten(-1);
+ imageFile.close();
+ add_hash(imageFile.fileName(), hash.result());
+ learnHash(picture, hash.result());
+ DivePictureModel::instance()->updateDivePictures();
+ }
+ reply->manager()->deleteLater();
+ reply->deleteLater();
+}
+
+DivePictureWidget::DivePictureWidget(QWidget *parent) : QListView(parent)
+{
+ 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(localFilePath(filePath));
+}