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/os.h"
21 #include "support/lstrings.h"
22 #include "support/convert.h"
27 #include <qlineedit.h>
28 #include <qtextcodec.h>
35 using support::isStrDbl;
44 string makeFontName(string const & family, string const & foundry)
48 return family + " [" + foundry + ']';
52 pair<string, string> parseFontName(string const & name)
54 string::size_type const idx = name.find('[');
55 if (idx == string::npos || idx == 0)
56 return make_pair(name, string());
57 return make_pair(name.substr(0, idx - 1),
58 name.substr(idx + 1, name.size() - idx - 2));
62 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
64 QString const length = input->text();
68 // Don't return unit-from-choice if the input(field) contains a unit
69 if (isValidGlueLength(fromqstr(length)))
70 return fromqstr(length);
72 LyXLength::UNIT const unit = combo->currentLengthItem();
74 return LyXLength(length.toDouble(), unit).asString();
78 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
80 QString const length = input->text();
84 // don't return unit-from-choice if the input(field) contains a unit
85 if (isValidGlueLength(fromqstr(length)))
86 return LyXLength(fromqstr(length));
88 LyXLength::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
90 return LyXLength(length.toDouble(), unit);
94 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
95 string const & len, LyXLength::UNIT defaultUnit)
98 // no length (UNIT_NONE)
99 combo->setCurrentItem(defaultUnit);
101 } else if (!isValidLength(len) && !isStrDbl(len)) {
102 // use input field only for gluelengths
103 combo->setCurrentItem(defaultUnit);
104 input->setText(toqstr(len));
106 combo->setCurrentItem(LyXLength(len).unit());
107 input->setText(toqstr(convert<string>(LyXLength(len).value())));
112 QString const qt_(char const * str, const char *)
114 return toqstr(_(str));
118 QString const qt_(string const & str)
120 return toqstr(_(str));
124 docstring const formatted(docstring const & text, int w)
131 docstring::size_type curpos = 0;
135 docstring::size_type const nxtpos1 = text.find(' ', curpos);
136 docstring::size_type const nxtpos2 = text.find('\n', curpos);
137 docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
139 docstring const word =
140 nxtpos == docstring::npos ?
141 text.substr(curpos) :
142 text.substr(curpos, nxtpos - curpos);
144 bool const newline = (nxtpos2 != docstring::npos &&
147 docstring const line_plus_word =
148 line.empty() ? word : line + lyx::char_type(' ') + word;
150 // FIXME: make w be size_t
151 if (int(line_plus_word.length()) >= w) {
152 sout += line + lyx::char_type('\n');
154 sout += word + lyx::char_type('\n');
160 } else if (newline) {
161 sout += line_plus_word + lyx::char_type('\n');
166 line += lyx::char_type(' ');
170 if (nxtpos == docstring::npos) {