Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Highlighted
Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany
Accepted Solution

Implement an own textfilter

I would like to implement my own text filter.

 

In the API there is AbstractTextFilter but it seems you cannot write your own extension to that, at least i did not find any guidelines and the API is quite empty.

 

The onTextChanged is fired both on user- and program-driven changes, and onTextChanging, despite stating "Emitted when the user enters a character in the text area" also fires on a programmatic change.

In any case, the signals are all async, so it would be difficult to implement something there i guess.

 

I am looking for a method like iOS shouldChangeCharactersInRange, or like keyChar in BB OS.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Implement an own textfilter

Have you looked at the KeyListener class?

 

http://developer.blackberry.com/cascades/documentation/dev/shortcuts/keylistener.html

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Implement an own textfilter

good idea, i'll take a look. its also async, but at least not looping when you modify the text.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Implement an own textfilter

[ Edited ]

I was also looking at this filter and it seems it's not ready to be used yet. It was added to SDK not too long ago.

 

onTextChanging can be blocked for program-driven changes by using control->blockSignals(true) ... blockSignals(false) or disconnecting/reconnecting the signal in QML.

 

I believe there isn't anything similar to shouldChangeCharactersInRange in current API.


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Implement an own textfilter

[ Edited ]

Zmey, What was the problem you found with it, as I'm currently cooking something up which was going to use it?

 

I agree with blocking the signals but I've found it gets messy quickly when you want to block some and have others such as your own.  Some kind of fllter signal blocker would be useful, perhaps in the next NDK update. Smiley Happy

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Implement an own textfilter

KeyListener is since 10.1, looks like i cannot use it at the moment.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Implement an own textfilter

I meant AbstractTextFilter, not KeyListener. Smiley Happy I haven't tried using KeyListener, thanks for suggesting it!

 

It's not getting messy if you block signals before manipulating a property then restore them right after that:

editBox->blockSignals(true);
editBox->setText(...);
editBox->blockSignals(false);

In iOS programmatic changes aren't triggering notifications so this is not needed.

 

In Qt (even in non-Cascades widgets) a common convention is to trigger updates, so this trick is often needed. Smiley Sad

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Implement an own textfilter

[ Edited ]

Thanks for clarifying, I'll continue down the KeyListener path.

 

As for the blocking for this example that's right, unfortunately I was trying to block UI signals coming in (i.e. peek, context menu opening) at different points within an onTouch handler. Smiley Frustrated

 

It doesn't need to be complex just a simple (accept my signals but ignore all others) filter would be fine. Smiley Happy

 

I'm trying to convince an engineer at BlackBerry at the moment that he needs to do something like this to fix a problem with peek.

 

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Implement an own textfilter

Hmm, how do i disconnect the signal in qml?

My TextArea has the id textArea:

 var success = textArea.textChanged.disconnect(textArea.onTextChanged)

 success is undefined, so it seems not to work.

maybe its time to hand it over to c++ Smiley Happy

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Implement an own textfilter

[ Edited ]

I have connected the dots, thanks @ Zmey for the block signals idea.

 

In my QML TextArea, textArea is the id of the element, textFilter a string with the filter expression:

onTextChanging: {                
  textUtil.filterTextElement(textArea, textFilter);                
}

 The c++ code (not using the textFilter parameter, just some random RegEx)

void TextUtil::filterTextElement(QObject* textElement, QString textFilter) {	
	TextArea* textArea = dynamic_cast<TextArea*>(textElement);
	if (textArea) {		
		textArea->blockSignals(true);
		textArea->setText(text.remove(QRegExp("[-`~!@#$%^&*()_—+=|:;<>«»,.?/{}\'\"\\\[\\\]\\\\]")));
		textArea->blockSignals(false);
	}
}

 I had to use dynamicCast as QML did not like a TextArea* parameter, but i decided that is for the better as i can also use it for TextField or similar components.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter