-
Notifications
You must be signed in to change notification settings - Fork 1
/
ivigraph.py
284 lines (206 loc) · 9.82 KB
/
ivigraph.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
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
from pyqtgraph.flowchart import Flowchart, Node,FlowchartCtrlWidget
import pyqtgraph.flowchart.library as fclib
from pyqtgraph.flowchart.library.common import CtrlNode
import pyqtgraph.configfile as configfile
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import numpy as np
import scipy.ndimage
import vigra
import os
from views import *
from operator_loader import *
from pyqtgraph.dockarea import *
import types
import __version__
class ImageSelector(QtGui.QWidget):
#__pyqtSignals__ = ("latitudeChanged(double)",
# "longitudeChanged(double)")
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
# current image
self.imageDesc = 'None'
self.currentIndex = 0
self.numInput = None
self.batchInputNames = None
self.ivigraph=None
self.comboBoxInputSelector = QtGui.QComboBox()
#imgItem = [str(i) for i in xrange(100)]
#self.comboBoxInputSelector.addItems(imgItem)
# prev & next image
self.prevImgButton = QtGui.QPushButton('<')
self.nextImgButton = QtGui.QPushButton('>')
self.prevImgButton.setFixedSize(20,30)
self.nextImgButton.setFixedSize(20,30)
layout = QtGui.QVBoxLayout(self)
layoutB = QtGui.QHBoxLayout()
layout.addLayout(layoutB)
layoutB.addWidget(self.comboBoxInputSelector)
layoutB.addWidget(self.prevImgButton)
layoutB.addWidget(self.nextImgButton)
def comboBoxInputSelectorChanged(index):
self.setCurrentIndex(index)
self.comboBoxInputSelector.currentIndexChanged.connect(comboBoxInputSelectorChanged)
def buttonNextImg():
if self.currentIndex+1 < self.numInput:
self.setCurrentIndex(self.currentIndex+1)
self.nextImgButton.released.connect(buttonNextImg)
def buttonPrevImg():
if self.currentIndex>0 :
self.setCurrentIndex(self.currentIndex-1)
self.prevImgButton.released.connect(buttonPrevImg)
def setCurrentIndex(self,newIndex):
self.nextImgButton.setEnabled(newIndex+1<self.numInput)
self.prevImgButton.setEnabled(newIndex>0)
self.currentIndex=newIndex
self.comboBoxInputSelector.setCurrentIndex(newIndex)
self.ivigraph._updateInput(self.currentIndex)
def setBatchInputNames(self,ivigraph,batchInputNames):
names = []
for i,bn in enumerate(batchInputNames):
names.append("Img %s: %s"%(str(i).zfill(3),bn) )
self.ivigraph = ivigraph
self.numInput = len(batchInputNames)
self.batchInputNames = batchInputNames
self.comboBoxInputSelector.clear()
self.comboBoxInputSelector.addItems(names)
class IViGrahp(QtGui.QWidget):
def __init__(self,parent=None,dataIn=None):
self.batchInput=None
self.batchMode=None
QtGui.QWidget.__init__(self, parent)
self.win = QtGui.QMainWindow()
self.win.setWindowTitle("IViGrahp V%s"%__version__.version)
self.win.setCentralWidget(self)
self.win.resize(1000,500)
# ui widgets
self.mylayout = QtGui.QVBoxLayout(self)
self.dockArea = DockArea(self)
self.layout().addWidget(self.dockArea)
#self.addLayout(self.layout)
self.constrollDock = Dock("Controll", size=(1, 1))
self.flowChartDock = Dock("FlowchartDock", size=(1, 1))
self.viewDocks = [ Dock("view0", size=(1, 1)), Dock("view1", size=(1, 1)),Dock("view2", size=(1, 1)), Dock("view3", size=(1, 1)) ]
self.imageSelectorDock = Dock("ImageSelector", size=(1, 1))
# add docks
self.dockArea.addDock(self.constrollDock, 'left')
self.dockArea.addDock(self.viewDocks[0], 'right')
self.dockArea.addDock(self.viewDocks[3], 'below', self.viewDocks[0])
self.dockArea.addDock(self.viewDocks[2], 'below', self.viewDocks[0])
self.dockArea.addDock(self.viewDocks[1], 'below', self.viewDocks[0])
self.dockArea.addDock(self.flowChartDock, 'below', self.viewDocks[0])
self.dockArea.addDock(self.imageSelectorDock,'bottom',self.constrollDock)
# Widgets:
# viewer widgets
self.viewers = [ ClickImageView(),ClickImageView(),
ClickImageView(),ClickImageView()]
# flochart (widget)
self.flowChart = Flowchart(terminals={
'dataIn': {'io': 'in'},
'dataOut': {'io': 'out'}
})
# image selctor widget
self.imgSelector = ImageSelector(parent=self)
self.imageSelectorDock.addWidget(self.imgSelector)
self.flowCharWidget = self.flowChart.widget()
self.flowCharWidget.ui.showChartBtn.hide()
self.flowCharWidget.ui.reloadBtn.hide()
self.constrollDock.addWidget(self.flowCharWidget)
self.flowChartDock.addWidget(self.flowCharWidget.chartWidget)
# add widgets
for viewDock,viewer in zip(self.viewDocks,self.viewers):
viewDock.addWidget(viewer)
# add view nodes
self.viewerNodes = [ self.flowChart.createNode('ImageView', pos=(posX, -150)) for posX in range(0,600,150) ]
for viewerNode,viewer in zip(self.viewerNodes,self.viewers):
viewerNode.setView(viewer)
# fix save and load
self.__fixSaveAndLoad()
def __fixSaveAndLoad(self):
ivigraph = self
def setCurrentFileFixed(fcc, fileName):
fn = str(fileName)
if fn.endswith('.fc')==False:
fn+='.fc'
fcc.currentFileName = fn
if fn is None:
fcc.ui.fileNameLabel.setText("<b>[ new ]</b>")
else:
fcc.ui.fileNameLabel.setText("<b>%s</b>" % os.path.split(str(fcc.currentFileName))[1])
fcc.resizeEvent(None)
def saveFile(self, fileName=None, startDir=None, suggestedFileName='flowchart.fc'):
if fileName is not None:
fileName = str(fileName)
if fileName.endswith('.fc')==False:
fileName+='.fc'
if fileName is None:
if startDir is None:
startDir = self.filePath
if startDir is None:
startDir = '.'
self.fileDialog = pg.FileDialog(None, "Save Flowchart..", startDir, "Flowchart (*.fc)")
#self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
#self.fileDialog.setDirectory(startDir)
self.fileDialog.show()
self.fileDialog.fileSelected.connect(self.saveFile)
return
#fileName = QtGui.QFileDialog.getSaveFileName(None, "Save Flowchart..", startDir, "Flowchart (*.fc)")
configfile.writeConfigFile(self.saveState(), fileName)
self.sigFileSaved.emit(fileName)
FlowchartCtrlWidget.setCurrentFile=setCurrentFileFixed
ivigraph.flowChart.saveFile=types.MethodType(saveFile,ivigraph.flowChart)
def loadFile(fc, fileName=None, startDir=None):#,nodes=(v1Node,v2Node,v3Node,v4Node),viewers=viewers):
#print "my file load",type(fc)
if isinstance(fc,FlowchartCtrlWidget):
return
import pyqtgraph.configfile as configfile
if fileName is None:
if startDir is None:
startDir = fc.filePath
if startDir is None:
startDir = '.'
fc.fileDialog = pg.FileDialog(None, "Load Flowchart..", startDir, "Flowchart (*.fc)")
#self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
#self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
fc.fileDialog.show()
fc.fileDialog.fileSelected.connect(fc.loadFile)
return
## NOTE: was previously using a real widget for the file dialog's parent, but this caused weird mouse event bugs..
#fileName = QtGui.QFileDialog.getOpenFileName(None, "Load Flowchart..", startDir, "Flowchart (*.fc)")
fileName = str(fileName)
state = configfile.readConfigFile(fileName)
#print "type..",type(fc)
fc.restoreState(state, clear=True)
fc.viewBox.autoRange()
#fc.emit(QtCore.SIGNAL('fileLoaded'), fileName)
fc.sigFileLoaded.emit(fileName)
for name, node in fc._nodes.items():
#print name
if isinstance(node,ivigraph.viewerNodes[0].__class__):
if name =='ImageView.0':
node.setView(ivigraph.viewers[0])
if name =='ImageView.1':
node.setView(ivigraph.viewers[1])
if name =='ImageView.2':
node.setView(ivigraph.viewers[2])
if name =='ImageView.3':
node.setView(ivigraph.viewers[3])
fc.inputNode.update()
ivigraph.flowChart.loadFile=types.MethodType(loadFile,ivigraph.flowChart)
def _updateInput(self,index):
data = vigra.readImage(self.batchInput[index])
self.flowChart.setInput(dataIn=data)
def setInput(self,**kwargs):
self.flowChart.setInput(**kwargs)
self.batchMode = False
self.imageSelectorDock.hide()
def setBatchInput(self,folder,fFilter,inputName='imageIn'):
self.batchInput = [ folder + f for f in os.listdir(folder) if f.endswith(fFilter)]
self.batchInputNames = [ f for f in os.listdir(folder) if f.endswith(fFilter)]
#for fn in self.batchInput:
# print fn
self.batchMode = True
self.imgSelector.setBatchInputNames(self,self.batchInputNames)
self.imgSelector.setCurrentIndex(0)
self.imageSelectorDock.show()