Skip to content

Commit

Permalink
Properly remove animations at overwriting on init.
Browse files Browse the repository at this point in the history
During initialization it can happen that a property gets assigned a
binding and later, still during init, gets assigned a constant value.
In this case it wouldn't properly remove the binding and when later,
during evaluating bindings phase, the constant value would get
overwritten again, by the binding. This is not intended.

No it's cleaned correctly.
  • Loading branch information
akreuzkamp committed Mar 3, 2015
1 parent 486cc05 commit 08a4558
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
36 changes: 21 additions & 15 deletions src/qtcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function createSimpleProperty(type, obj, propName) {
return obj.$properties[propName].get();
};
var setter = function(newVal) {
return obj.$properties[propName].set(newVal);
return obj.$properties[propName].set(newVal, QMLProperty.ReasonUser);
};
setupGetterSetter(obj, propName, getter, setter);
if (obj.$isComponentRoot)
Expand All @@ -345,6 +345,10 @@ function QMLProperty(type, obj, name) {
this.$tidyupList = [];
}

QMLProperty.ReasonUser = 0;
QMLProperty.ReasonInit = 1;
QMLProperty.ReasonAnimtation = 2;

// Updater recalculates the value of a property if one of the
// dependencies changed
QMLProperty.prototype.update = function() {
Expand Down Expand Up @@ -381,7 +385,7 @@ QMLProperty.prototype.get = function() {
}

// Define setter
QMLProperty.prototype.set = function(newVal, fromAnimation, objectScope, componentScope) {
QMLProperty.prototype.set = function(newVal, reason, objectScope, componentScope) {
var i,
oldVal = this.val;

Expand All @@ -404,7 +408,7 @@ QMLProperty.prototype.set = function(newVal, fromAnimation, objectScope, compone
return;
}
} else {
if (!fromAnimation)
if (reason != QMLProperty.ReasonAnimation)
this.binding = null;
if (newVal instanceof Array)
newVal = newVal.slice(); // Copies the array
Expand All @@ -424,7 +428,7 @@ QMLProperty.prototype.set = function(newVal, fromAnimation, objectScope, compone
}

if (this.val !== oldVal) {
if (this.animation && !fromAnimation) {
if (this.animation && reason == QMLProperty.ReasonUser) {
this.animation.running = false;
this.animation.$actions = [{
target: this.animation.target || this.obj,
Expand All @@ -434,7 +438,7 @@ QMLProperty.prototype.set = function(newVal, fromAnimation, objectScope, compone
}];
this.animation.running = true;
}
if (this.obj.$syncPropertyToRemote instanceof Function && !fromAnimation) { // is a remote object from e.g. a QWebChannel
if (this.obj.$syncPropertyToRemote instanceof Function && reason == QMLProperty.ReasonUser) { // is a remote object from e.g. a QWebChannel
this.obj.$syncPropertyToRemote(this.name, newVal);
} else {
this.changed(this.val, oldVal, this.name);
Expand Down Expand Up @@ -550,15 +554,15 @@ function applyProperties(metaObject, item, objectScope, componentScope) {
var obj = this.componentScope[this.val.objectName];
return this.val.propertyName ? obj.$properties[this.val.propertyName].get() : obj;
}
item.$properties[i].set = function(newVal, fromAnimation, objectScope, componentScope) {
item.$properties[i].set = function(newVal, reason, objectScope, componentScope) {
if (!this.val.propertyName)
throw "Cannot set alias property pointing to an QML object.";
this.componentScope[this.val.objectName].$properties[this.val.propertyName].set(newVal, fromAnimation, objectScope, componentScope);
this.componentScope[this.val.objectName].$properties[this.val.propertyName].set(newVal, reason, objectScope, componentScope);
}
continue;
} else if (value instanceof QMLPropertyDefinition) {
createSimpleProperty(value.type, item, i);
item.$properties[i].set(value.value, true, objectScope, componentScope);
item.$properties[i].set(value.value, QMLProperty.ReasonInit, objectScope, componentScope);
continue;
} else if (item[i] && value instanceof QMLMetaPropertyGroup) {
// Apply properties one by one, otherwise apply at once
Expand All @@ -567,7 +571,7 @@ function applyProperties(metaObject, item, objectScope, componentScope) {
}
}
if (item.$properties && i in item.$properties)
item.$properties[i].set(value, true, objectScope, componentScope);
item.$properties[i].set(value, QMLProperty.ReasonInit, objectScope, componentScope);
else if (i in item)
item[i] = value;
else if (item.$setCustomData)
Expand All @@ -577,7 +581,7 @@ function applyProperties(metaObject, item, objectScope, componentScope) {
}
if (metaObject.$children && metaObject.$children.length !== 0) {
if (item.$defaultProperty)
item.$properties[item.$defaultProperty].set(metaObject.$children, true, objectScope, componentScope);
item.$properties[item.$defaultProperty].set(metaObject.$children, QMLProperty.ReasonInit, objectScope, componentScope);
else
throw "Cannot assign to unexistant default property";
}
Expand Down Expand Up @@ -765,6 +769,8 @@ QMLEngine = function (element, options) {
// Initialize property bindings
for (var i = 0; i < this.bindedProperties.length; i++) {
var property = this.bindedProperties[i];
if (!property.binding)
continue; // Probably, the binding was overwritten by an explicit value. Ignore.
property.binding.compile();
property.update();
}
Expand Down Expand Up @@ -1491,7 +1497,7 @@ function QMLItem(meta) {
// before we fetch the values because properties can be interdependent.
for (i in actions) {
var action = actions[i];
action.target.$properties[action.property].set(action.value, false, action.target,
action.target.$properties[action.property].set(action.value, QMLProperty.ReasonUser, action.target,
newState ? newState.$context: action.target.$context);
}
for (i in actions) {
Expand Down Expand Up @@ -2470,7 +2476,7 @@ function QMLRepeater(meta) {
var model = self.model instanceof QMLListModel ? self.model.$model : self.model;
for (var i in model.roleNames) {
createSimpleProperty("variant", newItem, model.roleNames[i]);
newItem.$properties[model.roleNames[i]].set(model.data(index, model.roleNames[i]), true, newItem, self.model.$context);
newItem.$properties[model.roleNames[i]].set(model.data(index, model.roleNames[i]), QMLProperty.ReasonInit, newItem, self.model.$context);
}

self.parent.children.splice(self.parent.children.indexOf(self) - self.$items.length + index, 0, newItem);
Expand Down Expand Up @@ -2503,7 +2509,7 @@ function QMLRepeater(meta) {
roles = model.roleNames;
for (var index = startIndex; index <= endIndex; index++) {
for (var i in roles) {
self.$items[index].$properties[roles[i]].set(model.data(index, roles[i]), true, self.$items[index], self.model.$context);
self.$items[index].$properties[roles[i]].set(model.data(index, roles[i]), QMLProperty.ReasonInit, self.$items[index], self.model.$context);
}
}
});
Expand Down Expand Up @@ -3567,7 +3573,7 @@ function QMLNumberAnimation(meta) {
for (var i in self.$actions) {
var action = self.$actions[i],
value = self.easing.$valueForProgress(at) * (action.to - action.from) + action.from;
action.target.$properties[action.property].set(value, true);
action.target.$properties[action.property].set(value, QMLProperty.ReasonAnimation);
}
}
}
Expand Down Expand Up @@ -3595,7 +3601,7 @@ function QMLNumberAnimation(meta) {
this.complete = function() {
for (var i in this.$actions) {
var action = this.$actions[i];
action.target.$properties[action.property].set(action.to, true);
action.target.$properties[action.property].set(action.to, QMLProperty.ReasonAnimation);
}
engine.$requestDraw();

Expand Down
7 changes: 5 additions & 2 deletions test/testpad/TestComponent.qml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Rectangle {
border.width: greenRect.border.width
border.color: greenRect.border.color
default property alias kids: greenRect.children
property string txt: "Foo"
property string txt: "Fo" + "o"
signal test(string newColor, int number)

MouseArea {
Expand All @@ -35,7 +35,10 @@ Rectangle {

MouseArea {
anchors.fill: parent
onClicked: comp.test("darkred", Math.floor(10 * Math.random()));
onClicked: {
comp.test("darkred", Math.floor(10 * Math.random()));
console.log(txt);
}
}
}
}

0 comments on commit 08a4558

Please sign in to comment.