Yearly stats are being displayed.

Yearly stats are being displayed. the code is similar to the GTK one,
but I advise the reader that it's a bit risky  ( I got an unreproducible
crash but it seems to be fixed now. ). The selection behavior is *weird*
and I have no idea why, but I'm very sleepy - will try to look at
it again tomorrow.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
This commit is contained in:
Tomaz Canabrava 2013-06-17 21:05:17 -03:00
parent a05ea5a6e8
commit 87e380c7a2
3 changed files with 84 additions and 2 deletions

View file

@ -241,7 +241,7 @@ void MainWindow::on_actionToggleZoom_triggered()
void MainWindow::on_actionYearlyStatistics_triggered()
{
QTableView *view = new QTableView();
QTreeView *view = new QTreeView();
QAbstractItemModel *model = new YearlyStatisticsModel();
view->setModel(model);
view->show();

View file

@ -8,6 +8,8 @@
#include "../helpers.h"
#include "../dive.h"
#include "../device.h"
#include "../statistics.h"
#include <QCoreApplication>
#include <QDebug>
@ -1282,9 +1284,70 @@ void DiveComputerModel::remove(const QModelIndex& i)
* ################################################################
*/
class YearStatisticsItem : public TreeItem{
public:
enum { YEAR,DIVES,TOTAL_TIME,AVERAGE_TIME,SHORTEST_TIME,LONGEST_TIME,AVG_DEPTH,MIN_DEPTH,
MAX_DEPTH,AVG_SAC,MIN_SAC,MAX_SAC,AVG_TEMP,MIN_TEMP,MAX_TEMP,COLUMNS};
QVariant data(int column, int role) const;
YearStatisticsItem(stats_t interval);
private:
stats_t stats_interval;
};
YearStatisticsItem::YearStatisticsItem(stats_t interval)
: stats_interval(interval)
{
}
QVariant YearStatisticsItem::data(int column, int role) const
{
const char *unit;
QVariant ret;
if (role != Qt::DisplayRole){
return ret;
}
switch(column){
case YEAR: ret = stats_interval.period; break;
case DIVES: ret = stats_interval.selection_size;break;
case TOTAL_TIME: ret = get_time_string(stats_interval.total_time.seconds, 0);break;
case AVERAGE_TIME: ret = get_minutes(stats_interval.total_time.seconds / stats_interval.selection_size);break;
case SHORTEST_TIME: ret = get_minutes(stats_interval.shortest_time.seconds);break;
case LONGEST_TIME: ret = get_minutes(stats_interval.longest_time.seconds);break;
case AVG_DEPTH: ret = stats_interval.avg_depth.mm;break;
case MIN_DEPTH: ret = stats_interval.min_depth.mm;break;
case MAX_DEPTH: ret = stats_interval.max_depth.mm;break;
case AVG_SAC: ret = stats_interval.avg_sac.mliter;break;
case MIN_SAC: ret = stats_interval.min_sac.mliter;break;
case MAX_SAC: ret = stats_interval.max_sac.mliter;break;
case AVG_TEMP:{
get_temp_units(stats_interval.min_temp, &unit);
if (stats_interval.combined_temp && stats_interval.combined_count) {
ret = QString("%1 %2").arg(stats_interval.combined_temp / stats_interval.combined_count).arg(unit);
}
}break;
case MIN_TEMP:{
double value = get_temp_units(stats_interval.min_temp, &unit);
if (value > -100.0) {
ret = QString("%1 %2").arg(value).arg(unit);
}
}break;
case MAX_TEMP:{
double value = get_temp_units(stats_interval.max_temp, &unit);
if (value > -100.0) {
ret = QString("%1 %2").arg(value).arg(unit);
}
}break;
}
return ret;
}
YearlyStatisticsModel::YearlyStatisticsModel(QObject* parent)
{
columns = COLUMNS;
update_yearly_stats();
}
QVariant YearlyStatisticsModel::headerData(int section, Qt::Orientation orientation, int role) const
@ -1315,3 +1378,21 @@ QVariant YearlyStatisticsModel::headerData(int section, Qt::Orientation orientat
}
return val;
}
void YearlyStatisticsModel::update_yearly_stats()
{
int i, month = 0;
unsigned int j, combined_months;
for (i = 0; stats_yearly != NULL && stats_yearly[i].period; ++i){
YearStatisticsItem *item = new YearStatisticsItem(stats_yearly[i]);
combined_months = 0;
for (j = 0; combined_months < stats_yearly[i].selection_size; ++j) {
combined_months += stats_monthly[month].selection_size;
YearStatisticsItem *iChild = new YearStatisticsItem(stats_monthly[month]);
item->children.append(iChild);
month++;
}
rootItem->children.append(item);
}
}

View file

@ -204,5 +204,6 @@ public:
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
YearlyStatisticsModel(QObject* parent = 0);
void update_yearly_stats();
};
#endif