| 1 |
/* |
| 2 |
* Copyright (C) 2012-2015 Canonical, Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
import QtQuick 2.4 |
| 18 |
|
| 19 |
FocusScope { |
| 20 |
id: root |
| 21 |
|
| 22 |
property var details: [] |
| 23 |
readonly property alias detailDelegates: contents.children |
| 24 |
readonly property int detailsCount: detailsModel.count |
| 25 |
|
| 26 |
property variant inputFields: [] |
| 27 |
property QtObject contact: null |
| 28 |
property int detailType: 0 |
| 29 |
property variant fields |
| 30 |
property string title: "" |
| 31 |
property alias headerDelegate: headerItem.sourceComponent |
| 32 |
property Component detailDelegate |
| 33 |
property int minimumHeight: 0 |
| 34 |
property bool loaded: false |
| 35 |
property bool showEmpty: true |
| 36 |
|
| 37 |
signal newFieldAdded(int fieldIndex, QtObject field) |
| 38 |
|
| 39 |
function reloadDetails(clearFields) |
| 40 |
{ |
| 41 |
if (clearFields) { |
| 42 |
root.inputFields = [] |
| 43 |
} |
| 44 |
|
| 45 |
if (contact && detailType) { |
| 46 |
root.details = contact.details(detailType) |
| 47 |
} else { |
| 48 |
root.details = [] |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
function filterDetails(details) { |
| 53 |
var result = [] |
| 54 |
for(var d in details) { |
| 55 |
var isEmpty = true |
| 56 |
for(var f in root.fields) { |
| 57 |
var fieldValue = details[d].value(root.fields[f]) |
| 58 |
if (fieldValue && (String(fieldValue) !== "")) { |
| 59 |
isEmpty = false |
| 60 |
break; |
| 61 |
} |
| 62 |
} |
| 63 |
if (!isEmpty) { |
| 64 |
result.push(details[d]) |
| 65 |
} |
| 66 |
} |
| 67 |
return result |
| 68 |
} |
| 69 |
|
| 70 |
onContactChanged: reloadDetails(true) |
| 71 |
onDetailTypeChanged: reloadDetails(true) |
| 72 |
|
| 73 |
Connections { |
| 74 |
target: root.contact |
| 75 |
function onContactChanged() { reloadDetails(false) } |
| 76 |
} |
| 77 |
|
| 78 |
implicitHeight: detailsCount > 0 ? contents.implicitHeight : minimumHeight |
| 79 |
visible: implicitHeight > 0 |
| 80 |
|
| 81 |
// This model is used to avoid rebuild the repeater every time that the details change |
| 82 |
// With this model the changed info on the fields will remain after add a new field |
| 83 |
ListModel { |
| 84 |
id: detailsModel |
| 85 |
|
| 86 |
property var values: root.showEmpty && root.details ? root.details : filterDetails(root.details) |
| 87 |
|
| 88 |
onValuesChanged: { |
| 89 |
if (!values) { |
| 90 |
root.inputFields = [] |
| 91 |
clear() |
| 92 |
return |
| 93 |
} |
| 94 |
|
| 95 |
while (count > values.length) { |
| 96 |
remove(count - 1) |
| 97 |
} |
| 98 |
|
| 99 |
var modelCount = count |
| 100 |
for(var i=0; i < values.length; i++) { |
| 101 |
if (modelCount <= i) { |
| 102 |
append({"detail": values[i]}) |
| 103 |
} else if (get(i) != values[i]) { |
| 104 |
set(i, {"detail": values[i]}) |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
Column { |
| 111 |
id: contents |
| 112 |
|
| 113 |
anchors { |
| 114 |
left: parent.left |
| 115 |
right: parent.right |
| 116 |
} |
| 117 |
|
| 118 |
Loader { |
| 119 |
id: headerItem |
| 120 |
} |
| 121 |
|
| 122 |
Repeater { |
| 123 |
id: detailFields |
| 124 |
|
| 125 |
model: detailsModel |
| 126 |
|
| 127 |
Loader { |
| 128 |
id: detailItem |
| 129 |
|
| 130 |
sourceComponent: root.detailDelegate |
| 131 |
Binding { |
| 132 |
target: detailItem.item |
| 133 |
property: "detail" |
| 134 |
value: model.detail |
| 135 |
} |
| 136 |
|
| 137 |
Binding { |
| 138 |
target: detailItem.item |
| 139 |
property: "index" |
| 140 |
value: index |
| 141 |
} |
| 142 |
|
| 143 |
onStatusChanged: { |
| 144 |
if (status === Loader.Ready) { |
| 145 |
var newFields = root.inputFields |
| 146 |
newFields.push(detailItem.item) |
| 147 |
|
| 148 |
root.inputFields = newFields |
| 149 |
if (root.loaded) { |
| 150 |
root.newFieldAdded(detailItem.item, item) |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
Component.onCompleted: root.loaded = true |
| 159 |
} |