summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2020-05-04 09:47:55 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2020-05-06 13:58:09 -0700
commit4d407dc666ba471946541c835da3bb5805b514bc (patch)
tree3994e6ea0bc19352bccfd7185615ee33051273a4
parentc50e58d761c0c6e54219a3114a26bc033ddeee70 (diff)
downloadsubsurface-4d407dc666ba471946541c835da3bb5805b514bc.tar.gz
pictures: turn QString into std::string for filenames
For undo of picture manipulation, it will be crucial that the model and the core have the same order of pictures. The first sort criterion will be time, the second filename in the case that two pictures have, for whatever reason, the same timestamp. However in the core we us C-strings and thus sort byte-wise using strcmp. In the Qt-part we use QStrings, which sort according to unicode encoding. To enable consistent sorting, change the Qt-part to std::string, which uses a C-style 0-terminated string as its backing store. One might argue that in general filenames should use system-encoding and therefore use std::string instead of QString. However, a broader conversion to std::string turned out to be very painful, since Qt is (deliberately?) difficult to use with std::string. Notable all the file-manipulation functions don't take std::string by default. Thus, this commit only converts the internal data of DivePictureModel, but continues to use QString for the Qt-facing interface. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r--qt-models/divepicturemodel.cpp32
-rw-r--r--qt-models/divepicturemodel.h6
2 files changed, 24 insertions, 14 deletions
diff --git a/qt-models/divepicturemodel.cpp b/qt-models/divepicturemodel.cpp
index aab0e7f6f..799c49c0e 100644
--- a/qt-models/divepicturemodel.cpp
+++ b/qt-models/divepicturemodel.cpp
@@ -43,7 +43,7 @@ void DivePictureModel::updateThumbnails()
{
updateZoom();
for (PictureEntry &entry: pictures)
- entry.image = Thumbnailer::instance()->fetchThumbnail(entry.filename, false);
+ entry.image = Thumbnailer::instance()->fetchThumbnail(QString::fromStdString(entry.filename), false);
}
void DivePictureModel::updateDivePictures()
@@ -87,13 +87,13 @@ QVariant DivePictureModel::data(const QModelIndex &index, int role) const
if (index.column() == 0) {
switch (role) {
case Qt::ToolTipRole:
- return entry.filename;
+ return QString::fromStdString(entry.filename);
case Qt::DecorationRole:
return entry.image.scaled(size, size, Qt::KeepAspectRatio);
case Qt::DisplayRole:
- return QFileInfo(entry.filename).fileName();
+ return QFileInfo(QString::fromStdString(entry.filename)).fileName();
case Qt::DisplayPropertyRole:
- return QFileInfo(entry.filename).filePath();
+ return QFileInfo(QString::fromStdString(entry.filename)).filePath();
case Qt::UserRole:
return entry.diveId;
case Qt::UserRole + 1:
@@ -104,7 +104,7 @@ QVariant DivePictureModel::data(const QModelIndex &index, int role) const
} else if (index.column() == 1) {
switch (role) {
case Qt::DisplayRole:
- return entry.filename;
+ return QString::fromStdString(entry.filename);
}
}
return QVariant();
@@ -122,11 +122,17 @@ static bool removePictureFromSelectedDive(const char *fileUrl)
return false;
}
-void DivePictureModel::removePictures(const QVector<QString> &fileUrls)
+void DivePictureModel::removePictures(const QVector<QString> &fileUrlsIn)
{
+ // Transform vector of QStrings into vector of std::strings
+ std::vector<std::string> fileUrls;
+ fileUrls.reserve(fileUrlsIn.size());
+ std::transform(fileUrlsIn.begin(), fileUrlsIn.end(), std::back_inserter(fileUrls),
+ [] (const QString &s) { return s.toStdString(); });
+
bool removed = false;
- for (const QString &fileUrl: fileUrls)
- removed |= removePictureFromSelectedDive(qPrintable(fileUrl));
+ for (const std::string &fileUrl: fileUrls)
+ removed |= removePictureFromSelectedDive(fileUrl.c_str());
if (!removed)
return;
copy_dive(current_dive, &displayed_dive);
@@ -148,7 +154,7 @@ void DivePictureModel::removePictures(const QVector<QString> &fileUrls)
pictures.erase(pictures.begin() + i, pictures.begin() + j);
endRemoveRows();
}
- emit picturesRemoved(fileUrls);
+ emit picturesRemoved(fileUrlsIn);
}
int DivePictureModel::rowCount(const QModelIndex&) const
@@ -156,7 +162,7 @@ int DivePictureModel::rowCount(const QModelIndex&) const
return pictures.count();
}
-int DivePictureModel::findPictureId(const QString &filename)
+int DivePictureModel::findPictureId(const std::string &filename)
{
for (int i = 0; i < pictures.size(); ++i)
if (pictures[i].filename == filename)
@@ -193,7 +199,7 @@ static void addDurationToThumbnail(QImage &img, duration_t duration)
void DivePictureModel::updateThumbnail(QString filename, QImage thumbnail, duration_t duration)
{
- int i = findPictureId(filename);
+ int i = findPictureId(filename.toStdString());
if (i >= 0) {
if (duration.seconds > 0) {
addDurationToThumbnail(thumbnail, duration); // If we know the duration paint it on top of the thumbnail
@@ -204,8 +210,10 @@ void DivePictureModel::updateThumbnail(QString filename, QImage thumbnail, durat
}
}
-void DivePictureModel::updateDivePictureOffset(int diveId, const QString &filename, int offsetSeconds)
+void DivePictureModel::updateDivePictureOffset(int diveId, const QString &filenameIn, int offsetSeconds)
{
+ std::string filename = filenameIn.toStdString();
+
// Find the pictures of the given dive.
auto from = std::find_if(pictures.begin(), pictures.end(), [diveId](const PictureEntry &e) { return e.diveId == diveId; });
auto to = std::find_if(from, pictures.end(), [diveId](const PictureEntry &e) { return e.diveId != diveId; });
diff --git a/qt-models/divepicturemodel.h b/qt-models/divepicturemodel.h
index 5bee26864..1ed5e8095 100644
--- a/qt-models/divepicturemodel.h
+++ b/qt-models/divepicturemodel.h
@@ -8,9 +8,11 @@
#include <QImage>
#include <QFuture>
+// We use std::string instead of QString to use the same character-encoding
+// as in the C core (UTF-8). This is crucial to guarantee the same sort-order.
struct PictureEntry {
int diveId;
- QString filename;
+ std::string filename;
QImage image;
int offsetSeconds;
duration_t length;
@@ -34,7 +36,7 @@ public slots:
private:
DivePictureModel();
QVector<PictureEntry> pictures;
- int findPictureId(const QString &filename); // Return -1 if not found
+ int findPictureId(const std::string &filename); // Return -1 if not found
double zoomLevel; // -1.0: minimum, 0.0: standard, 1.0: maximum
int size;
void updateThumbnails();