#!/usr/bin/env python # vim:encoding=UTF-8 COPYRIGHT = "© 2007 Bart Kreska " LICENSE = """ CarCalc is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. CarCalc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.""" PROGNAME = 'CarCalc' VERSION = '0.1' try: import pygtk try: pygtk.require('2.0') except AssertionError: pygtkErr() import gtk except ImportError: pygtkErr() # Power def PS2kW(x): return (x * 0.735498) def kW2PS(x): return (x * 1.3596) def PS2HP(x): return (x * 0.9863199008) def HP2PS(x): return (x * 1.01386984) def HP2kW(x): return (x * 0.7457) def kW2HP(x): return (x * 1.341021859) # Length def mm2cm(x): return (x * 0.1) def mm2m(x): return (x * 0.001) def mm2km(x): return (x * 0.000001) def mm2inches(x): return (x * 0.0393700787401575) def mm2feet(x): return (x * 0.003281) def mm2yards(x): return (x * 0.001094) def mm2miles(x): return (x * 0.000000621) def cm2mm(x): return (x * 10) def cm2m(x): return (x * 0.01) def cm2km(x): return (x * 0.00001) def cm2inches(x): return (x * 0.3937) def cm2feet(x): return (x * 0.0328084) def cm2yards(x): return (x * 0.0109361) def cm2miles(x): return (x * 0.000006214) def m2mm(x): return (x * 1000) def m2cm(x): return (x * 100) def m2km(x): return (x * 0.001) def m2inches(x): return (x * 39.37) def m2feet(x): return (x * 3.281) def m2yards(x): return (x * 1.094) def m2miles(x): return (x * 0.000621371) def km2mm(x): return (x * 1000000) def km2cm(x): return (x * 100000) def km2m(x): return (x * 1000) def km2inches(x): return (x * 39370) def km2feet(x): return (x * 3280.84) def km2yards(x): return (x * 1093.613) def km2miles(x): return (x * 0.621) def inches2mm(x): return (x * 25.4) def inches2cm(x): return (x * 2.54) def inches2m(x): return (x * 0.0254) def inches2km(x): return (x * 0.0000254) def inches2feet(x): return (x * 0.0833333) def inches2yards(x): return (x * 0.0277778) def inches2miles(x): return (x * 0.000015783) def feet2mm(x): return (x * 304.8) def feet2cm(x): return (x * 30.48) def feet2m(x): return (x * 0.3048) def feet2km(x): return (x * 0.0003048) def feet2inches(x): return (x * 12) def feet2yards(x): return (x * 0.3333) def feet2miles(x): return (x * 0.000189394) def yards2mm(x): return (x * 914.4) def yards2cm(x): return (x * 91.44) def yards2m(x): return (x * 0.914) def yards2km(x): return (x * 0.0009144) def yards2inches(x): return (x * 36) def yards2feet(x): return (x * 3) def yards2miles(x): return (x * 0.000568182) def miles2mm(x): return (x * 0.000001609) def miles2cm(x): return (x * 0.00001609) def miles2m(x): return (x * 1609.344) def miles2km(x): return (x * 1.6093) def miles2inches(x): return (x * 0,06336) def miles2feet(x): return (x * 5280) def miles2yards(x): return (x * 1760) # Displacement def cu_cm2cu_inches(x): return (x * 0.061023378) def cu_cm2litres(x): return (x * 0.001) def cu_inches2cu_cm(x): return (x * 16.387064) def cu_inches2litres(x): return (x * 0.61023378) def litres2cu_cm(x): return (x * 1000) def litres2cu_inches(x): return (x * 61.023378) # Presure def atm2Pa(x): return (x * 101325) def atm2MPa(x): return (x * 0.101325) def atm2bar(x): return (x * 1.01325) def atm2psi(x): return (x * 14.696) def Pa2atm(x): return (x * 0.000009869) def Pa2MPa(x): return (x * 0.000001) def Pa2bar(x): return (x * 0.00001) def Pa2psi(x): return (x * 0.00014504) def MPa2atm(x): return (x * 9.869) def MPa2Pa(x): return (x * 1000000) def MPa2bar(x): return (x * 10) def MPa2psi(x): return (x * 145.04) def bar2atm(x): return (x * 0.98692) def bar2Pa(x): return (x * 100000) def bar2MPa(x): return (x * 0.1) def bar2psi(x): return (x * 14.504) def psi2atm(x): return (x * 0.068046) def psi2Pa(x): return (x * 6894.76) def psi2MPa(x): return (x * 0.00689476) def psi2bar(x): return (x * 0.068948) # Speed def mps2kph(x): return (m2km(x) * 3600) def mps2mph(x): return (m2miles(x) * 3600) def kph2mps(x): return (km2m(x) / 3600) def kph2mph(x): return km2miles(x) def mph2mps(x): return (miles2m(x) / 3600) def mph2kph(x): return miles2km(x) # Weight def g2dag(x): return (x * 0.1) def g2kg(x): return (x * 0.001) def g2t__metric_(x): return (x * 0.000001) def g2oz(x): return (x * 0.035273961) def g2lbs(x): return (x * 0.002204622) def g2tons__US_(x): return (x * 0.000001102) def dag2g(x): return (x * 10) def dag2kg(x): return (x * 0.01) def dag2t__metric_(x): return (x * 0.00001) def dag2oz(x): return (x * 0.35273961) def dag2lbs(x): return (x * 0.02204622) def dag2tons__US_(x): return (x * 0.00001102) def kg2g(x): return (x * 1000) def kg2dag(x): return (x * 100) def kg2t__metric_(x): return (x * 0.001) def kg2oz(x): return (x * 35.27396194) def kg2lbs(x): return (x * 2.20462262) def kg2tons__US_(x): return (x * 0.00110231) def t__metric_2g(x): return (x * 1000000) def t__metric_2dag(x): return (x * 100000) def t__metric_2kg(x): return (x * 1000) def t__metric_2oz(x): return (x * 35273.96194958) def t__metric_2lbs(x): return (x * 2204.62262184) def t__metric_2tons__US_(x): return (x * 1.10231131) def oz2g(x): return (x * 28.34952312) def oz2dag(x): return (x * 2.834952312) def oz2kg(x): return (x * 0.02834952312) def oz2t__metric_(x): return (x * 0.00002834952312) def oz2lbs(x): return (x * 0.0625) def oz2tons__US_(x): return (x * 1) def lbs2g(x): return (x * 453.59237) def lbs2dag(x): return (x * 45.359237) def lbs2kg(x): return (x * 0.45359237) def lbs2t__metric_(x): return (x * 0.00045359237) def lbs2oz(x): return (x * 16) def lbs2tons__US_(x): return (x * 0.0005) def tons__US_2g(x): return (x * 907184.74) def tons__US_2dag(x): return (x * 90718.474) def tons__US_2kg(x): return (x * 907.18474) def tons__US_2t__metric_(x): return (x * 0.90718474) def tons__US_2oz(x): return (x * 32000) def tons__US_2lbs(x): return (x * 2000) # Volume def ml2l(x): return (x * 0.001) def ml2oz(x): return (x * 0.03381402) def ml2pints__US_(x): return (x * 0.00211337) def ml2quarts__US_(x): return (x * 0.00105668) def ml2gallons__US_(x): return (x * 0.00026417) def l2ml(x): return (x * 1000) def l2oz(x): return (x * 33.8140227) def l2pints__US_(x): return (x * 2.11337641) def l2quarts__US_(x): return (x * 1.0566882) def l2gallons__US_(x): return (x * 0.26417205) def oz2ml(x): return (x * 29.57352956) def oz2l(x): return (x * 0.02957352) def oz2pints__US_(x): return (x * 0.0625) def oz2quarts__US_(x): return (x * 0.03125) def oz2gallons__US_(x): return (x * 0.0078125) def pints__US_2ml(x): return (x * 473.176473) def pints__US_2l(x): return (x * 0.473176473) def pints__US_2oz(x): return (x * 16) def pints__US_2quarts__US_(x): return (x * 0.5) def pints__US_2gallons__US_(x): return (x * 0.125) def quarts__US_2ml(x): return (x * 946.352946) def quarts__US_2l(x): return (x * 0.946352946) def quarts__US_2oz(x): return (x * 32) def quarts__US_2pints__US_(x): return (x * 2) def quarts__US_2gallons__US_(x): return (x * 0.25) def gallons__US_2ml(x): return (x * 3785.411784) def gallons__US_2l(x): return (x * 3.785411784) def gallons__US_2oz(x): return (x * 128) def gallons__US_2pints__US_(x): return (x * 8) def gallons__US_2quarts__US_(x): return (x * 4) # Temperature def C2F(x): return (x * 9/5 + 32) def C2K(x): return (x + 273.15) def F2C(x): return ((x - 32) * 5/9) def F2K(x): return ((x + 459.67) * 5/9) def K2C(x): return (x - 273.15) def K2F(x): return (x * 9/5 - 459.67) #def (x): return (x * 1) #def (x): return (x * 1) #def (x): return (x * 1) #def (x): return (x * 1) #def (x): return (x * 1) #def (x): return (x * 1) class Window(gtk.Window): def __init__(self): gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL) self.set_title("%s" % (PROGNAME)) self.set_border_width(2) self.set_resizable(False) self.connect('destroy', self.quit) vbox = gtk.VBox(False, 5) self.add(vbox) vbox.pack_start(self.menu(), False, True, 0) self.notebook = gtk.Notebook() vbox.pack_end(self.notebook, False, True, 0) self.notebook.set_tab_pos(gtk.POS_RIGHT) # One clipboard for all widgets self.clipboard = gtk.clipboard_get() self.counter = 0 stuff = [ (("Power", "PS", "HP", "kW"), (2, 0)), (("Displacement", "cu cm", "cu inches", "litres"), (1, 0)), (("Length", "mm", "cm", "m", "km", "inches", "feet", "yards", "miles"), (4, 1)), (("Weight", "g", "dag", "kg", "t (metric)", "oz", "lbs", "tons (US)"), (5, 2)), (("Volume", "ml", "l", "oz", "pints (US)", "quarts (US)", "gallons (US)"), (5, 1)), (("Speed", "mps", "kph", "mph"), (2, 1)), # http://www.cyfronet.krakow.pl/rowery/ciskal.html (("Pressure", "atm", "Pa", "MPa", "bar", "psi"), (2, 4)), (("Temperature", "C", "F", "K"), (1, 0)), # (("Torque", "", "", ""), (0, 0)), # (("Milage", "", "", ""), (0, 0)), # Area # Velocity # Bore # (("Tyre size", "", "", ""), (0, 0)), # (("RTI", "", "", ""), (0, 0)) ] for k, v in stuff: self.notebook.append_page(self.page(k[1:], v), gtk.Label(k[0])) self.show_all() gtk.main() def page(self, units=(), active=()): (a, b) = active vbox = gtk.VBox(False, 10) hbox1 = gtk.HBox() vbox.add(hbox1) # User enters value here entry1 = gtk.Entry() entry1.set_text("0") hbox1.pack_start(entry1, True, True, 0) # Spacer hbox1.pack_start(gtk.HBox(), False, False, 2) # Combos combo1 = gtk.combo_box_new_text() for u in units: combo1.append_text(u) combo1.set_active(a) hbox1.pack_start(combo1) image1 = gtk.Image() image1.set_from_stock(gtk.STOCK_GO_FORWARD, gtk.ICON_SIZE_LARGE_TOOLBAR) hbox1.pack_start(image1, True, True, 4) combo2 = gtk.combo_box_new_text() for u in units: combo2.append_text(u) combo2.set_active(b) hbox1.pack_start(combo2) hbox2 = gtk.HBox() vbox.add(hbox2) # Precision hbox3 = gtk.HBox() label = gtk.Label("Precision:") label.set_alignment(0.1, 0.5) adjustment = gtk.Adjustment(3, 0, 10, 1, 10, 10) spinbut = gtk.SpinButton(adjustment) hbox3.pack_start(label, False, False, 2) hbox3.pack_start(spinbut, False, False, 2) vbox.add(hbox3) # Result is shown here hbox4 = gtk.HBox() entry2 = gtk.Entry() entry2.set_editable(False) hbox4.pack_start(entry2, True, True) # "Copy to clipboard" button copy_button = self.get_copy_button() hbox4.pack_end(copy_button, False, False, 4) vbox.pack_start(hbox4) # Bottom button button = self.get_calculate_button() vbox.pack_end(button) # Signals args = (entry1, combo1, spinbut, combo2, entry2) button.connect('clicked', self.calculate, args) combo1.connect('changed', self.calculate, args) combo2.connect('changed', self.calculate, args) spinbut.connect('changed', self.calculate, args) copy_button.connect('clicked', self.set_clipboard, entry2) return vbox def get_copy_button(self): '''Return "Copy to clipboard button''' image2 = gtk.Image() image2.set_from_stock(gtk.STOCK_COPY, gtk.ICON_SIZE_LARGE_TOOLBAR) copy_button = gtk.Button() copy_button.add(image2) return copy_button def set_clipboard(self, widget, entry2): '''Put result into default clipboard''' self.clipboard.set_text(entry2.get_text()) def get_calculate_button(self): # rename to common_calc_button '''Return button with stock icon and custom string''' hbox = gtk.HBox() hbox.set_border_width(2) label = gtk.Label() label.set_markup('%s' % "Calculate!") # label.set_alignment(0.0, 0.0) hbox.pack_start(label) button = gtk.Button() # button.set_alignment(1.0, 1.0) button.add(hbox) return button def calculate(self, widget, args=()): (entry1, combo1, spinbut, combo2, entry2) = args data_in = combo1.get_active_text() data_in = data_in.replace(' ', '_').replace('(', '_').replace(')', '_') data_out = combo2.get_active_text() data_out = data_out.replace(' ', '_').replace('(', '_').replace(')', '_') value = entry1.get_text() # Validate input try: value = float(value) except: entry1.set_text("0") return # Build conversion string, e.g. '%.5f' conversion = '%.' + ('%s' % spinbut.get_value_as_int()) + 'f %s' if (data_in != data_out): func_name = eval('%s2%s' % (data_in, data_out)) x = func_name(value) entry2.set_text(conversion % (x, data_out.replace('_', ' ').replace(' ', ' '))) else: entry2.set_text(conversion % (value, data_out.replace('_', ' ').replace(' ', ' '))) def menu(self): accel_group = gtk.AccelGroup() self.add_accel_group(accel_group) file_menu = gtk.Menu() menu_item = gtk.ImageMenuItem(gtk.STOCK_QUIT) menu_item.add_accelerator('activate', accel_group, ord('Q'), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE) menu_item.connect('activate', self.quit) file_menu.append(menu_item) menu_bar = gtk.MenuBar() file_root_menu = gtk.MenuItem("_File") file_root_menu.set_submenu(file_menu) menu_bar.append(file_root_menu) about_menu = gtk.Menu() about_item = gtk.ImageMenuItem(gtk.STOCK_ABOUT) about_item.add_accelerator('activate', accel_group, ord('A'), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE) about_item.connect('activate', self.about) about_menu.append(about_item) help_root_menu = gtk.MenuItem("_Help") help_root_menu.set_submenu(about_menu) menu_bar.append(help_root_menu) return menu_bar def about(self, widget): about = gtk.AboutDialog() about.set_name(PROGNAME) about.set_version(VERSION) about.set_comments("Converts units commonly used in automotive") about.set_license(LICENSE) about.set_copyright(COPYRIGHT) about.set_authors(["Bart Kreska "]) about.run() about.destroy() def quit(self, widget): self.hide() gtk.main_quit() if __name__ == '__main__': run = Window()