diffmap.py 6.88 KB
Newer Older
1
# Copyright (C) 2002-2009 Stephen Kennedy <stevek@gnome.org>
2
# Copyright (C) 2009-2013, 2015 Kai Willadsen <kai.willadsen@gmail.com>
3
#
4 5 6 7 8 9 10 11 12 13 14 15
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

17 18
import collections

19
import cairo
20 21
from gi.repository import Gdk
from gi.repository import Gtk
22

23
from meld.misc import get_common_theme
24
from meld.settings import meldsettings
25

26

27
class DiffMap(Gtk.DrawingArea):
28 29 30 31

    __gtype_name__ = "DiffMap"

    def __init__(self):
32
        super().__init__()
33
        self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
34
        self._last_allocation = None
35 36 37 38 39
        self._scrolladj = None
        self._difffunc = lambda: None
        self._handlers = []
        self._y_offset = 0
        self._h_offset = 0
40 41
        self._y_start = 0
        self._height = 0
42
        self._setup = False
43
        self._width = 10
44 45
        meldsettings.connect('changed', self.on_setting_changed)
        self.on_setting_changed(meldsettings, 'style-scheme')
46

47
    def setup(self, scrollbar, change_chunk_fn):
48 49 50 51
        for (o, h) in self._handlers:
            o.disconnect(h)

        self._scrolladj = scrollbar.get_adjustment()
52
        self.on_scrollbar_style_updated(scrollbar)
53
        self.on_scrollbar_size_allocate(scrollbar, scrollbar.get_allocation())
54
        scrollbar.ensure_style()
55 56
        scroll_style_hid = scrollbar.connect("style-updated",
                                             self.on_scrollbar_style_updated)
57 58
        scroll_size_hid = scrollbar.connect("size-allocate",
                                            self.on_scrollbar_size_allocate)
59
        adj_change_hid = self._scrolladj.connect("changed",
Kai Willadsen's avatar
Kai Willadsen committed
60
                                                 lambda w: self.queue_draw())
61 62
        adj_val_hid = self._scrolladj.connect("value-changed",
                                              lambda w: self.queue_draw())
63
        self._handlers = [(scrollbar, scroll_style_hid),
64 65 66
                          (scrollbar, scroll_size_hid),
                          (self._scrolladj, adj_change_hid),
                          (self._scrolladj, adj_val_hid)]
67
        self._difffunc = change_chunk_fn
68
        self._setup = True
69
        self._cached_map = None
70 71
        self.queue_draw()

72 73 74
    def on_diffs_changed(self, *args):
        self._cached_map = None

75 76 77
    def on_setting_changed(self, meldsettings, key):
        if key == 'style-scheme':
            self.fill_colors, self.line_colors = get_common_theme()
78

79
    def on_scrollbar_style_updated(self, scrollbar):
80 81 82 83
        stepper_size = scrollbar.style_get_property("stepper-size")
        stepper_spacing = scrollbar.style_get_property("stepper-spacing")

        has_backward = scrollbar.style_get_property("has-backward-stepper")
Kai Willadsen's avatar
Kai Willadsen committed
84 85
        has_secondary_backward = scrollbar.style_get_property(
            "has-secondary-backward-stepper")
86 87
        has_secondary_forward = scrollbar.style_get_property(
            "has-secondary-forward-stepper")
Kai Willadsen's avatar
Kai Willadsen committed
88 89
        has_forward = scrollbar.style_get_property("has-forward-stepper")
        steppers = [
90 91
            has_backward, has_secondary_backward,
            has_secondary_forward, has_forward,
Kai Willadsen's avatar
Kai Willadsen committed
92
        ]
93 94 95 96 97 98 99 100 101 102 103 104 105

        offset = stepper_size * steppers[0:2].count(True)
        shorter = stepper_size * steppers.count(True)
        if steppers[0] or steppers[1]:
            offset += stepper_spacing
            shorter += stepper_spacing
        if steppers[2] or steppers[3]:
            shorter += stepper_spacing
        self._y_offset = offset
        self._h_offset = shorter
        self.queue_draw()

    def on_scrollbar_size_allocate(self, scrollbar, allocation):
106 107 108 109
        if self._last_allocation and self._last_allocation.equal(allocation):
            return

        self._last_allocation = allocation
110
        translation = scrollbar.translate_coordinates(self, 0, 0)
111 112 113
        _scroll_y = translation[1] if translation else 0
        self._y_start = _scroll_y + self._y_offset + 1
        self._height = allocation.height - self._h_offset - 1
114
        self._width = max(allocation.width, 10)
115
        self._cached_map = None
116
        self.queue_resize()
117

118
    def do_draw(self, context):
119 120
        if not self._setup:
            return
121
        height = self._height
122 123
        width = self.get_allocated_width()
        xpad = 2.5
124
        x0 = xpad
125
        x1 = width - 2 * xpad
126

127
        if not (width > 0 and height > 0):
128 129
            return

130
        context.translate(0, self._y_start)
131
        context.set_line_width(1)
132
        context.rectangle(x0 - 3, -1, x1 + 6, height + 1)
133 134
        context.clip()

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        if self._cached_map is None:
            surface = cairo.Surface.create_similar(
                context.get_target(), cairo.CONTENT_COLOR_ALPHA,
                width, height)
            cache_ctx = cairo.Context(surface)
            cache_ctx.set_line_width(1)

            tagged_diffs = collections.defaultdict(list)
            for c, y0, y1 in self._difffunc():
                tagged_diffs[c].append((y0, y1))

            for tag, diffs in tagged_diffs.items():
                cache_ctx.set_source_rgba(*self.fill_colors[tag])
                for y0, y1 in diffs:
                    y0, y1 = round(y0 * height) - 0.5, round(y1 * height) - 0.5
                    cache_ctx.rectangle(x0, y0, x1, y1 - y0)
                cache_ctx.fill_preserve()
                cache_ctx.set_source_rgba(*self.line_colors[tag])
                cache_ctx.stroke()
            self._cached_map = surface

        context.set_source_surface(self._cached_map, 0., 0.)
        context.paint()
158

159 160 161
        page_color = (0., 0., 0., 0.1)
        page_outline_color = (0.0, 0.0, 0.0, 0.3)
        adj = self._scrolladj
162 163
        s = round(height * (adj.get_value() / adj.get_upper())) - 0.5
        e = round(height * (adj.get_page_size() / adj.get_upper()))
164 165 166 167 168 169
        context.set_source_rgba(*page_color)
        context.rectangle(x0 - 2, s, x1 + 4, e)
        context.fill_preserve()
        context.set_source_rgba(*page_outline_color)
        context.stroke()

170 171
    def do_button_press_event(self, event):
        if event.button == 1:
172
            fraction = (event.y - self._y_start) / self._height
173 174

            adj = self._scrolladj
175 176 177
            val = fraction * adj.get_upper() - adj.get_page_size() / 2
            upper = adj.get_upper() - adj.get_page_size()
            adj.set_value(max(min(upper, val), adj.get_lower()))
178 179 180
            return True
        return False

181
    def do_get_preferred_width(self):
182
        return self._width, self._width