Exemple #1
0
class HotspotItem(TreeItem):
    def __init__(self, font_size=DEFAULT_FONT_SIZE):
        TreeItem.__init__(self)
        self.entry = None
        self.height = self.get_height()
        self.ssid_buffer = EntryBuffer("")
        self.ssid_buffer.set_property('cursor-visible', False)
        self.password_buffer = EntryBuffer("")
        self.password_buffer.set_property('cursor-visible', False)

        self.ssid_buffer.connect("changed", self.entry_buffer_changed)
        self.ssid_buffer.connect("insert-pos-changed",
                                 self.entry_buffer_changed)
        self.ssid_buffer.connect("selection-pos-changed",
                                 self.entry_buffer_changed)
        self.password_buffer.connect("changed", self.entry_buffer_changed)
        self.password_buffer.connect("insert-pos-changed",
                                     self.entry_buffer_changed)
        self.password_buffer.connect("selection-pos-changed",
                                     self.entry_buffer_changed)

        self.ENTRY_COLUMN = [2, 4]
        self.entry_buffer = None
        self.is_active = False
        self.check_pixbuf = app_theme.get_pixbuf("network/check_box-2.png")
        self.jumpto_pixbuf = app_theme.get_pixbuf("network/jump_to.png")
        self.border_color = border_normal_color
        self.bg_color = bg_normal_color

    def entry_buffer_changed(self, bf):
        if self.redraw_request_callback:
            self.redraw_request_callback(self)

    def render_check(self, cr, rect):
        render_background(cr, rect)

        gap = (rect.height - IMG_WIDTH) / 2

        if self.is_active:
            draw_pixbuf(cr, self.check_pixbuf.get_pixbuf(), rect.x + 10,
                        rect.y + gap)

    def render_ssid(self, cr, rect):
        render_background(cr, rect)

        draw_text(cr,
                  _("SSID:"),
                  rect.x,
                  rect.y,
                  rect.width,
                  rect.height,
                  alignment=pango.ALIGN_LEFT)

    def render_ssid_entry(self, cr, rect):
        render_background(cr, rect)
        with cairo_disable_antialias(cr):
            cr.set_source_rgb(0, 0, 0)
            cr.set_line_width(1)
            cr.rectangle(rect.x, rect.y + 4, rect.width, 22)
            cr.stroke()
        if self.is_select:
            text_color = "#ffffff"
        else:
            text_color = "#000000"
            self.ssid_buffer.move_to_start()

        self.ssid_buffer.set_text_color(text_color)
        height = self.ssid_buffer.get_pixel_size()[1]
        offset = (self.height - height) / 2
        if offset < 0:
            offset = 0
        rect.y += offset
        if self.entry and self.entry.allocation.width == self.get_column_widths(
        )[1] - 4:
            self.entry.calculate()
            rect.x += 2
            rect.width -= 4
            self.ssid_buffer.set_text_color("#000000")
            self.ssid_buffer.render(cr, rect, self.entry.im,
                                    self.entry.offset_x)
        else:
            self.ssid_buffer.render(cr, rect)

    def render_pwd(self, cr, rect):
        render_background(cr, rect)

        draw_text(cr,
                  _("Password:"******"#ffffff"
        else:
            text_color = "#000000"
            self.password_buffer.move_to_start()

        self.password_buffer.set_text_color(text_color)
        height = self.password_buffer.get_pixel_size()[1]
        offset = (self.height - height) / 2
        if offset < 0:
            offset = 0
        rect.y += offset
        if self.entry and self.entry.allocation.width == self.get_column_widths(
        )[1] - 4:
            self.entry.calculate()
            rect.x += 2
            rect.width -= 4
            self.password_buffer.set_text_color("#000000")
            self.password_buffer.render(cr, rect, self.entry.im,
                                        self.entry.offset_x)
        else:
            self.password_buffer.render(cr, rect)

    def render_active(self, cr, rect):
        with cairo_disable_antialias(cr):
            cr.set_source_rgb(0, 0, 0)
            cr.set_line_width(1)
            cr.rectangle(rect.x, rect.y + 4, rect.width, 22)
            cr.stroke()

        draw_text(cr,
                  _("active"),
                  rect.x,
                  rect.y,
                  rect.width,
                  rect.height,
                  alignment=pango.ALIGN_LEFT)

    def render_jump(self, cr, rect):
        render_background(cr, rect)
        gap = (rect.height - IMG_WIDTH) / 2
        draw_pixbuf(cr, self.jumpto_pixbuf.get_pixbuf(), rect.x + 10,
                    rect.y + gap)

    def set_connection_name(self, sdf):
        pass

    def expand(self):
        pass

    def unexpand(self):
        pass

    def get_buffer(self, column):
        buffers = [0, 0, self.ssid_buffer, 0, self.password_buffer, 0]
        self.entry_buffer = buffers[column]
        return self.entry_buffer

    def get_height(self):
        return CONTAINNER_HEIGHT

    def get_column_widths(self):
        return [20, 30, 200, 20, 200, 30, 20]

    def get_column_renders(self):
        return [
            self.render_check, self.render_ssid, self.render_ssid_entry,
            self.render_pwd, self.render_pwd_entry, self.render_active,
            self.render_jump
        ]

    def unselect(self):
        pass

    def select(self):
        pass

    def hover(self, column, offset_x, offset_y):
        self.is_hover = True
        self.border_color = border_hover_color

    def unhover(self, column, offset_x, offset_y):
        #print column, offset_x, offset_y
        self.is_hover = False
        self.border_color = border_normal_color

    def motion_notify(self, column, offset_x, offset_y):
        pass

    def button_press(self, column, offset_x, offset_y):
        pass

    def button_release(self, column, offset_x, offset_y):
        pass

    def single_click(self, column, offset_x, offset_y):
        pass

    def double_click(self, column, offset_x, offset_y):
        pass

    def draw_drag_line(self, drag_line, drag_line_at_bottom=False):
        pass

    def highlight(self):
        pass

    def unhighlight(self):
        pass

    def release_resource(self):
        return False

    def redraw(self):
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
class HotspotItem(TreeItem):

    def __init__(self, font_size=DEFAULT_FONT_SIZE):
        TreeItem.__init__(self)
        self.entry = None
        self.height = self.get_height()
        self.ssid_buffer = EntryBuffer("")
        self.ssid_buffer.set_property('cursor-visible', False)
        self.password_buffer = EntryBuffer("")
        self.password_buffer.set_property('cursor-visible', False)

        self.ssid_buffer.connect("changed", self.entry_buffer_changed)
        self.ssid_buffer.connect("insert-pos-changed", self.entry_buffer_changed)
        self.ssid_buffer.connect("selection-pos-changed", self.entry_buffer_changed)
        self.password_buffer.connect("changed", self.entry_buffer_changed)
        self.password_buffer.connect("insert-pos-changed", self.entry_buffer_changed)
        self.password_buffer.connect("selection-pos-changed", self.entry_buffer_changed)

        self.ENTRY_COLUMN = [2, 4]
        self.entry_buffer = None
        self.is_active = False
        self.check_pixbuf = app_theme.get_pixbuf("network/check_box-2.png")
        self.jumpto_pixbuf = app_theme.get_pixbuf("network/jump_to.png")
        self.border_color = border_normal_color
        self.bg_color = bg_normal_color

    def entry_buffer_changed(self, bf):
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
    
    def render_check(self, cr, rect):
        render_background(cr, rect)

        gap = (rect.height - IMG_WIDTH)/2
        
        if self.is_active:
            draw_pixbuf(cr, self.check_pixbuf.get_pixbuf(), rect.x + 10, rect.y + gap)   

    def render_ssid(self, cr ,rect):
        render_background(cr, rect)
        
        draw_text(cr, _("SSID:"), rect.x, rect.y, rect.width, rect.height,
                alignment = pango.ALIGN_LEFT)

    def render_ssid_entry(self, cr, rect):
        render_background(cr, rect)
        with cairo_disable_antialias(cr):
            cr.set_source_rgb(0, 0, 0)
            cr.set_line_width(1)
            cr.rectangle(rect.x, rect.y + 4, rect.width, 22)
            cr.stroke()
        if self.is_select:
            text_color = "#ffffff"
        else:
            text_color = "#000000"
            self.ssid_buffer.move_to_start()

        self.ssid_buffer.set_text_color(text_color)
        height = self.ssid_buffer.get_pixel_size()[1]
        offset = (self.height - height)/2
        if offset < 0 :
            offset = 0
        rect.y += offset  
        if self.entry and self.entry.allocation.width == self.get_column_widths()[1]-4:
            self.entry.calculate()
            rect.x += 2
            rect.width -= 4
            self.ssid_buffer.set_text_color("#000000")
            self.ssid_buffer.render(cr, rect, self.entry.im, self.entry.offset_x)
        else:
            self.ssid_buffer.render(cr, rect)

    def render_pwd(self, cr, rect):
        render_background(cr, rect)

        draw_text(cr, _("Password:"******"#ffffff"
        else:
            text_color = "#000000"
            self.password_buffer.move_to_start()

        self.password_buffer.set_text_color(text_color)
        height = self.password_buffer.get_pixel_size()[1]
        offset = (self.height - height)/2
        if offset < 0 :
            offset = 0
        rect.y += offset  
        if self.entry and self.entry.allocation.width == self.get_column_widths()[1]-4:
            self.entry.calculate()
            rect.x += 2
            rect.width -= 4
            self.password_buffer.set_text_color("#000000")
            self.password_buffer.render(cr, rect, self.entry.im, self.entry.offset_x)
        else:
            self.password_buffer.render(cr, rect)

    def render_active(self, cr, rect):
        with cairo_disable_antialias(cr):
            cr.set_source_rgb(0, 0, 0)
            cr.set_line_width(1)
            cr.rectangle(rect.x, rect.y + 4, rect.width, 22)
            cr.stroke()

        draw_text(cr, _("active"), rect.x, rect.y, rect.width, rect.height,
                alignment = pango.ALIGN_LEFT)
    
    def render_jump(self, cr, rect):
        render_background(cr, rect)
        gap = (rect.height - IMG_WIDTH)/2
        draw_pixbuf(cr, self.jumpto_pixbuf.get_pixbuf(), rect.x + 10, rect.y + gap)   

    def set_connection_name(self, sdf):
        pass

    def expand(self):
        pass
    
    def unexpand(self):
        pass
    
    def get_buffer(self, column):
        buffers = [0, 0, self.ssid_buffer, 0, self.password_buffer, 0]
        self.entry_buffer = buffers[column]
        return self.entry_buffer
    
    def get_height(self):
        return CONTAINNER_HEIGHT
    
    def get_column_widths(self):
        return [20, 30, 200, 20, 200, 30, 20]

    def get_column_renders(self):
        return [self.render_check, self.render_ssid, self.render_ssid_entry,
                self.render_pwd, self.render_pwd_entry, self.render_active,
                self.render_jump]
    
    def unselect(self):
        pass
    
    def select(self):
        pass
    
    def hover(self, column, offset_x, offset_y):
        self.is_hover = True
        self.border_color = border_hover_color

    def unhover(self, column, offset_x, offset_y):
        #print column, offset_x, offset_y
        self.is_hover = False
        self.border_color = border_normal_color
    
    def motion_notify(self, column, offset_x, offset_y):
        pass
    
    def button_press(self, column, offset_x, offset_y):
        pass        
    
    def button_release(self, column, offset_x, offset_y):
        pass        
    
    def single_click(self, column, offset_x, offset_y):
        pass        

    def double_click(self, column, offset_x, offset_y):
        pass        
    
    def draw_drag_line(self, drag_line, drag_line_at_bottom=False):
        pass

    def highlight(self):
        pass
    
    def unhighlight(self):
        pass
    
    def release_resource(self):
        return False
    
    def redraw(self):
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
class SettingItem(TreeItem):
    CHECK_LEFT_PADDING = FRAME_LEFT_PADDING
    CHECK_RIGHT_PADIING = 10
    def __init__(self, connection, delete_cb, set_button_cb=None):
        TreeItem.__init__(self)
        #self.title = title
        self.connection = connection
        #self.click = click_cb
        self.delete_connection = delete_cb
        self.set_button = set_button_cb
        self.entry = None
        self.entry_buffer = EntryBuffer(connection.get_setting("connection").id)
        self.entry_buffer.set_property('cursor-visible', False)
        self.entry_buffer.connect("changed", self.entry_buffer_changed)
        self.entry_buffer.connect("insert-pos-changed", self.entry_buffer_changed)
        self.entry_buffer.connect("selection-pos-changed", self.entry_buffer_changed)
        self.child_items = []
        self.height = 30
        self.ENTRY_COLUMN = [1]
        self.is_double_click = False

        self.check_select = False
        self.is_hover = False
        self.delete_hover = False
        self.connection_active = False

        '''
        Pixbuf
        '''
        self.check_pixbuf_active = app_theme.get_pixbuf("network/check_box-1.png")
        self.delete_pixbuf_out = app_theme.get_pixbuf("network/delete-3.png")
        self.delete_pixbuf_prelight = app_theme.get_pixbuf("network/delete.png")
        self.delete_pixbuf_active = app_theme.get_pixbuf("network/delete-1.png")
        
    def entry_buffer_changed(self, bf):
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
    
    def get_height(self):
        return self.height
    
    def get_column_widths(self):
        return [47, 20, 37]

    def get_buffer(self, column):
        buffers = [0, self.entry_buffer, 0]
        self.entry_buffer = buffers[column]
        return self.entry_buffer
    
    def get_column_renders(self):
        return [self.render_check, self.render_content, self.render_delete]
    
    def render_check(self, cr, rect):
        self.render_background(cr,rect)
        if self.is_select:
            check_icon = self.check_pixbuf_active
            draw_pixbuf(cr, check_icon.get_pixbuf(), rect.x + self.CHECK_LEFT_PADDING, rect.y + (rect.height - IMG_WIDTH)/2)

    def render_content(self, cr, rect):
        self.render_background(cr,rect)
        if self.is_select:
            bg_color = "#3399FF"
            text_color = "#ffffff"

            if not self.is_double_click:
                pass
        else:
            text_color = "#000000"
            self.entry_buffer.move_to_start()
        self.entry_buffer.set_text_color(text_color)
        height = self.entry_buffer.get_pixel_size()[1]
        offset = (self.height - height)/2
        if offset < 0 :
            offset = 0
        rect.y += offset  
        if self.entry and self.entry.allocation.width == self.get_column_widths()[1]-4:
            self.entry.calculate()
            rect.x += 2
            rect.width -= 4
            self.entry_buffer.set_text_color("#000000")
            self.entry_buffer.render(cr, rect, self.entry.im, self.entry.offset_x)
        else:
            self.entry_buffer.render(cr, rect)

    def render_delete(self, cr, rect):
         
        self.render_background(cr, rect)
        if self.delete_hover:
            delete_icon = self.delete_pixbuf_out
            draw_pixbuf(cr, delete_icon.get_pixbuf(), rect.x + self.CHECK_LEFT_PADDING/2, rect.y + (rect.height - IMG_WIDTH)/2)
        else:
            delete_icon = self.delete_pixbuf_out

        BORDER_COLOR = color_hex_to_cairo("#d2d2d2")
        with cairo_disable_antialias(cr):
            cr.set_source_rgb(*BORDER_COLOR)
            cr.set_line_width(1)
            draw_line(cr, rect.x + rect.width, rect.y, rect.x + rect.width, rect.y + rect.height)

    def render_background(self,  cr, rect):
        if self.is_select:
            background_color = app_theme.get_color("globalItemSelect")
            cr.set_source_rgb(*color_hex_to_cairo(background_color.get_color()))
        else:
            if  self.is_hover:
                background_color = app_theme.get_color("globalItemHover")
                cr.set_source_rgb(*color_hex_to_cairo(background_color.get_color()))
            else:
                background_color = "#ffffff"
                cr.set_source_rgb(*color_hex_to_cairo(background_color))
        cr.rectangle(rect.x, rect.y, rect.width, rect.height)
        cr.fill()

        #draw_hlinear(cr, rect.x - 20, rect.y, rect.width +20, rect.height, background_color)

    def set_active(self, active):
        self.connection_active = active
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
    
    def set_connection_name(self, text):
        self.connection.get_setting("connection").id = text
        log.debug(text)
        if hasattr(self.connection, "settings_obj"):
            self.connection.settings_obj.set_button("save", True)

    def unselect(self):
        self.is_select = False
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
    
    def select(self):
        #print self.get_highlight()
        #Dispatcher.select_connection(self.connection)
        self.is_select = True
        if self.is_hover:
            self.hover(0,0,0)
        # TODO 切换网络连接时更改按钮状态
        Dispatcher.change_setting(self.connection)
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
    
    def hover(self, column, offset_x, offset_y):
        self.is_hover = True
        if self.is_select:
            self.timer = gobject.timeout_add(300, self.show_delete)

    def show_delete(self):
        self.delete_hover = True
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
        return False

    def unhover(self, column, offset_x, offset_y):
        self.is_hover = False
        if hasattr(self, "timer"):
            gobject.source_remove(self.timer)
        self.delete_hover = False
        if self.redraw_request_callback:
            self.redraw_request_callback(self)

    def single_click(self, column, offset_x, offset_y):
        self.is_double_click = False

        if column == 0:
            self.check_select = not self.check_select
            print "check clicked"
        elif column == 2 and self.delete_hover:
            if self.is_hover:
                #self.delete_connection(self.connection)
                print "delete this setting"
                Dispatcher.delete_setting(self.connection)
            
        if self.redraw_request_callback:
            self.redraw_request_callback(self)
    
    def double_click(self, column, offset_x, offset_y):
        self.is_double_click = True

    def expand(self):
        pass

    def unexpand(self):
        pass