X-Git-Url: http://git.maemo.org/git/?p=gonvert;a=blobdiff_plain;f=src%2Fgonvert_glade.py;h=c80b8b651cfc933ccfcca533e885b80594ada40a;hp=843706c38258ebedbdb652d7ad9549344ffc73c2;hb=974eff0896ef23afc6759136c01a75c654384229;hpb=a4f65fa84985ca3301b1c7255435844eca0ff2ae diff --git a/src/gonvert_glade.py b/src/gonvert_glade.py index 843706c..c80b8b6 100755 --- a/src/gonvert_glade.py +++ b/src/gonvert_glade.py @@ -14,7 +14,6 @@ import gtk.glade import gtk.gdk import constants -import evil_globals import unit_data @@ -40,6 +39,18 @@ class Gonvert(object): ] 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._find_result = [] # empty find result list + self._find_count = 0 # default to find result number zero + + self._selected_category = '' # preset to no selected category + self._selected_units = {} # empty dictionary for later use + #check to see if glade file is in current directory (user must be # running from download untar directory) for gladePath in self._glade_files: @@ -74,15 +85,9 @@ class Gonvert(object): self._unitValue = widgets.get_widget('unitValue') self._previousUnitName = widgets.get_widget('previousUnitName') self._previousUnitValue = widgets.get_widget('previousUnitValue') - self._about_box = widgets.get_widget('about_box') messagebox = widgets.get_widget('msgbox') messageboxtext = widgets.get_widget('msgboxtext') - about_image = widgets.get_widget('about_image') - about_image.set_from_file(pixmapspath +'gonvert.png') - versionlabel = widgets.get_widget('versionlabel') - versionlabel.set_text(constants.__version__) - self._unitSymbol = widgets.get_widget('unitSymbol') self._previousUnitSymbol = widgets.get_widget('previousUnitSymbol') @@ -91,6 +96,8 @@ class Gonvert(object): self._findEntry = widgets.get_widget('findEntry') self._findLabel = widgets.get_widget('findLabel') findButton = widgets.get_widget('findButton') + ToolTips = gtk.Tooltips() + ToolTips.set_tip(findButton, _(u'Find unit (F6)')) #insert a self._categoryColumnumn into the units list even though the heading will not be seen renderer = gtk.CellRendererText() @@ -128,29 +135,25 @@ class Gonvert(object): self._categoryView.set_property('rules_hint', 1) #Populate the catagories list - keys = unit_data.list_dic.keys() + keys = unit_data.UNIT_DESCRIPTIONS.keys() keys.sort() for key in keys: iter = self._categoryModel.append() self._categoryModel.set(iter, 0, key) - ToolTips = gtk.Tooltips() - ToolTips.set_tip(findButton, _(u'Find unit (F6)')) - #--------- connections to GUI ---------------- dic = { "on_exit_menu_activate": self._on_user_exit, "on_main_window_destroy": self._on_user_exit, "on_categoryView_select_row": self._on_click_category, "on_unitsView__on_click_unit_column": self._on_click_unit_column, - "on_unitValue_changed": self.top, - "on_previousUnitValue_changed": self.bottom, + "on_unitValue_changed": self._on_unit_value_changed, + "on_previousUnitValue_changed": self._on_previous_unit_value_changed, "on_writeUnitsMenuItem_activate": self._on_user_write_units, "on_findButton_clicked": self._on_user_find_units, "on_findEntry_key_press_event": self._on_find_key_press, - "on_findEntry_changed": self._findEntry_changed, + "on_findEntry_changed": self._on_findEntry_changed, "on_aboutMenuItem_activate": self._on_about_clicked, - "on_about_close_clicked": self._on_about_hide, "on_messagebox_ok_clicked": self.messagebox_ok_clicked, "on_clearSelectionMenuItem_activate": self._on_user_clear_selections, "on_unitsView_cursor_changed": self._on_click_unit, @@ -194,13 +197,13 @@ class Gonvert(object): #Restoring previous selections. # #Make a list of categories to determine which one to select - categories = unit_data.list_dic.keys() + categories = unit_data.UNIT_DESCRIPTIONS.keys() categories.sort() # - #If the 'selected_unts' has been stored, then extract evil_globals.selected_units from selections. + #If the 'selected_unts' has been stored, then extract self._selected_units from selections. if 'selected_units' in selections: - evil_globals.selected_units = selections['selected_units'] - #Make sure that the 'evil_globals.selected_category' has been stored. + self._selected_units = selections['selected_units'] + #Make sure that the 'self._selected_category' has been stored. if 'selected_category' in selections: #Match an available category to the previously selected category. for counter in range(len(categories)): @@ -220,23 +223,15 @@ class Gonvert(object): self.restore_units() def _on_shortlist_changed(self, a): - print "shortlist" - if self._shortlistcheck.get_active(): - print "1" - else: - print "0" + raise NotImplementedError("%s" % self._shortlistcheck.get_active()) def _on_edit_shortlist(self, a): - print "edit shortlist" - if self._toggleShortList.get_active(): - print "1" - else: - print "0" + raise NotImplementedError("%s" % self._toggleShortList.get_active()) def _on_user_clear_selections(self, a): selectionsDatPath = "/".join((constants._data_path_, "selections.dat")) os.remove(selectionsDatPath) - evil_globals.selected_units = {} + self._selected_units = {} def _on_user_exit(self, a): """ @@ -244,17 +239,17 @@ class Gonvert(object): should therefore only be called when exiting the program. Update selections dictionary which consists of the following keys: - 'evil_globals.selected_category': full name of selected category - 'evil_globals.selected_units': evil_globals.selected_units dictionary which contains: + 'self._selected_category': full name of selected category + 'self._selected_units': self._selected_units dictionary which contains: [categoryname: #1 displayed unit, #2 displayed unit] """ #Determine the contents of the selected category row selected, iter = self._categoryView.get_selection().get_selected() - evil_globals.selected_category = self._categoryModel.get_value(iter, 0) + self._selected_category = self._categoryModel.get_value(iter, 0) selections = { - 'selected_category': evil_globals.selected_category, - 'selected_units': evil_globals.selected_units + 'selected_category': self._selected_category, + 'selected_units': self._selected_units } selectionsDatPath = "/".join((constants._data_path_, "selections.dat")) pickle.dump(selections, open(selectionsDatPath, 'w')) @@ -269,10 +264,10 @@ class Gonvert(object): gtk.mainquit sys.exit() - def _findEntry_changed(self, a): + def _on_findEntry_changed(self, a): #Clear out find results since the user wants to look for something new - evil_globals.find_result = [] #empty find result list - evil_globals.find_count = 0 #default to find result number zero + self._find_result = [] #empty find result list + self._find_count = 0 #default to find result number zero self._findLabel.set_text('') #clear result def _on_find_key_press(self, a, b): @@ -281,14 +276,18 @@ class Gonvert(object): #Check if the key pressed was the 'Enter' key if ord(b.string[0]) == 13: #Execute the find units function - _on_user_find_units(1) + self._on_user_find_units(1) def _on_about_clicked(self, a): - self._about_box.show() - - def _on_about_hide(self, *args): - self._about_box.hide() - return gtk.TRUE + dlg = gtk.AboutDialog() + dlg.set_name(constants.__pretty_app_name__) + dlg.set_version("%s-%d" % (constants.__version__, constants.__build__)) + dlg.set_copyright("Copyright 2009 - GPL") + dlg.set_comments("") + dlg.set_website("http://unihedron.com/projects/gonvert/gonvert.php") + dlg.set_authors(["Anthony Tekatch ", "Ed Page "]) + dlg.run() + dlg.destroy() def messagebox_ok_clicked(self, a): messagebox.hide() @@ -297,25 +296,25 @@ class Gonvert(object): #check if 'new find' or 'last find' or 'next-find' #new-find = run the find algorithm which also selects the first found unit - # = evil_globals.find_count = 0 and evil_globals.find_result = [] + # = self._find_count = 0 and self._find_result = [] #last-find = restart from top again - # = evil_globals.find_count = len(evil_globals.find_result) + # = self._find_count = len(self._find_result) #next-find = continue to next found location - # = evil_globals.find_count = 0 and len(evil_globals.find_result)>0 + # = self._find_count = 0 and len(self._find_result)>0 #check for new-find - if len(evil_globals.find_result) == 0: + if len(self._find_result) == 0: find_string = string.lower(string.strip(self._findEntry.get_text())) #Make sure that a valid find string has been requested if len(find_string)>0: - categories = unit_data.list_dic.keys() + categories = unit_data.UNIT_DESCRIPTIONS.keys() categories.sort() found_a_unit = 0 #reset the 'found-a-unit' flag cat_no = 0 for category in categories: - units = unit_data.list_dic[category].keys() + units = unit_data.UNIT_DESCRIPTIONS[category].keys() units.sort() del units[0] # do not display .base_unit description key unit_no = 0 @@ -323,34 +322,34 @@ class Gonvert(object): if string.find(string.lower(unit), find_string) >= 0: found_a_unit = 1 #indicate that a unit was found #print "'", find_string, "'", " found at category = ", category, " unit = ", unit - evil_globals.find_result.append((category, unit, cat_no, unit_no)) + self._find_result.append((category, unit, cat_no, unit_no)) unit_no = unit_no+1 cat_no = cat_no+1 if found_a_unit == 1: #select the first found unit - evil_globals.find_count = 0 + self._find_count = 0 #check if next find is in a new category (prevent category changes when unnecessary - if evil_globals.selected_category != evil_globals.find_result[evil_globals.find_count][0]: - self._categoryView.set_cursor(evil_globals.find_result[0][2], self._categoryColumn, False) - self._unitsView.set_cursor(evil_globals.find_result[0][3], self._unitNameColumn, True) - if len(evil_globals.find_result)>1: - self._findLabel.set_text(('Press Find for next unit. '+ str(len(evil_globals.find_result))+' result(s).')) + if self._selected_category != self._find_result[self._find_count][0]: + self._categoryView.set_cursor(self._find_result[0][2], self._categoryColumn, False) + self._unitsView.set_cursor(self._find_result[0][3], self._unitNameColumn, True) + if len(self._find_result)>1: + self._findLabel.set_text(('Press Find for next unit. '+ str(len(self._find_result))+' result(s).')) else: self._findLabel.set_text('Text not found') #Display error else: #must be next-find or last-find #check for last-find - if evil_globals.find_count == len(evil_globals.find_result)-1: + if self._find_count == len(self._find_result)-1: #select first result - evil_globals.find_count = 0 - self._categoryView.set_cursor(evil_globals.find_result[evil_globals.find_count][2], self._categoryColumn, False) - self._unitsView.set_cursor(evil_globals.find_result[evil_globals.find_count][3], self._unitNameColumn, True) + self._find_count = 0 + self._categoryView.set_cursor(self._find_result[self._find_count][2], self._categoryColumn, False) + self._unitsView.set_cursor(self._find_result[self._find_count][3], self._unitNameColumn, True) else: #must be next-find - evil_globals.find_count = evil_globals.find_count+1 + self._find_count = self._find_count+1 #check if next find is in a new category (prevent category changes when unnecessary - if evil_globals.selected_category != evil_globals.find_result[evil_globals.find_count][0]: - self._categoryView.set_cursor(evil_globals.find_result[evil_globals.find_count][2], self._categoryColumn, False) - self._unitsView.set_cursor(evil_globals.find_result[evil_globals.find_count][3], self._unitNameColumn, True) + if self._selected_category != self._find_result[self._find_count][0]: + self._categoryView.set_cursor(self._find_result[self._find_count][2], self._categoryColumn, False) + self._unitsView.set_cursor(self._find_result[self._find_count][3], self._unitNameColumn, True) def _on_click_unit_column(self, col): """ @@ -362,19 +361,19 @@ class Gonvert(object): self._unitNameColumn.set_sort_indicator(True) self._unitValueColumn.set_sort_indicator(False) self._unitSymbolColumn.set_sort_indicator(False) - self._unitNameColumn.set_sort_order(not evil_globals.unit_sort_direction) + 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 evil_globals.value_sort_direction) + 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 evil_globals.units_sort_direction) + self._unitSymbolColumn.set_sort_order(not self._units_sort_direction) else: assert False, "Unknown column: %s" % (col.get_title(), ) @@ -397,7 +396,7 @@ class Gonvert(object): return #special sorting exceptions for ascii values (instead of float values) - if evil_globals.selected_category == "Computer Numbers": + if self._selected_category == "Computer Numbers": value_text = self._unitModel.get_value(iter, 1) else: if self._unitModel.get_value(iter, 1) == None or unit_model.get_value(iter, 1) == '': @@ -421,26 +420,26 @@ class Gonvert(object): return else: if selectedUnitColumn == 0: - if not evil_globals.unit_sort_direction: + if not self._unit_sort_direction: sorted_list.sort(lambda (x, xx, xxx), (y, yy, yyy): cmp(string.lower(x), string.lower(y))) - evil_globals.unit_sort_direction = True + self._unit_sort_direction = True else: sorted_list.sort(lambda (x, xx, xxx), (y, yy, yyy): cmp(string.lower(y), string.lower(x))) - evil_globals.unit_sort_direction = False + self._unit_sort_direction = False elif selectedUnitColumn == 1: sorted_list.sort() - if not evil_globals.value_sort_direction: - evil_globals.value_sort_direction = True + if not self._value_sort_direction: + self._value_sort_direction = True else: sorted_list.reverse() - evil_globals.value_sort_direction = False + self._value_sort_direction = False else: - if not evil_globals.units_sort_direction: + if not self._units_sort_direction: sorted_list.sort(lambda (x, xx, xxx), (y, yy, yyy): cmp(string.lower(x), string.lower(y))) - evil_globals.units_sort_direction = True + self._units_sort_direction = True else: sorted_list.sort(lambda (x, xx, xxx), (y, yy, yyy): cmp(string.lower(y), string.lower(x))) - evil_globals.units_sort_direction = False + 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) @@ -468,8 +467,6 @@ class Gonvert(object): return def _on_click_category(self, row): - global unitDataInCategory, list_dic - #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) @@ -484,24 +481,24 @@ class Gonvert(object): #Determine the contents of the selected category row selected, iter = row.get_selection().get_selected() - evil_globals.selected_category = self._categoryModel.get_value(iter, 0) + self._selected_category = self._categoryModel.get_value(iter, 0) - evil_globals.unit_sort_direction = False - evil_globals.value_sort_direction = False - evil_globals.units_sort_direction = False + self._unit_sort_direction = False + self._value_sort_direction = False + self._units_sort_direction = False self._unitNameColumn.set_sort_indicator(False) self._unitValueColumn.set_sort_indicator(False) self._unitSymbolColumn.set_sort_indicator(False) - unitDataInCategory = unit_data.list_dic[selected.get_value(iter, 0)] - keys = unitDataInCategory.keys() + self._unitDataInCategory = unit_data.UNIT_DESCRIPTIONS[selected.get_value(iter, 0)] + keys = self._unitDataInCategory.keys() keys.sort() del keys[0] # do not display .base_unit description key #Fill up the units descriptions and clear the value cells for key in keys: iter = self._unitModel.append() - self._unitModel.set(iter, 0, key, 1, '', 2, unitDataInCategory[key][1]) + self._unitModel.set(iter, 0, key, 1, '', 2, self._unitDataInCategory[key][1]) self._unitName.set_text('') self._unitValue.set_text('') @@ -513,31 +510,29 @@ class Gonvert(object): self.restore_units() def restore_units(self): - global unitDataInCategory, list_dic - # Restore the previous historical settings of previously selected units in this newly selected category #Since category has just been clicked, the list will be sorted already. - if evil_globals.selected_category in evil_globals.selected_units: - if evil_globals.selected_units[evil_globals.selected_category][0]: + if self._selected_category in self._selected_units: + if self._selected_units[self._selected_category][0]: ''"debug ''" - #evil_globals.selected_units[evil_globals.selected_category] = [selected_unit, evil_globals.selected_units[evil_globals.selected_category][0]] + #self._selected_units[self._selected_category] = [selected_unit, self._selected_units[self._selected_category][0]] - units = unit_data.list_dic[evil_globals.selected_category].keys() + units = unit_data.UNIT_DESCRIPTIONS[self._selected_category].keys() units.sort() del units[0] # do not display .base_unit description key #Restore oldest selection first. - if evil_globals.selected_units[evil_globals.selected_category][1]: + if self._selected_units[self._selected_category][1]: unit_no = 0 for unit in units: - if unit == evil_globals.selected_units[evil_globals.selected_category][1]: + if unit == self._selected_units[self._selected_category][1]: self._unitsView.set_cursor(unit_no, self._unitNameColumn, True) unit_no = unit_no+1 #Restore newest selection second. unit_no = 0 for unit in units: - if unit == evil_globals.selected_units[evil_globals.selected_category][0]: + if unit == self._selected_units[self._selected_category][0]: self._unitsView.set_cursor(unit_no, self._unitNameColumn, True) unit_no = unit_no+1 @@ -549,14 +544,14 @@ class Gonvert(object): self._on_click_unit(row) def _on_click_unit(self, row): - evil_globals.calcsuppress = 1 #suppress calculations + self._calcsuppress = True #suppress calculations #Determine the contents of the selected row. selected, iter = self._unitsView.get_selection().get_selected() selected_unit = selected.get_value(iter, 0) - unit_spec = unitDataInCategory[selected_unit] + unit_spec = self._unitDataInCategory[selected_unit] #Clear out the description text_model = gtk.TextBuffer(None) @@ -578,24 +573,24 @@ class Gonvert(object): self._unitSymbol.set_text(unit_spec[1]) # put units into label text if self._unitValue.get_text() == '': - if evil_globals.selected_category == "Computer Numbers": + if self._selected_category == "Computer Numbers": self._unitValue.set_text("0") else: self._unitValue.set_text("0.0") #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 evil_globals.selected_category in evil_globals.selected_units: - if evil_globals.selected_units[evil_globals.selected_category][0]: - evil_globals.selected_units[evil_globals.selected_category] = [selected_unit, evil_globals.selected_units[evil_globals.selected_category][0]] + if self._selected_category in self._selected_units: + if self._selected_units[self._selected_category][0]: + self._selected_units[self._selected_category] = [selected_unit, self._selected_units[self._selected_category][0]] else: - evil_globals.selected_units[evil_globals.selected_category] = [selected_unit, ''] + self._selected_units[self._selected_category] = [selected_unit, ''] # select the text so user can start typing right away self._unitValue.grab_focus() self._unitValue.select_region(0, -1) - evil_globals.calcsuppress = 0 #enable calculations + self._calcsuppress = False #enable calculations def _on_user_write_units(self, a): ''"Write the list of categories and units to stdout for documentation purposes.''" @@ -605,7 +600,7 @@ class Gonvert(object): messagebox.show() while gtk.events_pending(): gtk.mainiteration(False) - category_keys = unit_data.list_dic.keys() + category_keys = unit_data.UNIT_DESCRIPTIONS.keys() category_keys.sort() total_categories = 0 total_units = 0 @@ -616,8 +611,8 @@ class Gonvert(object): for category_key in category_keys: total_categories = total_categories + 1 print category_key, ": " - unitDataInCategory = unit_data.list_dic[category_key] - unit_keys = unitDataInCategory.keys() + 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 for unit_key in unit_keys: @@ -625,18 +620,13 @@ class Gonvert(object): print "\t", unit_key print total_categories, ' categories' print total_units, ' units' - messagebox_model = gtk.TextBuffer(None) - messageboxtext.set_buffer(messagebox_model) - messagebox_model.insert_at_cursor(_(u'The units list has been written to stdout. You can capture this printout by starting gonvert from the command line as follows: \n$ gonvert > file.txt'), -1) - - def top(self, a): - global testvalue - if evil_globals.calcsuppress == 1: - #evil_globals.calcsuppress = 0 + def _on_unit_value_changed(self, a): + if self._calcsuppress: + #self._calcsuppress = False return # determine if value to be calculated is empty - if evil_globals.selected_category == "Computer Numbers": + if self._selected_category == "Computer Numbers": if self._unitValue.get_text() == '': value = '0' else: @@ -648,10 +638,10 @@ class Gonvert(object): value = float(self._unitValue.get_text()) if self._unitName.get_text() != '': - func, arg = unitDataInCategory[self._unitName.get_text()][0] #retrieve the conversion function and value from the selected unit + 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 = unitDataInCategory.keys() + keys = self._unitDataInCategory.keys() keys.sort() del keys[0] row = 0 @@ -661,7 +651,7 @@ class Gonvert(object): while iter: #get the formula from the name at the row - func, arg = unitDataInCategory[self._unitModel.get_value(iter, 0)][0] + 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, )))) @@ -671,17 +661,17 @@ class Gonvert(object): # if the second row has a unit then update its value if self._previousUnitName.get_text() != '': - evil_globals.calcsuppress = 1 - func, arg = unitDataInCategory[self._previousUnitName.get_text()][0] + self._calcsuppress = True + func, arg = self._unitDataInCategory[self._previousUnitName.get_text()][0] self._previousUnitValue.set_text(str(apply(func.from_base, (base, arg, )))) - evil_globals.calcsuppress = 0 + self._calcsuppress = False - def bottom(self, a): - if evil_globals.calcsuppress == 1: - #evil_globals.calcsuppress = 0 + 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 evil_globals.selected_category == "Computer Numbers": + if self._selected_category == "Computer Numbers": if self._previousUnitValue.get_text() == '': value = '0' else: @@ -693,10 +683,10 @@ class Gonvert(object): value = float(self._previousUnitValue.get_text()) if self._previousUnitName.get_text() != '': - func, arg = unitDataInCategory[self._previousUnitName.get_text()][0] #retrieve the conversion function and value from the selected unit + 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 = unitDataInCategory.keys() + keys = self._unitDataInCategory.keys() keys.sort() del keys[0] row = 0 @@ -706,7 +696,7 @@ class Gonvert(object): while iter: #get the formula from the name at the row - func, arg = unitDataInCategory[self._unitModel.get_value(iter, 0)][0] + 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, )))) @@ -716,10 +706,10 @@ class Gonvert(object): # if the second row has a unit then update its value if self._unitName.get_text() != '': - evil_globals.calcsuppress = 1 - func, arg = unitDataInCategory[self._unitName.get_text()][0] + self._calcsuppress = True + func, arg = self._unitDataInCategory[self._unitName.get_text()][0] self._unitValue.set_text(str(apply(func.from_base, (base, arg, )))) - evil_globals.calcsuppress = 0 + self._calcsuppress = False def main():