Juergen Spitzmueller wrote:
> the only way I see to solve this is to set the LyXLength default unit to
> UNIT_NONE instead of PT (which is very arbitrary anyway) and add a bool
> empty() to lyxlength

Attached is a patch which fixes bug 490 this way.

For QT, I have written two methods in QDoument.C, similar to 
UpdateWidgetsFormLength and GetLengthFromWidgets. They could be moved outside 
QDocument eventually (where?). Currently, they are only used by qdocument, 
but they will probably be used by qtabular too.
As a side effect, QDocument now supports default_unit and power user inputs 
("length unit")

Please apply, if there are no objections.

Thanks,
Jürgen.
Index: src/ChangeLog
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/ChangeLog,v
retrieving revision 1.974
diff -u -r1.974 ChangeLog
--- src/ChangeLog	2002/11/08 01:08:25	1.974
+++ src/ChangeLog	2002/11/08 11:09:33
@@ -1,3 +1,8 @@
+2002-11-07  Juergen Spitzmueller  <[EMAIL PROTECTED]>
+
+	* lyxlength.[Ch]: set default unit to UNIT_NONE, 
+	implement bool empty() [bug 490]
+
 2002-11-08  John Levon  <[EMAIL PROTECTED]>
 
 	* iterators.h:
Index: src/lyxlength.C
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/lyxlength.C,v
retrieving revision 1.24
diff -u -r1.24 lyxlength.C
--- src/lyxlength.C	2002/11/04 02:12:29	1.24
+++ src/lyxlength.C	2002/11/08 11:09:34
@@ -27,7 +27,7 @@
 using std::abs;
 
 LyXLength::LyXLength()
-	: val_(0), unit_(LyXLength::PT)
+	: val_(0), unit_(LyXLength::UNIT_NONE)
 {}
 
 
@@ -120,6 +120,12 @@
 bool LyXLength::zero() const
 {
 	return val_ == 0.0;
+}
+
+
+bool LyXLength::empty() const
+{
+	return unit_ == LyXLength::UNIT_NONE;
 }
 
 
Index: src/lyxlength.h
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/lyxlength.h,v
retrieving revision 1.10
diff -u -r1.10 lyxlength.h
--- src/lyxlength.h	2002/10/24 18:31:45	1.10
+++ src/lyxlength.h	2002/11/08 11:09:34
@@ -63,6 +63,8 @@
 	void unit(LyXLength::UNIT unit);
 	///
 	bool zero() const;
+	///
+	bool empty() const;
 	/// return string representation
 	string const asString() const;
 	/// return string representation for LaTeX
Index: src/frontends/qt2/ChangeLog
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/frontends/qt2/ChangeLog,v
retrieving revision 1.273
diff -u -r1.273 ChangeLog
--- src/frontends/qt2/ChangeLog	2002/11/08 09:57:07	1.273
+++ src/frontends/qt2/ChangeLog	2002/11/08 11:09:38
@@ -1,3 +1,8 @@
+2002-11-08  Juergen Spitzmueller  <[EMAIL PROTECTED]>
+
+	* QDocument.[Ch]: implement WidgetsToLength, LengthToWidgets, use them
+	use default_unit
+
 2002-11-08  Edwin Leuven  <[EMAIL PROTECTED]>
 
 	* Dialogs2.C: compile fix
@@ -26,11 +31,11 @@
 
 	* ui/QPref*: add initial prefs ui from Juergen S
  
-2002-11-27  Juergen Spitzmueller  <[EMAIL PROTECTED]>
+2002-11-07  Juergen Spitzmueller  <[EMAIL PROTECTED]>
 
 	* ui/QDocument.ui:
 	* QDocumentDialog.C:
-	* QDocument.[Ch]: 
+	* QDocument.[Ch]:
 	Implement "Save as default" and "Use class defaults"
 	
 	* QDocumentDialog.C: Fix dialog update on class change
Index: src/frontends/qt2/QDocument.C
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/frontends/qt2/QDocument.C,v
retrieving revision 1.21
diff -u -r1.21 QDocument.C
--- src/frontends/qt2/QDocument.C	2002/11/08 00:09:00	1.21
+++ src/frontends/qt2/QDocument.C	2002/11/08 11:09:39
@@ -31,6 +31,7 @@
 #include "lyxtextclasslist.h"
 #include "vspace.h"
 #include "bufferparams.h"
+#include "lyxrc.h" // default_unit
 
 #include <qpushbutton.h>
 #include <qmultilineedit.h>
@@ -294,16 +295,12 @@
 	params.papersize2 =
 		dialog_->paperModule->papersizeCO->currentItem();
 
-	params.paperwidth =
-		LyXLength(dialog_->paperModule->paperwidthLE->text().toDouble(),
-			  dialog_->paperModule->paperwidthUnitCO->currentLengthItem()
-			).asString();
-
-	params.paperheight =
-		LyXLength(dialog_->paperModule->paperheightLE->text().toDouble(),
-			  dialog_->paperModule->paperheightUnitCO->currentLengthItem()
-			).asString();
+	params.paperwidth = WidgetsToLength(dialog_->paperModule->paperwidthLE,
+		dialog_->paperModule->paperwidthUnitCO);
 
+	params.paperheight = WidgetsToLength(dialog_->paperModule->paperheightLE,
+		dialog_->paperModule->paperheightUnitCO);
+
 	if (dialog_->paperModule->twoColumnCB->isChecked())
 		params.columns = 2;
 	else
@@ -328,46 +325,27 @@
 		margin = margin - 1;
 	}
 	params.paperpackage = char(margin);
-
-	params.leftmargin =
-		LyXLength(dialog_->marginsModule->innerLE->text().toDouble(),
-			  dialog_->marginsModule->innerUnit->currentLengthItem()
-			  ).asString();
-
-	params.topmargin =
-		LyXLength(dialog_->marginsModule->topLE->text().toDouble(),
-			  dialog_->marginsModule->topUnit->currentLengthItem()
-			  ).asString();
-
-
-	params.rightmargin =
-		LyXLength(dialog_->marginsModule->outerLE->text().toDouble(),
-			  dialog_->marginsModule->outerUnit->currentLengthItem()
-			  ).asString();
-
 
-	params.bottommargin =
-		LyXLength(dialog_->marginsModule->bottomLE->text().toDouble(),
-			  dialog_->marginsModule->bottomUnit->currentLengthItem()
-			  ).asString();
+	params.leftmargin = WidgetsToLength(dialog_->marginsModule->innerLE,
+		dialog_->marginsModule->innerUnit);
 
+	params.topmargin = WidgetsToLength(dialog_->marginsModule->topLE,
+		dialog_->marginsModule->topUnit);
 
-	params.headheight =
-		LyXLength(dialog_->marginsModule->headheightLE->text().toDouble(),
-			  dialog_->marginsModule->headheightUnit->currentLengthItem()
-			  ).asString();
+	params.rightmargin = WidgetsToLength(dialog_->marginsModule->outerLE,
+		dialog_->marginsModule->outerUnit);
 
+	params.bottommargin = WidgetsToLength(dialog_->marginsModule->bottomLE,
+		dialog_->marginsModule->bottomUnit);
 
-	params.headsep =
-		LyXLength(dialog_->marginsModule->headsepLE->text().toDouble(),
-			  dialog_->marginsModule->headsepUnit->currentLengthItem()
-			  ).asString();
+	params.headheight = WidgetsToLength(dialog_->marginsModule->headheightLE,
+		dialog_->marginsModule->headheightUnit);
 
+	params.headsep = WidgetsToLength(dialog_->marginsModule->headsepLE,
+		dialog_->marginsModule->headsepUnit);
 
-	params.footskip =
-		LyXLength(dialog_->marginsModule->footskipLE->text().toDouble(),
-			  dialog_->marginsModule->footskipUnit->currentLengthItem()
-			  ).asString();
+	params.footskip = WidgetsToLength(dialog_->marginsModule->footskipLE,
+		dialog_->marginsModule->footskipUnit);
 
 }
 
@@ -391,6 +369,35 @@
 } // namespace anom
 
 
+string QDocument::WidgetsToLength(QLineEdit * & input, LengthCombo * & combo)
+{
+	QString length = input->text();
+	if (length.isEmpty())
+		return string();
+
+	// don't return unit-from-choice if the input(field) contains a unit
+	if (isValidGlueLength(length.latin1()))
+		return length.latin1();
+
+	LyXLength::UNIT unit = combo->currentLengthItem();
+
+	return LyXLength(length.toDouble(), unit).asString();
+}
+
+void QDocument::LengthToWidgets(QLineEdit * & input, LengthCombo * & combo,
+	string const & len, LyXLength::UNIT default_unit)
+{
+	if (len.empty()) {
+		// no length (UNIT_NONE)
+		combo->setCurrentItem(default_unit);
+		input->setText("");
+	} else {
+		combo->setCurrentItem(LyXLength(len).unit());
+		input->setText(tostr(LyXLength(len).value()).c_str());
+	}
+}
+
+
 void QDocument::update_contents()
 {
 	if (!dialog_.get())
@@ -566,19 +573,20 @@
 
 	dialog_->paperModule->twoColumnCB->setChecked(
 		params.columns == 2);
+		
+	// Default unit choice is cm if metric, inches if US paper.
+	int const paperchoice = dialog_->paperModule->papersizeCO->currentItem();
+	bool const metric = 
+		(paperchoice == 0 && lyxrc.default_papersize > BufferParams::PAPER_EXECUTIVEPAPER)
+		|| paperchoice == 1 || paperchoice > 4;
+	LyXLength::UNIT default_unit = metric ? LyXLength::CM : LyXLength::IN;
 
-	dialog_->paperModule->paperwidthUnitCO->setCurrentItem(
-		LyXLength(params.paperwidth).unit());
+	LengthToWidgets(dialog_->paperModule->paperwidthLE,
+		dialog_->paperModule->paperwidthUnitCO, params.paperwidth, default_unit);
 
-	dialog_->paperModule->paperwidthLE->setText(
-		tostr(LyXLength(params.paperwidth).value()).c_str());
+	LengthToWidgets(dialog_->paperModule->paperheightLE,
+		dialog_->paperModule->paperheightUnitCO, params.paperheight, default_unit);
 
-	dialog_->paperModule->paperheightUnitCO->setCurrentItem(
-		LyXLength(params.paperheight).unit());
-
-	dialog_->paperModule->paperheightLE->setText(
-		tostr(LyXLength(params.paperheight).value()).c_str());
-
 	// margins
 	int item = params.paperpackage;
 	if (params.use_geometry) {
@@ -588,41 +596,27 @@
 	}
 	dialog_->marginsModule->marginCO->setCurrentItem(item);
 	dialog_->setCustomMargins(item);
+
+	LengthToWidgets(dialog_->marginsModule->topLE,
+		dialog_->marginsModule->topUnit, params.topmargin, default_unit);
+
+	LengthToWidgets(dialog_->marginsModule->bottomLE,
+		dialog_->marginsModule->bottomUnit, params.bottommargin, default_unit);
+		
+	LengthToWidgets(dialog_->marginsModule->innerLE,
+		dialog_->marginsModule->innerUnit, params.leftmargin, default_unit);
+
+	LengthToWidgets(dialog_->marginsModule->outerLE,
+		dialog_->marginsModule->outerUnit, params.rightmargin, default_unit);
+
+	LengthToWidgets(dialog_->marginsModule->headheightLE,
+		dialog_->marginsModule->headheightUnit, params.headheight, default_unit);
+
+	LengthToWidgets(dialog_->marginsModule->headsepLE,
+		dialog_->marginsModule->headsepUnit, params.headsep, default_unit);
 
-	dialog_->marginsModule->topUnit->setCurrentItem(
-		LyXLength(params.topmargin).unit());
-	dialog_->marginsModule->topLE->setText(
-		tostr(LyXLength(params.topmargin).value()).c_str());
-
-	dialog_->marginsModule->bottomUnit->setCurrentItem(
-		LyXLength(params.bottommargin).unit());
-	dialog_->marginsModule->bottomLE->setText(
-		tostr(LyXLength(params.bottommargin).value()).c_str());
-
-	dialog_->marginsModule->innerUnit->setCurrentItem(
-		LyXLength(params.leftmargin).unit());
-	dialog_->marginsModule->innerLE->setText(
-		tostr(LyXLength(params.leftmargin).value()).c_str());
-
-	dialog_->marginsModule->outerUnit->setCurrentItem(
-		LyXLength(params.rightmargin).unit());
-	dialog_->marginsModule->outerLE->setText(
-		tostr(LyXLength(params.rightmargin).value()).c_str());
-
-	dialog_->marginsModule->headheightUnit->setCurrentItem(
-		LyXLength(params.headheight).unit());
-	dialog_->marginsModule->headheightLE->setText(
-		tostr(LyXLength(params.headheight).value()).c_str());
-
-	dialog_->marginsModule->headsepUnit->setCurrentItem(
-		LyXLength(params.headsep).unit());
-	dialog_->marginsModule->headsepLE->setText(
-		tostr(LyXLength(params.headsep).value()).c_str());
-
-	dialog_->marginsModule->footskipUnit->setCurrentItem(
-		LyXLength(params.footskip).unit());
-	dialog_->marginsModule->footskipLE->setText(
-		tostr(LyXLength(params.footskip).value()).c_str());
+	LengthToWidgets(dialog_->marginsModule->footskipLE,
+		dialog_->marginsModule->footskipUnit, params.footskip, default_unit);
 }
 
 
Index: src/frontends/qt2/QDocument.h
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/frontends/qt2/QDocument.h,v
retrieving revision 1.12
diff -u -r1.12 QDocument.h
--- src/frontends/qt2/QDocument.h	2002/11/07 15:48:38	1.12
+++ src/frontends/qt2/QDocument.h	2002/11/08 11:09:39
@@ -20,6 +20,7 @@
 #include "Qt2Base.h"
 #include "Qt2BC.h"
 //#include "QtLyXView.h"
+#include "lengthcombo.h"
 
 #include <boost/scoped_ptr.hpp>
 
@@ -37,6 +38,11 @@
 private:
 	/// Apply changes
 	void apply();
+	/// Method get Length from Widgets
+	string WidgetsToLength(QLineEdit * & input, LengthCombo * & combo);
+	/// Method to set Widgets from a LyXLength
+	void LengthToWidgets(QLineEdit * & input, LengthCombo * & combo,
+		string const & len, LyXLength::UNIT default_unit);
 	/// update
 	void update_contents();
 	/// build the dialog
Index: src/frontends/xforms/ChangeLog
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/frontends/xforms/ChangeLog,v
retrieving revision 1.597
diff -u -r1.597 ChangeLog
--- src/frontends/xforms/ChangeLog	2002/11/07 00:21:28	1.597
+++ src/frontends/xforms/ChangeLog	2002/11/08 11:09:48
@@ -1,3 +1,8 @@
+2002-11-08  Juergen Spitzmueller  <[EMAIL PROTECTED]>
+
+	* xforms_helpers.C: (updateWidgetsFromLength)
+	use len.empty() instead of len.zero() [bug 490]
+
 2002-10-31  Herbert Voss  <[EMAIL PROTECTED]>
  
  	* FormGraphics.C (build, apply): get the rotate-list from the 
Index: src/frontends/xforms/xforms_helpers.C
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/frontends/xforms/xforms_helpers.C,v
retrieving revision 1.52
diff -u -r1.52 xforms_helpers.C
--- src/frontends/xforms/xforms_helpers.C	2002/10/23 08:31:10	1.52
+++ src/frontends/xforms/xforms_helpers.C	2002/11/08 11:09:49
@@ -163,7 +163,7 @@
 	lyx::Assert(input  && input->objclass  == FL_INPUT &&
 		    choice && choice->objclass == FL_CHOICE);
 
-	if (len.zero()) {
+	if (len.empty()) {
 		fl_set_input(input, "");
 		fl_set_choice_text(choice, default_unit.c_str());
 	} else {

Reply via email to