Skip to content

Commit 852c268

Browse files
committed
Fix: Crash when clicking random ascent button on last ascent
1 parent 979c1b2 commit 852c268

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/ui/ascent_viewer.ui

+1-1
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@
935935
<set>Qt::AlignCenter</set>
936936
</property>
937937
<property name="textInteractionFlags">
938-
<set>Qt::TextBrowserInteraction</set>
938+
<set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse</set>
939939
</property>
940940
</widget>
941941
</item>

src/viewer/ascent_viewer.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,9 @@ void AscentViewer::updateInfoArea()
402402
*/
403403
void AscentViewer::updateAscentNavigationTargets()
404404
{
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);
407408

408409
firstAscentViewRowIndex = (currentViewRowIndex == minViewRowIndex) ? ViewRowIndex() : minViewRowIndex;
409410
previousAscentViewRowIndex = (currentViewRowIndex == minViewRowIndex) ? ViewRowIndex() : (currentViewRowIndex - 1);
@@ -473,10 +474,9 @@ void AscentViewer::updateAscentNavigationButtonsEnabled()
473474
*/
474475
void AscentViewer::updateAscentNavigationNumbers()
475476
{
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);
478478
allAscentsNumberLabel->setText(allAscentsNewText);
479-
allAscentsNumberLabel->setEnabled(numAscents > 0);
479+
allAscentsNumberLabel->setEnabled(numShownAscents > 0);
480480

481481
QString peakAscentsNewText = QString::number(currentAscentOfPeakIndex + 1) + " / " + QString::number(numAscentsOfPeak);
482482
ascentOfPeakNumberLabel->setText(peakAscentsNewText);
@@ -864,6 +864,7 @@ void AscentViewer::saveDescription()
864864
*/
865865
void AscentViewer::handle_firstAscent()
866866
{
867+
assert(firstAscentViewRowIndex.isValid(numShownAscents));
867868
changeToAscent(firstAscentViewRowIndex);
868869
}
869870

@@ -872,6 +873,7 @@ void AscentViewer::handle_firstAscent()
872873
*/
873874
void AscentViewer::handle_previousAscent()
874875
{
876+
assert(previousAscentViewRowIndex.isValid(numShownAscents));
875877
changeToAscent(previousAscentViewRowIndex);
876878
}
877879

@@ -880,6 +882,7 @@ void AscentViewer::handle_previousAscent()
880882
*/
881883
void AscentViewer::handle_nextAscent()
882884
{
885+
assert(nextAscentViewRowIndex.isValid(numShownAscents));
883886
changeToAscent(nextAscentViewRowIndex);
884887
}
885888

@@ -888,6 +891,7 @@ void AscentViewer::handle_nextAscent()
888891
*/
889892
void AscentViewer::handle_lastAscent()
890893
{
894+
assert(lastAscentViewRowIndex.isValid(numShownAscents));
891895
changeToAscent(lastAscentViewRowIndex);
892896
}
893897

@@ -898,9 +902,11 @@ void AscentViewer::handle_randomAscent()
898902
{
899903
QRandomGenerator rand = QRandomGenerator();
900904
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
902908
if (randomIndex == currentViewRowIndex) {
903-
randomIndex = lastAscentViewRowIndex;
909+
randomIndex = ViewRowIndex(numShownAscents - 1);
904910
}
905911
changeToAscent(ViewRowIndex(randomIndex));
906912
}
@@ -910,6 +916,7 @@ void AscentViewer::handle_randomAscent()
910916
*/
911917
void AscentViewer::handle_firstAscentOfPeak()
912918
{
919+
assert(firstAscentOfPeakViewRowIndex.isValid(numAscentsOfPeak));
913920
changeToAscent(firstAscentOfPeakViewRowIndex);
914921
}
915922

@@ -918,6 +925,7 @@ void AscentViewer::handle_firstAscentOfPeak()
918925
*/
919926
void AscentViewer::handle_previousAscentOfPeak()
920927
{
928+
assert(previousAscentOfPeakViewRowIndex.isValid(numAscentsOfPeak));
921929
changeToAscent(previousAscentOfPeakViewRowIndex);
922930
}
923931

@@ -926,6 +934,7 @@ void AscentViewer::handle_previousAscentOfPeak()
926934
*/
927935
void AscentViewer::handle_nextAscentOfPeak()
928936
{
937+
assert(nextAscentOfPeakViewRowIndex.isValid(numAscentsOfPeak));
929938
changeToAscent(nextAscentOfPeakViewRowIndex);
930939
}
931940

@@ -934,6 +943,7 @@ void AscentViewer::handle_nextAscentOfPeak()
934943
*/
935944
void AscentViewer::handle_lastAscentOfPeak()
936945
{
946+
assert(lastAscentOfPeakViewRowIndex.isValid(numAscentsOfPeak));
937947
changeToAscent(lastAscentOfPeakViewRowIndex);
938948
}
939949

src/viewer/ascent_viewer.h

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class AscentViewer : public QDialog, public Ui_AscentViewer {
6262
ViewRowIndex nextAscentViewRowIndex;
6363
/** The view row index of the last ascent in the composite ascents table. */
6464
ViewRowIndex lastAscentViewRowIndex;
65+
66+
/** The number of ascents shown in the composite ascents table. */
67+
int numShownAscents;
6568

6669
/** The view row index of the first ascent of the current ascent's peak in the composite ascents table. */
6770
ViewRowIndex firstAscentOfPeakViewRowIndex;

0 commit comments

Comments
 (0)