* 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
This commit is contained in:
Jürgen Spitzmüller 2009-01-14 10:20:33 +00:00
parent b05dcd0252
commit 18a8d490c7
2 changed files with 72 additions and 5 deletions

View File

@ -4,6 +4,7 @@
* Licence details can be found in the file COPYING. * Licence details can be found in the file COPYING.
* *
* \author John Levon * \author John Levon
* \author Jürgen Spitzmüller
* \author Herbert Voß * \author Herbert Voß
* *
* Full author contact details are available in file CREDITS. * Full author contact details are available in file CREDITS.
@ -11,16 +12,26 @@
#include <config.h> #include <config.h>
#include "support/qstring_helpers.h"
#include "LengthCombo.h" #include "LengthCombo.h"
#include "qt_helpers.h" #include "qt_helpers.h"
#include <string>
LengthCombo::LengthCombo(QWidget * parent) LengthCombo::LengthCombo(QWidget * parent)
: QComboBox(parent) : QComboBox(parent)
{ {
for (int i = 0; i < lyx::num_units; i++) for (int i = 0; i < lyx::num_units; i++) {
addItem(lyx::qt_(lyx::unit_name_gui[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)), connect(this, SIGNAL(activated(int)),
this, SLOT(has_activated(int))); this, SLOT(has_activated(int)));
@ -29,7 +40,8 @@ LengthCombo::LengthCombo(QWidget * parent)
lyx::Length::UNIT LengthCombo::currentLengthItem() const lyx::Length::UNIT LengthCombo::currentLengthItem() const
{ {
return static_cast<lyx::Length::UNIT>(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) 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(); int num = QComboBox::count();
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
if (QComboBox::itemText(i).contains('%') > 0) { if (QComboBox::itemData(i).toString().contains('%')) {
QComboBox::removeItem(i); QComboBox::removeItem(i);
--i; --i;
--num; --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" #include "moc_LengthCombo.cpp"

View File

@ -38,6 +38,14 @@ public:
virtual void setEnabled(bool b); virtual void setEnabled(bool b);
/// use the %-items? /// use the %-items?
virtual void noPercents(); 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: protected Q_SLOTS:
virtual void has_activated(int index); virtual void has_activated(int index);