@@ -402,8 +402,9 @@ void AscentViewer::updateInfoArea()
402
402
*/
403
403
void AscentViewer::updateAscentNavigationTargets ()
404
404
{
405
- ViewRowIndex minViewRowIndex = ViewRowIndex (0 );
406
- ViewRowIndex maxViewRowIndex = ViewRowIndex (compAscents->rowCount () - 1 );
405
+ numShownAscents = compAscents->rowCount ();
406
+ const ViewRowIndex minViewRowIndex = ViewRowIndex (0 );
407
+ const ViewRowIndex maxViewRowIndex = ViewRowIndex (numShownAscents - 1 );
407
408
408
409
firstAscentViewRowIndex = (currentViewRowIndex == minViewRowIndex) ? ViewRowIndex () : minViewRowIndex;
409
410
previousAscentViewRowIndex = (currentViewRowIndex == minViewRowIndex) ? ViewRowIndex () : (currentViewRowIndex - 1 );
@@ -473,10 +474,9 @@ void AscentViewer::updateAscentNavigationButtonsEnabled()
473
474
*/
474
475
void AscentViewer::updateAscentNavigationNumbers ()
475
476
{
476
- int numAscents = compAscents->rowCount ();
477
- QString allAscentsNewText = QString::number (currentViewRowIndex.get () + 1 ) + " / " + QString::number (numAscents);
477
+ QString allAscentsNewText = QString::number (currentViewRowIndex.get () + 1 ) + " / " + QString::number (numShownAscents);
478
478
allAscentsNumberLabel->setText (allAscentsNewText);
479
- allAscentsNumberLabel->setEnabled (numAscents > 0 );
479
+ allAscentsNumberLabel->setEnabled (numShownAscents > 0 );
480
480
481
481
QString peakAscentsNewText = QString::number (currentAscentOfPeakIndex + 1 ) + " / " + QString::number (numAscentsOfPeak);
482
482
ascentOfPeakNumberLabel->setText (peakAscentsNewText);
@@ -864,6 +864,7 @@ void AscentViewer::saveDescription()
864
864
*/
865
865
void AscentViewer::handle_firstAscent ()
866
866
{
867
+ assert (firstAscentViewRowIndex.isValid (numShownAscents));
867
868
changeToAscent (firstAscentViewRowIndex);
868
869
}
869
870
@@ -872,6 +873,7 @@ void AscentViewer::handle_firstAscent()
872
873
*/
873
874
void AscentViewer::handle_previousAscent ()
874
875
{
876
+ assert (previousAscentViewRowIndex.isValid (numShownAscents));
875
877
changeToAscent (previousAscentViewRowIndex);
876
878
}
877
879
@@ -880,6 +882,7 @@ void AscentViewer::handle_previousAscent()
880
882
*/
881
883
void AscentViewer::handle_nextAscent ()
882
884
{
885
+ assert (nextAscentViewRowIndex.isValid (numShownAscents));
883
886
changeToAscent (nextAscentViewRowIndex);
884
887
}
885
888
@@ -888,6 +891,7 @@ void AscentViewer::handle_nextAscent()
888
891
*/
889
892
void AscentViewer::handle_lastAscent ()
890
893
{
894
+ assert (lastAscentViewRowIndex.isValid (numShownAscents));
891
895
changeToAscent (lastAscentViewRowIndex);
892
896
}
893
897
@@ -898,9 +902,11 @@ void AscentViewer::handle_randomAscent()
898
902
{
899
903
QRandomGenerator rand = QRandomGenerator ();
900
904
rand .seed (QDateTime::currentMSecsSinceEpoch ());
901
- ViewRowIndex randomIndex = ViewRowIndex (rand .bounded (lastAscentViewRowIndex.get ()));
905
+ // Generate random ViewRowIndex between 0 and numShownAscents - 2 (excluding the last ascent)
906
+ ViewRowIndex randomIndex = ViewRowIndex (rand .bounded (numShownAscents - 1 ));
907
+ // If the random index is the same as the current one, use the last ascent instead
902
908
if (randomIndex == currentViewRowIndex) {
903
- randomIndex = lastAscentViewRowIndex ;
909
+ randomIndex = ViewRowIndex (numShownAscents - 1 ) ;
904
910
}
905
911
changeToAscent (ViewRowIndex (randomIndex));
906
912
}
@@ -910,6 +916,7 @@ void AscentViewer::handle_randomAscent()
910
916
*/
911
917
void AscentViewer::handle_firstAscentOfPeak ()
912
918
{
919
+ assert (firstAscentOfPeakViewRowIndex.isValid (numAscentsOfPeak));
913
920
changeToAscent (firstAscentOfPeakViewRowIndex);
914
921
}
915
922
@@ -918,6 +925,7 @@ void AscentViewer::handle_firstAscentOfPeak()
918
925
*/
919
926
void AscentViewer::handle_previousAscentOfPeak ()
920
927
{
928
+ assert (previousAscentOfPeakViewRowIndex.isValid (numAscentsOfPeak));
921
929
changeToAscent (previousAscentOfPeakViewRowIndex);
922
930
}
923
931
@@ -926,6 +934,7 @@ void AscentViewer::handle_previousAscentOfPeak()
926
934
*/
927
935
void AscentViewer::handle_nextAscentOfPeak ()
928
936
{
937
+ assert (nextAscentOfPeakViewRowIndex.isValid (numAscentsOfPeak));
929
938
changeToAscent (nextAscentOfPeakViewRowIndex);
930
939
}
931
940
@@ -934,6 +943,7 @@ void AscentViewer::handle_nextAscentOfPeak()
934
943
*/
935
944
void AscentViewer::handle_lastAscentOfPeak ()
936
945
{
946
+ assert (lastAscentOfPeakViewRowIndex.isValid (numAscentsOfPeak));
937
947
changeToAscent (lastAscentOfPeakViewRowIndex);
938
948
}
939
949
0 commit comments