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
10 * Full author contact details are available in file CREDITS.
15 #include "LengthCombo.h"
16 #include "qt_helpers.h"
18 #include "lengthcommon.h"
21 #include "support/os.h"
22 #include "support/lstrings.h"
23 #include "support/convert.h"
29 #include <qlineedit.h>
30 #include <qtextcodec.h>
37 using support::isStrDbl;
46 string makeFontName(string const & family, string const & foundry)
50 return family + " [" + foundry + ']';
54 pair<string, string> parseFontName(string const & name)
56 string::size_type const idx = name.find('[');
57 if (idx == string::npos || idx == 0)
58 return make_pair(name, string());
59 return make_pair(name.substr(0, idx - 1),
60 name.substr(idx + 1, name.size() - idx - 2));
64 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
66 QString const 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 Length::UNIT const unit = combo->currentLengthItem();
76 return Length(length.toDouble(), unit).asString();
80 Length widgetsToLength(QLineEdit const * input, QComboBox const * combo)
82 QString const length = input->text();
86 // don't return unit-from-choice if the input(field) contains a unit
87 if (isValidGlueLength(fromqstr(length)))
88 return Length(fromqstr(length));
90 Length::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
92 return Length(length.toDouble(), unit);
96 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
97 Length const & len, Length::UNIT /*defaultUnit*/)
99 combo->setCurrentItem(Length(len).unit());
100 input->setText(toqstr(convert<string>(Length(len).value())));
104 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
105 string const & len, Length::UNIT defaultUnit)
108 // no length (UNIT_NONE)
109 combo->setCurrentItem(defaultUnit);
111 } else if (!isValidLength(len) && !isStrDbl(len)) {
112 // use input field only for gluelengths
113 combo->setCurrentItem(defaultUnit);
114 input->setText(toqstr(len));
116 lengthToWidgets(input, combo, Length(len), defaultUnit);
121 void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo,
122 Length const & len, Length::UNIT defaultUnit)
124 if (len.value() == 0)
125 lengthToWidgets(input, combo, "auto", defaultUnit);
127 lengthToWidgets(input, combo, len, defaultUnit);
131 //NOTE "CB" here because we probably will want one of these
132 //for labeled sets, as well.
133 void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
134 LengthCombo * lengthCombo)
136 if (!checkBox->isChecked())
137 lengthToWidgets(lineEdit, lengthCombo,
138 "auto", lengthCombo->currentLengthItem());
139 else if (lineEdit->text() == "auto")
140 lengthToWidgets(lineEdit, lengthCombo, string(),
141 lengthCombo->currentLengthItem());
145 QString const qt_(char const * str, const char *)
147 return toqstr(_(str));
151 QString const qt_(string const & str)
153 return toqstr(_(str));
157 docstring const formatted(docstring const & text, int w)
164 docstring::size_type curpos = 0;
168 docstring::size_type const nxtpos1 = text.find(' ', curpos);
169 docstring::size_type const nxtpos2 = text.find('\n', curpos);
170 docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
172 docstring const word =
173 nxtpos == docstring::npos ?
174 text.substr(curpos) :
175 text.substr(curpos, nxtpos - curpos);
177 bool const newline = (nxtpos2 != docstring::npos &&
180 docstring const line_plus_word =
181 line.empty() ? word : line + char_type(' ') + word;
183 // FIXME: make w be size_t
184 if (int(line_plus_word.length()) >= w) {
185 sout += line + char_type('\n');
187 sout += word + char_type('\n');
193 } else if (newline) {
194 sout += line_plus_word + char_type('\n');
199 line += char_type(' ');
203 if (nxtpos == docstring::npos) {