From 29b242c70349cbd67aacc3e4f1206630d22c54eb Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Mon, 17 Jun 2013 15:58:26 -0700 Subject: Converting the device_info list into a Qt data structure This data structure was quite fragile and made 'undo' when editing rather hard to implement. So instead I decided to turn this into a QMultiMap which seemed like the ideal data structure for it. This map holds all the dive computer related data indexed by the model. As QMultiMap it allows multiple entries per key (model string) and disambiguates between them with the deviceId. This commit turned out much larger than I wanted. But I didn't manage to find a clean way to break it up and make the pieces make sense. So this brings back the Ok / Cancel button for the dive computer edit dialog. And it makes those two buttons actually do the right thing (which is what started this whole process). For this to work we simply copy the map to a working copy and do all edits on that one - and then copy that over the 'real' map when we accept the changes. Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 63 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 31 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 9e076930d..c56fa2f44 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -8,7 +8,7 @@ #include "../helpers.h" #include "../dive.h" #include "../device.h" - +#include "../qthelper.h" #include #include #include @@ -1162,9 +1162,10 @@ void DiveTripModel::setLayout(DiveTripModel::Layout layout) *#################################################################### */ -DiveComputerModel::DiveComputerModel(QObject* parent): QAbstractTableModel(parent) +DiveComputerModel::DiveComputerModel(QMultiMap &dcMap, QObject* parent): QAbstractTableModel(parent) { - + dcWorkingMap = dcMap; + numRows = 0; } int DiveComputerModel::columnCount(const QModelIndex& parent) const @@ -1188,17 +1189,15 @@ QVariant DiveComputerModel::headerData(int section, Qt::Orientation orientation, QVariant DiveComputerModel::data(const QModelIndex& index, int role) const { - struct device_info *device = head_of_device_info_list(); - for(int i = 0; i < index.row(); i++){ - device = device->next; - } + QList values = dcWorkingMap.values(); + DiveComputerNode node = values.at(index.row()); QVariant ret; if (role == Qt::DisplayRole || role == Qt::EditRole){ switch(index.column()){ - case ID: ret = QString("0x").append(QString::number(device->deviceid, 16)); break; - case MODEL: ret = device->model; break; - case NICKNAME: ret = device->nickname; break; + case ID: ret = QString("0x").append(QString::number(node.deviceId, 16)); break; + case MODEL: ret = node.model; break; + case NICKNAME: ret = node.nickName; break; } } @@ -1215,12 +1214,8 @@ int DiveComputerModel::rowCount(const QModelIndex& parent) const void DiveComputerModel::update() { - int count = 0; - struct device_info *nnl = head_of_device_info_list(); - while (nnl) { - nnl = nnl->next; - count++; - } + QList values = dcWorkingMap.values(); + int count = values.count(); if(numRows){ beginRemoveRows(QModelIndex(), 0, numRows-1); @@ -1245,24 +1240,30 @@ Qt::ItemFlags DiveComputerModel::flags(const QModelIndex& index) const bool DiveComputerModel::setData(const QModelIndex& index, const QVariant& value, int role) { - struct device_info *nnl = head_of_device_info_list(); - - for(int i = 0; i < index.row(); i++){ - nnl = nnl->next; - } - - - QByteArray v = value.toByteArray(); - nnl->nickname = strdup(v.data()); // how should I free this before setting a new one? - // set_dc_nickname(dive); -> should this be used instead? - + QList values = dcWorkingMap.values(); + DiveComputerNode node = values.at(index.row()); + dcWorkingMap.remove(node.model, node); + node.nickName = value.toString(); + dcWorkingMap.insert(node.model, node); return true; } -void DiveComputerModel::remove(const QModelIndex& i) +void DiveComputerModel::remove(const QModelIndex& index) { - QByteArray model = data(index(i.row(), (int)MODEL)).toByteArray(); - uint32_t deviceid = data(index(i.row(), (int) ID)).toUInt(); - remove_dive_computer(model.data(), deviceid); + QList values = dcWorkingMap.values(); + DiveComputerNode node = values.at(index.row()); + dcWorkingMap.remove(node.model, node); update(); } + +void DiveComputerModel::dropWorkingList() +{ + // how do I prevent the memory leak ? +} + +void DiveComputerModel::keepWorkingList() +{ + if (dcList.dcMap != dcWorkingMap) + mark_divelist_changed(TRUE); + dcList.dcMap = dcWorkingMap; +} -- cgit v1.2.3-70-g09d2