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/divetripmodel.cpp | |
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/divetripmodel.cpp')
-rw-r--r-- | qt-ui/divetripmodel.cpp | 141 |
1 files changed, 0 insertions, 141 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; -} |