diff options
Diffstat (limited to 'qt-ui/models.cpp')
-rw-r--r-- | qt-ui/models.cpp | 350 |
1 files changed, 240 insertions, 110 deletions
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index c56fa2f44..485015379 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -8,7 +8,9 @@ #include "../helpers.h" #include "../dive.h" #include "../device.h" +#include "../statistics.h" #include "../qthelper.h" + #include <QCoreApplication> #include <QDebug> #include <QColor> @@ -769,6 +771,11 @@ void TankInfoModel::update() } } +//################################################################################################# +//# +//# Tree Model - a Basic Tree Model so I don't need to kill myself repeating this for every model. +//# +//################################################################################################# /*! A DiveItem for use with a DiveTripModel * @@ -778,46 +785,98 @@ void TankInfoModel::update() * */ -TreeItemDT::~TreeItemDT() +TreeItem::~TreeItem() { qDeleteAll(children); } -int TreeItemDT::row() const +int TreeItem::row() const { if (parent) - return parent->children.indexOf(const_cast<TreeItemDT*>(this)); - + return parent->children.indexOf(const_cast<TreeItem*>(this)); return 0; } -QVariant TreeItemDT::data(int column, int role) const +QVariant TreeItem::data(int column, int role) const { - QVariant ret; - switch(role){ - case Qt::DisplayRole : - switch (column) { - case NR: ret = tr("#"); break; - case DATE: ret = tr("Date"); break; - case RATING: ret = UTF8_BLACKSTAR; break; - case DEPTH: ret = (get_units()->length == units::METERS) ? tr("m") : tr("ft"); break; - case DURATION: ret = tr("min"); break; - case TEMPERATURE: ret = QString("%1%2").arg(UTF8_DEGREE).arg((get_units()->temperature == units::CELSIUS) ? "C" : "F"); break; - case TOTALWEIGHT: ret = (get_units()->weight == units::KG) ? tr("kg") : tr("lbs"); break; - case SUIT: ret = tr("Suit"); break; - case CYLINDER: ret = tr("Cyl"); break; - case NITROX: ret = QString("O%1%").arg(UTF8_SUBSCRIPT_2); break; - case SAC: ret = tr("SAC"); break; - case OTU: ret = tr("OTU"); break; - case MAXCNS: ret = tr("maxCNS"); break; - case LOCATION: ret = tr("Location"); break; - } - break; + return QVariant(); +} + +TreeModel::TreeModel(QObject* parent): QAbstractItemModel(parent) +{ + rootItem = new TreeItem(); +} + +TreeModel::~TreeModel() +{ + delete rootItem; +} + +QVariant TreeModel::data(const QModelIndex& index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (role == Qt::FontRole) { + return defaultModelFont(); } - return ret; + TreeItem* item = static_cast<TreeItem*>(index.internalPointer()); + + return item->data(index.column(), role); +} + +QModelIndex TreeModel::index(int row, int column, const QModelIndex& parent) +const +{ + if (!hasIndex(row, column, parent)) + return QModelIndex(); + + TreeItem* parentItem = (!parent.isValid()) ? rootItem : static_cast<TreeItem*>(parent.internalPointer()); + + TreeItem* childItem = parentItem->children[row]; + + return (childItem) ? createIndex(row, column, childItem) : QModelIndex(); +} + +QModelIndex TreeModel::parent(const QModelIndex& index) const +{ + if (!index.isValid()) + return QModelIndex(); + + TreeItem* childItem = static_cast<TreeItem*>(index.internalPointer()); + TreeItem* parentItem = childItem->parent; + + if (parentItem == rootItem || !parentItem) + return QModelIndex(); + + return createIndex(parentItem->row(), 0, parentItem); +} + +int TreeModel::rowCount(const QModelIndex& parent) const +{ + TreeItem* parentItem; + + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast<TreeItem*>(parent.internalPointer()); + + int amount = parentItem->children.count(); + + return amount; +} + +int TreeModel::columnCount(const QModelIndex& parent) const +{ + return columns; } -struct TripItem : public TreeItemDT { +/*################################################################ + * + * Implementation of the Dive List. + * + * ############################################################### */ +struct TripItem : public TreeItem { virtual QVariant data(int column, int role) const; dive_trip_t* trip; }; @@ -826,12 +885,12 @@ QVariant TripItem::data(int column, int role) const { QVariant ret; - if (role == SORT_ROLE) + if (role == DiveTripModel::SORT_ROLE) return (qulonglong)trip->when; if (role == Qt::DisplayRole) { switch (column) { - case NR: + case DiveTripModel::NR: ret = QString(trip->location) + ", " + QString(get_trip_date_string(trip->when, trip->nrdives)); break; } @@ -840,7 +899,10 @@ QVariant TripItem::data(int column, int role) const return ret; } -struct DiveItem : public TreeItemDT { +struct DiveItem : public TreeItem { + enum Column {NR, DATE, RATING, DEPTH, DURATION, TEMPERATURE, TOTALWEIGHT, + SUIT, CYLINDER, NITROX, SAC, OTU, MAXCNS, LOCATION, COLUMNS }; + virtual QVariant data(int column, int role) const; struct dive* dive; @@ -876,7 +938,7 @@ QVariant DiveItem::data(int column, int role) const break; } break; - case SORT_ROLE: + case DiveTripModel::SORT_ROLE: switch (column) { case NR: retVal = (qulonglong) dive->when; break; case DATE: retVal = (qulonglong) dive->when; break; @@ -913,10 +975,10 @@ QVariant DiveItem::data(int column, int role) const break; } - if (role == STAR_ROLE) + if (role == DiveTripModel::STAR_ROLE) retVal = dive->rating; - if (role == DIVE_ROLE) + if (role == DiveTripModel::DIVE_ROLE) retVal = QVariant::fromValue<void*>(dive); return retVal; @@ -1001,37 +1063,9 @@ int DiveItem::weight() const return tw.grams; } - -DiveTripModel::DiveTripModel(QObject* parent) : - QAbstractItemModel(parent) -{ - rootItem = new TreeItemDT(); -} - -DiveTripModel::~DiveTripModel() -{ - delete rootItem; -} - -int DiveTripModel::columnCount(const QModelIndex& parent) const -{ - if (parent.isValid()) - return static_cast<TreeItemDT*>(parent.internalPointer())->columnCount(); - else - return rootItem->columnCount(); -} - -QVariant DiveTripModel::data(const QModelIndex& index, int role) const +DiveTripModel::DiveTripModel(QObject* parent): TreeModel(parent) { - if (!index.isValid()) - return QVariant(); - - if (role == Qt::FontRole) { - return defaultModelFont(); - } - TreeItemDT* item = static_cast<TreeItemDT*>(index.internalPointer()); - - return item->data(index.column(), role); + columns = COLUMNS; } Qt::ItemFlags DiveTripModel::flags(const QModelIndex& index) const @@ -1042,59 +1076,35 @@ Qt::ItemFlags DiveTripModel::flags(const QModelIndex& index) const return Qt::ItemIsEnabled | Qt::ItemIsSelectable; } -QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, - int role) const +QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int role) const { - if (orientation == Qt::Horizontal && role == Qt::DisplayRole) - return rootItem->data(section, role); + QVariant ret; + if (orientation == Qt::Vertical) + return ret; switch(role){ case Qt::FontRole : - return defaultModelFont(); + ret = defaultModelFont(); break; + case Qt::DisplayRole : + switch (section) { + case NR: ret = tr("#"); break; + case DATE: ret = tr("Date"); break; + case RATING: ret = UTF8_BLACKSTAR; break; + case DEPTH: ret = (get_units()->length == units::METERS) ? tr("m") : tr("ft"); break; + case DURATION: ret = tr("min"); break; + case TEMPERATURE:ret = QString("%1%2").arg(UTF8_DEGREE).arg((get_units()->temperature == units::CELSIUS) ? "C" : "F"); break; + case TOTALWEIGHT:ret = (get_units()->weight == units::KG) ? tr("kg") : tr("lbs"); break; + case SUIT: ret = tr("Suit"); break; + case CYLINDER: ret = tr("Cyl"); break; + case NITROX: ret = QString("O%1%").arg(UTF8_SUBSCRIPT_2); break; + case SAC: ret = tr("SAC"); break; + case OTU: ret = tr("OTU"); break; + case MAXCNS: ret = tr("maxCNS"); break; + case LOCATION: ret = tr("Location"); break; + }break; } - return QVariant(); -} - -QModelIndex DiveTripModel::index(int row, int column, const QModelIndex& parent) -const -{ - if (!hasIndex(row, column, parent)) - return QModelIndex(); - - TreeItemDT* parentItem = (!parent.isValid()) ? rootItem : static_cast<TreeItemDT*>(parent.internalPointer()); - - TreeItemDT* childItem = parentItem->children[row]; - - return (childItem) ? createIndex(row, column, childItem) : QModelIndex(); -} - -QModelIndex DiveTripModel::parent(const QModelIndex& index) const -{ - if (!index.isValid()) - return QModelIndex(); - - TreeItemDT* childItem = static_cast<TreeItemDT*>(index.internalPointer()); - TreeItemDT* parentItem = childItem->parent; - - if (parentItem == rootItem || !parentItem) - return QModelIndex(); - - return createIndex(parentItem->row(), 0, parentItem); -} - -int DiveTripModel::rowCount(const QModelIndex& parent) const -{ - TreeItemDT* parentItem; - - if (!parent.isValid()) - parentItem = rootItem; - else - parentItem = static_cast<TreeItemDT*>(parent.internalPointer()); - - int amount = parentItem->children.count(); - - return amount; + return ret; } void DiveTripModel::setupModelData() @@ -1256,6 +1266,126 @@ void DiveComputerModel::remove(const QModelIndex& index) update(); } +/*################################################################# + * # + * # Yearly Statistics Model + * # + * ################################################################ + */ + +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 +{ + QVariant val; + if (role == Qt::FontRole){ + val = defaultModelFont(); + } + + if (role == Qt::DisplayRole && orientation == Qt::Horizontal){ + switch(section){ + case YEAR: val = tr("Year \n > Month"); break; + case DIVES: val = tr("#"); break; + case TOTAL_TIME: val = tr("Duration \n Total"); break; + case AVERAGE_TIME: val = tr("Average"); break; + case SHORTEST_TIME: val = tr("Shortest"); break; + case LONGEST_TIME: val = tr("Longest"); break; + case AVG_DEPTH: val = tr("Depth \n Average"); break; + case MIN_DEPTH: val = tr("Minimum"); break; + case MAX_DEPTH: val = tr("Maximum"); break; + case AVG_SAC: val = tr("SAC \n Average"); break; + case MIN_SAC: val = tr("Minimum"); break; + case MAX_SAC: val = tr("Maximum"); break; + case AVG_TEMP: val = tr("Temperature \n Average"); break; + case MIN_TEMP: val = tr("Minimum"); break; + case MAX_TEMP: val = tr("Maximum"); break; + } + } + 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); + } +} + void DiveComputerModel::dropWorkingList() { // how do I prevent the memory leak ? |