summaryrefslogtreecommitdiff
path: root/plugins/python/cglimpse/panel.py
blob: 2245e3ae706be3bcafd22b6b639fd0a260caa94e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323

import os
from gi.repository import Gtk
from pychrysalide import core
from pychrysalide.gtkext import BuiltNamedWidget
from pychrysalide.gtkext import EasyGtk
from pychrysalide.gui import PanelItem
from pychrysalide.gui.panels import UpdatablePanel


from .distro import ByteDistribution
from .entropy import ShannonEntropy


class CGlimpsePanel(PanelItem, UpdatablePanel):

    _key = 'cglimpse'

    _path = 'Ms'

    _working_group_id = core.setup_tiny_global_work_group()


    def __init__(self):
        """Initialize the GUI panel."""

        directory = os.path.dirname(os.path.realpath(__file__))
        filename = os.path.join(directory, 'panel.ui')

        widget = BuiltNamedWidget('Content glimpse', 'Binary content glimpse', filename)

        super(CGlimpsePanel, self).__init__(widget)

        self._builder = self.named_widget.builder

        self._builder.connect_signals(self)

        self._content = None

        combo = self._builder.get_object('method_sel')
        self._on_method_changed(combo)


    def _change_content(self, old, new):
        """Get notified about a LoadedContent change."""

        self._content = new.content if new else None

        combo = self._builder.get_object('method_sel')
        self._on_method_changed(combo)


    def _setup(self, uid):
        """Prepare an update process for a panel."""

        assert(uid == 0)

        return ( 1, {}, 'Computing data for content glimpse' )


    def _introduce(self, uid, data):
        """Introduce the update process and switch display."""

        assert(uid == 0)

        self.switch_to_updating_mask()


    def _process(self, uid, status, id, data):
        """Perform the computing of data to render."""

        assert(uid == 0)

        self._current.update(self._content.data)

        area = self._builder.get_object('content')
        area.queue_draw()


    def _conclude(self, uid, data):
        """Conclude the update process and display the computed data."""

        assert(uid == 0)

        self.switch_to_updated_content()


    def _clean_data(self, uid, data):
        """Delete dynamically generated objects for the panel update."""

        # Not really useful here...

        assert(uid == 0)


    def _on_method_changed(self, combo):
        """React on method selection change."""

        tree_iter = combo.get_active_iter()

        if tree_iter:

            model = combo.get_model()
            key = model[tree_iter][1]

            if key == 'shanon':
                self._current = ShannonEntropy(self._builder)

            else:
                assert(key == 'distrib')
                self._current = ByteDistribution(self._builder)

            self._current.switch()

            if self._content:

                self.run_update(0)


    def _on_options_toggled(self, button):
        """React on options display/hide order."""

        lbl = self._builder.get_object('options_label')

        common = self._builder.get_object('common_options')

        if button.get_active():

            button.get_parent().child_set_property(button, 'expand', False)
            lbl.set_angle(0)

            common.show()

        else:

            button.get_parent().child_set_property(button, 'expand', True)
            lbl.set_angle(90)

            common.hide()


    def _render_grid(self, widget, cr):
        """Draw a basic empty grid."""

        # Colors

        color = EasyGtk.get_color_from_style('view', True)
        bg_color = [ color.red, color.green, color.blue, color.alpha ]

        color = EasyGtk.get_color_from_style('background', False)
        line_color = [ color.red, color.green, color.blue, color.alpha ]

        # Background

        w = widget.get_allocation().width
        h = widget.get_allocation().height

        cr.set_source_rgba(*bg_color)

        cr.rectangle(0, 0, w, h)
        cr.fill()

        # Area definitions

        x_range, y_range = self._current.setup_rendering()

        margin_left = 0
        margin_bottom = 0

        y_count = int((y_range[2] - y_range[0]) / y_range[1])

        for i in range(y_count + 1):

            text = self._current.format_legend(y_range[0] + i * y_range[1], True)

            (_, _, width, height, _, _) = cr.text_extents(text)

            if width > margin_left:

                margin_left = width
                margin_bottom = height

        bar_padding = 5
        bar_tick = 3
        arrow_size = 4

        graph_left = bar_padding + margin_left + bar_tick * 3
        graph_right = w - 2 * bar_padding

        graph_bottom = h - bar_padding - margin_bottom - bar_tick * 3

        data_left = graph_left + 2 * bar_padding
        data_right = graph_right - 2 * bar_padding

        data_top = 5 * bar_padding
        data_bottom = graph_bottom - 2 * bar_padding

        data_height = data_bottom - data_top
        data_width = data_right - data_left

        data_area = [ data_left, data_top, data_width, data_height ]

        # Grid content #1

        cr.set_source_rgba(*line_color)

        cr.set_line_width(1)

        cr.move_to(graph_left, 2 * bar_padding)
        cr.line_to(graph_left, graph_bottom)
        cr.line_to(graph_right, graph_bottom)

        cr.stroke()

        cr.move_to(graph_right, graph_bottom)
        cr.line_to(graph_right - arrow_size, graph_bottom - arrow_size)
        cr.line_to(graph_right - arrow_size, graph_bottom + arrow_size)

        cr.fill()

        cr.move_to(graph_left, 2 * bar_padding)
        cr.line_to(graph_left - arrow_size, 2 * bar_padding + arrow_size)
        cr.line_to(graph_left + arrow_size, 2 * bar_padding + arrow_size)

        cr.fill()

        cr.set_source_rgba(0, 0, 0, 0.2)

        cr.rectangle(*data_area)
        cr.fill()

        # Grid content #2

        y_count = int((y_range[2] - y_range[0]) / y_range[1])

        for i in range(y_count + 1):

            y = data_bottom - (i * data_height) / y_count

            # Line

            cr.save()

            cr.set_source_rgba(*line_color[:3], line_color[3] * 0.4)
            cr.set_dash([ 2 * bar_tick, 6 * bar_tick ])

            cr.move_to(graph_left + 6 * bar_tick, y)
            cr.line_to(data_right, y)

            cr.stroke()

            cr.restore()

            # Tick

            cr.set_source_rgba(*line_color)

            cr.move_to(graph_left - bar_tick, y)
            cr.line_to(graph_left + bar_tick, y)

            cr.stroke()

            # Text

            text = self._current.format_legend(y_range[0] + i * y_range[1], True)

            _, _, tw, th, _, _ = cr.text_extents(text)

            x = graph_left - 3 * bar_tick - tw

            cr.move_to(x, y + th / 2)
            cr.show_text(text)

        x_count = int((x_range[2] - x_range[0]) / x_range[1])

        for i in range(x_count + 1):

            x = data_left + (i * data_width) / x_count

            # Line

            cr.save()

            cr.set_source_rgba(*line_color[:3], line_color[3] * 0.4)
            cr.set_dash([ 2 * bar_tick, 6 * bar_tick ])

            cr.move_to(x, data_top)
            cr.line_to(x, data_bottom)

            cr.stroke()

            cr.restore()

            # Tick

            cr.set_source_rgba(*line_color)

            cr.move_to(x, graph_bottom - bar_tick)
            cr.line_to(x, graph_bottom + bar_tick)

            cr.stroke()

            # Text

            text = self._current.format_legend(x_range[0] + i * x_range[1], False)

            _, _, tw, th, _, _ = cr.text_extents(text)

            y = graph_bottom + 3 * bar_tick + th

            cr.move_to(x - tw / 2, y)
            cr.show_text(text)

        return data_area


    def _render_content_glimpse(self, widget, cr):
        """Draw the selected content view."""

        data_area = self._render_grid(widget, cr)

        if self._content:
            self._current.render(cr, data_area)