mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
core: C++-ify statistics.c
The old code was wild: For the yearly statistics it would allocate one entry per dive in the log. Of course, it would also leak C-style strings. Convert the whole thing to somewhat idiomatic C++. Somewhat wasted work, because I'd like to convert the whole thing to the new statistics code. But let's finish the conversion to C++ first. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
84219641de
commit
1daa4f0584
12 changed files with 496 additions and 582 deletions
|
@ -42,77 +42,63 @@ YearStatisticsItem::YearStatisticsItem(const stats_t &interval) : stats_interval
|
|||
|
||||
QVariant YearStatisticsItem::data(int column, int role) const
|
||||
{
|
||||
QVariant ret;
|
||||
|
||||
if (role == Qt::FontRole) {
|
||||
QFont font = defaultModelFont();
|
||||
font.setBold(stats_interval.is_year);
|
||||
return font;
|
||||
} else if (role != Qt::DisplayRole) {
|
||||
return ret;
|
||||
return QVariant();
|
||||
}
|
||||
switch (column) {
|
||||
case YEAR:
|
||||
if (stats_interval.is_trip) {
|
||||
ret = QString(stats_interval.location);
|
||||
return QString::fromStdString(stats_interval.location);
|
||||
} else {
|
||||
ret = stats_interval.period;
|
||||
return stats_interval.period;
|
||||
}
|
||||
break;
|
||||
case DIVES:
|
||||
ret = stats_interval.selection_size;
|
||||
break;
|
||||
return stats_interval.selection_size;
|
||||
case TOTAL_TIME:
|
||||
ret = get_dive_duration_string(stats_interval.total_time.seconds, tr("h"), tr("min"), tr("sec"), " ");
|
||||
break;
|
||||
return get_dive_duration_string(stats_interval.total_time.seconds, tr("h"), tr("min"), tr("sec"), " ");
|
||||
case AVERAGE_TIME:
|
||||
ret = formatMinutes(stats_interval.total_time.seconds / stats_interval.selection_size);
|
||||
break;
|
||||
return formatMinutes(stats_interval.total_time.seconds / stats_interval.selection_size);
|
||||
case SHORTEST_TIME:
|
||||
ret = formatMinutes(stats_interval.shortest_time.seconds);
|
||||
break;
|
||||
return formatMinutes(stats_interval.shortest_time.seconds);
|
||||
case LONGEST_TIME:
|
||||
ret = formatMinutes(stats_interval.longest_time.seconds);
|
||||
break;
|
||||
return formatMinutes(stats_interval.longest_time.seconds);
|
||||
case AVG_DEPTH:
|
||||
ret = get_depth_string(stats_interval.avg_depth);
|
||||
break;
|
||||
return get_depth_string(stats_interval.avg_depth);
|
||||
case AVG_MAX_DEPTH:
|
||||
if (stats_interval.selection_size)
|
||||
ret = get_depth_string(stats_interval.combined_max_depth.mm / stats_interval.selection_size);
|
||||
return get_depth_string(stats_interval.combined_max_depth.mm / stats_interval.selection_size);
|
||||
break;
|
||||
case MIN_DEPTH:
|
||||
ret = get_depth_string(stats_interval.min_depth);
|
||||
break;
|
||||
return get_depth_string(stats_interval.min_depth);
|
||||
case MAX_DEPTH:
|
||||
ret = get_depth_string(stats_interval.max_depth);
|
||||
break;
|
||||
return get_depth_string(stats_interval.max_depth);
|
||||
case AVG_SAC:
|
||||
ret = get_volume_string(stats_interval.avg_sac);
|
||||
break;
|
||||
return get_volume_string(stats_interval.avg_sac);
|
||||
case MIN_SAC:
|
||||
ret = get_volume_string(stats_interval.min_sac);
|
||||
break;
|
||||
return get_volume_string(stats_interval.min_sac);
|
||||
case MAX_SAC:
|
||||
ret = get_volume_string(stats_interval.max_sac);
|
||||
break;
|
||||
return get_volume_string(stats_interval.max_sac);
|
||||
case AVG_TEMP:
|
||||
if (stats_interval.combined_temp.mkelvin && stats_interval.combined_count) {
|
||||
temperature_t avg_temp;
|
||||
avg_temp.mkelvin = stats_interval.combined_temp.mkelvin / stats_interval.combined_count;
|
||||
ret = get_temperature_string(avg_temp);
|
||||
return get_temperature_string(avg_temp);
|
||||
}
|
||||
break;
|
||||
case MIN_TEMP:
|
||||
if (stats_interval.min_temp.mkelvin)
|
||||
ret = get_temperature_string(stats_interval.min_temp);
|
||||
return get_temperature_string(stats_interval.min_temp);
|
||||
break;
|
||||
case MAX_TEMP:
|
||||
if (stats_interval.max_temp.mkelvin)
|
||||
ret = get_temperature_string(stats_interval.max_temp);
|
||||
return get_temperature_string(stats_interval.max_temp);
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
YearlyStatisticsModel::YearlyStatisticsModel(QObject *parent) : TreeModel(parent)
|
||||
|
@ -184,17 +170,15 @@ QVariant YearlyStatisticsModel::headerData(int section, Qt::Orientation orientat
|
|||
|
||||
void YearlyStatisticsModel::update_yearly_stats()
|
||||
{
|
||||
int i, month = 0;
|
||||
unsigned int j, combined_months;
|
||||
stats_summary_auto_free stats;
|
||||
QString label;
|
||||
temperature_t t_range_min,t_range_max;
|
||||
calculate_stats_summary(&stats, false);
|
||||
stats_summary stats = calculate_stats_summary(false);
|
||||
|
||||
for (i = 0; stats.stats_yearly != NULL && stats.stats_yearly[i].period; ++i) {
|
||||
YearStatisticsItem *item = new YearStatisticsItem(stats.stats_yearly[i]);
|
||||
combined_months = 0;
|
||||
for (j = 0; combined_months < stats.stats_yearly[i].selection_size; ++j) {
|
||||
int month = 0;
|
||||
for (const auto &s: stats.stats_yearly) {
|
||||
YearStatisticsItem *item = new YearStatisticsItem(s);
|
||||
size_t combined_months = 0;
|
||||
while (combined_months < s.selection_size) {
|
||||
combined_months += stats.stats_monthly[month].selection_size;
|
||||
YearStatisticsItem *iChild = new YearStatisticsItem(stats.stats_monthly[month]);
|
||||
item->children.append(iChild);
|
||||
|
@ -205,10 +189,10 @@ void YearlyStatisticsModel::update_yearly_stats()
|
|||
item->parent = rootItem.get();
|
||||
}
|
||||
|
||||
if (stats.stats_by_trip != NULL && stats.stats_by_trip[0].is_trip == true) {
|
||||
if (stats.stats_by_trip[0].is_trip == true) {
|
||||
YearStatisticsItem *item = new YearStatisticsItem(stats.stats_by_trip[0]);
|
||||
for (i = 1; stats.stats_by_trip != NULL && stats.stats_by_trip[i].is_trip; ++i) {
|
||||
YearStatisticsItem *iChild = new YearStatisticsItem(stats.stats_by_trip[i]);
|
||||
for (auto it = std::next(stats.stats_by_trip.begin()); it != stats.stats_by_trip.end(); ++it) {
|
||||
YearStatisticsItem *iChild = new YearStatisticsItem(*it);
|
||||
item->children.append(iChild);
|
||||
iChild->parent = item;
|
||||
}
|
||||
|
@ -217,12 +201,12 @@ void YearlyStatisticsModel::update_yearly_stats()
|
|||
}
|
||||
|
||||
/* Show the statistic sorted by dive type */
|
||||
if (stats.stats_by_type != NULL && stats.stats_by_type[0].selection_size) {
|
||||
if (stats.stats_by_type[0].selection_size) {
|
||||
YearStatisticsItem *item = new YearStatisticsItem(stats.stats_by_type[0]);
|
||||
for (i = 1; i <= NUM_DIVEMODE; ++i) {
|
||||
if (stats.stats_by_type[i].selection_size == 0)
|
||||
for (auto it = std::next(stats.stats_by_type.begin()); it != stats.stats_by_type.end(); ++it) {
|
||||
if (it->selection_size == 0)
|
||||
continue;
|
||||
YearStatisticsItem *iChild = new YearStatisticsItem(stats.stats_by_type[i]);
|
||||
YearStatisticsItem *iChild = new YearStatisticsItem(*it);
|
||||
item->children.append(iChild);
|
||||
iChild->parent = item;
|
||||
}
|
||||
|
@ -231,35 +215,41 @@ void YearlyStatisticsModel::update_yearly_stats()
|
|||
}
|
||||
|
||||
/* Show the statistic sorted by dive depth */
|
||||
if (stats.stats_by_depth != NULL && stats.stats_by_depth[0].selection_size) {
|
||||
if (stats.stats_by_depth[0].selection_size) {
|
||||
YearStatisticsItem *item = new YearStatisticsItem(stats.stats_by_depth[0]);
|
||||
for (i = 1; stats.stats_by_depth[i].is_trip; ++i)
|
||||
if (stats.stats_by_depth[i].selection_size) {
|
||||
label = QString(tr("%1 - %2")).arg(get_depth_string((i - 1) * (STATS_DEPTH_BUCKET * 1000), true, false),
|
||||
get_depth_string(i * (STATS_DEPTH_BUCKET * 1000), true, false));
|
||||
stats.stats_by_depth[i].location = strdup(label.toUtf8().data());
|
||||
YearStatisticsItem *iChild = new YearStatisticsItem(stats.stats_by_depth[i]);
|
||||
int i = 0;
|
||||
for (auto it = std::next(stats.stats_by_depth.begin()); it != stats.stats_by_depth.end(); ++it) {
|
||||
if (it->selection_size) {
|
||||
QString label = QString(tr("%1 - %2")).arg(get_depth_string(i * (STATS_DEPTH_BUCKET * 1000), true, false),
|
||||
get_depth_string((i + 1) * (STATS_DEPTH_BUCKET * 1000), true, false));
|
||||
it->location = label.toStdString();
|
||||
YearStatisticsItem *iChild = new YearStatisticsItem(*it);
|
||||
item->children.append(iChild);
|
||||
iChild->parent = item;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
rootItem->children.append(item);
|
||||
item->parent = rootItem.get();
|
||||
}
|
||||
|
||||
/* Show the statistic sorted by dive temperature */
|
||||
if (stats.stats_by_temp != NULL && stats.stats_by_temp[0].selection_size) {
|
||||
if (stats.stats_by_temp[0].selection_size) {
|
||||
YearStatisticsItem *item = new YearStatisticsItem(stats.stats_by_temp[0]);
|
||||
for (i = 1; stats.stats_by_temp[i].is_trip; ++i)
|
||||
if (stats.stats_by_temp[i].selection_size) {
|
||||
t_range_min.mkelvin = C_to_mkelvin((i - 1) * STATS_TEMP_BUCKET);
|
||||
t_range_max.mkelvin = C_to_mkelvin(i * STATS_TEMP_BUCKET);
|
||||
int i = 0;
|
||||
for (auto it = std::next(stats.stats_by_temp.begin()); it != stats.stats_by_temp.end(); ++it) {
|
||||
if (it->selection_size) {
|
||||
t_range_min.mkelvin = C_to_mkelvin(i * STATS_TEMP_BUCKET);
|
||||
t_range_max.mkelvin = C_to_mkelvin((i + 1) * STATS_TEMP_BUCKET);
|
||||
label = QString(tr("%1 - %2")).arg(get_temperature_string(t_range_min, true),
|
||||
get_temperature_string(t_range_max, true));
|
||||
stats.stats_by_temp[i].location = strdup(label.toUtf8().data());
|
||||
YearStatisticsItem *iChild = new YearStatisticsItem(stats.stats_by_temp[i]);
|
||||
it->location = label.toStdString();
|
||||
YearStatisticsItem *iChild = new YearStatisticsItem(*it);
|
||||
item->children.append(iChild);
|
||||
iChild->parent = item;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
rootItem->children.append(item);
|
||||
item->parent = rootItem.get();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue