Exemple #1
0
class RecommendItem(TreeItem):
    '''
    class docs
    '''
    def __init__(self, data_manager, recommend_status):
        '''
        init docs
        '''
        TreeItem.__init__(self)
        self.name = _("Home")
        self.data_manager = data_manager
        self.recommend_status = recommend_status

        self.page_cache = {}
        self.page_name = ['recommend', 'album', 'download_rank']

        self.init_recommend_page()
        global_event.register_event('download-home-infos-finish',
                                    self.update_home_page)

    def init_recommend_page(self):
        self.recommend_scrolled_window = ScrolledWindow()

        self.background_box = BackgroundBox()
        self.background_box.draw_mask = self.draw_mask

        self.box = gtk.VBox()

        self.loading_box = LoadingBox()
        self.network_failed_box = NetworkConnectFailed(
            self.check_network_connection)
        self.network_timeout_box = NetworkConnectTimeout(
            self.check_network_connection)
        self.timeout_times = 0
        self.recommend_scrolled_window_initial = False

    @post_gui
    def update_home_page(self, data):

        if not data:
            self.check_network_connection()
            return

        slide_infos = sort_for_home_page_data(data['slide'])
        self.slider_switcher = IndexSlideSwitcher(slide_infos)
        self.slider_switcher.connect(
            "motion-notify-index",
            lambda w, i: global_event.emit("set-cursor", gtk.gdk.HAND2))
        self.slider_switcher.connect(
            "button-press-index", lambda w, i: global_event.emit(
                "switch-to-detail-page", slide_infos[i][0]))
        self.slider_switcher.connect(
            "leave-notify-index",
            lambda w, i: global_event.emit("set-cursor", None))

        self.recommend_infos = sort_for_home_page_data(data['recommend'])
        self.tab_switcher = TabSwitcher(
            [_("Recommendations"),
             _("HotApps"),
             _("Download rank")], CATEGORY_ITEM_NAME_SIZE)
        self.tab_switcher_align = gtk.Alignment()
        self.tab_switcher_align.set(0.5, 0.5, 1, 1)
        self.tab_switcher_align.set_padding(10, 0, 0, 9)
        self.tab_switcher_align.add(self.tab_switcher)
        self.tab_switcher_pages_callback = [
            "get_pkg_icon_view_page",
            "get_album_page",
            "get_download_rank_page",
        ]

        self.page_box = gtk.VBox()
        self.box.pack_start(self.slider_switcher, False, False)
        self.box.pack_start(self.tab_switcher_align, False, False)

        self.box_align = gtk.Alignment()
        self.box_align.set(0.5, 0.5, 1, 1)
        self.box_align.set_padding(5, 0, 10, 11)
        self.box_align.add(self.box)

        self.page_box_align = gtk.Alignment()
        self.page_box_align.set(0.5, 0, 1, 1)
        self.page_box_align.add(self.page_box)

        self.background_box.pack_start(self.box_align, False, False)
        self.background_box.pack_start(self.page_box_align)

        self.recommend_scrolled_window.add_child(self.background_box)
        self.switch_page(0)

        self.tab_switcher.connect(
            "tab-switch-start",
            lambda switcher, page_index: self.switch_page(page_index))
        self.tab_switcher.connect(
            "click-current-tab",
            lambda switcher, page_index: self.click_page())
        self.switch_page_view(self.recommend_scrolled_window)
        self.recommend_scrolled_window_initial = True

    def try_fetch_data(self):
        FetchHomeData(LANGUAGE, self.recommend_status).start()
        self.timeout_times += 1

    def check_network_connection(self):
        if is_network_connected():
            if self.timeout_times < NETWORK_TRY_TIMES:
                self.network_connected_flag = True
                self.switch_page_view(self.loading_box)
                self.try_fetch_data()
            else:
                self.network_connected_flag = False
                self.switch_page_view(self.network_timeout_box)
                self.timeout_times = 0
        else:
            self.network_connected_flag = False
            self.switch_page_view(self.network_failed_box)

    def switch_page_view(self, view):
        global_event.emit('show-pkg-view', view)

    def render_name(self, cr, rect):
        text_color = "#333333"
        font_image_color = app_theme.get_color("sidebar_select").get_color()
        if self.is_select:
            cr.set_source_rgba(*color_hex_to_cairo(
                app_theme.get_color("sidebar_select").get_color()))
            cr.rectangle(rect.x, rect.y, rect.width, rect.height)
            cr.fill()

            text_color = "#FFFFFF"
            font_image_color = "#FFFFFF"
        elif self.is_hover:
            cr.set_source_rgba(*color_hex_to_cairo(
                app_theme.get_color("sidebar_hover").get_color()))
            cr.rectangle(rect.x, rect.y, rect.width, rect.height)
            cr.fill()

        pixbuf = app_theme.get_pixbuf("category/12.png").get_pixbuf()
        #draw_pixbuf(
        #cr,
        #pixbuf,
        #rect.x + 12,
        #rect.y + (rect.height - pixbuf.get_height()) / 2)

        category_img_path = os.path.join(get_parent_dir(__file__, 2), "image",
                                         "category", "1.png")
        surface = cairo.ImageSurface.create_from_png(category_img_path)
        cr.set_source_rgb(*color_hex_to_cairo(font_image_color))
        cr.mask_surface(surface, rect.x + 14, rect.y + (rect.height - 24) / 2)

        draw_text(
            cr,
            self.name,
            rect.x + pixbuf.get_width() + 22,
            rect.y,
            rect.width,
            rect.height,
            text_size=CATEGORY_ITEM_NAME_SIZE,
            text_color=text_color,
        )

    def get_height(self):
        return CATEGORY_ITEM_HEIGHT

    def get_column_widths(self):
        return [CATEGORY_ITEM_NAME_WIDTH]

    def get_column_renders(self):
        return [self.render_name]

    def hover(self, column, offset_x, offset_y):
        self.is_hover = True
        if self.redraw_request_callback:
            self.redraw_request_callback(self)

    def unhover(self, column, offset_x, offset_y):
        self.is_hover = False
        if self.redraw_request_callback:
            self.redraw_request_callback(self)

    def select(self):
        self.is_select = True
        if self.redraw_request_callback:
            self.redraw_request_callback(self)

    def unselect(self):
        self.is_select = False
        if self.redraw_request_callback:
            self.redraw_request_callback(self)

    def button_press(self, column, offset_x, offset_y):
        self.show_page()

    def get_pkg_icon_view_page(self):
        items = []
        for info in self.recommend_infos:
            items.append(RecommendIconItem(info))

        self.pkg_icon_view = IconView()
        self.pkg_icon_view.add_items(items)
        self.pkg_icon_scrolled_window = ScrolledWindow()
        self.pkg_icon_scrolled_window.add_child(self.pkg_icon_view)
        self.pkg_icon_view.draw_mask = self.draw_mask

        self.pkg_icon_view_align = gtk.Alignment()
        self.pkg_icon_view_align.set(0.5, 0.5, 1, 1)
        self.pkg_icon_view_align.set_padding(6, 0, 1, 11)
        self.pkg_icon_view_align.add(self.pkg_icon_scrolled_window)
        return self.pkg_icon_view_align

    def get_download_rank_page(self):
        self.download_rank_page = DownloadRankPage(self.data_manager)
        return self.download_rank_page

    def get_album_page(self):
        self.album_page = AlbumPage(self.data_manager)
        return self.album_page

    # from deepin_utils.date_time import print_exec_time
    # @print_exec_time
    def show_page(self):
        if self.recommend_scrolled_window_initial:
            global_event.emit("show-pkg-view", self.recommend_scrolled_window)
        else:
            self.check_network_connection()

    def draw_blank_mask(self, cr, x, y, w, h):
        pass

    def draw_mask(self, cr, x, y, w, h):
        '''
        Draw mask interface.

        @param cr: Cairo context.
        @param x: X coordiante of draw area.
        @param y: Y coordiante of draw area.
        @param w: Width of draw area.
        @param h: Height of draw area.
        '''
        draw_vlinear(cr, x, y, w, h, [
            (0, ("#FFFFFF", 0.9)),
            (1, ("#FFFFFF", 0.9)),
        ])

    def click_page(self):
        pass
        #if isinstance(self.active_page, AlbumPage) and self.active_page.in_detail_view:
        #self.active_page.switch_to_album_summary_view()

    def switch_page(self, page_index):
        container_remove_all(self.page_box)
        page_name = self.page_name[page_index]

        if not self.page_cache.has_key(self.page_name[page_index]):
            self.page_cache[page_name] = getattr(
                self, self.tab_switcher_pages_callback[page_index])()

        self.active_page = self.page_cache[page_name]
        self.page_box.pack_start(self.active_page, True, True)

        #if isinstance(self.active_page, AlbumPage):
        #if self.active_page.in_detail_view:
        #self.active_page.switch_to_album_summary_view()

        self.recommend_scrolled_window.show_all()
class RecommendItem(TreeItem):
    '''
    class docs
    '''
    def __init__(self, data_manager, recommend_status):
        '''
        init docs
        '''
        TreeItem.__init__(self)
        self.name = _("Home")
        self.data_manager = data_manager
        self.recommend_status = recommend_status
        
        self.page_cache = {}
        self.page_name = ['recommend', 'album', 'download_rank']
        
        self.init_recommend_page()
        global_event.register_event('download-home-infos-finish', self.update_home_page)
        
    def init_recommend_page(self):    
        self.recommend_scrolled_window = ScrolledWindow()
        
        self.background_box = BackgroundBox()
        self.background_box.draw_mask = self.draw_mask
        
        self.box = gtk.VBox()

        self.loading_box = LoadingBox()
        self.network_failed_box = NetworkConnectFailed(self.check_network_connection)
        self.network_timeout_box = NetworkConnectTimeout(self.check_network_connection)
        self.timeout_times = 0
        self.recommend_scrolled_window_initial = False

    @post_gui
    def update_home_page(self, data):

        if not data:
            self.check_network_connection()
            return

        slide_infos = sort_for_home_page_data(data['slide'])
        self.slider_switcher = IndexSlideSwitcher(slide_infos)
        self.slider_switcher.connect("motion-notify-index", 
                lambda w, i: global_event.emit("set-cursor", gtk.gdk.HAND2))
        self.slider_switcher.connect("button-press-index", 
                lambda w, i: global_event.emit("switch-to-detail-page", slide_infos[i][0]))
        self.slider_switcher.connect("leave-notify-index", lambda w, i: global_event.emit("set-cursor", None))

        self.recommend_infos = sort_for_home_page_data(data['recommend'])
        self.tab_switcher = TabSwitcher([_("Recommendations"), _("Topics"), _("Download rank")], CATEGORY_ITEM_NAME_SIZE)
        self.tab_switcher_align = gtk.Alignment()
        self.tab_switcher_align.set(0.5, 0.5, 1, 1)
        self.tab_switcher_align.set_padding(10, 0, 0, 9)
        self.tab_switcher_align.add(self.tab_switcher)
        self.tab_switcher_pages_callback = [
                "get_pkg_icon_view_page",
                "get_album_page",
                "get_download_rank_page",
                ]
        
        self.page_box = gtk.VBox()
        self.box.pack_start(self.slider_switcher, False, False)
        self.box.pack_start(self.tab_switcher_align, False, False)
        
        self.box_align = gtk.Alignment()
        self.box_align.set(0.5, 0.5, 1, 1)
        self.box_align.set_padding(5, 0, 10, 11)
        self.box_align.add(self.box)

        self.page_box_align = gtk.Alignment()
        self.page_box_align.set(0.5, 0, 1, 1)
        self.page_box_align.add(self.page_box)

        self.background_box.pack_start(self.box_align, False, False)
        self.background_box.pack_start(self.page_box_align)
        
        self.recommend_scrolled_window.add_child(self.background_box)
        self.switch_page(0)
        
        self.tab_switcher.connect("tab-switch-start", lambda switcher, page_index: self.switch_page(page_index))
        self.tab_switcher.connect("click-current-tab", lambda switcher, page_index: self.click_page())
        self.switch_page_view(self.recommend_scrolled_window)
        self.recommend_scrolled_window_initial = True
    
    def try_fetch_data(self):
        FetchHomeData(LANGUAGE, self.recommend_status).start()
        self.timeout_times += 1

    def check_network_connection(self):
        if is_network_connected():
            if self.timeout_times < NETWORK_TRY_TIMES:
                self.network_connected_flag = True
                self.switch_page_view(self.loading_box)
                self.try_fetch_data()
            else:
                self.network_connected_flag = False
                self.switch_page_view(self.network_timeout_box)
                self.timeout_times = 0
        else:    
            self.network_connected_flag = False
            self.switch_page_view(self.network_failed_box)

    def switch_page_view(self, view):
        global_event.emit('show-pkg-view', view)
    
    def render_name(self, cr, rect):
        text_color = "#333333"
        font_image_color = app_theme.get_color("sidebar_select").get_color()
        if self.is_select:
            cr.set_source_rgba(*color_hex_to_cairo(app_theme.get_color("sidebar_select").get_color()))
            cr.rectangle(rect.x, rect.y, rect.width, rect.height)
            cr.fill()
            
            text_color = "#FFFFFF"
            font_image_color = "#FFFFFF"
        elif self.is_hover:
            cr.set_source_rgba(*color_hex_to_cairo(app_theme.get_color("sidebar_hover").get_color()))
            cr.rectangle(rect.x, rect.y, rect.width, rect.height)
            cr.fill()
        
        pixbuf = app_theme.get_pixbuf("category/12.png").get_pixbuf()
        #draw_pixbuf(
            #cr,
            #pixbuf,
            #rect.x + 12,
            #rect.y + (rect.height - pixbuf.get_height()) / 2)
        
        category_img_path = os.path.join(get_parent_dir(__file__, 2), "image", "category", "1.png")
        surface = cairo.ImageSurface.create_from_png(category_img_path)
        cr.set_source_rgb(*color_hex_to_cairo(font_image_color))
        cr.mask_surface(surface, rect.x+14, rect.y+(rect.height-24)/2)

        draw_text(cr, 
                  self.name,
                  rect.x + pixbuf.get_width() + 22,
                  rect.y,
                  rect.width,
                  rect.height,
                  text_size=CATEGORY_ITEM_NAME_SIZE,
                  text_color=text_color,
                  )
        
    def get_height(self):
        return CATEGORY_ITEM_HEIGHT
    
    def get_column_widths(self):
        return [CATEGORY_ITEM_NAME_WIDTH]
        
    def get_column_renders(self):    
        return [self.render_name]
        
    def hover(self, column, offset_x, offset_y):
        self.is_hover = True
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
        
    def unhover(self, column, offset_x, offset_y):
        self.is_hover = False
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
    
    def select(self):
        self.is_select = True
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
        
    def unselect(self):
        self.is_select = False
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
            
    def button_press(self, column, offset_x, offset_y):
        self.show_page()

    def get_pkg_icon_view_page(self):
        items = []
        for info in self.recommend_infos:
            items.append(RecommendIconItem(info))
        
        self.pkg_icon_view = IconView() 
        self.pkg_icon_view.add_items(items)
        self.pkg_icon_scrolled_window = ScrolledWindow()
        self.pkg_icon_scrolled_window.add_child(self.pkg_icon_view)
        self.pkg_icon_view.draw_mask = self.draw_mask
        
        self.pkg_icon_view_align = gtk.Alignment()
        self.pkg_icon_view_align.set(0.5, 0.5, 1, 1)
        self.pkg_icon_view_align.set_padding(6, 0, 1, 11)
        self.pkg_icon_view_align.add(self.pkg_icon_scrolled_window)
        return self.pkg_icon_view_align

    def get_download_rank_page(self):
        self.download_rank_page = DownloadRankPage(self.data_manager)
        return self.download_rank_page
    
    def get_album_page(self):
        self.album_page = AlbumPage(self.data_manager)
        return self.album_page
        
    # from deepin_utils.date_time import print_exec_time
    # @print_exec_time
    def show_page(self):
        if self.recommend_scrolled_window_initial:        
            global_event.emit("show-pkg-view", self.recommend_scrolled_window)
        else:
            self.check_network_connection()
        
    def draw_blank_mask(self, cr, x, y, w, h):
        pass
        
    def draw_mask(self, cr, x, y, w, h):
        '''
        Draw mask interface.
        
        @param cr: Cairo context.
        @param x: X coordiante of draw area.
        @param y: Y coordiante of draw area.
        @param w: Width of draw area.
        @param h: Height of draw area.
        '''
        draw_vlinear(cr, x, y, w, h,
                     [(0, ("#FFFFFF", 0.9)),
                      (1, ("#FFFFFF", 0.9)),]
                     )
        
    def click_page(self):
        if isinstance(self.active_page, AlbumPage) and self.active_page.in_detail_view:
            self.active_page.switch_to_album_summary_view()
        
    def switch_page(self, page_index):
        container_remove_all(self.page_box)
        page_name = self.page_name[page_index]

        if not self.page_cache.has_key(self.page_name[page_index]):
            self.page_cache[page_name] = getattr(self, self.tab_switcher_pages_callback[page_index])()

        self.active_page = self.page_cache[page_name]
        self.page_box.pack_start(self.active_page, True, True)
        
        if isinstance(self.active_page, AlbumPage):
            if self.active_page.in_detail_view:
                self.active_page.switch_to_album_summary_view()

        self.recommend_scrolled_window.show_all()