def __init__(self):
self._unitDataInCategory = None
- self._calcsuppress = False
self._unit_sort_direction = False
self._value_sort_direction = False
self._units_sort_direction = False
self._unitValue.grab_focus()
self._unitValue.select_region(0, -1)
+ def _sanitize_value(self, userEntry):
+ if self._selectedCategory == "Computer Numbers":
+ if userEntry == '':
+ value = '0'
+ else:
+ value = userEntry
+ else:
+ if userEntry == '':
+ value = 0.0
+ else:
+ value = float(userEntry)
+ return value
+
def _on_shortlist_changed(self, *args):
try:
raise NotImplementedError("%s" % self._shortlistcheck.get_active())
except Exception:
_moduleLogger.exception("")
- def _on_find_activate(self, a):
+ def _on_find_activate(self, *args):
try:
self._find_next()
self._findButton.grab_focus()
except Exception:
_moduleLogger.exception("")
- def _on_click_unit(self, row):
- self._calcsuppress = True #suppress calculations
+ def _on_click_unit(self, *args):
+ try:
+ selected, iter = self._unitsView.get_selection().get_selected()
+ selected_unit = selected.get_value(iter, 0)
+ unit_spec = self._unitDataInCategory[selected_unit]
- #Determine the contents of the selected row.
- selected, iter = self._unitsView.get_selection().get_selected()
+ if self._unitName.get_text() != selected_unit:
+ self._previousUnitName.set_text(self._unitName.get_text())
+ self._previousUnitValue.set_text(self._unitValue.get_text())
+ self._previousUnitSymbol.set_text(self._unitSymbol.get())
- selected_unit = selected.get_value(iter, 0)
+ self._unitName.set_text(selected_unit)
+ self._unitValue.set_text(selected.get_value(iter, 1))
+ buffer = self._unitDescription.get_buffer()
+ buffer.set_text(unit_spec[2])
+ self._unitSymbol.set_text(unit_spec[1]) # put units into label text
- unit_spec = self._unitDataInCategory[selected_unit]
+ if self._unitValue.get_text() == '':
+ if self._selectedCategory == "Computer Numbers":
+ self._unitValue.set_text("0")
+ else:
+ self._unitValue.set_text("0.0")
- #Clear out the description
- text_model = gtk.TextBuffer(None)
- self._unitDescription.set_buffer(text_model)
+ self._defaultUnitForCategory[self._selectedCategory] = [
+ self._unitName.get_text(), self._previousUnitName.get_text()
+ ]
- enditer = text_model.get_end_iter()
- text_model.insert(enditer, unit_spec[2])
+ # select the text so user can start typing right away
+ self._unitValue.grab_focus()
+ self._unitValue.select_region(0, -1)
+ except Exception:
+ _moduleLogger.exception("")
- if self._unitName.get_text() != selected_unit:
- self._previousUnitName.set_text(self._unitName.get_text())
- self._previousUnitValue.set_text(self._unitValue.get_text())
- if self._unitSymbol.get() == None:
- self._previousUnitSymbol.set_text('')
- else:
- self._previousUnitSymbol.set_text(self._unitSymbol.get())
- self._unitName.set_text(selected_unit)
+ def _on_unit_value_changed(self, *args):
+ try:
+ if self._unitName.get_text() == '':
+ return
+ if not self._unitValue.is_focus():
+ return
- self._unitValue.set_text(selected.get_value(iter, 1))
+ value = self._sanitize_value(self._unitValue.get_text())
- self._unitSymbol.set_text(unit_spec[1]) # put units into label text
- if self._unitValue.get_text() == '':
- if self._selectedCategory == "Computer Numbers":
- self._unitValue.set_text("0")
- else:
- self._unitValue.set_text("0.0")
+ #retrieve the conversion function and value from the selected unit
+ func, arg = self._unitDataInCategory[self._unitName.get_text()][0]
+ base = func.to_base(value, arg)
- #For historical purposes, record this unit as the most recent one in this category.
- # Also, if a previous unit exists, then shift that previous unit to oldest unit.
- if self._selectedCategory in self._defaultUnitForCategory:
- if self._defaultUnitForCategory[self._selectedCategory][0]:
- self._defaultUnitForCategory[self._selectedCategory] = [selected_unit, self._defaultUnitForCategory[self._selectedCategory][0]]
- else:
- self._defaultUnitForCategory[self._selectedCategory] = [selected_unit, '']
+ #point to the first row
+ for row in self._unitModel:
+ func, arg = self._unitDataInCategory[row[0]][0]
+ row[1] = str(func.from_base(base, arg))
- # select the text so user can start typing right away
- self._unitValue.grab_focus()
- self._unitValue.select_region(0, -1)
+ if self._previousUnitName.get_text() != '':
+ func, arg = self._unitDataInCategory[self._previousUnitName.get_text()][0]
+ self._previousUnitValue.set_text(str(func.from_base(base, arg, )))
+ except Exception:
+ _moduleLogger.exception("")
+
+ def _on_previous_unit_value_changed(self, *args):
+ try:
+ if self._previousUnitName.get_text() == '':
+ return
+ if not self._previousUnitValue.is_focus():
+ return
+
+ value = self._sanitize_value(self._previousUnitValue.get_text())
+
+ #retrieve the conversion function and value from the selected unit
+ func, arg = self._unitDataInCategory[self._previousUnitName.get_text()][0]
+ base = func.to_base(value, arg)
- self._calcsuppress = False #enable calculations
+ #point to the first row
+ for row in self._unitModel:
+ func, arg = self._unitDataInCategory[row[0]][0]
+ row[1] = str(func.from_base(base, arg))
+
+ func, arg = self._unitDataInCategory[self._unitName.get_text()][0]
+ self._unitValue.set_text(str(func.from_base(base, arg, )))
+ except Exception:
+ _moduleLogger.exception("")
def messagebox_ok_clicked(self, a):
messagebox.hide()
total_categories = total_categories + 1
print category_key, ": "
self._unitDataInCategory = unit_data.UNIT_DESCRIPTIONS[category_key]
- unit_keys = self._unitDataInCategory.keys()
- unit_keys.sort()
- del unit_keys[0] # do not display .base_unit description key
+ unit_keys = unit_data.get_units_from_category(self._unitDataInCategory)
for unit_key in unit_keys:
total_units = total_units + 1
print "\t", unit_key
print total_categories, ' categories'
print total_units, ' units'
- def _on_unit_value_changed(self, a):
- if self._calcsuppress:
- #self._calcsuppress = False
- return
- # determine if value to be calculated is empty
- if self._selectedCategory == "Computer Numbers":
- if self._unitValue.get_text() == '':
- value = '0'
- else:
- value = self._unitValue.get_text()
- else:
- if self._unitValue.get_text() == '':
- value = 0.0
- else:
- value = float(self._unitValue.get_text())
-
- if self._unitName.get_text() != '':
- func, arg = self._unitDataInCategory[self._unitName.get_text()][0] #retrieve the conversion function and value from the selected unit
- base = apply(func.to_base, (value, arg, )) #determine the base unit value
-
- keys = self._unitDataInCategory.keys()
- keys.sort()
- del keys[0]
- row = 0
-
- #point to the first row
- iter = self._unitModel.get_iter_first()
-
- while iter:
- #get the formula from the name at the row
- func, arg = self._unitDataInCategory[self._unitModel.get_value(iter, 0)][0]
-
- #set the result in the value column
- self._unitModel.set(iter, 1, str(apply(func.from_base, (base, arg, ))))
-
- #point to the next row in the self._unitModel
- iter = self._unitModel.iter_next(iter)
-
- # if the second row has a unit then update its value
- if self._previousUnitName.get_text() != '':
- self._calcsuppress = True
- func, arg = self._unitDataInCategory[self._previousUnitName.get_text()][0]
- self._previousUnitValue.set_text(str(apply(func.from_base, (base, arg, ))))
- self._calcsuppress = False
-
- def _on_previous_unit_value_changed(self, a):
- if self._calcsuppress == True:
- #self._calcsuppress = False
- return
- # determine if value to be calculated is empty
- if self._selectedCategory == "Computer Numbers":
- if self._previousUnitValue.get_text() == '':
- value = '0'
- else:
- value = self._previousUnitValue.get_text()
- else:
- if self._previousUnitValue.get_text() == '':
- value = 0.0
- else:
- value = float(self._previousUnitValue.get_text())
-
- if self._previousUnitName.get_text() != '':
- func, arg = self._unitDataInCategory[self._previousUnitName.get_text()][0] #retrieve the conversion function and value from the selected unit
- base = apply(func.to_base, (value, arg, )) #determine the base unit value
-
- keys = self._unitDataInCategory.keys()
- keys.sort()
- del keys[0]
- row = 0
-
- #point to the first row
- iter = self._unitModel.get_iter_first()
-
- while iter:
- #get the formula from the name at the row
- func, arg = self._unitDataInCategory[self._unitModel.get_value(iter, 0)][0]
-
- #set the result in the value column
- self._unitModel.set(iter, 1, str(apply(func.from_base, (base, arg, ))))
-
- #point to the next row in the self._unitModel
- iter = self._unitModel.iter_next(iter)
-
- # if the second row has a unit then update its value
- if self._unitName.get_text() != '':
- self._calcsuppress = True
- func, arg = self._unitDataInCategory[self._unitName.get_text()][0]
- self._unitValue.set_text(str(apply(func.from_base, (base, arg, ))))
- self._calcsuppress = False
-
def _on_about_clicked(self, a):
dlg = gtk.AboutDialog()
dlg.set_name(constants.__pretty_app_name__)