示例#1
0
    def follow_if_link(self,
                       text_view: 'LinkedTimeView',
                       itr: Gtk.TextIter) -> bool:
        """Launch a timer if a link was clicked.

        This is done by emitting the `time-link-activated` signal defined in
        this class.
        Whether or not the it was a link, the click won't be processed further.
        """
        # Get the displayed text sentence, to use as a label in the timer.
        start_sentence = itr.copy()
        start_sentence.backward_sentence_start()

        end_sentence = itr.copy()
        if not end_sentence.ends_sentence():
            end_sentence.forward_sentence_end()

        sentence = self.get_buffer().get_slice(start_sentence,
                                               end_sentence,
                                               False)

        # Get the time duration (target of the link).
        start_ts = itr.copy()
        start_ts.backward_to_tag_toggle()
        itr.forward_to_tag_toggle()
        end_ts = itr.copy()
        time_string = self.get_buffer().get_slice(start_ts, end_ts, False)

        # Confirm that is is in the links dictionary.
        if self.get_buffer().markup_dict.get(time_string) == time_string:
            self.emit("time-link-activated", time_string, sentence)

        return False  # Do not process the event further.
示例#2
0
    def follow_if_link(self,
                       text_view: 'LinkedTimeView',
                       itr: Gtk.TextIter) -> bool:
        """Looks at all tags covered by the TextIter position in the text view,
           and if one of them is a time link, emit a signal to display the timer
        """
        blue = Gdk.RGBA(red=0., green=0, blue=1., alpha=1.)

        tags = itr.get_tags()
        for tag in tags:
            color = tag.get_property('foreground-rgba')
            if color and color == blue:
                # By Gourmet convention, only links have color
                start_sentence = itr.copy()
                start_sentence.backward_sentence_start()

                end_sentence = itr.copy()
                if not end_sentence.ends_sentence():
                    end_sentence.forward_sentence_end()

                sentence = self.get_buffer().get_slice(start_sentence,
                                                      end_sentence,
                                                      False)

                start_ts = itr.copy()
                start_ts.backward_to_tag_toggle(tag)
                itr.forward_to_tag_toggle(tag)
                end_ts = itr.copy()
                time_string = self.get_buffer().get_slice(start_ts,
                                                          end_ts,
                                                          False)
                self.emit("time-link-activated", time_string, sentence)
                return True

        return False