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 "support/tostr.h"
15 #include "support/lstrings.h"
17 #include "qt_helpers.h"
19 #include "lengthcombo.h"
21 #include <qlineedit.h>
22 #include <qtextcodec.h>
27 using lyx::support::isStrDbl;
34 string makeFontName(string const & family, string const & foundry)
39 return family + " [" + foundry + ']';
41 return foundry + '-' + family;
46 pair<string,string> parseFontName(string const & name)
49 string::size_type const idx = name.find('[');
50 if (idx == string::npos || idx == 0)
51 return make_pair(name, string());
52 return make_pair(name.substr(0, idx - 1),
53 name.substr(idx + 1, name.size() - idx - 2));
55 string::size_type const idx = name.find('-');
56 if (idx == string::npos || idx == 0)
57 return make_pair(name, string());
58 return make_pair(name.substr(idx + 1),
64 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
66 QString length = input->text();
70 // don't return unit-from-choice if the input(field) contains a unit
71 if (isValidGlueLength(fromqstr(length)))
72 return fromqstr(length);
74 LyXLength::UNIT unit = combo->currentLengthItem();
76 return LyXLength(length.toDouble(), unit).asString();
80 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
81 string const & len, LyXLength::UNIT defaultUnit)
84 // no length (UNIT_NONE)
85 combo->setCurrentItem(defaultUnit);
87 } else if (!isValidLength(len) && !isStrDbl(len)) {
88 // use input field only for gluelengths
89 combo->setCurrentItem(defaultUnit);
90 input->setText(toqstr(len));
92 combo->setCurrentItem(LyXLength(len).unit());
93 input->setText(toqstr(tostr(LyXLength(len).value())));
98 QString const toqstr(char const * str)
100 QTextCodec * codec = QTextCodec::codecForLocale();
102 return codec->toUnicode(str);
106 QString const toqstr(string const & str)
108 return toqstr(str.c_str());
112 QString const qt_(char const * str)
114 return toqstr(_(str));
118 QString const qt_(string const & str)
120 return toqstr(_(str));
124 string const fromqstr(QString const & str)
126 QTextCodec * codec = QTextCodec::codecForLocale();
127 QCString tmpstr = codec->fromUnicode(str);
128 char const * tmpcstr = tmpstr;
133 string const formatted(string const & text, int w)
140 string::size_type curpos = 0;
144 string::size_type const nxtpos1 = text.find(' ', curpos);
145 string::size_type const nxtpos2 = text.find('\n', curpos);
146 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
148 string const word = nxtpos == string::npos ?
149 text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
151 bool const newline = (nxtpos2 != string::npos &&
154 string const line_plus_word =
155 line.empty() ? word : line + ' ' + word;
157 // FIXME: make w be size_t
158 if (int(line_plus_word.length()) >= w) {
167 } else if (newline) {
168 sout += line_plus_word + '\n';
177 if (nxtpos == string::npos) {