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>
32 using lyx::support::isStrDbl;
42 string makeFontName(string const & family, string const & foundry)
46 return family + " [" + foundry + ']';
50 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));
60 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
62 QString length = input->text();
66 // don't return unit-from-choice if the input(field) contains a unit
67 if (isValidGlueLength(fromqstr(length)))
68 return fromqstr(length);
70 LyXLength::UNIT unit = combo->currentLengthItem();
72 return LyXLength(length.toDouble(), unit).asString();
76 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
78 QString length = input->text();
82 // don't return unit-from-choice if the input(field) contains a unit
83 if (isValidGlueLength(fromqstr(length)))
84 return LyXLength(fromqstr(length));
86 LyXLength::UNIT unit = unitFromString(fromqstr(combo->currentText()));
88 return LyXLength(length.toDouble(), unit);
92 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
93 string const & len, LyXLength::UNIT defaultUnit)
96 // no length (UNIT_NONE)
97 combo->setCurrentItem(defaultUnit);
99 } else if (!isValidLength(len) && !isStrDbl(len)) {
100 // use input field only for gluelengths
101 combo->setCurrentItem(defaultUnit);
102 input->setText(toqstr(len));
104 combo->setCurrentItem(LyXLength(len).unit());
105 input->setText(toqstr(convert<string>(LyXLength(len).value())));
110 QString const toqstr(char const * str)
112 return QString::fromAscii(str);
116 QString const toqstr(string const & str)
118 return toqstr(str.c_str());
122 QString const ucs4_to_qstring(char_type const * str, size_t ls)
126 for (size_t i = 0; i < ls; ++i)
127 s.append(ucs4_to_qchar(str[i]));
133 QString const ucs4_to_qstring(vector<char_type> const & ucs4)
136 size_t const ls = ucs4.size();
138 for (size_t i = 0; i < ls; ++i)
139 s.append(ucs4_to_qchar(ucs4[i]));
145 vector<char_type> qstring_to_ucs4(QString const & qstr)
147 int ls = qstr.size();
148 vector<char_type> ucs4;
149 for (int i = 0; i < ls; ++i)
150 ucs4.push_back(static_cast<boost::uint32_t>(qstr[i].unicode()));
156 void qstring_to_ucs4(QString const & qstr, vector<char_type> & ucs4)
158 int ls = qstr.size();
160 for (int i = 0; i < ls; ++i)
161 ucs4.push_back(static_cast<boost::uint32_t>(qstr[i].unicode()));
165 char_type const qchar_to_ucs4(QChar const & qchar)
167 return static_cast<boost::uint32_t>(qchar.unicode());
171 QChar const ucs4_to_qchar(char_type const & ucs4)
173 return QChar(static_cast<unsigned short>(ucs4));
177 QString const qt_(char const * str)
179 return toqstr(_(str));
183 QString const qt_(string const & str)
185 return toqstr(_(str));
189 string const fromqstr(QString const & str)
191 return str.isEmpty()? string(): string(str.toAscii());
195 string const formatted(string const & text, int w)
202 string::size_type curpos = 0;
206 string::size_type const nxtpos1 = text.find(' ', curpos);
207 string::size_type const nxtpos2 = text.find('\n', curpos);
208 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
210 string const word = nxtpos == string::npos ?
211 text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
213 bool const newline = (nxtpos2 != string::npos &&
216 string const line_plus_word =
217 line.empty() ? word : line + ' ' + word;
219 // FIXME: make w be size_t
220 if (int(line_plus_word.length()) >= w) {
229 } else if (newline) {
230 sout += line_plus_word + '\n';
239 if (nxtpos == string::npos) {