diff options
Diffstat (limited to 'qt-ui/models.cpp')
-rw-r--r-- | qt-ui/models.cpp | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 6d6459983..03300a8d6 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -7,6 +7,8 @@ #include "models.h" #include "../helpers.h" #include "../dive.h" +#include "../device.h" + #include <QCoreApplication> #include <QDebug> #include <QColor> @@ -1152,3 +1154,115 @@ void DiveTripModel::setLayout(DiveTripModel::Layout layout) currentLayout = layout; setupModelData(); } + +/*#################################################################### + * + * Dive Computer Model + * + *#################################################################### + */ + +DiveComputerModel::DiveComputerModel(QObject* parent): QAbstractTableModel(parent) +{ + +} + +int DiveComputerModel::columnCount(const QModelIndex& parent) const +{ + return COLUMNS; +} + +QVariant DiveComputerModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + QVariant ret; + if (role != Qt::DisplayRole || orientation != Qt::Horizontal){ + return ret; + } + switch(section){ + case ID : ret = tr("Device ID"); break; + case MODEL : ret = tr("Model"); break; + case NICKNAME : ret = tr("Nickname"); break; + } + return ret; +} + +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; + } + + QVariant ret; + if (role == Qt::DisplayRole || role == Qt::EditRole){ + switch(index.column()){ + case ID : ret = device->deviceid; break; + case MODEL : ret = device->model; break; + case NICKNAME : ret = device->nickname; break; + } + } + + if (role == Qt::DecorationRole && index.column() == REMOVE){ + ret = QIcon(":trash"); + } + return ret; +} + +int DiveComputerModel::rowCount(const QModelIndex& parent) const +{ + return numRows; +} + +void DiveComputerModel::update() +{ + int count = 0; + struct device_info *nnl = head_of_device_info_list(); + while (nnl) { + nnl = nnl->next; + count++; + } + + if(numRows){ + beginRemoveRows(QModelIndex(), 0, numRows-1); + numRows = 0; + endRemoveRows(); + } + + if (count){ + beginInsertRows(QModelIndex(), 0, count-1); + numRows = count; + endInsertRows(); + } +} + +Qt::ItemFlags DiveComputerModel::flags(const QModelIndex& index) const +{ + Qt::ItemFlags flags = QAbstractItemModel::flags(index); + if (index.column() == NICKNAME) + flags |= Qt::ItemIsEditable; + return flags; +} + +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? + + return true; +} + +void DiveComputerModel::remove(const QModelIndex& i) +{ + 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); + update(); +} |