- #check to see if glade file is in current directory (user must be running from download untar directory)
- _glade_files = [
- os.path.join(os.path.dirname(__file__), "gonvert.glade"),
- os.path.join(os.path.dirname(__file__), "../data/gonvert.glade"),
- os.path.join(os.path.dirname(__file__), "../lib/gonvert.glade"),
- '/usr/lib/gonvert/gonvert.glade',
- ]
- for gladePath in _glade_files:
- if os.path.isfile(gladePath):
- homepath = os.path.dirname(gladePath)
- pixmapspath = "/".join((homepath, "pixmaps"))
- widgets = gtk.glade.XML(gladePath)
- break
- else:
- return
-
- calculate = Ccalculate()
- mainWindow = widgets.get_widget('mainWindow')
-
- #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']
- mainWindow.resize(a, b)
- else:
- #Maximize if no previous size was found
- #mainWindow.maximize()
- pass
- else:
- #Maximize if no previous window.dat file was found
- #mainWindow.maximize()
- pass
-
- mainWindow.set_title('gonvert- %s - Unit Conversion Utility' % constants.__version__)
- iconPath = pixmapspath + '/gonvert.png'
- if os.path.exists(iconPath):
- mainWindow.set_icon(gtk.gdk.pixbuf_new_from_file(iconPath))
- else:
- _moduleLogger.warn("Error: Could not find gonvert icon: %s" % iconPath)
-
- #--------- connections to GUI ----------------
- dic = {
- "on_exitMenuItem_activate": _on_user_exit,
- "on_mainWindow_destroy": _on_user_exit,
- "on_categoryView_select_row": _on_click_category,
- "on_unitsView__on_click_unit_column": _on_click_unit_column,
- "on_unitValue_changed": calculate.top,
- "on_previousUnitValue_changed": calculate.bottom,
- "on_writeUnitsMenuItem_activate": _on_user_write_units,
- "on_findButton_clicked": _on_user_find_units,
- "on_findEntry_key_press_event": _on_find_key_press,
- "on_findEntry_changed": findEntry_changed,
- "on_aboutMenuItem_activate": _on_about_clicked,
- "on_about_close_clicked": _on_about_hide,
- "on_messagebox_ok_clicked": messagebox_ok_clicked,
- "on_clearSelectionMenuItem_activate": _on_user_clear_selections,
- "on_unitsView_cursor_changed": _on_click_unit,
- "on_unitsView_button_released": _on_button_released,
- "on_shortlistcheck_toggled": _on_shortlist_changed,
- "on_toggleShortList_activate": _on_edit_shortlist,
- }
-
- widgets.signal_autoconnect(dic)
- mainWindow.connect("destroy", _on_user_exit)
-
- def change_menu_label(labelname, newtext):
- item_label = widgets.get_widget(labelname).get_children()[0]
- item_label.set_text(newtext)
-
- def change_label(labelname, newtext):
- item_label = widgets.get_widget(labelname)
- item_label.set_text(newtext)
-
- change_menu_label('fileMenuItem', _('File'))
- change_menu_label('exitMenuItem', _('Exit'))
- change_menu_label('toolsMenuItem', _('Tools'))
- change_menu_label('clearSelectionMenuItem', _('Clear selections'))
- change_menu_label('writeUnitsMenuItem', _('Write Units'))
- change_menu_label('helpMenuItem', _('Help'))
- change_menu_label('aboutMenuItem', _('About'))
-
- change_menu_label('findButton', _('Find'))
-
- shortlistcheck = widgets.get_widget('shortlistcheck')
- toggleShortList = widgets.get_widget('toggleShortList')
-
- categoryView = widgets.get_widget('categoryView')
-
- unitsView = widgets.get_widget('unitsView')
- unitsView_selection = unitsView.get_selection()
-
- unitName = widgets.get_widget('unitName')
- unitValue = widgets.get_widget('unitValue')
- previousUnitName = widgets.get_widget('previousUnitName')
- previousUnitValue = widgets.get_widget('previousUnitValue')
- 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__)
-
- unitSymbol = widgets.get_widget('unitSymbol')
- previousUnitSymbol = widgets.get_widget('previousUnitSymbol')
-
- unitDescription = widgets.get_widget('unitDescription')
-
- findEntry = widgets.get_widget('findEntry')
- findLabel = widgets.get_widget('findLabel')
-
- #insert a categoryColumnumn into the units list even though the heading will not be seen
- renderer = gtk.CellRendererText()
- unitNameColumn = gtk.TreeViewColumn(_('Unit Name'), renderer)
- unitNameColumn.set_property('resizable', 1)
- unitNameColumn.add_attribute(renderer, 'text', 0)
- unitNameColumn.set_clickable(True)
- unitNameColumn.connect("clicked", _on_click_unit_column)
- unitsView.append_column(unitNameColumn)
-
- unitValueColumn = gtk.TreeViewColumn(_('Value'), renderer)
- unitValueColumn.set_property('resizable', 1)
- unitValueColumn.add_attribute(renderer, 'text', 1)
- unitValueColumn.set_clickable(True)
- unitValueColumn.connect("clicked", _on_click_unit_column)
- unitsView.append_column(unitValueColumn)
-
- unitSymbolColumn = gtk.TreeViewColumn(_('Units'), renderer)
- unitSymbolColumn.set_property('resizable', 1)
- unitSymbolColumn.add_attribute(renderer, 'text', 2)
- unitSymbolColumn.set_clickable(True)
- unitSymbolColumn.connect("clicked", _on_click_unit_column)
- unitsView.append_column(unitSymbolColumn)
-
- #Insert a column into the category list even though the heading will not be seen
- renderer = gtk.CellRendererText()
- categoryColumn = gtk.TreeViewColumn('Title', renderer)
- categoryColumn.set_property('resizable', 1)
- categoryColumn.add_attribute(renderer, 'text', 0)
- categoryView.append_column(categoryColumn)
-
- categoryModel = gtk.ListStore(gobject.TYPE_STRING)
- categoryView.set_model(categoryModel)
- #colourize each row differently for easier reading
- categoryView.set_property('rules_hint', 1)
-
- #Populate the catagories list
- keys = unit_data.list_dic.keys()
- keys.sort()
- for key in keys:
- iter = categoryModel.append()
- categoryModel.set(iter, 0, key)
-
- ToolTips = gtk.Tooltips()
- findButton = widgets.get_widget('findButton')
- ToolTips.set_tip(findButton, _(u'Find unit (F6)'))
-
- #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.
- #
- #Make a list of categories to determine which one to select
- categories = unit_data.list_dic.keys()
- categories.sort()
- #
- #If the 'selected_unts' has been stored, then extract evil_globals.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.
- if 'selected_category' in selections:
- #Match an available category to the previously selected category.
- for counter in range(len(categories)):
- if selections['selected_category'] == categories[counter]:
- # Restore the previously selected category.
- categoryView.set_cursor(counter, categoryColumn, False)
- 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
- categoryView.set_cursor(0, categoryColumn, False)
- categoryView.grab_focus()
-
- restore_units()
-
- gtk.main()
-
-
-if __name__ == "__main__":
- main()