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