From e3118d915cf49de4cc722522031b0be44014d790 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Tue, 3 Oct 2017 23:16:15 +0300 Subject: divelist: prevent a crash for missing column width The `static int defaultWidth[]` definition in divelistview.cpp could potentially end up missing an element which can later result in out-of-bounds access when iterating through the list of columns and updating their widths. Add a couple of methods in DiveTripModel for setting and getting the widths and use those. The default values are now pre-set in a QVector in the DiveTripModel() constructor. Throw warnings if out-of-bounds columns are requested. Reported-by: Gaetan Bisson Signed-off-by: Lubomir I. Ivanov --- qt-models/divetripmodel.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'qt-models/divetripmodel.cpp') diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index 4082fa989..5777ae88a 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -6,6 +6,7 @@ #include "core/helpers.h" #include "core/dive.h" #include +#include static int nitrox_sort_value(struct dive *dive) { @@ -407,6 +408,18 @@ DiveTripModel::DiveTripModel(QObject *parent) : currentLayout(TREE) { columns = COLUMNS; + // setup the default width of columns (px) + columnWidthMap = QVector(COLUMNS); + // pre-fill with 50px; the rest are explicit + for(int i = 0; i < COLUMNS; i++) + columnWidthMap[i] = 50; + columnWidthMap[NR] = 70; + columnWidthMap[DATE] = 140; + columnWidthMap[RATING] = 90; + columnWidthMap[SUIT] = 70; + columnWidthMap[SAC] = 70; + columnWidthMap[PHOTOS] = 5; + columnWidthMap[LOCATION] = 500; } Qt::ItemFlags DiveTripModel::flags(const QModelIndex &index) const @@ -605,3 +618,21 @@ bool DiveTripModel::setData(const QModelIndex &index, const QVariant &value, int return false; return diveItem->setData(index, value, role); } + +int DiveTripModel::columnWidth(int column) +{ + if (column > COLUMNS - 1 || column < 0) { + qWarning() << "DiveTripModel::columnWidth(): not a valid column index -" << column; + return 50; + } + return columnWidthMap[column]; +} + +void DiveTripModel::setColumnWidth(int column, int width) +{ + if (column > COLUMNS - 1 || column < 0) { + qWarning() << "DiveTripModel::setColumnWidth(): not a valid column index -" << column; + return; + } + columnWidthMap[column] = width; +} -- cgit v1.2.3-70-g09d2