Exemplo n.º 1
0
    def configure_colors(self):
        # Get colors from GTKrc file
        if not resource_exists(__name__, self.get_assets_path("gtk-2.0", "gtkrc")):
            raise ValueError("GTK theme does not exist")

        gtkrc_file_path = resource_filename(__name__, self.get_assets_path("gtk-2.0", "gtkrc"))
        with open(gtkrc_file_path) as f:
            lines = f.readlines()

        for line in lines:
            if re.match("\s*gtk_color_scheme", line):
                color = re.findall(r'"(.*?)"', line)
                color = color[0]
                color = color.split(':')
                self.colors[color[0].upper()] = color[1]
                self.gtk_colors[color[0].upper()] = gtk.gdk.Color(color[1])

        # Get color definitions
        color_file_path = resource_filename(__name__, self.get_assets_path(filename="colors.json"))
        try:
            colors = storage_utils.load_objects_from_json(color_file_path)
        except IOError:
            raise ValueError("No color definitions found")

        # replace unicode strings with str strings
        colors = {str(key): str(value) for key, value in colors.iteritems()}
        gtk_colors = {str(key): gtk.gdk.Color(str(value)) for key, value in colors.iteritems()}
        self.gtk_colors.update(gtk_colors)
        self.colors.update(colors)
Exemplo n.º 2
0
    def configure_gtk(self):
        if not resource_exists(__name__, self.get_assets_path()):
            raise ValueError("GTK theme 'RAFCON' does not exist")

        theme_name = "RAFCON"
        dark_theme = self.get_config_value('THEME_DARK_VARIANT', True)

        data_dir = resource_filename(__name__,
                                     self.get_assets_path(for_theme=False))
        os.environ['GTK_DATA_PREFIX'] = data_dir
        os.environ['GTK_THEME'] = "{}{}".format(theme_name,
                                                ":dark" if dark_theme else "")

        # The env vars GTK_DATA_PREFIX and GTK_THEME must be set before Gtk is imported first to prevent GTK warnings
        # from other themes
        try:
            from gi.repository import Gtk
            settings = Gtk.Settings.get_default()
            if settings:
                settings.set_property("gtk-theme-name", theme_name)
                settings.set_property("gtk-application-prefer-dark-theme",
                                      dark_theme)

            Gtk.Window.set_default_icon_name(
                "rafcon" if dark_theme else "rafcon-light")
        except ImportError:
            pass
Exemplo n.º 3
0
    def configure_colors(self):
        from gi.repository import Gdk
        dark_theme = self.get_config_value('THEME_DARK_VARIANT', True)
        css_filename = "gtk-dark.css" if dark_theme else "gtk.css"
        # Get colors from GTKrc file
        if not resource_exists(__name__,
                               self.get_assets_path("gtk-3.0", css_filename)):
            raise ValueError("GTK theme does not exist")

        # Provide black as fallback color if theme is not found instead of crashing
        self.colors = defaultdict(lambda: "#FFFFFF")
        self.gtk_colors = defaultdict(lambda: Gdk.RGBA(0, 0, 0).to_color())

        gtkrc_file_path = resource_filename(
            __name__, self.get_assets_path("gtk-3.0", css_filename))
        with open(gtkrc_file_path) as f:
            lines = f.readlines()

        for line in lines:
            match = re.match("\s*@define-color (\w*) (#[\w]{3,6})", line)
            if match:
                color_name = match.group(1).upper()
                color_code = match.group(2)
                self.colors[color_name] = color_code
                gtk_color = Gdk.RGBA()
                if gtk_color.parse(color_code):
                    self.gtk_colors[color_name] = gtk_color.to_color()
                else:
                    self.logger.warning(
                        "Could not parse color with name '{}' and code '{}'".
                        format(color_name, color_code))

        # Get color definitions
        colors_filename = "colors-dark.json" if dark_theme else "colors.json"
        color_file_path = resource_filename(
            __name__, self.get_assets_path(filename=colors_filename))
        try:
            colors = storage_utils.load_objects_from_json(color_file_path)
        except IOError:
            raise ValueError("No color definitions found")

        for color_name, color_code in colors.items():
            # replace unicode strings with str strings
            color_name = str(color_name)
            color_code = str(color_code)
            if color_code.startswith("#"):
                color = Gdk.Color.parse(color_code)[1]
            elif color_code in self.colors:
                color = self.gtk_colors[color_code]
                color_code = self.gtk_colors[color_code]
            else:
                self.logger.warning(
                    "Undefined color alias '{}' for color name '{}'".format(
                        color_code, color_name))
                continue
            self.gtk_colors[color_name] = color
            self.colors[color_name] = color_code
Exemplo n.º 4
0
    def configure_gtk(self):
        if not resource_exists(__name__, self.get_assets_path("gtk-2.0", "gtkrc")):
            raise ValueError("GTK theme does not exist")
        gtkrc_file_path = resource_filename(__name__, self.get_assets_path("gtk-2.0", "gtkrc"))
        filename = resource_filename(__name__, self.get_assets_path(
            "icons", "RAFCON_figurative_mark_negative.svg", for_theme=False))
        gtk.window_set_default_icon_from_file(filename)

        # wait for all gtk events being processed before parsing the gtkrc file
        wait_for_gui()
        gtk.rc_parse(gtkrc_file_path)