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"
24 #include <qlineedit.h>
25 #include <qtextcodec.h>
30 using lyx::support::isStrDbl;
37 string makeFontName(string const & family, string const & foundry)
41 return family + " [" + foundry + ']';
45 pair<string,string> parseFontName(string const & name)
47 string::size_type const idx = name.find('[');
48 if (idx == string::npos || idx == 0)
49 return make_pair(name, string());
50 return make_pair(name.substr(0, idx - 1),
51 name.substr(idx + 1, name.size() - idx - 2));
55 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
57 QString length = input->text();
61 // don't return unit-from-choice if the input(field) contains a unit
62 if (isValidGlueLength(fromqstr(length)))
63 return fromqstr(length);
65 LyXLength::UNIT unit = combo->currentLengthItem();
67 return LyXLength(length.toDouble(), unit).asString();
71 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
73 QString length = input->text();
77 // don't return unit-from-choice if the input(field) contains a unit
78 if (isValidGlueLength(fromqstr(length)))
79 return LyXLength(fromqstr(length));
81 LyXLength::UNIT unit = unitFromString(fromqstr(combo->currentText()));
83 return LyXLength(length.toDouble(), unit);
87 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
88 string const & len, LyXLength::UNIT defaultUnit)
91 // no length (UNIT_NONE)
92 combo->setCurrentItem(defaultUnit);
94 } else if (!isValidLength(len) && !isStrDbl(len)) {
95 // use input field only for gluelengths
96 combo->setCurrentItem(defaultUnit);
97 input->setText(toqstr(len));
99 combo->setCurrentItem(LyXLength(len).unit());
100 input->setText(toqstr(convert<string>(LyXLength(len).value())));
105 QString const toqstr(char const * str)
107 return QString::fromAscii(str);
111 QString const toqstr(string const & str)
113 return toqstr(str.c_str());
117 QString const qt_(char const * str)
119 return toqstr(_(str));
123 QString const qt_(string const & str)
125 return toqstr(_(str));
129 string const fromqstr(QString const & str)
133 return str.ascii() ? str.ascii() : "";
137 string const formatted(string const & text, int w)
144 string::size_type curpos = 0;
148 string::size_type const nxtpos1 = text.find(' ', curpos);
149 string::size_type const nxtpos2 = text.find('\n', curpos);
150 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
152 string const word = nxtpos == string::npos ?
153 text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
155 bool const newline = (nxtpos2 != string::npos &&
158 string const line_plus_word =
159 line.empty() ? word : line + ' ' + word;
161 // FIXME: make w be size_t
162 if (int(line_plus_word.length()) >= w) {
171 } else if (newline) {
172 sout += line_plus_word + '\n';
181 if (nxtpos == string::npos) {