X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffrontends%2Fqt4%2FLengthCombo.cpp;h=a21bc4da3668bae8cb52d7900d45b614f671e89f;hb=425d092204118ea6c24c28e85fdf03fcf2bb51a4;hp=0ddd1fcc137b36606bfaaf3e6dd86c0a07642c93;hpb=1fc477efac5fd3804c6407a03791e713f643fc43;p=lyx.git diff --git a/src/frontends/qt4/LengthCombo.cpp b/src/frontends/qt4/LengthCombo.cpp index 0ddd1fcc13..a21bc4da36 100644 --- a/src/frontends/qt4/LengthCombo.cpp +++ b/src/frontends/qt4/LengthCombo.cpp @@ -4,36 +4,48 @@ * Licence details can be found in the file COPYING. * * \author John Levon - * \author Herbert Voß + * \author Jürgen Spitzmüller + * \author Herbert Voß * * Full author contact details are available in file CREDITS. */ #include +#include "support/qstring_helpers.h" + #include "LengthCombo.h" #include "qt_helpers.h" +#include + LengthCombo::LengthCombo(QWidget * parent) : QComboBox(parent) { - for (int i = 0; i < lyx::num_units; i++) - addItem(lyx::qt_(lyx::unit_name_gui[i])); + for (int i = 0; i < lyx::num_units; i++) { + // mu does not make sense usually + // so it must be added manually, if needed + if (QString(lyx::unit_name[i]) == "mu") + continue; + QComboBox::addItem(lyx::qt_(lyx::unit_name_gui[i]), + lyx::toqstr(lyx::unit_name[i])); + } connect(this, SIGNAL(activated(int)), - this, SLOT(has_activated(int))); + this, SLOT(hasActivated(int))); } lyx::Length::UNIT LengthCombo::currentLengthItem() const { - return static_cast(currentIndex()); + QString const val = itemData(currentIndex()).toString(); + return lyx::unitFromString(lyx::fromqstr(val)); } -void LengthCombo::has_activated(int) +void LengthCombo::hasActivated(int) { // emit signal selectionChanged(currentLengthItem()); @@ -42,7 +54,19 @@ void LengthCombo::has_activated(int) void LengthCombo::setCurrentItem(lyx::Length::UNIT unit) { - QComboBox::setCurrentIndex(int(unit)); + setCurrentItem(lyx::toqstr(lyx::stringFromUnit(unit))); +} + + +void LengthCombo::setCurrentItem(QString const item) +{ + int num = QComboBox::count(); + for (int i = 0; i < num; i++) { + if (QComboBox::itemData(i).toString() == item) { + QComboBox::setCurrentIndex(i); + break; + } + } } @@ -62,7 +86,7 @@ void LengthCombo::noPercents() { int num = QComboBox::count(); for (int i = 0; i < num; i++) { - if (QComboBox::itemText(i).contains('%') > 0) { + if (QComboBox::itemData(i).toString().contains('%')) { QComboBox::removeItem(i); --i; --num; @@ -70,4 +94,33 @@ void LengthCombo::noPercents() } } + +void LengthCombo::removeUnit(lyx::Length::UNIT unit) +{ + QString const val = lyx::toqstr(lyx::stringFromUnit(unit)); + int num = QComboBox::count(); + for (int i = 0; i < num; i++) { + if (QComboBox::itemData(i).toString() == val) { + QComboBox::removeItem(i); + break; + } + } +} + + +void LengthCombo::addUnit(lyx::Length::UNIT unit) +{ + QString const val = lyx::toqstr(lyx::stringFromUnit(unit)); + int num = QComboBox::count(); + for (int i = 0; i < num; i++) { + if (QComboBox::itemData(i).toString() == val) { + // already there, nothing to do + return; + } + } + insertItem(int(unit), lyx::qt_(lyx::unit_name_gui[int(unit)]), + lyx::toqstr(lyx::unit_name[int(unit)])); +} + + #include "moc_LengthCombo.cpp"