Add pop-up menus to the statusbar items.

This commit is contained in:
Andrew Resch 2008-03-09 05:38:07 +00:00
parent bed05c7a05
commit 74cd111513
4 changed files with 201 additions and 102 deletions

View File

@ -138,6 +138,101 @@ def open_url_in_browser(url):
import gobject
gobject.idle_add(start_browser)
def build_menu_radio_list(value_list, callback, pref_value=None,
suffix=None, show_notset=False, notset_label=None, notset_lessthan=0,
show_other=False, show_activated=False, activated_label=None):
# Build a menu with radio menu items from a list and connect them to
# the callback. The pref_value is what you would like to test for the
# default active radio item.
import gtk
if notset_label is None:
notset_label = _("Unlimited")
if activated_label is None:
activated_label = _("Activated")
menu = gtk.Menu()
group = None
if show_activated is False:
if pref_value > -1 and pref_value not in value_list:
value_list.pop()
value_list.append(pref_value)
for value in sorted(value_list):
if suffix != None:
menuitem = gtk.RadioMenuItem(group, str(value) + " " + \
suffix)
else:
menuitem = gtk.RadioMenuItem(group, str(value))
group = menuitem
if value == pref_value and pref_value != None:
menuitem.set_active(True)
if callback != None:
menuitem.connect("toggled", callback)
menu.append(menuitem)
if show_activated is True:
for value in sorted(value_list):
menuitem = gtk.RadioMenuItem(group, str(activated_label))
group = menuitem
if value == pref_value and pref_value != None:
menuitem.set_active(True)
if callback != None:
menuitem.connect("toggled", callback)
menu.append(menuitem)
if show_notset:
menuitem = gtk.RadioMenuItem(group, notset_label)
if pref_value < notset_lessthan and pref_value != None:
menuitem.set_active(True)
if show_activated and pref_value == 1:
menuitem.set_active(True)
menuitem.connect("toggled", callback)
menu.append(menuitem)
# Add the Other... menuitem
if show_other is True:
menuitem = gtk.SeparatorMenuItem()
menu.append(menuitem)
menuitem = gtk.MenuItem(_("Other..."))
menuitem.connect("activate", callback)
menu.append(menuitem)
return menu
def show_other_dialog(string, default=None):
"""Shows a dialog to get an 'other' speed and return the value"""
import gtk
import gtk.glade
dialog_glade = gtk.glade.XML(
pkg_resources.resource_filename("deluge.ui.gtkui",
"glade/dgtkpopups.glade"))
speed_dialog = dialog_glade.get_widget("speed_dialog")
spin_title = dialog_glade.get_widget("spin_title")
spin_title.set_text(_("%s" % string))
spin_speed = dialog_glade.get_widget("spin_speed")
if default != None:
spin_speed.set_value(default)
spin_speed.select_region(0, -1)
response = speed_dialog.run()
if response == 1: # OK Response
value = spin_speed.get_value()
else:
speed_dialog.destroy()
return None
speed_dialog.destroy()
return value
## Formatting text functions

View File

@ -86,6 +86,7 @@ DEFAULT_PREFS = {
"window_pane_position": -1,
"tray_download_speed_list" : [5.0, 10.0, 30.0, 80.0, 300.0],
"tray_upload_speed_list" : [5.0, 10.0, 30.0, 80.0, 300.0],
"connection_limit_list": [50, 100, 200, 300, 500],
"enabled_plugins": [],
"show_connection_manager_on_start": True,
"autoconnect": False,

View File

@ -35,6 +35,7 @@ import gtk
import deluge.component as component
import deluge.common
from deluge.configmanager import ConfigManager
from deluge.ui.client import aclient as client
from deluge.log import LOG as log
@ -96,6 +97,7 @@ class StatusBar(component.Component):
component.Component.__init__(self, "StatusBar", interval=3000)
self.window = component.get("MainWindow")
self.statusbar = self.window.main_glade.get_widget("statusbar")
self.config = ConfigManager("gtkui.conf")
# Status variables that are updated via callback
self.max_connections = -1
@ -130,15 +132,18 @@ class StatusBar(component.Component):
# Add in images and labels
self.remove_item(self.not_connected_item)
self.connections_item = StatusBarItem(
stock=gtk.STOCK_NETWORK)
stock=gtk.STOCK_NETWORK,
callback=self._on_connection_item_clicked)
self.hbox.pack_start(
self.connections_item.get_eventbox(), expand=False, fill=False)
self.download_item = StatusBarItem(
image=deluge.common.get_pixmap("downloading16.png"))
image=deluge.common.get_pixmap("downloading16.png"),
callback=self._on_download_item_clicked)
self.hbox.pack_start(
self.download_item.get_eventbox(), expand=False, fill=False)
self.upload_item = StatusBarItem(
image=deluge.common.get_pixmap("seeding16.png"))
image=deluge.common.get_pixmap("seeding16.png"),
callback=self._on_upload_item_clicked)
self.hbox.pack_start(
self.upload_item.get_eventbox(), expand=False, fill=False)
self.dht_item = StatusBarItem(
@ -280,3 +285,90 @@ class StatusBar(component.Component):
def update(self):
# Send status request
self.send_status_request()
def _on_download_item_clicked(self, widget, event):
menu = deluge.common.build_menu_radio_list(
self.config["tray_download_speed_list"],
self._on_set_download_speed,
self.max_download_speed,
_("KiB/s"), show_notset=True, show_other=True)
menu.show_all()
menu.popup(None, None, None, event.button, event.time)
def _on_set_download_speed(self, widget):
log.debug("_on_set_download_speed")
value = widget.get_children()[0].get_text().split(" ")[0]
log.debug("value: %s", value)
if value == "Unlimited":
value = -1
if value == _("Other..."):
value = deluge.common.show_other_speed_dialog(
_("Download Speed (KiB/s):"), self.max_download_speed)
if value == None:
return
# Set the config in the core
value = float(value)
if value != self.max_download_speed:
client.set_config({"max_download_speed": value})
def _on_upload_item_clicked(self, widget, event):
menu = deluge.common.build_menu_radio_list(
self.config["tray_upload_speed_list"],
self._on_set_upload_speed,
self.max_upload_speed,
_("KiB/s"), show_notset=True, show_other=True)
menu.show_all()
menu.popup(None, None, None, event.button, event.time)
def _on_set_upload_speed(self, widget):
log.debug("_on_set_upload_speed")
value = widget.get_children()[0].get_text().split(" ")[0]
log.debug("value: %s", value)
if value == "Unlimited":
value = -1
if value == _("Other..."):
value = deluge.common.show_other_speed_dialog(
_("Upload Speed (KiB/s):"), self.max_upload_speed)
if value == None:
return
# Set the config in the core
value = float(value)
if value != self.max_upload_speed:
client.set_config({"max_upload_speed": value})
def _on_connection_item_clicked(self, widget, event):
menu = deluge.common.build_menu_radio_list(
self.config["connection_limit_list"],
self._on_set_connection_limit,
self.max_connections, show_notset=True, show_other=True)
menu.show_all()
menu.popup(None, None, None, event.button, event.time)
def _on_set_connection_limit(self, widget):
log.debug("_on_set_connection_limit")
value = widget.get_children()[0].get_text().split(" ")[0]
log.debug("value: %s", value)
if value == "Unlimited":
value = -1
if value == _("Other..."):
value = deluge.common.show_other_dialog(
_("Connection Limit:"), self.max_connections)
if value == None:
return
# Set the config in the core
value = int(value)
if value != self.max_connections:
client.set_config({"max_connections_global": value})

View File

@ -191,13 +191,13 @@ class SystemTray(component.Component):
def build_tray_bwsetsubmenu(self):
# Create the Download speed list sub-menu
submenu_bwdownset = self.build_menu_radio_list(
submenu_bwdownset = deluge.common.build_menu_radio_list(
self.config["tray_download_speed_list"], self.tray_setbwdown,
self.max_download_speed,
_("KiB/s"), show_notset=True, show_other=True)
# Create the Upload speed list sub-menu
submenu_bwupset = self.build_menu_radio_list(
submenu_bwupset = deluge.common.build_menu_radio_list(
self.config["tray_upload_speed_list"], self.tray_setbwup,
self.max_upload_speed,
_("KiB/s"), show_notset=True, show_other=True)
@ -298,121 +298,32 @@ class SystemTray(component.Component):
self.window.quit()
client.shutdown()
def build_menu_radio_list(self, value_list, callback, pref_value=None,
suffix=None, show_notset=False, notset_label=None, notset_lessthan=0,
show_other=False, show_activated=False, activated_label=None):
# Build a menu with radio menu items from a list and connect them to
# the callback. The pref_value is what you would like to test for the
# default active radio item.
if notset_label is None:
notset_label = _("Unlimited")
if activated_label is None:
activated_label = _("Activated")
menu = gtk.Menu()
group = None
if show_activated is False:
if pref_value > -1 and pref_value not in value_list:
value_list.pop()
value_list.append(pref_value)
for value in sorted(value_list):
if suffix != None:
menuitem = gtk.RadioMenuItem(group, str(value) + " " + \
suffix)
else:
menuitem = gtk.RadioMenuItem(group, str(value))
group = menuitem
if value == pref_value and pref_value != None:
menuitem.set_active(True)
if callback != None:
menuitem.connect("toggled", callback)
menu.append(menuitem)
if show_activated is True:
for value in sorted(value_list):
menuitem = gtk.RadioMenuItem(group, str(activated_label))
group = menuitem
if value == pref_value and pref_value != None:
menuitem.set_active(True)
if callback != None:
menuitem.connect("toggled", callback)
menu.append(menuitem)
if show_notset:
menuitem = gtk.RadioMenuItem(group, notset_label)
if pref_value < notset_lessthan and pref_value != None:
menuitem.set_active(True)
if show_activated and pref_value == 1:
menuitem.set_active(True)
menuitem.connect("toggled", callback)
menu.append(menuitem)
# Add the Other... menuitem
if show_other is True:
menuitem = gtk.SeparatorMenuItem()
menu.append(menuitem)
menuitem = gtk.MenuItem(_("Other..."))
menuitem.connect("activate", callback)
menu.append(menuitem)
return menu
def tray_setbwdown(self, widget, data=None):
self.setbwlimit(widget, _("Download"), "max_download_speed",
"tray_download_speed_list")
"tray_download_speed_list", self.max_download_speed)
def tray_setbwup(self, widget, data=None):
self.setbwlimit(widget, _("Upload"), "max_upload_speed",
"tray_upload_speed_list")
"tray_upload_speed_list", self.max_upload_speed)
def setbwlimit(self, widget, string, core_key, ui_key):
def setbwlimit(self, widget, string, core_key, ui_key, default):
"""Sets the bandwidth limit based on the user selection."""
value = widget.get_children()[0].get_text().rstrip(" " +
_("KiB/s"))
value = widget.get_children()[0].get_text().rstrip(" " + _("KiB/s"))
if value == _("Unlimited"):
value = -1
if value == _("Other..."):
dialog_glade = gtk.glade.XML(
pkg_resources.resource_filename("deluge.ui.gtkui",
"glade/dgtkpopups.glade"))
speed_dialog = dialog_glade.get_widget("speed_dialog")
spin_title = dialog_glade.get_widget("spin_title")
spin_title.set_text(_("%s Speed (KiB/s):" % string))
spin_speed = dialog_glade.get_widget("spin_speed")
spin_speed.set_value(
client.get_config_value(core_key))
spin_speed.select_region(0, -1)
response = speed_dialog.run()
if response == 1: # OK Response
value = spin_speed.get_value()
else:
speed_dialog.destroy()
value = deluge.common.show_other_speed_dialog(
string + " Speed (KiB/s):", default)
if value == None:
return
speed_dialog.destroy()
# Set the config in the core
value = float(value)
config_to_set = {core_key: value}
client.set_config(config_to_set)
# Update the tray speed limit list
if value not in self.config[ui_key] and value >= 0:
# We prepend this value and remove the last value in the list
self.config[ui_key].insert(0, value)
self.config[ui_key].pop()
# Re-build the menu
self.build_tray_bwsetsubmenu()
self.build_tray_bwsetsubmenu()
def unlock_tray(self, comingnext, is_showing_dlg=[False]):
import sha