]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.h
Fix incorrect conversion from QString to std::string. lyxerr output is wrong
[lyx.git] / src / frontends / qt4 / qt_helpers.h
1 // -*- C++ -*-
2 /**
3  * \file qt_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 QTHELPERS_H
13 #define QTHELPERS_H
14
15 #include "lyxlength.h"
16 #include "support/docstring.h"
17
18 #include <QChar>
19 #include <QString>
20
21 #include <vector>
22 #include <utility>
23 #include <boost/assert.hpp>
24
25 class QComboBox;
26 class QLineEdit;
27
28 class LengthCombo;
29
30 namespace lyx {
31
32 std::string makeFontName(std::string const & family, std::string const & foundry);
33
34 std::pair<std::string,std::string> parseFontName(std::string const & name);
35
36 /// method to get a LyXLength from widgets (LengthCombo)
37 std::string widgetsToLength(QLineEdit const * input, LengthCombo const * combo);
38 /// method to get a LyXLength from widgets (QComboBox)
39 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo);
40
41 /// method to set widgets from a LyXLength
42 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
43         std::string const & len, LyXLength::UNIT default_unit);
44
45 /// format a string to the given width
46 docstring const formatted(docstring const & text, int w = 80);
47
48
49 /**
50  * toqstr - convert a UTF8 encoded char * into a QString
51  *
52  * This should not be used, since all possibly non-ASCII stuff should be
53  * stored in a docstring.
54  */
55 inline QString const toqstr(char const * str)
56 {
57         return QString::fromUtf8(str);
58 }
59
60
61 /**
62  * toqstr - convert a UTF8 encoded std::string into a QString
63  *
64  * This should not be used, since all possibly non-ASCII stuff should be
65  * stored in a docstring.
66  */
67 inline QString const toqstr(std::string const & str)
68 {
69         return toqstr(str.c_str());
70 }
71
72
73 /**
74  * Convert a QChar into a UCS4 character.
75  * This is a hack (it does only make sense for the common part of the UCS4
76  * and UTF16 encodings) and should not be used.
77  * This does only exist because of performance reasons (a real conversion
78  * using iconv is too slow on windows).
79  */
80 inline char_type const qchar_to_ucs4(QChar const & qchar)
81 {
82         return static_cast<char_type>(qchar.unicode());
83 }
84
85
86 /**
87  * Convert a UCS4 character into a QChar.
88  * This is a hack (it does only make sense for the common part of the UCS4
89  * and UTF16 encodings) and should not be used.
90  * This does only exist because of performance reasons (a real conversion
91  * using iconv is too slow on windows).
92  */
93 inline QChar const ucs4_to_qchar(char_type const ucs4)
94 {
95         // FIXME: The following cast is not a real conversion but it work
96         // for the ucs2 subrange of unicode. Instead of an assertion we should
97         // return some special characters that indicates that its display is
98         // not supported.
99         BOOST_ASSERT(ucs4 < 65536);
100         return QChar(static_cast<unsigned short>(ucs4));
101 }
102
103
104 /**
105  * toqstr - convert a UCS4 encoded docstring into a QString
106  *
107  * This is the preferred method of converting anything that possibly
108  * contains non-ASCII stuff to QString.
109  */
110 #if QT_VERSION >= 0x040200
111 inline QString const toqstr(docstring const & ucs4)
112 {
113         // If possible we let qt do the work, since this version does not
114         // need to be superfast.
115         return QString::fromUcs4(reinterpret_cast<uint const *>(ucs4.data()), ucs4.length());
116 }
117 #else
118 QString const toqstr(docstring const & ucs4);
119 #endif
120
121
122 /**
123  * ucs4_to_qstring - convert a UCS4 encoded char_type * into a QString
124  *
125  * This is a hack for the painter and font metrics and should not be used
126  * elsewhere. Since it uses ucs4_to_qchar it has the same limitations.
127  */
128 inline void ucs4_to_qstring(char_type const * str, size_t ls, QString & s)
129 {
130         int i = static_cast<int>(ls);
131         s.resize(i);
132         for (; --i >= 0;)
133                 s[i] = ucs4_to_qchar(str[i]);
134 }
135
136
137 /**
138  * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring
139  *
140  * This is the preferred method of converting anything that possibly
141  * contains non-ASCII stuff to docstring.
142  */
143 docstring const qstring_to_ucs4(QString const & qstr);
144
145
146 /**
147  * qt_ - i18nize string and convert to QString
148  *
149  * Use this in qt4/ instead of _()
150  */
151 QString const qt_(char const * str, const char * comment = 0);
152
153
154 /**
155  * qt_ - i18nize string and convert to QString
156  *
157  * Use this in qt4/ instead of _()
158  */
159 QString const qt_(std::string const & str);
160
161
162 /**
163  * fromqstr - convert a QString into a UTF8 encoded std::string
164  *
165  * This should not be used except for output to lyxerr, since all possibly
166  * non-ASCII stuff should be stored in a docstring.
167  */
168 std::string const fromqstr(QString const & str);
169
170 } // namespace lyx
171
172 #endif // QTHELPERS_H