From f1d67cfbacd1e879725e5ebeb2774439246764a4 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Sat, 31 May 2014 00:42:54 -0300 Subject: [PATCH] Add threaded processing of image pixmaps This code adds threaded processing of a batch of images. It uses the QtConcurrent implementations to call a function repeteadly using MAX_THREADS (Qt gets that for us) and returns a list of it. This call is blocking, so while the pixmaps are being scaled in threads, it will wait for all scalling to be done. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- qt-ui/divepicturewidget.cpp | 28 ++++++++++++++++++++++++++-- qt-ui/divepicturewidget.h | 4 ++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/qt-ui/divepicturewidget.cpp b/qt-ui/divepicturewidget.cpp index 11849af5f..eda4ff68c 100644 --- a/qt-ui/divepicturewidget.cpp +++ b/qt-ui/divepicturewidget.cpp @@ -1,5 +1,6 @@ #include "divepicturewidget.h" #include +#include void DivePictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { @@ -11,29 +12,52 @@ DivePictureModel::DivePictureModel(QObject *parent): QAbstractTableModel(parent) } +typedef QPair SPixmap; +typedef QList SPixmapList; + +SPixmap scaleImages(const QString& s){ + QPixmap p = QPixmap(s).scaled(128,128, Qt::KeepAspectRatio); + SPixmap ret; + ret.first = s; + ret.second = p; + return ret; +} + void DivePictureModel::updateDivePictures(int divenr) { + qDebug() << "Updating dive pictures."; beginRemoveRows(QModelIndex(), 0, numberOfPictures-1); numberOfPictures = 0; endRemoveRows(); + QStringList pictures; struct dive *d = get_dive(divenr); - if (!d) + if (!d){ + qDebug() << "Got no dive, exiting."; return; + } // All pictures are set in *all* divecomputers. ( waste of memory if > 100 pictures? ) // so just get from the first one. struct event *ev = d->dc.events; while(ev){ if(ev->type == 123){ // 123 means PICTURE. numberOfPictures++; + pictures.push_back(QString(ev->name)); } ev = ev->next; } - if (numberOfPictures == 0) + SPixmapList retList = QtConcurrent::blockingMapped( pictures, scaleImages); + Q_FOREACH(const SPixmap & pixmap, retList) + stringPixmapCache[pixmap.first] = pixmap.second; + + if (numberOfPictures == 0){ + qDebug() << "Got no pictures, exiting."; return; + } beginInsertRows(QModelIndex(), 0, numberOfPictures-1); endInsertRows(); + qDebug() << "Everything Ok."; } int DivePictureModel::columnCount(const QModelIndex &parent) const diff --git a/qt-ui/divepicturewidget.h b/qt-ui/divepicturewidget.h index 062e225d7..ff8d1aa8f 100644 --- a/qt-ui/divepicturewidget.h +++ b/qt-ui/divepicturewidget.h @@ -16,6 +16,10 @@ public: void updateDivePictures(int divenr); private: 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 stringPixmapCache; }; class DivePictureDelegate : QStyledItemDelegate {