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)
42 return family + " [" + foundry + ']';
44 return foundry + '-' + family;
49 pair<string,string> parseFontName(string const & name)
52 string::size_type const idx = name.find('[');
53 if (idx == string::npos || idx == 0)
54 return make_pair(name, string());
55 return make_pair(name.substr(0, idx - 1),
56 name.substr(idx + 1, name.size() - idx - 2));
58 string::size_type const idx = name.find('-');
59 if (idx == string::npos || idx == 0)
60 return make_pair(name, string());
61 return make_pair(name.substr(idx + 1),
67 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
69 QString length = input->text();
73 // don't return unit-from-choice if the input(field) contains a unit
74 if (isValidGlueLength(fromqstr(length)))
75 return fromqstr(length);
77 LyXLength::UNIT unit = combo->currentLengthItem();
79 return LyXLength(length.toDouble(), unit).asString();
83 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
85 QString length = input->text();
89 // don't return unit-from-choice if the input(field) contains a unit
90 if (isValidGlueLength(fromqstr(length)))
91 return LyXLength(fromqstr(length));
93 LyXLength::UNIT unit = unitFromString(fromqstr(combo->currentText()));
95 return LyXLength(length.toDouble(), unit);
99 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
100 string const & len, LyXLength::UNIT defaultUnit)
103 // no length (UNIT_NONE)
104 combo->setCurrentItem(defaultUnit);
106 } else if (!isValidLength(len) && !isStrDbl(len)) {
107 // use input field only for gluelengths
108 combo->setCurrentItem(defaultUnit);
109 input->setText(toqstr(len));
111 combo->setCurrentItem(LyXLength(len).unit());
112 input->setText(toqstr(convert<string>(LyXLength(len).value())));
117 QString const toqstr(char const * str)
119 return QString::fromAscii(str);
123 QString const toqstr(string const & str)
125 return toqstr(str.c_str());
129 QString const qt_(char const * str)
131 return toqstr(_(str));
135 QString const qt_(string const & str)
137 return toqstr(_(str));
141 string const fromqstr(QString const & str)
145 return str.ascii() ? str.ascii() : "";
149 string const formatted(string const & text, int w)
156 string::size_type curpos = 0;
160 string::size_type const nxtpos1 = text.find(' ', curpos);
161 string::size_type const nxtpos2 = text.find('\n', curpos);
162 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
164 string const word = nxtpos == string::npos ?
165 text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
167 bool const newline = (nxtpos2 != string::npos &&
170 string const line_plus_word =
171 line.empty() ? word : line + ' ' + word;
173 // FIXME: make w be size_t
174 if (int(line_plus_word.length()) >= w) {
183 } else if (newline) {
184 sout += line_plus_word + '\n';
193 if (nxtpos == string::npos) {