This file is indexed.

/usr/lib/python3/dist-packages/ginga/rv/plugins/Pan.py is in python3-ginga 2.6.1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#
# Pan.py -- Pan plugin for fits viewer
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
import sys
import traceback
import math
from ginga.BaseImage import BaseImage
from ginga.gw import Widgets, Viewers
from ginga.misc import Bunch
from ginga import GingaPlugin

class Pan(GingaPlugin.GlobalPlugin):

    def __init__(self, fv):
        # superclass defines some variables for us, like logger
        super(Pan, self).__init__(fv)

        self.active = None
        self.info = None

        fv.add_callback('add-channel', self.add_channel)
        fv.add_callback('delete-channel', self.delete_channel)
        fv.set_callback('channel-change', self.focus_cb)

        self.dc = fv.get_draw_classes()

        prefs = self.fv.get_preferences()
        self.settings = prefs.createCategory('plugin_Pan')
        self.settings.addDefaults(use_shared_canvas=False,
                                  pan_position_color='yellow',
                                  pan_rectangle_color='red',
                                  compass_color='skyblue',
                                  rotate_pan_image=True)
        self.settings.load(onError='silent')
        # share canvas with channel viewer?
        self.use_shared_canvas = self.settings.get('use_shared_canvas', False)

        self._wd = 200
        self._ht = 200

    def build_gui(self, container):
        nb = Widgets.StackWidget()
        self.nb = nb
        container.add_widget(self.nb, stretch=1)

    def _create_pan_image(self, fitsimage):
        pi = Viewers.CanvasView(logger=self.logger)
        pi.enable_autozoom('on')
        pi.enable_autocuts('off')
        hand = pi.get_cursor('pan')
        pi.define_cursor('pick', hand)
        pi.set_bg(0.4, 0.4, 0.4)
        pi.set_desired_size(self._wd, self._ht)
        pi.set_callback('cursor-down', self.btndown)
        pi.set_callback('cursor-move', self.drag_cb)
        pi.set_callback('none-move', self.motion_cb)
        pi.set_callback('zoom-scroll', self.zoom_cb)
        pi.set_callback('configure', self.reconfigure)
        # for debugging
        pi.set_name('panimage')
        #pi.ui_setActive(True)

        my_canvas = pi.get_canvas()
        my_canvas.enable_draw(True)
        my_canvas.set_drawtype('rectangle', linestyle='dash', color='green')
        my_canvas.set_callback('draw-event', self.draw_cb)
        my_canvas.ui_setActive(True)

        if self.use_shared_canvas:
            canvas = fitsimage.get_canvas()
            pi.set_canvas(canvas)

        bd = pi.get_bindings()
        bd.enable_pan(False)
        bd.enable_zoom(False)

        return pi

    def add_channel(self, viewer, channel):
        fitsimage = channel.fitsimage
        panimage = self._create_pan_image(fitsimage)
        chname = channel.name

        iw = Viewers.GingaViewerWidget(panimage)
        iw.resize(self._wd, self._ht)
        self.nb.add_widget(iw)
        index = self.nb.index_of(iw)
        paninfo = Bunch.Bunch(panimage=panimage, widget=iw,
                              pancompass=None, panrect=None)
        channel.extdata._pan_info = paninfo

        # Extract RGBMap object from main image and attach it to this
        # pan image
        rgbmap = fitsimage.get_rgbmap()
        panimage.set_rgbmap(rgbmap)
        rgbmap.add_callback('changed', self.rgbmap_cb, panimage)

        fitsimage.copy_attributes(panimage, ['cutlevels'])

        fitssettings = fitsimage.get_settings()
        pansettings = panimage.get_settings()

        xfrmsettings = ['flip_x', 'flip_y', 'swap_xy']
        if self.settings.get('rotate_pan_image', False):
            xfrmsettings.append('rot_deg')
        fitssettings.shareSettings(pansettings, xfrmsettings)
        for key in xfrmsettings:
            pansettings.getSetting(key).add_callback('set', self.settings_cb,
                                                     fitsimage, channel, paninfo, 0)


        fitssettings.shareSettings(pansettings, ['cuts'])
        pansettings.getSetting('cuts').add_callback('set', self.settings_cb,
                                                    fitsimage, channel, paninfo, 1)

        zoomsettings = ['zoom_algorithm', 'zoom_rate',
                        'scale_x_base', 'scale_y_base']
        fitssettings.shareSettings(pansettings, zoomsettings)
        for key in zoomsettings:
            pansettings.getSetting(key).add_callback('set', self.zoom_ext_cb,
                                                     fitsimage, channel, paninfo)

        fitsimage.add_callback('redraw', self.redraw_cb, channel, paninfo)

        self.logger.debug("channel '%s' added." % (channel.name))

    def delete_channel(self, viewer, channel):
        chname = channel.name
        self.logger.debug("deleting channel %s" % (chname))
        widget = channel.extdata._pan_info.widget
        self.nb.remove(widget, delete=True)
        self.active = None
        self.info = None

    def start(self):
        names = self.fv.get_channel_names()
        for name in names:
            channel = self.fv.get_channel(name)
            self.add_channel(self.fv, channel)

    # CALLBACKS

    def rgbmap_cb(self, rgbmap, panimage):
        # color mapping has changed in some way
        panimage.redraw(whence=1)

    def redo(self, channel, image):
        paninfo = channel.extdata._pan_info

        if (image is None) or not isinstance(image, BaseImage):
            # TODO: clear not yet working
            #paninfo.panimage.clear()
            return

        loval, hival = channel.fitsimage.get_cut_levels()
        paninfo.panimage.cut_levels(loval, hival)

        # add cb to image so that if it is modified we can update info
        ## image.add_callback('modified', self.image_update_cb, fitsimage,
        ##                    channel, paninfo)
        self.set_image(channel, paninfo, image)

    ## def image_update_cb(self, image, fitsimage, channel, paninfo):
    ##     # image has changed (e.g. size, value range, etc)
    ##     cur_img = fitsimage.get_image()
    ##     if cur_img == image:
    ##         self.fv.gui_do(self.set_image, channel, paninfo, image)
    ##     return False

    def focus_cb(self, viewer, channel):
        chname = channel.name

        # If the active widget has changed, then raise our Info widget
        # that corresponds to it
        if self.active != chname:
            if not channel.extdata.has_key('_pan_info'):
                self.add_channel(viewer, channel)
            paninfo = channel.extdata._pan_info
            iw = paninfo.widget
            index = self.nb.index_of(iw)
            self.nb.set_index(index)
            self.active = chname
            self.info = paninfo


    def reconfigure(self, panimage, width, height):
        self.logger.debug("new pan image dimensions are %dx%d" % (
            width, height))
        panimage.zoom_fit()
        panimage.redraw(whence=0)
        return True

    def redraw_cb(self, fitsimage, channel, paninfo):
        #paninfo.panimage.redraw(whence=whence)
        self.panset(channel.fitsimage, channel, paninfo)
        return True

    def settings_cb(self, setting, value, fitsimage, channel, paninfo, whence):
        #paninfo.panimage.redraw(whence=whence)
        self.panset(channel.fitsimage, channel, paninfo)
        return True

    def zoom_ext_cb(self, setting, value, fitsimage, channel, paninfo):
        # refit the pan image, because scale factors may have changed
        paninfo.panimage.zoom_fit()
        # redraw pan info
        self.panset(fitsimage, channel, paninfo)
        return False

    # LOGIC

    def clear(self):
        self.info.panimage.clear()

    def set_image(self, channel, paninfo, image):
        if (image is None) or not isinstance(image, BaseImage):
            # TODO: clear not yet working
            #paninfo.panimage.clear()
            return

        if not self.use_shared_canvas:
            paninfo.panimage.set_image(image)
        else:
            paninfo.panimage.zoom_fit()

        p_canvas = paninfo.panimage.get_private_canvas()
        # remove old compass
        try:
            p_canvas.delete_object_by_tag(paninfo.pancompass)
        except Exception:
            pass

        # create compass
        if image.has_valid_wcs() and hasattr(image, 'calc_compass_radius'):
            try:
                width, height = image.get_size()
                x, y = width / 2.0, height / 2.0
                # radius we want the arms to be (approx 1/4 the largest dimension)
                radius = float(max(width, height)) / 4.0

                # HACK: force a wcs error here if one is going to happen
                image.add_offset_xy(x, y, 1.0, 1.0)

                paninfo.pancompass = p_canvas.add(self.dc.Compass(
                    x, y, radius, color=self.settings.get('compass_color', 'skyblue'),
                    fontsize=14))

            except Exception as e:
                self.logger.warning("Can't calculate compass: %s" % (
                    str(e)))
                try:
                    # log traceback, if possible
                    (type_, value_, tb) = sys.exc_info()
                    tb_str = "".join(traceback.format_tb(tb))
                    self.logger.error("Traceback:\n%s" % (tb_str))
                except Exception:
                    tb_str = "Traceback information unavailable."
                    self.logger.error(tb_str)

        self.panset(channel.fitsimage, channel, paninfo)

    def panset(self, fitsimage, channel, paninfo):
        image = fitsimage.get_image()
        if (image is None) or not isinstance(image, BaseImage):
            # TODO: clear not yet working
            #paninfo.panimage.clear()
            return

        x, y = fitsimage.get_pan()
        points = fitsimage.get_pan_rect()

        # calculate pan position point radius
        p_image = paninfo.panimage.get_image()
        try:
            obj = paninfo.panimage.canvas.get_object_by_tag('__image')
        except KeyError:
            obj = None
        #print(('panset', image, p_image, obj, obj.image, paninfo.panimage._imgobj))

        width, height = image.get_size()
        edgew = math.sqrt(width**2 + height**2)
        radius = int(0.015 * edgew)

        # Mark pan rectangle and pan position
        #p_canvas = paninfo.panimage.get_canvas()
        p_canvas = paninfo.panimage.get_private_canvas()
        try:
            obj = p_canvas.get_object_by_tag(paninfo.panrect)
            if obj.kind != 'compound':
                return False
            point, bbox = obj.objects
            self.logger.debug("starting panset")
            point.x, point.y = x, y
            point.radius = radius
            bbox.points = points
            p_canvas.update_canvas(whence=0)

        except KeyError:
            paninfo.panrect = p_canvas.add(self.dc.CompoundObject(
                self.dc.Point(x, y, radius=radius, style='plus',
                              color=self.settings.get('pan_position_color', 'yellow')),
                self.dc.Polygon(points,
                                color=self.settings.get('pan_rectangle_color', 'red'))))

        #p_canvas.update_canvas(whence=0)
        paninfo.panimage.zoom_fit()
        return True

    def motion_cb(self, fitsimage, event, data_x, data_y):
        chviewer = self.fv.getfocus_viewer()
        self.fv.showxy(chviewer, data_x, data_y)
        return True

    def drag_cb(self, fitsimage, event, data_x, data_y):
        # this is a panning move in the small
        # window for the big window
        chviewer = self.fv.getfocus_viewer()
        chviewer.panset_xy(data_x, data_y)
        return True

    def btndown(self, fitsimage, event, data_x, data_y):
        chviewer = self.fv.getfocus_viewer()
        chviewer.panset_xy(data_x, data_y)
        return True

    def zoom_cb(self, fitsimage, event):
        """Zoom event in the small fits window.  Just zoom the large fits
        window.
        """
        chviewer = self.fv.getfocus_viewer()
        bd = chviewer.get_bindings()

        if hasattr(bd, 'sc_zoom'):
            return bd.sc_zoom(chviewer, event)

        return False

    def draw_cb(self, canvas, tag):
        # Get and delete the drawn object
        obj = canvas.get_object_by_tag(tag)
        canvas.delete_object_by_tag(tag)

        # determine center of drawn rectangle and set pan position
        if obj.kind != 'rectangle':
            return False
        xc = (obj.x1 + obj.x2) / 2.0
        yc = (obj.y1 + obj.y2) / 2.0
        chviewer = self.fv.getfocus_viewer()
        # note: chviewer <-- referring to channel viewer
        with chviewer.suppress_redraw:
            chviewer.panset_xy(xc, yc)

            # Determine appropriate zoom level to fit this rect
            wd = obj.x2 - obj.x1
            ht = obj.y2 - obj.y1
            wwidth, wheight = chviewer.get_window_size()
            wd_scale = float(wwidth) / float(wd)
            ht_scale = float(wheight) / float(ht)
            scale = min(wd_scale, ht_scale)
            self.logger.debug("wd_scale=%f ht_scale=%f scale=%f" % (
                wd_scale, ht_scale, scale))
            if scale < 1.0:
                zoomlevel = - max(2, int(math.ceil(1.0/scale)))
            else:
                zoomlevel = max(1, int(math.floor(scale)))
            self.logger.debug("zoomlevel=%d" % (zoomlevel))

            chviewer.zoom_to(zoomlevel)

        return True


    def __str__(self):
        return 'pan'

#END