Esempio n. 1
0
class SearchPage(gtk.VBox):
    '''
    class docs
    '''
    def __init__(self, module_infos):
        '''
        init docs
        '''
        gtk.VBox.__init__(self)

        self.scrolled_window = ScrolledWindow()
        self.scrolled_window.set_size_request(800, 425)
        self.result_align = self.__setup_align(
            padding_top=TEXT_WINDOW_TOP_PADDING,
            padding_left=TEXT_WINDOW_LEFT_PADDING)
        self.result_box = gtk.VBox()
        self.result_box.connect("expose-event", self.__expose)
        self.result_align.add(self.result_box)
        '''
        struct keywords {
            module_id, 
            module_name, 
            module_keywords {
                uid, 
                keyword
            }
        }
        '''
        self.__keywords = []

        self.__module_infos = module_infos
        '''
        TODO: from EACH MODULE import keywords
        '''
        for module_info_list in self.__module_infos:
            for module_info in module_info_list:
                if module_info.search_keyword != "None":
                    try:
                        module = __import__(
                            "%s.%s" %
                            (module_info.id, module_info.search_keyword),
                            fromlist=["keywords"])
                        self.__keywords.append(
                            (module_info.id, module_info.name, module.keywords,
                             module_info.menu_icon_pixbuf))
                    except Exception, e:
                        print "Error %s %s" % (module_info.id, e)
                        continue

        self.__keyword_search = KeywordSearch(self.__keywords)
        self.scrolled_window.add_child(self.result_align)
        self.pack_start(self.scrolled_window)
class SearchPage(gtk.VBox):
    '''
    class docs
    '''
	
    def __init__(self, module_infos):
        '''
        init docs
        '''
        gtk.VBox.__init__(self)

        self.scrolled_window = ScrolledWindow()
        self.scrolled_window.set_size_request(800, 425)
        self.result_align = self.__setup_align(padding_top = TEXT_WINDOW_TOP_PADDING, 
                                               padding_left = TEXT_WINDOW_LEFT_PADDING)
        self.result_box = gtk.VBox()
        self.result_box.connect("expose-event", self.__expose)
        self.result_align.add(self.result_box)

        '''
        struct keywords {
            module_id, 
            module_name, 
            module_keywords {
                uid, 
                keyword
            }
        }
        '''
        self.__keywords = []

        self.__module_infos = module_infos
        '''
        TODO: from EACH MODULE import keywords
        '''
        for module_info_list in self.__module_infos:
            for module_info in module_info_list:
                if module_info.search_keyword != "None":
                    try:
                        module = __import__("%s.%s" % (module_info.id, module_info.search_keyword), fromlist=["keywords"])
                        self.__keywords.append((module_info.id, module_info.name, module.keywords, module_info.menu_icon_pixbuf))
                    except Exception, e:
                        print "Error %s %s" % (module_info.id, e)
                        continue

        self.__keyword_search = KeywordSearch(self.__keywords)
        self.scrolled_window.add_child(self.result_align)
        self.pack_start(self.scrolled_window)
Esempio n. 3
0
class ShareToWeibo(object):
    '''share picture to weibo'''
    def __init__(self, filename=""):
        '''
        init share
        @param filename: the file to share
        '''
        self.upload_image = filename
        self.thumb_width = 188
        self.thumb_height = 168
        self.MAX_CHAR = 140
        #self.__text_frame_color = (0.76, 0.76, 0.76)
        self.__win_width = 602
        open(COOKIE_FILE,'wb').close()

        self.window = DialogBox(_("Share to social networks"), close_callback=gtk.main_quit)
        self.window.set_keep_above(True)
        self.window.set_size_request(self.__win_width+20, 288)
        self.window.set_resizable(False)
        self.window.titlebar.connect("expose-event", self.__expose_top_and_bottome)
        self.window.button_box.connect("expose-event", self.__expose_top_and_bottome)

        # create slider
        self.slider = HSlider()
        self.slider_list = []

        self.share_box = gtk.VBox(False, 2)     # first page, input context
        self.web_box = gtk.VBox(False, 10)      # second page, login
        self.result_box = gtk.VBox(False, 10)   # third page, share result

        share_align = gtk.Alignment()
        share_align.set(0.5, 0.5, 0, 0)
        share_align.add(self.share_box)
        share_align.connect("expose-event", self.__slider_expose)

        # go back button
        web_left_button = ImageButton(
            app_theme.get_pixbuf("share/back_normal.png"),
            app_theme.get_pixbuf("share/back_hover.png"),
            app_theme.get_pixbuf("share/back_press.png"))
        web_left_button.connect("clicked", lambda w: self.set_slide_index(0))
        web_left_button.set_can_focus(False)
        utils.set_clickable_cursor(web_left_button)
        # show url entry
        self.web_url_entry = InputEntry()
        self.web_url_entry.set_editable(False)
        self.web_url_entry.set_size(555, 20)
        self.web_url_entry.entry.right_menu_visible_flag = False
        # alig url entry
        web_navigate_vbox = gtk.VBox(False)
        web_navigate_vbox.pack_start(self.web_url_entry)
        web_navigate_t_align = gtk.Alignment()
        web_navigate_t_align.set(0.0, 0.5, 0, 0)
        web_navigate_t_align.add(web_navigate_vbox)
        # pack back button and url entry
        web_navigate_box = gtk.HBox(False, 7)
        web_navigate_box.pack_start(web_left_button, False, False)
        web_navigate_box.pack_start(web_navigate_t_align)

        web_navigate_align = gtk.Alignment()
        web_navigate_align.set(0.5, 0.5, 0, 0)
        web_navigate_align.set_padding(4, 0, 11, 13)
        web_navigate_align.add(web_navigate_box)

        # create a webkit
        self.web_view = WebView(COOKIE_FILE)
        self.web_view.connect("notify::load-status", self.web_view_load_status)
        self.web_view.connect("load-error", self.web_view_load_error)
        self.web_scrolled_window = ScrolledWindow()
        self.web_scrolled_window.add(self.web_view)
        self.web_scrolled_window.set_size_request(590, 228)

        self.web_box.pack_start(web_navigate_align, False, False)
        self.web_box.pack_start(self.web_scrolled_window)
        #self.web_box.set_size_request(-1, 258)
        web_align = gtk.Alignment()
        web_align.set(0.5, 0.0, 0, 1)
        web_align.add(self.web_box)
        web_align.connect("expose-event", self.__slider_expose)

        res_align = gtk.Alignment()
        res_align.set(0.5, 0.5, 0, 0)
        res_align.add(self.result_box)
        res_align.connect("expose-event", self.__slider_expose)

        self.slider.set_to_page(share_align)
        self.slider_list.append(share_align)
        self.slider_list.append(web_align)
        self.slider_list.append(res_align)

        self.__weibo_list = []
        self.sina = weibo.Sina(self.web_view)
        self.qq = weibo.Tencent(self.web_view)
        self.twitter = weibo.Twitter(self.web_view)
        self.__weibo_list.append(self.sina)
        self.__weibo_list.append(self.qq)
        self.__weibo_list.append(self.twitter)
        self.__current_weibo = None

        self.window.body_box.pack_start(self.slider, True, True)
        self.init_share_box()

    # webkit load-status, login success, go back
    def web_view_load_status(self, web, status):
        '''web_view notify load-status callback'''
        state = web.get_property("load-status")
        url = web.get_property('uri')
        if url:
            self.web_url_entry.set_editable(True)
            self.web_url_entry.set_text(url)
            self.web_url_entry.entry.move_to_start()
            self.web_url_entry.set_editable(False)
        if state == webkit.LOAD_FAILED:  # load failed
            print "load failed",
            print web.get_property('uri')

        elif state == webkit.LOAD_COMMITTED:
            if self.__current_weibo and self.__current_weibo.is_callback_url(url):
                web.stop_loading()  # if go to  callback url, stop loading
                # access token
                #print "load committed", url
                t = threading.Thread(target=self.weibo_login_thread)
                t.setDaemon(True)
                t.start()

    def web_view_load_error(self, web, fram, url, error, data=None):
        web.load_string(
            "<html><body><p><h1>%s</h1></p>%s</body></html>" % (
            _("Unable to load page"),
            _("Problem occurred while loading the URL '%s'") % (url)),
            "text/html", "UTF-8", "")
        print url
        return True
    
    # login or switch user
    def weibo_login(self, widget, weibo):
        '''weibo button clicked callback'''
        self.web_view.load_uri("about:blank")
        utils.set_cursor(widget)
        self.set_slide_index(1)
        self.__current_weibo = weibo
        t = threading.Thread(target=self.__current_weibo.request_oauth)
        t.setDaemon(True)
        t.start()

    def weibo_login_thread(self):
        '''in webkit login finish, get user info again'''
        self.__current_weibo.access_token()
        self.get_user_info_again()
        gtk.gdk.threads_enter()
        self.set_slide_index(0)
        gtk.gdk.threads_leave()

    def get_user_info_again(self):
        ''' login or switch user, and get user info again'''
        box = self.__current_weibo.get_box()
        #print "cuurent weibo:", self.__current_weibo.t_type
        gtk.gdk.threads_enter()
        children = box.get_children()
        for child in children:
            if child in self.__weibo_check_button_list:
                self.__weibo_check_button_list.remove(child)
            if child in self.__weibo_image_button_list:
                self.__weibo_image_button_list.remove(child)
            child.destroy()
        gtk.gdk.threads_leave()

        self.get_user_info(self.__current_weibo)

        gtk.gdk.threads_enter()
        box.show_all()
        gtk.gdk.threads_leave()

    def set_slide_index(self, index):
        '''
        set slide to index
        @param index: the index of widget in slider, an int num
        '''
        if index >= len(self.slider_list):
            return
        direct = "right"
        if index == 1 and self.window.button_box in self.window.window_frame.get_children():
            #self.slider.set_size_request(-1, 260)
            win = self.window
            if win.left_button_box in win.button_box.get_children():
                win.button_box.remove(win.left_button_box)
            if win.right_button_box in win.button_box.get_children():
                win.button_box.remove(win.right_button_box)
            tmp = gtk.HSeparator()
            tmp.set_size_request(-1, 1)
            tmp.show()
            win.button_box.pack_start(tmp)
            direct = "right"
            #if self.window.button_box in self.window.window_frame.get_children():
                #self.window.window_frame.remove(self.window.button_box)
        elif index == 0:
            #self.slider.set_size_request(-1, 223)
            win = self.window
            for each in win.button_box.get_children():
                each.destroy()
            if win.left_button_box not in win.button_box.get_children():
                win.button_box.pack_start(win.left_button_box)
            if win.right_button_box not in win.button_box.get_children():
                win.button_box.pack_start(win.right_button_box)
            direct = "left"
            #if self.window.button_box not in self.window.window_frame.get_children():
                #self.window.window_frame.pack_start(self.window.button_box, False, False)
        elif index == 2:
            self.window.left_button_box.set_buttons([])
            l = Label("  ")
            l.show()
            self.window.right_button_box.set_buttons([l])
            direct = "right"
            #self.slider.set_size_request(-1, 223)
            
        self.slider.slide_to_page(self.slider_list[index], direct)

    def weibo_check_toggle(self, button, weibo):
        '''weibo check button toggled callback. check the weibo to share'''
        if button.get_active():
            self.to_share_weibo[weibo] = 1
        else:
            self.to_share_weibo[weibo] = 0

    def create_ico_image(self, name):
        ''' create image from file'''
        pix1 = app_theme_get_dynamic_pixbuf('image/share/%s.png' % name).get_pixbuf()
        pix2 = app_theme_get_dynamic_pixbuf('image/share/%s_no.png' % name).get_pixbuf()
        return (pix1, pix2)
    
    def get_user_info(self, weibo):
        '''get weibo user info'''
        info = weibo.get_user_name()
        gtk.gdk.threads_enter()
        #self.get_user_error_text = ""
        weibo_hbox = weibo.get_box()
        hbox = gtk.HBox(False)
        vbox = gtk.VBox(False)
        weibo_hbox.pack_start(vbox, False, False)
        vbox.pack_start(hbox)
        #print weibo.t_type, info
        if info:
            self.is_get_user_info[weibo] = 1
            label = Label(text=info, label_width=70, enable_select=False)
            check = CheckButton()
            #check = gtk.CheckButton()
            check.connect("toggled", self.weibo_check_toggle, weibo)
            check.set_active(True)
            check_vbox = gtk.VBox(False)
            check_align = gtk.Alignment(0.5, 0.5, 0, 0)
            check_align.add(check_vbox)
            check_vbox.pack_start(check, False, False)
            button = ImageButton(
                app_theme.get_pixbuf("share/" + weibo.t_type + ".png"),
                app_theme.get_pixbuf("share/" + weibo.t_type + ".png"),
                app_theme.get_pixbuf("share/" + weibo.t_type + ".png"))
            utils.set_clickable_cursor(button)
            button.connect("enter-notify-event", self.show_tooltip, _("Click to switch user"))
            hbox.pack_start(check_align, False, False)
            hbox.pack_start(button, False, False, 5)
            hbox.pack_start(label, False, False)
        else:
            self.is_get_user_info[weibo] = 0
            check = CheckButton()
            #check = gtk.CheckButton()
            check.set_sensitive(False)
            check_vbox = gtk.VBox(False)
            check_align = gtk.Alignment(0.5, 0.5, 0, 0)
            check_align.add(check_vbox)
            check_vbox.pack_start(check, False, False)
            button = ImageButton(
                app_theme.get_pixbuf("share/" + weibo.t_type + "_no.png"),
                app_theme.get_pixbuf("share/" + weibo.t_type + "_no.png"),
                app_theme.get_pixbuf("share/" + weibo.t_type + "_no.png"))
            utils.set_clickable_cursor(button)
            button.connect("enter-notify-event", self.show_tooltip, _("Click to login"))
            hbox.pack_start(check_align, False, False)
            hbox.pack_start(button, False, False, 5)
            # curl time out
            info_error = weibo.get_curl_error()
            if info_error:
                #self.get_user_error_text += "%s:%s." % (weibo.t_type, _(info_error))
                hbox.pack_start(
                    Label(text="(%s)" % _(info_error), label_width=70,enable_select=False,
                    text_color = app_theme.get_color("left_char_num1")), False, False)
            
        button.connect("clicked", self.weibo_login, weibo)
        self.__weibo_check_button_list.append(check)
        self.__weibo_image_button_list.append(button)
        gtk.gdk.threads_leave()
        return weibo_hbox
    
    def show_tooltip(self, widget, event, text):
        '''Create help tooltip.'''
        Tooltip.text(widget, text)

    def init_user_info_thread(self, button, text_view):
        '''get user name thread'''
        time.sleep(0.1)

        for weibo in self.__weibo_list:
            self.get_user_info(weibo)

        gtk.gdk.threads_enter()
        #self.share_box.set_sensitive(True)
        button.set_sensitive(True)
        text_view.set_editable(True)
        for weibo in self.__weibo_list:
            weibo.get_box().show_all()
            weibo.get_box().queue_draw()
        self.loading_label.destroy()
        gtk.gdk.threads_leave()
    
    # init share box, create share button, input
    def init_share_box(self):
        '''get weibo info, and create button'''
        self.to_share_weibo = {}
        self.to_share_weibo_res = {}
        self.deepin_info = {}
        self.is_get_user_info = {}
        self.__weibo_check_button_list = []
        self.__weibo_image_button_list = []

        # create Thumbnail
        if exists(self.upload_image):
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.upload_image)
            pix_w = pixbuf.get_width()
            pix_h = pixbuf.get_height()
            if pix_w > pix_h:
                pix_s_w = self.thumb_width
                pix_s_h = int(pix_h / (float(pix_w) / self.thumb_width))
            else:
                pix_s_h = self.thumb_height
                pix_s_w = int(pix_w / (float(pix_h) / self.thumb_height))
            pixbuf = pixbuf.scale_simple(pix_s_w, pix_s_h, gtk.gdk.INTERP_TILES)
            thumb = gtk.image_new_from_pixbuf(pixbuf)
        else:
            thumb = gtk.Image()
        thumb.set_size_request(self.thumb_width, self.thumb_height)

        # weibo context input
        text_box = gtk.HBox(False, 2)
        text_vbox = gtk.VBox(False, 2)
        text_bg_vbox = gtk.VBox(False)
        text_bg_align = gtk.Alignment()
        text_bg_align.set(0.5, 0.5, 0, 0)
        text_bg_align.set_padding(5, 5, 16, 5)
        text_bg_align.connect("expose-event", self.text_view_bg_expose)
        text_scrolled_win = ScrolledWindow()
        text_scrolled_win.set_size_request(340, 157)

        text_view = gtk.TextView()
        text_view.set_left_margin(10)
        text_view.set_right_margin(10)
        text_view.set_pixels_above_lines(5)
        text_view.set_pixels_below_lines(5)
        text_view.set_wrap_mode(gtk.WRAP_WORD| gtk.WRAP_CHAR)
        text_view.connect("expose-event", self.text_view_expose)
        buf = text_view.get_buffer()
        text_scrolled_win.add(text_view)
        text_bg_vbox.pack_start(text_scrolled_win)
        text_bg_align.add(text_bg_vbox)

        text_align = gtk.Alignment() 
        text_align.set(0.5, 0.5, 0, 0)
        text_align.set_padding(25, 30, 10, 10)

        text_box.pack_start(thumb, False, False, 10)
        text_box.pack_start(text_bg_align)
        text_vbox.pack_start(text_box, False, False, 10)

        text_align.add(text_vbox)
        #tmp_align = gtk.Alignment()
        #tmp_align.set(0.5, 0, 0, 1)
        #self.share_box.pack_start(tmp_align, False, False)
        self.share_box.pack_start(text_align, False, False)

        # dialog button box
        left_box = self.window.left_button_box
        right_box = self.window.right_button_box

        # input tip label
        self.input_num_label = Label("%d" % self.MAX_CHAR,
            text_size=16, text_x_align=pango.ALIGN_CENTER, label_width=50, enable_select=False)
        self.input_num_label.text_color = app_theme.get_color("left_char_num")

        # login box
        #weibo_box = gtk.HBox(False, 1)
        #weibo_box.set_size_request(-1, 50)
        weibo_box_list = []
        self.loading_label = Label("%s..." % _("Loading"), text_size=12,
            label_width=70, enable_select=False)
        weibo_box_list.append(self.loading_label)

        for weibo in self.__weibo_list:
            box = gtk.HBox(False, 2)
            weibo.set_box(box)
            weibo_box_list.append(box)
        left_box.set_buttons(weibo_box_list)

        # share button
        button = Button(_("Share"))
        #button.set_size_request(75, 25)
        button.connect("clicked", self.share_button_clicked, text_view)
        buf.connect("changed", self.text_view_changed, button)  # check char num

        tmp_vbox = gtk.VBox(False)
        tmp_align = gtk.Alignment()
        tmp_align.set(0.5, 0.5, 0, 0)
        tmp_vbox.pack_start(button, False, False)
        #tmp_vbox.pack_start(tmp_align)
        tmp_align.add(tmp_vbox)
        right_box.set_buttons([self.input_num_label, tmp_align])

        # at first, set widget insensitive
        button.set_sensitive(False)
        text_view.set_editable(False)
        t = threading.Thread(target=self.init_user_info_thread, args=(button, text_view))
        t.setDaemon(True)
        t.start()

    # draw text view background
    def text_view_bg_expose(self, widget, event):
        '''draw text view bg'''
        cr = widget.window.cairo_create()
        rect = widget.allocation
        text_pixbuf = app_theme_get_dynamic_pixbuf('image/share/text_view.png').get_pixbuf()
        draw.draw_pixbuf(cr, text_pixbuf, rect.x, rect.y)

    # if text is empty, show tip info
    def text_view_expose(self, text_view, event):
        '''text_view expose'''
        buf = text_view.get_buffer()
        text = buf.get_text(*buf.get_bounds())

        if text == "" and text_view.get_editable() and not text_view.is_focus():
            win = text_view.get_window(gtk.TEXT_WINDOW_TEXT)
            cr = win.cairo_create()
            cr.move_to(10, 5)
            context = pangocairo.CairoContext(cr)
            layout = context.create_layout()
            layout.set_font_description(pango.FontDescription("Snas 10"))
            layout.set_alignment(pango.ALIGN_LEFT)
            layout.set_text(_("Please input text here"))
            cr.set_source_rgb(0.66, 0.66, 0.66)
            context.update_layout(layout)
            context.show_layout(layout)
    
    # show input char num
    def text_view_changed(self, buf, button):
        '''text_view changed callback'''
        count = buf.get_char_count()
        if count <= self.MAX_CHAR:
            #self.input_tip_label.set_text(_("left"))
            self.input_num_label.set_text("%d" % (self.MAX_CHAR - count))
            self.input_num_label.text_color = app_theme.get_color("left_char_num")
            if not button.is_sensitive():
                button.set_sensitive(True)
        else:
            #self.input_tip_label.set_text(_("exceeds"))
            self.input_num_label.set_text("-%d" % (count - self.MAX_CHAR))
            self.input_num_label.text_color = app_theme.get_color("left_char_num1")
            if button.is_sensitive():
                button.set_sensitive(False)

    def share_button_clicked(self, button, text_view):
        '''share_button_clicked callback'''
        # file is not exist.
        if not exists(self.upload_image):
            d = ConfirmDialog(_("Error"), "%s." % ( _("Picture does not exist.")))
            d.show_all()
            d.set_transient_for(self.window)
            return False
        has_share_web = False
        for weibo in self.to_share_weibo:
            if self.to_share_weibo[weibo]:
                has_share_web = True
                break
        # have no web selected
        if not has_share_web:
            d = ConfirmDialog(_("Error"), _("Please choose at least one platform to share on"))
            d.show_all()
            d.set_transient_for(self.window)
            return False
        # at first, set widget insensitive
        button.set_sensitive(False)
        text_view.set_editable(False)
        #self.window.left_button_box.set_sensitive(False)
        # set weibo checkbutton sensitive
        for check in self.__weibo_check_button_list:
            check.set_sensitive(False)
        # disconnect weibo ico button clicked function
        for img in self.__weibo_image_button_list:
            try:
                img.disconnect_by_func(self.weibo_login)
            except:
                pass
        button.set_label(_("Uploading"))
        t = threading.Thread(target=self.share_to_weibo_thread, args=(text_view, ))
        t.setDaemon(True)
        t.start()
    
    # upload image thread
    def share_to_weibo_thread(self, text_view):
        '''share in thread'''
        buf = text_view.get_buffer()
        text = buf.get_text(*buf.get_bounds())
        if text.strip() == "":
            text = _("from Deepin Screenshot")
        # get deepin official info
        self.deepin_info[self.sina] = self.sina.get_deepin_info()
        self.deepin_info[self.qq] = self.qq.get_deepin_info()
        if default_locale != 'zh_CN':
            self.deepin_info[self.twitter] = self.twitter.get_deepin_info()
        # upload
        for weibo in self.to_share_weibo:
            if self.to_share_weibo[weibo]:
                self.to_share_weibo_res[weibo] = weibo.upload_image(self.upload_image, text)
        self.share_to_weibo_result()
    
    # show upload result
    @post_gui
    def share_to_weibo_result(self):
        '''result of share to weibo'''
        font_color = app_theme.get_color("share_result_text")
        res_hbox = gtk.HBox(False)
        res_hbox.set_size_request(-1, 240)

        res_left_box = DialogLeftButtonBox()
        res_right_box = DialogRightButtonBox()

        res_left_box.button_align.set(0.5, 0.0, 0, 1)
        res_right_box.button_align.set(0.5, 0.0, 0, 1)
        res_left_box.button_align.set_padding(5, 9, 19, 0)
        res_right_box.button_align.set_padding(30, 0, 0, 0)

        res_left_box.set_size_request(405, -1)
        res_right_box.set_size_request(195, -1)
        
        res_hbox.pack_start(res_left_box)
        res_hbox.pack_start(
            VSeparator(app_theme.get_shadow_color("VSeparator").get_color_info(), 0, 0))
        res_hbox.pack_start(res_right_box)

        res_vbox = gtk.VBox(False)
        follow_vbox = gtk.VBox(False)

        tmp_img = gtk.Image()       # only use as a placeholder
        tmp_img.set_size_request(-1, 50) 
        res_vbox.pack_start(tmp_img, False, False)

        follow_tip_hbox = gtk.HBox(False)
        img = gtk.image_new_from_file(app_theme.get_theme_file_path("image/share/deepin_logo.png"))
        follow_tip_hbox.pack_start(img, False, False, 5)
        follow_tip_hbox.pack_start(
            Label("%s %s" % (_("Follow"), "Linux Deepin"), 
                text_color=app_theme_get_dynamic_color("#5f5f5f"),
                text_size=12, enable_select=False), False, False)
        follow_vbox.pack_start(follow_tip_hbox, False, False, 13)
        for weibo in self.to_share_weibo_res:
            vbox = gtk.VBox(False, 1)
            tip_box = gtk.HBox()
            error_box = gtk.HBox()
            vbox.pack_start(tip_box, False, False)
            vbox.pack_start(error_box, False, False)
            if self.to_share_weibo_res[weibo][0]:   # upload succeed
                img = gtk.image_new_from_file(app_theme.get_theme_file_path("image/share/share_succeed.png"))
                #link = LinkButton(_(weibo.t_type), text_size=13, self.to_share_weibo_res[weibo][1])
                link = Label(_(weibo.t_type), text_size=12, 
                    text_color=app_theme.get_color("link_text"))
                #, enable_gaussian=True, gaussian_radious=1, border_radious=0)
                link.add_events(gtk.gdk.BUTTON_PRESS_MASK)
                link.connect("enter-notify-event", lambda w, e: self.__draw_under_line(w))
                link.connect("leave-notify-event", lambda w, e: w.queue_draw())
                link.connect("button-press-event", self.goto_weibo_button_clicked, weibo)
                link_box = gtk.HBox(False)
                link_box.pack_start(link, False, False)
                utils.set_clickable_cursor(link)
                text = _("Share to")
                label = Label(text, text_size=12, 
                    text_color=font_color, enable_select=False)
                text = _("Successful")
                label1 = Label(text, text_size=12, 
                    text_color=font_color, enable_select=False)
                tip_box.pack_start(img, False, False, 15)
                tip_box.pack_start(label, False, False, 3)
                tip_box.pack_start(link_box, False, False, 3)
                tip_box.pack_start(label1, False, False)
                # only use as a placeholder
                img = gtk.Image()
                img.set_size_request(20, 1)
                error_box.pack_start(img, False, False, 16)
                tmp = Label(" ", text_size=9, label_width=200)
                tmp.set_size_request(200, 1)
                error_box.pack_start(tmp, False, False)
                #print text
            else:   # upload failed
                img = gtk.image_new_from_file(app_theme.get_theme_file_path("image/share/share_failed.png"))
                #text = "% %s %s." % (_(weibo.t_type), _("upload failed"))
                text = _("Share to")
                label1 = Label(text, text_size=12, 
                    text_color=font_color, enable_select=False)
                label2 = Label(_(weibo.t_type), text_size=12, 
                    text_color=font_color, enable_select=False)
                text = _("Failed")
                label3 = Label(text, text_size=12, 
                    text_color=font_color, enable_select=False)
                if weibo.curl.error:
                    error = "(%s)" % _(weibo.curl.error)
                elif weibo.get_error_msg():
                    error = "(%s)" % _(weibo.get_error_msg()) 
                else:
                    error = "(%s)" % _("Unknown reason")
                #print "%s: %s" % (weibo.t_type, error)
                #print "%s: %s" % (weibo.t_type, weibo.get_error_msg())
                label = Label(text, text_size=12,
                    text_color=font_color, enable_select=False)
                tip_box.pack_start(img, False, False, 15)
                tip_box.pack_start(label1, False, False, 3)
                tip_box.pack_start(label2, False, False, 3)
                tip_box.pack_start(label3, False, False)
                img = gtk.Image()   # only use as a placeholder
                img.set_size_request(20, 20)
                error_box.pack_start(img, False, False, 16)
                error_box.pack_start(Label(error, text_size=9, label_width=200,
                    text_color=font_color, enable_select=False), False, False)
                #print text
            res_vbox.pack_start(vbox, False, False, 10)

        for weibo in self.deepin_info:
            box = gtk.HBox(False, 15)
            # followed
            img = gtk.image_new_from_pixbuf(app_theme.get_pixbuf("share/"+weibo.t_type+".png").get_pixbuf())
            box.pack_start(img, False, False)
            if self.deepin_info[weibo] is not None and self.deepin_info[weibo][3]:
                if not default_locale.startswith("zh_"):
                    button = gtk.image_new_from_pixbuf(
                        app_theme.get_pixbuf("share/followed_en.png").get_pixbuf())
                else:
                    button = gtk.image_new_from_pixbuf(
                        app_theme.get_pixbuf("share/followed.png").get_pixbuf())
            else:   # to follow
                if not default_locale.startswith("zh_"):
                    button = ImageButton(
                        app_theme.get_pixbuf("share/follow_normal_en.png"),
                        app_theme.get_pixbuf("share/follow_hover_en.png"),
                        app_theme.get_pixbuf("share/follow_press_en.png"))
                else:
                    button = ImageButton(
                        app_theme.get_pixbuf("share/follow_normal.png"),
                        app_theme.get_pixbuf("share/follow_hover.png"),
                        app_theme.get_pixbuf("share/follow_press.png"))
                button.connect("clicked", self.friendships_add_button_clicked, weibo, box)
            box.pack_start(button, False, False)
            align = gtk.Alignment()
            align.set(0.0, 0.5, 0, 0)
            align.set_padding(0, 0, 30, 0)
            align.add(box)
            follow_vbox.pack_start(align, False, False, 8)

        res_left_box.set_buttons([res_vbox])
        res_right_box.set_buttons([follow_vbox])

        self.result_box.pack_start(res_hbox, False, False)
        self.result_box.show_all()
        self.set_slide_index(2)
    
    def goto_weibo_button_clicked(self, widget, event, weibo):
        '''goto my weibo'''
        #print "goto weibo button clicked", weibo.t_type, "xdg-open %s" % self.to_share_weibo_res[weibo][1]
        if weibo in self.to_share_weibo_res:
            if self.to_share_weibo_res[weibo][1]:
                utils.run_command("xdg-open %s" % self.to_share_weibo_res[weibo][1])
        
    def friendships_add_button_clicked(self, widget, weibo, box):
        '''add friendships'''
        #self.result_box.set_sensitive(False)
        if not self.is_get_user_info[weibo]:
            utils.run_command("xdg-open %s" % weibo.index_url)
            return True

        widget.set_sensitive(False)
        t = threading.Thread(target=self.friendships_add_thread, args=(widget, weibo, box))
        t.setDaemon(True)
        t.start()
    
    def friendships_add_thread(self, button, weibo, box):
        '''add friendships'''
        if weibo.friendships_create() is not None:
            gtk.gdk.threads_enter()
            button.destroy()
            if not default_locale.startswith("zh_"):
                button = gtk.image_new_from_pixbuf(
                    app_theme.get_pixbuf("share/followed_en.png").get_pixbuf())
            else:
                button = gtk.image_new_from_pixbuf(
                    app_theme.get_pixbuf("share/followed.png").get_pixbuf())
            button.show()
            box.pack_start(button, False, False)
            #button.set_label("已关注")
            gtk.gdk.threads_leave()
    
    # show window
    def show(self):
        '''show'''
        self.window.show_window()

    # close widnow
    def quit(self, widget):
        ''' close '''
        gtk.main_quit()

    def __slider_expose(self, widget, event):
        ''' slider expose redraw'''
        cr = widget.window.cairo_create()
        rect = widget.allocation
        cr.set_source_rgba(1.0, 1.0, 1.0, 0.8)
        cr.rectangle(rect.x, rect.y, rect.width, rect.height)
        cr.fill()
    
    def __expose_top_and_bottome(self, widget, event):
        '''titlebar or button_box expose'''
        cr = widget.window.cairo_create()
        rect = widget.allocation
        cr.set_source_rgb(0.89, 0.89, 0.89)
        cr.rectangle(rect.x+2, rect.y+2, rect.width-4, rect.height-4)
        cr.fill()

    def __draw_under_line(self, widget):
        '''draw under line'''
        cr = widget.window.cairo_create()
        with utils.cairo_disable_antialias(cr):
            x, y, w, h = widget.allocation
            # #1A70b1
            cr.set_source_rgba(0.1, 0.43, 0.69, 1.0)
            cr.set_line_width(1)
            cr.move_to(x, y+h-3)
            cr.line_to(x+w, y+h-3)
            cr.stroke()
Esempio n. 4
0
    def init_share_box(self):
        '''get weibo info, and create button'''
        self.to_share_weibo = {}
        self.to_share_weibo_res = {}
        self.deepin_info = {}
        self.is_get_user_info = {}
        self.__weibo_check_button_list = []
        self.__weibo_image_button_list = []

        # create Thumbnail
        if exists(self.upload_image):
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.upload_image)
            pix_w = pixbuf.get_width()
            pix_h = pixbuf.get_height()
            if pix_w > pix_h:
                pix_s_w = self.thumb_width
                pix_s_h = int(pix_h / (float(pix_w) / self.thumb_width))
            else:
                pix_s_h = self.thumb_height
                pix_s_w = int(pix_w / (float(pix_h) / self.thumb_height))
            pixbuf = pixbuf.scale_simple(pix_s_w, pix_s_h, gtk.gdk.INTERP_TILES)
            thumb = gtk.image_new_from_pixbuf(pixbuf)
        else:
            thumb = gtk.Image()
        thumb.set_size_request(self.thumb_width, self.thumb_height)

        # weibo context input
        text_box = gtk.HBox(False, 2)
        text_vbox = gtk.VBox(False, 2)
        text_bg_vbox = gtk.VBox(False)
        text_bg_align = gtk.Alignment()
        text_bg_align.set(0.5, 0.5, 0, 0)
        text_bg_align.set_padding(5, 5, 16, 5)
        text_bg_align.connect("expose-event", self.text_view_bg_expose)
        text_scrolled_win = ScrolledWindow()
        text_scrolled_win.set_size_request(340, 157)

        text_view = gtk.TextView()
        text_view.set_left_margin(10)
        text_view.set_right_margin(10)
        text_view.set_pixels_above_lines(5)
        text_view.set_pixels_below_lines(5)
        text_view.set_wrap_mode(gtk.WRAP_WORD| gtk.WRAP_CHAR)
        text_view.connect("expose-event", self.text_view_expose)
        buf = text_view.get_buffer()
        text_scrolled_win.add(text_view)
        text_bg_vbox.pack_start(text_scrolled_win)
        text_bg_align.add(text_bg_vbox)

        text_align = gtk.Alignment() 
        text_align.set(0.5, 0.5, 0, 0)
        text_align.set_padding(25, 30, 10, 10)

        text_box.pack_start(thumb, False, False, 10)
        text_box.pack_start(text_bg_align)
        text_vbox.pack_start(text_box, False, False, 10)

        text_align.add(text_vbox)
        #tmp_align = gtk.Alignment()
        #tmp_align.set(0.5, 0, 0, 1)
        #self.share_box.pack_start(tmp_align, False, False)
        self.share_box.pack_start(text_align, False, False)

        # dialog button box
        left_box = self.window.left_button_box
        right_box = self.window.right_button_box

        # input tip label
        self.input_num_label = Label("%d" % self.MAX_CHAR,
            text_size=16, text_x_align=pango.ALIGN_CENTER, label_width=50, enable_select=False)
        self.input_num_label.text_color = app_theme.get_color("left_char_num")

        # login box
        #weibo_box = gtk.HBox(False, 1)
        #weibo_box.set_size_request(-1, 50)
        weibo_box_list = []
        self.loading_label = Label("%s..." % _("Loading"), text_size=12,
            label_width=70, enable_select=False)
        weibo_box_list.append(self.loading_label)

        for weibo in self.__weibo_list:
            box = gtk.HBox(False, 2)
            weibo.set_box(box)
            weibo_box_list.append(box)
        left_box.set_buttons(weibo_box_list)

        # share button
        button = Button(_("Share"))
        #button.set_size_request(75, 25)
        button.connect("clicked", self.share_button_clicked, text_view)
        buf.connect("changed", self.text_view_changed, button)  # check char num

        tmp_vbox = gtk.VBox(False)
        tmp_align = gtk.Alignment()
        tmp_align.set(0.5, 0.5, 0, 0)
        tmp_vbox.pack_start(button, False, False)
        #tmp_vbox.pack_start(tmp_align)
        tmp_align.add(tmp_vbox)
        right_box.set_buttons([self.input_num_label, tmp_align])

        # at first, set widget insensitive
        button.set_sensitive(False)
        text_view.set_editable(False)
        t = threading.Thread(target=self.init_user_info_thread, args=(button, text_view))
        t.setDaemon(True)
        t.start()
class SettingUI(gtk.Alignment):
    def __init__(self, slide_back_cb, change_crumb_cb):
        gtk.Alignment.__init__(self, 0, 0, 0, 0)
        self.slide_back = slide_back_cb
        self.change_crumb = change_crumb_cb
        
        self.scroll_win = ScrolledWindow()
        self.scroll_win.set_can_focus(False)

        self.scroll_win.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        main_vbox = gtk.VBox()
        self.foot_box = FootBox()
        self.hbox = gtk.HBox()

        self.scroll_win.add_with_viewport(self.hbox)
        self.scroll_align = gtk.Alignment()
        self.scroll_win.set_size_request(800, 435)
        self.scroll_align.set(0, 0, 0, 0)
        self.scroll_align.set_padding(0, 0, 30, 0)
        self.scroll_align.add(self.scroll_win)
        
        padding_align = gtk.Alignment(0, 0, 0, 0)
        padding_align.set_padding(15, 0, 0, 0)
        self.sidebar = SideBar( None)
        padding_align.add(self.sidebar)
        self.hpaned = MyPaned()
        self.hpaned.set_size_request(800, -1)
        self.hpaned.connect("expose-event",self.expose_line)
        #self.hpaned.do_enter_notify_event = self.enter_notify_event
        self.hpaned.add1(padding_align)
        self.hpaned.add2(self.scroll_align)
        self.connect_after("show", self.__init_paned)
        main_vbox.pack_start(self.hpaned, True, True)
        main_vbox.pack_start(self.foot_box, False, False)
        self.add(main_vbox)

        self.__init_signals()

    def enter_notify_event(self, e):
        pass

    def __init_paned(self, widget):
        log.debug("")
        self.hpaned.saved_position = 160
        self.hpaned.set_position(1)
        self.hpaned.animation_position_frames = [0]
        self.hpaned.update_position()

    def __init_signals(self):
        Dispatcher.connect("connection-change", self.switch_content)
        Dispatcher.connect("setting-saved", self.save_connection_setting)
        Dispatcher.connect("setting-appled", self.apply_connection_setting)
        Dispatcher.connect("request_redraw", lambda w: self.scroll_win.show_all())

    def load_module(self, module_obj, hide_left):
        # create a reference
        self.setting_group = module_obj
        
        # init paned
        self.__init_paned(None)
        log.info("dss start load module", module_obj)
        self.hpaned.set_button_show(hide_left)
        
        # init foot_box
        self.foot_box.set_setting(module_obj)

        # init sidebar
        self.sidebar.load_list(module_obj)
        self.apply_method = module_obj.apply_changes
        self.save_method = module_obj.save_changes
        

    def switch_content(self, widget, connection):
        container_remove_all(self.hbox)
        self.set_tab_content(connection)
        self.set_foot_bar_button(connection)
    
        self.focus_connection = connection

    def set_foot_bar_button(self, connection):
        if type(connection) == NMRemoteConnection:
            self.foot_box.show_delete(connection)
        else:
            self.foot_box.hide_delete()
        states = self.setting_group.get_button_state(connection)
        if states:
            Dispatcher.set_button(*states)
        
    def set_tab_content(self, connection):
        log.debug("set tab content", connection)
        setting = self.setting_group.init_items(connection)
        self.hbox.add(setting)
        self.hbox.show_all()
        self.foot_box.set_lock(False)

    def expose_line(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation
        style.draw_out_line(cr, rect, exclude=["left", "right", "top"])

    def draw_tab_title_background(self, cr, widget):
        rect = widget.allocation
        cr.set_source_rgb(1, 1, 1)    
        cr.rectangle(0, 0, rect.width, rect.height - 1)
        cr.fill()

    def save_connection_setting(self, widget):
        self.save_method(self.focus_connection)

    def apply_connection_setting(self, widget):
        #print type(self.focus_connection)
        self.apply_method(self.focus_connection)

    def create_new_connection(self):
        self.sidebar.add_new_connection()
class DisplayView(gtk.VBox):
    '''
    class docs
    '''
	
    def __init__(self):
        '''
        init docs
        '''
        gtk.VBox.__init__(self)

        self.brightness_id = None

        self.display_manager = DisplayManager()
        self.__xrandr_settings = self.display_manager.get_xrandr_settings()
        self.__xrandr_settings.connect("changed", self.__xrandr_changed)

        self.resize_width = 790
        self.resize_height = 200
        self.monitor_items = []
        self.__output_names = []
        self.__current_output_name = self.display_manager.get_primary_output_name()
        self.__setup_monitor_items()
        self.sizes_items = []
        self.monitor_combo = None
        if len(self.monitor_items) > 1 and self.display_manager.is_copy_monitors():
            self.__set_same_sizes()
        else:
            self.__setup_sizes_items()
        self.multi_monitors_items = [(_("Copy Display"), 1), 
                                     (_("Extend Display"), 2), 
                                     (_("Only shown in display 1"), 3), 
                                     (_("Only shown in display 2"), 4)]
        self.rotation_items = [(_("Normal"), 1), 
                               (_("Right"), 2), 
                               (_("Left"), 3), 
                               (_("Inverted"), 4)]
        self.duration_items = [("1 %s" % _("Minute"), 1), 
                               ("2 %s" % _("Minutes"), 2), 
                               ("3 %s" % _("Minutes"), 3), 
                               ("5 %s" % _("Minutes"), 5), 
                               ("10 %s" % _("Minutes"), 10), 
                               ("30 %s" % _("Minutes"), 30), 
                               ("1 %s" % _("Hour"), 60), 
                               (_("Never"), DisplayManager.BIG_NUM / 60)]
        '''
        scrolled_window
        '''
        self.scrolled_window = ScrolledWindow()
        self.scrolled_window.set_size_request(-1, 425)
        self.scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        self.main_box = gtk.VBox()
        self.main_box.set_size_request(600, -1)
        self.body_box = gtk.HBox()
        '''
        left, right align
        '''
        self.left_align = self.__setup_align(padding_top = FRAME_TOP_PADDING, 
                                             padding_left = TEXT_WINDOW_LEFT_PADDING)
        self.right_align = self.__setup_align(padding_top = FRAME_TOP_PADDING, 
                                              padding_left = 0)
        '''
        left, right box
        '''
        self.left_box = gtk.VBox(spacing = WIDGET_SPACING)
        self.right_box = gtk.VBox(spacing = WIDGET_SPACING)
        '''
        monitor operation && detect
        '''
        self.monitor_resize_align = self.__setup_align(padding_top = 11, 
                                                       padding_left = int(TEXT_WINDOW_LEFT_PADDING / 2))
        self.monitor_resize_box = MonitorResizableBox(self.display_manager)
        self.monitor_resize_box.select_output(self.__current_output_name)
        self.monitor_resize_box.connect("select-output", self.__select_output)
        self.monitor_resize_box.connect("resize", self.__resize_box)
        self.monitor_resize_align.add(self.monitor_resize_box)
        '''
        monitor display
        '''
        self.monitor_display_align = self.__setup_title_align(
            app_theme.get_pixbuf("display/monitor_display.png"), 
            _("Display"), 
            0)
        '''
        monitor
        '''
        self.monitor_align = self.__setup_align()
        self.monitor_box = gtk.HBox(spacing = WIDGET_SPACING)
        self.monitor_label = self.__setup_label(_("Monitor"))
        self.monitor_combo = self.__setup_combo(self.monitor_items)
        self.monitor_combo.set_select_index(self.display_manager.get_primary_output_name_index(self.monitor_items))
        self.monitor_combo.connect("item-selected", self.__combo_item_selected, "monitor_combo")
        self.__widget_pack_start(self.monitor_box, 
            [self.monitor_label, 
             self.monitor_combo])
        self.monitor_align.add(self.monitor_box)
        '''
        goto individuation or power setting
        '''
        self.goto_align = self.__setup_align()
        self.goto_box = gtk.VBox(spacing = WIDGET_SPACING)
        self.goto_label = self.__setup_label(_("Relevant Settings"), 
                                             text_size = TITLE_FONT_SIZE, 
                                             width = None, 
                                             align = ALIGN_START)
        goto_color = GOTO_FG_COLOR
        self.goto_individuation_label = self.__setup_label(
            text = _("<span foreground=\"%s\" underline=\"single\">Personalization</span>") % goto_color, 
            width = None, 
            align = ALIGN_START)
        self.goto_individuation_label.connect("button-press-event", 
                                              self.__button_press, 
                                              "individuation")
        set_clickable_cursor(self.goto_individuation_label)
        self.goto_power_label = self.__setup_label(
            text = _("<span foreground=\"%s\" underline=\"single\">Power</span>") % goto_color, 
            width = None, 
            align = ALIGN_START)
        self.goto_power_label.connect("button-press-event", 
                                      self.__button_press, 
                                      "power")
        set_clickable_cursor(self.goto_power_label)
        self.__widget_pack_start(self.goto_box, 
                                 [self.goto_label, 
                                  self.goto_individuation_label, 
                                  self.goto_power_label
                                 ])
        self.goto_align.add(self.goto_box)
        '''
        sizes
        '''
        self.sizes_align = self.__setup_align()
        self.sizes_box = gtk.HBox(spacing = WIDGET_SPACING)
        self.sizes_label = self.__setup_label(_("Resolution"))
        self.sizes_combo = self.__setup_combo(self.sizes_items)
        if self.sizes_combo:
            self.sizes_combo.set_select_index(
                self.display_manager.get_screen_size_index(self.__current_output_name, 
                                                           self.sizes_items))
            self.sizes_combo.connect("item-selected", self.__combo_item_selected, "sizes_combo")
            self.__widget_pack_start(self.sizes_box, 
                                     [self.sizes_label, self.sizes_combo])
        self.sizes_align.add(self.sizes_box)
        '''
        rotation
        '''
        self.rotation_align = self.__setup_align()
        self.rotation_box = gtk.HBox(spacing = WIDGET_SPACING)
        self.rotation_label = self.__setup_label(_("Rotation"))
        self.rotation_combo = self.__setup_combo(self.rotation_items)
        self.rotation_combo.set_select_index(self.display_manager.get_screen_rotation_index(self.__current_output_name))
        self.rotation_combo.connect("item-selected", self.__combo_item_selected, "rotation_combo")
        self.__widget_pack_start(self.rotation_box, 
            [self.rotation_label, 
             self.rotation_combo])
        self.rotation_align.add(self.rotation_box)
        '''
        multi-monitors
        '''
        self.multi_monitors_align = self.__setup_align()
        self.multi_monitors_box = gtk.HBox(spacing = WIDGET_SPACING)
        self.multi_monitors_label = self.__setup_label(_("Multi-Monitor"))
        self.multi_monitors_combo = self.__setup_combo(self.multi_monitors_items)
        self.multi_monitors_combo.set_select_index(self.display_manager.get_multi_monitor_index())
        self.multi_monitors_combo.connect("item-selected", self.__combo_item_selected, "multi_monitors_combo")
        self.__widget_pack_start(self.multi_monitors_box, 
            [self.multi_monitors_label, self.multi_monitors_combo])
        self.multi_monitors_align.add(self.multi_monitors_box)
        if self.display_manager.get_output_count() < 2:
            self.multi_monitors_align.set_size_request(-1, 0)
            self.multi_monitors_align.set_child_visible(False)
        '''
        monitor brightness
        '''
        self.monitor_bright_align = self.__setup_title_align(
            app_theme.get_pixbuf("display/monitor_bright.png"), 
            _("Brightness"))
        '''
        brightness
        '''
        self.brightness_align = self.__setup_align()
        self.brightness_box = gtk.HBox(spacing = 2)
        self.brightness_label_align = self.__setup_align(padding_top = 8, 
                                                         padding_left = 0, 
                                                         padding_right = 5)
        self.brightness_label = self.__setup_label(_("Brightness"))
        self.brightness_label_align.add(self.brightness_label)
        
        self.brightness_scale = HScalebar(point_dpixbuf = app_theme.get_pixbuf("scalebar/point.png"), 
                                          value_min = 0.1, 
                                          value_max = 1.0)
        self.brightness_scale.set_size_request(HSCALEBAR_WIDTH, 33)
        self.brightness_scale.set_value(self.display_manager.get_screen_brightness())
        self.brightness_scale.connect("value-changed", self.__set_brightness)
        self.__widget_pack_start(self.brightness_box, 
            [self.brightness_label_align, 
             self.brightness_scale])
        self.brightness_align.add(self.brightness_box)
        '''
        auto adjust monitor brightness
        '''
        self.auto_adjust_align = self.__setup_align()
        self.auto_adjust_box = gtk.HBox(spacing = WIDGET_SPACING)
        self.auto_adjust_label = self.__setup_label(_("Auto-Brightness"))
        self.auto_adjust_toggle_align = self.__setup_align(padding_top = 4, padding_left = 158)
        self.auto_adjust_toggle = self.__setup_toggle()
        self.auto_adjust_toggle.set_active(self.display_manager.is_enable_close_monitor())
        self.auto_adjust_toggle.connect("toggled", self.__toggled, "auto_adjust_toggle")
        self.auto_adjust_toggle_align.add(self.auto_adjust_toggle)
        self.__widget_pack_start(self.auto_adjust_box, 
            [self.auto_adjust_label, self.auto_adjust_toggle_align])
        self.auto_adjust_align.add(self.auto_adjust_box)
        '''
        close monitor
        '''
        self.close_monitor_align = self.__setup_align()
        self.close_monitor_box = gtk.HBox(spacing = WIDGET_SPACING)
        self.close_monitor_label = self.__setup_label(_("Turn off monitor"))
        self.close_monitor_combo = self.__setup_combo(self.duration_items)
        self.close_monitor_combo.set_select_index(self.display_manager.get_close_monitor_index(self.duration_items))
        self.close_monitor_combo.connect("item-selected", self.__combo_item_selected, "close_monitor_combo")
        self.__widget_pack_start(self.close_monitor_box, 
            [self.close_monitor_label, 
             self.close_monitor_combo])
        self.close_monitor_align.add(self.close_monitor_box)
        '''
        monitor lock
        '''
        self.monitor_lock_align = self.__setup_title_align(
            app_theme.get_pixbuf("lock/lock.png"), 
            _("Lock Screen"))
        '''
        auto monitor lock
        '''
        self.auto_lock_align = self.__setup_align()
        self.auto_lock_box = gtk.HBox(spacing = WIDGET_SPACING)
        self.auto_lock_label = self.__setup_label(_("Lock screen automatically"))
        self.auto_lock_toggle_align = self.__setup_align(padding_top = 4, padding_left = 158)
        self.auto_lock_toggle = self.__setup_toggle()
        is_enable_lock_display = self.display_manager.is_enable_lock_display()
        self.auto_lock_toggle.set_active(is_enable_lock_display)
        self.auto_lock_toggle.connect("toggled", self.__toggled, "auto_lock_toggle")
        self.auto_lock_toggle_align.add(self.auto_lock_toggle)
        self.__widget_pack_start(self.auto_lock_box, 
            [self.auto_lock_label, self.auto_lock_toggle_align])
        self.auto_lock_align.add(self.auto_lock_box)
        '''
        lock display
        '''
        self.lock_display_align = self.__setup_align(padding_bottom = 20)
        self.lock_display_box = gtk.HBox(spacing = WIDGET_SPACING)
        self.lock_display_label = self.__setup_label(_("Lock Screen")) 
        self.lock_display_combo = self.__setup_combo(self.duration_items)
        self.lock_display_combo.set_select_index(self.display_manager.get_lock_display_index(self.duration_items))
        self.lock_display_combo.connect("item-selected", self.__combo_item_selected, "lock_display_combo")
        self.__widget_pack_start(self.lock_display_box, 
            [self.lock_display_label, 
             self.lock_display_combo])
        self.lock_display_align.add(self.lock_display_box)
        '''
        left_align pack_start
        '''
        self.__widget_pack_start(self.left_box, 
            [self.monitor_display_align, 
             self.monitor_align, 
             self.sizes_align, 
             self.rotation_align, 
             self.multi_monitors_align, 
             self.monitor_bright_align, 
             self.brightness_align, 
             #self.auto_adjust_align, 
             #self.close_monitor_align, 
             self.monitor_lock_align, 
             self.auto_lock_align, 
             self.lock_display_align])
        self.left_align.add(self.left_box)
        '''
        right_align pack_start
        '''
        self.__widget_pack_start(self.right_box, 
            [self.goto_align])
        self.right_box.set_size_request(280, -1)
        self.right_align.add(self.right_box)
        '''
        main && body box
        '''
        self.main_box.pack_start(self.monitor_resize_align, False, False)
        self.body_box.pack_start(self.left_align)
        self.body_box.pack_start(self.right_align, False, False)
        self.main_box.pack_start(self.body_box)
        '''
        this->HBox pack_start
        '''
        self.scrolled_window.add_child(self.main_box)
        self.pack_start(self.scrolled_window)

        self.__send_message("status", ("display", ""))

    def show_again(self):
        self.__send_message("status", ("display", ""))

    def reset(self):
        self.__send_message("status", ("display", _("Reset to default value")))
        self.display_manager.reset()
        self.close_monitor_combo.set_select_index(self.display_manager.get_close_monitor_index(self.duration_items))
        self.lock_display_combo.set_select_index(self.display_manager.get_lock_display_index(self.duration_items))
        self.multi_monitors_combo.set_select_index(0)

    def __handle_dbus_replay(self, *reply):
        pass

    def __handle_dbus_error(self, *error):
        pass

    def __send_message(self, message_type, message_content):
        if is_dbus_name_exists(APP_DBUS_NAME):
            bus_object = dbus.SessionBus().get_object(APP_DBUS_NAME, APP_OBJECT_NAME)
            method = bus_object.get_dbus_method("message_receiver")
            method(message_type, 
                   message_content, 
                   reply_handler=self.__handle_dbus_replay, 
                   error_handler=self.__handle_dbus_error)

    def __button_press(self, widget, event, module_id):
        self.__send_message("goto", (module_id, ""))

    def __expose(self, widget, event):
        try:
            cr = widget.window.cairo_create()                                        
            rect = widget.allocation                                                 
        
            cr.set_source_rgb(*color_hex_to_cairo(MODULE_BG_COLOR))                  
            cr.rectangle(rect.x, rect.y, rect.width, rect.height)                    
            cr.fill()
        except e:
            print "DEBUG", e

    def __change_current_output(self, output_name, from_monitor_combo=True):
        self.__current_output_name = output_name

        if not from_monitor_combo:
            self.monitor_combo.set_select_index(self.display_manager.get_output_name_index(output_name, self.monitor_items))

        if not self.display_manager.is_copy_monitors():
            self.__setup_sizes_items()
        if len(self.sizes_items):
            self.sizes_combo.add_items(items = self.sizes_items)
        self.sizes_combo.set_select_index(self.display_manager.get_screen_size_index(
            self.__current_output_name, self.sizes_items))
    
    def __select_output(self, widget, output_name):
        print "Output name:", output_name
        self.__change_current_output(output_name, False)
    
    def __set_same_sizes(self):
        same_sizes = self.display_manager.get_same_sizes(                      
            self.display_manager.get_screen_sizes(self.monitor_items[0][1]), 
            self.display_manager.get_screen_sizes(self.monitor_items[1][1]))
        i = 0
        
        del self.sizes_items[:]                                             
        while i < len(same_sizes):                                          
            self.sizes_items.append((same_sizes[i], i))                     
                                                                                
            i += 1                                                          
    
    def __xrandr_changed(self, key):
        if key == "output-names":
            self.display_manager.init_xml()
            self.__setup_monitor_items()
            self.monitor_combo.add_items(items = self.monitor_items)
            if len(self.monitor_items) > 1:
                if self.display_manager.is_copy_monitors():
                    self.__set_same_sizes()
                    self.sizes_combo.add_items(items = self.sizes_items) 

                self.multi_monitors_align.set_size_request(-1, 30)
                self.multi_monitors_align.set_child_visible(True)
            else:
                self.multi_monitors_align.set_size_request(-1, 0)
                self.multi_monitors_align.set_child_visible(False)
            return

        if key == "brightness":
            self.brightness_scale.set_value(self.display_manager.get_screen_brightness())
            return

    def __set_brightness_value(self, value):
        self.display_manager.set_screen_brightness(self.__current_output_name, value)

    def __set_brightness(self, widget, event):
        value = self.brightness_scale.get_value()
        self.__send_message("status", 
                ("display", _("Changed brightness to %d%%") % int(value * 100)))
        if self.brightness_id:
            gobject.source_remove(self.brightness_id)
        self.brightness_id = gobject.timeout_add_seconds(1, self.__set_brightness_value, value)
    
    def __setup_monitor_items(self):
        self.__output_names = self.display_manager.get_output_names()
        del self.monitor_items[:]
        i = 0

        while (i < len(self.__output_names)):
            self.monitor_items.append(self.display_manager.get_output_name(self.__output_names[i]))
            i += 1

    def __setup_sizes_items(self):
        screen_sizes = self.display_manager.get_screen_sizes(self.__current_output_name)
        del self.sizes_items[:]
        i = 0

        while i < len(screen_sizes):
            self.sizes_items.append((screen_sizes[i], i))
            i += 1

    def __toggled(self, widget, object=None):
        if object == "auto_adjust_toggle":
            if not widget.get_active():
                self.__send_message("status", ("display", _("Changed to manual adjustment")))
                self.display_manager.set_close_monitor(DisplayManager.BIG_NUM / 60)
            else:
                self.__send_message("status", ("display", _("Changed to automatic adjustment")))
            return

        if object == "auto_lock_toggle":
            if not widget.get_active():
                self.lock_display_combo.set_sensitive(False)
                self.__send_message("status", ("display", _("Changed to manual lock")))
            else:
                self.lock_display_combo.set_sensitive(True)
                self.__send_message("status", ("display", _("Changed to automatic lock")))
            return

    def __combo_item_selected(self, widget, item_text=None, item_value=None, item_index=None, object=None):
        if object == "monitor_combo":
            self.__send_message("status", ("display", _("Changed current output to %s") % item_text))
            self.__change_current_output(item_value)
            return

        if object == "sizes_combo":
            size = self.sizes_items[item_value][0]
            self.__send_message("status", ("display", _("Changed resolution to %s") % size))
            self.display_manager.set_screen_size(self.__current_output_name, size)
            return
        
        if object == "rotation_combo":
            self.__send_message("status", ("display", _("Changed rotation to %s") % item_text))
            self.display_manager.set_screen_rotation(self.__current_output_name, item_value)
            return

        if object == "multi_monitors_combo":
            self.__send_message("status", ("display", _("Changed multi-monitor mode to %s") % item_text))
            self.display_manager.set_multi_monitor(item_value)
            return
        
        if object == "close_monitor_combo":
            self.__send_message("status", ("display", _("Idle time before turning off display has been changed to to %s") % item_text))
            self.display_manager.set_close_monitor(item_value)
            return

        if object == "lock_display_combo":
            self.__send_message("status", ("display", _("Idle time before locking display has been changed to %s") % item_text))
            if item_value == DisplayManager.BIG_NUM / 60:
                self.auto_lock_toggle.set_active(False)
            else:
                self.auto_lock_toggle.set_active(True)
            self.display_manager.set_lock_display(item_value)
            return

    def __resize_box(self, widget, height):
        self.monitor_resize_box.set_size_request(self.resize_width, height - FRAME_TOP_PADDING)

    def __setup_separator(self):
        hseparator = HSeparator(app_theme.get_shadow_color("hSeparator").get_color_info(), 0, 0)
        hseparator.set_size_request(500, HSEPARATOR_HEIGHT)
        return hseparator
    
    def __setup_title_label(self, 
                            text="", 
                            text_color=app_theme.get_color("globalTitleForeground"), 
                            text_size=TITLE_FONT_SIZE, 
                            text_x_align=ALIGN_START, 
                            label_width=180):
        return Label(text = text, 
                     text_color = text_color, 
                     text_size = text_size, 
                     text_x_align = text_x_align, 
                     label_width = label_width, 
                     enable_select = False, 
                     enable_double_click = False)
    
    def __setup_label(self, text="", text_size=CONTENT_FONT_SIZE, width=180, align=ALIGN_END, wrap_width=None):
        label = Label(text = text, 
                      text_color = None, 
                      text_size = text_size, 
                      text_x_align = align, 
                      label_width = width, 
                      wrap_width = wrap_width, 
                      enable_select = False, 
                      enable_double_click = False)
        return label

    def __setup_combo(self, items=[], width=HSCALEBAR_WIDTH):
        if len(items) == 0:
            return None

        combo = ComboBox(items = items, select_index = 0, max_width = width, fixed_width = width)
        combo.set_size_request(width, WIDGET_HEIGHT)
        return combo

    def __setup_toggle(self):
        toggle = ToggleButton(app_theme.get_pixbuf("toggle_button/inactive_normal.png"), 
            app_theme.get_pixbuf("toggle_button/active_normal.png"))
        return toggle

    def __setup_title_align(self, pixbuf, text, padding_top=FRAME_TOP_PADDING, padding_left=0):
        align = self.__setup_align(padding_top = padding_top, padding_left = padding_left)          
        align_box = gtk.VBox(spacing = WIDGET_SPACING)           
        title_box = gtk.HBox(spacing = WIDGET_SPACING)        
        image = ImageBox(pixbuf)
        label = self.__setup_title_label(text)
        separator = self.__setup_separator()               
        self.__widget_pack_start(title_box, [image, label])                  
        self.__widget_pack_start(align_box, [title_box, separator])
        align.add(align_box)
        return align
    
    def __setup_align(self, 
                      xalign=0, 
                      yalign=0, 
                      xscale=0, 
                      yscale=0, 
                      padding_top=0, 
                      padding_bottom=0, 
                      padding_left=FRAME_LEFT_PADDING + int(WIDGET_SPACING / 2), 
                      padding_right=0):
        align = gtk.Alignment()
        align.set(xalign, yalign, xscale, yscale)
        align.set_padding(padding_top, padding_bottom, padding_left, padding_right)
        align.connect("expose-event", self.__expose)
        return align

    def __widget_pack_start(self, parent_widget, widgets=[], expand=False, fill=False):
        if parent_widget == None:
            return
        for item in widgets:
            parent_widget.pack_start(item, expand, fill)
Esempio n. 7
0
    def icon_item_release_resource(self):
        # Return True to tell IconView call gc.collect() to release memory resource.
        if self.pixbuf:
            del self.pixbuf
        self.pixbuf = None
        return True


if __name__ == '__main__':
    gtk.gdk.threads_init()
    module_frame = ModuleFrame(
        os.path.join(get_parent_dir(__file__, 2), "config.ini"))

    scrolled_window = ScrolledWindow()
    scrolled_window.set_size_request(788, 467)
    wallpaper_view = WallpaperView()
    scrolled_window.add_child(wallpaper_view)
    module_frame.add(scrolled_window)

    scrolled_window.connect("vscrollbar-state-changed",
                            wallpaper_view.load_more_background)

    download_pool = MissionThreadPool(5)
    download_pool.start()

    def message_handler(*message):
        (message_type, message_content) = message
        if message_type == "show_again":
            module_frame.send_module_info()
        elif message_type == "exit":
Esempio n. 8
0
class ShareToWeibo(object):
    '''share picture to weibo'''
    def __init__(self, filename=""):
        '''
        init share
        @param filename: the file to share
        '''
        self.upload_image = filename
        self.thumb_width = 188
        self.thumb_height = 168
        self.MAX_CHAR = 140
        #self.__text_frame_color = (0.76, 0.76, 0.76)
        self.__win_width = 602
        open(COOKIE_FILE,'wb').close()

        self.window = DialogBox(_("Share to social networks"), close_callback=gtk.main_quit)
        self.window.set_keep_above(True)
        self.window.set_size_request(self.__win_width+20, 288)
        self.window.set_resizable(False)
        #self.window.titlebar.connect("expose-event", self.__expose_top_and_bottome)
        #self.window.button_box.connect("expose-event", self.__expose_top_and_bottome)

        # create slider
        self.slider = HSlider()
        self.slider_list = []

        self.share_box = gtk.VBox(False)     # first page, input context
        self.web_box = gtk.VBox(False, 10)      # second page, login
        self.result_box = gtk.VBox(False, 10)   # third page, share result

        share_align = gtk.Alignment()
        share_align.set(0.5, 0.5, 0, 0)
        share_align.add(self.share_box)
        share_align.connect("expose-event", self.__slider_expose)

        # go back button
        web_left_button = ImageButton(
            app_theme.get_pixbuf("share/back_normal.png"),
            app_theme.get_pixbuf("share/back_hover.png"),
            app_theme.get_pixbuf("share/back_press.png"))
        web_left_button.connect("clicked", lambda w: self.set_slide_index(0))
        web_left_button.set_can_focus(False)
        utils.set_clickable_cursor(web_left_button)
        # show url entry
        self.web_url_entry = InputEntry()
        self.web_url_entry.set_editable(False)
        self.web_url_entry.set_size(555, 20)
        self.web_url_entry.entry.right_menu_visible_flag = False
        # alig url entry
        web_navigate_vbox = gtk.VBox(False)
        web_navigate_vbox.pack_start(self.web_url_entry)
        web_navigate_t_align = gtk.Alignment()
        web_navigate_t_align.set(0.0, 0.5, 0, 0)
        web_navigate_t_align.add(web_navigate_vbox)
        # pack back button and url entry
        web_navigate_box = gtk.HBox(False, 7)
        web_navigate_box.pack_start(web_left_button, False, False)
        web_navigate_box.pack_start(web_navigate_t_align)

        web_navigate_align = gtk.Alignment()
        web_navigate_align.set(0.5, 0.5, 0, 0)
        web_navigate_align.set_padding(4, 0, 11, 13)
        web_navigate_align.add(web_navigate_box)

        # create a webkit
        self.web_view = WebView(COOKIE_FILE)
        self.web_view.connect("notify::load-status", self.web_view_load_status)
        self.web_view.connect("load-error", self.web_view_load_error)
        self.web_scrolled_window = ScrolledWindow()
        self.web_scrolled_window.add(self.web_view)
        self.web_scrolled_window.set_size_request(590, 228)

        self.web_box.pack_start(web_navigate_align, False, False)
        self.web_box.pack_start(self.web_scrolled_window)
        #self.web_box.set_size_request(-1, 258)
        web_align = gtk.Alignment()
        web_align.set(0.5, 0.0, 0, 1)
        web_align.add(self.web_box)
        web_align.connect("expose-event", self.__slider_expose)

        res_align = gtk.Alignment()
        res_align.set(0.5, 0.5, 0, 0)
        res_align.add(self.result_box)
        res_align.connect("expose-event", self.__slider_expose)

        self.slider.set_to_page(share_align)
        self.slider_list.append(share_align)
        self.slider_list.append(web_align)
        self.slider_list.append(res_align)

        self.__weibo_list = []
        self.sina = weibo.Sina(self.web_view)
        self.qq = weibo.Tencent(self.web_view)
        self.__weibo_list.append(self.sina)
        self.__weibo_list.append(self.qq)
        if default_locale != 'zh_CN':
            self.twitter = weibo.Twitter(self.web_view)
            #self.__weibo_list.append(self.twitter)
        self.__current_weibo = None

        self.weibo_name_l18n = {
                'Sina': _("Sina"),
                'Tencent': _("Tencent"),
                'Twitter': _("Twitter"),
                }

        self.window.body_box.pack_start(self.slider, True, True)
        self.init_share_box()

    # webkit load-status, login success, go back
    def web_view_load_status(self, web, status):
        '''web_view notify load-status callback'''
        state = web.get_property("load-status")
        url = web.get_property('uri')
        if url:
            self.web_url_entry.set_editable(True)
            self.web_url_entry.set_text(url)
            self.web_url_entry.entry.move_to_start()
            self.web_url_entry.set_editable(False)
        if state == webkit.LOAD_FAILED:  # load failed
            print "load failed",
            print web.get_property('uri')

        elif state == webkit.LOAD_COMMITTED:
            if self.__current_weibo and self.__current_weibo.is_callback_url(url):
                web.stop_loading()  # if go to  callback url, stop loading
                # access token
                #print "load committed", url
                t = threading.Thread(target=self.weibo_login_thread)
                t.setDaemon(True)
                t.start()

    def web_view_load_error(self, web, fram, url, error, data=None):
        web.load_string(
            "<html><body><p><h1>%s</h1></p>%s</body></html>" % (
            _("Unable to load page"),
            _("Problem occurred while loading the URL '%s'") % (url)),
            "text/html", "UTF-8", "")
        print url
        return True

    # login or switch user
    def weibo_login(self, widget, weibo):
        '''weibo button clicked callback'''
        self.web_view.load_uri("about:blank")
        utils.set_cursor(widget)
        self.set_slide_index(1)
        self.__current_weibo = weibo
        t = threading.Thread(target=self.__current_weibo.request_oauth)
        t.setDaemon(True)
        t.start()

    def weibo_login_thread(self):
        '''in webkit login finish, get user info again'''
        self.__current_weibo.access_token()
        self.get_user_info_again()
        gtk.gdk.threads_enter()
        self.set_slide_index(0)
        gtk.gdk.threads_leave()

    def get_user_info_again(self):
        ''' login or switch user, and get user info again'''
        box = self.__current_weibo.get_box()
        #print "cuurent weibo:", self.__current_weibo.t_type
        gtk.gdk.threads_enter()
        children = box.get_children()
        for child in children:
            if child in self.__weibo_check_button_list:
                self.__weibo_check_button_list.remove(child)
            if child in self.__weibo_image_button_list:
                self.__weibo_image_button_list.remove(child)
            child.destroy()
        gtk.gdk.threads_leave()

        self.get_user_info(self.__current_weibo)

        gtk.gdk.threads_enter()
        box.show_all()
        gtk.gdk.threads_leave()

    def set_slide_index(self, index):
        '''
        set slide to index
        @param index: the index of widget in slider, an int num
        '''
        if index >= len(self.slider_list):
            return
        direct = "right"
        if index == 1 and self.window.button_box in self.window.window_frame.get_children():
            #self.slider.set_size_request(-1, 260)
            win = self.window
            if win.left_button_box in win.button_box.get_children():
                win.button_box.remove(win.left_button_box)
            if win.right_button_box in win.button_box.get_children():
                win.button_box.remove(win.right_button_box)
            tmp = gtk.HSeparator()
            tmp.set_size_request(-1, 1)
            tmp.show()
            win.button_box.pack_start(tmp)
            direct = "right"
            #if self.window.button_box in self.window.window_frame.get_children():
                #self.window.window_frame.remove(self.window.button_box)
        elif index == 0:
            #self.slider.set_size_request(-1, 223)
            win = self.window
            for each in win.button_box.get_children():
                each.destroy()
            if win.left_button_box not in win.button_box.get_children():
                win.button_box.pack_start(win.left_button_box)
            if win.right_button_box not in win.button_box.get_children():
                win.button_box.pack_start(win.right_button_box)
            direct = "left"
            #if self.window.button_box not in self.window.window_frame.get_children():
                #self.window.window_frame.pack_start(self.window.button_box, False, False)
        elif index == 2:
            self.window.left_button_box.set_buttons([])
            l = Label("  ")
            l.show()
            self.window.right_button_box.set_buttons([l])
            direct = "right"
            #self.slider.set_size_request(-1, 223)

        self.slider.slide_to_page(self.slider_list[index], direct)

    def weibo_check_toggle(self, button, weibo):
        '''weibo check button toggled callback. check the weibo to share'''
        if button.get_active():
            self.to_share_weibo[weibo] = 1
        else:
            self.to_share_weibo[weibo] = 0

    def create_ico_image(self, name):
        ''' create image from file'''
        pix1 = app_theme_get_dynamic_pixbuf('image/share/%s.png' % name).get_pixbuf()
        pix2 = app_theme_get_dynamic_pixbuf('image/share/%s_no.png' % name).get_pixbuf()
        return (pix1, pix2)

    def get_user_info(self, weibo):
        '''get weibo user info'''
        info = weibo.get_user_name()
        gtk.gdk.threads_enter()
        #self.get_user_error_text = ""
        weibo_hbox = weibo.get_box()
        hbox = gtk.HBox(False)
        vbox = gtk.VBox(False)
        weibo_hbox.pack_start(vbox, False, False)
        vbox.pack_start(hbox)
        #print weibo.t_type, info
        if info:
            self.is_get_user_info[weibo] = 1
            label = Label(text=info, label_width=70, enable_select=False)
            check = CheckButton()
            #check = gtk.CheckButton()
            check.connect("toggled", self.weibo_check_toggle, weibo)
            check.set_active(True)
            check_vbox = gtk.VBox(False)
            check_align = gtk.Alignment(0.5, 0.5, 0, 0)
            check_align.add(check_vbox)
            check_vbox.pack_start(check, False, False)
            button = ImageButton(
                app_theme.get_pixbuf("share/" + weibo.t_type + ".png"),
                app_theme.get_pixbuf("share/" + weibo.t_type + ".png"),
                app_theme.get_pixbuf("share/" + weibo.t_type + ".png"))
            utils.set_clickable_cursor(button)
            button.connect("enter-notify-event", self.show_tooltip, _("Click to switch user"))
            hbox.pack_start(check_align, False, False)
            hbox.pack_start(button, False, False, 5)
            hbox.pack_start(label, False, False)
        else:
            self.is_get_user_info[weibo] = 0
            check = CheckButton()
            #check = gtk.CheckButton()
            check.set_sensitive(False)
            check_vbox = gtk.VBox(False)
            check_align = gtk.Alignment(0.5, 0.5, 0, 0)
            check_align.add(check_vbox)
            check_vbox.pack_start(check, False, False)
            button = ImageButton(
                app_theme.get_pixbuf("share/" + weibo.t_type + "_no.png"),
                app_theme.get_pixbuf("share/" + weibo.t_type + "_no.png"),
                app_theme.get_pixbuf("share/" + weibo.t_type + "_no.png"))
            utils.set_clickable_cursor(button)
            button.connect("enter-notify-event", self.show_tooltip, _("Click to login"))
            hbox.pack_start(check_align, False, False)
            hbox.pack_start(button, False, False, 5)
            # curl time out
            info_error = weibo.get_curl_error()
            if info_error:
                #self.get_user_error_text += "%s:%s." % (weibo.t_type, _(info_error))
                hbox.pack_start(
                    Label(text="(%s)" % _(info_error), label_width=70,enable_select=False,
                    text_color = ui_theme.get_color("category_item")), False, False)

        button.connect("clicked", self.weibo_login, weibo)
        self.__weibo_check_button_list.append(check)
        self.__weibo_image_button_list.append(button)
        gtk.gdk.threads_leave()
        return weibo_hbox

    def show_tooltip(self, widget, event, text):
        '''Create help tooltip.'''
        Tooltip.text(widget, text)

    def init_user_info_thread(self, button, text_view):
        '''get user name thread'''
        time.sleep(0.1)

        for weibo in self.__weibo_list:
            self.get_user_info(weibo)

        gtk.gdk.threads_enter()
        #self.share_box.set_sensitive(True)
        button.set_sensitive(True)
        text_view.set_editable(True)
        for weibo in self.__weibo_list:
            weibo.get_box().show_all()
            weibo.get_box().queue_draw()
        self.loading_label.destroy()
        gtk.gdk.threads_leave()

    # init share box, create share button, input
    def init_share_box(self):
        '''get weibo info, and create button'''
        self.to_share_weibo = {}
        self.to_share_weibo_res = {}
        self.deepin_info = {}
        self.is_get_user_info = {}
        self.__weibo_check_button_list = []
        self.__weibo_image_button_list = []

        # create Thumbnail
        if exists(self.upload_image):
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.upload_image)
            pix_w = pixbuf.get_width()
            pix_h = pixbuf.get_height()
            if pix_w > pix_h:
                pix_s_w = self.thumb_width
                pix_s_h = int(pix_h / (float(pix_w) / self.thumb_width))
            else:
                pix_s_h = self.thumb_height
                pix_s_w = int(pix_w / (float(pix_h) / self.thumb_height))
            pixbuf = pixbuf.scale_simple(pix_s_w, pix_s_h, gtk.gdk.INTERP_TILES)
            thumb = gtk.image_new_from_pixbuf(pixbuf)
        else:
            thumb = gtk.Image()
        thumb.set_size_request(self.thumb_width, self.thumb_height)

        # weibo context input
        text_box = gtk.HBox(False, 2)
        text_vbox = gtk.VBox(False, 2)
        text_bg_vbox = gtk.VBox(False)
        text_bg_align = gtk.Alignment()
        text_bg_align.set(0.5, 0.5, 0, 0)
        text_bg_align.set_padding(5, 5, 16, 5)
        text_bg_align.connect("expose-event", self.text_view_bg_expose)
        text_scrolled_win = gtk.ScrolledWindow()
        text_scrolled_win.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
        text_scrolled_win.set_size_request(340, 157)

        text_view = gtk.TextView()
        text_view.set_left_margin(10)
        text_view.set_right_margin(10)
        text_view.set_pixels_above_lines(5)
        text_view.set_pixels_below_lines(5)
        text_view.set_wrap_mode(gtk.WRAP_WORD| gtk.WRAP_CHAR)
        text_view.connect("expose-event", self.text_view_expose)
        buf = text_view.get_buffer()
        text_scrolled_win.add(text_view)
        text_bg_vbox.pack_start(text_scrolled_win)
        text_bg_align.add(text_bg_vbox)

        text_align = gtk.Alignment()
        text_align.set(0.5, 0.5, 0, 0)
        text_align.set_padding(25, 30, 10, 10)

        text_box.pack_start(thumb, False, False, 10)
        text_box.pack_start(text_bg_align)
        text_vbox.pack_start(text_box, False, False, 10)

        text_align.add(text_vbox)
        #tmp_align = gtk.Alignment()
        #tmp_align.set(0.5, 0, 0, 1)
        #self.share_box.pack_start(tmp_align, False, False)
        self.share_box.pack_start(text_align, False, False)

        # dialog button box
        left_box = self.window.left_button_box
        right_box = self.window.right_button_box

        # input tip label
        self.input_num_label = Label("%d" % self.MAX_CHAR,
            text_size=16, text_x_align=pango.ALIGN_CENTER, label_width=50, enable_select=False)
        self.input_num_label.text_color = ui_theme.get_color("label_select_text")

        # login box
        #weibo_box = gtk.HBox(False, 1)
        #weibo_box.set_size_request(-1, 50)
        weibo_box_list = []
        self.loading_label = Label("%s..." % _("Loading"), text_size=12,
            label_width=70, enable_select=False)
        weibo_box_list.append(self.loading_label)

        for weibo in self.__weibo_list:
            box = gtk.HBox(False, 2)
            weibo.set_box(box)
            weibo_box_list.append(box)
        left_box.set_buttons(weibo_box_list)

        # share button
        button = Button(_("Share"))
        #button.set_size_request(75, 25)
        button.connect("clicked", self.share_button_clicked, text_view)
        buf.connect("changed", self.text_view_changed, button)  # check char num

        tmp_vbox = gtk.VBox(False)
        tmp_align = gtk.Alignment()
        tmp_align.set(0.5, 0.5, 0, 0)
        tmp_vbox.pack_start(button, False, False)
        #tmp_vbox.pack_start(tmp_align)
        tmp_align.add(tmp_vbox)
        right_box.set_buttons([self.input_num_label, tmp_align])

        # at first, set widget insensitive
        button.set_sensitive(False)
        text_view.set_editable(False)
        t = threading.Thread(target=self.init_user_info_thread, args=(button, text_view))
        t.setDaemon(True)
        t.start()

    # draw text view background
    def text_view_bg_expose(self, widget, event):
        '''draw text view bg'''
        cr = widget.window.cairo_create()
        rect = widget.allocation
        text_pixbuf = app_theme_get_dynamic_pixbuf('image/share/text_view.png').get_pixbuf()
        draw.draw_pixbuf(cr, text_pixbuf, rect.x, rect.y)

    # if text is empty, show tip info
    def text_view_expose(self, text_view, event):
        '''text_view expose'''
        buf = text_view.get_buffer()
        text = buf.get_text(*buf.get_bounds())

        if text == "" and text_view.get_editable() and not text_view.is_focus():
            win = text_view.get_window(gtk.TEXT_WINDOW_TEXT)
            cr = win.cairo_create()
            cr.move_to(10, 5)
            context = pangocairo.CairoContext(cr)
            layout = context.create_layout()
            layout.set_font_description(pango.FontDescription("Snas 10"))
            layout.set_alignment(pango.ALIGN_LEFT)
            layout.set_text(_("Please input text here"))
            cr.set_source_rgb(0.66, 0.66, 0.66)
            context.update_layout(layout)
            context.show_layout(layout)

    # show input char num
    def text_view_changed(self, buf, button):
        '''text_view changed callback'''
        count = buf.get_char_count()
        if count <= self.MAX_CHAR:
            #self.input_tip_label.set_text(_("left"))
            self.input_num_label.set_text("%d" % (self.MAX_CHAR - count))
            self.input_num_label.text_color = ui_theme.get_color("category_item")
            if not button.is_sensitive():
                button.set_sensitive(True)
        else:
            #self.input_tip_label.set_text(_("exceeds"))
            self.input_num_label.set_text("-%d" % (count - self.MAX_CHAR))
            self.input_num_label.text_color = ui_theme.get_color("category_item")
            if button.is_sensitive():
                button.set_sensitive(False)

    def show_confirm_dialog(self, title, content):
        d = ConfirmDialog(
                title,
                content,
                text_wrap_width=300,
                )
        d.show_all()
        d.set_transient_for(self.window)

    def share_button_clicked(self, button, text_view):
        '''share_button_clicked callback'''
        # file is not exist.
        if not exists(self.upload_image):
            self.show_confirm_dialog(
                    _("Error"),
                    _("Nonexistent picture"),
                    )
            return False
        has_share_web = False
        for weibo in self.to_share_weibo:
            if self.to_share_weibo[weibo]:
                has_share_web = True
                break
        # have no web selected
        if not has_share_web:
            self.show_confirm_dialog(
                    _("Error"),
                    _("Please choose at least one platform to share on"),
                    )
            return False
        # at first, set widget insensitive
        button.set_sensitive(False)
        text_view.set_editable(False)
        #self.window.left_button_box.set_sensitive(False)
        # set weibo checkbutton sensitive
        for check in self.__weibo_check_button_list:
            check.set_sensitive(False)
        # disconnect weibo ico button clicked function
        for img in self.__weibo_image_button_list:
            try:
                img.disconnect_by_func(self.weibo_login)
            except:
                pass
        button.set_label(_("Sharing"))
        t = threading.Thread(target=self.share_to_weibo_thread, args=(text_view, ))
        t.setDaemon(True)
        t.start()

    # upload image thread
    def share_to_weibo_thread(self, text_view):
        '''share in thread'''
        buf = text_view.get_buffer()
        text = buf.get_text(*buf.get_bounds())
        if text.strip() == "":
            text = _("from Deepin Game")
        # get deepin official info
        self.deepin_info[self.sina] = self.sina.get_deepin_info()
        self.deepin_info[self.qq] = self.qq.get_deepin_info()
        if default_locale != 'zh_CN':
            self.deepin_info[self.twitter] = self.twitter.get_deepin_info()
        # upload
        for weibo in self.to_share_weibo:
            if self.to_share_weibo[weibo]:
                self.to_share_weibo_res[weibo] = weibo.upload_image(self.upload_image, text)
        self.share_to_weibo_result()

    # show upload result
    @post_gui
    def share_to_weibo_result(self):
        '''result of share to weibo'''
        font_color = ui_theme.get_color("category_item")
        res_hbox = gtk.HBox(False)
        res_hbox.set_size_request(-1, 240)

        res_left_box = DialogLeftButtonBox()
        res_right_box = DialogRightButtonBox()

        res_left_box.button_align.set(0.5, 0.0, 0, 1)
        res_right_box.button_align.set(0.5, 0.0, 0, 1)
        res_left_box.button_align.set_padding(5, 9, 19, 0)
        res_right_box.button_align.set_padding(30, 0, 0, 0)

        res_left_box.set_size_request(405, -1)
        res_right_box.set_size_request(195, -1)

        res_hbox.pack_start(res_left_box)
        res_hbox.pack_start(
            VSeparator(app_theme.get_shadow_color("VSeparator").get_color_info(), 0, 0))
        res_hbox.pack_start(res_right_box)

        res_vbox = gtk.VBox(False)
        follow_vbox = gtk.VBox(False)

        tmp_img = gtk.Image()       # only use as a placeholder
        tmp_img.set_size_request(-1, 50)
        res_vbox.pack_start(tmp_img, False, False)

        follow_tip_hbox = gtk.HBox(False)
        img = gtk.image_new_from_icon_name("deepin-logo", 16)
        if img.get_pixel_size() == -1:
            img = gtk.image_new_from_file(app_theme.get_theme_file_path("image/share/deepin_logo.png"))
        follow_tip_hbox.pack_start(img, False, False, 5)
        follow_tip_hbox.pack_start(
            Label("%s %s" % (_("Follow"), "Linux Deepin"),
                text_color=app_theme_get_dynamic_color("#5f5f5f"),
                text_size=12, enable_select=False), False, False)
        follow_vbox.pack_start(follow_tip_hbox, False, False, 13)
        for weibo in self.to_share_weibo_res:
            vbox = gtk.VBox(False, 1)
            tip_box = gtk.HBox()
            error_box = gtk.HBox()
            vbox.pack_start(tip_box, False, False)
            vbox.pack_start(error_box, False, False)
            if self.to_share_weibo_res[weibo][0]:   # upload succeed
                img = gtk.image_new_from_file(app_theme.get_theme_file_path("image/share/share_succeed.png"))
                #link = LinkButton(_(weibo.t_type), text_size=13, self.to_share_weibo_res[weibo][1])
                link = Label(self.weibo_name_l18n[weibo.t_type], text_size=12,
                    text_color=ui_theme.get_color("link_text"))
                #, enable_gaussian=True, gaussian_radious=1, border_radious=0)
                link.add_events(gtk.gdk.BUTTON_PRESS_MASK)
                link.connect("enter-notify-event", lambda w, e: self.__draw_under_line(w))
                link.connect("leave-notify-event", lambda w, e: w.queue_draw())
                link.connect("button-press-event", self.goto_weibo_button_clicked, weibo)
                link_box = gtk.HBox(False)
                link_box.pack_start(link, False, False)
                utils.set_clickable_cursor(link)
                text = _("Share to")
                label = Label(text, text_size=12,
                    text_color=font_color, enable_select=False)
                text = _("Successful")
                label1 = Label(text, text_size=12,
                    text_color=font_color, enable_select=False)
                tip_box.pack_start(img, False, False, 15)
                tip_box.pack_start(label, False, False, 3)
                tip_box.pack_start(link_box, False, False, 3)
                tip_box.pack_start(label1, False, False)
                # only use as a placeholder
                img = gtk.Image()
                img.set_size_request(20, 1)
                error_box.pack_start(img, False, False, 16)
                tmp = Label(" ", text_size=9, label_width=200)
                tmp.set_size_request(200, 1)
                error_box.pack_start(tmp, False, False)
                #print text
            else:   # upload failed
                img = gtk.image_new_from_file(app_theme.get_theme_file_path("image/share/share_failed.png"))
                #text = "% %s %s." % (_(weibo.t_type), _("upload failed"))
                text = _("Share to")
                label1 = Label(text, text_size=12,
                    text_color=font_color, enable_select=False)
                label2 = Label(_(weibo.t_type), text_size=12,
                    text_color=font_color, enable_select=False)
                text = _("Failed")
                label3 = Label(text, text_size=12,
                    text_color=font_color, enable_select=False)
                if weibo.curl.error:
                    error = "(%s)" % _(weibo.curl.error)
                elif weibo.get_error_msg():
                    error = "(%s)" % _(weibo.get_error_msg())
                else:
                    error = "(%s)" % _("Unknown reason")
                #print "%s: %s" % (weibo.t_type, error)
                #print "%s: %s" % (weibo.t_type, weibo.get_error_msg())
                label = Label(text, text_size=12,
                    text_color=font_color, enable_select=False)
                tip_box.pack_start(img, False, False, 15)
                tip_box.pack_start(label1, False, False, 3)
                tip_box.pack_start(label2, False, False, 3)
                tip_box.pack_start(label3, False, False)
                img = gtk.Image()   # only use as a placeholder
                img.set_size_request(20, 20)
                error_box.pack_start(img, False, False, 16)
                error_box.pack_start(Label(error, text_size=9, label_width=200,
                    text_color=font_color, enable_select=False), False, False)
                #print text
            res_vbox.pack_start(vbox, False, False, 10)

        for weibo in self.deepin_info:
            box = gtk.HBox(False, 15)
            # followed
            img = gtk.image_new_from_pixbuf(app_theme.get_pixbuf("share/"+weibo.t_type+".png").get_pixbuf())
            box.pack_start(img, False, False)
            if self.deepin_info[weibo] is not None and self.deepin_info[weibo][3]:
                if not default_locale.startswith("zh_"):
                    button = gtk.image_new_from_pixbuf(
                        app_theme.get_pixbuf("share/followed_en.png").get_pixbuf())
                else:
                    button = gtk.image_new_from_pixbuf(
                        app_theme.get_pixbuf("share/followed.png").get_pixbuf())
            else:   # to follow
                if not default_locale.startswith("zh_"):
                    button = ImageButton(
                        app_theme.get_pixbuf("share/follow_normal_en.png"),
                        app_theme.get_pixbuf("share/follow_hover_en.png"),
                        app_theme.get_pixbuf("share/follow_press_en.png"))
                else:
                    button = ImageButton(
                        app_theme.get_pixbuf("share/follow_normal.png"),
                        app_theme.get_pixbuf("share/follow_hover.png"),
                        app_theme.get_pixbuf("share/follow_press.png"))
                button.connect("clicked", self.friendships_add_button_clicked, weibo, box)
            box.pack_start(button, False, False)
            align = gtk.Alignment()
            align.set(0.0, 0.5, 0, 0)
            align.set_padding(0, 0, 30, 0)
            align.add(box)
            follow_vbox.pack_start(align, False, False, 8)

        res_left_box.set_buttons([res_vbox])
        res_right_box.set_buttons([follow_vbox])

        self.result_box.pack_start(res_hbox, False, False)
        self.result_box.show_all()
        self.set_slide_index(2)

    def goto_weibo_button_clicked(self, widget, event, weibo):
        '''goto my weibo'''
        #print "goto weibo button clicked", weibo.t_type, "xdg-open %s" % self.to_share_weibo_res[weibo][1]
        if weibo in self.to_share_weibo_res:
            if self.to_share_weibo_res[weibo][1]:
                webbrowser.open(self.to_share_weibo_res[weibo][1])

    def friendships_add_button_clicked(self, widget, weibo, box):
        '''add friendships'''
        #self.result_box.set_sensitive(False)
        if not self.is_get_user_info[weibo]:
            utils.run_command("xdg-open %s" % weibo.index_url)
            return True

        widget.set_sensitive(False)
        t = threading.Thread(target=self.friendships_add_thread, args=(widget, weibo, box))
        t.setDaemon(True)
        t.start()

    def friendships_add_thread(self, button, weibo, box):
        '''add friendships'''
        if weibo.friendships_create() is not None:
            gtk.gdk.threads_enter()
            button.destroy()
            if not default_locale.startswith("zh_"):
                button = gtk.image_new_from_pixbuf(
                    app_theme.get_pixbuf("share/followed_en.png").get_pixbuf())
            else:
                button = gtk.image_new_from_pixbuf(
                    app_theme.get_pixbuf("share/followed.png").get_pixbuf())
            button.show()
            box.pack_start(button, False, False)
            #button.set_label("已关注")
            gtk.gdk.threads_leave()

    # show window
    def show(self):
        '''show'''
        self.window.show_window()

    # close widnow
    def quit(self, widget):
        ''' close '''
        gtk.main_quit()

    def __slider_expose(self, widget, event):
        ''' slider expose redraw'''
        cr = widget.window.cairo_create()
        rect = widget.allocation
        cr.set_source_rgba(1.0, 1.0, 1.0, 0.8)
        cr.rectangle(rect.x, rect.y, rect.width, rect.height)
        cr.fill()

    def __expose_top_and_bottome(self, widget, event):
        '''titlebar or button_box expose'''
        cr = widget.window.cairo_create()
        rect = widget.allocation
        cr.set_source_rgb(0.89, 0.89, 0.89)
        cr.rectangle(rect.x+2, rect.y+2, rect.width-4, rect.height-4)
        cr.fill()

    def __draw_under_line(self, widget):
        '''draw under line'''
        cr = widget.window.cairo_create()
        with utils.cairo_disable_antialias(cr):
            x, y, w, h = widget.allocation
            # #1A70b1
            cr.set_source_rgba(0.1, 0.43, 0.69, 1.0)
            cr.set_line_width(1)
            cr.move_to(x, y+h-3)
            cr.line_to(x+w, y+h-3)
            cr.stroke()
    def __init__(self):
        super(PreferenceDialog, self).__init__(_("Preference"), 575, 495, 
                                               mask_type=DIALOG_MASK_MULTIPLE_PAGE,
                                               close_callback=self.hide_all)
        
        self.set_position(gtk.WIN_POS_CENTER)
        
        self.main_box = gtk.VBox()
        close_button = Button(_("Close"))
        close_button.connect("clicked", lambda w: self.hide_all())
        
        # Init widget.
        self.general_setting = GeneralSetting()
        self.hotkey_setting = HotKeySetting()
        self.desktop_lyrics_setting = DesktopLyricsSetting()
        self.scroll_lyrics_setting = ScrollLyricsSetting()
        
        # Category bar
        self.category_bar = TreeView(font_x_padding=20)
        self.category_bar.draw_mask = self.draw_treeview_mask
        self.general_category_item = CategoryItem(_("General"), self.general_setting)
        self.category_bar.add_item(None, self.general_category_item)
        self.category_bar.add_item(None, CategoryItem(_("Hotkeys"), self.hotkey_setting))
        lyrics_node = self.category_bar.add_item(None, CategoryItem(_("Lyrics")))
        self.category_bar.add_item(lyrics_node, CategoryItem(_("Desktop"), self.desktop_lyrics_setting))
        self.category_bar.add_item(lyrics_node, CategoryItem(_("Window"), self.scroll_lyrics_setting))
        self.category_bar.add_item(None, CategoryItem(_("About us"), AboutBox()))
        self.category_bar.connect("single-click-item", self.category_single_click_cb)
        self.category_bar.set_highlight_index(0)
        
        category_box = gtk.VBox()
        background_box = BackgroundBox()
        background_box.set_size_request(132, 11)
        background_box.draw_mask = self.draw_treeview_mask
        category_box.pack_start(background_box, False, False)
        
        category_scrolled_window = ScrolledWindow()
        category_scrolled_window.add_child(self.category_bar)
        category_scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
        category_scrolled_window.set_size_request(132, 516)
        
        category_scrolled_window_align = gtk.Alignment()
        category_scrolled_window_align.set(0, 0, 1, 1,)
        category_scrolled_window_align.set_padding(0, 1, 0, 0)
        category_scrolled_window_align.add(category_scrolled_window)
        
        category_box.pack_start(category_scrolled_window_align, True, True)
        
        # Pack widget.
        left_box = gtk.VBox()
        self.right_box = gtk.VBox()
        left_box.add(category_box)
        self.right_box.add(self.general_category_item.get_allocated_widget())
        right_align = gtk.Alignment()
        right_align.set_padding(0, 0, 10, 0)
        right_align.add(self.right_box)

        body_box = gtk.HBox()
        body_box.pack_start(left_box, False, False)
        body_box.pack_start(right_align, False, False)
        self.main_box.add(body_box)
        
        # DialogBox code.
        self.body_box.pack_start(self.main_box, True, True)
        self.right_button_box.set_buttons([close_button])        
Esempio n. 10
0
    def init_share_box(self):
        '''get weibo info, and create button'''
        self.to_share_weibo = {}
        self.to_share_weibo_res = {}
        self.deepin_info = {}
        self.is_get_user_info = {}
        self.__weibo_check_button_list = []
        self.__weibo_image_button_list = []

        # create Thumbnail
        if exists(self.upload_image):
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.upload_image)
            pix_w = pixbuf.get_width()
            pix_h = pixbuf.get_height()
            if pix_w > pix_h:
                pix_s_w = self.thumb_width
                pix_s_h = int(pix_h / (float(pix_w) / self.thumb_width))
            else:
                pix_s_h = self.thumb_height
                pix_s_w = int(pix_w / (float(pix_h) / self.thumb_height))
            pixbuf = pixbuf.scale_simple(pix_s_w, pix_s_h,
                                         gtk.gdk.INTERP_TILES)
            thumb = gtk.image_new_from_pixbuf(pixbuf)
        else:
            thumb = gtk.Image()
        thumb.set_size_request(self.thumb_width, self.thumb_height)

        # weibo context input
        text_box = gtk.HBox(False, 2)
        text_vbox = gtk.VBox(False, 2)
        text_bg_vbox = gtk.VBox(False)
        text_bg_align = gtk.Alignment()
        text_bg_align.set(0.5, 0.5, 0, 0)
        text_bg_align.set_padding(5, 5, 16, 5)
        text_bg_align.connect("expose-event", self.text_view_bg_expose)
        text_scrolled_win = ScrolledWindow()
        text_scrolled_win.set_size_request(340, 157)

        text_view = gtk.TextView()
        text_view.set_left_margin(10)
        text_view.set_right_margin(10)
        text_view.set_pixels_above_lines(5)
        text_view.set_pixels_below_lines(5)
        text_view.set_wrap_mode(gtk.WRAP_WORD | gtk.WRAP_CHAR)
        text_view.connect("expose-event", self.text_view_expose)
        buf = text_view.get_buffer()
        text_scrolled_win.add(text_view)
        text_bg_vbox.pack_start(text_scrolled_win)
        text_bg_align.add(text_bg_vbox)

        text_align = gtk.Alignment()
        text_align.set(0.5, 0.5, 0, 0)
        text_align.set_padding(25, 30, 10, 10)

        text_box.pack_start(thumb, False, False, 10)
        text_box.pack_start(text_bg_align)
        text_vbox.pack_start(text_box, False, False, 10)

        text_align.add(text_vbox)
        #tmp_align = gtk.Alignment()
        #tmp_align.set(0.5, 0, 0, 1)
        #self.share_box.pack_start(tmp_align, False, False)
        self.share_box.pack_start(text_align, False, False)

        # dialog button box
        left_box = self.window.left_button_box
        right_box = self.window.right_button_box

        # input tip label
        self.input_num_label = Label("%d" % self.MAX_CHAR,
                                     text_size=16,
                                     text_x_align=pango.ALIGN_CENTER,
                                     label_width=50,
                                     enable_select=False)
        self.input_num_label.text_color = app_theme.get_color("left_char_num")

        # login box
        #weibo_box = gtk.HBox(False, 1)
        #weibo_box.set_size_request(-1, 50)
        weibo_box_list = []
        self.loading_label = Label("%s..." % _("Loading"),
                                   text_size=12,
                                   label_width=70,
                                   enable_select=False)
        weibo_box_list.append(self.loading_label)

        for weibo in self.__weibo_list:
            box = gtk.HBox(False, 2)
            weibo.set_box(box)
            weibo_box_list.append(box)
        left_box.set_buttons(weibo_box_list)

        # share button
        button = Button(_("Share"))
        #button.set_size_request(75, 25)
        button.connect("clicked", self.share_button_clicked, text_view)
        buf.connect("changed", self.text_view_changed,
                    button)  # check char num

        tmp_vbox = gtk.VBox(False)
        tmp_align = gtk.Alignment()
        tmp_align.set(0.5, 0.5, 0, 0)
        tmp_vbox.pack_start(button, False, False)
        #tmp_vbox.pack_start(tmp_align)
        tmp_align.add(tmp_vbox)
        right_box.set_buttons([self.input_num_label, tmp_align])

        # at first, set widget insensitive
        button.set_sensitive(False)
        text_view.set_editable(False)
        t = threading.Thread(target=self.init_user_info_thread,
                             args=(button, text_view))
        t.setDaemon(True)
        t.start()
Esempio n. 11
0
class ThemePage(gtk.VBox):
    '''
    class docs
    '''
    def __init__(self):
        '''
        init docs
        '''
        gtk.VBox.__init__(self)

        self.status_box = StatusBox()

        self.scroll = ScrolledWindow()
        self.scroll.set_size_request(800, 432)
        self.scroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)

        self.label_padding_x = 10
        self.label_padding_y = 10

        self.theme_box = gtk.VBox()
        self.user_theme_label = Label(
            _("My Themes"),
            text_size=TITLE_FONT_SIZE,
            text_color=app_theme.get_color("globalTitleForeground"))
        self.user_theme_view = UserThemeView(status_box=self.status_box)
        self.user_theme_scrolledwindow = self.user_theme_view.get_scrolled_window(
        )

        self.system_theme_label = Label(
            _("System Themes"),
            text_size=TITLE_FONT_SIZE,
            text_color=app_theme.get_color("globalTitleForeground"))
        self.system_theme_view = SystemThemeView(status_box=self.status_box)
        self.system_theme_scrolledwindow = self.system_theme_view.get_scrolled_window(
        )

        self.theme_box.pack_start(self.user_theme_label, False, False)
        self.theme_box.pack_start(get_separator(), False, False)
        self.theme_box.pack_start(self.user_theme_scrolledwindow, False, False)

        self.theme_box.pack_start(self.system_theme_label, False, False)
        self.theme_box.pack_start(get_separator(), False, False)
        self.theme_box.pack_start(self.system_theme_scrolledwindow, True, True)

        main_align = gtk.Alignment()
        main_align.set_padding(15, 0, 20, 20)
        main_align.set(1, 1, 1, 1)
        main_align.add(self.theme_box)

        self.scroll.add_child(main_align)

        main_align.connect("expose-event", self.expose_label_align)

        self.pack_start(self.scroll, False, False)
        self.pack_start(self.status_box)

    def expose_label_align(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation

        self.draw_mask(cr, rect.x, rect.y, rect.width, rect.height)

    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.
        '''
        cr.set_source_rgb(1, 1, 1)
        cr.rectangle(x, y, w, h)
        cr.fill()
Esempio n. 12
0
class SettingUI(gtk.Alignment):
    def __init__(self, slide_back_cb, change_crumb_cb):
        gtk.Alignment.__init__(self, 0, 0, 0, 0)
        self.slide_back = slide_back_cb
        self.change_crumb = change_crumb_cb

        self.scroll_win = ScrolledWindow()
        self.scroll_win.set_can_focus(False)

        self.scroll_win.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        main_vbox = gtk.VBox()
        self.foot_box = FootBox()
        self.hbox = gtk.HBox()

        self.scroll_win.add_with_viewport(self.hbox)
        self.scroll_align = gtk.Alignment()
        self.scroll_win.set_size_request(800, 435)
        self.scroll_align.set(0, 0, 0, 0)
        self.scroll_align.set_padding(0, 0, 30, 0)
        self.scroll_align.add(self.scroll_win)

        padding_align = gtk.Alignment(0, 0, 0, 0)
        padding_align.set_padding(15, 0, 0, 0)
        self.sidebar = SideBar(None)
        padding_align.add(self.sidebar)
        self.hpaned = MyPaned()
        self.hpaned.set_size_request(800, -1)
        self.hpaned.connect("expose-event", self.expose_line)
        #self.hpaned.do_enter_notify_event = self.enter_notify_event
        self.hpaned.add1(padding_align)
        self.hpaned.add2(self.scroll_align)
        self.connect_after("show", self.__init_paned)
        main_vbox.pack_start(self.hpaned, True, True)
        main_vbox.pack_start(self.foot_box, False, False)
        self.add(main_vbox)

        self.__init_signals()

    def enter_notify_event(self, e):
        pass

    def __init_paned(self, widget):
        log.debug("")
        self.hpaned.saved_position = 160
        self.hpaned.set_position(1)
        self.hpaned.animation_position_frames = [0]
        self.hpaned.update_position()

    def __init_signals(self):
        Dispatcher.connect("connection-change", self.switch_content)
        Dispatcher.connect("setting-saved", self.save_connection_setting)
        Dispatcher.connect("setting-appled", self.apply_connection_setting)
        Dispatcher.connect("request_redraw",
                           lambda w: self.scroll_win.show_all())

    def load_module(self, module_obj, hide_left):
        # create a reference
        self.setting_group = module_obj

        # init paned
        self.__init_paned(None)
        log.info("dss start load module", module_obj)
        self.hpaned.set_button_show(hide_left)

        # init foot_box
        self.foot_box.set_setting(module_obj)

        # init sidebar
        self.sidebar.load_list(module_obj)
        self.apply_method = module_obj.apply_changes
        self.save_method = module_obj.save_changes

    def switch_content(self, widget, connection):
        container_remove_all(self.hbox)
        self.set_tab_content(connection)
        self.set_foot_bar_button(connection)

        self.focus_connection = connection

    def set_foot_bar_button(self, connection):
        if type(connection) == NMRemoteConnection:
            self.foot_box.show_delete(connection)
        else:
            self.foot_box.hide_delete()
        states = self.setting_group.get_button_state(connection)
        if states:
            Dispatcher.set_button(*states)

    def set_tab_content(self, connection):
        log.debug("set tab content", connection)
        setting = self.setting_group.init_items(connection)
        self.hbox.add(setting)
        self.hbox.show_all()
        self.foot_box.set_lock(False)

    def expose_line(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation
        style.draw_out_line(cr, rect, exclude=["left", "right", "top"])

    def draw_tab_title_background(self, cr, widget):
        rect = widget.allocation
        cr.set_source_rgb(1, 1, 1)
        cr.rectangle(0, 0, rect.width, rect.height - 1)
        cr.fill()

    def save_connection_setting(self, widget):
        self.save_method(self.focus_connection)

    def apply_connection_setting(self, widget):
        #print type(self.focus_connection)
        self.apply_method(self.focus_connection)

    def create_new_connection(self):
        self.sidebar.add_new_connection()
class DisplayView(gtk.VBox):
    '''
    class docs
    '''
    def __init__(self):
        '''
        init docs
        '''
        gtk.VBox.__init__(self)

        self.brightness_id = None

        self.display_manager = DisplayManager()
        self.__xrandr_settings = self.display_manager.get_xrandr_settings()
        self.__xrandr_settings.connect("changed", self.__xrandr_changed)

        self.resize_width = 790
        self.resize_height = 200
        self.monitor_items = []
        self.__output_names = []
        self.__current_output_name = self.display_manager.get_primary_output_name(
        )
        self.__setup_monitor_items()
        self.sizes_items = []
        self.monitor_combo = None
        if len(self.monitor_items
               ) > 1 and self.display_manager.is_copy_monitors():
            self.__set_same_sizes()
        else:
            self.__setup_sizes_items()
        self.multi_monitors_items = [(_("Copy Display"), 1),
                                     (_("Extend Display"), 2),
                                     (_("Only shown in display 1"), 3),
                                     (_("Only shown in display 2"), 4)]
        self.rotation_items = [(_("Normal"), 1), (_("Right"), 2),
                               (_("Left"), 3), (_("Inverted"), 4)]
        self.duration_items = [("1 %s" % _("Minute"), 1),
                               ("2 %s" % _("Minutes"), 2),
                               ("3 %s" % _("Minutes"), 3),
                               ("5 %s" % _("Minutes"), 5),
                               ("10 %s" % _("Minutes"), 10),
                               ("30 %s" % _("Minutes"), 30),
                               ("1 %s" % _("Hour"), 60),
                               (_("Never"), DisplayManager.BIG_NUM / 60)]
        '''
        scrolled_window
        '''
        self.scrolled_window = ScrolledWindow()
        self.scrolled_window.set_size_request(-1, 425)
        self.scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        self.main_box = gtk.VBox()
        self.main_box.set_size_request(600, -1)
        self.body_box = gtk.HBox()
        '''
        left, right align
        '''
        self.left_align = self.__setup_align(
            padding_top=FRAME_TOP_PADDING,
            padding_left=TEXT_WINDOW_LEFT_PADDING)
        self.right_align = self.__setup_align(padding_top=FRAME_TOP_PADDING,
                                              padding_left=0)
        '''
        left, right box
        '''
        self.left_box = gtk.VBox(spacing=WIDGET_SPACING)
        self.right_box = gtk.VBox(spacing=WIDGET_SPACING)
        '''
        monitor operation && detect
        '''
        self.monitor_resize_align = self.__setup_align(
            padding_top=11, padding_left=int(TEXT_WINDOW_LEFT_PADDING / 2))
        self.monitor_resize_box = MonitorResizableBox(self.display_manager)
        self.monitor_resize_box.select_output(self.__current_output_name)
        self.monitor_resize_box.connect("select-output", self.__select_output)
        self.monitor_resize_box.connect("resize", self.__resize_box)
        self.monitor_resize_align.add(self.monitor_resize_box)
        '''
        monitor display
        '''
        self.monitor_display_align = self.__setup_title_align(
            app_theme.get_pixbuf("display/monitor_display.png"), _("Display"),
            0)
        '''
        monitor
        '''
        self.monitor_align = self.__setup_align()
        self.monitor_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.monitor_label = self.__setup_label(_("Monitor"))
        self.monitor_combo = self.__setup_combo(self.monitor_items)
        self.monitor_combo.set_select_index(
            self.display_manager.get_primary_output_name_index(
                self.monitor_items))
        self.monitor_combo.connect("item-selected", self.__combo_item_selected,
                                   "monitor_combo")
        self.__widget_pack_start(self.monitor_box,
                                 [self.monitor_label, self.monitor_combo])
        self.monitor_align.add(self.monitor_box)
        '''
        goto individuation or power setting
        '''
        self.goto_align = self.__setup_align()
        self.goto_box = gtk.VBox(spacing=WIDGET_SPACING)
        self.goto_label = self.__setup_label(_("Relevant Settings"),
                                             text_size=TITLE_FONT_SIZE,
                                             width=None,
                                             align=ALIGN_START)
        goto_color = GOTO_FG_COLOR
        self.goto_individuation_label = self.__setup_label(text=_(
            "<span foreground=\"%s\" underline=\"single\">Personalization</span>"
        ) % goto_color,
                                                           width=None,
                                                           align=ALIGN_START)
        self.goto_individuation_label.connect("button-press-event",
                                              self.__button_press,
                                              "individuation")
        set_clickable_cursor(self.goto_individuation_label)
        self.goto_power_label = self.__setup_label(
            text=_("<span foreground=\"%s\" underline=\"single\">Power</span>")
            % goto_color,
            width=None,
            align=ALIGN_START)
        self.goto_power_label.connect("button-press-event",
                                      self.__button_press, "power")
        set_clickable_cursor(self.goto_power_label)
        self.__widget_pack_start(self.goto_box, [
            self.goto_label, self.goto_individuation_label,
            self.goto_power_label
        ])
        self.goto_align.add(self.goto_box)
        '''
        sizes
        '''
        self.sizes_align = self.__setup_align()
        self.sizes_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.sizes_label = self.__setup_label(_("Resolution"))
        self.sizes_combo = self.__setup_combo(self.sizes_items)
        if self.sizes_combo:
            self.sizes_combo.set_select_index(
                self.display_manager.get_screen_size_index(
                    self.__current_output_name, self.sizes_items))
            self.sizes_combo.connect("item-selected",
                                     self.__combo_item_selected, "sizes_combo")
            self.__widget_pack_start(self.sizes_box,
                                     [self.sizes_label, self.sizes_combo])
        self.sizes_align.add(self.sizes_box)
        '''
        rotation
        '''
        self.rotation_align = self.__setup_align()
        self.rotation_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.rotation_label = self.__setup_label(_("Rotation"))
        self.rotation_combo = self.__setup_combo(self.rotation_items)
        self.rotation_combo.set_select_index(
            self.display_manager.get_screen_rotation_index(
                self.__current_output_name))
        self.rotation_combo.connect("item-selected",
                                    self.__combo_item_selected,
                                    "rotation_combo")
        self.__widget_pack_start(self.rotation_box,
                                 [self.rotation_label, self.rotation_combo])
        self.rotation_align.add(self.rotation_box)
        '''
        multi-monitors
        '''
        self.multi_monitors_align = self.__setup_align()
        self.multi_monitors_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.multi_monitors_label = self.__setup_label(_("Multi-Monitor"))
        self.multi_monitors_combo = self.__setup_combo(
            self.multi_monitors_items)
        self.multi_monitors_combo.set_select_index(
            self.display_manager.get_multi_monitor_index())
        self.multi_monitors_combo.connect("item-selected",
                                          self.__combo_item_selected,
                                          "multi_monitors_combo")
        self.__widget_pack_start(
            self.multi_monitors_box,
            [self.multi_monitors_label, self.multi_monitors_combo])
        self.multi_monitors_align.add(self.multi_monitors_box)
        if self.display_manager.get_output_count() < 2:
            self.multi_monitors_align.set_size_request(-1, 0)
            self.multi_monitors_align.set_child_visible(False)
        '''
        monitor brightness
        '''
        self.monitor_bright_align = self.__setup_title_align(
            app_theme.get_pixbuf("display/monitor_bright.png"),
            _("Brightness"))
        '''
        brightness
        '''
        self.brightness_align = self.__setup_align()
        self.brightness_box = gtk.HBox(spacing=2)
        self.brightness_label_align = self.__setup_align(padding_top=8,
                                                         padding_left=0,
                                                         padding_right=5)
        self.brightness_label = self.__setup_label(_("Brightness"))
        self.brightness_label_align.add(self.brightness_label)

        self.brightness_scale = HScalebar(
            point_dpixbuf=app_theme.get_pixbuf("scalebar/point.png"),
            value_min=0.1,
            value_max=1.0)
        self.brightness_scale.set_size_request(HSCALEBAR_WIDTH, 33)
        self.brightness_scale.set_value(
            self.display_manager.get_screen_brightness())
        self.brightness_scale.connect("value-changed", self.__set_brightness)
        self.__widget_pack_start(
            self.brightness_box,
            [self.brightness_label_align, self.brightness_scale])
        self.brightness_align.add(self.brightness_box)
        '''
        auto adjust monitor brightness
        '''
        self.auto_adjust_align = self.__setup_align()
        self.auto_adjust_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.auto_adjust_label = self.__setup_label(_("Auto-Brightness"))
        self.auto_adjust_toggle_align = self.__setup_align(padding_top=4,
                                                           padding_left=158)
        self.auto_adjust_toggle = self.__setup_toggle()
        self.auto_adjust_toggle.set_active(
            self.display_manager.is_enable_close_monitor())
        self.auto_adjust_toggle.connect("toggled", self.__toggled,
                                        "auto_adjust_toggle")
        self.auto_adjust_toggle_align.add(self.auto_adjust_toggle)
        self.__widget_pack_start(
            self.auto_adjust_box,
            [self.auto_adjust_label, self.auto_adjust_toggle_align])
        self.auto_adjust_align.add(self.auto_adjust_box)
        '''
        close monitor
        '''
        self.close_monitor_align = self.__setup_align()
        self.close_monitor_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.close_monitor_label = self.__setup_label(_("Turn off monitor"))
        self.close_monitor_combo = self.__setup_combo(self.duration_items)
        self.close_monitor_combo.set_select_index(
            self.display_manager.get_close_monitor_index(self.duration_items))
        self.close_monitor_combo.connect("item-selected",
                                         self.__combo_item_selected,
                                         "close_monitor_combo")
        self.__widget_pack_start(
            self.close_monitor_box,
            [self.close_monitor_label, self.close_monitor_combo])
        self.close_monitor_align.add(self.close_monitor_box)
        '''
        monitor lock
        '''
        self.monitor_lock_align = self.__setup_title_align(
            app_theme.get_pixbuf("lock/lock.png"), _("Lock Screen"))
        '''
        auto monitor lock
        '''
        self.auto_lock_align = self.__setup_align()
        self.auto_lock_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.auto_lock_label = self.__setup_label(
            _("Lock screen automatically"))
        self.auto_lock_toggle_align = self.__setup_align(padding_top=4,
                                                         padding_left=158)
        self.auto_lock_toggle = self.__setup_toggle()
        is_enable_lock_display = self.display_manager.is_enable_lock_display()
        self.auto_lock_toggle.set_active(is_enable_lock_display)
        self.auto_lock_toggle.connect("toggled", self.__toggled,
                                      "auto_lock_toggle")
        self.auto_lock_toggle_align.add(self.auto_lock_toggle)
        self.__widget_pack_start(
            self.auto_lock_box,
            [self.auto_lock_label, self.auto_lock_toggle_align])
        self.auto_lock_align.add(self.auto_lock_box)
        '''
        lock display
        '''
        self.lock_display_align = self.__setup_align(padding_bottom=20)
        self.lock_display_box = gtk.HBox(spacing=WIDGET_SPACING)
        self.lock_display_label = self.__setup_label(_("Lock Screen"))
        self.lock_display_combo = self.__setup_combo(self.duration_items)
        self.lock_display_combo.set_select_index(
            self.display_manager.get_lock_display_index(self.duration_items))
        self.lock_display_combo.connect("item-selected",
                                        self.__combo_item_selected,
                                        "lock_display_combo")
        self.__widget_pack_start(
            self.lock_display_box,
            [self.lock_display_label, self.lock_display_combo])
        self.lock_display_align.add(self.lock_display_box)
        '''
        left_align pack_start
        '''
        self.__widget_pack_start(
            self.left_box,
            [
                self.monitor_display_align,
                self.monitor_align,
                self.sizes_align,
                self.rotation_align,
                self.multi_monitors_align,
                self.monitor_bright_align,
                self.brightness_align,
                #self.auto_adjust_align,
                #self.close_monitor_align,
                self.monitor_lock_align,
                self.auto_lock_align,
                self.lock_display_align
            ])
        self.left_align.add(self.left_box)
        '''
        right_align pack_start
        '''
        self.__widget_pack_start(self.right_box, [self.goto_align])
        self.right_box.set_size_request(280, -1)
        self.right_align.add(self.right_box)
        '''
        main && body box
        '''
        self.main_box.pack_start(self.monitor_resize_align, False, False)
        self.body_box.pack_start(self.left_align)
        self.body_box.pack_start(self.right_align, False, False)
        self.main_box.pack_start(self.body_box)
        '''
        this->HBox pack_start
        '''
        self.scrolled_window.add_child(self.main_box)
        self.pack_start(self.scrolled_window)

        self.__send_message("status", ("display", ""))

    def show_again(self):
        self.__send_message("status", ("display", ""))

    def reset(self):
        self.__send_message("status", ("display", _("Reset to default value")))
        self.display_manager.reset()
        self.close_monitor_combo.set_select_index(
            self.display_manager.get_close_monitor_index(self.duration_items))
        self.lock_display_combo.set_select_index(
            self.display_manager.get_lock_display_index(self.duration_items))
        self.multi_monitors_combo.set_select_index(0)

    def __handle_dbus_replay(self, *reply):
        pass

    def __handle_dbus_error(self, *error):
        pass

    def __send_message(self, message_type, message_content):
        if is_dbus_name_exists(APP_DBUS_NAME):
            bus_object = dbus.SessionBus().get_object(APP_DBUS_NAME,
                                                      APP_OBJECT_NAME)
            method = bus_object.get_dbus_method("message_receiver")
            method(message_type,
                   message_content,
                   reply_handler=self.__handle_dbus_replay,
                   error_handler=self.__handle_dbus_error)

    def __button_press(self, widget, event, module_id):
        self.__send_message("goto", (module_id, ""))

    def __expose(self, widget, event):
        try:
            cr = widget.window.cairo_create()
            rect = widget.allocation

            cr.set_source_rgb(*color_hex_to_cairo(MODULE_BG_COLOR))
            cr.rectangle(rect.x, rect.y, rect.width, rect.height)
            cr.fill()
        except e:
            print "DEBUG", e

    def __change_current_output(self, output_name, from_monitor_combo=True):
        self.__current_output_name = output_name

        if not from_monitor_combo:
            self.monitor_combo.set_select_index(
                self.display_manager.get_output_name_index(
                    output_name, self.monitor_items))

        if not self.display_manager.is_copy_monitors():
            self.__setup_sizes_items()
        if len(self.sizes_items):
            self.sizes_combo.add_items(items=self.sizes_items)
        self.sizes_combo.set_select_index(
            self.display_manager.get_screen_size_index(
                self.__current_output_name, self.sizes_items))

    def __select_output(self, widget, output_name):
        print "Output name:", output_name
        self.__change_current_output(output_name, False)

    def __set_same_sizes(self):
        same_sizes = self.display_manager.get_same_sizes(
            self.display_manager.get_screen_sizes(self.monitor_items[0][1]),
            self.display_manager.get_screen_sizes(self.monitor_items[1][1]))
        i = 0

        del self.sizes_items[:]
        while i < len(same_sizes):
            self.sizes_items.append((same_sizes[i], i))

            i += 1

    def __xrandr_changed(self, key):
        if key == "output-names":
            self.display_manager.init_xml()
            self.__setup_monitor_items()
            self.monitor_combo.add_items(items=self.monitor_items)
            if len(self.monitor_items) > 1:
                if self.display_manager.is_copy_monitors():
                    self.__set_same_sizes()
                    self.sizes_combo.add_items(items=self.sizes_items)

                self.multi_monitors_align.set_size_request(-1, 30)
                self.multi_monitors_align.set_child_visible(True)
            else:
                self.multi_monitors_align.set_size_request(-1, 0)
                self.multi_monitors_align.set_child_visible(False)
            return

        if key == "brightness":
            self.brightness_scale.set_value(
                self.display_manager.get_screen_brightness())
            return

    def __set_brightness_value(self, value):
        self.display_manager.set_screen_brightness(self.__current_output_name,
                                                   value)

    def __set_brightness(self, widget, event):
        value = self.brightness_scale.get_value()
        self.__send_message(
            "status",
            ("display", _("Changed brightness to %d%%") % int(value * 100)))
        if self.brightness_id:
            gobject.source_remove(self.brightness_id)
        self.brightness_id = gobject.timeout_add_seconds(
            1, self.__set_brightness_value, value)

    def __setup_monitor_items(self):
        self.__output_names = self.display_manager.get_output_names()
        del self.monitor_items[:]
        i = 0

        while (i < len(self.__output_names)):
            self.monitor_items.append(
                self.display_manager.get_output_name(self.__output_names[i]))
            i += 1

    def __setup_sizes_items(self):
        screen_sizes = self.display_manager.get_screen_sizes(
            self.__current_output_name)
        del self.sizes_items[:]
        i = 0

        while i < len(screen_sizes):
            self.sizes_items.append((screen_sizes[i], i))
            i += 1

    def __toggled(self, widget, object=None):
        if object == "auto_adjust_toggle":
            if not widget.get_active():
                self.__send_message(
                    "status", ("display", _("Changed to manual adjustment")))
                self.display_manager.set_close_monitor(DisplayManager.BIG_NUM /
                                                       60)
            else:
                self.__send_message(
                    "status",
                    ("display", _("Changed to automatic adjustment")))
            return

        if object == "auto_lock_toggle":
            if not widget.get_active():
                self.lock_display_combo.set_sensitive(False)
                self.__send_message("status",
                                    ("display", _("Changed to manual lock")))
            else:
                self.lock_display_combo.set_sensitive(True)
                self.__send_message(
                    "status", ("display", _("Changed to automatic lock")))
            return

    def __combo_item_selected(self,
                              widget,
                              item_text=None,
                              item_value=None,
                              item_index=None,
                              object=None):
        if object == "monitor_combo":
            self.__send_message(
                "status",
                ("display", _("Changed current output to %s") % item_text))
            self.__change_current_output(item_value)
            return

        if object == "sizes_combo":
            size = self.sizes_items[item_value][0]
            self.__send_message(
                "status", ("display", _("Changed resolution to %s") % size))
            self.display_manager.set_screen_size(self.__current_output_name,
                                                 size)
            return

        if object == "rotation_combo":
            self.__send_message(
                "status", ("display", _("Changed rotation to %s") % item_text))
            self.display_manager.set_screen_rotation(
                self.__current_output_name, item_value)
            return

        if object == "multi_monitors_combo":
            self.__send_message(
                "status",
                ("display", _("Changed multi-monitor mode to %s") % item_text))
            self.display_manager.set_multi_monitor(item_value)
            return

        if object == "close_monitor_combo":
            self.__send_message("status", (
                "display",
                _("Idle time before turning off display has been changed to to %s"
                  ) % item_text))
            self.display_manager.set_close_monitor(item_value)
            return

        if object == "lock_display_combo":
            self.__send_message(
                "status",
                ("display",
                 _("Idle time before locking display has been changed to %s") %
                 item_text))
            if item_value == DisplayManager.BIG_NUM / 60:
                self.auto_lock_toggle.set_active(False)
            else:
                self.auto_lock_toggle.set_active(True)
            self.display_manager.set_lock_display(item_value)
            return

    def __resize_box(self, widget, height):
        self.monitor_resize_box.set_size_request(self.resize_width,
                                                 height - FRAME_TOP_PADDING)

    def __setup_separator(self):
        hseparator = HSeparator(
            app_theme.get_shadow_color("hSeparator").get_color_info(), 0, 0)
        hseparator.set_size_request(500, HSEPARATOR_HEIGHT)
        return hseparator

    def __setup_title_label(
            self,
            text="",
            text_color=app_theme.get_color("globalTitleForeground"),
            text_size=TITLE_FONT_SIZE,
            text_x_align=ALIGN_START,
            label_width=180):
        return Label(text=text,
                     text_color=text_color,
                     text_size=text_size,
                     text_x_align=text_x_align,
                     label_width=label_width,
                     enable_select=False,
                     enable_double_click=False)

    def __setup_label(self,
                      text="",
                      text_size=CONTENT_FONT_SIZE,
                      width=180,
                      align=ALIGN_END,
                      wrap_width=None):
        label = Label(text=text,
                      text_color=None,
                      text_size=text_size,
                      text_x_align=align,
                      label_width=width,
                      wrap_width=wrap_width,
                      enable_select=False,
                      enable_double_click=False)
        return label

    def __setup_combo(self, items=[], width=HSCALEBAR_WIDTH):
        if len(items) == 0:
            return None

        combo = ComboBox(items=items,
                         select_index=0,
                         max_width=width,
                         fixed_width=width)
        combo.set_size_request(width, WIDGET_HEIGHT)
        return combo

    def __setup_toggle(self):
        toggle = ToggleButton(
            app_theme.get_pixbuf("toggle_button/inactive_normal.png"),
            app_theme.get_pixbuf("toggle_button/active_normal.png"))
        return toggle

    def __setup_title_align(self,
                            pixbuf,
                            text,
                            padding_top=FRAME_TOP_PADDING,
                            padding_left=0):
        align = self.__setup_align(padding_top=padding_top,
                                   padding_left=padding_left)
        align_box = gtk.VBox(spacing=WIDGET_SPACING)
        title_box = gtk.HBox(spacing=WIDGET_SPACING)
        image = ImageBox(pixbuf)
        label = self.__setup_title_label(text)
        separator = self.__setup_separator()
        self.__widget_pack_start(title_box, [image, label])
        self.__widget_pack_start(align_box, [title_box, separator])
        align.add(align_box)
        return align

    def __setup_align(self,
                      xalign=0,
                      yalign=0,
                      xscale=0,
                      yscale=0,
                      padding_top=0,
                      padding_bottom=0,
                      padding_left=FRAME_LEFT_PADDING +
                      int(WIDGET_SPACING / 2),
                      padding_right=0):
        align = gtk.Alignment()
        align.set(xalign, yalign, xscale, yscale)
        align.set_padding(padding_top, padding_bottom, padding_left,
                          padding_right)
        align.connect("expose-event", self.__expose)
        return align

    def __widget_pack_start(self,
                            parent_widget,
                            widgets=[],
                            expand=False,
                            fill=False):
        if parent_widget == None:
            return
        for item in widgets:
            parent_widget.pack_start(item, expand, fill)
        '''
        pass
    
    def icon_item_release_resource(self):
        # Return True to tell IconView call gc.collect() to release memory resource.
        if self.pixbuf:
            del self.pixbuf
        self.pixbuf = None    
        return True
    
if __name__ == '__main__':
    gtk.gdk.threads_init()
    module_frame = ModuleFrame(os.path.join(get_parent_dir(__file__, 2), "config.ini"))

    scrolled_window = ScrolledWindow()
    scrolled_window.set_size_request(788, 467)
    wallpaper_view = WallpaperView()
    scrolled_window.add_child(wallpaper_view)
    module_frame.add(scrolled_window)
    
    scrolled_window.connect("vscrollbar-state-changed", wallpaper_view.load_more_background)
    
    download_pool = MissionThreadPool(5)
    download_pool.start()
    
    def message_handler(*message):
        (message_type, message_content) = message
        if message_type == "show_again":
            module_frame.send_module_info()
        elif message_type == "exit":
            module_frame.exit()
class ThemePage(gtk.VBox):
    '''
    class docs
    '''
	
    def __init__(self):
        '''
        init docs
        '''
        gtk.VBox.__init__(self)

        self.status_box = StatusBox()

        self.scroll = ScrolledWindow()
        self.scroll.set_size_request(800, 432)
        self.scroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)

        self.label_padding_x = 10
        self.label_padding_y = 10
        
        self.theme_box = gtk.VBox()
        self.user_theme_label = Label(_("My Themes"), text_size=TITLE_FONT_SIZE, 
                                      text_color=app_theme.get_color("globalTitleForeground"))
        self.user_theme_view = UserThemeView(status_box = self.status_box)
        self.user_theme_scrolledwindow = self.user_theme_view.get_scrolled_window()
        
        self.system_theme_label = Label(_("System Themes"), text_size=TITLE_FONT_SIZE, 
                                      text_color=app_theme.get_color("globalTitleForeground"))
        self.system_theme_view = SystemThemeView(status_box = self.status_box)
        self.system_theme_scrolledwindow = self.system_theme_view.get_scrolled_window()
        
        self.theme_box.pack_start(self.user_theme_label, False, False)
        self.theme_box.pack_start(get_separator(), False, False)
        self.theme_box.pack_start(self.user_theme_scrolledwindow, False, False)
        
        self.theme_box.pack_start(self.system_theme_label, False, False)
        self.theme_box.pack_start(get_separator(), False, False)
        self.theme_box.pack_start(self.system_theme_scrolledwindow, True, True)
        
        main_align = gtk.Alignment()
        main_align.set_padding(15, 0, 20, 20)
        main_align.set(1, 1, 1, 1)
        main_align.add(self.theme_box)
        
        self.scroll.add_child(main_align)
        
        main_align.connect("expose-event", self.expose_label_align)

        self.pack_start(self.scroll, False, False)
        self.pack_start(self.status_box)
        
    def expose_label_align(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation
        
        self.draw_mask(cr, rect.x, rect.y, rect.width, rect.height)
                
    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.
        '''
        cr.set_source_rgb(1, 1, 1)
        cr.rectangle(x, y, w, h)
        cr.fill()
Esempio n. 16
0
class PlaylistItem(gobject.GObject):
    
    def __init__(self, playlist):
        '''Init song item.'''
        self.item_id = None
        self.main_box = gtk.VBox()
        self.update(playlist)        
        self.create_jobs_box()


    def draw_mask(self, widget, event):            
        cr = widget.window.cairo_create()
        rect = widget.allocation
        draw_alpha_mask(cr, rect.x, rect.y, rect.width, rect.height, "layoutMiddle")
        
    def create_jobs_box(self):    
        
        self.file_job_button = self.create_job_button("plus", _("Add Music"), self.song_view.recursion_add_dir)
        # self.file_job_button.connect("clicked", self.open_file_or_dir)

        self.job_box = gtk.EventBox()
        self.job_box.set_size_request(220, -1)
        targets = [("text/deepin-songs", gtk.TARGET_SAME_APP, 1), ("text/uri-list", 0, 2), ("text/plain", 0, 3)]
        self.job_box.drag_dest_set(gtk.DEST_DEFAULT_MOTION | gtk.DEST_DEFAULT_DROP,
                           targets, gtk.gdk.ACTION_COPY)
        self.job_box.set_visible_window(False)
        self.job_box.connect("drag-data-received", self.song_view.on_drag_data_received)
        
        # Content box. 
        
        content_box = gtk.VBox()
        content_box.pack_start(create_bottom_align(), True, True)
        # content_box.pack_start(ImageBox(app_theme.get_pixbuf("jobs/scan_tip.png")), False, False)
        content_box.pack_start(self.file_job_button, False, False)
        content_box.pack_start(create_upper_align(), True, True)
        
        # Rind box.
        rind_box = gtk.HBox()
        rind_box.pack_start(create_right_align(), True, True)
        rind_box.pack_start(content_box, False, False)
        rind_box.pack_start(create_left_align(), True, True)
        
        self.job_box.add(rind_box)
        self.jobs_align = gtk.Alignment()
        self.jobs_align.set(0.5, 0.5, 1, 1)
        self.jobs_align.add(self.job_box)
        self.jobs_align.connect("expose-event", self.draw_mask)
        
    def create_job_button(self, icon_name, content, callback=None):    
        button = ComplexButton(
            [app_theme.get_pixbuf("jobs/complex_normal.png"),
             app_theme.get_pixbuf("jobs/complex_hover.png"),
             app_theme.get_pixbuf("jobs/complex_press.png")],
            app_theme.get_pixbuf("jobs/%s.png" % icon_name),
            content
            )
        if callback:
            button.connect("clicked", lambda w : callback())
        return button    
        
    def set_title(self, value):    
        self.title = value
        
    def get_title(self):    
        return self.title
    
    def get_left_image(self):
        return None
    
    def get_has_arrow(self):
        return None
    
    def set_item_id(self, index):
        self.item_id = index
        
    def get_item_id(self):    
        return self.item_id
        
    def update(self, playlist):
        '''update'''
        self.playlist = playlist
        songs = self.playlist.get_songs()
        self.song_view = SongView()
        self.song_view.add_songs(songs)
        self.song_view.connect("begin-add-items", lambda w: self.switch_it())
        self.song_view.connect("empty-items", lambda w: self.switch_it(False))
        self.scrolled_window = ScrolledWindow(0, 0)
        self.scrolled_window.add_child(self.song_view)
        self.scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        self.scrolled_window.set_size_request(220, -1)
        self.title = playlist.get_name()
        
    def get_list_widget(self):
        if self.get_songs():
            switch_box(self.main_box, self.scrolled_window)
        else:    
            switch_box(self.main_box, self.jobs_align)
        return self.main_box    
    
    def switch_it(self, scrolled_window=True):
        if scrolled_window:
            switch_box(self.main_box, self.scrolled_window)
        else:    
            switch_box(self.main_box, self.jobs_align)
    
    def get_songs(self):
        if self.song_view:
            return self.song_view.get_songs()