3 * \file qstring_helpers.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
12 #ifndef QSTRING_HELPERS_H
13 #define QSTRING_HELPERS_H
15 #include "support/docstring.h"
20 #include <boost/assert.hpp>
25 * toqstr - convert a UTF8 encoded char * into a QString
27 * This should not be used, since all possibly non-ASCII stuff should be
28 * stored in a docstring.
30 inline QString const toqstr(char const * str)
32 return QString::fromUtf8(str);
37 * toqstr - convert a UTF8 encoded std::string into a QString
39 * This should not be used, since all possibly non-ASCII stuff should be
40 * stored in a docstring.
42 inline QString const toqstr(std::string const & str)
44 return toqstr(str.c_str());
49 * Convert a QChar into a UCS4 character.
50 * This is a hack (it does only make sense for the common part of the UCS4
51 * and UTF16 encodings) and should not be used.
52 * This does only exist because of performance reasons (a real conversion
53 * using iconv is too slow on windows).
55 inline char_type const qchar_to_ucs4(QChar const & qchar)
57 return static_cast<char_type>(qchar.unicode());
62 * Convert a UCS4 character into a QChar.
63 * This is a hack (it does only make sense for the common part of the UCS4
64 * and UTF16 encodings) and should not be used.
65 * This does only exist because of performance reasons (a real conversion
66 * using iconv is too slow on windows).
68 inline QChar const ucs4_to_qchar(char_type const ucs4)
70 // FIXME: The following cast is not a real conversion but it work
71 // for the ucs2 subrange of unicode. Instead of an assertion we should
72 // return some special characters that indicates that its display is
74 BOOST_ASSERT(ucs4 < 65536);
75 return QChar(static_cast<unsigned short>(ucs4));
80 * toqstr - convert a UCS4 encoded docstring into a QString
82 * This is the preferred method of converting anything that possibly
83 * contains non-ASCII stuff to QString.
85 #if QT_VERSION >= 0x040200
86 inline QString const toqstr(docstring const & ucs4)
88 // If possible we let qt do the work, since this version does not
89 // need to be superfast.
90 return QString::fromUcs4(reinterpret_cast<uint const *>(ucs4.data()), ucs4.length());
93 QString const toqstr(docstring const & ucs4);
98 * ucs4_to_qstring - convert a UCS4 encoded char_type * into a QString
100 * This is a hack for the painter and font metrics and should not be used
101 * elsewhere. Since it uses ucs4_to_qchar it has the same limitations.
103 inline void ucs4_to_qstring(char_type const * str, size_t ls, QString & s)
105 int i = static_cast<int>(ls);
108 s[i] = ucs4_to_qchar(str[i]);
113 * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring
115 * This is the preferred method of converting anything that possibly
116 * contains non-ASCII stuff to docstring.
118 docstring const qstring_to_ucs4(QString const & qstr);
122 * fromqstr - convert a QString into a UTF8 encoded std::string
124 * This should not be used except for output to lyxerr, since all possibly
125 * non-ASCII stuff should be stored in a docstring.
127 std::string const fromqstr(QString const & str);
131 #endif // QSTRING_HELPERS_H