[Veusz-discuss] Adding units combobox

2005-04-19 Par sujet James Graham
Attached is the patch I mentioned to add a dropdown combo box containing 
the distance measures. The patch seems   to work -  doing things like 
entering "25cm" will correctly parse out the units and update the 
combobox and lineedit. The major piece of wierdness is that selecting 
"fraction" in the dropdown with a number > 1 will actually choose "pts" 
(this is what the code in utilfuncs.py actually does, but it doesn't 
seem like great UI).


_However_

The patch is kinda ugly, for example there's some quite involved logic 
to determine what happens if you enter a value without units. I'm not 
quite sure what the best way to make it prettier is. Suggestions?
Index: setting/controls.py
===
RCS file: /cvs/veusz/veusz/setting/controls.py,v
retrieving revision 1.6
diff -u -r1.6 controls.py
--- setting/controls.py	25 Mar 2005 18:11:38 -	1.6
+++ setting/controls.py	19 Apr 2005 20:24:48 -
@@ -24,6 +24,7 @@
 
 import qt
 import setting
+import utils
 
 class SettingEdit(qt.QLineEdit):
 """Main control for editing settings which are text."""
@@ -70,7 +71,88 @@
 def onModified(self, mod):
 """called when the setting is changed remotely"""
 self.setText( self.setting.toText() )
+
+class DistanceSettingEdit(qt.QHBox):
+def __init__(self, setting, parent):
+qt.QWidget.__init__(self,parent)
+
+self.setting=setting
+self.bgcolour = self.paletteBackgroundColor()
+
+self.box = qt.QHBox(self)
+
+self.valueBox = qt.QLineEdit(self,"valueBox")
+
+self.unitsCombo = qt.QComboBox(0,self,"unitsCombo")
+ 
+self.unitsCombo.setEditable(False)
+self.unitsCombo.clear()
+
+units = utils.getDistUnitNames()
+del units[units.index("ratio")]
+
+for item in units:
+self.unitsCombo.insertItem(item)
+
+self.connect(self.unitsCombo, qt.SIGNAL('activated(const QString&)'),
+ self.validateAndSet)
+
+self.setting.setOnModified(self.onModified)
+
+self.connect(self.valueBox, qt.SIGNAL('lostFocus()'),
+ self.validateAndSet)
+self.connect(self.valueBox, qt.SIGNAL('returnPressed()'),
+ self.validateAndSet)
+self.updateFromString(setting.toText(), True)
+
+def updateFromString(self, string, noUnitsIsRatio=False):
+"""Take a string and set the values of the controls appropriately"""
+value,unit = utils.splitDist(string)
+self.valueBox.setText(value)
+#If the value returned is ratio, there are three possibilites
+# - we are being called from the lineEdit being modified without
+#   a specific unit-  in this case we want to keep the current unit
+# - a dimenionless unit < 1 is specified in the lineEdit- this
+#   indicates a decimal fraction of the maximum allowed size
+# - a dimenionless unit > 1 is specified in the lineEdit- this converts
+#   the unit to points
+if unit=="ratio":
+if noUnitsIsRatio:
+if float(value) > 1:
+unit = "pt"
+else:
+unit = "fraction"
+self.unitsCombo.setCurrentText(unit)
+else:
+self.unitsCombo.setCurrentText(unit)
+
+def done(self):
+"""Delete modification notification."""
+self.setting.removeOnModified(self.onModified)
+
+def validateAndSet(self):
+"""Check the text is a valid setting and update it."""
+self.updateFromString(str(self.valueBox.text()))
+
+if not(self.unitsCombo.currentText() == "fraction"):
+text = str(self.valueBox.text()) +  str(self.unitsCombo.currentText())
+else:
+text = str(self.valueBox.text())
+try:
+val = self.setting.fromText(text)
+self.valueBox.setPaletteBackgroundColor(self.bgcolour)
+
+# value has changed
+if self.setting.get() != val:
+self.setting.set(val)
+
+except setting.InvalidType:
+self.valueBox.setPaletteBackgroundColor(qt.QColor('red'))
+
+def onModified(self, mod):
+self.updateFromString(self.setting.toText(), True)
 
+
 class BoolSettingEdit(qt.QCheckBox):
 """A check box for changing a bool setting."""
 
Index: setting/setting.py
===
RCS file: /cvs/veusz/veusz/setting/setting.py,v
retrieving revision 1.10
diff -u -r1.10 setting.py
--- setting/setting.py	9 Apr 2005 22:20:02 -	1.10
+++ setting/setting.py	19 Apr 2005 20:24:48 -
@@ -364,7 +364,7 @@
 raise InvalidType
 
 def makeControl(self, *args):
-return controls.SettingEdit(self, *args)
+return controls.DistanceSettingEdit(self, *args)
 
 class Choice(Setting):
 """One out of a l

[Veusz-discuss] Re: [Veusz-exception-reports] Veusz exception report

2005-04-19 Par sujet Jeremy Sanders

On Tue, 19 Apr 2005, Ian Reid wrote:


UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-6: ordinal not in range(128)


Okay, the included patch should fix the problem. If you're using the 
source distribution, you can cd to the veusz source directory and run


patch -p0 < patch

(or maybe try patch -p1 < patch)

Where patch is the file included here, to fix the problem.

If you can't do this, I could always build another rpm/distribution for 
you.


Thanks

Jeremy

--
Jeremy Sanders <[EMAIL PROTECTED]>
http://www.jeremysanders.net/Cambridge, UK
Public Key Server PGP Key ID: E1AAE053--- setting/collections.py.~1.10.~  2005-04-10 17:15:32.0 +0100
+++ setting/collections.py  2005-04-19 14:57:49.664460633 +0100
@@ -209,7 +209,7 @@
 # build a dict up with the list of families
 families = {}
 for i in db.families():
-families[str(i)] = True
+families[unicode(i)] = True
 
 for i in ['Times New Roman', 'Bitstream Vera Serif', 'Times', 'Utopia',
   'Serif']:


[Veusz-discuss] Complex numbers

2005-04-19 Par sujet James Graham
So I've ben trying to use the fitting function with functions that (I 
discovered) internally require complex algebra even though the answer is 
real. Clearly this doesn't work because the arrays are of a float type. 
Is there a way to work around this (e.g. by defining a function on the 
command line that takes the free parameters as arguments and returns the 
results and fitting against that), or a way to fix this (using complex 
arrays internally and converting to real arrays for display - though I 
would worry about possible implications of this)?


--
"But if science you say still sounds too deep,
Just do what Beaker does, just shrug and 'Meep!'"

-- Dr. Bunsen Honeydew & Beaker of Muppet Labs