home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / invest / widgets.py < prev    next >
Encoding:
Python Source  |  2006-08-30  |  4.9 KB  |  172 lines

  1. import os, time
  2. from os.path import *
  3. import gnomeapplet, gtk, gtk.gdk, gconf, gobject, pango
  4. from gettext import gettext as _
  5. import gtk, gtk.glade, egg.trayicon, gobject, gnomevfs
  6. import csv, os
  7. from gettext import gettext as _
  8. import invest, invest.about, invest.chart
  9. from invest.quotes import get_quotes_updater
  10. from invest import *
  11.  
  12. COLORSCALE_POSITIVE = [
  13.     "white",
  14.     "#ad7fa8",
  15.     "#75507b",
  16.     "#5c3566",
  17.     "#729fcf",
  18.     "#3465a4",
  19.     "#204a87",
  20.     "#8ae234",
  21.     "#73d216",
  22.     "#4e9a06",
  23. ]
  24. GREEN = COLORSCALE_POSITIVE[-1]
  25. COLORSCALE_NEGATIVE = [
  26.     "white",
  27.     "#fce94f",
  28.     "#e9b96e",
  29.     "#fcaf3e",
  30.     "#c17d11",
  31.     "#f57900",
  32.     "#ce5c00",
  33.     "#ef2929",
  34.     "#cc0000",
  35.     "#a40000",
  36. ]
  37. RED = COLORSCALE_NEGATIVE[-1]
  38.  
  39. class InvestWidget(gtk.TreeView):
  40.     def __init__(self):
  41.         gtk.TreeView.__init__(self)
  42.                 
  43.         self.set_property("headers-visible", False)
  44.         self.set_property("rules-hint", True)
  45.         self.set_reorderable(True)
  46.         
  47.         cell = gtk.CellRendererText ()
  48.         self.column_description = gtk.TreeViewColumn ("Description", cell)
  49.         self.column_description.set_cell_data_func(cell, self._get_cell_data)
  50.         
  51.         self.append_column(self.column_description)
  52.         self.connect('row-activated', self.on_row_activated)
  53.         
  54.         self.set_model(get_quotes_updater())
  55.     
  56.     def _get_cell_data(self, column, cell, model, iter):
  57.         if model[iter][model.TICKER_ONLY]:
  58.             color = GREEN
  59.             if model[iter][model.VARIATION] < 0:
  60.                 color = RED
  61.                             
  62.             cell.set_property('markup',
  63.                 "<span face='Monospace'>%s: <span foreground='%s'>%+.2f</span> (<span foreground='%s'>%+.2f%%</span>)</span>" %
  64.                 (model[iter][model.SYMBOL], color, model[iter][model.VALUE], color, model[iter][model.VARIATION]))
  65.         else:
  66.             color = GREEN
  67.             if model[iter][model.BALANCE] < 0:
  68.                 color = RED
  69.                             
  70.             cell.set_property('markup',
  71.                 "<span face='Monospace'>%s: <span foreground='%s'>%.2f</span> (<span foreground='%s'>%+.2f%%</span>) %.2f</span>" %
  72.                 (model[iter][model.SYMBOL], color, model[iter][model.BALANCE], color, model[iter][model.BALANCE_PCT], model[iter][model.VALUE]))
  73.                 
  74.     def on_row_activated(self, treeview, path, view_column):
  75.         ticker = self.get_model()[self.get_model().get_iter(path)][0]
  76.         if ticker == None:
  77.             return
  78.         
  79.         invest.chart.show_chart([ticker])
  80.  
  81. gobject.type_register(InvestWidget)
  82.     
  83. #class InvestTicker(gtk.Label):
  84. #    def __init__(self):
  85. #        gtk.Label.__init__(self, _("Waiting..."))
  86. #        
  87. #        self.quotes = []
  88. #        gobject.timeout_add(TICKER_TIMEOUT, self.scroll_quotes)
  89. #        
  90. #        get_quotes_updater().connect('quotes-updated', self.on_quotes_update)
  91. #                        
  92. #    def on_quotes_update(self, updater):
  93. #        self.quotes = []
  94. #        updater.foreach(self.update_quote, None)
  95. #    
  96. #    def update_quote(self, model, path, iter, user_data):
  97. #        color = GREEN
  98. #        if model[iter][model.BALANCE] < 0:
  99. #            color = RED
  100. #        
  101. #        self.quotes.append(
  102. #            "%s: <span foreground='%s'>%+.2f (%+.2f%%)</span> %.2f" %
  103. #            (model[iter][model.SYMBOL], color, model[iter][model.BALANCE], model[iter][model.BALANCE_PCT], model[iter][model.VALUE]))
  104. #                
  105. #    def scroll_quotes(self):
  106. #        if len(self.quotes) == 0:
  107. #            return True
  108. #        
  109. #        q = self.quotes.pop()
  110. #        self.set_markup("<span face='Monospace'>%s</span>" % q)
  111. #        self.quotes.insert(0, q)
  112. #        
  113. #        return True
  114. #
  115. #gobject.type_register(InvestTicker)
  116.  
  117. class InvestTrend(gtk.Image):    
  118.     def __init__(self):
  119.         gtk.Image.__init__(self)
  120.         self.pixbuf = None
  121.         self.previous_allocation = (0,0)
  122.         self.connect('size-allocate', self.on_size_allocate)
  123.         get_quotes_updater().connect('quotes-updated', self.on_quotes_update)
  124.     
  125.     def on_size_allocate(self, widget, allocation):
  126.         if self.previous_allocation == (allocation.width, allocation.height):
  127.             return
  128.             
  129.         self.pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, allocation.height, allocation.height)
  130.         self.set_color("grey")
  131.         self.previous_allocation = (allocation.width, allocation.height)
  132.         
  133.     def set_color(self, color, opacity=0xFF):
  134.         if self.pixbuf != None:
  135.             try:
  136.                 color = pango.Color(color)
  137.                 factor = float(0xFF)/0xFFFF
  138.                 self.pixbuf.fill(
  139.                     int(color.red*factor)<<24|int(color.green*factor)<<16|int(color.blue*factor)<<8|opacity)
  140.                 self.set_from_pixbuf(self.pixbuf)
  141.             except Exception, e:
  142.                 print e
  143.  
  144.     def on_quotes_update(self, updater):
  145.         start_total = 0
  146.         now_total = 0
  147.         for row in updater:
  148.             # Don't count the ticker only symbols in the color-trend
  149.             if row[updater.TICKER_ONLY]:
  150.                 continue
  151.                 
  152.             var = row[updater.VARIATION]/100
  153.             now = row[updater.VALUE]
  154.  
  155.             start = now / (1 + var)
  156.             
  157.             portfolio_number = sum([purchase["amount"] for purchase in invest.STOCKS[row[updater.SYMBOL]]])
  158.             start_total += start * portfolio_number
  159.             now_total += now * portfolio_number
  160.         
  161.         day_var = (now_total - start_total) / start_total * 100
  162.         color = int(2*day_var)
  163.         opacity = min(0xFF, int(abs(127.5*day_var)))
  164.         if day_var < 0:
  165.             color = COLORSCALE_NEGATIVE[min(len(COLORSCALE_NEGATIVE)-1, abs(color))]
  166.         else:
  167.             color = COLORSCALE_POSITIVE[min(len(COLORSCALE_POSITIVE)-1, abs(color))]
  168.  
  169.         self.set_color(color, opacity)
  170.     
  171. gobject.type_register(InvestTrend)
  172.