summaryrefslogtreecommitdiff
path: root/silx/gui/plot/test/testPlotInteraction.py
blob: 25f57a9f19087cbb1329f5b9d053e0e2e28286f0 (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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 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.
#
# ###########################################################################*/
"""Tests of plot interaction, through a PlotWidget"""

__authors__ = ["T. Vincent"]
__license__ = "MIT"
__date__ = "13/10/2016"


import unittest
from silx.gui import qt
from silx.gui.plot.test.testPlotWidget import _PlotWidgetTest


class _SignalDump(object):
    """Callable object that store passed arguments in a list"""

    def __init__(self):
        self._received = []

    def __call__(self, *args):
        self._received.append(args)

    @property
    def received(self):
        """Return a shallow copy of the list of received arguments"""
        return list(self._received)


class TestSelectPolygon(_PlotWidgetTest):
    """Test polygon selection interaction"""

    def _interactionModeChanged(self, source):
        """Check that source received in event is the correct one"""
        self.assertEqual(source, self)

    def _draw(self, polygon):
        """Draw a polygon in the plot

        :param polygon: List of points (x, y) of the polygon (not closed)
        """
        plot = self.plot.centralWidget()

        dump = _SignalDump()
        self.plot.sigPlotSignal.connect(dump)

        for pos in polygon:
            self.mouseMove(plot, pos=pos)
            btn = qt.Qt.LeftButton if pos != polygon[-1] else qt.Qt.RightButton
            self.mouseClick(plot, btn, pos=pos)

        self.plot.sigPlotSignal.disconnect(dump)
        return [args[0] for args in dump.received]

    def test(self):
        """Test draw polygons + events"""
        self.plot.sigInteractiveModeChanged.connect(
            self._interactionModeChanged)

        self.plot.setInteractiveMode(
            'draw', shape='polygon', label='test', source=self)
        interaction = self.plot.getInteractiveMode()

        self.assertEqual(interaction['mode'], 'draw')
        self.assertEqual(interaction['shape'], 'polygon')

        self.plot.sigInteractiveModeChanged.disconnect(
            self._interactionModeChanged)

        plot = self.plot.centralWidget()
        xCenter, yCenter = plot.width() // 2, plot.height() // 2
        offset = min(plot.width(), plot.height()) // 10

        # Star polygon
        star = [(xCenter, yCenter + offset),
                (xCenter - offset, yCenter - offset),
                (xCenter + offset, yCenter),
                (xCenter - offset, yCenter),
                (xCenter + offset, yCenter - offset)]

        # Draw while dumping signals
        events = self._draw(star)

        # Test last event
        drawEvents = [event for event in events
                      if event['event'].startswith('drawing')]
        self.assertEqual(drawEvents[-1]['event'], 'drawingFinished')
        self.assertEqual(len(drawEvents[-1]['points']), 6)

        # Large square
        largeSquare = [(xCenter - offset, yCenter - offset),
                       (xCenter + offset, yCenter - offset),
                       (xCenter + offset, yCenter + offset),
                       (xCenter - offset, yCenter + offset)]

        # Draw while dumping signals
        events = self._draw(largeSquare)

        # Test last event
        drawEvents = [event for event in events
                      if event['event'].startswith('drawing')]
        self.assertEqual(drawEvents[-1]['event'], 'drawingFinished')
        self.assertEqual(len(drawEvents[-1]['points']), 5)

        # Rectangle too thin along X: Some points are ignored
        thinRectX = [(xCenter, yCenter - offset),
                     (xCenter, yCenter + offset),
                     (xCenter + 1, yCenter + offset),
                     (xCenter + 1, yCenter - offset)]

        # Draw while dumping signals
        events = self._draw(thinRectX)

        # Test last event
        drawEvents = [event for event in events
                      if event['event'].startswith('drawing')]
        self.assertEqual(drawEvents[-1]['event'], 'drawingFinished')
        self.assertEqual(len(drawEvents[-1]['points']), 3)

        # Rectangle too thin along Y: Some points are ignored
        thinRectY = [(xCenter - offset, yCenter),
                     (xCenter + offset, yCenter),
                     (xCenter + offset, yCenter + 1),
                     (xCenter - offset, yCenter + 1)]

        # Draw while dumping signals
        events = self._draw(thinRectY)

        # Test last event
        drawEvents = [event for event in events
                      if event['event'].startswith('drawing')]
        self.assertEqual(drawEvents[-1]['event'], 'drawingFinished')
        self.assertEqual(len(drawEvents[-1]['points']), 3)


def suite():
    test_suite = unittest.TestSuite()
    for TestClass in (TestSelectPolygon,):
        test_suite.addTest(
            unittest.defaultTestLoader.loadTestsFromTestCase(TestClass))
    return test_suite


if __name__ == '__main__':
    unittest.main(defaultTest='suite')