| 1 |
/* |
| 2 |
* Copyright (C) 2016 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 version 3 as |
| 6 |
* published by the Free Software Foundation. |
| 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 |
* Authored by: Florian Boucault <[email protected]> |
| 17 |
*/ |
| 18 |
|
| 19 |
import QtQuick 2.4 |
| 20 |
|
| 21 |
QtObject { |
| 22 |
id: doubleBinding |
| 23 |
|
| 24 |
property var source |
| 25 |
property string sourceProperty |
| 26 |
property var target |
| 27 |
property string targetProperty |
| 28 |
|
| 29 |
function sourceToTarget(sourceValue) { |
| 30 |
return sourceValue; |
| 31 |
} |
| 32 |
|
| 33 |
function targetToSource(targetValue) { |
| 34 |
return targetValue; |
| 35 |
} |
| 36 |
|
| 37 |
Component.onCompleted: { |
| 38 |
if (source && target && sourceProperty && targetProperty) { |
| 39 |
updateTarget(); |
| 40 |
connectSourceChanges(); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
function connectTargetChanges() { |
| 45 |
target[targetProperty+"Changed"].connect(updateSource); |
| 46 |
} |
| 47 |
|
| 48 |
function disconnectTargetChanges() { |
| 49 |
target[targetProperty+"Changed"].disconnect(updateSource); |
| 50 |
} |
| 51 |
|
| 52 |
function connectSourceChanges() { |
| 53 |
source[sourceProperty+"Changed"].connect(updateTarget); |
| 54 |
} |
| 55 |
|
| 56 |
function disconnectSourceChanges() { |
| 57 |
source[sourceProperty+"Changed"].disconnect(updateTarget); |
| 58 |
} |
| 59 |
|
| 60 |
function updateTarget() { |
| 61 |
disconnectTargetChanges(); |
| 62 |
target[targetProperty] = sourceToTarget(source[sourceProperty]); |
| 63 |
connectTargetChanges(); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
function updateSource() { |
| 68 |
disconnectSourceChanges(); |
| 69 |
source[sourceProperty] = targetToSource(target[targetProperty]); |
| 70 |
connectSourceChanges(); |
| 71 |
} |
| 72 |
} |