Alex Grönholm avatar Alex Grönholm committed 1afa189

Added the possibility of giving a lambda instead of an attribute name

Comments (0)

Files changed (1)

swingutils/models/table.py

     # Overridden DelegateTableModel methods
     #
 
+    def __init__(self, delegate, *args):
+        AbstractTableModel.__init__(self)
+        AbstractDelegateList.__init__(self, delegate)
+
+        self.__columns__ = list(args if args else self.__columns__)
+        self._getters = [None] * len(self.__columns__)
+        for index, column in enumerate(self.__columns__):
+            self.__columns__[index] = self._validateColumn(column, index)
+
     def _validateColumn(self, column, index):
         column = DelegateTableModel._validateColumn(self, column, index)
         if len(column) < 3:
             raise ValueError('Column %d: missing object attribute name' %
                              index)
-        if not isinstance(column[2], basestring):
-            raise ValueError('Column %d: object attribute name must be a '
-                             'string' % index)
+        if isinstance(column[2], basestring):
+            self._getters[index] = lambda row: getattr(row, column[2])
+        elif hasattr(column[2], '__call__'):
+            self._getters[index] = column[2]
+        else:
+            raise ValueError('Column %d: object attribute name must either be '
+                             'a string or a callable' % index)
         return column
 
     def getValueAt(self, rowIndex, columnIndex):
-        attrname = self.__columns__[columnIndex][2]
-        return getattr(self[rowIndex], attrname)
+        line = self[rowIndex]
+        return self._getters[columnIndex](line)
 
     def setValueAt(self, aValue, rowIndex, columnIndex):
         attrname = self.__columns__[columnIndex][2]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.