3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "lengthcombo.h"
15 #include "qt_helpers.h"
17 #include "lengthcommon.h"
20 #include "support/lstrings.h"
21 #include "support/convert.h"
26 #include <qlineedit.h>
27 #include <qtextcodec.h>
32 using lyx::support::isStrDbl;
43 string makeFontName(string const & family, string const & foundry)
47 return family + " [" + foundry + ']';
51 pair<string, string> parseFontName(string const & name)
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));
61 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
63 QString const length = input->text();
67 // Don't return unit-from-choice if the input(field) contains a unit
68 if (isValidGlueLength(fromqstr(length)))
69 return fromqstr(length);
71 LyXLength::UNIT const unit = combo->currentLengthItem();
73 return LyXLength(length.toDouble(), unit).asString();
77 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
79 QString const length = input->text();
83 // don't return unit-from-choice if the input(field) contains a unit
84 if (isValidGlueLength(fromqstr(length)))
85 return LyXLength(fromqstr(length));
87 LyXLength::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
89 return LyXLength(length.toDouble(), unit);
93 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
94 string const & len, LyXLength::UNIT defaultUnit)
97 // no length (UNIT_NONE)
98 combo->setCurrentItem(defaultUnit);
100 } else if (!isValidLength(len) && !isStrDbl(len)) {
101 // use input field only for gluelengths
102 combo->setCurrentItem(defaultUnit);
103 input->setText(toqstr(len));
105 combo->setCurrentItem(LyXLength(len).unit());
106 input->setText(toqstr(convert<string>(LyXLength(len).value())));
111 QString const toqstr(char const * str)
113 return QString::fromUtf8(str);
117 QString const toqstr(string const & str)
119 return toqstr(str.c_str());
123 void ucs4_to_qstring(char_type const * str, size_t ls, QString & s)
127 for (size_t i = 0; i < ls; ++i)
128 s.append(ucs4_to_qchar(str[i]));
132 QString const toqstr(docstring const & ucs4)
135 size_t const ls = ucs4.size();
137 for (size_t i = 0; i < ls; ++i)
138 s.append(ucs4_to_qchar(ucs4[i]));
144 docstring const qstring_to_ucs4(QString const & qstr)
146 int const ls = qstr.size();
148 for (int i = 0; i < ls; ++i)
149 ucs4 += static_cast<char_type>(qstr[i].unicode());
155 void qstring_to_ucs4(QString const & qstr, vector<char_type> & ucs4)
157 int const ls = qstr.size();
159 for (int i = 0; i < ls; ++i)
160 ucs4.push_back(static_cast<lyx::char_type>(qstr[i].unicode()));
164 QString const qt_(char const * str)
166 return toqstr(_(str));
170 QString const qt_(string const & str)
172 return toqstr(_(str));
176 string const fromqstr(QString const & str)
178 return str.isEmpty()? string(): string(str.toAscii());
182 docstring const formatted(docstring const & text, int w)
189 docstring::size_type curpos = 0;
193 docstring::size_type const nxtpos1 = text.find(' ', curpos);
194 docstring::size_type const nxtpos2 = text.find('\n', curpos);
195 docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
197 docstring const word =
198 nxtpos == docstring::npos ?
199 text.substr(curpos) :
200 text.substr(curpos, nxtpos - curpos);
202 bool const newline = (nxtpos2 != docstring::npos &&
205 docstring const line_plus_word =
206 line.empty() ? word : line + lyx::char_type(' ') + word;
208 // FIXME: make w be size_t
209 if (int(line_plus_word.length()) >= w) {
210 sout += line + lyx::char_type('\n');
212 sout += word + lyx::char_type('\n');
218 } else if (newline) {
219 sout += line_plus_word + lyx::char_type('\n');
224 line += lyx::char_type(' ');
228 if (nxtpos == docstring::npos) {