On Tue, 19 Apr 2005, James Graham wrote:

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).

James - I think I've managed to make a cleaner patch based on your ideas to solve the problem. I'm using a combobox, but its list is populated according to the number currently entered. e.g., if "15cm" is in the box, the options are 15pt, 15mm, 15cm, 15in, 15%, 1/15. (maybe it should do _real_ unit conversions :-)

Do you think this is as good as your solution? Apologies for messing around with what you did.

Jeremy

--
Jeremy Sanders <[EMAIL PROTECTED]>
http://www.jeremysanders.net/                Cambridge, UK
Public Key Server PGP Key ID: E1AAE053
Index: ChangeLog
===================================================================
RCS file: /cvs/veusz/veusz/ChangeLog,v
retrieving revision 1.21
diff -u -r1.21 ChangeLog
--- ChangeLog   21 Apr 2005 20:51:41 -0000      1.21
+++ ChangeLog   22 Apr 2005 20:34:18 -0000
@@ -1,3 +1,6 @@
+* 2005-04-22 Jeremy Sanders <[EMAIL PROTECTED]>
+       - Display drop down list of units based on James Graham's patch
+       
 * 2005-04-21 Jeremy Sanders <[EMAIL PROTECTED]>
        - Widgets can now be added if a parent allows the widget as parent
        - New item selected after widget is added
Index: setting/controls.py
===================================================================
RCS file: /cvs/veusz/veusz/setting/controls.py,v
retrieving revision 1.8
diff -u -r1.8 controls.py
--- setting/controls.py 20 Apr 2005 10:10:11 -0000      1.8
+++ setting/controls.py 22 Apr 2005 20:34:19 -0000
@@ -22,6 +22,8 @@
    to be changed.
 """
 
+import re
+
 import qt
 import setting
 
@@ -117,7 +119,7 @@
             items.append(i)
         self.insertStringList(items)
 
-        # set the text of the widget to the 
+        # set the text of the widget to the setting
         self.setCurrentText( setting.toText() )
 
         # if a different item is selected
@@ -140,6 +142,8 @@
 
     def slotActivated(self, val):
         """If a different item is chosen."""
+        #print self, "Activated!"
+
         text = unicode(self.currentText())
         try:
             val = self.setting.fromText(text)
@@ -200,3 +204,61 @@
     def onModified(self, mod):
         """called when the setting is changed remotely"""
         self.setText( self.setting.toText() )
+
+class SettingDistance(SettingChoice):
+    """For editing distance settings."""
+
+    # used to remove non-numerics from the string
+    # we also remove 
+    stripnumre = re.compile(r"[0-9]*/|[^0-9.]")
+
+    # remove spaces
+    stripspcre = re.compile(r"\s")
+
+    def __init__(self, setting, parent):
+        '''Initialise with blank list, then populate with sensible units.'''
+        SettingChoice.__init__(self, setting, True, [], parent)
+        self.updateComboList()
+        
+    def updateComboList(self):
+        '''Populates combo list with sensible list of other possible units.'''
+
+        # turn off signals, so our modifications don't create more signals
+        self.blockSignals(True)
+
+        # get current text
+        text = unicode(self.currentText())
+
+        # get rid of non-numeric things from the string
+        num = self.stripnumre.sub('', text)
+
+        # here are a list of possible different units
+        # should this be in utils?
+        newitems = [ num+'pt', num+'cm', num+'mm',
+                     num+'in', num+'%', '1/'+num ]
+
+        # if we're already in this list, we position the current selection
+        # to the correct item (up and down keys work properly then)
+        # spaces are removed to make sure we get sensible matches
+        try:
+            spcfree = self.stripspcre.sub('', text)
+            index = newitems.index(spcfree)
+        except ValueError:
+            index = 0
+            newitems.insert(0, text)
+
+        # get rid of existing items in list (clear doesn't work here)
+        for i in range(self.count()):
+            self.removeItem(0)
+
+        # put new items in and select the correct option
+        self.insertStrList(newitems)
+        self.setCurrentItem(index)
+
+        # must remember to do this!
+        self.blockSignals(False)
+
+    def slotActivated(self, val):
+        '''Populate the drop down list before activation.'''
+        self.updateComboList()
+        SettingChoice.slotActivated(self, val)
Index: setting/setting.py
===================================================================
RCS file: /cvs/veusz/veusz/setting/setting.py,v
retrieving revision 1.12
diff -u -r1.12 setting.py
--- setting/setting.py  19 Apr 2005 20:34:40 -0000      1.12
+++ setting/setting.py  22 Apr 2005 20:34:20 -0000
@@ -364,7 +364,7 @@
             raise InvalidType
         
     def makeControl(self, *args):
-        return controls.SettingEdit(self, *args)
+        return controls.SettingDistance(self, *args)
 
 class Choice(Setting):
     """One out of a list of strings."""

Répondre à