1# This file is part of MyPaint.
2# Copyright (C) 2013-2018 by the MyPaint Development Team
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7
8
9"""Color preview widget / current color indicator, for the status bar."""
10
11# TODO: This *might* evolve to a color preview + alpha selector, possibly
12# TODO:   with a history row taking up the bottom. For now let's draw it at
13# TODO:   an aspect ratio of about 1:5 and see how users like it.
14
15from __future__ import division, print_function
16
17from .colors import PreviousCurrentColorAdjuster
18
19
20class BrushColorIndicator (PreviousCurrentColorAdjuster):
21    """Previous/Current color adjuster bound to app.brush_color_manager"""
22
23    __gtype_name__ = "MyPaintBrushColorIndicator"
24
25    HAS_DETAILS_DIALOG = False
26
27    def __init__(self):
28        PreviousCurrentColorAdjuster.__init__(self)
29        self.connect("realize", self._init_color_manager)
30        self._app = None
31        self.clicked += self._clicked_cb
32
33    def _init_color_manager(self, widget):
34        from gui.application import get_app
35        self._app = get_app()
36        mgr = self._app.brush_color_manager
37        assert mgr is not None
38        self.set_color_manager(mgr)
39
40    def _clicked_cb(self, adj, event, pos):
41        x0, y0 = pos
42        w = self.get_allocated_width()
43        if x0 > w // 2:
44            return
45        chooser = self._app.drawWindow.color_chooser
46        if chooser.get_visible():
47            chooser.hide()
48        else:
49            chooser.popup(
50                widget=self,
51                above=True,
52                textwards=True,
53                event=event,
54            )
55