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"
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(QString::number(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 void setValid(QWidget * widget, bool valid)
148 widget->setPalette(QPalette());
150 QPalette pal = widget->palette();
151 pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0));
152 widget->setPalette(pal);
157 QString const qt_(char const * str, const char *)
159 return toqstr(_(str));
163 QString const qt_(string const & str)
165 return toqstr(_(str));
169 docstring const formatted(docstring const & text, int w)
176 docstring::size_type curpos = 0;
180 docstring::size_type const nxtpos1 = text.find(' ', curpos);
181 docstring::size_type const nxtpos2 = text.find('\n', curpos);
182 docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
184 docstring const word =
185 nxtpos == docstring::npos ?
186 text.substr(curpos) :
187 text.substr(curpos, nxtpos - curpos);
189 bool const newline = (nxtpos2 != docstring::npos &&
192 docstring const line_plus_word =
193 line.empty() ? word : line + char_type(' ') + word;
195 // FIXME: make w be size_t
196 if (int(line_plus_word.length()) >= w) {
197 sout += line + char_type('\n');
199 sout += word + char_type('\n');
205 } else if (newline) {
206 sout += line_plus_word + char_type('\n');
211 line += char_type(' ');
215 if (nxtpos == docstring::npos) {