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 / applet.py < prev    next >
Encoding:
Python Source  |  2006-08-30  |  6.3 KB  |  241 lines

  1. import os, time
  2. from os.path import *
  3. import gnomeapplet, gtk, gtk.gdk, gconf, gobject
  4. from gettext import gettext as _
  5. import gconf
  6.  
  7. import invest, invest.about, invest.chart, invest.preferences
  8. from invest.quotes import get_quotes_updater
  9. from invest.widgets import *
  10.  
  11.         
  12. class InvestAppletPreferences:
  13.     def __init__(self, applet):
  14.         # Default values
  15.         self.GCONF_APPLET_DIR = invest.GCONF_DIR
  16.         self.GCONF_CLIENT = gconf.client_get_default ()
  17.         
  18.         # Retreive this applet's pref folder
  19.         path = applet.get_preferences_key()
  20.         if path != None:
  21.             self.GCONF_APPLET_DIR = path
  22.             self.GCONF_CLIENT = gconf.client_get_default()
  23.             print 'Using per-applet gconf key:', self.GCONF_APPLET_DIR
  24.             gtik_settings = self.GCONF_CLIENT.get_string(self.GCONF_APPLET_DIR+"/prefs/tik_syms")
  25.             if gtik_settings != None and gtik_settings != "":
  26.                 # Import old settings
  27.                 self.GCONF_CLIENT.set_string(self.GCONF_APPLET_DIR+"/prefs/tik_syms", "")
  28.                 for sym in gtik_settings.split('+'):
  29.                     invest.STOCKS[sym].append({
  30.                         "amount": 0,
  31.                         "bought": 0,
  32.                         "comission": 0,
  33.                     })
  34.                         
  35. class InvestApplet:
  36.     def __init__(self, applet):
  37.         self.applet = applet
  38.         self.prefs = InvestAppletPreferences(applet)
  39.         
  40.         self.investwidget = InvestWidget()
  41.         self.investticker = InvestTrend()#InvestTicker()
  42.         
  43.         self.pw = ProgramWindow(applet, self.investticker)
  44.         self.pw.add(self.investwidget)
  45.         self.tb = ToggleButton(applet, self.pw)
  46.         
  47.         self.investwidget.connect('row-activated', lambda treeview, path, view_column: self.tb.set_active(False))
  48.         get_quotes_updater().connect('quotes-updated', self._on_quotes_updated)
  49.             
  50.         box = gtk.HBox()
  51.         box.add(self.tb)
  52.         box.add(self.investticker)
  53.         
  54.         self.applet.add(box)
  55.         self.applet.setup_menu_from_file (
  56.             invest.SHARED_DATA_DIR, "Invest_Applet.xml",
  57.             None, [("About", self.on_about), ("Prefs", self.on_preferences), ("Refresh", self.on_refresh)])
  58.  
  59.         self.applet.show_all()
  60.         get_quotes_updater().refresh()
  61.         
  62.     def on_about(self, component, verb):
  63.         invest.about.show_about()
  64.     
  65.     def on_preferences(self, component, verb):
  66.         invest.preferences.show_preferences(self)
  67.         get_quotes_updater().refresh()
  68.     
  69.     def on_refresh(self, component, verb):
  70.         get_quotes_updater().refresh()
  71.         
  72.     def _on_quotes_updated(self, updater):
  73.         pass
  74.         #invest.dbusnotification.notify(
  75.         #    _("Financial Report"),
  76.         #    "stock_chart",
  77.         #    _("Financial Report"),
  78.         #    _("Quotes updated"),
  79.         #    3000)
  80.             
  81. class ToggleButton(gtk.ToggleButton):
  82.     def __init__(self, applet, pw):
  83.         gtk.ToggleButton.__init__(self)
  84.         self.pw = pw
  85.  
  86.         self.connect("toggled", self.toggled)
  87.  
  88.         self.set_relief(gtk.RELIEF_NONE)
  89.  
  90.         image = gtk.Image()
  91.         try:
  92.             pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(join(invest.ART_DATA_DIR, "invest-button.png"), -1,-1)
  93.             image.set_from_pixbuf(pixbuf)
  94.         except Exception, msg:
  95.             image.set_from_icon_name("stock_chart", gtk.ICON_SIZE_BUTTON)
  96.         
  97.         hbox = gtk.HBox()
  98.         hbox.pack_start(image)
  99.         
  100.         orient = applet.get_orient()
  101.         arrow_dir = gtk.ARROW_DOWN
  102.         if orient == gnomeapplet.ORIENT_RIGHT:
  103.             arrow_dir = gtk.ARROW_RIGHT
  104.         elif orient == gnomeapplet.ORIENT_LEFT:
  105.             arrow_dir = gtk.ARROW_LEFT
  106.         elif orient == gnomeapplet.ORIENT_DOWN:
  107.             arrow_dir = gtk.ARROW_DOWN
  108.         elif orient == gnomeapplet.ORIENT_UP:
  109.             arrow_dir = gtk.ARROW_UP
  110.             
  111.         hbox.pack_start(gtk.Arrow(arrow_dir, gtk.SHADOW_NONE))
  112.         
  113.         self.add(hbox)
  114.             
  115.     def toggled(self, togglebutton):
  116.         if togglebutton.get_active():
  117.             self.pw.update_position()
  118.             self.pw.show_all()
  119.             self.pw.grab_focus()
  120.         else:
  121.             self.pw.hide()
  122.  
  123. gobject.type_register(ToggleButton)
  124.     
  125. class ProgramWindow(gtk.Window):
  126.     """
  127.     Borderless window aligning itself to a given widget.
  128.     Use CuemiacWindow.update_position() to align it.
  129.     """
  130.     def __init__(self, applet, widgetToAlignWith):
  131.         """
  132.         alignment should be one of
  133.             gnomeapplet.ORIENT_{DOWN,UP,LEFT,RIGHT}
  134.         
  135.         Call CuemiacWindow.update_position () to position the window.
  136.         """
  137.         gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
  138.         self.set_decorated (False)
  139.  
  140.         # Skip the taskbar, and the pager, stick and stay on top
  141.         self.stick()
  142.         self.set_keep_above(True)
  143.         self.set_skip_pager_hint(True)
  144.         self.set_skip_taskbar_hint(True)
  145.         
  146.         self.widgetToAlignWith = widgetToAlignWith        
  147.         self.applet = applet
  148.         self.alignment = applet.get_orient ()
  149.  
  150.         self.realize_status = None
  151.         self.connect ("realize", lambda win : self.__register_realize ())
  152.         self.connect ("size-allocate", self.__resize_event)
  153.         
  154.     def __resize_event (self, event, data):
  155.         self.update_position ()
  156.     
  157.     def update_position (self):
  158.         """
  159.         Calculates the position and moves the window to it.
  160.         IMPORATNT: widgetToAlignWith should be realized!
  161.         """
  162.         if self.realize_status == None:
  163.             self.realize_status = False
  164.             self.realize ()
  165.             return
  166.         
  167.         if self.realize_status == False:
  168.             return
  169.             
  170.         # Get our own dimensions & position
  171.         (wx, wy) = self.window.get_origin ()
  172.         (ax, ay) = self.widgetToAlignWith.window.get_origin ()
  173.  
  174.         (ww, wh) = self.window.get_size ()
  175.         (aw, ah) = self.widgetToAlignWith.window.get_size ()
  176.  
  177.         screen = self.get_screen()
  178.         monitor = screen.get_monitor_geometry (screen.get_monitor_at_window (self.applet.window))
  179.         
  180.         if self.alignment == gnomeapplet.ORIENT_LEFT:
  181.                 x = ax - ww
  182.                 y = ay
  183.  
  184.                 if (y + wh > monitor.y + monitor.height):
  185.                     y = monitor.y + monitor.height - wh
  186.  
  187.                 if (y < 0):
  188.                     y = 0
  189.                 
  190.                 if (y + wh > monitor.height / 2):
  191.                     gravity = gtk.gdk.GRAVITY_SOUTH_WEST    
  192.                 else:
  193.                     gravity = gtk.gdk.GRAVITY_NORTH_WEST
  194.                     
  195.         elif self.alignment == gnomeapplet.ORIENT_RIGHT:
  196.                 x = ax + aw
  197.                 y = ay
  198.  
  199.                 if (y + wh > monitor.y + monitor.height):
  200.                     y = monitor.y + monitor.height - wh
  201.                 
  202.                 if (y < 0):
  203.                     y = 0
  204.                 
  205.                 if (y + wh > monitor.height / 2):
  206.                     gravity = gtk.gdk.GRAVITY_SOUTH_EAST
  207.                 else:
  208.                     gravity = gtk.gdk.GRAVITY_NORTH_EAST
  209.  
  210.         elif self.alignment == gnomeapplet.ORIENT_DOWN:
  211.                 x = ax
  212.                 y = ay + ah
  213.  
  214.                 if (x + ww > monitor.x + monitor.width):
  215.                     x = monitor.x + monitor.width - ww
  216.  
  217.                 if (x < 0):
  218.                     x = 0
  219.  
  220.                 gravity = gtk.gdk.GRAVITY_NORTH_WEST
  221.         elif self.alignment == gnomeapplet.ORIENT_UP:
  222.                 x = ax
  223.                 y = ay - wh
  224.  
  225.                 if (x + ww > monitor.x + monitor.width):
  226.                     x = monitor.x + monitor.width - ww
  227.  
  228.                 if (x < 0):
  229.                     x = 0
  230.  
  231.                 gravity = gtk.gdk.GRAVITY_SOUTH_WEST
  232.         
  233.         self.move(x, y)
  234.         self.set_gravity(gravity)
  235.     
  236.     def __register_realize (self):
  237.         self.realize_status = True
  238.         self.update_position()
  239.         
  240. gobject.type_register (ProgramWindow)
  241.