]> git.lyx.org Git - lyx.git/blob - src/support/qstring_helpers.h
* src/text2.C: deleteEmptyParagraphMechanism(): fix a crash in
[lyx.git] / src / support / qstring_helpers.h
1 // -*- C++ -*-
2 /**
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.
6  *
7  * \author Dekel Tsur
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef QSTRING_HELPERS_H
13 #define QSTRING_HELPERS_H
14
15 #include "support/docstring.h"
16
17 #include <QChar>
18 #include <QString>
19
20 #include <boost/assert.hpp>
21
22 namespace lyx {
23
24 /**
25  * toqstr - convert a UTF8 encoded char * into a QString
26  *
27  * This should not be used, since all possibly non-ASCII stuff should be
28  * stored in a docstring.
29  */
30 inline QString const toqstr(char const * str)
31 {
32         return QString::fromUtf8(str);
33 }
34
35
36 /**
37  * toqstr - convert a UTF8 encoded std::string into a QString
38  *
39  * This should not be used, since all possibly non-ASCII stuff should be
40  * stored in a docstring.
41  */
42 inline QString const toqstr(std::string const & str)
43 {
44         return toqstr(str.c_str());
45 }
46
47
48 /**
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).
54  */
55 inline char_type const qchar_to_ucs4(QChar const & qchar)
56 {
57         return static_cast<char_type>(qchar.unicode());
58 }
59
60
61 /**
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).
67  */
68 inline QChar const ucs4_to_qchar(char_type const ucs4)
69 {
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
73         // not supported.
74         BOOST_ASSERT(ucs4 < 65536);
75         return QChar(static_cast<unsigned short>(ucs4));
76 }
77
78
79 /**
80  * toqstr - convert a UCS4 encoded docstring into a QString
81  *
82  * This is the preferred method of converting anything that possibly
83  * contains non-ASCII stuff to QString.
84  */
85 #if QT_VERSION >= 0x040200
86 inline QString const toqstr(docstring const & ucs4)
87 {
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());
91 }
92 #else
93 QString const toqstr(docstring const & ucs4);
94 #endif
95
96
97 /**
98  * ucs4_to_qstring - convert a UCS4 encoded char_type * into a QString
99  *
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.
102  */
103 inline void ucs4_to_qstring(char_type const * str, size_t ls, QString & s)
104 {
105         int i = static_cast<int>(ls);
106         s.resize(i);
107         for (; --i >= 0;)
108                 s[i] = ucs4_to_qchar(str[i]);
109 }
110
111
112 /**
113  * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring
114  *
115  * This is the preferred method of converting anything that possibly
116  * contains non-ASCII stuff to docstring.
117  */
118 docstring const qstring_to_ucs4(QString const & qstr);
119
120
121 /**
122  * fromqstr - convert a QString into a UTF8 encoded std::string
123  *
124  * This should not be used except for output to lyxerr, since all possibly
125  * non-ASCII stuff should be stored in a docstring.
126  */
127 std::string const fromqstr(QString const & str);
128
129 } // namespace lyx
130
131 #endif // QSTRING_HELPERS_H