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