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 "qt_helpers.h"
17 #include "LengthCombo.h"
23 #include "support/os.h"
24 #include "support/lstrings.h"
36 using support::isStrDbl;
42 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
44 QString const length = input->text();
48 // Don't return unit-from-choice if the input(field) contains a unit
49 if (isValidGlueLength(fromqstr(length)))
50 return fromqstr(length);
52 Length::UNIT const unit = combo->currentLengthItem();
54 return Length(length.toDouble(), unit).asString();
58 Length widgetsToLength(QLineEdit const * input, QComboBox const * combo)
60 QString const length = input->text();
64 // don't return unit-from-choice if the input(field) contains a unit
65 if (isValidGlueLength(fromqstr(length)))
66 return Length(fromqstr(length));
68 Length::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
70 return Length(length.toDouble(), unit);
74 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
75 Length const & len, Length::UNIT /*defaultUnit*/)
77 combo->setCurrentItem(len.unit());
78 input->setText(QString::number(Length(len).value()));
82 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
83 string const & len, Length::UNIT defaultUnit)
86 // no length (UNIT_NONE)
87 combo->setCurrentItem(defaultUnit);
89 } else if (!isValidLength(len) && !isStrDbl(len)) {
90 // use input field only for gluelengths
91 combo->setCurrentItem(defaultUnit);
92 input->setText(toqstr(len));
94 lengthToWidgets(input, combo, Length(len), defaultUnit);
99 void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo,
100 Length const & len, Length::UNIT defaultUnit)
102 if (len.value() == 0)
103 lengthToWidgets(input, combo, "auto", defaultUnit);
105 lengthToWidgets(input, combo, len, defaultUnit);
109 //NOTE "CB" here because we probably will want one of these
110 //for labeled sets, as well.
111 void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
112 LengthCombo * lengthCombo)
114 if (!checkBox->isChecked())
115 lengthToWidgets(lineEdit, lengthCombo,
116 "auto", lengthCombo->currentLengthItem());
117 else if (lineEdit->text() == "auto")
118 lengthToWidgets(lineEdit, lengthCombo, string(),
119 lengthCombo->currentLengthItem());
123 void setValid(QWidget * widget, bool valid)
126 widget->setPalette(QPalette());
128 QPalette pal = widget->palette();
129 pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0));
130 widget->setPalette(pal);
135 QString const qt_(char const * str, const char *)
137 return toqstr(_(str));
141 QString const qt_(string const & str)
143 return toqstr(_(str));