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/tostr.h"
23 #include <qcombobox.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(tostr(LyXLength(len).value())));
117 QString const toqstr(char const * str)
119 QTextCodec * codec = QTextCodec::codecForLocale();
121 return codec->toUnicode(str);
125 QString const toqstr(string const & str)
127 return toqstr(str.c_str());
131 QString const qt_(char const * str)
133 return toqstr(_(str));
137 QString const qt_(string const & str)
139 return toqstr(_(str));
143 string const fromqstr(QString const & str)
145 QTextCodec * codec = QTextCodec::codecForLocale();
146 QCString tmpstr = codec->fromUnicode(str);
147 char const * tmpcstr = tmpstr;
152 string const formatted(string const & text, int w)
159 string::size_type curpos = 0;
163 string::size_type const nxtpos1 = text.find(' ', curpos);
164 string::size_type const nxtpos2 = text.find('\n', curpos);
165 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
167 string const word = nxtpos == string::npos ?
168 text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
170 bool const newline = (nxtpos2 != string::npos &&
173 string const line_plus_word =
174 line.empty() ? word : line + ' ' + word;
176 // FIXME: make w be size_t
177 if (int(line_plus_word.length()) >= w) {
186 } else if (newline) {
187 sout += line_plus_word + '\n';
196 if (nxtpos == string::npos) {