]> git.lyx.org Git - lyx.git/blob - src/support/qstring_helpers.cpp
Rename .C ==> .cpp for files in src/support, part two
[lyx.git] / src / support / qstring_helpers.cpp
1 /**
2  * \file qstring_helpers.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "qstring_helpers.h"
15
16 #include <QVector>
17
18
19 namespace lyx {
20
21 using std::string;
22
23 #if QT_VERSION < 0x040200
24 // We use QString::fromUcs4 in Qt 4.2 and higher
25 QString const toqstr(docstring const & str)
26 {
27         // This does not properly convert surrogate pairs
28         QString s;
29         int i = static_cast<int>(str.size()); 
30         s.resize(i);
31         for (; --i >= 0;)
32                 s[i] = ucs4_to_qchar(str[i]);
33         return s;
34 }
35 #endif
36
37
38 docstring const qstring_to_ucs4(QString const & qstr)
39 {
40 #if QT_VERSION >= 0x040200
41         QVector<uint> const ucs4 = qstr.toUcs4();
42         return docstring(ucs4.begin(), ucs4.end());
43 #else
44         // This does not properly convert surrogate pairs
45         int const ls = qstr.size();
46         docstring ucs4;
47         for (int i = 0; i < ls; ++i)
48                 ucs4 += qchar_to_ucs4(qstr[i].unicode());
49         return ucs4;
50 #endif
51 }
52
53
54 string const fromqstr(QString const & str)
55 {
56         return str.isEmpty() ? string() : string(str.toUtf8());
57 }
58
59 } // namespace lyx