Active Topics

 


Reply
Thread Tools
Posts: 4 | Thanked: 0 times | Joined on Nov 2018
#1
Hi everyone, I don't know if this is the right place, but I'll try it anyway.
I was wondering if this method I implemented it's allowed or not. By allowed I mean if it is a terrible coding practise and it should be avoided, or if it is fair enough and I can freely use it.
I have to say in advance that I'm not a "featured" programmer, so I might not respect some good/common coding practise, and I'm sorry for that (and that's probably the reason why I'm writing here).

Basically I have an Item inside the file ParentQml.qml, in which I have two children:
Code:
// ParentQml.qml
Item{
	id: defaultParent
	property bool randomBoolProperty

	DefautChild {
		// no id
		property bool 	prop1
		property int 	prop2
		property string	prop3
		property var 	prop4

		enabled: parent.randomBoolProperty
	}
	property Item useUserQml: userChoice  ? MyLovelyQml{} : someDefaultQml{}
}
I simplified the picture, but the point is that "userQml" is variable according to the user choice.

As a user, I cannot modify neither ParentQml.qml or DefautChild.qml files, but only MyLovelyQml.qml.
My problem is that, in general, from inside the file MyLovelyQml.qml I cannot access to any of the properties of any of its parents' children.
But I am an hubristic user and I want to disable DefaultChild! (because it is in conflict with something I do inside MyLovelyQml.qml)

In order to accomplish this, here comes the hack.
I know that DefautChild{} has a (more or less) unique set of properties. Represented previously as "prop1", "prop2", "prop3" and "prop4".
The method I used to disable (on will) DefaultChild is the following.
Code:
// MyLovelyQml.qml
Item{
  id:myLovelyQml
  property bool myUniqueProperty: true
  onVisibleChanged: {
   if (myLovelyQml.visible) {
    var defaultChild_Idx = 0
    for (var i=0; i<parent.children.length; ++i){
      // seaching for the child 'DefaultChild' by looking at all 
      // the properties I know it has
      if ( (typeof parent.children[i].prop1 !== 'undefined') 
          && (typeof parent.children[i].prop2 !== 'undefined') 
          && (typeof parent.children[i].prop3 !== 'undefined') 
          && (typeof parent.children[i].prop4 !== 'undefined')){
        defaultChild_Idx = i;
      }
    }
    parent.children[defaultChild_Idx].enabled = Qt.binding(function() {
       return (parent.randomBoolProperty &&
         (typeof parent.userChoice.myUniqueProperty === 'undefined'))
           // when user will change "userChoice", "myUniqueProperty"
           // will be undefined and only the value of 
           // "randomBoolProperty" will matter
     })
   }
  }	
}
So, as you see, this feels quite like stealing someone else work, even though when "myUniqueProperty" does not exist (because ParentQml is loading some other user's qml and not mine) then the default value of the child property is maintained.

Ok, to be more specific I did this because I'm interested in special keyboard layouts in SailfishOS, such as ease, 8pen or flick. In order to work properly, these keyboards need to modify KeyboardBase.qml, in particular to disable the children MultiPointTouchArea{} and substitute it with something made ad hoc.
I didn't want to patch the non-user-accessible files, that is the reason I spent my time trying do cheat default files. As far as I understood the method works properly and I would like to share the so created layouts with the community, but if this is a practise that really shouldn't be done, I'll stick with the patching.

Please let me know what you think and share your coding experience with me.
 
Reply


 
Forum Jump


All times are GMT. The time now is 17:41.