Skip to content

Commit 8683348

Browse files
committed
Fix: Save photo description when adding photos
Consolidated AscentViewer::changeToPhoto(...) and AscentViewer::updatePhoto() Closes #165
1 parent 4fb185f commit 8683348

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

src/viewer/ascent_viewer.cpp

+23-27
Original file line numberDiff line numberDiff line change
@@ -378,25 +378,20 @@ void AscentViewer::setupPhotos()
378378

379379

380380

381-
void AscentViewer::changeToPhoto(int photoIndex)
381+
void AscentViewer::changeToPhoto(int photoIndex, bool saveDescriptionFirst)
382382
{
383-
savePhotoDescription();
383+
if (saveDescriptionFirst) {
384+
savePhotoDescription();
385+
}
384386

385387
currentPhotoIndex = photoIndex;
386-
updatePhoto();
387-
updatePhotoButtonsEnabled();
388-
}
389-
390-
void AscentViewer::updatePhoto()
391-
{
388+
392389
photoDescriptionLabel ->setText(QString());
393390
photoDescriptionLineEdit->setText(QString());
394391
photoDescriptionLabel ->setVisible(false);
395392
photoDescriptionLineEdit->setVisible(false);
396393
imageLabel ->setToolTip(QString());
397394

398-
updatePhotoIndexLabel();
399-
400395
if (currentPhotoIndex < 0 || photos.isEmpty()) {
401396
imageLabel->clearImage();
402397
return;
@@ -418,21 +413,23 @@ void AscentViewer::updatePhoto()
418413
QMessageBox::StandardButton result = QMessageBox::warning(this, title, message, buttons);
419414

420415
if (result == QMessageBox::Yes) {
421-
removeCurrentPhoto();
422-
updatePhoto();
416+
removeCurrentPhoto(); // calls changeToPhoto() back, recursing until valid image is found or all photos removed
423417
}
424-
return;
425418
}
426-
427-
image = newImage;
428-
if (image.colorSpace().isValid()) image.convertToColorSpace(QColorSpace::SRgb);
429-
imageLabel->setImage(image);
419+
else {
420+
image = newImage;
421+
if (image.colorSpace().isValid()) image.convertToColorSpace(QColorSpace::SRgb);
422+
imageLabel->setImage(image);
423+
}
430424

431425
photoDescriptionLabel ->setText(photos.at(currentPhotoIndex).description);
432426
photoDescriptionLineEdit->setText(photos.at(currentPhotoIndex).description);
433427
photoDescriptionLabel ->setVisible(!photoDescriptionEditable);
434428
photoDescriptionLineEdit->setVisible(photoDescriptionEditable);
435429
imageLabel ->setToolTip(filepath);
430+
431+
updatePhotoIndexLabel();
432+
updatePhotoButtonsEnabled();
436433
}
437434

438435
void AscentViewer::updatePhotoIndexLabel()
@@ -481,26 +478,25 @@ void AscentViewer::addPhotos()
481478
QStringList filepaths = openFileDialogForPhotosSelection(this);
482479
if (filepaths.isEmpty()) return;
483480

481+
savePhotoDescription();
482+
484483
if (currentPhotoIndex < 0) currentPhotoIndex = -1;
485484
currentPhotoIndex++; // Set to index of first inserted photo
486485
for (int i = 0; i < filepaths.size(); i++) {
487486
photos.insert(currentPhotoIndex + i, Photo(currentAscentID, ItemID(), -1, filepaths.at(i), QString()));
488487
}
489-
490488
savePhotosList();
491489

492-
updatePhoto();
493-
updatePhotoButtonsEnabled();
490+
changeToPhoto(currentPhotoIndex);
494491
}
495492

496493
void AscentViewer::removeCurrentPhoto()
497494
{
498495
photos.removeAt(currentPhotoIndex);
499496
savePhotosList();
500497

501-
if (currentPhotoIndex >= photos.size()) currentPhotoIndex = photos.size() - 1;
502-
updatePhotoButtonsEnabled();
503-
updatePhoto();
498+
int newPhotoIndex = std::min(currentPhotoIndex, (int) photos.size() - 1);
499+
changeToPhoto(newPhotoIndex);
504500
}
505501

506502
void AscentViewer::savePhotoDescription()
@@ -589,22 +585,22 @@ void AscentViewer::handle_lastAscentOfPeak()
589585

590586
void AscentViewer::handle_firstPhoto()
591587
{
592-
changeToPhoto(0);
588+
changeToPhoto(0, true);
593589
}
594590

595591
void AscentViewer::handle_previousPhoto()
596592
{
597-
changeToPhoto(currentPhotoIndex - 1);
593+
changeToPhoto(currentPhotoIndex - 1, true);
598594
}
599595

600596
void AscentViewer::handle_nextPhoto()
601597
{
602-
changeToPhoto(currentPhotoIndex + 1);
598+
changeToPhoto(currentPhotoIndex + 1, true);
603599
}
604600

605601
void AscentViewer::handle_lastPhoto()
606602
{
607-
changeToPhoto(photos.size() - 1);
603+
changeToPhoto(photos.size() - 1, true);
608604
}
609605

610606

src/viewer/ascent_viewer.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ class AscentViewer : public QDialog, public Ui_AscentViewer {
8585
void setupPhotos();
8686

8787
// Photo change
88-
void changeToPhoto(int photoIndex);
89-
void updatePhoto();
88+
void changeToPhoto(int photoIndex, bool saveDescriptionFirst = false);
9089
void updatePhotoIndexLabel();
9190
void updatePhotoButtonsEnabled();
9291

0 commit comments

Comments
 (0)