Skip to content

Commit

Permalink
TextWidget/MultiLineTextWidth : Support non-ASCII characters...
Browse files Browse the repository at this point in the history
...kindof. Our eventual aim is to standardise on representing text using UTF8-encoded `std::string` objects on the C++ side of things, and `unicode` objects on the Python side (see GafferHQ#1208). But here we're using UTF8 encoded `str` (bytes) objects on the Python side as well. Rationale :

- Returning unicode from `getText()` now would be too much of a breaking change, potentially affecting a _lot_ of client code. The better time to make this change will be when we transition to Python 3, when `str` objects _are_ unicode, and everything will be breaking left right and centre anyway.
- We've already got de facto use of UTF8 in Python anyway, because that's what we get from things like `os.listdir()` and `glob.glob()`.
- The main downside of using UTF8 `str` is that when multi-byte characters are present, naive indexing will produce incorrect results. This is evident in the API for `TextWidget.getSelection()` and `TextWidget.setSelection()`, which will not match the indexing of `TextWidget.getText()`. But we don't do much character-level processing of text, so in the majority of cases this is an improvement over the status quo, where `getText()` alone was unusable (throwing an exception).
  • Loading branch information
johnhaddon committed Jan 27, 2019
1 parent 0d69c05 commit 62de6cc
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python/GafferUI/MultiLineTextWidget.py
Expand Up @@ -82,7 +82,7 @@ def __init__( self, text="", editable=True, wrapMode=WrapMode.WordOrCharacter, f

def getText( self ) :

return str( self._qtWidget().toPlainText() )
return self._qtWidget().toPlainText().encode( "utf-8" )

def setText( self, text ) :

Expand Down
2 changes: 1 addition & 1 deletion python/GafferUI/TextWidget.py
Expand Up @@ -65,7 +65,7 @@ def setText( self, text ) :

def getText( self ) :

return str( self._qtWidget().text() )
return self._qtWidget().text().encode( "utf-8" )

def setEditable( self, editable ) :

Expand Down

0 comments on commit 62de6cc

Please sign in to comment.