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"
28 #include <qlineedit.h>
29 #include <qtextcodec.h>
36 using support::isStrDbl;
45 string makeFontName(string const & family, string const & foundry)
49 return family + " [" + foundry + ']';
53 pair<string, string> parseFontName(string const & name)
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(0, idx - 1),
59 name.substr(idx + 1, name.size() - idx - 2));
63 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
65 QString const length = input->text();
69 // Don't return unit-from-choice if the input(field) contains a unit
70 if (isValidGlueLength(fromqstr(length)))
71 return fromqstr(length);
73 Length::UNIT const unit = combo->currentLengthItem();
75 return Length(length.toDouble(), unit).asString();
79 Length widgetsToLength(QLineEdit const * input, QComboBox const * combo)
81 QString const length = input->text();
85 // don't return unit-from-choice if the input(field) contains a unit
86 if (isValidGlueLength(fromqstr(length)))
87 return Length(fromqstr(length));
89 Length::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
91 return Length(length.toDouble(), unit);
95 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
96 Length const & len, Length::UNIT /*defaultUnit*/)
98 combo->setCurrentItem(Length(len).unit());
99 input->setText(QString::number(Length(len).value()));
103 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
104 string const & len, Length::UNIT defaultUnit)
107 // no length (UNIT_NONE)
108 combo->setCurrentItem(defaultUnit);
110 } else if (!isValidLength(len) && !isStrDbl(len)) {
111 // use input field only for gluelengths
112 combo->setCurrentItem(defaultUnit);
113 input->setText(toqstr(len));
115 lengthToWidgets(input, combo, Length(len), defaultUnit);
120 void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo,
121 Length const & len, Length::UNIT defaultUnit)
123 if (len.value() == 0)
124 lengthToWidgets(input, combo, "auto", defaultUnit);
126 lengthToWidgets(input, combo, len, defaultUnit);
130 //NOTE "CB" here because we probably will want one of these
131 //for labeled sets, as well.
132 void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
133 LengthCombo * lengthCombo)
135 if (!checkBox->isChecked())
136 lengthToWidgets(lineEdit, lengthCombo,
137 "auto", lengthCombo->currentLengthItem());
138 else if (lineEdit->text() == "auto")
139 lengthToWidgets(lineEdit, lengthCombo, string(),
140 lengthCombo->currentLengthItem());
144 QString const qt_(char const * str, const char *)
146 return toqstr(_(str));
150 QString const qt_(string const & str)
152 return toqstr(_(str));
156 docstring const formatted(docstring const & text, int w)
163 docstring::size_type curpos = 0;
167 docstring::size_type const nxtpos1 = text.find(' ', curpos);
168 docstring::size_type const nxtpos2 = text.find('\n', curpos);
169 docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
171 docstring const word =
172 nxtpos == docstring::npos ?
173 text.substr(curpos) :
174 text.substr(curpos, nxtpos - curpos);
176 bool const newline = (nxtpos2 != docstring::npos &&
179 docstring const line_plus_word =
180 line.empty() ? word : line + char_type(' ') + word;
182 // FIXME: make w be size_t
183 if (int(line_plus_word.length()) >= w) {
184 sout += line + char_type('\n');
186 sout += word + char_type('\n');
192 } else if (newline) {
193 sout += line_plus_word + char_type('\n');
198 line += char_type(' ');
202 if (nxtpos == docstring::npos) {