summaryrefslogtreecommitdiff
path: root/silx/gui/plot/tools/profile/manager.py
blob: 68db9a6a407cc505402cf547fc45e045c3050855 (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
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
402
403
404
405
406
407
408
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
489
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
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
806
807
808
809
810
811
812
813
814
815
816
817
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
844
845
846
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2018-2021 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.
#
# ###########################################################################*/
"""This module provides a manager to compute and display profiles.
"""

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "28/06/2018"

import logging
import weakref

from silx.gui import qt
from silx.gui import colors
from silx.gui import utils

from silx.utils.weakref import WeakMethodProxy
from silx.gui import icons
from silx.gui.plot import PlotWidget
from silx.gui.plot.tools.roi import RegionOfInterestManager
from silx.gui.plot.tools.roi import CreateRoiModeAction
from silx.gui.plot import items
from silx.gui.qt import silxGlobalThreadPool
from silx.gui.qt import inspect
from . import rois
from . import core
from . import editors


_logger = logging.getLogger(__name__)


class _RunnableComputeProfile(qt.QRunnable):
    """Runner to process profiles

    :param qt.QThreadPool threadPool: The thread which will be used to
        execute this runner. It is used to update the used signals
    :param ~silx.gui.plot.items.Item item: Item in which the profile is
        computed
    :param ~silx.gui.plot.tools.profile.core.ProfileRoiMixIn roi: ROI
        defining the profile shape and other characteristics
    """

    class _Signals(qt.QObject):
        """Signal holder"""
        resultReady = qt.Signal(object, object)
        runnerFinished = qt.Signal(object)

    def __init__(self, threadPool, item, roi):
        """Constructor
        """
        super(_RunnableComputeProfile, self).__init__()
        self._signals = self._Signals()
        self._signals.moveToThread(threadPool.thread())
        self._item = item
        self._roi = roi
        self._cancelled = False

    def _lazyCancel(self):
        """Cancel the runner if it is not yet started.

        The threadpool will still execute the runner, but this will process
        nothing.

        This is only used with Qt<5.9 where QThreadPool.tryTake is not available.
        """
        self._cancelled = True

    def autoDelete(self):
        return False

    def getRoi(self):
        """Returns the ROI in which the runner will compute a profile.

        :rtype: ~silx.gui.plot.tools.profile.core.ProfileRoiMixIn
        """
        return self._roi

    @property
    def resultReady(self):
        """Signal emitted when the result of the computation is available.

        This signal provides 2 values: The ROI, and the computation result.
        """
        return self._signals.resultReady

    @property
    def runnerFinished(self):
        """Signal emitted when runner have finished.

        This signal provides a single value: the runner itself.
        """
        return self._signals.runnerFinished

    def run(self):
        """Process the profile computation.
        """
        if not self._cancelled:
            try:
                profileData = self._roi.computeProfile(self._item)
            except Exception:
                _logger.error("Error while computing profile", exc_info=True)
            else:
                self.resultReady.emit(self._roi, profileData)
        self.runnerFinished.emit(self)


class ProfileWindow(qt.QMainWindow):
    """
    Display a computed profile.

    The content can be described using :meth:`setRoiProfile` if the source of
    the profile is a profile ROI, and :meth:`setProfile` for the data content.
    """

    sigClose = qt.Signal()
    """Emitted by :meth:`closeEvent` (e.g. when the window is closed
    through the window manager's close icon)."""

    def __init__(self, parent=None, backend=None):
        qt.QMainWindow.__init__(self, parent=parent, flags=qt.Qt.Dialog)

        self.setWindowTitle('Profile window')
        self._plot1D = None
        self._plot2D = None
        self._backend = backend
        self._data = None

        widget = qt.QWidget()
        self._layout = qt.QStackedLayout(widget)
        self._layout.setContentsMargins(0, 0, 0, 0)
        self.setCentralWidget(widget)

    def prepareWidget(self, roi):
        """Called before the show to prepare the window to use with
        a specific ROI."""
        if isinstance(roi, rois._DefaultImageStackProfileRoiMixIn):
            profileType = roi.getProfileType()
        else:
            profileType = "1D"
        if profileType == "1D":
            self.getPlot1D()
        elif profileType == "2D":
            self.getPlot2D()

    def createPlot1D(self, parent, backend):
        """Inherit this function to create your own plot to render 1D
        profiles. The default value is a `Plot1D`.

        :param parent: The parent of this widget or None.
        :param backend: The backend to use for the plot.
                        See :class:`PlotWidget` for the list of supported backend.
        :rtype: PlotWidget
        """
        # import here to avoid circular import
        from ...PlotWindow import Plot1D
        plot = Plot1D(parent=parent, backend=backend)
        plot.setDataMargins(yMinMargin=0.1, yMaxMargin=0.1)
        plot.setGraphYLabel('Profile')
        plot.setGraphXLabel('')
        return plot

    def createPlot2D(self, parent, backend):
        """Inherit this function to create your own plot to render 2D
        profiles. The default value is a `Plot2D`.

        :param parent: The parent of this widget or None.
        :param backend: The backend to use for the plot.
                        See :class:`PlotWidget` for the list of supported backend.
        :rtype: PlotWidget
        """
        # import here to avoid circular import
        from ...PlotWindow import Plot2D
        return Plot2D(parent=parent, backend=backend)

    def getPlot1D(self, init=True):
        """Return the current plot used to display curves and create it if it
        does not yet exists and `init` is True. Else returns None."""
        if not init:
            return self._plot1D
        if self._plot1D is None:
            self._plot1D = self.createPlot1D(self, self._backend)
            self._layout.addWidget(self._plot1D)
        return self._plot1D

    def _showPlot1D(self):
        plot = self.getPlot1D()
        self._layout.setCurrentWidget(plot)

    def getPlot2D(self, init=True):
        """Return the current plot used to display image and create it if it
        does not yet exists and `init` is True. Else returns None."""
        if not init:
            return self._plot2D
        if self._plot2D is None:
            self._plot2D = self.createPlot2D(parent=self, backend=self._backend)
            self._layout.addWidget(self._plot2D)
        return self._plot2D

    def _showPlot2D(self):
        plot = self.getPlot2D()
        self._layout.setCurrentWidget(plot)

    def getCurrentPlotWidget(self):
        return self._layout.currentWidget()

    def closeEvent(self, qCloseEvent):
        self.sigClose.emit()
        qCloseEvent.accept()

    def setRoiProfile(self, roi):
        """Set the profile ROI which it the source of the following data
        to display.

        :param ProfileRoiMixIn roi: The profile ROI data source
        """
        if roi is None:
            return
        self.__color = colors.rgba(roi.getColor())

    def _setImageProfile(self, data):
        """
        Setup the window to display a new profile data which is represented
        by an image.

        :param core.ImageProfileData data: Computed data profile
        """
        plot = self.getPlot2D()

        plot.clear()
        plot.setGraphTitle(data.title)
        plot.getXAxis().setLabel(data.xLabel)


        coords = data.coords
        colormap = data.colormap
        profileScale = (coords[-1] - coords[0]) / data.profile.shape[1], 1
        plot.addImage(data.profile,
                      legend="profile",
                      colormap=colormap,
                      origin=(coords[0], 0),
                      scale=profileScale)
        plot.getYAxis().setLabel("Frame index (depth)")

        self._showPlot2D()

    def _setCurveProfile(self, data):
        """
        Setup the window to display a new profile data which is represented
        by a curve.

        :param core.CurveProfileData data: Computed data profile
        """
        plot = self.getPlot1D()

        plot.clear()
        plot.setGraphTitle(data.title)
        plot.getXAxis().setLabel(data.xLabel)
        plot.getYAxis().setLabel(data.yLabel)

        plot.addCurve(data.coords,
                      data.profile,
                      legend="level",
                      color=self.__color)

        self._showPlot1D()

    def _setRgbaProfile(self, data):
        """
        Setup the window to display a new profile data which is represented
        by a curve.

        :param core.RgbaProfileData data: Computed data profile
        """
        plot = self.getPlot1D()

        plot.clear()
        plot.setGraphTitle(data.title)
        plot.getXAxis().setLabel(data.xLabel)
        plot.getYAxis().setLabel(data.yLabel)

        self._showPlot1D()

        plot.addCurve(data.coords, data.profile,
                      legend="level", color="black")
        plot.addCurve(data.coords, data.profile_r,
                      legend="red", color="red")
        plot.addCurve(data.coords, data.profile_g,
                      legend="green", color="green")
        plot.addCurve(data.coords, data.profile_b,
                      legend="blue", color="blue")
        if data.profile_a is not None:
            plot.addCurve(data.coords, data.profile_a, legend="alpha", color="gray")

    def clear(self):
        """Clear the window profile"""
        plot = self.getPlot1D(init=False)
        if plot is not None:
            plot.clear()
        plot = self.getPlot2D(init=False)
        if plot is not None:
            plot.clear()

    def getProfile(self):
        """Returns the profile data which is displayed"""
        return self.__data

    def setProfile(self, data):
        """
        Setup the window to display a new profile data.

        This method dispatch the result to a specific method according to the
        data type.

        :param data: Computed data profile
        """
        self.__data = data
        if data is None:
            self.clear()
        elif isinstance(data, core.ImageProfileData):
            self._setImageProfile(data)
        elif isinstance(data, core.RgbaProfileData):
            self._setRgbaProfile(data)
        elif isinstance(data, core.CurveProfileData):
            self._setCurveProfile(data)
        else:
            raise TypeError("Unsupported type %s" % type(data))


class _ClearAction(qt.QAction):
    """Action to clear the profile manager

    The action is only enabled if something can be cleaned up.
    """

    def __init__(self, parent, profileManager):
        super(_ClearAction, self).__init__(parent)
        self.__profileManager = weakref.ref(profileManager)
        icon = icons.getQIcon('profile-clear')
        self.setIcon(icon)
        self.setText('Clear profile')
        self.setToolTip('Clear the profiles')
        self.setCheckable(False)
        self.setEnabled(False)
        self.triggered.connect(profileManager.clearProfile)
        plot = profileManager.getPlotWidget()
        roiManager = profileManager.getRoiManager()
        plot.sigInteractiveModeChanged.connect(self.__modeUpdated)
        roiManager.sigRoiChanged.connect(self.__roiListUpdated)

    def getProfileManager(self):
        return self.__profileManager()

    def __roiListUpdated(self):
        self.__update()

    def __modeUpdated(self, source):
        self.__update()

    def __update(self):
        profileManager = self.getProfileManager()
        if profileManager is None:
            return
        roiManager = profileManager.getRoiManager()
        if roiManager is None:
            return
        enabled = roiManager.isStarted() or len(roiManager.getRois()) > 0
        self.setEnabled(enabled)


class _StoreLastParamBehavior(qt.QObject):
    """This object allow to store and restore the properties of the ROI
    profiles"""

    def __init__(self, parent):
        assert isinstance(parent, ProfileManager)
        super(_StoreLastParamBehavior, self).__init__(parent=parent)
        self.__properties = {}
        self.__profileRoi = None
        self.__filter = utils.LockReentrant()

    def _roi(self):
        """Return the spied ROI"""
        if self.__profileRoi is None:
            return None
        roi = self.__profileRoi()
        if roi is None:
            self.__profileRoi = None
        return roi

    def setProfileRoi(self, roi):
        """Set a profile ROI to spy.

        :param ProfileRoiMixIn roi: A profile ROI
        """
        previousRoi = self._roi()
        if previousRoi is roi:
            return
        if previousRoi is not None:
            previousRoi.sigProfilePropertyChanged.disconnect(self._profilePropertyChanged)
        self.__profileRoi = None if roi is None else weakref.ref(roi)
        if roi is not None:
            roi.sigProfilePropertyChanged.connect(self._profilePropertyChanged)

    def _profilePropertyChanged(self):
        """Handle changes on the properties defining the profile ROI.
        """
        if self.__filter.locked():
            return
        roi = self.sender()
        self.storeProperties(roi)

    def storeProperties(self, roi):
        if isinstance(roi, (rois._DefaultImageStackProfileRoiMixIn,
                              rois.ProfileImageStackCrossROI)):
            self.__properties["method"] = roi.getProfileMethod()
            self.__properties["line-width"] = roi.getProfileLineWidth()
            self.__properties["type"] = roi.getProfileType()
        elif isinstance(roi, (rois._DefaultImageProfileRoiMixIn,
                            rois.ProfileImageCrossROI)):
            self.__properties["method"] = roi.getProfileMethod()
            self.__properties["line-width"] = roi.getProfileLineWidth()
        elif isinstance(roi, (rois._DefaultScatterProfileRoiMixIn,
                              rois.ProfileScatterCrossROI)):
            self.__properties["npoints"] = roi.getNPoints()

    def restoreProperties(self, roi):
        with self.__filter:
            if isinstance(roi, (rois._DefaultImageStackProfileRoiMixIn,
                                  rois.ProfileImageStackCrossROI)):
                value = self.__properties.get("method", None)
                if value is not None:
                    roi.setProfileMethod(value)
                value = self.__properties.get("line-width", None)
                if value is not None:
                    roi.setProfileLineWidth(value)
                value = self.__properties.get("type", None)
                if value is not None:
                    roi.setProfileType(value)
            elif isinstance(roi, (rois._DefaultImageProfileRoiMixIn,
                                rois.ProfileImageCrossROI)):
                value = self.__properties.get("method", None)
                if value is not None:
                    roi.setProfileMethod(value)
                value = self.__properties.get("line-width", None)
                if value is not None:
                    roi.setProfileLineWidth(value)
            elif isinstance(roi, (rois._DefaultScatterProfileRoiMixIn,
                                  rois.ProfileScatterCrossROI)):
                value = self.__properties.get("npoints", None)
                if value is not None:
                    roi.setNPoints(value)


class ProfileManager(qt.QObject):
    """Base class for profile management tools

    :param plot: :class:`~silx.gui.plot.PlotWidget` on which to operate.
    :param plot: :class:`~silx.gui.plot.tools.roi.RegionOfInterestManager`
        on which to operate.
    """
    def __init__(self, parent=None, plot=None, roiManager=None):
        super(ProfileManager, self).__init__(parent)

        assert isinstance(plot, PlotWidget)
        self._plotRef = weakref.ref(
            plot, WeakMethodProxy(self.__plotDestroyed))

        # Set-up interaction manager
        if roiManager is None:
            roiManager = RegionOfInterestManager(plot)

        self._roiManagerRef = weakref.ref(roiManager)
        self._rois = []
        self._pendingRunners = []
        """List of ROIs which have to be updated"""

        self.__reentrantResults = {}
        """Store reentrant result to avoid to skip some of them
        cause the implementation uses a QEventLoop."""

        self._profileWindowClass = ProfileWindow
        """Class used to display the profile results"""

        self._computedProfiles = 0
        """Statistics for tests"""

        self.__itemTypes = []
        """Kind of items to use"""

        self.__tracking = False
        """Is the plot active items are tracked"""

        self.__useColorFromCursor = True
        """If true, force the ROI color with the colormap marker color"""

        self._item = None
        """The selected item"""

        self.__singleProfileAtATime = True
        """When it's true, only a single profile is displayed at a time."""

        self._previousWindowGeometry = []

        self._storeProperties = _StoreLastParamBehavior(self)
        """If defined the profile properties of the last ROI are reused to the
        new created ones"""

        # Listen to plot limits changed
        plot.getXAxis().sigLimitsChanged.connect(self.requestUpdateAllProfile)
        plot.getYAxis().sigLimitsChanged.connect(self.requestUpdateAllProfile)

        roiManager.sigInteractiveModeFinished.connect(self.__interactionFinished)
        roiManager.sigInteractiveRoiCreated.connect(self.__roiCreated)
        roiManager.sigRoiAdded.connect(self.__roiAdded)
        roiManager.sigRoiAboutToBeRemoved.connect(self.__roiRemoved)

    def setSingleProfile(self, enable):
        """
        Enable or disable the single profile mode.

        In single mode, the manager enforce a single ROI at the same
        time. A new one will remove the previous one.

        If this mode is not enabled, many ROIs can be created, and many
        profile windows will be displayed.
        """
        self.__singleProfileAtATime = enable

    def isSingleProfile(self):
        """
        Returns true if the manager is in a single profile mode.

        :rtype: bool
        """
        return self.__singleProfileAtATime

    def __interactionFinished(self):
        """Handle end of interactive mode"""
        pass

    def __roiAdded(self, roi):
        """Handle new ROI"""
        # Filter out non profile ROIs
        if not isinstance(roi, core.ProfileRoiMixIn):
            return
        self.__addProfile(roi)

    def __roiRemoved(self, roi):
        """Handle removed ROI"""
        # Filter out non profile ROIs
        if not isinstance(roi, core.ProfileRoiMixIn):
            return
        self.__removeProfile(roi)

    def createProfileAction(self, profileRoiClass, parent=None):
        """Create an action from a class of ProfileRoi

        :param core.ProfileRoiMixIn profileRoiClass: A class of a profile ROI
        :param qt.QObject parent: The parent of the created action.
        :rtype: qt.QAction
        """
        if not issubclass(profileRoiClass, core.ProfileRoiMixIn):
            raise TypeError("Type %s not expected" % type(profileRoiClass))
        roiManager = self.getRoiManager()
        action = CreateRoiModeAction(parent, roiManager, profileRoiClass)
        if hasattr(profileRoiClass, "ICON"):
            action.setIcon(icons.getQIcon(profileRoiClass.ICON))
        if hasattr(profileRoiClass, "NAME"):
            def articulify(word):
                """Add an an/a article in the front of the word"""
                first = word[1] if word[0] == 'h' else word[0]
                if first in "aeiou":
                    return "an " + word
                return "a " + word
            action.setText('Define %s' % articulify(profileRoiClass.NAME))
            action.setToolTip('Enables %s selection mode' % profileRoiClass.NAME)
        action.setSingleShot(True)
        return action

    def createClearAction(self, parent):
        """Create an action to clean up the plot from the profile ROIs.

        :param qt.QObject parent: The parent of the created action.
        :rtype: qt.QAction
        """
        action = _ClearAction(parent, self)
        return action

    def createImageActions(self, parent):
        """Create actions designed for image items. This actions created
        new ROIs.

        :param qt.QObject parent: The parent of the created action.
        :rtype: List[qt.QAction]
        """
        profileClasses = [
            rois.ProfileImageHorizontalLineROI,
            rois.ProfileImageVerticalLineROI,
            rois.ProfileImageLineROI,
            rois.ProfileImageDirectedLineROI,
            rois.ProfileImageCrossROI,
            ]
        return [self.createProfileAction(pc, parent=parent) for pc in profileClasses]

    def createScatterActions(self, parent):
        """Create actions designed for scatter items. This actions created
        new ROIs.

        :param qt.QObject parent: The parent of the created action.
        :rtype: List[qt.QAction]
        """
        profileClasses = [
            rois.ProfileScatterHorizontalLineROI,
            rois.ProfileScatterVerticalLineROI,
            rois.ProfileScatterLineROI,
            rois.ProfileScatterCrossROI,
            ]
        return [self.createProfileAction(pc, parent=parent) for pc in profileClasses]

    def createScatterSliceActions(self, parent):
        """Create actions designed for regular scatter items. This actions
        created new ROIs.

        This ROIs was designed to use the input data without interpolation,
        like you could do with an image.

        :param qt.QObject parent: The parent of the created action.
        :rtype: List[qt.QAction]
        """
        profileClasses = [
            rois.ProfileScatterHorizontalSliceROI,
            rois.ProfileScatterVerticalSliceROI,
            rois.ProfileScatterCrossSliceROI,
            ]
        return [self.createProfileAction(pc, parent=parent) for pc in profileClasses]

    def createImageStackActions(self, parent):
        """Create actions designed for stack image items. This actions
        created new ROIs.

        This ROIs was designed to create both profile on the displayed image
        and profile on the full stack (2D result).

        :param qt.QObject parent: The parent of the created action.
        :rtype: List[qt.QAction]
        """
        profileClasses = [
            rois.ProfileImageStackHorizontalLineROI,
            rois.ProfileImageStackVerticalLineROI,
            rois.ProfileImageStackLineROI,
            rois.ProfileImageStackCrossROI,
            ]
        return [self.createProfileAction(pc, parent=parent) for pc in profileClasses]

    def createEditorAction(self, parent):
        """Create an action containing GUI to edit the selected profile ROI.

        :param qt.QObject parent: The parent of the created action.
        :rtype: qt.QAction
        """
        action = editors.ProfileRoiEditorAction(parent)
        action.setRoiManager(self.getRoiManager())
        return action

    def setItemType(self, image=False, scatter=False):
        """Set the item type to use and select the active one.

        :param bool image: Image item are allowed
        :param bool scatter: Scatter item are allowed
        """
        self.__itemTypes = []
        plot = self.getPlotWidget()
        item = None
        if image:
            self.__itemTypes.append("image")
            item = plot.getActiveImage()
        if scatter:
            self.__itemTypes.append("scatter")
            if item is None:
                item = plot.getActiveScatter()
        self.setPlotItem(item)

    def setProfileWindowClass(self, profileWindowClass):
        """Set the class which will be instantiated to display profile result.
        """
        self._profileWindowClass = profileWindowClass

    def setActiveItemTracking(self, tracking):
        """Enable/disable the tracking of the active item of the plot.

        :param bool tracking: Tracking mode
        """
        if self.__tracking == tracking:
            return
        plot = self.getPlotWidget()
        if self.__tracking:
            plot.sigActiveImageChanged.disconnect(self._activeImageChanged)
            plot.sigActiveScatterChanged.disconnect(self._activeScatterChanged)
        self.__tracking = tracking
        if self.__tracking:
            plot.sigActiveImageChanged.connect(self.__activeImageChanged)
            plot.sigActiveScatterChanged.connect(self.__activeScatterChanged)

    def setDefaultColorFromCursorColor(self, enabled):
        """Enabled/disable the use of the colormap cursor color to display the
        ROIs.

        If set, the manager will update the color of the profile ROIs using the
        current colormap cursor color from the selected item.
        """
        self.__useColorFromCursor = enabled

    def __activeImageChanged(self, previous, legend):
        """Handle plot item selection"""
        if "image" in self.__itemTypes:
            plot = self.getPlotWidget()
            item = plot.getImage(legend)
            self.setPlotItem(item)

    def __activeScatterChanged(self, previous, legend):
        """Handle plot item selection"""
        if "scatter" in self.__itemTypes:
            plot = self.getPlotWidget()
            item = plot.getScatter(legend)
            self.setPlotItem(item)

    def __roiCreated(self, roi):
        """Handle ROI creation"""
        # Filter out non profile ROIs
        if isinstance(roi, core.ProfileRoiMixIn):
            if self._storeProperties is not None:
                # Initialize the properties with the previous ones
                self._storeProperties.restoreProperties(roi)

    def __addProfile(self, profileRoi):
        """Add a new ROI to the manager."""
        if profileRoi.getFocusProxy() is None:
            if self._storeProperties is not None:
                # Follow changes on properties
                self._storeProperties.setProfileRoi(profileRoi)
            if self.__singleProfileAtATime:
                # FIXME: It would be good to reuse the windows to avoid blinking
                self.clearProfile()

        profileRoi._setProfileManager(self)
        self._updateRoiColor(profileRoi)
        self._rois.append(profileRoi)
        self.requestUpdateProfile(profileRoi)

    def __removeProfile(self, profileRoi):
        """Remove a ROI from the manager."""
        window = self._disconnectProfileWindow(profileRoi)
        if window is not None:
            geometry = window.geometry()
            if not geometry.isEmpty():
                self._previousWindowGeometry.append(geometry)
            self.clearProfileWindow(window)
        if profileRoi in self._rois:
            self._rois.remove(profileRoi)

    def _disconnectProfileWindow(self, profileRoi):
        """Handle profile window close."""
        window = profileRoi.getProfileWindow()
        profileRoi.setProfileWindow(None)
        return window

    def clearProfile(self):
        """Clear the associated ROI profile"""
        roiManager = self.getRoiManager()
        for roi in list(self._rois):
            if roi.getFocusProxy() is not None:
                # Skip sub ROIs, it will be removed by their parents
                continue
            roiManager.removeRoi(roi)

        if not roiManager.isDrawing():
            # Clean the selected mode
            roiManager.stop()

    def hasPendingOperations(self):
        """Returns true if a thread is still computing or displaying a profile.

        :rtype: bool
        """
        return len(self.__reentrantResults) > 0 or len(self._pendingRunners) > 0

    def requestUpdateAllProfile(self):
        """Request to update the profile of all the managed ROIs.
        """
        for roi in self._rois:
            self.requestUpdateProfile(roi)

    def requestUpdateProfile(self, profileRoi):
        """Request to update a specific profile ROI.

        :param ~core.ProfileRoiMixIn profileRoi:
        """
        if profileRoi.computeProfile is None:
            return
        threadPool = silxGlobalThreadPool()

        # Clean up deprecated runners
        for runner in list(self._pendingRunners):
            if not inspect.isValid(runner):
                self._pendingRunners.remove(runner)
                continue
            if runner.getRoi() is profileRoi:
                if hasattr(threadPool, "tryTake"):
                    if threadPool.tryTake(runner):
                        self._pendingRunners.remove(runner)
                else:  # Support Qt<5.9
                    runner._lazyCancel()

        item = self.getPlotItem()
        if item is None or not isinstance(item, profileRoi.ITEM_KIND):
            # This item is not compatible with this profile
            profileRoi._setPlotItem(None)
            profileWindow = profileRoi.getProfileWindow()
            if profileWindow is not None:
                profileWindow.setProfile(None)
            return

        profileRoi._setPlotItem(item)
        runner = _RunnableComputeProfile(threadPool, item, profileRoi)
        runner.runnerFinished.connect(self.__cleanUpRunner)
        runner.resultReady.connect(self.__displayResult)
        self._pendingRunners.append(runner)
        threadPool.start(runner)

    def __cleanUpRunner(self, runner):
        """Remove a thread pool runner from the list of hold tasks.

        Called at the termination of the runner.
        """
        if runner in self._pendingRunners:
            self._pendingRunners.remove(runner)

    def __displayResult(self, roi, profileData):
        """Display the result of a ROI.

        :param ~core.ProfileRoiMixIn profileRoi: A managed ROI
        :param ~core.CurveProfileData profileData: Computed data profile
        """
        if roi in self.__reentrantResults:
            # Store the data to process it in the main loop
            # And not a sub loop created by initProfileWindow
            # This also remove the duplicated requested
            self.__reentrantResults[roi] = profileData
            return

        self.__reentrantResults[roi] = profileData
        self._computedProfiles = self._computedProfiles + 1
        window = roi.getProfileWindow()
        if window is None:
            plot = self.getPlotWidget()
            window = self.createProfileWindow(plot, roi)
            # roi.profileWindow have to be set before initializing the window
            # Cause the initialization is using QEventLoop
            roi.setProfileWindow(window)
            self.initProfileWindow(window, roi)
            window.show()

        lastData = self.__reentrantResults.pop(roi)
        window.setProfile(lastData)

    def __plotDestroyed(self, ref):
        """Handle finalization of PlotWidget

        :param ref: weakref to the plot
        """
        self._plotRef = None
        self._roiManagerRef = None
        self._pendingRunners = []

    def setPlotItem(self, item):
        """Set the plot item focused by the profile manager.

        :param ~silx.gui.plot.items.Item item: A plot item
        """
        previous = self.getPlotItem()
        if previous is item:
            return
        if item is None:
            self._item = None
        else:
            item.sigItemChanged.connect(self.__itemChanged)
            self._item = weakref.ref(item)
        self._updateRoiColors()
        self.requestUpdateAllProfile()

    def getDefaultColor(self, item):
        """Returns the default ROI color to use according to the given item.

        :param ~silx.gui.plot.items.item.Item item: AN item
        :rtype: qt.QColor
        """
        color = 'pink'
        if isinstance(item, items.ColormapMixIn):
            colormap = item.getColormap()
            name = colormap.getName()
            if name is not None:
                color = colors.cursorColorForColormap(name)
        color = colors.asQColor(color)
        return color

    def _updateRoiColors(self):
        """Update ROI color according to the item selection"""
        if not self.__useColorFromCursor:
            return
        item = self.getPlotItem()
        color = self.getDefaultColor(item)
        for roi in self._rois:
            roi.setColor(color)

    def _updateRoiColor(self, roi):
        """Update a specific ROI according to the current selected item.

        :param RegionOfInterest roi: The ROI to update
        """
        if not self.__useColorFromCursor:
            return
        item = self.getPlotItem()
        color = self.getDefaultColor(item)
        roi.setColor(color)

    def __itemChanged(self, changeType):
        """Handle item changes.
        """
        if changeType in (items.ItemChangedType.DATA,
                          items.ItemChangedType.MASK,
                          items.ItemChangedType.POSITION,
                          items.ItemChangedType.SCALE):
            self.requestUpdateAllProfile()
        elif changeType == (items.ItemChangedType.COLORMAP):
            self._updateRoiColors()

    def getPlotItem(self):
        """Returns the item focused by the profile manager.

        :rtype: ~silx.gui.plot.items.Item
        """
        if self._item is None:
            return None
        item = self._item()
        if item is None:
            self._item = None
        return item

    def getPlotWidget(self):
        """The plot associated to the profile manager.

        :rtype: ~silx.gui.plot.PlotWidget
        """
        if self._plotRef is None:
            return None
        plot = self._plotRef()
        if plot is None:
            self._plotRef = None
        return plot

    def getCurrentRoi(self):
        """Returns the currently selected ROI, else None.

        :rtype: core.ProfileRoiMixIn
        """
        roiManager = self.getRoiManager()
        if roiManager is None:
            return None
        roi = roiManager.getCurrentRoi()
        if not isinstance(roi, core.ProfileRoiMixIn):
            return None
        return roi

    def getRoiManager(self):
        """Returns the used ROI manager

        :rtype: RegionOfInterestManager
        """
        return self._roiManagerRef()

    def createProfileWindow(self, plot, roi):
        """Create a new profile window.

        :param ~core.ProfileRoiMixIn roi: The plot containing the raw data
        :param ~core.ProfileRoiMixIn roi: A managed ROI
        :rtype: ~ProfileWindow
        """
        return self._profileWindowClass(plot)

    def initProfileWindow(self, profileWindow, roi):
        """This function is called just after the profile window creation in
        order to initialize the window location.

        :param ~ProfileWindow profileWindow:
            The profile window to initialize.
        """
        # Enforce the use of one of the widgets
        # To have the correct window size
        profileWindow.prepareWidget(roi)
        profileWindow.adjustSize()

        # Trick to avoid blinking while retrieving the right window size
        # Display the window, hide it and wait for some event loops
        profileWindow.show()
        profileWindow.hide()
        eventLoop = qt.QEventLoop(self)
        for _ in range(10):
            if not eventLoop.processEvents():
                break

        profileWindow.show()
        if len(self._previousWindowGeometry) > 0:
            geometry = self._previousWindowGeometry.pop()
            profileWindow.setGeometry(geometry)
            return

        window = self.getPlotWidget().window()
        winGeom = window.frameGeometry()
        qapp = qt.QApplication.instance()
        desktop = qapp.desktop()
        screenGeom = desktop.availableGeometry(window)
        spaceOnLeftSide = winGeom.left()
        spaceOnRightSide = screenGeom.width() - winGeom.right()

        profileGeom = profileWindow.frameGeometry()
        profileWidth = profileGeom.width()

        # Align vertically to the center of the window
        top = winGeom.top() + (winGeom.height() - profileGeom.height()) // 2

        margin = 5
        if profileWidth < spaceOnRightSide:
            # Place profile on the right
            left = winGeom.right() + margin
        elif profileWidth < spaceOnLeftSide:
            # Place profile on the left
            left = max(0, winGeom.left() - profileWidth - margin)
        else:
            # Move it as much as possible where there is more space
            if spaceOnLeftSide > spaceOnRightSide:
                left = 0
            else:
                left = screenGeom.width() - profileGeom.width()
        profileWindow.move(left, top)


    def clearProfileWindow(self, profileWindow):
        """Called when a profile window is not anymore needed.

        By default the window will be closed. But it can be
        inherited to change this behavior.
        """
        profileWindow.deleteLater()