/** * \file qt_helpers.cpp * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * * \author Dekel Tsur * \author Jürgen Spitzmüller * \author Richard Heck * * Full author contact details are available in file CREDITS. */ #include #include "LengthCombo.h" #include "qt_helpers.h" #include "lengthcommon.h" #include "gettext.h" #include "support/os.h" #include "support/lstrings.h" #include "debug.h" #include #include #include #include #include namespace lyx { using support::isStrDbl; using std::vector; using std::string; string widgetsToLength(QLineEdit const * input, LengthCombo const * combo) { QString const length = input->text(); if (length.isEmpty()) return string(); // Don't return unit-from-choice if the input(field) contains a unit if (isValidGlueLength(fromqstr(length))) return fromqstr(length); Length::UNIT const unit = combo->currentLengthItem(); return Length(length.toDouble(), unit).asString(); } Length widgetsToLength(QLineEdit const * input, QComboBox const * combo) { QString const length = input->text(); if (length.isEmpty()) return Length(); // don't return unit-from-choice if the input(field) contains a unit if (isValidGlueLength(fromqstr(length))) return Length(fromqstr(length)); Length::UNIT const unit = unitFromString(fromqstr(combo->currentText())); return Length(length.toDouble(), unit); } void lengthToWidgets(QLineEdit * input, LengthCombo * combo, Length const & len, Length::UNIT /*defaultUnit*/) { combo->setCurrentItem(len.unit()); input->setText(QString::number(Length(len).value())); } void lengthToWidgets(QLineEdit * input, LengthCombo * combo, string const & len, Length::UNIT defaultUnit) { if (len.empty()) { // no length (UNIT_NONE) combo->setCurrentItem(defaultUnit); input->setText(""); } else if (!isValidLength(len) && !isStrDbl(len)) { // use input field only for gluelengths combo->setCurrentItem(defaultUnit); input->setText(toqstr(len)); } else { lengthToWidgets(input, combo, Length(len), defaultUnit); } } void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo, Length const & len, Length::UNIT defaultUnit) { if (len.value() == 0) lengthToWidgets(input, combo, "auto", defaultUnit); else lengthToWidgets(input, combo, len, defaultUnit); } //NOTE "CB" here because we probably will want one of these //for labeled sets, as well. void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit, LengthCombo * lengthCombo) { if (!checkBox->isChecked()) lengthToWidgets(lineEdit, lengthCombo, "auto", lengthCombo->currentLengthItem()); else if (lineEdit->text() == "auto") lengthToWidgets(lineEdit, lengthCombo, string(), lengthCombo->currentLengthItem()); } void setValid(QWidget * widget, bool valid) { if (valid) { widget->setPalette(QPalette()); } else { QPalette pal = widget->palette(); pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0)); widget->setPalette(pal); } } QString const qt_(char const * str, const char *) { return toqstr(_(str)); } QString const qt_(string const & str) { return toqstr(_(str)); } } // namespace lyx