[PATCH 3/3] QML UI: Add helper function to get weights

Tomaz Canabrava tcanabrava at kde.org
Mon Jan 25 16:25:10 PST 2016


On Mon, Jan 25, 2016 at 3:44 PM, Joakim Bygdell <j.bygdell at gmail.com> wrote:

> Helper function that retrieved the total weight for a dive to be displayed
> on the DiveDetials page.
>
> Signed-off-by: Joakim Bygdell <j.bygdell at gmail.com>
> ---
>  qt-mobile/qml/DiveDetailsView.qml |  2 +-
>  qt-mobile/qmlmanager.cpp          | 20 ++++++++++++++++++++
>  qt-mobile/qmlmanager.h            |  1 +
>  3 files changed, 22 insertions(+), 1 deletion(-)
>
> diff --git a/qt-mobile/qml/DiveDetailsView.qml
> b/qt-mobile/qml/DiveDetailsView.qml
> index 6c33359..df1d255 100644
> --- a/qt-mobile/qml/DiveDetailsView.qml
> +++ b/qt-mobile/qml/DiveDetailsView.qml
> @@ -13,6 +13,7 @@ Item {
>         id: detailsView
>         property int labelWidth: MobileComponents.Units.gridUnit * 10
>         property string cylinder: manager.getCylinder(dive.id)
> +       property string weight: manager.getWeights(dive.id)
>         width: parent.width
>         height: mainLayout.implicitHeight +
> MobileComponents.Units.iconSizes.large
>         Rectangle {
> @@ -171,7 +172,6 @@ Item {
>                 MobileComponents.Label {
>                         id: txtWeight
>                         text: weight
> -                       Layout.fillWidth: true
>                         wrapMode: TextEdit.WrapAtWordBoundaryOrAnywhere
>                         Layout.alignment: Qt.AlignRight
>                 }
> diff --git a/qt-mobile/qmlmanager.cpp b/qt-mobile/qmlmanager.cpp
> index d2b5c47..a4cfd5f 100644
> --- a/qt-mobile/qmlmanager.cpp
> +++ b/qt-mobile/qmlmanager.cpp
> @@ -718,3 +718,23 @@ QString QMLManager::getCylinder(QString diveId)
>                 cylinder = d->cylinder[0].type.description;
>         return cylinder;
>  }
> +
> +QString QMLManager::getWeights(QString diveId)
> +{
> +       int dive_id = diveId.toInt();
> +       struct dive *d = get_dive_by_uniq_id(dive_id);
> +       int weight = 0;
> +       QString weights;
> +       if (d){
> +               for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++)
> +                       weight += d->weightsystem[i].weight.grams;
> +
> +               if (informational_prefs.unit_system == IMPERIAL){
> +                       weights = QString::number(grams_to_lbs(weight)) +
> " lbs";
> +               }
> +               else {
> +                       weights = QString::number(weight/1000) + " kg";
> +               }
> +       }
> +       return weights;
> +}
>

I don't like very much what's happening to QMLManager at this point, it's
becomming a godlike class.

we already have a DiveObjectWrapper that's exported to Qml as 'Dive' so you
can use a Dive object there directly,

you can add a Q_INVOKABLE QString DiveObjectWrapper::weightsAsString() (or
any other name) {
    your foreach code up there;
}

and call it on the QML as follows:

model.dive.weightsAsString();



> diff --git a/qt-mobile/qmlmanager.h b/qt-mobile/qmlmanager.h
> index 57b67c2..bcaf9c7 100644
> --- a/qt-mobile/qmlmanager.h
> +++ b/qt-mobile/qmlmanager.h
> @@ -85,6 +85,7 @@ public slots:
>         QString getDate(QString diveId);
>         QString getCurrentPosition();
>         QString getCylinder(QString diveId);
> +       QString getWeights(QString diveId);
>         void deleteGpsFix(quint64 when);
>         void refreshDiveList();
>
> --
> 2.4.9 (Apple Git-60)
>
> _______________________________________________
> subsurface mailing list
> subsurface at subsurface-divelog.org
> http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.subsurface-divelog.org/pipermail/subsurface/attachments/20160125/51abcdf7/attachment-0001.html>


More information about the subsurface mailing list