Dive pictures: change removePicture() interface

The function removePicture() had a flag "last", which would indicate
that the called had finished removing pictures. Only then would
the model be recalculated.

This is a strange interface and, matter of fact, the caller was buggy:
if the last picture to be removed didn't have a proper url, removePicture()
was never called with "last" being set.

Change the interface to take a list of pictures to be deleted. This
will allow us to make picture deletion smarter in follow-up commits.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-05-18 21:57:18 +02:00 committed by Lubomir I. Ivanov
parent fbe1144eaf
commit 3c0c1801cd
4 changed files with 27 additions and 19 deletions

View file

@ -60,23 +60,21 @@ void TabDivePhotos::contextMenuEvent(QContextMenuEvent *event)
void TabDivePhotos::removeSelectedPhotos() void TabDivePhotos::removeSelectedPhotos()
{ {
bool last = false;
if (!ui->photosView->selectionModel()->hasSelection()) if (!ui->photosView->selectionModel()->hasSelection())
return; return;
QModelIndexList indexes = ui->photosView->selectionModel()->selectedRows(); QModelIndexList indexes = ui->photosView->selectionModel()->selectedRows();
if (indexes.count() == 0) if (indexes.count() == 0)
indexes = ui->photosView->selectionModel()->selectedIndexes(); indexes = ui->photosView->selectionModel()->selectedIndexes();
QModelIndex photo = indexes.first(); QVector<QString> photosToDelete;
do { photosToDelete.reserve(indexes.count());
photo = indexes.first(); for (const auto &photo: indexes) {
last = indexes.count() == 1;
if (photo.isValid()) { if (photo.isValid()) {
QString fileUrl = photo.data(Qt::DisplayPropertyRole).toString(); QString fileUrl = photo.data(Qt::DisplayPropertyRole).toString();
if (fileUrl.length() > 0) if (!fileUrl.isEmpty())
DivePictureModel::instance()->removePicture(fileUrl, last); photosToDelete.push_back(fileUrl);
} }
indexes.removeFirst(); }
} while(!indexes.isEmpty()); DivePictureModel::instance()->removePictures(photosToDelete);
} }
//TODO: This looks overly wrong. We shouldn't call MainWindow to retrieve the DiveList to add Images. //TODO: This looks overly wrong. We shouldn't call MainWindow to retrieve the DiveList to add Images.

View file

@ -116,6 +116,6 @@ void DivePictureItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
void DivePictureItem::removePicture() void DivePictureItem::removePicture()
{ {
#ifndef SUBSURFACE_MOBILE #ifndef SUBSURFACE_MOBILE
DivePictureModel::instance()->removePicture(fileUrl, true); DivePictureModel::instance()->removePictures({ fileUrl });
#endif #endif
} }

View file

@ -127,19 +127,29 @@ QVariant DivePictureModel::data(const QModelIndex &index, int role) const
return ret; return ret;
} }
void DivePictureModel::removePicture(const QString &fileUrl, bool last) // Return true if we actually removed a picture
static bool removePictureFromSelectedDive(const char *fileUrl)
{ {
int i; int i;
struct dive *dive; struct dive *dive;
for_each_dive (i, dive) { for_each_dive (i, dive) {
if (dive->selected && dive_remove_picture(dive, qPrintable(fileUrl))) if (dive->selected && dive_remove_picture(dive, fileUrl))
break; return true;
}
if (last) {
copy_dive(current_dive, &displayed_dive);
updateDivePictures();
mark_divelist_changed(true);
} }
return false;
}
void DivePictureModel::removePictures(const QVector<QString> &fileUrls)
{
bool removed = false;
for (const QString &fileUrl: fileUrls)
removed |= removePictureFromSelectedDive(qPrintable(fileUrl));
if (!removed)
return;
copy_dive(current_dive, &displayed_dive);
mark_divelist_changed(true);
updateDivePictures();
} }
int DivePictureModel::rowCount(const QModelIndex &parent) const int DivePictureModel::rowCount(const QModelIndex &parent) const

View file

@ -22,7 +22,7 @@ public:
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual void updateDivePictures(); virtual void updateDivePictures();
void updateDivePicturesWhenDone(QList<QFuture<void>>); void updateDivePicturesWhenDone(QList<QFuture<void>>);
void removePicture(const QString& fileUrl, bool last); void removePictures(const QVector<QString> &fileUrls);
int rowDDStart, rowDDEnd; int rowDDStart, rowDDEnd;
void updateDivePictureOffset(const QString &filename, int offsetSeconds); void updateDivePictureOffset(const QString &filename, int offsetSeconds);
public slots: public slots: