Jürgen Spitzmüller wrote:
> The problem is that if you remove the "mu" item from the combo,
> widgetsToLength will return a wrong value (off by one) for all subsequent
> items, so it will return "mu" for "textwidth%", for instance. For the
> percent- items, this was no problem only because they are the highest items
> in the enum anyway.

The attached patch is a safe implementation, IMHO.
It separates the value of the Length enum and the index of the combo, and it 
implements a general removeItem method instead of a dedicated noMu.

Furthermore, I have commented out the buggy and apparently unused function 
Length widgetsToLength()
in qt_helpers. This function would fail because it checks for the wrong 
strings. I think commenting out prevents that this is used somewhere.

BTW Uwe, it appears that the mu unit is not allowed in further contexts. I 
tried minipage width, where it triggers an error. We need to check that.

Jürgen
Index: src/frontends/qt4/qt_helpers.cpp
===================================================================
--- src/frontends/qt4/qt_helpers.cpp	(Revision 28095)
+++ src/frontends/qt4/qt_helpers.cpp	(Arbeitskopie)
@@ -82,6 +82,7 @@
 }
 
 
+#if 0
 Length widgetsToLength(QLineEdit const * input, QComboBox const * combo)
 {
 	QString const length = input->text();
@@ -92,10 +93,13 @@
 	if (isValidGlueLength(fromqstr(length)))
 		return Length(fromqstr(length));
 
-	Length::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
+//FIXME: this does not work. combo->currentText() is translatable!
+	Length::UNIT const unit =
+		unitFromString(fromqstr(combo->currentText()));
 
 	return Length(length.toDouble(), unit);
 }
+#endif
 
 
 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
Index: src/frontends/qt4/LengthCombo.cpp
===================================================================
--- src/frontends/qt4/LengthCombo.cpp	(Revision 28095)
+++ src/frontends/qt4/LengthCombo.cpp	(Arbeitskopie)
@@ -19,8 +19,10 @@
 LengthCombo::LengthCombo(QWidget * 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]));
+		setItemData(i, i);
+	}
 
 	connect(this, SIGNAL(activated(int)),
 		this, SLOT(has_activated(int)));
@@ -29,7 +31,7 @@
 
 lyx::Length::UNIT LengthCombo::currentLengthItem() const
 {
-	return static_cast<lyx::Length::UNIT>(currentIndex());
+	return static_cast<lyx::Length::UNIT>(itemData(currentIndex()).toInt());
 }
 
 
@@ -42,7 +44,13 @@
 
 void LengthCombo::setCurrentItem(lyx::Length::UNIT unit)
 {
-	QComboBox::setCurrentIndex(int(unit));
+	int num = QComboBox::count();
+	for (int i = 0; i < num; i++) {
+		if (QComboBox::itemData(i).toInt() == int(unit)) {
+			QComboBox::setCurrentIndex(i);
+			break;
+		}
+	}
 }
 
 
@@ -62,7 +70,7 @@
 {
 	int num = QComboBox::count();
 	for (int i = 0; i < num; i++) {
-		if (QComboBox::itemText(i).contains('%') > 0) {
+		if (QComboBox::itemText(i).contains('%')) {
 			QComboBox::removeItem(i);
 			--i;
 			--num;
@@ -70,4 +78,16 @@
 	}
 }
 
+
+void LengthCombo::removeItem(lyx::Length::UNIT unit)
+{
+	int num = QComboBox::count();
+	for (int i = 0; i < num; i++) {
+		if (QComboBox::itemData(i).toInt() == int(unit)) {
+			QComboBox::removeItem(i);
+			break;
+		}
+	}
+}
+
 #include "LengthCombo_moc.cpp"
Index: src/frontends/qt4/qt_helpers.h
===================================================================
--- src/frontends/qt4/qt_helpers.h	(Revision 28095)
+++ src/frontends/qt4/qt_helpers.h	(Arbeitskopie)
@@ -39,7 +39,7 @@
 /// method to get a Length from widgets (LengthCombo)
 std::string widgetsToLength(QLineEdit const * input, LengthCombo const * combo);
 /// method to get a Length from widgets (QComboBox)
-Length widgetsToLength(QLineEdit const * input, QComboBox const * combo);
+//Length widgetsToLength(QLineEdit const * input, QComboBox const * combo);
 
 //FIXME It would be nice if defaultUnit were a default argument
 /// method to set widgets from a Length
Index: src/frontends/qt4/GuiVSpace.cpp
===================================================================
--- src/frontends/qt4/GuiVSpace.cpp	(Revision 28095)
+++ src/frontends/qt4/GuiVSpace.cpp	(Arbeitskopie)
@@ -79,6 +79,8 @@
 
 	// remove the %-items from the unit choice
 	unitCO->noPercents();
+	// remove the unit "mu" because it is not allowed by \vspace
+	unitCO->removeItem(Length::MU);
 }
 
 
Index: src/frontends/qt4/LengthCombo.h
===================================================================
--- src/frontends/qt4/LengthCombo.h	(Revision 28095)
+++ src/frontends/qt4/LengthCombo.h	(Arbeitskopie)
@@ -38,6 +38,8 @@
 	virtual void setEnabled(bool b);
 	/// use the %-items?
 	virtual void noPercents();
+	/// remove items from the combo
+	virtual void removeItem(lyx::Length::UNIT unit);
 
 protected Q_SLOTS:
 	virtual void has_activated(int index);
Index: src/frontends/qt4/GuiHSpace.cpp
===================================================================
--- src/frontends/qt4/GuiHSpace.cpp	(Revision 28095)
+++ src/frontends/qt4/GuiHSpace.cpp	(Arbeitskopie)
@@ -78,6 +78,8 @@
 
 	// remove the %-items from the unit choice
 	unitCO->noPercents();
+	// remove the unit "mu" because it is not allowed by \hspace
+	unitCO->removeItem(Length::MU);
 }
 
 

Reply via email to