Use new method to add plugin tab

Stop graph tearing
Patches from Ian Martin
This commit is contained in:
Andrew Resch 2009-10-04 19:25:10 +00:00
parent 9a0b6030e6
commit b680ff48e5
3 changed files with 38 additions and 15 deletions

View File

@ -1,6 +1,7 @@
#
# core.py
#
# Copyright (C) 2009 Ian Martin <ianmartin@cantab.net>
# Copyright (C) 2008 Damien Churchill <damoxc@gmail.com>
# Copyright (C) 2008 Martijn Voncken <mvoncken@gmail.com>
# Copyright (C) Marcos Pinto 2007 <markybob@gmail.com>
@ -44,6 +45,7 @@
# this exception statement from your version. If you delete this exception
from twisted.internet.task import LoopingCall
import time
import deluge
from deluge.log import LOG as log
@ -111,6 +113,7 @@ class Core(CorePluginBase):
for stat_list in self.stats.values():
if len(stat_list) > self.config["length"]:
stat_list.pop()
self.last_update = time.time()
except Exception, e:
log.exception(e)
@ -132,6 +135,7 @@ class Core(CorePluginBase):
for key in keys:
if key in self.stats:
stats_dict[key] = self.stats[key]
stats_dict["_last_update"] = self.last_update
return stats_dict
@export

View File

@ -85,7 +85,7 @@ class Graph:
self.legend_selected = True
self.max_selected = True
self.black = (0, 0 , 0,)
self.interval = 2000 # 2 secs
self.interval = 2 # 2 secs
self.text_bg = (255, 255 , 255, 128) # prototyping
self.set_left_axis()
@ -102,6 +102,9 @@ class Graph:
}
def set_stats(self, stats):
self.last_update = stats["_last_update"]
log.debug("Last update: %s" % self.last_update)
del stats["_last_update"]
self.stats = stats
def set_config(self, config):
@ -137,9 +140,8 @@ class Graph:
return self.surface
def draw_x_axis(self):
now = time.time()
duration = self.length * (self.interval / 1000.0)
start = now - duration
duration = float(self.length * self.interval)
start = self.last_update - duration
ratio = (self.width - 40) / duration
seconds_to_minute = 60 - time.localtime(start)[5]

View File

@ -65,26 +65,35 @@ class GraphsTab(Tab):
self._name = 'Graphs'
self.glade = glade
self.window = self.glade.get_widget('graph_tab')
self._child_widget = self.window
self.notebook = self.glade.get_widget('graph_notebook')
self.label = self.glade.get_widget('graph_label')
self._tab_label = self.label
self.bandwidth_graph = self.glade.get_widget('bandwidth_graph')
self.bandwidth_graph.connect('expose_event', self.bandwidth_expose)
self.bandwidth_graph.connect('expose_event', self.expose)
self.window.unparent()
self.label.unparent()
def bandwidth_expose(self, widget, event):
self.graph_widget = self.bandwidth_graph
self.graph = graph.Graph()
self.graph.add_stat('payload_download_rate', label='Download Rate', color=graph.green)
self.graph.add_stat('payload_upload_rate', label='Upload Rate', color=graph.blue)
self.graph.set_left_axis(formatter=fspeed, min=10240)
self.update_timer = gobject.timeout_add(2000, self.update_graph)
self.update_graph()
def update_graph(self):
width, height = self.graph_widget.allocation.width, self.graph_widget.allocation.height
def expose(self, widget, event):
"""Redraw"""
context = self.graph_widget.window.cairo_create()
# set a clip region
context.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
context.clip()
width, height = self.graph_widget.allocation.width, self.graph_widget.allocation.height
self.graph.draw_to_context(context, width, height)
#Do not propagate the event
return False
def update(self):
log.debug("getstat keys: %s", self.graph.stat_info.keys())
d1 = client.stats.get_stats(self.graph.stat_info.keys())
d1.addCallback(self.graph.set_stats)
@ -92,14 +101,21 @@ class GraphsTab(Tab):
d2.addCallback(self.graph.set_config)
dl = defer.DeferredList([d1, d2])
def draw_context(result):
self.graph.draw_to_context(context, width, height)
dl.addCallback(draw_context)
def _on_update(result):
width, height = self.graph_widget.allocation.width, self.graph_widget.allocation.height
rect = gtk.gdk.Rectangle(0, 0, width, height)
self.graph_widget.window.invalidate_rect(rect, True)
dl.addCallback(_on_update)
def clear(self):
pass
return True
class GtkUI(GtkPluginBase):
def enable(self):
log.debug("Stats plugin enable called")
self.glade = XML(self.get_resource("config.glade"))
component.get("Preferences").add_page("Stats", self.glade.get_widget("prefs_box"))
component.get("PluginManager").register_hook("on_apply_prefs", self.on_apply_prefs)
@ -108,12 +124,13 @@ class GtkUI(GtkPluginBase):
self.graphs_tab = GraphsTab(XML(self.get_resource("tabs.glade")))
self.torrent_details = component.get('TorrentDetails')
self.torrent_details.notebook.append_page(self.graphs_tab.window, self.graphs_tab.label)
self.torrent_details.add_tab(self.graphs_tab)
def disable(self):
component.get("Preferences").remove_page("Stats")
component.get("PluginManager").deregister_hook("on_apply_prefs", self.on_apply_prefs)
component.get("PluginManager").deregister_hook("on_show_prefs", self.on_show_prefs)
self.torrent_details.remove_tab(self.graphs_tab.get_name())
def on_apply_prefs(self):
log.debug("applying prefs for Stats")