import sys, os import gtk from Workshop import WorkshopListItem from Rating import Rating import operator import logging class TutorialStoreResults(gtk.VBox): def __init__(self,controller): """Constructor @param controller The controller to attach the view to """ gtk.VBox.__init__(self,False,10) back_image = gtk.Image() back_image.set_from_file('arrow_back.png') self.back_button = gtk.Button("Prev") self.back_button.set_image(back_image) next_image = gtk.Image() next_image.set_from_file('arrow_next.png') self.next_button = gtk.Button("Next") self.next_button.set_image(next_image) self.arrow_box = gtk.HBox() self.arrow_box.pack_start(self.back_button,False,False) self.arrow_box.pack_end(self.next_button,False,False) self.back_button.set_sensitive(False) self.controller = controller self.tutorial_list = [] #by default tutorials are sorted by name self.sorting_key = 'Name' self.set_border_width(10) #create the list item container with a scroll bar if necessary self.main_container = gtk.ScrolledWindow() self.main_container.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC) self.list_container= gtk.VBox() self.main_container.add_with_viewport(self.list_container) self.pack_start(self.main_container) self.pack_end(self.arrow_box,False,False) self.back_button.connect("clicked",self.controller.prev_page,None) self.next_button.connect("clicked",self.controller.next_page,None) #Show the components self.list_container.show() self.main_container.show() self.arrow_box.show_all() def set_button_sensitive(self,back,next): self.back_button.set_sensitive(back) self.next_button.set_sensitive(next) def change_sorting(self,sorting): """ Changes the property by which tutorial are sorted @param sorting The property by which tutorials will be sorted """ logging.info("Change_sorting was called") self.sorting_key = sorting self.sort_tutorial() def sort_tutorial(self): """ Sorts the tutorials """ #if tutorials are sorted by rating they are in the reverse order self.tutorial_list.sort(key=operator.attrgetter(self.sorting_key.lower())) self.refresh_tutorial_display() def set_tutorial_list(self,tutorial_list): """ Set the list of tutorial to display @param tutorial_list the tutorial list """ self.tutorial_list = tutorial_list self.sort_tutorial() def refresh_tutorial_display(self): """ Refresh the tutorial content by deleting every item and recreating them """ #delete every tutorial list item for child in self.list_container.get_children(): self.list_container.remove(child) #Creates and add a new item for every tutorial for tuto in self.tutorial_list: item = TutorialStoreListItem(tuto,self.controller) self.list_container.pack_start(item) item.show() if self.tutorial_list[-1] != tuto: sep = gtk.HSeparator() self.list_container.pack_start(sep) sep.show() class TutorialStoreListItem(WorkshopListItem): def __init__(self,tutorial,controller): WorkshopListItem.__init__(self,tutorial,controller) self.last_row = gtk.HBox(False,15) self.btn_detail = gtk.Button('Details') self.last_row.pack_end(self.btn_detail,False,False) self.table.attach(self.last_row,1,3,2,3,yoptions = 0) self.last_row.show_all() #connect the buttons self.btn_detail.connect("clicked",self.controller.show_details,self.tutorial)