]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.h
* qt_helpers.h:
[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
24 class QComboBox;
25 class QLineEdit;
26
27 class LengthCombo;
28
29 namespace lyx {
30
31 std::string makeFontName(std::string const & family, std::string const & foundry);
32
33 std::pair<std::string,std::string> parseFontName(std::string const & name);
34
35 /// method to get a LyXLength from widgets (LengthCombo)
36 std::string widgetsToLength(QLineEdit const * input, LengthCombo const * combo);
37 /// method to get a LyXLength from widgets (QComboBox)
38 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo);
39
40 /// method to set widgets from a LyXLength
41 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
42         std::string const & len, LyXLength::UNIT default_unit);
43
44 /// format a string to the given width
45 docstring const formatted(docstring const & text, int w = 80);
46
47 /**
48  * toqstr - convert char * into Qt's unicode (UTF16)
49  *
50  * Use this whenever there's a user-visible string that is encoded
51  * for the locale (menus, dialogs etc.)
52  */
53 inline QString const toqstr(char const * str)
54 {
55         return QString::fromUtf8(str);
56 }
57
58
59 /**
60  * toqstr - convert string into unicode
61  *
62  * Use this whenever there's a user-visible string that is encoded
63  * for the locale (menus, dialogs etc.)
64  */
65 inline QString const toqstr(std::string const & str)
66 {
67         return toqstr(str.c_str());
68 }
69
70
71 /**
72  * toqstr - convert ucs4 into QString
73  *
74  * QString uses utf16 internally.
75  */
76 inline char_type const qchar_to_ucs4(QChar const & qchar) {
77         return static_cast<char_type>(qchar.unicode());
78 }
79
80 inline QChar const ucs4_to_qchar(char_type const ucs4) {
81         // FIXME: The following cast is not a real conversion but it work
82         // for the ucs2 subrange of unicode. Instead of an assertion we should
83         // return some special characters that indicates that its display is
84         // not supported.
85         BOOST_ASSERT(ucs4 < 65536);
86         return QChar(static_cast<unsigned short>(ucs4));
87 }
88
89 QString const toqstr(docstring const & ucs4);
90
91 void ucs4_to_qstring(docstring const & str, QString & s);
92
93 inline void ucs4_to_qstring(char_type const * str, size_t ls, QString & s)
94 {
95         int i = static_cast<int>(ls);
96         s.resize(i);
97         for (i ; --i >= 0; )
98                 s[i] = ucs4_to_qchar(str[i]);
99 }
100
101
102 QString ucs4_to_qstring(docstring const & str);
103
104 docstring const qstring_to_ucs4(QString const & qstr);
105
106 void qstring_to_ucs4(QString const & qstr, std::vector<char_type> & ucs4);
107
108 /**
109  * qt_ - i18nize string and convert to unicode
110  *
111  * Use this in qt4/ instead of qt_()
112  */
113 QString const qt_(char const * str, const char * comment);
114
115
116 /**
117  * qt_ - i18nize string and convert to unicode
118  *
119  * Use this in qt4/ instead of qt_()
120  */
121 QString const qt_(std::string const & str);
122
123
124 /**
125  * fromqstr - convert QString into std::string in locale
126  *
127  * Return the QString encoded in the locale
128  */
129 std::string const fromqstr(QString const & str);
130
131
132 } // namespace lyx
133
134 #endif // QTHELPERS_H