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"
22 #include "support/unicode.h"
24 #include <qcombobox.h>
25 #include <qlineedit.h>
26 #include <qtextcodec.h>
31 using lyx::support::isStrDbl;
40 string makeFontName(string const & family, string const & foundry)
44 return family + " [" + foundry + ']';
48 pair<string,string> parseFontName(string const & name)
50 string::size_type const idx = name.find('[');
51 if (idx == string::npos || idx == 0)
52 return make_pair(name, string());
53 return make_pair(name.substr(0, idx - 1),
54 name.substr(idx + 1, name.size() - idx - 2));
58 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
60 QString length = input->text();
64 // don't return unit-from-choice if the input(field) contains a unit
65 if (isValidGlueLength(fromqstr(length)))
66 return fromqstr(length);
68 LyXLength::UNIT unit = combo->currentLengthItem();
70 return LyXLength(length.toDouble(), unit).asString();
74 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
76 QString length = input->text();
80 // don't return unit-from-choice if the input(field) contains a unit
81 if (isValidGlueLength(fromqstr(length)))
82 return LyXLength(fromqstr(length));
84 LyXLength::UNIT unit = unitFromString(fromqstr(combo->currentText()));
86 return LyXLength(length.toDouble(), unit);
90 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
91 string const & len, LyXLength::UNIT defaultUnit)
94 // no length (UNIT_NONE)
95 combo->setCurrentItem(defaultUnit);
97 } else if (!isValidLength(len) && !isStrDbl(len)) {
98 // use input field only for gluelengths
99 combo->setCurrentItem(defaultUnit);
100 input->setText(toqstr(len));
102 combo->setCurrentItem(LyXLength(len).unit());
103 input->setText(toqstr(convert<string>(LyXLength(len).value())));
108 QString const toqstr(char const * str)
110 return QString::fromUtf8(str);
114 QString const toqstr(string const & str)
116 return toqstr(str.c_str());
120 QString const toqstr(docstring const & str)
122 std::vector<unsigned short> ucs2 =
123 ucs4_to_ucs2(str.c_str(), str.length());
124 ucs2.push_back('\0');
125 return QString::fromUcs2(&ucs2[0]);
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 docstring const qstring_to_ucs4(QString const & str)
151 unsigned short const * const ucs2 = str.ucs2();
152 std::vector<char_type> const ucs4 = ucs2_to_ucs4(
153 std::vector<unsigned short>(ucs2, ucs2 + str.length()));
154 return docstring(ucs4.begin(), ucs4.end());
158 string const formatted(string const & text, int w)
165 string::size_type curpos = 0;
169 string::size_type const nxtpos1 = text.find(' ', curpos);
170 string::size_type const nxtpos2 = text.find('\n', curpos);
171 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
173 string const word = nxtpos == string::npos ?
174 text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
176 bool const newline = (nxtpos2 != string::npos &&
179 string const line_plus_word =
180 line.empty() ? word : line + ' ' + word;
182 // FIXME: make w be size_t
183 if (int(line_plus_word.length()) >= w) {
192 } else if (newline) {
193 sout += line_plus_word + '\n';
202 if (nxtpos == string::npos) {