TextPropertyComponent confusion


#1

I was trying to subclass the TextPropertyComponent to create a restriction so only integer values would be accepted and I was struggling to get it working by overriding the setText() method, so I decided to create a subclass of PropertyComponent instead, using the TextPropertyComponent as a template.

Anyway, while doing this, I found the following, which I can't get my head around:


void TextPropertyComponent::textWasEdited()
{
    const String newText (textEditor->getText());
    if (getText() != newText)
        setText (newText);
}

where 

String TextPropertyComponent::getText() const
{
    return textEditor->getText();
}

Surely this means that the setText() method will never be called from textWasEdited()?


#2

No.. If you look inside the Label::getText method, it's really not that simple. Are you reporting this because you actually found a situation where the TextPropertyComponent isn't working correctly?


#3

So, the TextPropertyComponent works fine, but as far as I can tell, it works because of the following line in the constructor:

textEditor->getTextValue().referTo (valueToControl);

not because of the textWasEdited callback from the TextEditor.

It works when you override both getText() and setText(), like you're doing in the Introjucer's PaintElementText::TextProperty:

void   setText (const String& newText)  { element->setText (newText, true); }
String getText() const                  { return element->getText(); }

because in that case the TextProperty's getText() does not return the same value as the TextEditor's getText().

To be clear, I'm just pointing this out because it caused me confusion while trying to subclass the TextPropertyComponent and means you can't override setText() without also overriding getText() (which isn't the end of the world).

P.S. I wasn't sure how to read your comment re. the Label::getText method, as I can't see that this is called anywhere here.


#4

 I wasn't sure how to read your comment re. the Label::getText method, as I can't see that this is called anywhere here.

The textEditor object is a Label, so that's where it gets called.


#5

Ok, I am officially a dope for asking that question :) Anyway, the rest still holds...


String Label::getText (const bool returnActiveEditorContents) const
{
    return (returnActiveEditorContents && isBeingEdited())
                ? editor->getText()
                : textValue.toString();
}

That's being called in the same context twice, without passing in a value for returnActiveEditorContents, so it must be returning the same value each time.


#6

Well the point is that if you've overridden TextPropertyComponent::getText, then that could be doing anything, it's not necessarily just returning the label's getText.


#7

Ok, I understand now :)