]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.h
Make string conversion work with non-ucs2-characters if using qt 4.2
[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  * toqstr - convert char * into Qt's unicode (UTF16)
50  *
51  * Use this whenever there's a user-visible string that is encoded
52  * for the locale (menus, dialogs etc.)
53  */
54 inline QString const toqstr(char const * str)
55 {
56         return QString::fromUtf8(str);
57 }
58
59
60 /**
61  * toqstr - convert string into unicode
62  *
63  * Use this whenever there's a user-visible string that is encoded
64  * for the locale (menus, dialogs etc.)
65  */
66 inline QString const toqstr(std::string const & str)
67 {
68         return toqstr(str.c_str());
69 }
70
71
72 /**
73  * toqstr - convert ucs4 into QString
74  *
75  * QString uses utf16 internally.
76  */
77 inline char_type const qchar_to_ucs4(QChar const & qchar) {
78         return static_cast<char_type>(qchar.unicode());
79 }
80
81 inline QChar const ucs4_to_qchar(char_type const ucs4) {
82         // FIXME: The following cast is not a real conversion but it work
83         // for the ucs2 subrange of unicode. Instead of an assertion we should
84         // return some special characters that indicates that its display is
85         // not supported.
86         BOOST_ASSERT(ucs4 < 65536);
87         return QChar(static_cast<unsigned short>(ucs4));
88 }
89
90
91 #if QT_VERSION >= 0x040200
92 inline QString const toqstr(docstring const & ucs4)
93 {
94         // If possible we let qt do the work, since this version does not
95         // need to be superfast.
96         return QString::fromUcs4(reinterpret_cast<uint const *>(ucs4.data()), ucs4.length());
97 }
98 #else
99 QString const toqstr(docstring const & ucs4);
100 #endif
101
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 docstring const qstring_to_ucs4(QString const & qstr);
113
114
115 /**
116  * qt_ - i18nize string and convert to unicode
117  *
118  * Use this in qt4/ instead of _()
119  */
120 QString const qt_(char const * str, const char * comment = 0);
121
122
123 /**
124  * qt_ - i18nize string and convert to unicode
125  *
126  * Use this in qt4/ instead of _()
127  */
128 QString const qt_(std::string const & str);
129
130
131 /**
132  * fromqstr - convert QString into std::string in locale
133  *
134  * Return the QString encoded in the locale
135  */
136 std::string const fromqstr(QString const & str);
137
138
139 } // namespace lyx
140
141 #endif // QTHELPERS_H