summaryrefslogtreecommitdiff
path: root/silx/gui/plot/test/testProfile.py
blob: 28d966925fdf3fdfc4e78fd6a880a2bf3d7663a1 (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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 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.
#
# ###########################################################################*/
"""Basic tests for Profile"""

__authors__ = ["T. Vincent", "P. Knobel"]
__license__ = "MIT"
__date__ = "17/01/2018"

import numpy
import unittest

from silx.utils.testutils import ParametricTestCase
from silx.gui.test.utils import (
    TestCaseQt, getQToolButtonFromAction)
from silx.gui import qt
from silx.gui.plot import PlotWindow, Plot1D, Plot2D, Profile
from silx.gui.plot.StackView import StackView


# Makes sure a QApplication exists
_qapp = qt.QApplication.instance() or qt.QApplication([])


class TestProfileToolBar(TestCaseQt, ParametricTestCase):
    """Tests for ProfileToolBar widget."""

    def setUp(self):
        super(TestProfileToolBar, self).setUp()
        profileWindow = PlotWindow()
        self.plot = PlotWindow()
        self.toolBar = Profile.ProfileToolBar(
            plot=self.plot, profileWindow=profileWindow)
        self.plot.addToolBar(self.toolBar)

        self.plot.show()
        self.qWaitForWindowExposed(self.plot)
        profileWindow.show()
        self.qWaitForWindowExposed(profileWindow)

        self.mouseMove(self.plot)  # Move to center
        self.qapp.processEvents()

    def tearDown(self):
        self.qapp.processEvents()
        self.plot.setAttribute(qt.Qt.WA_DeleteOnClose)
        self.plot.close()
        del self.plot
        del self.toolBar

        super(TestProfileToolBar, self).tearDown()

    def testAlignedProfile(self):
        """Test horizontal and vertical profile, without and with image"""
        # Use Plot backend widget to submit mouse events
        widget = self.plot.getWidgetHandle()

        # 2 positions to use for mouse events
        pos1 = widget.width() * 0.4, widget.height() * 0.4
        pos2 = widget.width() * 0.6, widget.height() * 0.6

        for action in (self.toolBar.hLineAction, self.toolBar.vLineAction):
            with self.subTest(mode=action.text()):
                # Trigger tool button for mode
                toolButton = getQToolButtonFromAction(action)
                self.assertIsNot(toolButton, None)
                self.mouseMove(toolButton)
                self.mouseClick(toolButton, qt.Qt.LeftButton)

                # Without image
                self.mouseMove(widget, pos=pos1)
                self.mouseClick(widget, qt.Qt.LeftButton, pos=pos1)

                # with image
                self.plot.addImage(numpy.arange(100 * 100).reshape(100, -1))
                self.mousePress(widget, qt.Qt.LeftButton, pos=pos1)
                self.mouseMove(widget, pos=pos2)
                self.mouseRelease(widget, qt.Qt.LeftButton, pos=pos2)

                self.mouseMove(widget)
                self.mouseClick(widget, qt.Qt.LeftButton)

    def testDiagonalProfile(self):
        """Test diagonal profile, without and with image"""
        # Use Plot backend widget to submit mouse events
        widget = self.plot.getWidgetHandle()

        # 2 positions to use for mouse events
        pos1 = widget.width() * 0.4, widget.height() * 0.4
        pos2 = widget.width() * 0.6, widget.height() * 0.6

        # Trigger tool button for diagonal profile mode
        toolButton = getQToolButtonFromAction(self.toolBar.lineAction)
        self.assertIsNot(toolButton, None)
        self.mouseMove(toolButton)
        self.mouseClick(toolButton, qt.Qt.LeftButton)

        for image in (False, True):
            with self.subTest(image=image):
                if image:
                    self.plot.addImage(numpy.arange(100 * 100).reshape(100, -1))

                self.mouseMove(widget, pos=pos1)
                self.mousePress(widget, qt.Qt.LeftButton, pos=pos1)
                self.mouseMove(widget, pos=pos2)
                self.mouseRelease(widget, qt.Qt.LeftButton, pos=pos2)

                self.plot.clear()


class TestGetProfilePlot(TestCaseQt):

    def testProfile1D(self):
        plot = Plot2D()
        plot.show()
        self.qWaitForWindowExposed(plot)
        plot.addImage([[0, 1], [2, 3]])
        self.assertIsInstance(plot.getProfileToolbar().getProfileMainWindow(),
                              qt.QMainWindow)
        self.assertIsInstance(plot.getProfilePlot(),
                              Plot1D)
        plot.setAttribute(qt.Qt.WA_DeleteOnClose)
        plot.close()
        del plot

    def testProfile2D(self):
        """Test that the profile plot associated to a stack view is either a
        Plot1D or a plot 2D instance."""
        plot = StackView()
        plot.show()
        self.qWaitForWindowExposed(plot)

        plot.setStack(numpy.array([[[0, 1], [2, 3]],
                                   [[4, 5], [6, 7]]]))

        self.assertIsInstance(plot.getProfileToolbar().getProfileMainWindow(),
                              qt.QMainWindow)

        # plot.getProfileToolbar().profile3dAction.computeProfileIn2D()  # default

        self.assertIsInstance(plot.getProfileToolbar().getProfilePlot(),
                              Plot2D)
        plot.getProfileToolbar().profile3dAction.computeProfileIn1D()
        self.assertIsInstance(plot.getProfileToolbar().getProfilePlot(),
                              Plot1D)

        plot.setAttribute(qt.Qt.WA_DeleteOnClose)
        plot.close()
        del plot


def suite():
    test_suite = unittest.TestSuite()
    # test_suite.addTest(positionInfoTestSuite)
    for testClass in (TestProfileToolBar, TestGetProfilePlot):
        test_suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(
            testClass))
    return test_suite


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