Skip to content
Permalink
Browse files
Load background section list store incrementally
As per SL sugarlabs#245, some cpsections take a long time to load, leaving the
user with the impression that something has gone wrong. This patch is
specific to the background section: it is a refactoring of the code to
load the list store, adding images one at a time, so the user sees
that the widget is working. A busy cursor is also loaded.
  • Loading branch information
walterbender authored and dnarvaez committed Aug 16, 2013
1 parent 566415f commit 3ff0390
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 57 deletions.
@@ -17,9 +17,7 @@
#

from gi.repository import GConf
from gi.repository import GdkPixbuf

from sugar3.graphics import style
from jarabe.journal.model import get_documents_path
from jarabe.desktop.homebackgroundbox import BACKGROUND_IMAGE_PATH_STRING
from jarabe.desktop.homebackgroundbox import BACKGROUND_ALPHA_LEVEL_STRING
@@ -71,38 +69,6 @@ def get_background_alpha_level():
PREVIOUS_BACKGROUND_ALPHA_LEVEL = get_background_alpha_level()


def fill_background_list(store):
"""
Traverse the background directories looking for images. Append
them to the given store.
Return a list of paths for the image files found in the background
directories.
"""
paths_list = []

for directory in BACKGROUNDS_DIRS:
if directory is not None and os.path.exists(directory):
for root, dirs, files in os.walk(directory):
for file_ in files:
filepath = os.path.join(root, file_)
pixbuf = None
try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
filepath, style.XLARGE_ICON_SIZE,
style.XLARGE_ICON_SIZE)
except:
# if the file can't be converted to a pixbuf,
# ie it is not a valid image, gi will raise a
# generic gi._glib.GError .
pass
else:
store.append([pixbuf, filepath])
paths_list.append(filepath)

return paths_list


def undo(store):
set_background_image_path(PREVIOUS_BACKGROUND_IMAGE_PATH)
set_background_alpha_level(PREVIOUS_BACKGROUND_ALPHA_LEVEL)
@@ -15,7 +15,12 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

import os

import gi
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
from gi.repository import GdkPixbuf

from sugar3.graphics import style
@@ -32,14 +37,18 @@ def __init__(self, model, alerts=None):

self._model = model

self.connect('realize', self.__realize_cb)

self.set_border_width(style.DEFAULT_SPACING * 2)
self.set_spacing(style.DEFAULT_SPACING)

label_box = Gtk.Box()
label_bg = Gtk.Label(label=_('Select a background:'))
label_bg.modify_fg(Gtk.StateType.NORMAL,
style.COLOR_SELECTION_GREY.get_gdk_color())
label_bg.show()
label_box.pack_start(label_bg, False, True, 0)
label_box.show()
self.pack_start(label_box, False, True, 1)

clear_button = Gtk.Button()
@@ -55,16 +64,15 @@ def __init__(self, model, alerts=None):
self.pack_start(scrolled_window, True, True, 0)
scrolled_window.show()

store = Gtk.ListStore(GdkPixbuf.Pixbuf, str)
self._store = Gtk.ListStore(GdkPixbuf.Pixbuf, str)

icon_view = Gtk.IconView.new_with_model(store)
icon_view.set_selection_mode(Gtk.SelectionMode.SINGLE)
icon_view.connect('selection-changed', self._background_selected,
store)
icon_view.set_pixbuf_column(0)
icon_view.grab_focus()
scrolled_window.add(icon_view)
icon_view.show()
self._icon_view = Gtk.IconView.new_with_model(self._store)
self._icon_view.set_selection_mode(Gtk.SelectionMode.SINGLE)
self._icon_view.connect('selection-changed', self._background_selected)
self._icon_view.set_pixbuf_column(0)
self._icon_view.grab_focus()
scrolled_window.add(self._icon_view)
self._icon_view.show()

alpha = self._model.get_background_alpha_level()

@@ -91,40 +99,70 @@ def __init__(self, model, alerts=None):
self.pack_start(alpha_alignment, False, False, 0)
alpha_alignment.show()

paths_list = self._model.fill_background_list(store)
self._select_background(icon_view, paths_list)
self._paths_list = []

file_paths = []
for directory in self._model.BACKGROUNDS_DIRS:
if directory is not None and os.path.exists(directory):
for root, dirs, files in os.walk(directory):
for file_ in files:
file_paths.append(os.path.join(root, file_))

self._append_to_store(file_paths)
self.setup()

def _append_to_store(self, file_paths):
if file_paths:
file_path = file_paths.pop()
pixbuf = None

try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
file_path, style.XLARGE_ICON_SIZE,
style.XLARGE_ICON_SIZE)
except gi._glib._glib.GError:
pass
else:
self._store.append([pixbuf, file_path])
self._paths_list.append(file_path)

GObject.idle_add(self._append_to_store, file_paths)
else:
self._select_background()
self.get_window().set_cursor(None)

def __realize_cb(self, widget):
self.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.WATCH))

def _set_alpha_cb(self, widget, value):
self._model.set_background_alpha_level(value)

def _get_selected_path(self, widget, store):
def _get_selected_path(self, widget):
try:
iter_ = store.get_iter(widget.get_selected_items()[0])
image_path = store.get(iter_, 1)[0]
iter_ = self._store.get_iter(widget.get_selected_items()[0])
image_path = self._store.get(iter_, 1)[0]

return image_path, iter_
except:
return None

def _background_selected(self, widget, store):
selected = self._get_selected_path(widget, store)
def _background_selected(self, widget):
selected = self._get_selected_path(widget)

if selected is None:
return

image_path, _iter = selected
iter_ = store.get_iter(widget.get_selected_items()[0])
image_path = store.get(iter_, 1)[0]
iter_ = self._store.get_iter(widget.get_selected_items()[0])
image_path = self._store.get(iter_, 1)[0]
self._model.set_background_image_path(image_path)

def _select_background(self, icon_view, paths_list):
def _select_background(self):
background = self._model.get_background_image_path()
if background in paths_list:
_path = paths_list.index(background)
path = Gtk.TreePath.new_from_string('%s' % _path)
icon_view.select_path(path)
if background in self._paths_list:
self._icon_view.select_path(
Gtk.TreePath.new_from_string(
'%s' % self._paths_list.index(background)))

def _clear_clicked_cb(self, widget, event=None):
self._model.set_background_image_path(None)

0 comments on commit 3ff0390

Please sign in to comment.