While working on the filename stuff I noticed that fromqstr converts to the locale encoding, but we use it where a conversion to utf8 is needed in several places. This patch makes fromqstr convert to utf8. This is correct except for output to lyxerr, which should be in the locale encoding.
With this patch all output to lyxerr (not only the one from the core) is now in utf8. I will address this problem and a possible solution later and put in this patch for now. Enrico, maybe the fromqstr problem was also the reason for your invalid iconv conversions. Georg
Index: src/frontends/qt4/qt_helpers.C =================================================================== --- src/frontends/qt4/qt_helpers.C (Revision 16205) +++ src/frontends/qt4/qt_helpers.C (Arbeitskopie) @@ -155,7 +155,7 @@ QString const qt_(string const & str) string const fromqstr(QString const & str) { - return str.isEmpty()? string(): string(str.toAscii()); + return str.isEmpty() ? string() : string(str.toUtf8()); } Index: src/frontends/qt4/qt_helpers.h =================================================================== --- src/frontends/qt4/qt_helpers.h (Revision 16205) +++ src/frontends/qt4/qt_helpers.h (Arbeitskopie) @@ -45,11 +45,12 @@ void lengthToWidgets(QLineEdit * input, /// format a string to the given width docstring const formatted(docstring const & text, int w = 80); + /** - * toqstr - convert char * into Qt's unicode (UTF16) + * toqstr - convert a UTF8 encoded char * into a QString * - * Use this whenever there's a user-visible string that is encoded - * for the locale (menus, dialogs etc.) + * This should not be used, since all possibly non-ASCII stuff should be + * stored in a docstring. */ inline QString const toqstr(char const * str) { @@ -58,10 +59,10 @@ inline QString const toqstr(char const * /** - * toqstr - convert string into unicode + * toqstr - convert a UTF8 encoded std::string into a QString * - * Use this whenever there's a user-visible string that is encoded - * for the locale (menus, dialogs etc.) + * This should not be used, since all possibly non-ASCII stuff should be + * stored in a docstring. */ inline QString const toqstr(std::string const & str) { @@ -70,15 +71,27 @@ inline QString const toqstr(std::string /** - * toqstr - convert ucs4 into QString - * - * QString uses utf16 internally. + * Convert a QChar into a UCS4 character. + * This is a hack (it does only make sense for the common part of the UCS4 + * and UTF16 encodings) and should not be used. + * This does only exist because of performance reasons (a real conversion + * using iconv is too slow on windows). */ -inline char_type const qchar_to_ucs4(QChar const & qchar) { +inline char_type const qchar_to_ucs4(QChar const & qchar) +{ return static_cast<char_type>(qchar.unicode()); } -inline QChar const ucs4_to_qchar(char_type const ucs4) { + +/** + * Convert a UCS4 character into a QChar. + * This is a hack (it does only make sense for the common part of the UCS4 + * and UTF16 encodings) and should not be used. + * This does only exist because of performance reasons (a real conversion + * using iconv is too slow on windows). + */ +inline QChar const ucs4_to_qchar(char_type const ucs4) +{ // FIXME: The following cast is not a real conversion but it work // for the ucs2 subrange of unicode. Instead of an assertion we should // return some special characters that indicates that its display is @@ -88,6 +101,12 @@ inline QChar const ucs4_to_qchar(char_ty } +/** + * toqstr - convert a UCS4 encoded docstring into a QString + * + * This is the preferred method of converting anything that possibly + * contains non-ASCII stuff to QString. + */ #if QT_VERSION >= 0x040200 inline QString const toqstr(docstring const & ucs4) { @@ -100,6 +119,12 @@ QString const toqstr(docstring const & u #endif +/** + * ucs4_to_qstring - convert a UCS4 encoded char_type * into a QString + * + * This is a hack for the painter and font metrics and should not be used + * elsewhere. Since it uses ucs4_to_qchar it has the same limitations. + */ inline void ucs4_to_qstring(char_type const * str, size_t ls, QString & s) { int i = static_cast<int>(ls); @@ -109,11 +134,17 @@ inline void ucs4_to_qstring(char_type co } +/** + * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring + * + * This is the preferred method of converting anything that possibly + * contains non-ASCII stuff to docstring. + */ docstring const qstring_to_ucs4(QString const & qstr); /** - * qt_ - i18nize string and convert to unicode + * qt_ - i18nize string and convert to QString * * Use this in qt4/ instead of _() */ @@ -121,7 +152,7 @@ QString const qt_(char const * str, cons /** - * qt_ - i18nize string and convert to unicode + * qt_ - i18nize string and convert to QString * * Use this in qt4/ instead of _() */ @@ -129,13 +160,13 @@ QString const qt_(std::string const & st /** - * fromqstr - convert QString into std::string in locale + * fromqstr - convert a QString into a UTF8 encoded std::string * - * Return the QString encoded in the locale + * This should not be used except for output to lyxerr, since all possibly + * non-ASCII stuff should be stored in a docstring. */ std::string const fromqstr(QString const & str); - } // namespace lyx #endif // QTHELPERS_H Index: src/frontends/qt4/README =================================================================== --- src/frontends/qt4/README (Revision 16205) +++ src/frontends/qt4/README (Arbeitskopie) @@ -1,4 +1,4 @@ -This file contains some do's and dont's for the Qt2 frontend. +This file contains some do's and dont's for the Qt4 frontend. General rules ------------- @@ -83,10 +83,11 @@ ex. Qt, Unicode, and LyX -------------------- -LyX isn't unicoded yet. But you should follow these simple rules : +LyX uses a different encoding (UCS4) than Qt (UTF16), therefore there are a +number of conversion functions in qt_helpers.[Ch]. Read the doxygen +documentation for details when to use which function. -o Use qt_() not _() in code -o Use fromqstr and toqstr NOT .latin1() / .c_str() +Additionally, you should follow these simple rules : -Using these functions (in qt_helpers.h) will make sure we use -the right locale for converting to Qt's QString, which is unicode. +o Use qt_() not _() in code +o Use the conversion functions of qt_helpers.h, NOT .latin1() / .c_str() etc.