ImageRois.py 27.5 KB
Newer Older
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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2004-2016 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""Roi items."""

from collections import OrderedDict, namedtuple

import numpy as np
from silx.gui import qt, icons

__author__ = ["D. Naudet"]
__license__ = "MIT"
__date__ = "01/09/2016"


_registeredRoiItems = OrderedDict()


def RoiItemClassDef(roiName, shape,
                    actionToolTip=None,
                    actionIcon=None,
                    actionText=None):
    def inner(cls):
        cls.roiName = roiName
        cls.shape = shape
        cls.actionToolTip = actionToolTip
        cls.actionIcon = actionIcon
        cls.actionText = actionText
        registerRoiItem(cls)
        return cls

    return inner


_RoiData = namedtuple('RoiData', ['x', 'y', 'shape'])
""" Named tuple used to return a ROI's x and y data """


def registerRoiItem(klass):
    global _registeredRoiItems

    roiName = klass.roiName
    if roiName is None:
        raise AttributeError('Failed to register Roi class {0} roiName '
                             'attribute is None.'.format(klass.__name__))

    # TODO : some kind of checks
    if roiName in _registeredRoiItems:
        raise ValueError('Cannot register roi class "{0}" :'
                         ' a ROI with the same roiName already exists.'
                         ''.format(roiName))

    # TODO : some kind of checks on the klass
    _registeredRoiItems[roiName] = klass


class RoiItemBase(qt.QObject):
    sigRoiDrawingStarted = qt.Signal(str)
    sigRoiDrawingFinished = qt.Signal(object)
    sigRoiDrawingCanceled = qt.Signal(str)
    sigRoiMoved = qt.Signal(object)

    roiName = None
    shape = None
    actionIcon = None
    actionText = None
    actionToolTip = None

    plot = property(lambda self: self._plot)
    name = property(lambda self: self._name)

    def __init__(self, plot, parent, name=None):
        super(RoiItemBase, self).__init__(parent=parent)

        self._manager = parent
        self._plot = plot
        self._handles = []
        self._items = []
        self._points = {}
        self._kwargs = []

        self._finished = False
        self._startNotified = False
        self._connected = False
        self._editing = False
        self._visible = True

        self._xData = []
        self._yData = []

        if not name:
            uuid = str(id(self))
            name = '{0}_{1}'.format(self.__class__.__name__, uuid)

        self._name = name

    def setVisible(self, visible):
        changed = self._visible == visible
        self._visible = visible
        if not visible:
            self._disconnect()
            self._remove()
        else:
            self._connect()
            self._draw(drawHandles=self._editing)

        if changed:
            self._visibilityChanged(visible)

    def _remove(self, handles=True, shape=True):
        if handles:
Damien Naudet's avatar
Damien Naudet committed
133
134
            for item in self._handles:
                self._plot.removeMarker(item)
135
136
        if shape:
            self._plot.removeItem(self._name)
Damien Naudet's avatar
Damien Naudet committed
137
138
            for item in self._items:
                self._plot.removeItem(item)
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

    def _interactiveModeChanged(self, source):
        if source is not self or source is not self.parent():
            self.stop()

    def _plotSignal(self, event):
        evType = event['event']
        if (evType == 'drawingFinished' and
                event['parameters']['label'] == self._name):
            self._finish(event)
        elif (evType == 'drawingProgress' and
                event['parameters']['label'] == self._name):
            if not self._startNotified:
                self._drawStarted()
                # TODO : this is a temporary workaround
                # until we can get a mouse click event
                self.sigRoiDrawingStarted.emit(self.name)
                self._startNotified = True
            self._drawEvent(event)
            self._emitDataEvent(self.sigRoiMoved)
        elif evType == 'markerMoving':
            label = event['label']
            try:
                idx = self._handles.index(label)
            except ValueError:
                idx = None
            else:
                x = event['x']
                y = event['y']
                self._setHandleData(label, (x, y))
                self._handleMoved(label, x, y, idx)
                self._emitDataEvent(self.sigRoiMoved)
                self._draw()

    def _registerHandle(self, handle, point, idx=-1):
        if handle in self._handles:
            return
        if idx is not None and idx >= 0 and idx < len(self._handles):
            self._handles.insert(handle, idx)
        else:
            self._handles.append(handle)
            idx = len(self._handles)
        self._points[handle] = point
        return idx

    def _unregisterHandle(self, label):
        try:
            self._handles.remove(label)
        except ValueError:
            pass

    def _registerItem(self, legend):
        if legend in self._items:
            raise ValueError('Item {0} is already registered.'
                             ''.format(legend))
        self._items.append(legend)

    def _unregisterItem(self, legend):
        try:
            self._items.remove(legend)
        except ValueError:
            pass

    def _connect(self):
        if self._connected:
            return

        self._plot.sigPlotSignal.connect(self._plotSignal)
        self._plot.sigInteractiveModeChanged.connect(
            self._interactiveModeChanged)
        self._connected = True

    def _disconnect(self):
        if not self._connected:
            return

        self._plot.sigPlotSignal.disconnect(self._plotSignal)
        self._plot.sigInteractiveModeChanged.disconnect(
            self._interactiveModeChanged)
        self._connected = False

    def _draw(self, drawHandles=True, excludes=()):
        if drawHandles:
            if excludes is not None and len(excludes) > 0:
                draw_legends = set(self._handles) - set(excludes)
            else:
                draw_legends = self._handles
            self._drawHandles(draw_legends)

        self._drawShape()

    def _drawHandles(self, handles):
        for i_handle, handle in enumerate(handles):
            item = self._plot.addMarker(self._points[handle][0],
                                        self._points[handle][1],
                                        legend=handle,
                                        draggable=True,
DN's avatar
DN committed
236
237
                                        symbol='x',
                                        color='pink')
238
239
240
241
242
243
244
            assert item == handle

    def _drawShape(self):
        item = self._plot.addItem(self.xData,
                                  self.yData,
                                  shape=self.shape,
                                  legend=self._name,
DN's avatar
DN committed
245
246
                                  overlay=True,
                                  color='pink')
247
248
249
250
251
252
253
254
255
256
257
258
259
        assert item == self._name

    def _setHandleData(self, name, point):
        self._points[name] = point

    def start(self):
        self.edit(False)
        self._finished = False
        self._startNotified = False
        self._visible = True
        self._plot.setInteractiveMode('draw',
                                      shape=self.shape,
                                      source=self,
DN's avatar
DN committed
260
261
                                      label=self._name,
                                      color='pink')
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
        self._connect()

    def edit(self, enable):
        if not self._finished:
            return

        if self._editing == enable:
            return

        if enable:
            self._connect()
            self._editStarted()
            self._draw()
        else:
            self._disconnect()
            self._remove(shape=False)
            self._editStopped()
            self._draw(drawHandles=False)

        self._editing = enable

    def _finish(self, event):
        self._drawFinished(event)
        self._draw(drawHandles=False)
        self._finished = True
        self._emitDataEvent(self.sigRoiDrawingFinished)
        self._disconnect()

    def _emitDataEvent(self, signal):
        signal.emit({'name': self._name,
                     'shape': self.shape,
                     'xdata': self._xData,
                     'ydata': self._yData})

    def stop(self):
        """
        Stops whatever state the ROI is in.
        draw state : cancel the drawning, emit sigRoiDrawningCanceled
        edit state : ends the editing, emit sigRoiEditingFinished
        """
        if not self._finished:
            self._disconnect()
            self._drawCanceled()
            if self._startNotified:
                self.sigRoiDrawingCanceled.emit(self.name)
            return
        if self._editing:
            self.edit(False)

    xData = property(lambda self: self._xData[:])
    yData = property(lambda self: self._yData[:])

    def _drawEvent(self, event):
        """
        This method updates the _xData and _yData members with data found
        in the event object. The default implementation just uses the
        xdata and ydata fields from the event dictionary.
        This method should be overridden if necessary.
        """
        self._xData = event['xdata'].reshape(-1)
        self._yData = event['ydata'].reshape(-1)

    def _handleMoved(self, label, x, y, idx):
        """
        Called when one of the registered handle has moved.
        To be overridden if necessary
        """
        pass

    def _drawStarted(self):
        """
        To be overridden if necessary
        """
        pass

    def _drawFinished(self, event):
        """
        To be overridden if necessary
        """
        pass

    def _drawCanceled(self):
        """
        To be overridden if necessary
        """
        pass

    def _editStarted(self):
        """
        To be overridden if necessary
        """
        pass

    def _editStopped(self):
        """
        To be overridden if necessary
        """
        pass

    def _visibilityChanged(self, visible):
        """
        To be overridden if necessary
        """
        pass


class ImageRoiManager(qt.QObject):
    """
    Developpers doc : to add a new ROI simply append the necessary values to
    these three members
    """

    sigRoiDrawingStarted = qt.Signal(str)
    sigRoiDrawingFinished = qt.Signal(object)
    sigRoiDrawingCanceled = qt.Signal(str)
    sigRoiMoved = qt.Signal(object)
    sigRoiRemoved = qt.Signal(str)

    def __init__(self, plot, parent=None):
        super(ImageRoiManager, self).__init__(parent=parent)

        self._plot = plot

        self._klassInfos = _registeredRoiItems

        self._multipleSelection = False
        self._roiVisible = True
        self._roiInProgress = None

        self._roiActions = None
        self._optionActions = None
        self._roiActionGroup = None

        self._currentKlass = None

        self._rois = {}

        self._plot.sigInteractiveModeChanged.connect(
            self._interactiveModeChanged, qt.Qt.QueuedConnection)

Damien Naudet's avatar
Damien Naudet committed
402
403
404
405
406
407
408
    def plot(self):
        """
        Returns the plot controlled by this manager.
        :return:
        """
        return self._plot

409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
    def _createRoiActions(self):

        if self._roiActions:
            return self._roiActions

        # roi shapes
        self._roiActionGroup = roiActionGroup = qt.QActionGroup(self)

        self._roiActions = roiActions = OrderedDict()

        for name, klass in self._klassInfos.items():

            try:
                qIcon = icons.getQIcon(klass.actionIcon)
            except:
                qIcon = qt.QIcon()

            text = klass.actionText
            if text is None:
                text = klass.roiName

            action = qt.QAction(qIcon, text, None)
            action.setCheckable(True)
            toolTip = klass.actionToolTip
            if toolTip is not None:
                action.setToolTip(toolTip)
            roiActions[name] = action
            roiActionGroup.addAction(action)

            if klass.roiName == self._currentKlass:
                action.setChecked(True)
            else:
                action.setChecked(False)

        roiActionGroup.triggered.connect(self._roiActionTriggered,
                                         qt.Qt.QueuedConnection)

        return roiActions

    def _createOptionActions(self):

        if self._optionActions:
            return self._optionActions

        # options
        self._optionActions = optionActions = OrderedDict()

        # temporary Unicode icons until I have time to draw some icons.
        action = qt.QAction(u'\u2200', None)
        action.setCheckable(False)
        action.setToolTip('Select all [WIP]')
        action.triggered.connect(self._selectAll)
        action.setEnabled(False)
        optionActions['selectAll'] = action

        # temporary Unicode icons until I have time to draw some icons.
        action = qt.QAction(u'\u2717', None)
        action.setCheckable(False)
        action.setToolTip('Clear all ROIs')
        action.triggered.connect(self._clearRois)
        optionActions['clearAll'] = action

        action = qt.QAction(u'\u2685', None)
        action.setCheckable(True)
        action.setChecked(self._multipleSelection)
        action.setToolTip('Single/Multiple ROI selection')
        action.setText(u'\u2682' if self._multipleSelection else u'\u2680')
        action.triggered.connect(self.allowMultipleSelections)
        optionActions['multiple'] = action

        action = qt.QAction(u'\U0001F440', None)
        action.setCheckable(True)
        action.setChecked(self._roiVisible)
        action.setToolTip('Show/Hide ROI(s)')
        action.triggered.connect(self.showRois)
        optionActions['show'] = action

        return optionActions

    def _selectAll(self, checked):
Damien Naudet's avatar
Damien Naudet committed
489
        pass
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518

    def _clearRois(self, checked):
        self.clear()

    def clear(self, name=None):
        if name is None:
            for roi in self._rois.values():
                roi.stop()
                roi.setVisible(False)
                try:
                    roi.sigRoiMoved.disconnect(self._roiMoved)
                except:
                    pass
                self.sigRoiRemoved.emit(roi.name)
            self._rois = {}
        else:
            try:
                roi = self._rois.pop(name)
                roi.stop()
                roi.setVisible(False)
                try:
                    roi.sigRoiMoved.disconnect(self._roiMoved)
                except:
                    pass
                self.sigRoiRemoved.emit(roi.name)
            except KeyError:
                # TODO : to raise or not to raise?
                pass

Damien Naudet's avatar
Damien Naudet committed
519
    rois = property(lambda self: list(self._rois.keys()))
520
521
522
523
524
525
526
527
528
529
530
531
532
533

    def showRois(self, show):
        # TODO : name param to tell that we only want to toggle
        # one specific ROI
        # TODO : exclusive param to tell that we want to show only
        # one given roi (w/ name param)
        self._roiVisible = show

        if self._optionActions:
            action = self._optionActions['show']
            action.setText(u'\U0001F440' if show else u'\u2012')
            if self.sender() != action:
                action.setChecked(show)

Damien Naudet's avatar
Damien Naudet committed
534
535
        for roi in self._rois.values():
            roi.setVisible(show)
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556

    def allowMultipleSelections(self, allow):

        self._multipleSelection = allow
        if self._optionActions:
            action = self._optionActions['multiple']
            action.setText(u'\u2682' if allow else u'\u2680')
            if self.sender() != action:
                action.setChecked(allow)

    def _roiActionTriggered(self, action):

        if not action.isChecked():
            return

        name = [k for k, v in self._roiActions.items() if v == action][0]
        self._currentKlass = name
        self._startRoi()

    def _editRois(self):
        # TODO : should we call stop first?
Damien Naudet's avatar
Damien Naudet committed
557
558
        for item in self._rois.values():
            item.edit(True)
559
560
561
562
563
564
565
566
567
568

    def _startRoi(self):
        """
        Initialize a new roi, ready to be drawn.
        """
        if self._currentKlass not in self._klassInfos.keys():
            return

        self._stopRoi()

Damien Naudet's avatar
Damien Naudet committed
569
570
        for item in self._rois.values():
            item.edit(False)
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
        self.showRois(True)

        klass = self._klassInfos[self._currentKlass]
        item = klass(self._plot, self)

        self._roiInProgress = item

        item.sigRoiDrawingFinished.connect(self._roiDrawingFinished,
                                           qt.Qt.QueuedConnection)
        item.sigRoiDrawingStarted.connect(self._roiDrawingStarted,
                                          qt.Qt.QueuedConnection)
        item.sigRoiDrawingCanceled.connect(self._roiDrawingCanceled,
                                           qt.Qt.QueuedConnection)
        item.sigRoiMoved.connect(self.sigRoiMoved,
                                 qt.Qt.QueuedConnection)
        item.start()

    def _stopRoi(self):
        """
        Stops the roi that was ready to be drawn, if any.
        """
        if self._roiInProgress is None:
            return
        self._roiInProgress.stop()
        self._roiInProgress = None

    def _roiDrawingStarted(self, name):
        if not self._multipleSelection:
            self.clear()
        self.sigRoiDrawingStarted.emit(name)

    def _roiDrawingFinished(self, event):
        # TODO : check if the sender is the same as the roiInProgress
        item = self._roiInProgress
        assert item.name == event['name']

        self._roiInProgress = None

        item.sigRoiDrawingFinished.disconnect(self._roiDrawingFinished)
        item.sigRoiDrawingStarted.disconnect(self._roiDrawingStarted)
        item.sigRoiDrawingCanceled.disconnect(self._roiDrawingCanceled)

        self._rois[item.name] = item

        self._startRoi()

        self.sigRoiDrawingFinished.emit(event)

    def _roiDrawingCanceled(self, name):
        self.sigRoiDrawingCanceled.emit(name)

    def _interactiveModeChanged(self, source):
        """Handle plot interactive mode changed:
        If changed from elsewhere, disable tool.
        """
        if source in self._rois.values() or source == self._roiInProgress:
            pass
        elif source is not self:
            if self._roiActions:
                if self._currentKlass:
                    self._roiActions[self._currentKlass].setChecked(False)

                # specific code needed to update the zoom action
                mode = self._plot.getInteractiveMode()['mode']
                if mode == 'zoom':
                    self._roiActions['zoom'].setChecked(True)

            self._currentKlass = None
            self._stopRoi()

    roiActions = property(lambda self: self._createRoiActions().copy())

    optionActions = property(lambda self: self._createOptionActions())

    def toolBar(self,
                options=None,
                rois=None):
        """
        shapes : list
        options : list
        """
        roiActions = self._createRoiActions()
        if rois is not None:
            # this wont work if shape is a string and not an array
            diff = set(rois) - set(roiActions.keys())
            if len(diff) > 0:
                raise ValueError('Unknown roi(s) {0}.'.format(diff))
        else:
            rois = roiActions.keys()

661
662
663
664
665
666
667
668
669
670
671
        try:
            rois.pop('zoom')
        except:
            pass
        try:
            rois.pop('edit')
        except:
            pass

        rois = ['zoom', 'edit'] + rois

672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
        optionActions = self._createOptionActions()
        # TODO : find a better way to ensure that the order of
        # actions returned is always the same
        optionNames = sorted(optionActions.keys())
        if options is not None:
            # this wont work if shape is a string and not an array
            diff = set(options) - set(optionNames)
            if len(diff) > 0:
                raise ValueError('Unknown options(s) {0}.'.format(diff))
            options = [option for option in optionNames
                       if option in options]
        else:
            options = optionNames

        keepRoiActions = [roiActions[roi] for roi in rois]
        keepOptionActions = [optionActions[option] for option in options]

        toolBar = qt.QToolBar('Roi')
DN's avatar
DN committed
690
        # toolBar.addWidget(qt.QLabel('Roi'))
Damien Naudet's avatar
Damien Naudet committed
691
692
        for action in keepRoiActions:
            toolBar.addAction(action)
693
694
695

        toolBar.addSeparator()

Damien Naudet's avatar
Damien Naudet committed
696
697
        for action in keepOptionActions:
            toolBar.addAction(action)
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805

        return toolBar

    def roiData(self, name):
        item = self.roiItem(name)
        return _RoiData(x=item.xData, y=item.yData, shape=item.shape)

    def roiItem(self, name):
        if self._roiInProgress and self._roiInProgress.name == name:
            return self._roiInProgress
        else:
            try:
                return self._rois[name]
            except KeyError:
                raise ValueError('Unknown roi {0}.'.format(name))


# WARNING : if you change the name of this particular item
#  make sure to change it in ImageRoiManager::_interactiveModeChanged
#  as well, otherwise its action button will not be properly set
# if the zoom mode is set elsewhere.
@RoiItemClassDef('zoom', None,
                 actionIcon='normal',
                 actionToolTip='Zoom.')
class _DummyZoomRoiItem(RoiItemBase):
    def start(self):
        self._plot.setInteractiveMode('zoom', source=self)

    def stop(self):
        pass

    def edit(self):
        pass


@RoiItemClassDef('edit', None,
                 actionIcon='crosshair',
                 actionToolTip='Edit ROI(s)')
class _DummyEditRoiItem(RoiItemBase):
    def start(self):
        self._plot.setInteractiveMode('zoom', source=self)
        self._manager._editRois()

    def stop(self):
        pass

    def edit(self):
        pass


@RoiItemClassDef('rectangle', shape='rectangle',
                 actionIcon='shape-rectangle',
                 actionToolTip='Draw a rectangle ROI.')
class RectRoiItem(RoiItemBase):

    pos = property(lambda self: (self._left, self._bottom))
    """
    Coordinates of the lower left point.
    """

    width = property(lambda self: self._right - self._left)
    """
    Roi width.
    """

    height = property(lambda self: self._top - self._bottom)
    """
    Roi height.
    """

    center = property(lambda self:
                      (self._left + (self._right - self._left) / 2.,
                       self._bottom + (self._top - self._bottom) / 2.))
    """
    Center point of the rectangle.
    """

    def _drawFinished(self, event):
        self._drawEvent(event)
        self._updateSides()

        # initial coordinates of the rect, in that order :
        # bottom left, top left, top right, bottom right
        # which means the edges will always be (and the code assumes this) :
        #   vertical edges : [0, 1] and [2, 3]
        #   horizontal edges : [0, 3] and [1, 2]
        uuid = str(id(self))
        corners = ['C{0}_{1}'.format(idx, uuid) for idx in range(4)]
        xcoords = np.array([self._left, self._left, self._right, self._right])
        ycoords = np.array([self._bottom, self._top, self._top, self._bottom])
        opposites = [[3, 1], [2, 0], [1, 3], [0, 2]]
        rubber = 'RUBBER_{0}'.format(uuid)
        center = 'CENTER_{0}'.format(uuid)

        self._corners = corners
        self._rubber = rubber
        self._xData = xcoords
        self._yData = ycoords
        self._opposites = opposites
        self._center = center

        # note that the order of the handles is preserved
        # (this is the index sent to the handleMoved method)
        # since we re registering the corners first,
        # we will be able to use the index given to the handleMoved
        # function to get data in the _xData, _yData, ... arrays.
        # this only works because we re not adding or removing vertices
        # when editing
Damien Naudet's avatar
Damien Naudet committed
806
807
        for i, corner in enumerate(corners):
            self._registerHandle(corner, (xcoords[i], ycoords[i]))
808
809
810
811
812
813
814
815
        self._registerHandle(center, self.center)

    def _handleMoved(self, name, x, y, index):
        if name == self._center:
            # center moved
            c_x, c_y = self.center
            self._xData += x - c_x
            self._yData += y - c_y
Damien Naudet's avatar
Damien Naudet committed
816
817
            for i, corner in enumerate(self._corners):
                self._setHandleData(corner, (self._xData[i], self._yData[i]))
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
        else:
            # see the comment about the index value
            # (in the finished method)
            h_op, v_op = self._opposites[index]

            v_op_x = self._xData[v_op]
            h_op_y = self._xData[h_op]

            newLeft = min(x, v_op_x)
            newRight = max(x, v_op_x)
            newBottom = min(y, h_op_y)
            newTop = max(y, h_op_y)

            if newLeft != v_op_x:
                self._xData[v_op] = newLeft
            if newRight != v_op_x:
                self._xData[v_op] = newRight
            if newBottom != h_op_y:
                self._yData[h_op] = newBottom
            if newTop != h_op_y:
                self._yData[h_op] = newTop

            self._xData[index] = x
            self._yData[index] = y

            self._setHandleData(self._center, self.center)
Damien Naudet's avatar
Damien Naudet committed
844
845
846
            for i in (v_op, h_op):
                self._setHandleData(self._corners[i],
                                    (self._xData[i], self._yData[i]))
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888

        self._updateSides()

    def _updateSides(self):
        # caching positions
        self._left = min(self._xData[1:3])
        self._right = max(self._xData[1:3])
        self._bottom = min(self._yData[0:2])
        self._top = max(self._yData[0:2])

    def _drawEvent(self, event):
        left = event['x']
        bottom = event['y']
        right = left + event['width']
        top = bottom + event['height']
        self._xData = np.array([left, left, right, right])
        self._yData = np.array([bottom, top, top, bottom])
        self._left, self._bottom, self._right, self._top = \
            left, bottom, right, top


@RoiItemClassDef('polygon', shape='polygon',
                 actionIcon='shape-polygon',
                 actionToolTip='Draw a polygon ROI.')
class PolygonRoiItem(RoiItemBase):

    def _drawFinished(self, event):
        self._xData = event['xdata'].reshape(-1)
        self._yData = event['ydata'].reshape(-1)
        points = event['points']
        uuid = str(id(self))

        # len(points) - 1 because the first and last points are the same!
        vertices = ['V{0}_{1}'.format(idx, uuid)
                    for idx in range(len(points) - 1)]
        map(self._registerHandle, vertices, points[:-1])

    def _handleMoved(self, label, x, y, idx):
        self._xData[idx] = x
        self._yData[idx] = y
        self._xData[-1] = self._xData[0]
        self._yData[-1] = self._yData[0]