From 18a8d490c7b8c2f33d41d529b5634007be5d157a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Spitzm=C3=BCller?= Date: Wed, 14 Jan 2009 10:20:33 +0000 Subject: [PATCH] * LengthCombo.{cpp, h}: - use model/view infrastructure to store reliable data - use real unit name, not i18n strings, for all comparision purposes - remove unit "mu" in default contruction (bug 5682) - add some helpers to add and remove units git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@28146 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/qt4/LengthCombo.cpp | 69 ++++++++++++++++++++++++++++--- src/frontends/qt4/LengthCombo.h | 8 ++++ 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/frontends/qt4/LengthCombo.cpp b/src/frontends/qt4/LengthCombo.cpp index c006f66452..448737c3d7 100644 --- a/src/frontends/qt4/LengthCombo.cpp +++ b/src/frontends/qt4/LengthCombo.cpp @@ -4,6 +4,7 @@ * Licence details can be found in the file COPYING. * * \author John Levon + * \author Jürgen Spitzmüller * \author Herbert Voß * * Full author contact details are available in file CREDITS. @@ -11,16 +12,26 @@ #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 (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))); @@ -29,7 +40,8 @@ LengthCombo::LengthCombo(QWidget * parent) lyx::Length::UNIT LengthCombo::currentLengthItem() const { - return static_cast(currentIndex()); + QString const val = itemData(currentIndex()).toString(); + return lyx::unitFromString(lyx::fromqstr(val)); } @@ -42,7 +54,14 @@ void LengthCombo::has_activated(int) void LengthCombo::setCurrentItem(lyx::Length::UNIT unit) { - QComboBox::setCurrentIndex(int(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::setCurrentIndex(i); + break; + } + } } @@ -62,7 +81,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 +89,44 @@ void LengthCombo::noPercents() } } + +void LengthCombo::removeItem(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::removeItem(int item) +{ + QComboBox::removeItem(item); +} + + +void LengthCombo::addItem(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)])); +} + + +void LengthCombo::addItem(QString const item) +{ + QComboBox::addItem(item); +} + #include "moc_LengthCombo.cpp" diff --git a/src/frontends/qt4/LengthCombo.h b/src/frontends/qt4/LengthCombo.h index affae3034f..32e98c48d4 100644 --- a/src/frontends/qt4/LengthCombo.h +++ b/src/frontends/qt4/LengthCombo.h @@ -38,6 +38,14 @@ public: virtual void setEnabled(bool b); /// use the %-items? virtual void noPercents(); + /// remove a unit from the combo + virtual void removeItem(lyx::Length::UNIT unit); + /// remove an item to the combo + virtual void removeItem(int item); + /// add a unit to the combo + virtual void addItem(lyx::Length::UNIT unit); + /// add an item to the combo + virtual void addItem(QString const item); protected Q_SLOTS: virtual void has_activated(int index);