]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.C
* qt_helpers.[Ch]: new conversion from/to ucs4 strings and chars to/from QChar and...
[lyx.git] / src / frontends / qt4 / qt_helpers.C
1 /**
2  * \file qt_helpers.C
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 "lengthcombo.h"
15 #include "qt_helpers.h"
16
17 #include "lengthcommon.h"
18 #include "gettext.h"
19
20 #include "support/lstrings.h"
21 #include "support/convert.h"
22
23 #include "debug.h"
24
25 #include <QComboBox>
26 #include <qlineedit.h>
27 #include <qtextcodec.h>
28
29 #include <algorithm>
30
31
32 using lyx::support::isStrDbl;
33 using lyx::char_type;
34
35 using std::vector;
36 using std::make_pair;
37 using std::string;
38 using std::pair;
39 using std::endl;
40
41
42 string makeFontName(string const & family, string const & foundry)
43 {
44         if (foundry.empty())
45                 return family;
46         return family + " [" + foundry + ']';
47 }
48
49
50 pair<string,string> parseFontName(string const & name)
51 {
52         string::size_type const idx = name.find('[');
53         if (idx == string::npos || idx == 0)
54                 return make_pair(name, string());
55         return make_pair(name.substr(0, idx - 1),
56                          name.substr(idx + 1, name.size() - idx - 2));
57 }
58
59
60 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
61 {
62         QString length = input->text();
63         if (length.isEmpty())
64                 return string();
65
66         // don't return unit-from-choice if the input(field) contains a unit
67         if (isValidGlueLength(fromqstr(length)))
68                 return fromqstr(length);
69
70         LyXLength::UNIT unit = combo->currentLengthItem();
71
72         return LyXLength(length.toDouble(), unit).asString();
73 }
74
75
76 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
77 {
78         QString length = input->text();
79         if (length.isEmpty())
80                 return LyXLength();
81
82         // don't return unit-from-choice if the input(field) contains a unit
83         if (isValidGlueLength(fromqstr(length)))
84                 return LyXLength(fromqstr(length));
85
86         LyXLength::UNIT unit = unitFromString(fromqstr(combo->currentText()));
87
88         return LyXLength(length.toDouble(), unit);
89 }
90
91
92 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
93         string const & len, LyXLength::UNIT defaultUnit)
94 {
95         if (len.empty()) {
96                 // no length (UNIT_NONE)
97                 combo->setCurrentItem(defaultUnit);
98                 input->setText("");
99         } else if (!isValidLength(len) && !isStrDbl(len)) {
100                 // use input field only for gluelengths
101                 combo->setCurrentItem(defaultUnit);
102                 input->setText(toqstr(len));
103         } else {
104                 combo->setCurrentItem(LyXLength(len).unit());
105                 input->setText(toqstr(convert<string>(LyXLength(len).value())));
106         }
107 }
108
109
110 QString const toqstr(char const * str)
111 {
112         return QString::fromAscii(str);
113 }
114
115
116 QString const toqstr(string const & str)
117 {
118         return toqstr(str.c_str());
119 }
120
121
122 QString const ucs4_to_qstring(char_type const * str, size_t ls)
123 {
124         QString s;
125
126         for (size_t i = 0; i < ls; ++i)
127                 s.append(ucs4_to_qchar(str[i]));
128
129         return s;
130 }
131
132
133 QString const ucs4_to_qstring(vector<char_type> const & ucs4)
134 {
135         QString s;
136         size_t const ls = ucs4.size();
137
138         for (size_t i = 0; i < ls; ++i)
139                 s.append(ucs4_to_qchar(ucs4[i]));
140
141         return s;
142 }
143
144
145 vector<char_type> qstring_to_ucs4(QString const & qstr)
146 {
147         size_t ls = qstr.size();
148         vector<char_type> ucs4;
149         for (size_t i = 0; i < ls; ++i)
150                 ucs4.push_back(static_cast<boost::uint32_t>(qstr[i].unicode()));
151
152         return ucs4;
153 }
154
155
156 void qstring_to_ucs4(QString const & qstr, vector<char_type> & ucs4)
157 {
158         size_t ls = qstr.size();
159         ucs4.clear();
160         for (size_t i = 0; i < ls; ++i)
161                 ucs4.push_back(static_cast<boost::uint32_t>(qstr[i].unicode()));
162 }
163
164
165 char_type const qchar_to_ucs4(QChar const & qchar)
166 {
167         return static_cast<boost::uint32_t>(qchar.unicode());
168 }
169
170
171 QChar const ucs4_to_qchar(char_type const & ucs4)
172 {
173         return QChar(static_cast<unsigned short>(ucs4));
174 }
175
176
177 QString const qt_(char const * str)
178 {
179         return toqstr(_(str));
180 }
181
182
183 QString const qt_(string const & str)
184 {
185         return toqstr(_(str));
186 }
187
188
189 string const fromqstr(QString const & str)
190 {
191         return str.isEmpty()? string(): string(str.toAscii());
192 }
193
194
195 string const formatted(string const & text, int w)
196 {
197         string sout;
198
199         if (text.empty())
200                 return sout;
201
202         string::size_type curpos = 0;
203         string line;
204
205         for (;;) {
206                 string::size_type const nxtpos1 = text.find(' ',  curpos);
207                 string::size_type const nxtpos2 = text.find('\n', curpos);
208                 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
209
210                 string const word = nxtpos == string::npos ?
211                         text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
212
213                 bool const newline = (nxtpos2 != string::npos &&
214                                       nxtpos2 < nxtpos1);
215
216                 string const line_plus_word =
217                         line.empty() ? word : line + ' ' + word;
218
219                 // FIXME: make w be size_t
220                 if (int(line_plus_word.length()) >= w) {
221                         sout += line + '\n';
222                         if (newline) {
223                                 sout += word + '\n';
224                                 line.erase();
225                         } else {
226                                 line = word;
227                         }
228
229                 } else if (newline) {
230                         sout += line_plus_word + '\n';
231                         line.erase();
232
233                 } else {
234                         if (!line.empty())
235                                 line += ' ';
236                         line += word;
237                 }
238
239                 if (nxtpos == string::npos) {
240                         if (!line.empty())
241                                 sout += line;
242                         break;
243                 }
244
245                 curpos = nxtpos + 1;
246         }
247
248         return sout;
249 }