myMaskImageWidget.py 6.02 KB
Newer Older
1
2
3
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
4
from  PyMca5.PyMcaGui  import MaskImageWidget  as sole_MaskImageWidget
Alessandro Mirone's avatar
Alessandro Mirone committed
5
6
import PyMca5.PyMcaGui.MaskImageWidget
print (  PyMca5.PyMcaGui.MaskImageWidget .__file__)
myron's avatar
myron committed
7
8
9
# from  PyQt4 import Qt, QtCore
from silx.gui import qt as Qt
from silx.gui import qt as QtCore
10
import numpy 
11
12
import string

Christoph Sahle's avatar
Christoph Sahle committed
13
from PyMca5.PyMcaGraph.Plot import Plot
14
from six.moves import range
15
16
17
18

# from PyMca5.PyMcaGraph.backends.OpenGLBackend import OpenGLBackend
# Plot.defaultBackend = OpenGLBackend 
# Plot.defaultBackend = OpenGLBackend 
19

Alessandro Mirone's avatar
Alessandro Mirone committed
20
21
22
import silx.gui.plot.MaskToolsWidget as MTW


23
class MaskImageWidget(sole_MaskImageWidget.MaskImageWidget):
Alessandro Mirone's avatar
Alessandro Mirone committed
24
# class MaskImageWidget(MTW.ImageMask ):
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    changeTagOn=False
    def _graphSignal(self, ddict, ownsignal = None):
 
        if self.changeTagOn:
            if self.__selectionMask is not None and ddict['event']=="mouseClicked" and ddict['button']=="middle" :
                x,y = int(ddict["x"]), int(ddict["y"])

                y, x = sole_MaskImageWidget.convertToRowAndColumn(x , y,
                                             self.__imageData.shape,
                                             xScale=self._xScale,
                                             yScale=self._yScale,
                                             safe=True)

                id_target = self.__selectionMask[y,x]
                if id_target and id_target!= self._nRoi:
                    mask_target =  (self.__selectionMask == id_target  )
                    mask_swap   =  (self.__selectionMask== self._nRoi)
                    self.__selectionMask [mask_target] = self._nRoi
                    self.__selectionMask [mask_swap] = id_target
                    emitsignal = True
                    if emitsignal:
                        self.plotImage(update = False)
                        self._emitMaskChangedSignal()
                    return
myron's avatar
myron committed
49
50
        super(MaskImageWidget, self)._graphSignal(ddict, ownsignal) 
        # sole_MaskImageWidget.MaskImageWidget._graphSignal(self, ddict, ownsignal)
51
52
53


    def dragEnterEvent(self,event):
Alessandro Mirone's avatar
Alessandro Mirone committed
54
55
56
        print( dir(event.mimeData()))
        print( list(event.mimeData().formats()))
        print( event.mimeData().text())
57
58
59

        model = Qt.QStandardItemModel()
        model.dropMimeData(event.mimeData(), QtCore.Qt.CopyAction, 0,0, Qt.QModelIndex())
Alessandro Mirone's avatar
Alessandro Mirone committed
60
        print( model)
61
62
63
64
65
66

        if event.mimeData().hasFormat('application/x-qabstractitemmodeldatalist'):
            event.acceptProposedAction()

            bytearray = event.mimeData().data('application/x-qabstractitemmodeldatalist')
            data_items = decode_data(bytearray)
Alessandro Mirone's avatar
Alessandro Mirone committed
67
            print( data_items)
68
69

        if event.mimeData().hasFormat("text/plain"):
Alessandro Mirone's avatar
Alessandro Mirone committed
70
            print( " OK ")
71
72
73
74
            event.acceptProposedAction()
 
    def dropEvent(self, e):

75
76
77
78
79
80
   

        localpos = self.graph.getWidgetHandle().mapFromGlobal( Qt.QCursor().pos()   )
        x,y = localpos.x(), localpos.y()

        x,y = self.graph.pixelToData(x,y ) 
Alessandro Mirone's avatar
Alessandro Mirone committed
81
        print( "POSITION ",x,y)
82
83
84
85
    
        mask = self.getSelectionMask()
        
        Ct = mask[int(y), int(x)]
Alessandro Mirone's avatar
Alessandro Mirone committed
86
        print( " VALORE MASCHERA ", Ct)
87

88
        if Ct:
Alessandro Mirone's avatar
Alessandro Mirone committed
89
            print( str(e.mimeData().text()))
Christoph Sahle's avatar
Christoph Sahle committed
90
            Cc = int( str(e.mimeData().text()))
91
92
93
94
95
            # bytearray = e.mimeData().data('application/x-qabstractitemmodeldatalist')
            # data = decode_data(bytearray)
            # print data
            # Cc = (2-data[0])*4+data[1] + 1
            # print Cc
96
97
98
99
            zonet = (mask==Ct)
            zonec = (mask==Cc)
            mask[zonet]=Cc
            mask[zonec]=Ct
100
            self.setSelectionMask(mask)
101
102
103
            self.annotateSpots()


104
    def annotateSpots(self, a_ids = None, offset=None):
105
106
107
108
109
110
111
112
113
114
115
116
117

        self.graph.clearMarkers()
        mask = self.getSelectionMask().astype("i")

        nspots = mask.max()
        for i in range(1,nspots+1):
            m = (mask==i).astype("f")
            msum=m.sum()
            if msum:
                ny,nx = m.shape
                px= (m.sum(axis=0)*numpy.arange(nx)).sum()/msum
                py= (m.sum(axis=1)*numpy.arange(ny)).sum()/msum

Alessandro Mirone's avatar
Alessandro Mirone committed
118
                print( " ##################################    ", px, py)
119
120
                extra_info = ""
                if a_ids is not None:
121
                    if offset is None:
122
                        extra_info = "(N%d)"% (a_ids.index(i) +1)
123
                    else:
124
                        extra_info = "(N%d,ROI%02d )"% (  a_ids.index(i) +1  , offset+i-1)
125

126
127
128
129
130
131
132
133
134
135
136
137
138
139

                if hasattr( self.graph, "insertMarker") :
                        
                    res=self.graph.insertMarker( px, py, "legend"+str(i)+extra_info, "%d"%(i)+extra_info, color='black', selectable=False, draggable=False, searchFeature=True, 
                                                 xytext = (-20, 0),
                                                 textcoords = 'offset points',
                                                 ha = 'right', va = 'bottom',
                                                 bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.4),
                                                 arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
                else:
                    res=self.graph.addMarker( px, py, "legend"+str(i)+extra_info, "%d"%(i)+extra_info, color='black',
                                              selectable=False, draggable=False, 
                                              symbol="+")

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156




def decode_data( bytearray):
        
    data = []
    item = {}
    
    ds = QtCore.QDataStream(bytearray)
    while not ds.atEnd():
            
        row = ds.readInt32()
        column = ds.readInt32()

        return row, column

Alessandro Mirone's avatar
Alessandro Mirone committed
157
        print( row, column)
158
159
160
161
162
163
164
165
166
167
168
169
170

        map_items = ds.readInt32()
        for i in range(map_items):
               
            key = ds.readInt32()
            
            value = QtCore.QVariant()
            ds >> value
            item[QtCore.Qt.ItemDataRole(key)] = value
               
        data.append(item)
           
    return data
Alessandro Mirone's avatar
Alessandro Mirone committed
171

172

173

174
175
176



177