mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-28 05:00:20 +00:00
Add DiveListModel
This model will be used to show the dives in QML. This commit adds the model, and the means to link it to QML. Signed-off-by: Grace Karanja <gracie.karanja89@gmail.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
bf882416f9
commit
b7e4b35cde
6 changed files with 392 additions and 4 deletions
|
@ -423,7 +423,7 @@ endif()
|
|||
|
||||
# create the executables
|
||||
if(SUBSURFACE_MOBILE)
|
||||
set(MOBILE_SRC qt-mobile/qmlmanager.cpp)
|
||||
set(MOBILE_SRC qt-mobile/qmlmanager.cpp qt-models/divelistmodel.cpp)
|
||||
add_definitions(-DSUBSURFACE_MOBILE)
|
||||
qt5_add_resources(MOBILE_RESOURCES qt-mobile/mobile-resources.qrc)
|
||||
add_executable(subsurface-mobile ${MOBILE_SRC} ${SUBSURFACE_PKG} ${SUBSURFACE_APP} ${SUBSURFACE_RESOURCES} ${MOBILE_RESOURCES})
|
||||
|
|
|
@ -16,7 +16,9 @@
|
|||
#ifdef SUBSURFACE_MOBILE
|
||||
#include <QQuickWindow>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <QQmlContext>
|
||||
#include "qt-mobile/qmlmanager.h"
|
||||
#include "qt-models/divelistmodel.h"
|
||||
#endif
|
||||
|
||||
static MainWindow *window = NULL;
|
||||
|
@ -38,6 +40,9 @@ void run_ui()
|
|||
window->hide();
|
||||
qmlRegisterType<QMLManager>("org.subsurfacedivelog.mobile", 1, 0, "QMLManager");
|
||||
QQmlApplicationEngine engine;
|
||||
DiveListModel diveListModel;
|
||||
QQmlContext *ctxt = engine.rootContext();
|
||||
ctxt->setContextProperty("diveModel", &diveListModel);
|
||||
engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));
|
||||
QObject *mainWindow = engine.rootObjects().value(0);
|
||||
QQuickWindow *qml_window = qobject_cast<QQuickWindow *>(mainWindow);
|
||||
|
|
|
@ -37,4 +37,53 @@ ApplicationWindow {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
Rectangle {
|
||||
width: parent.width; height: parent.height
|
||||
anchors.fill: parent
|
||||
|
||||
Component {
|
||||
id: diveDelegate
|
||||
Item {
|
||||
id: wrapper
|
||||
width: parent.width; height: 55
|
||||
Column {
|
||||
Text { text: '#:' + diveNumber + "(" + location + ")" }
|
||||
Text { text: date }
|
||||
Text { text: duration + " " + depth }
|
||||
}
|
||||
MouseArea { anchors.fill: parent; onClicked: diveListView.currentIndex = index }
|
||||
|
||||
states: State {
|
||||
name: "Current"
|
||||
when: wrapper.ListView.isCurrentItem
|
||||
PropertyChanges { target: wrapper; x:20 }
|
||||
}
|
||||
transitions: Transition {
|
||||
NumberAnimation { properties: "x"; duration: 200 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Component {
|
||||
id: highlightBar
|
||||
Rectangle {
|
||||
width: parent.width; height: 50
|
||||
color: "#FFFF88"
|
||||
y: diveListView.currentItem.y;
|
||||
Behavior on y { SpringAnimation { spring: 2; damping: 0.1 } }
|
||||
}
|
||||
}
|
||||
|
||||
ListView {
|
||||
id: diveListView
|
||||
width: parent.width; height: parent.height
|
||||
anchors.fill: parent
|
||||
model: diveModel
|
||||
delegate: diveDelegate
|
||||
focus: true
|
||||
highlight: highlightBar
|
||||
highlightFollowsCurrentItem: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
#include "qmlmanager.h"
|
||||
#include <QUrl>
|
||||
|
||||
#include "../qt-models/divelistmodel.h"
|
||||
|
||||
QMLManager::QMLManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
QMLManager::~QMLManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QString QMLManager::filename()
|
||||
|
@ -20,12 +20,21 @@ QString QMLManager::filename()
|
|||
void QMLManager::setFilename(const QString &f)
|
||||
{
|
||||
m_fileName = f;
|
||||
emit filenameChanged();
|
||||
loadFile();
|
||||
emit filenameChanged();
|
||||
}
|
||||
|
||||
void QMLManager::loadFile()
|
||||
{
|
||||
QUrl url(m_fileName);
|
||||
QString strippedFileName = url.toLocalFile();
|
||||
|
||||
parse_file(strippedFileName.toUtf8().data());
|
||||
|
||||
int i;
|
||||
struct dive *d;
|
||||
|
||||
for_each_dive(i, d) {
|
||||
DiveListModel::instance()->addDive(d);
|
||||
}
|
||||
}
|
||||
|
|
225
qt-models/divelistmodel.cpp
Normal file
225
qt-models/divelistmodel.cpp
Normal file
|
@ -0,0 +1,225 @@
|
|||
#include "divelistmodel.h"
|
||||
#include "helpers.h"
|
||||
|
||||
Dive::Dive(dive *d)
|
||||
{
|
||||
m_thisDive = d;
|
||||
setDiveNumber(QString::number(d->number));
|
||||
setDate(get_dive_date_string(d->when));
|
||||
setDepth(get_depth_string(d->maxdepth));
|
||||
setDuration(get_dive_duration_string(d->duration.seconds, "h:","min"));
|
||||
|
||||
if (!d->watertemp.mkelvin)
|
||||
m_depth = "";
|
||||
|
||||
if (get_units()->temperature == units::CELSIUS)
|
||||
m_depth = QString::number(mkelvin_to_C(d->watertemp.mkelvin), 'f', 1);
|
||||
else
|
||||
m_depth = QString::number(mkelvin_to_F(d->watertemp.mkelvin), 'f', 1);
|
||||
|
||||
weight_t tw = { total_weight(d) };
|
||||
setWeight(weight_string(tw.grams));
|
||||
|
||||
setSuit(QString(d->suit));
|
||||
setCylinder(QString(d->cylinder[0].type.description));
|
||||
setSac(QString::number(d->sac));
|
||||
setLocation(get_dive_location(d));
|
||||
}
|
||||
|
||||
QString Dive::date() const
|
||||
{
|
||||
return m_date;
|
||||
}
|
||||
|
||||
void Dive::setDate(const QString &date)
|
||||
{
|
||||
m_date = date;
|
||||
}
|
||||
QString Dive::location() const
|
||||
{
|
||||
return m_location;
|
||||
}
|
||||
|
||||
void Dive::setLocation(const QString &location)
|
||||
{
|
||||
m_location = location;
|
||||
}
|
||||
QString Dive::sac() const
|
||||
{
|
||||
return m_sac;
|
||||
}
|
||||
|
||||
void Dive::setSac(const QString &sac)
|
||||
{
|
||||
m_sac = sac;
|
||||
}
|
||||
QString Dive::gas() const
|
||||
{
|
||||
return m_gas;
|
||||
}
|
||||
|
||||
void Dive::setGas(const QString &gas)
|
||||
{
|
||||
m_gas = gas;
|
||||
}
|
||||
QString Dive::cylinder() const
|
||||
{
|
||||
return m_cylinder;
|
||||
}
|
||||
|
||||
void Dive::setCylinder(const QString &cylinder)
|
||||
{
|
||||
m_cylinder = cylinder;
|
||||
}
|
||||
QString Dive::suit() const
|
||||
{
|
||||
return m_suit;
|
||||
}
|
||||
|
||||
void Dive::setSuit(const QString &suit)
|
||||
{
|
||||
m_suit = suit;
|
||||
}
|
||||
QString Dive::weight() const
|
||||
{
|
||||
return m_weight;
|
||||
}
|
||||
|
||||
void Dive::setWeight(const QString &weight)
|
||||
{
|
||||
m_weight = weight;
|
||||
}
|
||||
QString Dive::temp() const
|
||||
{
|
||||
return m_temp;
|
||||
}
|
||||
|
||||
void Dive::setTemp(const QString &temp)
|
||||
{
|
||||
m_temp = temp;
|
||||
}
|
||||
QString Dive::duration() const
|
||||
{
|
||||
return m_duration;
|
||||
}
|
||||
|
||||
void Dive::setDuration(const QString &duration)
|
||||
{
|
||||
m_duration = duration;
|
||||
}
|
||||
QString Dive::depth() const
|
||||
{
|
||||
return m_depth;
|
||||
}
|
||||
|
||||
void Dive::setDepth(const QString &depth)
|
||||
{
|
||||
m_depth = depth;
|
||||
}
|
||||
QString Dive::rating() const
|
||||
{
|
||||
return m_rating;
|
||||
}
|
||||
|
||||
void Dive::setRating(const QString &rating)
|
||||
{
|
||||
m_rating = rating;
|
||||
}
|
||||
dive *Dive::thisDive() const
|
||||
{
|
||||
return m_thisDive;
|
||||
}
|
||||
|
||||
void Dive::setThisDive(dive *thisDive)
|
||||
{
|
||||
m_thisDive = thisDive;
|
||||
}
|
||||
QString Dive::diveNumber() const
|
||||
{
|
||||
return m_diveNumber;
|
||||
}
|
||||
|
||||
void Dive::setDiveNumber(const QString &diveNumber)
|
||||
{
|
||||
m_diveNumber = diveNumber;
|
||||
}
|
||||
|
||||
|
||||
DiveListModel *DiveListModel::m_instance = NULL;
|
||||
|
||||
DiveListModel::DiveListModel(QObject *parent) : QAbstractListModel(parent)
|
||||
{
|
||||
m_instance = this;
|
||||
}
|
||||
|
||||
void DiveListModel::addDive(dive *d)
|
||||
{
|
||||
beginInsertRows(QModelIndex(), rowCount(), rowCount());
|
||||
m_dives.append(Dive(d));
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
int DiveListModel::rowCount(const QModelIndex &) const
|
||||
{
|
||||
return m_dives.count();
|
||||
}
|
||||
|
||||
QVariant DiveListModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if(index.row() < 0 || index.row() > m_dives.count())
|
||||
return QVariant();
|
||||
|
||||
const Dive &dive = m_dives[index.row()];
|
||||
|
||||
if (role == DiveNumberRole)
|
||||
return dive.diveNumber();
|
||||
else if (role == DiveDateRole)
|
||||
return dive.date();
|
||||
else if (role == DiveRatingRole)
|
||||
return dive.rating();
|
||||
else if (role == DiveDepthRole)
|
||||
return dive.depth();
|
||||
else if (role == DiveDurationRole)
|
||||
return dive.duration();
|
||||
else if (role == DiveTemperatureRole)
|
||||
return dive.temp();
|
||||
else if (role == DiveWeightRole)
|
||||
return dive.weight();
|
||||
else if (role == DiveSuitRole)
|
||||
return dive.suit();
|
||||
else if (role == DiveCylinderRole)
|
||||
return dive.cylinder();
|
||||
else if (role == DiveGasRole)
|
||||
return dive.gas();
|
||||
else if (role == DiveSacRole)
|
||||
return dive.sac();
|
||||
else if (role == DiveLocationRole)
|
||||
return dive.location();
|
||||
return QVariant();
|
||||
|
||||
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> DiveListModel::roleNames() const
|
||||
{
|
||||
QHash<int, QByteArray> roles;
|
||||
roles[DiveNumberRole] = "diveNumber";
|
||||
roles[DiveDateRole] = "date";
|
||||
roles[DiveRatingRole] = "rating";
|
||||
roles[DiveDepthRole] = "depth";
|
||||
roles[DiveDurationRole] = "duration";
|
||||
roles[DiveTemperatureRole] = "temp";
|
||||
roles[DiveWeightRole] = "weight";
|
||||
roles[DiveSuitRole] = "suit";
|
||||
roles[DiveCylinderRole] = "cylinder";
|
||||
roles[DiveGasRole] = "gas";
|
||||
roles[DiveSacRole] = "sac";
|
||||
roles[DiveLocationRole] = "location";
|
||||
|
||||
return roles;
|
||||
}
|
||||
|
||||
DiveListModel *DiveListModel::instance()
|
||||
{
|
||||
return m_instance;
|
||||
}
|
100
qt-models/divelistmodel.h
Normal file
100
qt-models/divelistmodel.h
Normal file
|
@ -0,0 +1,100 @@
|
|||
#ifndef DIVELISTMODEL_H
|
||||
#define DIVELISTMODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include "dive.h"
|
||||
|
||||
class Dive {
|
||||
public:
|
||||
Dive(dive* d);
|
||||
|
||||
QString date() const;
|
||||
void setDate(const QString &date);
|
||||
|
||||
QString location() const;
|
||||
void setLocation(const QString &location);
|
||||
|
||||
QString sac() const;
|
||||
void setSac(const QString &sac);
|
||||
|
||||
QString gas() const;
|
||||
void setGas(const QString &gas);
|
||||
|
||||
QString cylinder() const;
|
||||
void setCylinder(const QString &cylinder);
|
||||
|
||||
QString suit() const;
|
||||
void setSuit(const QString &suit);
|
||||
|
||||
QString weight() const;
|
||||
void setWeight(const QString &weight);
|
||||
|
||||
QString temp() const;
|
||||
void setTemp(const QString &temp);
|
||||
|
||||
QString duration() const;
|
||||
void setDuration(const QString &duration);
|
||||
|
||||
QString depth() const;
|
||||
void setDepth(const QString &depth);
|
||||
|
||||
QString rating() const;
|
||||
void setRating(const QString &rating);
|
||||
|
||||
dive *thisDive() const;
|
||||
void setThisDive(dive *thisDive);
|
||||
|
||||
QString diveNumber() const;
|
||||
void setDiveNumber(const QString &diveNumber);
|
||||
|
||||
private:
|
||||
QString m_diveNumber;
|
||||
QString m_date;
|
||||
QString m_rating;
|
||||
QString m_depth;
|
||||
QString m_duration;
|
||||
QString m_temp;
|
||||
QString m_weight;
|
||||
QString m_suit;
|
||||
QString m_cylinder;
|
||||
QString m_gas;
|
||||
QString m_sac;
|
||||
QString m_location;
|
||||
|
||||
|
||||
dive *m_thisDive;
|
||||
};
|
||||
|
||||
class DiveListModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
enum DiveListRoles {
|
||||
DiveNumberRole = Qt::UserRole + 1,
|
||||
DiveDateRole,
|
||||
DiveRatingRole,
|
||||
DiveDepthRole,
|
||||
DiveDurationRole,
|
||||
DiveTemperatureRole,
|
||||
DiveWeightRole,
|
||||
DiveSuitRole,
|
||||
DiveCylinderRole,
|
||||
DiveGasRole,
|
||||
DiveSacRole,
|
||||
DiveLocationRole
|
||||
};
|
||||
|
||||
static DiveListModel *instance();
|
||||
DiveListModel(QObject *parent = 0);
|
||||
void addDive(dive *d);
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
||||
QHash<int, QByteArray> roleNames() const;
|
||||
|
||||
private:
|
||||
QList<Dive> m_dives;
|
||||
static DiveListModel *m_instance;
|
||||
};
|
||||
|
||||
#endif // DIVELISTMODEL_H
|
Loading…
Reference in a new issue