GTKUI Torrentview Speed optimizations

This commit is contained in:
bendikro 2012-11-24 19:13:24 +01:00
parent ffb902ba06
commit ca272bb36a
2 changed files with 82 additions and 58 deletions

View File

@ -237,17 +237,28 @@ class ListView:
model_filter = self.liststore.filter_new()
model_filter.set_visible_column(
self.columns["filter"].column_indices[0])
sort_info = None
if self.model_filter:
sort_info = self.model_filter.get_sort_column_id()
self.model_filter = gtk.TreeModelSort(model_filter)
if sort_info and sort_info[0] and sort_info[1] > -1:
self.model_filter.set_sort_column_id(sort_info[0], sort_info[1])
self.set_sort_functions()
self.model_filter.connect("sort-column-changed", self.on_model_sort_changed)
self.model_filter.connect("row-inserted", self.on_model_row_inserted)
self.treeview.set_model(self.model_filter)
self.set_sort_functions()
self.set_model_sort()
def set_model_sort(self):
column_state = self.get_sort_column_from_state()
if column_state:
self.treeview.get_model().set_sort_column_id(column_state.sort, column_state.sort_order)
# Using the default sort column
elif self.default_sort_column_id:
self.model_filter.set_sort_column_id(self.default_sort_column_id, gtk.SORT_ASCENDING)
self.model_filter.set_default_sort_func(None)
def get_sort_column_from_state(self):
"""Find the first (should only be one) state with sort enabled"""
if self.state is None:
return None
for column_state in self.state:
if column_state.sort is not None and column_state.sort > -1:
return column_state
return None
def on_model_sort_changed(self, model):
if self.unique_column_id:
@ -281,7 +292,6 @@ class ListView:
return cmp(model[iter1][data], model[iter2][data])
def set_sort_functions(self):
self.model_filter.set_default_sort_func(None)
for column in self.columns.values():
sort_func = column.sort_func or self.generic_sort_func
self.model_filter.set_sort_func(
@ -449,10 +459,6 @@ class ListView:
self.liststore.foreach(copy_row, (new_list, self.columns))
self.liststore = new_list
# Create the model
self.create_model_filter()
return
def remove_column(self, header):
"""Removes the column with the name 'header' from the listview"""
@ -575,11 +581,6 @@ class ListView:
if tooltip:
column.get_widget().set_tooltip_markup(tooltip)
if default_sort:
if self.model_filter.get_sort_column_id()[0] is None:
self.model_filter.set_sort_column_id(column_indices[sortid],
gtk.SORT_ASCENDING)
# Check for loaded state and apply
column_in_state = False
if self.state != None:
@ -591,10 +592,6 @@ class ListView:
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
column.set_fixed_width(column_state.width)
if column_state.sort is not None and column_state.sort > -1:
self.model_filter.set_sort_column_id(
column_state.sort, column_state.sort_order
)
column.set_visible(column_state.visible)
position = column_state.position
break

View File

@ -452,18 +452,12 @@ class TorrentView(listview.ListView, component.Component):
{}, status_fields).addCallback(self._on_session_state)
def _on_session_state(self, state):
self.treeview.freeze_child_notify()
model = self.treeview.get_model()
for torrent_id in state:
self.add_row(torrent_id, update=False)
self.mark_dirty(torrent_id)
self.treeview.set_model(model)
self.treeview.thaw_child_notify()
self.add_rows(state)
self.got_state = True
# Update the view right away with our status
self.status = state
self.set_columns_to_update()
self.update_view()
self.update_view(load_new_list=True)
def stop(self):
"""Stops the torrentview"""
@ -551,43 +545,66 @@ class TorrentView(listview.ListView, component.Component):
# Send a status request
gobject.idle_add(self.send_status_request)
def update_view(self, columns=None):
"""Update the view. If columns is not None, it will attempt to only
update those columns selected.
"""
def update_view(self, load_new_list=False):
"""Update the torrent view model with data we've received."""
filter_column = self.columns["filter"].column_indices[0]
# Update the torrent view model with data we've received
status = self.status
if not load_new_list:
# Freeze notications while updating
self.treeview.freeze_child_notify()
# Get the columns to update from one of the torrents
if status:
torrent_id = status.keys()[0]
fields_to_update = []
for column in self.columns_to_update:
column_index = self.get_column_index(column)
for i, status_field in enumerate(self.columns[column].status_field):
# Only use columns that the torrent has in the state
if status_field in status[torrent_id]:
fields_to_update.append((column_index[i], status_field))
for row in self.liststore:
torrent_id = row[self.columns["torrent_id"].column_indices[0]]
# We expect the torrent_id to be in status and prev_status,
# as it will be as long as the list isn't changed by the user
if not torrent_id in status.keys():
row[filter_column] = False
else:
row[filter_column] = True
if torrent_id in self.prev_status and status[torrent_id] == self.prev_status[torrent_id]:
# The status dict is the same, so do not update
torrent_id_in_status = False
try:
torrent_status = status[torrent_id]
torrent_id_in_status = True
if torrent_status == self.prev_status[torrent_id]:
# The status dict is the same, so do nothing to update for this torrent
continue
except KeyError, e:
pass
# Set values for each column in the row
for column in self.columns_to_update:
column_index = self.get_column_index(column)
for i, status_field in enumerate(self.columns[column].status_field):
if status_field in status[torrent_id]:
try:
# Only update if different
row_value = status[torrent_id][status_field]
if row[column_index[i]] != row_value:
row[column_index[i]] = row_value
except Exception, e:
log.debug("Error while updating row for column "
"index %d, status field %r, value %r:"
" %s", column_index[0], status_field,
row_value, e)
if not torrent_id_in_status:
if row[filter_column] is True:
row[filter_column] = False
else:
if row[filter_column] is False:
row[filter_column] = True
# Find the fields to update
to_update = []
for i, status_field in fields_to_update:
row_value = status[torrent_id][status_field]
if row[i] != row_value:
to_update.append(i)
to_update.append(row_value)
# Update fields in the liststore
if to_update:
self.liststore.set(row.iter, *to_update)
if load_new_list:
# Create the model filter. This sets the model for the treeview and enables sorting.
self.create_model_filter()
else:
self.treeview.thaw_child_notify()
component.get("MenuBar").update_menu()
self.prev_status = status
def _on_get_torrents_status(self, status):
@ -602,6 +619,16 @@ class TorrentView(listview.ListView, component.Component):
return
gobject.idle_add(self.update_view)
def add_rows(self, state):
"""Adds all the torrents from state to self.liststore"""
torrent_id_column = self.columns["torrent_id"].column_indices[0]
dirty_column = self.columns["dirty"].column_indices[0]
filter_column = self.columns["filter"].column_indices[0]
for i, torrent_id in enumerate(state):
# Insert a new row to the liststore
row = self.liststore.append()
self.liststore.set(row, torrent_id_column, torrent_id, dirty_column, True, filter_column, True)
def add_row(self, torrent_id, update=True):
"""Adds a new torrent row to the treeview"""
# Make sure this torrent isn't already in the list