-def clear_selections(a):
- selectionsDatPath = "/".join((constants._data_path_, "selections.dat"))
- os.remove(selectionsDatPath)
- evil_globals.selected_units={}
-
-
-def exitprogram(a):
- """
- This routine saves the selections to a file, and
- 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:
- [categoryname: #1 displayed unit, #2 displayed unit]
- """
- #Determine the contents of the selected category row
- selected,iter= cat_clist.get_selection().get_selected()
- evil_globals.selected_category = cat_model.get_value(iter,0)
-
- selections = {'evil_globals.selected_category':evil_globals.selected_category, 'evil_globals.selected_units':evil_globals.selected_units}
- selectionsDatPath = "/".join((constants._data_path_, "selections.dat"))
- pickle.dump(selections, open(selectionsDatPath,'w'))
-
- #Get last size of app and save it
- window_settings = {'size':evil_globals.window_size}
- windowDatPath = "/".join((constants._data_path_, "window.dat"))
- pickle.dump(window_settings, open(windowDatPath,'w'))
-
- gtk.mainquit
- sys.exit()
-
-
-def find_entry_changed(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
- find_label.set_text('') #clear result
-
-
-def find_key_press(a,b):
- #Check if the key pressed was an ASCII key
- if len(b.string)>0:
- #Check if the key pressed was the 'Enter' key
- if ord(b.string[0])==13:
- #Execute the find units function
- find_units(1)
-
-
-def about_clicked(a):
- about_box.show()
-
-
-def about_hide(*args):
- about_box.hide()
- return gtk.TRUE
-
-
-def messagebox_ok_clicked(a):
- messagebox.hide()
-
-
-def find_units(a):
- global column1
- global col
- #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=[]
-
- #last-find = restart from top again
- # = evil_globals.find_count=len(evil_globals.find_result)
-
- #next-find = continue to next found location
- # = evil_globals.find_count=0 and len(evil_globals.find_result)>0
-
- #check for new-find
- if len(evil_globals.find_result)==0:
- find_string = string.lower(string.strip(find_entry.get_text()))
- #Make sure that a valid find string has been requested
- if len(find_string)>0:
- categories = unit_data.list_dic.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.sort()
- del units[0] # do not display .base_unit description key
- unit_no=0
- for unit in units:
- 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))
- 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
- #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]:
- cat_clist.set_cursor(evil_globals.find_result[0][2],col,False)
- unitsView.set_cursor(evil_globals.find_result[0][3],column1,True)
- if len(evil_globals.find_result)>1:
- find_label.set_text(('Press Find for next unit. '+ str(len(evil_globals.find_result))+' result(s).'))
- else:
- find_label.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:
- #select first result
- evil_globals.find_count=0
- cat_clist.set_cursor(evil_globals.find_result[evil_globals.find_count][2],col,False)
- unitsView.set_cursor(evil_globals.find_result[evil_globals.find_count][3],column1,True)
- else: #must be next-find
- evil_globals.find_count=evil_globals.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]:
- cat_clist.set_cursor(evil_globals.find_result[evil_globals.find_count][2],col,False)
- unitsView.set_cursor(evil_globals.find_result[evil_globals.find_count][3],column1,True)
-
-
-def click_column(col):
- ''"Sort the contents of the column when the user clicks on the title.''"
- global column1, column2, unit_model
-
- #Determine which column requires sorting
- if col.get_title()==_(u"Unit Name"):
- selected_column=0
- column1.set_sort_indicator(True)
- column2.set_sort_indicator(False)
- column3.set_sort_indicator(False)
- column1.set_sort_order(not evil_globals.unit_sort_direction)
- elif col.get_title()==_(u"Value"):
- selected_column=1
- column1.set_sort_indicator(False)
- column2.set_sort_indicator(True)
- column3.set_sort_indicator(False)
- column2.set_sort_order(not evil_globals.value_sort_direction)
- else:
- selected_column=2
- column1.set_sort_indicator(False)
- column2.set_sort_indicator(False)
- column3.set_sort_indicator(True)
- column3.set_sort_order(not evil_globals.units_sort_direction)
-
- #declare a spot to hold the sorted list
- sorted_list = []
-
- #point to the first row
- iter=unit_model.get_iter_first()
- row=0
-
- while (iter):
- #grab all text from columns for sorting
-
- #get the text from each column
- unit_text = unit_model.get_value(iter,0)
- units_text = unit_model.get_value(iter,2)
-
- #do not bother sorting if the value column is empty
- if unit_model.get_value(iter,1)=='' and selected_column==1:
+ self._mainWindow = widgets.get_widget('mainWindow')
+ self._app = hildonize.get_app_class()()
+ self._mainWindow = hildonize.hildonize_window(self._app, self._mainWindow)
+
+ change_menu_label(widgets, 'fileMenuItem', _('File'))
+ change_menu_label(widgets, 'exitMenuItem', _('Exit'))
+ change_menu_label(widgets, 'toolsMenuItem', _('Tools'))
+ change_menu_label(widgets, 'clearSelectionMenuItem', _('Clear selections'))
+ change_menu_label(widgets, 'helpMenuItem', _('Help'))
+ change_menu_label(widgets, 'aboutMenuItem', _('About'))
+ change_menu_label(widgets, 'findButton', _('Find'))
+
+ self._shortlistcheck = widgets.get_widget('shortlistcheck')
+ self._toggleShortList = widgets.get_widget('toggleShortList')
+
+ self._categoryView = widgets.get_widget('categoryView')
+
+ self._unitsView = widgets.get_widget('unitsView')
+ self._unitsView.set_property('rules_hint', 1)
+ self._unitsView_selection = self._unitsView.get_selection()
+ if hildonize.IS_HILDON_SUPPORTED or FORCE_HILDON_LIKE:
+ self._unitsView.set_headers_visible(False)
+
+ self._unitName = widgets.get_widget('unitName')
+ self._unitValue = widgets.get_widget('unitValue')
+ self._previousUnitName = widgets.get_widget('previousUnitName')
+ self._previousUnitValue = widgets.get_widget('previousUnitValue')
+ if hildonize.IS_HILDON_SUPPORTED or FORCE_HILDON_LIKE:
+ self._previousUnitName.get_parent().hide()
+
+ self._unitSymbol = widgets.get_widget('unitSymbol')
+ self._previousUnitSymbol = widgets.get_widget('previousUnitSymbol')
+
+ self._unitDescription = widgets.get_widget('unitDescription')
+ if hildonize.IS_HILDON_SUPPORTED or FORCE_HILDON_LIKE:
+ self._unitDescription.get_parent().get_parent().hide()
+
+ self._searchLayout = widgets.get_widget('searchLayout')
+ self._searchLayout.hide()
+ self._findEntry = widgets.get_widget('findEntry')
+ self._findLabel = widgets.get_widget('findLabel')
+ self._findButton = widgets.get_widget('findButton')
+ ToolTips = gtk.Tooltips()
+ ToolTips.set_tip(self._findButton, _(u'Find unit (F6)'))
+
+ #insert a self._categoryColumnumn into the units list even though the heading will not be seen
+ renderer = gtk.CellRendererText()
+ self._unitNameColumn = gtk.TreeViewColumn(_('Unit Name'), renderer)
+ self._unitNameColumn.set_property('resizable', 1)
+ self._unitNameColumn.add_attribute(renderer, 'text', 0)
+ self._unitNameColumn.set_clickable(True)
+ self._unitNameColumn.connect("clicked", self._on_click_unit_column)
+ self._unitsView.append_column(self._unitNameColumn)
+
+ self._unitValueColumn = gtk.TreeViewColumn(_('Value'), renderer)
+ self._unitValueColumn.set_property('resizable', 1)
+ self._unitValueColumn.add_attribute(renderer, 'text', 1)
+ self._unitValueColumn.set_clickable(True)
+ self._unitValueColumn.connect("clicked", self._on_click_unit_column)
+ self._unitsView.append_column(self._unitValueColumn)
+
+ self._unitSymbolColumn = gtk.TreeViewColumn(_('Units'), renderer)
+ self._unitSymbolColumn.set_property('resizable', 1)
+ self._unitSymbolColumn.add_attribute(renderer, 'text', 2)
+ self._unitSymbolColumn.set_clickable(True)
+ self._unitSymbolColumn.connect("clicked", self._on_click_unit_column)
+ self._unitsView.append_column(self._unitSymbolColumn)
+
+ self._unitModel = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
+ self._sortedUnitModel = gtk.TreeModelSort(self._unitModel)
+ columns = self._get_column_sort_stuff()
+ for columnIndex, (column, sortDirection, col_cmp) in enumerate(columns):
+ self._sortedUnitModel.set_sort_func(columnIndex, col_cmp)
+ self._unitsView.set_model(self._sortedUnitModel)
+
+ #Insert a column into the category list even though the heading will not be seen
+ renderer = gtk.CellRendererText()
+ self._categoryColumn = gtk.TreeViewColumn('Title', renderer)
+ self._categoryColumn.set_property('resizable', 1)
+ self._categoryColumn.add_attribute(renderer, 'text', 0)
+ self._categoryView.append_column(self._categoryColumn)
+
+ self._categoryModel = gtk.ListStore(gobject.TYPE_STRING)
+ self._categoryView.set_model(self._categoryModel)
+ #colourize each row differently for easier reading
+ self._categoryView.set_property('rules_hint', 1)
+
+ #Populate the catagories list
+ for key in unit_data.UNIT_CATEGORIES:
+ iter = self._categoryModel.append()
+ self._categoryModel.set(iter, 0, key)
+
+ #--------- 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_unitValue_changed": self._on_unit_value_changed,
+ "on_previousUnitValue_changed": self._on_previous_unit_value_changed,
+ "on_findButton_clicked": self._on_find_activate,
+ "on_findEntry_activated": self._on_find_activate,
+ "on_findEntry_changed": self._on_findEntry_changed,
+ "on_aboutMenuItem_activate": self._on_about_clicked,
+ "on_clearSelectionMenuItem_activate": self._on_user_clear_selections,
+ "on_unitsView_cursor_changed": self._on_click_unit,
+ "on_shortlistcheck_toggled": self._on_shortlist_changed,
+ "on_toggleShortList_activate": self._on_edit_shortlist,
+ }
+ widgets.signal_autoconnect(dic)
+ self._mainWindow.connect("key-press-event", self._on_key_press)
+ self._mainWindow.connect("window-state-event", self._on_window_state_change)
+
+ self._mainWindow.set_title('gonvert- %s - Unit Conversion Utility' % constants.__version__)
+ iconPath = pixmapspath + '/gonvert.png'
+ if os.path.exists(iconPath):
+ self._mainWindow.set_icon(gtk.gdk.pixbuf_new_from_file(iconPath))
+ else:
+ _moduleLogger.warn("Error: Could not find gonvert icon: %s" % iconPath)
+
+ self._load_settings()
+
+ def _load_settings(self):
+ #Restore window size from previously saved settings if it exists and is valid.
+ windowDatPath = "/".join((constants._data_path_, "window.dat"))
+ if os.path.exists(windowDatPath):
+ #Retrieving previous window settings from ~/.gonvert/window.dat
+ saved_window = pickle.load(open(windowDatPath, "r"))
+ #If the 'size' has been stored, then extract size from saved_window.
+ if 'size' in saved_window:
+ a, b = saved_window['size']
+ self._mainWindow.resize(a, b)
+
+ #Restore selections from previously saved settings if it exists and is valid.
+ historical_catergory_found = False
+ selectionsDatPath = "/".join((constants._data_path_, "selections.dat"))
+ if os.path.exists(selectionsDatPath):
+ #Retrieving previous selections from ~/.gonvert/selections.dat
+ selections = pickle.load(open(selectionsDatPath, 'r'))
+ #Restoring previous selections.
+ #If the 'selected_unts' has been stored, then extract self._defaultUnitForCategory from selections.
+ if 'selected_units' in selections:
+ self._defaultUnitForCategory = selections['selected_units']
+ #Make sure that the 'self._selectedCategoryName' has been stored.
+ if 'selected_category' in selections:
+ #Match an available category to the previously selected category.
+ for counter in range(len(unit_data.UNIT_CATEGORIES)):
+ if selections['selected_category'] == unit_data.UNIT_CATEGORIES[counter]:
+ # Restore the previously selected category.
+ self._categoryView.set_cursor(counter, self._categoryColumn, False)
+ self._categoryView.grab_focus()
+ historical_catergory_found = True
+
+ if not historical_catergory_found:
+ print "Couldn't find saved category, using default."
+ #If historical records were not kept then default to
+ # put the focus on the first category
+ self._categoryView.set_cursor(0, self._categoryColumn, False)
+ self._categoryView.grab_focus()
+
+ self._select_default_unit()
+
+ def _save_settings(self):
+ """
+ This routine saves the selections to a file, and
+ should therefore only be called when exiting the program.
+
+ Update selections dictionary which consists of the following keys:
+ 'self._selectedCategoryName': full name of selected category
+ 'self._defaultUnitForCategory': self._defaultUnitForCategory 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()
+ self._selectedCategoryName = self._categoryModel.get_value(iter, 0)
+
+ selections = {
+ 'selected_category': self._selectedCategoryName,
+ 'selected_units': self._defaultUnitForCategory
+ }
+ selectionsDatPath = "/".join((constants._data_path_, "selections.dat"))
+ pickle.dump(selections, open(selectionsDatPath, 'w'))
+
+ #Get last size of app and save it
+ window_settings = {
+ 'size': self._mainWindow.get_size()
+ }
+ windowDatPath = "/".join((constants._data_path_, "window.dat"))
+ pickle.dump(window_settings, open(windowDatPath, 'w'))
+
+ def _clear_find(self):
+ # switch to "new find" state
+ self._find_result = []
+ self._findIndex = 0
+
+ # Clear our user message
+ self._findLabel.set_text('')
+
+ def _find_first(self):
+ assert len(self._find_result) == 0
+ assert self._findIndex == 0
+ findString = self._findEntry.get_text().strip().lower()
+ if not findString: