diff options
author | Tomaz Canabrava <tcanabrava@kde.org> | 2013-04-21 22:12:36 -0300 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-04-22 09:42:13 -0700 |
commit | a0280ae7d2cdd483bc53c7bc91a8aa438f9234de (patch) | |
tree | 1953e0422d8c714d4616f7266622b2b1fde3c185 /qt-ui | |
parent | 5da11cbc6a1d3339cbb519cf74e4377b8cbaee5f (diff) | |
download | subsurface-a0280ae7d2cdd483bc53c7bc91a8aa438f9234de.tar.gz |
Move model related code from MainWindow and adjustments.
Moves the DiveTrip model related code to models.h
The DiveTripModel was implemented in three parts:
DiveTripModel.h
DiveTripModel.cpp
MainWindow.cpp (the code to populate the model)
This patch changes the DiveTripModel from it's original
implementation to the file models.h, wich should store
all models (Dirk requested the Qt developers to not create
2 files per class, but instead to use a file for functionality,
and data-models are one functionality.)
Besides that, this code cleans up a bit the style:
removed operator<< for .push_back, const references where they apply,
moved the internal DiveItem class to the .cpp since it should be visible
only to the DiveTripModel class, and redesigned the current interface of
the model to be identical of the GTK one (used the UTF8-star and 2
subscribed, for instance).
Amit (the creator of the original code) should comment here if it's ok
with my changes.
Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui')
-rw-r--r-- | qt-ui/divetripmodel.cpp | 141 | ||||
-rw-r--r-- | qt-ui/divetripmodel.h | 86 | ||||
-rw-r--r-- | qt-ui/mainwindow.cpp | 41 | ||||
-rw-r--r-- | qt-ui/models.cpp | 224 | ||||
-rw-r--r-- | qt-ui/models.h | 34 |
5 files changed, 258 insertions, 268 deletions
diff --git a/qt-ui/divetripmodel.cpp b/qt-ui/divetripmodel.cpp deleted file mode 100644 index 5082494a0..000000000 --- a/qt-ui/divetripmodel.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/* - * divetripmodel.cpp - * - * classes for the dive trip list in Subsurface - */ -#include "divetripmodel.h" - - -DiveItem::DiveItem(int num, QString dt, float dur, float dep, QString loc, DiveItem *p): - number(num), dateTime(dt), duration(dur), depth(dep), location(loc), parentItem(p) -{ - if (parentItem) - parentItem->addChild(this); -} - - -DiveTripModel::DiveTripModel(const QString &filename, QObject *parent) : QAbstractItemModel(parent), filename(filename) -{ - rootItem = new DiveItem; -} - - -Qt::ItemFlags DiveTripModel::flags(const QModelIndex &index) const -{ - Qt::ItemFlags diveFlags = QAbstractItemModel::flags(index); - if (index.isValid()) { - diveFlags |= Qt::ItemIsSelectable|Qt::ItemIsEnabled; - } - return diveFlags; -} - - -QVariant DiveTripModel::data(const QModelIndex &index, int role) const -{ - if (!index.isValid()) - return QVariant(); - - if (role != Qt::DisplayRole) - return QVariant(); - - DiveItem *item = static_cast<DiveItem*>(index.internalPointer()); - - QVariant retVal; - switch (index.column()) { - case DIVE_NUMBER: - retVal = QVariant(item->diveNumber()); - break; - case DIVE_DATE_TIME: - retVal = QVariant(item->diveDateTime()); - break; - case DIVE_DURATION: - retVal = QVariant(item->diveDuration()); - break; - case DIVE_DEPTH: - retVal = QVariant(item->diveDepth()); - break; - case DIVE_LOCATION: - retVal = QVariant(item->diveLocation()); - break; - default: - return QVariant(); - }; - return retVal; -} - - -QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { - if (section == DIVE_NUMBER) { - return tr("Dive number"); - } else if (section == DIVE_DATE_TIME) { - return tr("Date"); - } else if (section == DIVE_DURATION) { - return tr("Duration"); - } else if (section == DIVE_DEPTH) { - return tr("Depth"); - } else if (section == DIVE_LOCATION) { - return tr("Location"); - } - } - return QVariant(); -} - -int DiveTripModel::rowCount(const QModelIndex &parent) const -{ - /* only allow kids in column 0 */ - if (parent.isValid() && parent.column() > 0){ - return 0; - } - DiveItem *item = itemForIndex(parent); - return item ? item->childCount() : 0; -} - - - -int DiveTripModel::columnCount(const QModelIndex &parent) const -{ - return parent.isValid() && parent.column() != 0 ? 0 : COLUMNS; -} - - -QModelIndex DiveTripModel::index(int row, int column, const QModelIndex &parent) const -{ - - if (!rootItem || row < 0 || column < 0 || column >= COLUMNS || - (parent.isValid() && parent.column() != 0)) - return QModelIndex(); - - DiveItem *parentItem = itemForIndex(parent); - Q_ASSERT(parentItem); - if (DiveItem *item = parentItem->childAt(row)){ - return createIndex(row, column, item); - } - return QModelIndex(); -} - - -QModelIndex DiveTripModel::parent(const QModelIndex &childIndex) const -{ - if (!childIndex.isValid()) - return QModelIndex(); - - DiveItem *child = static_cast<DiveItem*>(childIndex.internalPointer()); - DiveItem *parent = child->parent(); - - if (parent == rootItem) - return QModelIndex(); - - return createIndex(parent->rowOfChild(child), 0, parent); -} - - -DiveItem* DiveTripModel::itemForIndex(const QModelIndex &index) const -{ - if (index.isValid()) { - DiveItem *item = static_cast<DiveItem*>(index.internalPointer()); - return item; - } - return rootItem; -} diff --git a/qt-ui/divetripmodel.h b/qt-ui/divetripmodel.h deleted file mode 100644 index ad1815798..000000000 --- a/qt-ui/divetripmodel.h +++ /dev/null @@ -1,86 +0,0 @@ -/* - * divetripmodel.h - * - * header file for the divetrip model of Subsurface - * - */ -#ifndef DIVETRIPMODEL_H -#define DIVETRIPMODEL_H - -#include <QAbstractItemModel> - -/*! A DiveItem for use with a DiveTripModel - * - * A simple class which wraps basic stats for a dive (e.g. duration, depth) and - * tidies up after it's children. This is done manually as we don't inherit from - * QObject. - * -*/ -class DiveItem -{ -public: - explicit DiveItem(): number(0), dateTime(QString()), duration(0.0), depth(0.0), location(QString()) {parentItem = 0;} - explicit DiveItem(int num, QString dt, float, float, QString loc, DiveItem *parent = 0); - ~DiveItem() { qDeleteAll(childlist); } - - int diveNumber() const { return number; } - QString diveDateTime() const { return dateTime; } - float diveDuration() const { return duration; } - float diveDepth() const { return depth; } - QString diveLocation() const { return location; } - - DiveItem *parent() const { return parentItem; } - DiveItem *childAt(int row) const { return childlist.value(row); } - int rowOfChild(DiveItem *child) const { return childlist.indexOf(child); } - int childCount() const { return childlist.count(); } - bool hasChildren() const { return !childlist.isEmpty(); } - QList<DiveItem *> children() const { return childlist; } - void addChild(DiveItem* item) { item->parentItem = this; childlist << item; } /* parent = self */ - - -private: - - int number; - QString dateTime; - float duration; - float depth; - QString location; - - DiveItem *parentItem; - QList <DiveItem*> childlist; - -}; - - -enum Column {DIVE_NUMBER, DIVE_DATE_TIME, DIVE_DURATION, DIVE_DEPTH, DIVE_LOCATION, COLUMNS}; - - -/*! An AbstractItemModel for recording dive trip information such as a list of dives. -* -*/ -class DiveTripModel : public QAbstractItemModel -{ -public: - - DiveTripModel(const QString &filename, QObject *parent = 0); - - Qt::ItemFlags flags(const QModelIndex &index) const; - QVariant data(const QModelIndex &index, int role) const; - QVariant headerData(int section, Qt::Orientation orientation, int role) const; - int rowCount(const QModelIndex &parent) const; - - int columnCount(const QModelIndex &parent = QModelIndex()) const; - virtual QModelIndex index(int row, int column, - const QModelIndex &parent = QModelIndex()) const; - virtual QModelIndex parent(const QModelIndex &child) const; - - DiveItem *itemForIndex(const QModelIndex &) const; - -private: - - DiveItem *rootItem; - QString filename; - -}; - -#endif // DIVETRIPMODEL_H diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 551c28faa..46ce076d4 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -13,7 +13,6 @@ #include <QDateTime> #include "divelistview.h" -#include "divetripmodel.h" #include "glib.h" #include "../dive.h" @@ -21,45 +20,11 @@ #include "../pref.h" -MainWindow::MainWindow() : ui(new Ui::MainWindow()) +MainWindow::MainWindow() : ui(new Ui::MainWindow()), + model(new DiveTripModel(this)) { ui->setupUi(this); - - /* may want to change ctor to avoid filename as 1st param. - * here we just use an empty string - */ - model = new DiveTripModel("",this); - if (model) { - ui->ListWidget->setModel(model); - } - /* we need root to parent all top level dives - * trips need more work as it complicates parent/child stuff. - * - * Todo: look at alignment/format of e.g. duration in view - * - */ - DiveItem *dive = 0; - DiveItem *root = model->itemForIndex(QModelIndex()); - if (root) { - int i; - Q_UNUSED(dive) - - struct dive *d; - qDebug("address of dive_table %p", &dive_table); - for_each_dive(i, d) { - struct tm tm; - char *buffer; - utc_mkdate(d->when, &tm); - buffer = get_dive_date_string(&tm); - dive = new DiveItem(d->number, - buffer, - (float)d->duration.seconds/60, - (float)d->maxdepth.mm/1000 , - d->location, - root); - free(buffer); - } - } + ui->ListWidget->setModel(model); } void MainWindow::on_actionNew_triggered() diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index d1b8dc0a0..40307d022 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -6,6 +6,7 @@ */ #include "models.h" #include "../dive.h" +#include "../divelist.h" extern struct tank_info tank_info[100]; @@ -280,3 +281,226 @@ void TankInfoModel::update() endInsertRows(); } } + +/*! A DiveItem for use with a DiveTripModel + * + * A simple class which wraps basic stats for a dive (e.g. duration, depth) and + * tidies up after it's children. This is done manually as we don't inherit from + * QObject. + * +*/ +class DiveItem +{ +public: + explicit DiveItem(): number(0), dateTime(QString()), duration(0.0), depth(0.0), location(QString()) {parentItem = 0;} + explicit DiveItem(int num, QString dt, float, float, QString loc, DiveItem *parent = 0); + ~DiveItem() { qDeleteAll(childlist); } + + int diveNumber() const { return number; } + const QString& diveDateTime() const { return dateTime; } + float diveDuration() const { return duration; } + float diveDepth() const { return depth; } + const QString& diveLocation() const { return location; } + DiveItem *parent() const { return parentItem; } + const QList<DiveItem *>& children() const { return childlist; } + + void addChild(DiveItem* item) { + item->parentItem = this; + childlist.push_back(item); + } /* parent = self */ + +private: + int number; + QString dateTime; + float duration; + float depth; + QString location; + + DiveItem *parentItem; + QList <DiveItem*> childlist; + +}; + +DiveItem::DiveItem(int num, QString dt, float dur, float dep, QString loc, DiveItem *p): + number(num), dateTime(dt), duration(dur), depth(dep), location(loc), parentItem(p) +{ + if (parentItem) + parentItem->addChild(this); +} + +DiveTripModel::DiveTripModel(QObject *parent) : QAbstractItemModel(parent) +{ + rootItem = new DiveItem; + int i; + struct dive *d; + + for_each_dive(i, d) { + struct tm tm; + char *buffer; + utc_mkdate(d->when, &tm); + buffer = get_dive_date_string(&tm); + new DiveItem(d->number, + buffer, + d->duration.seconds/60.0, + d->maxdepth.mm/1000.0 , + d->location, + rootItem); + free(buffer); + } +} + + +Qt::ItemFlags DiveTripModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags diveFlags = QAbstractItemModel::flags(index); + if (index.isValid()) { + diveFlags |= Qt::ItemIsSelectable|Qt::ItemIsEnabled; + } + return diveFlags; +} + + +QVariant DiveTripModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + DiveItem *item = static_cast<DiveItem*>(index.internalPointer()); + + QVariant retVal; + if (role == Qt::DisplayRole){ + switch (index.column()) { + case NR: + retVal = item->diveNumber(); + break; + case DATE: + retVal = item->diveDateTime(); + break; + case DURATION: + retVal = item->diveDuration(); + break; + case DEPTH: + retVal = item->diveDepth(); + break; + case LOCATION: + retVal = item->diveLocation(); + break; + } + } + return retVal; +} + + +QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + QVariant ret; + if (orientation != Qt::Horizontal){ + return ret; + } + + if (role == Qt::DisplayRole) { + switch(section){ + case NR: + ret = tr("#"); + break; + case DATE: + ret = tr("Date"); + break; + case RATING: + ret = UTF8_BLACKSTAR; + break; + case DEPTH: + ret = tr("ft"); + break; + case DURATION: + ret = tr("min"); + break; + case TEMPERATURE: + ret = UTF8_DEGREE "F"; + break; + case TOTALWEIGHT: + ret = tr("lbs"); + break; + case SUIT: + ret = tr("Suit"); + break; + case CYLINDER: + ret = tr("Cyl"); + break; + case NITROX: + ret = "O" 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; + } + } + return ret; +} + +int DiveTripModel::rowCount(const QModelIndex &parent) const +{ + /* only allow kids in column 0 */ + if (parent.isValid() && parent.column() > 0){ + return 0; + } + DiveItem *item = itemForIndex(parent); + return item ? item->children().count() : 0; +} + + + +int DiveTripModel::columnCount(const QModelIndex &parent) const +{ + return parent.isValid() && parent.column() != 0 ? 0 : COLUMNS; +} + + +QModelIndex DiveTripModel::index(int row, int column, const QModelIndex &parent) const +{ + + if (!rootItem || row < 0 || column < 0 || column >= COLUMNS || + (parent.isValid() && parent.column() != 0)) + return QModelIndex(); + + DiveItem *parentItem = itemForIndex(parent); + Q_ASSERT(parentItem); + if (DiveItem *item = parentItem->children().at(row)){ + return createIndex(row, column, item); + } + return QModelIndex(); +} + + +QModelIndex DiveTripModel::parent(const QModelIndex &childIndex) const +{ + if (!childIndex.isValid()) + return QModelIndex(); + + DiveItem *child = static_cast<DiveItem*>(childIndex.internalPointer()); + DiveItem *parent = child->parent(); + + if (parent == rootItem) + return QModelIndex(); + + return createIndex(parent->children().indexOf(child), 0, parent); +} + + +DiveItem* DiveTripModel::itemForIndex(const QModelIndex &index) const +{ + if (index.isValid()) { + DiveItem *item = static_cast<DiveItem*>(index.internalPointer()); + return item; + } + return rootItem; +} diff --git a/qt-ui/models.h b/qt-ui/models.h index 8d86102cb..d64faf0bd 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -15,7 +15,7 @@ class TankInfoModel : public QAbstractTableModel { Q_OBJECT public: - enum { DESCRIPTION, ML, BAR}; + enum Column { DESCRIPTION, ML, BAR}; TankInfoModel(); /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; @@ -30,10 +30,12 @@ private: int rows; }; +/* Encapsulation of the Cylinder Model, that presents the + * Current cylinders that are used on a dive. */ class CylindersModel : public QAbstractTableModel { Q_OBJECT public: - enum {TYPE, SIZE, MAXPRESS, START, END, O2, HE}; + enum Column {TYPE, SIZE, MAXPRESS, START, END, O2, HE}; explicit CylindersModel(QObject* parent = 0); /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; @@ -53,8 +55,10 @@ private: QMap<dive*, int> usedRows; }; +/* Encapsulation of the Weight Model, that represents + * the current weights on a dive. */ class WeightModel : public QAbstractTableModel { - enum{TYPE, WEIGHT}; + enum Column {TYPE, WEIGHT}; /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; /*reimp*/ int columnCount(const QModelIndex& parent = QModelIndex()) const; /*reimp*/ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; @@ -67,4 +71,28 @@ private: int rows; }; +/*! An AbstractItemModel for recording dive trip information such as a list of dives. +* +*/ +class DiveItem; // Represents a single item on the model, implemented in the .cpp since it's private for this class. +class DiveTripModel : public QAbstractItemModel +{ +public: + enum Column {NR, DATE, RATING, DEPTH, DURATION, TEMPERATURE, TOTALWEIGHT, SUIT, CYLINDER, NITROX, SAC, OTU, MAXCNS, LOCATION, COLUMNS }; + + DiveTripModel(QObject *parent = 0); + + /*reimp*/ Qt::ItemFlags flags(const QModelIndex &index) const; + /*reimp*/ QVariant data(const QModelIndex &index, int role) const; + /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role) const; + /*reimp*/ int rowCount(const QModelIndex &parent) const; + /*reimp*/ int columnCount(const QModelIndex &parent = QModelIndex()) const; + /*reimp*/ QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; + /*reimp*/ QModelIndex parent(const QModelIndex &child) const; + +private: + DiveItem *itemForIndex(const QModelIndex& index) const; + DiveItem *rootItem; +}; + #endif |