2017-04-18 15:32:10 +00:00
|
|
|
#include "diveimportedmodel.h"
|
2018-06-03 20:15:19 +00:00
|
|
|
#include "core/qthelper.h"
|
2017-04-18 15:32:10 +00:00
|
|
|
|
|
|
|
DiveImportedModel::DiveImportedModel(QObject *o) : QAbstractTableModel(o),
|
|
|
|
firstIndex(0),
|
|
|
|
lastIndex(-1),
|
2017-04-18 17:14:03 +00:00
|
|
|
diveTable(nullptr)
|
2017-04-18 15:32:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int DiveImportedModel::columnCount(const QModelIndex&) const
|
2017-04-18 15:32:10 +00:00
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int DiveImportedModel::rowCount(const QModelIndex&) const
|
2017-04-18 15:32:10 +00:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2017-06-04 12:40:25 +00:00
|
|
|
if (role >= DateTime) {
|
2017-05-26 15:53:25 +00:00
|
|
|
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:
|
2017-06-12 10:08:34 +00:00
|
|
|
return QVariant(get_dive_duration_string(d->duration.seconds, tr("h"), tr("min")));
|
2017-04-18 15:32:10 +00:00
|
|
|
case 2:
|
|
|
|
return QVariant(get_depth_string(d->maxdepth.mm, true, false));
|
2017-06-04 12:40:25 +00:00
|
|
|
case 3:
|
|
|
|
return checkStates[index.row()];
|
2017-04-18 15:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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()];
|
2017-06-04 12:40:25 +00:00
|
|
|
dataChanged(index(clickedIndex.row(), 0), index(clickedIndex.row(), 0), QVector<int>() << Qt::CheckStateRole << Selected);
|
2017-04-18 15:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveImportedModel::selectAll()
|
|
|
|
{
|
2018-12-09 18:10:55 +00:00
|
|
|
std::fill(checkStates.begin(), checkStates.end(), true);
|
2017-06-04 12:40:25 +00:00
|
|
|
dataChanged(index(0, 0), index(lastIndex - firstIndex, 0), QVector<int>() << Qt::CheckStateRole << Selected);
|
2017-04-18 15:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 18:36:00 +00:00
|
|
|
void DiveImportedModel::selectRow(int row)
|
|
|
|
{
|
|
|
|
checkStates[row] = !checkStates[row];
|
2017-06-04 12:40:25 +00:00
|
|
|
dataChanged(index(row, 0), index(row, 0), QVector<int>() << Qt::CheckStateRole << Selected);
|
2017-05-29 18:36:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 15:32:10 +00:00
|
|
|
void DiveImportedModel::selectNone()
|
|
|
|
{
|
2018-12-09 18:10:55 +00:00
|
|
|
std::fill(checkStates.begin(), checkStates.end(), false);
|
2017-06-04 12:40:25 +00:00
|
|
|
dataChanged(index(0, 0), index(lastIndex - firstIndex,0 ), QVector<int>() << Qt::CheckStateRole << Selected);
|
2017-04-18 15:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2017-06-24 19:06:19 +00:00
|
|
|
if (lastIndex < firstIndex) {
|
|
|
|
// just to be sure it's the right numbers
|
|
|
|
// but don't call RemoveRows or Qt in debug mode with trigger an ASSERT
|
|
|
|
lastIndex = -1;
|
|
|
|
firstIndex = 0;
|
|
|
|
return;
|
|
|
|
}
|
2017-04-18 15:32:10 +00:00
|
|
|
beginRemoveRows(QModelIndex(), 0, lastIndex - firstIndex);
|
|
|
|
lastIndex = -1;
|
|
|
|
firstIndex = 0;
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2018-12-23 11:46:25 +00:00
|
|
|
void DiveImportedModel::repopulate(dive_table_t *table, trip_table_t *trips)
|
2017-04-18 15:32:10 +00:00
|
|
|
{
|
2018-12-09 17:56:51 +00:00
|
|
|
beginResetModel();
|
|
|
|
|
2018-12-10 13:49:32 +00:00
|
|
|
diveTable = table;
|
2018-12-23 11:46:25 +00:00
|
|
|
tripTable = trips;
|
2018-12-09 17:56:51 +00:00
|
|
|
firstIndex = 0;
|
|
|
|
lastIndex = diveTable->nr - 1;
|
2018-12-09 18:10:55 +00:00
|
|
|
checkStates.resize(diveTable->nr);
|
|
|
|
std::fill(checkStates.begin(), checkStates.end(), true);
|
2017-05-26 15:53:25 +00:00
|
|
|
|
2018-12-09 17:56:51 +00:00
|
|
|
endResetModel();
|
2017-05-26 15:53:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 18:48:30 +00:00
|
|
|
void DiveImportedModel::recordDives()
|
|
|
|
{
|
2017-06-29 21:27:45 +00:00
|
|
|
if (diveTable->nr == 0)
|
|
|
|
// nothing to do, just exit
|
|
|
|
return;
|
|
|
|
|
2018-09-28 08:21:23 +00:00
|
|
|
// delete non-selected dives
|
|
|
|
int total = diveTable->nr;
|
|
|
|
int j = 0;
|
|
|
|
for (int i = 0; i < total; i++) {
|
|
|
|
if (checkStates[i])
|
|
|
|
j++;
|
|
|
|
else
|
2018-12-10 13:49:32 +00:00
|
|
|
delete_dive_from_table(diveTable, j);
|
2017-05-28 18:48:30 +00:00
|
|
|
}
|
2018-09-28 08:21:23 +00:00
|
|
|
|
2018-12-23 11:46:45 +00:00
|
|
|
process_imported_dives(diveTable, tripTable, true, true);
|
2017-05-28 18:48:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 15:53:25 +00:00
|
|
|
QHash<int, QByteArray> DiveImportedModel::roleNames() const {
|
|
|
|
static QHash<int, QByteArray> roles = {
|
|
|
|
{ DateTime, "datetime"},
|
|
|
|
{ Depth, "depth"},
|
2017-05-29 18:36:00 +00:00
|
|
|
{ Duration, "duration"},
|
2017-06-04 12:40:25 +00:00
|
|
|
{ Selected, "selected"}
|
2017-05-29 18:36:00 +00:00
|
|
|
};
|
2017-05-26 15:53:25 +00:00
|
|
|
return roles;
|
|
|
|
}
|