Add state saving to the the files tab so that column size, sort order

and positions are persistent.
This commit is contained in:
Andrew Resch 2008-03-25 01:53:00 +00:00
parent 13b507a925
commit c4ee6c090e
2 changed files with 97 additions and 1 deletions

View File

@ -35,14 +35,24 @@ import gtk, gtk.glade
import gobject
import gettext
import os.path
import cPickle
from deluge.ui.client import aclient as client
from deluge.configmanager import ConfigManager
import deluge.component as component
import deluge.common
import deluge.ui.gtkui.listview
from deluge.log import LOG as log
class ColumnState:
def __init__(self, name, position, width, sort, sort_order):
self.name = name
self.position = position
self.width = width
self.sort = sort
self.sort_order = sort_order
class FilesTab:
def __init__(self):
glade = component.get("MainWindow").get_glade()
@ -55,6 +65,12 @@ class FilesTab:
render = gtk.CellRendererText()
column.pack_start(render, False)
column.add_attribute(render, "text", 0)
column.set_sort_column_id(0)
column.set_clickable(True)
column.set_resizable(True)
column.set_expand(False)
column.set_min_width(10)
column.set_reorderable(True)
self.listview.append_column(column)
# Size column
@ -62,14 +78,26 @@ class FilesTab:
render = gtk.CellRendererText()
column.pack_start(render, False)
column.set_cell_data_func(render, deluge.ui.gtkui.listview.cell_data_size, 1)
column.set_sort_column_id(1)
column.set_clickable(True)
column.set_resizable(True)
column.set_expand(False)
column.set_min_width(10)
column.set_reorderable(True)
self.listview.append_column(column)
# Progress column
column = gtk.TreeViewColumn(_("Progress"))
render = gtk.CellRendererProgress()
column.pack_start(render, False)
column.pack_start(render)
column.add_attribute(render, "text", 2)
column.add_attribute(render, "value", 3)
column.set_sort_column_id(3)
column.set_clickable(True)
column.set_resizable(True)
column.set_expand(False)
column.set_min_width(10)
column.set_reorderable(True)
self.listview.append_column(column)
# Priority column
@ -77,15 +105,76 @@ class FilesTab:
render = gtk.CellRendererText()
column.pack_start(render, False)
column.add_attribute(render, "text", 4)
column.set_sort_column_id(4)
column.set_clickable(True)
column.set_resizable(True)
column.set_expand(False)
column.set_min_width(10)
column.set_reorderable(True)
self.listview.append_column(column)
self.listview.set_model(self.liststore)
# Attempt to load state
self.load_state()
# torrent_id: (filepath, size)
self.files_list = {}
self.torrent_id = None
def save_state(self):
filename = "files_tab.state"
state = []
for index, column in enumerate(self.listview.get_columns()):
state.append(ColumnState(column.get_title(), index, column.get_width(),
column.get_sort_indicator(), int(column.get_sort_order())))
# Get the config location for saving the state file
config_location = ConfigManager("gtkui.conf")["config_location"]
try:
log.debug("Saving FilesTab state file: %s", filename)
state_file = open(os.path.join(config_location, filename), "wb")
cPickle.dump(state, state_file)
state_file.close()
except IOError, e:
log.warning("Unable to save state file: %s", e)
def load_state(self):
filename = "files_tab.state"
# Get the config location for loading the state file
config_location = ConfigManager("gtkui.conf")["config_location"]
state = None
try:
log.debug("Loading FilesTab state file: %s", filename)
state_file = open(os.path.join(config_location, filename), "rb")
state = cPickle.load(state_file)
state_file.close()
except IOError, e:
log.warning("Unable to load state file: %s", e)
if state == None:
return
for column_state in state:
# Find matching columns in the listview
for (index, column) in enumerate(self.listview.get_columns()):
if column_state.name == column.get_title():
# We have a match, so set options that were saved in state
if column_state.width > 0:
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
column.set_fixed_width(column_state.width)
column.set_sort_indicator(column_state.sort)
column.set_sort_order(column_state.sort_order)
if column_state.position != index:
# Column is in wrong position
if column_state.position == 0:
self.listview.move_column_after(column, None)
else:
self.listview.move_column_after(column, self.listview.get_columns()[column_state.position - 1])
def update(self):
# Get the first selected torrent
torrent_id = component.get("TorrentView").get_selected_torrents()

View File

@ -74,6 +74,13 @@ class TorrentDetails(component.Component):
self.window.vpaned.set_position(-1)
def stop(self):
# Save the state of the tabs
for tab in self.tabs:
try:
tab.save_state()
except AttributeError:
pass
self.clear()
def update(self):