Permalink
Browse files

Add an attachments ListModel to Bill objects

We need to use various workaround as:
- u1db load models in ListModel and any array property (like attachments) are
  transformed in a ListModel
- however, we can't store directly a ListModel (containing ModelObject) to u1db
  as it's transformed as null in the sqlite lite database
  • Loading branch information...
1 parent 3b69022 commit 9d1e72131f7a2842bea08b4e144895ef0a06f4d2 @didrocks committed Aug 11, 2015
@@ -2,7 +2,7 @@ import QtQuick 2.4
import "../tools.js" as Tools
-QtObject {
+Item {
id: self
property string billId
@@ -13,9 +13,12 @@ QtObject {
property int numTotalPeople
property int numSharePeople
property string comments
+ // we use directly a ListModel as a list of Strings is automatically transtyped as such by any attached property
+ // to a model
+ property ListModel attachments: newListModel.createObject(parent)
property var billSavedProperties: ["billId", "title", "date", "rawBill", "tipShare", "numTotalPeople",
- "numSharePeople", "comments"]
+ "numSharePeople", "comments", "attachments"]
readonly property double bill: {
var value = parseFloat(Tools.normalizeNum(rawBill));
@@ -33,6 +36,10 @@ QtObject {
readonly property double shareBill: _sharePercent * totalBill
readonly property string formattedDate: date.toLocaleDateString() + " - " + date.toLocaleTimeString()
+ NewListModel {
+ id: newListModel
+ }
+
/* load from json or dictionary compatible objects */
// this show that qml property can be accessed with object["name"] or object.name
function loadFromJson(billJson) {
@@ -49,7 +56,33 @@ QtObject {
var returnJson = {};
for (var index in billSavedProperties) {
var prop = billSavedProperties[index];
- returnJson[prop] = self[prop];
+ /* WORKAROUND:
+ * assigning a list of anything as a property in a model makes it a QMLListModel. We want for our
+ * Bill object to still have a list of json objects for storing in u1db
+ */
+ if (self[prop].toString().indexOf("QQmlListModel") === 0) {
+ var listResult = [];
+ for (var i = 0; i < self[prop].count; i++) {
+ var elem = self[prop].get(i);
+ var jsonToStore = {};
+ /*
+ * transform a ModelObject to json:
+ * - we can't store in u1db a ModelObject directly, it will store null
+ * - there is no trivial function to achieve it (so having to parse it after stringifying it)
+ * - we want to be flexible and not knowing in advance the properties that can get out of it
+ */
+ var jsonElem = JSON.parse(JSON.stringify(elem));
+ for (var propElem in jsonElem) {
+ // we don't store the objectName
+ if (propElem === "objectName")
+ continue;
+ jsonToStore[propElem] = jsonElem[propElem];
+ }
+ listResult.push(jsonToStore);
+ }
+ returnJson[prop] = listResult;
+ } else
+ returnJson[prop] = self[prop];
}
return returnJson;
}
@@ -63,5 +96,6 @@ QtObject {
numTotalPeople = 2;
numSharePeople = 1;
comments = "";
+ attachments = newListModel.createObject(parent);
}
}
@@ -25,6 +25,11 @@ ListItem {
numSharePeople: model.numSharePeople
// be future-proof when adding new properties
comments: model.comments ? model.comments: ""
+ attachments: model.attachments ? model.attachments : newListModel.createObject(parent)
+ }
+
+ NewListModel {
+ id: newListModel
}
Column {
@@ -0,0 +1,7 @@
+import QtQuick 2.4
+
+// this show how to create component dynamically
+Component {
+ ListModel {
+ }
+}
@@ -24,7 +24,8 @@ OTHER_FILES += $${CONF_FILES} \
pages/SettingsPage.qml \
components/AppSettings.qml \
components/ErrorDialog.qml \
- components/DateTimeDialog.qml
+ components/DateTimeDialog.qml \
+ components/NewListModel.qml
#specify where the qml/js files are installed to
qml_files.path = /splitthebill

0 comments on commit 9d1e721

Please sign in to comment.