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>
35 using lyx::support::isStrDbl;
46 string makeFontName(string const & family, string const & foundry)
50 return family + " [" + foundry + ']';
54 pair<string, string> parseFontName(string const & name)
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));
64 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
66 QString const length = input->text();
70 // Don't return unit-from-choice if the input(field) contains a unit
71 if (isValidGlueLength(fromqstr(length)))
72 return fromqstr(length);
74 LyXLength::UNIT const unit = combo->currentLengthItem();
76 return LyXLength(length.toDouble(), unit).asString();
80 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
82 QString const length = input->text();
86 // don't return unit-from-choice if the input(field) contains a unit
87 if (isValidGlueLength(fromqstr(length)))
88 return LyXLength(fromqstr(length));
90 LyXLength::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
92 return LyXLength(length.toDouble(), unit);
96 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
97 string const & len, LyXLength::UNIT defaultUnit)
100 // no length (UNIT_NONE)
101 combo->setCurrentItem(defaultUnit);
103 } else if (!isValidLength(len) && !isStrDbl(len)) {
104 // use input field only for gluelengths
105 combo->setCurrentItem(defaultUnit);
106 input->setText(toqstr(len));
108 combo->setCurrentItem(LyXLength(len).unit());
109 input->setText(toqstr(convert<string>(LyXLength(len).value())));
114 QString const toqstr(char const * str)
116 return QString::fromUtf8(str);
120 QString const toqstr(string const & str)
122 return toqstr(str.c_str());
126 void ucs4_to_qstring(char_type const * str, size_t ls, QString & s)
131 for (size_t i = 0; i < ls; ++i)
132 s.append(ucs4_to_qchar(str[i]));
136 void ucs4_to_qstring(lyx::docstring const & str, QString & s)
138 size_t ls = str.size();
142 for (size_t i = 0; i < ls; ++i)
143 s.append(ucs4_to_qchar(str[i]));
147 QString ucs4_to_qstring(lyx::docstring const & str)
150 ucs4_to_qstring(str, tmp);
155 QString const toqstr(docstring const & ucs4)
158 ucs4_to_qstring(ucs4, s);
164 docstring const qstring_to_ucs4(QString const & qstr)
166 int const ls = qstr.size();
168 for (int i = 0; i < ls; ++i)
169 ucs4 += static_cast<char_type>(qstr[i].unicode());
175 void qstring_to_ucs4(QString const & qstr, vector<char_type> & ucs4)
177 int const ls = qstr.size();
179 for (int i = 0; i < ls; ++i)
180 ucs4.push_back(static_cast<lyx::char_type>(qstr[i].unicode()));
184 QString const qt_(char const * str)
186 return toqstr(_(str));
190 QString const qt_(string const & str)
192 return toqstr(_(str));
196 string const fromqstr(QString const & str)
198 return str.isEmpty()? string(): string(str.toAscii());
202 docstring const formatted(docstring const & text, int w)
209 docstring::size_type curpos = 0;
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);
217 docstring const word =
218 nxtpos == docstring::npos ?
219 text.substr(curpos) :
220 text.substr(curpos, nxtpos - curpos);
222 bool const newline = (nxtpos2 != docstring::npos &&
225 docstring const line_plus_word =
226 line.empty() ? word : line + lyx::char_type(' ') + word;
228 // FIXME: make w be size_t
229 if (int(line_plus_word.length()) >= w) {
230 sout += line + lyx::char_type('\n');
232 sout += word + lyx::char_type('\n');
238 } else if (newline) {
239 sout += line_plus_word + lyx::char_type('\n');
244 line += lyx::char_type(' ');
248 if (nxtpos == docstring::npos) {