-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLWindow.py
215 lines (159 loc) · 5.64 KB
/
CLWindow.py
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
__author__ = 'Marc de Klerk'
import sys
from PyQt4 import QtCore, QtGui
import pyopencl as cl
import numpy as np
try:
from OpenGL.GL import *
except ImportError:
raise ImportError("Error importing PyOpenGL")
cm = cl.mem_flags
szFloat = np.dtype(np.float32).itemsize
szInt = np.dtype(np.int32).itemsize
class CLWindow(QtGui.QMainWindow):
class CenteredScrollArea(QtGui.QScrollArea):
def __init__(self, parent=None):
QtGui.QScrollArea.__init__(self, parent)
def eventFilter(self, object, event):
if object == self.widget() and event.type() == QtCore.QEvent.Resize:
QtGui.QScrollArea.eventFilter(self, object, event)
else:
QtGui.QScrollArea.eventFilter(self, object, event)
return True
def __init__(self, canvas):
super(CLWindow, self).__init__(None)
self.slider = QtGui.QSlider(QtCore.Qt.Horizontal)
self.slider.setRange(0, 100)
self.slider.setSingleStep(1)
self.slider.setTickInterval(10)
self.slider.valueChanged.connect(self.sigLayerOpacity)
self.layerList = QtGui.QListWidget(self)
self.layerList.setAlternatingRowColors(True)
# self.layerList.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
self.layerList.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.layerList.setDropIndicatorShown(True)
self.layerList.setFocusPolicy(QtCore.Qt.NoFocus)
self.layerList.installEventFilter(self)
self.layerList.itemClicked.connect(self.sigLayerClicked)
self.layerList.itemChanged.connect(self.sigCurrentLayerChanged)
# self.layerList.indexesMoved.connect(self.sigLayerIndexesMoved)
self.canvas = canvas
self.canvas.mousePress = self.mousePress
self.canvas.mouseDrag = self.mouseDrag
self.scrollarea = QtGui.QScrollArea()
#self.scrollarea = self.CenteredScrollArea()
self.scrollarea.setWidget(self.canvas)
self.scrollarea.setAlignment(QtCore.Qt.AlignCenter)
layoutButtons = QtGui.QVBoxLayout()
self.widgetButtons = QtGui.QWidget()
self.widgetButtons.setLayout(layoutButtons)
self.sliderZoom = QtGui.QSlider(QtCore.Qt.Horizontal)
self.sliderZoom.setRange(100, 400)
self.sliderZoom.setSingleStep(1)
self.sliderZoom.setTickInterval(100)
self.sliderZoom.setTickPosition(QtGui.QSlider.TicksBelow)
self.sliderZoom.valueChanged.connect(self.sigZoom)
self.sliderZoom.setValue(100)
layout = QtGui.QGridLayout()
layout.addWidget(self.slider)
layout.addWidget(self.layerList)
layout.addWidget(self.widgetButtons)
layout.addWidget(self.sliderZoom)
side = QtGui.QWidget()
side.setLayout(layout)
splitter = QtGui.QSplitter()
splitter.addWidget(side)
splitter.addWidget(self.scrollarea)
self.setCentralWidget(splitter)
self.installEventFilter(self)
splitter.setSizes([100, self.size().width()-100])
def addButton(self, name, action):
btn = QtGui.QPushButton(name)
self.widgetButtons.layout().addWidget(btn)
btn.clicked.connect(action)
def addViewNp(self, arr, name=None, mem_flags=cm.READ_ONLY, buffer=False):
view = self.addView(arr.shape, name, mem_flags, buffer)
if buffer:
cl.enqueue_copy(self.queue, view, arr).wait()
else:
cl.enqueue_copy(self.queue, view, arr, origin=(0, 0), region=(arr.shape[1], arr.shape[0])).wait()
return view
def setLayerMap(self, name, func):
item = self.layerList.findItems(name, QtCore.Qt.MatchExactly)[0]
item.map = func
def setLayerOpacity(self, name, opacity):
item = self.layerList.findItems(name, QtCore.Qt.MatchExactly)[0]
item.view.opacity = opacity
self.updateCanvas()
def addLayer(self, name, clobj, opacity=None, filters=[]):
layer = self.canvas.addLayer(clobj, opacity, filters=filters)
item = QtGui.QListWidgetItem(name)
item.setText(name)
item.setFlags( QtCore.Qt.ItemIsUserCheckable |
QtCore.Qt.ItemIsDragEnabled |
QtCore.Qt.ItemIsSelectable |
QtCore.Qt.ItemIsEnabled)
item.setCheckState(QtCore.Qt.Checked)
item.layer = layer
self.layerList.addItem(item)
def sigCurrentLayerChanged(self, item):
layer = item.layer
def sigLayerOpacity(self, value):
if self.layerList.currentItem() == None:
return
item = self.layerList.currentItem().layer
item.opacity = float(value)/100
self.updateCanvas()
def sigZoom(self, value):
self.setZoom(value)
def sigLayerClicked(self, item):
checked = item.checkState() == QtCore.Qt.Unchecked
layer = item.layer
if layer.enabled == checked:
layer.enabled = not layer.enabled
self.updateCanvas()
else:
self.slider.setValue(100*layer.opacity)
# def sigLayerIndexesMoved(self):
# del self.canvas.layers[:]
#
# for i in range(self.layerList.count()):
# layer = self.layerList.item(i).layer
# self.canvas.layers.append(layer)
#
# print self.layerList
#
# self.updateCanvas()
def updateCanvas(self):
self.canvas.repaint()
def setCursor(self, cursor):
image = QtGui.QImage(cursor.tostring(), cursor.size[0], cursor.size[1], QtGui.QImage.Format_ARGB32)
pixmap = QtGui.QPixmap(image)
cursor = QtGui.QCursor(pixmap)
self.canvas.setCursor(cursor)
def setZoom(self, zoom):
self.canvas.setZoom(float(zoom)/100)
def setMousePress(self, func):
self.userMousePress = func
def setMouseDrag(self, func):
self.userMouseDrag = func
def setKeyPress(self, func):
self.userKeyPress = func
def mousePress(self, pos):
if hasattr(self, 'userMousePress'):
self.userMousePress(pos)
def mouseDrag(self, pos1, pos2):
if hasattr(self, 'userMouseDrag'):
self.userMouseDrag(pos1, pos2)
def eventFilter(self, object, event):
if hasattr(self, 'userKeyPress') and event.type() == QtCore.QEvent.KeyPress:
self.userKeyPress(event.key())
return True
return False
def drawLayer(self):
for i in range(self.layerList.count()):
layer = self.layerList.item(i)
if layer.map:
layer.map()
else:
pass