2017-04-18 15:32:10 +00:00
|
|
|
#include "diveimportedmodel.h"
|
|
|
|
#include "core/helpers.h"
|
|
|
|
|
|
|
|
DiveImportedModel::DiveImportedModel(QObject *o) : QAbstractTableModel(o),
|
|
|
|
firstIndex(0),
|
|
|
|
lastIndex(-1),
|
2017-04-18 17:14:03 +00:00
|
|
|
checkStates(nullptr),
|
|
|
|
diveTable(nullptr)
|
2017-04-18 15:32:10 +00:00
|
|
|
{
|
2017-05-26 15:53:25 +00:00
|
|
|
// Defaults to downloadTable, can be changed later.
|
|
|
|
diveTable = &downloadTable;
|
2017-04-18 15:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DiveImportedModel::columnCount(const QModelIndex &model) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(model)
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DiveImportedModel::rowCount(const QModelIndex &model) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(model)
|
|
|
|
return lastIndex - firstIndex + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DiveImportedModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if (orientation == Qt::Vertical)
|
|
|
|
return QVariant();
|
2017-05-26 15:53:25 +00:00
|
|
|
|
|
|
|
// widgets access the model via index.column(), qml via role.
|
|
|
|
int column = section;
|
|
|
|
if (role == DateTime || role == Duration || role == Depth) {
|
|
|
|
column = role - DateTime;
|
|
|
|
role = Qt::DisplayRole;
|
|
|
|
}
|
|
|
|
|
2017-04-18 15:32:10 +00:00
|
|
|
if (role == Qt::DisplayRole) {
|
2017-05-26 15:53:25 +00:00
|
|
|
switch (column) {
|
2017-04-18 15:32:10 +00:00
|
|
|
case 0:
|
|
|
|
return QVariant(tr("Date/time"));
|
|
|
|
case 1:
|
|
|
|
return QVariant(tr("Duration"));
|
|
|
|
case 2:
|
|
|
|
return QVariant(tr("Depth"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2017-04-18 17:14:03 +00:00
|
|
|
void DiveImportedModel::setDiveTable(struct dive_table* table)
|
|
|
|
{
|
2017-05-26 15:53:25 +00:00
|
|
|
diveTable = table;
|
2017-04-18 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 15:32:10 +00:00
|
|
|
QVariant DiveImportedModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
if (index.row() + firstIndex > lastIndex)
|
|
|
|
return QVariant();
|
|
|
|
|
2017-04-18 17:14:03 +00:00
|
|
|
struct dive *d = get_dive_from_table(index.row() + firstIndex, diveTable);
|
2017-04-18 15:32:10 +00:00
|
|
|
if (!d)
|
|
|
|
return QVariant();
|
2017-05-26 15:53:25 +00:00
|
|
|
|
|
|
|
// widgets access the model via index.column(), qml via role.
|
|
|
|
int column = index.column();
|
|
|
|
if (role == DateTime || role == Duration || role == Depth) {
|
|
|
|
column = role - DateTime;
|
|
|
|
role = Qt::DisplayRole;
|
|
|
|
}
|
|
|
|
|
2017-04-18 15:32:10 +00:00
|
|
|
if (role == Qt::DisplayRole) {
|
2017-05-26 15:53:25 +00:00
|
|
|
switch (column) {
|
2017-04-18 15:32:10 +00:00
|
|
|
case 0:
|
|
|
|
return QVariant(get_short_dive_date_string(d->when));
|
|
|
|
case 1:
|
|
|
|
return QVariant(get_dive_duration_string(d->duration.seconds, tr("h:"), tr("min")));
|
|
|
|
case 2:
|
|
|
|
return QVariant(get_depth_string(d->maxdepth.mm, true, false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (role == Qt::CheckStateRole) {
|
|
|
|
if (index.column() == 0)
|
|
|
|
return checkStates[index.row()] ? Qt::Checked : Qt::Unchecked;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveImportedModel::changeSelected(QModelIndex clickedIndex)
|
|
|
|
{
|
|
|
|
checkStates[clickedIndex.row()] = !checkStates[clickedIndex.row()];
|
|
|
|
dataChanged(index(clickedIndex.row(), 0), index(clickedIndex.row(), 0), QVector<int>() << Qt::CheckStateRole);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveImportedModel::selectAll()
|
|
|
|
{
|
|
|
|
memset(checkStates, true, lastIndex - firstIndex + 1);
|
|
|
|
dataChanged(index(0, 0), index(lastIndex - firstIndex, 0), QVector<int>() << Qt::CheckStateRole);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveImportedModel::selectNone()
|
|
|
|
{
|
|
|
|
memset(checkStates, false, lastIndex - firstIndex + 1);
|
|
|
|
dataChanged(index(0, 0), index(lastIndex - firstIndex,0 ), QVector<int>() << Qt::CheckStateRole);
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags DiveImportedModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (index.column() != 0)
|
|
|
|
return QAbstractTableModel::flags(index);
|
|
|
|
return QAbstractTableModel::flags(index) | Qt::ItemIsUserCheckable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveImportedModel::clearTable()
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), 0, lastIndex - firstIndex);
|
|
|
|
lastIndex = -1;
|
|
|
|
firstIndex = 0;
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveImportedModel::setImportedDivesIndexes(int first, int last)
|
|
|
|
{
|
|
|
|
Q_ASSERT(last >= first);
|
|
|
|
if (lastIndex >= firstIndex) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, lastIndex - firstIndex);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
beginInsertRows(QModelIndex(), 0, last - first);
|
|
|
|
lastIndex = last;
|
|
|
|
firstIndex = first;
|
|
|
|
delete[] checkStates;
|
|
|
|
checkStates = new bool[last - first + 1];
|
|
|
|
memset(checkStates, true, last - first + 1);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
2017-05-26 15:53:25 +00:00
|
|
|
|
|
|
|
void DiveImportedModel::repopulate()
|
|
|
|
{
|
|
|
|
setImportedDivesIndexes(0, diveTable->nr-1);
|
|
|
|
}
|
|
|
|
|
2017-05-28 18:48:30 +00:00
|
|
|
void DiveImportedModel::recordDives()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < rowCount(); i++) {
|
|
|
|
if (diveTable->dives[i] && checkStates[i]) {
|
|
|
|
record_dive(diveTable->dives[i]);
|
|
|
|
diveTable->dives[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
diveTable->nr = 0;
|
|
|
|
}
|
|
|
|
|
2017-05-26 15:53:25 +00:00
|
|
|
QHash<int, QByteArray> DiveImportedModel::roleNames() const {
|
|
|
|
static QHash<int, QByteArray> roles = {
|
|
|
|
{ DateTime, "datetime"},
|
|
|
|
{ Depth, "depth"},
|
|
|
|
{ Duration, "duration"}};
|
|
|
|
return roles;
|
|
|
|
}
|