- #Determine which column requires sorting
- if col is self._unitNameColumn:
- selectedUnitColumn = 0
- self._unitNameColumn.set_sort_indicator(True)
- self._unitValueColumn.set_sort_indicator(False)
- self._unitSymbolColumn.set_sort_indicator(False)
- self._unitNameColumn.set_sort_order(not self._unit_sort_direction)
- elif col is self._unitValueColumn:
- selectedUnitColumn = 1
- self._unitNameColumn.set_sort_indicator(False)
- self._unitValueColumn.set_sort_indicator(True)
- self._unitSymbolColumn.set_sort_indicator(False)
- self._unitValueColumn.set_sort_order(not self._value_sort_direction)
- elif col is self._unitSymbolColumn:
- selectedUnitColumn = 2
- self._unitNameColumn.set_sort_indicator(False)
- self._unitValueColumn.set_sort_indicator(False)
- self._unitSymbolColumn.set_sort_indicator(True)
- self._unitSymbolColumn.set_sort_order(not self._units_sort_direction)
- else:
- assert False, "Unknown column: %s" % (col.get_title(), )
-
- #declare a spot to hold the sorted list
- sorted_list = []
-
- #point to the first row
- iter = self._unitModel.get_iter_first()
- row = 0
-
- while iter:
- #grab all text from columns for sorting
-
- #get the text from each column
- unit_text = self._unitModel.get_value(iter, 0)
- units_text = self._unitModel.get_value(iter, 2)
-
- #do not bother sorting if the value column is empty
- if self._unitModel.get_value(iter, 1) == '' and selectedUnitColumn == 1:
- return
-
- #special sorting exceptions for ascii values (instead of float values)
- if self._selected_category == "Computer Numbers":
- value_text = self._unitModel.get_value(iter, 1)
- else:
- if self._unitModel.get_value(iter, 1) == None or self._unitModel.get_value(iter, 1) == '':
- value_text = ''
- else:
- value_text = float(self._unitModel.get_value(iter, 1))
-
- if selectedUnitColumn == 0:
- sorted_list.append((unit_text, value_text, units_text))
- elif selectedUnitColumn == 1:
- sorted_list.append((value_text, unit_text, units_text))
- else:
- sorted_list.append((units_text, value_text, unit_text))
-
- #point to the next row in the self._unitModel
- iter = self._unitModel.iter_next(iter)
- row = row+1
-
- #check if no calculations have been made yet (don't bother sorting)
- if row == 0:
- return
- else:
- if selectedUnitColumn == 0:
- if not self._unit_sort_direction:
- sorted_list.sort(lambda (x, xx, xxx), (y, yy, yyy): cmp(string.lower(x), string.lower(y)))
- self._unit_sort_direction = True
- else:
- sorted_list.sort(lambda (x, xx, xxx), (y, yy, yyy): cmp(string.lower(y), string.lower(x)))
- self._unit_sort_direction = False
- elif selectedUnitColumn == 1:
- sorted_list.sort()
- if not self._value_sort_direction:
- self._value_sort_direction = True
- else:
- sorted_list.reverse()
- self._value_sort_direction = False
- else:
- if not self._units_sort_direction:
- sorted_list.sort(lambda (x, xx, xxx), (y, yy, yyy): cmp(string.lower(x), string.lower(y)))
- self._units_sort_direction = True
- else:
- sorted_list.sort(lambda (x, xx, xxx), (y, yy, yyy): cmp(string.lower(y), string.lower(x)))
- self._units_sort_direction = False
-
- #Clear out the previous list of units
- self._unitModel = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
- self._unitsView.set_model(self._unitModel)
-
- #colourize each row differently for easier reading
- self._unitsView.set_property('rules_hint', 1)
-
- #Clear out the description
- text_model = gtk.TextBuffer(None)
- self._unitDescription.set_buffer(text_model)
-
- if selectedUnitColumn == 0:
- for unit, value, units in sorted_list:
- iter = self._unitModel.append()
- self._unitModel.set(iter, 0, unit, 1, str(value), 2, units)
- elif selectedUnitColumn == 1:
- for value, unit, units in sorted_list:
- iter = self._unitModel.append()
- self._unitModel.set(iter, 0, unit, 1, str(value), 2, units)