summaryrefslogtreecommitdiff
path: root/binoculars-processgui.py
blob: 865d83e53fbc1a528ec41db3ea9d2ffacd38e84b (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
#!/usr/bin/env python
"""
binoculars gui for data processing
Created on 2015-06-04
author: Remy Nencib (remy.nencib@esrf.r)
"""

import sys
import os
import glob
import traceback
from PyQt4 import QtGui, QtCore, Qt
import sys,os 
import itertools
import inspect
import glob
import binoculars.util, binoculars.main
import time

#--------------------------------------------CREATE MAIN WINDOW----------------------------------------
class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.initUI()
        self.tab_widget = QtGui.QTabWidget(self)
        self.setCentralWidget(self.tab_widget)
        #add the close button for tabs
        close = self.tab_widget.setTabsClosable(True)
        self.tab_widget.tabCloseRequested.connect(self.close_tab)

    #method for close tabs
    def close_tab(self, tab):
        self.tab_widget.removeTab(tab)

    def initUI(self):
        #we create the menu bar
        openFile = QtGui.QAction('Open', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip('Open new File')
        openFile.triggered.connect(self.ShowFile)

        saveFile = QtGui.QAction('Save', self)
        saveFile.setShortcut('Ctrl+S')
        saveFile.setStatusTip('Save File')
        saveFile.triggered.connect(self.Save)

        Create = QtGui.QAction('Create', self)
        Create.setStatusTip('Create Configfile')
        Create.triggered.connect(self.New_Config)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(openFile)
        fileMenu.addAction(saveFile)
        fileMenu = menubar.addMenu('&New Configfile')
        fileMenu.addAction(Create)

        #we configue the main windows
        palette = QtGui.QPalette()
        palette.setColor(QtGui.QPalette.Background,QtCore.Qt.gray)
        self.setPalette(palette)
        self.setGeometry(50, 100,700,700)
        self.setWindowTitle('Binoculars processgui')
        self.show()

        self.ListCommand = QtGui.QTableWidget(1, 2, self)
        self.ListCommand.verticalHeader().setVisible(True)
        self.ListCommand.horizontalHeader().setVisible(False)
        self.ListCommand.horizontalHeader().stretchSectionCount()
        self.ListCommand.setColumnWidth(0, 80)
        self.ListCommand.setColumnWidth(1, 80)
        self.ListCommand.setRowCount(0)
        self.buttonDelete = QtGui.QPushButton('Delete',self)
        self.connect(self.buttonDelete, QtCore.SIGNAL("clicked()"), self.removeConf)
        self.process = QtGui.QPushButton('run',self)
        self.process.setStyleSheet("background-color: darkred")
        self.connect(self.process, QtCore.SIGNAL("clicked()"),self.run)

        self.wid = QtGui.QWidget()
        self.CommandLayout = QtGui.QVBoxLayout()
        self.CommandLayout.addWidget(self.ListCommand)
        self.CommandLayout.addWidget(self.process)
        self.CommandLayout.addWidget(self.buttonDelete)
        self.wid.setLayout(self.CommandLayout)

        self.Dock = QtGui.QDockWidget()
        self.Dock.setAllowedAreas( QtCore.Qt.LeftDockWidgetArea)
        self.Dock.setFeatures(QtGui.QDockWidget.NoDockWidgetFeatures)
        self.Dock.setWidget(self.wid)
        self.Dock.setMaximumWidth(200)
        self.Dock.setMinimumWidth(200)
        self.addDockWidget(QtCore.Qt.DockWidgetArea(1),self.Dock)
    
    def removeConf(self):
        self.ListCommand.removeRow(self.ListCommand.currentRow()) 
        
    def Add_To_Liste(self,(command, cfg)):
        row = self.ListCommand.rowCount()
        index = self.tab_widget.currentIndex()
        filename = self.tab_widget.tabText(index)
        self.ListCommand.insertRow(self.ListCommand.rowCount())
        dic = {filename:cfg}
        self.item1 = QtGui.QTableWidgetItem(str(command))
        self.item1.command = command
        self.item2 = QtGui.QTableWidgetItem(str(filename))
        self.item2.cfg = dic[filename]
        self.ListCommand.setItem(row, 0, self.item1)
        self.ListCommand.setItem(row, 1, self.item2)
        
    #We run the script and create a hdf5 file            
    def run(self):
        maximum = self.ListCommand.rowCount()
        pd = QtGui.QProgressDialog('running', 'Cancel', 0, maximum, self)
        pd.setWindowModality(QtCore.Qt.WindowModal)
        pd.show()
        def progress(cfg, command):
            if pd.wasCanceled(): 
                raise KeyboardInterrupt
            QtGui.QApplication.processEvents()
            return binoculars.main.Main.from_object(cfg, command)
        try:
            for index in range(self.ListCommand.rowCount()):
                pd.setValue(index)
                cfg = self.ListCommand.item(index,1).cfg
                command = self.ListCommand.item(index,0).command
                print cfg
                progress(cfg, command)
            self.ListCommand.clear()
            self.ListCommand.setRowCount(0)
        except BaseException, e: 
                #cfg = self.ListCommand.item(index,1).cfg
                #print cfg
                QtGui.QMessageBox.about(self,"Error","There was an error processing one of the scans: {0}".format(e))
                
        finally:
                pd.close()

    #we call the load function
    def ShowFile(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '')
        confwidget = Conf_Tab(self)
        confwidget.read_data(str(filename))
        newIndex =  self.tab_widget.addTab(confwidget, os.path.basename(str(filename)))
        QtCore.QObject.connect(confwidget, QtCore.SIGNAL("command"),self.Add_To_Liste)
        self.tab_widget.setCurrentIndex(newIndex)

    #we call the save function
    def Save(self):
        filename = QtGui.QFileDialog().getSaveFileName(self, 'Save', '', '*.txt')
        widget = self.tab_widget.currentWidget() 
        widget.save(filename) 

    #we call the new tab conf   
    def New_Config(self):
        widget = Conf_Tab(self) 
        self.tab_widget.addTab(widget,'New configfile') 
        QtCore.QObject.connect(widget, QtCore.SIGNAL("command"),self.Add_To_Liste)

#----------------------------------------------------------------------------------------------------
#-----------------------------------------CREATE TABLE-----------------------------------------------
class Table(QtGui.QWidget):
    def __init__(self, label,  parent = None):
        super(Table, self).__init__()
        
        # create a QTableWidget
        self.table = QtGui.QTableWidget(1, 2, self)
        self.table.setHorizontalHeaderLabels(['Parameter', 'Value','Comment'])
        self.table.horizontalHeader().setStretchLastSection(True)
        self.table.verticalHeader().setVisible(False)
        self.table.setTextElideMode(QtCore.Qt.ElideLeft)
        #create combobox
        self.combobox = QtGui.QComboBox()
        #add items
        self.cell = QtGui.QTableWidgetItem(QtCore.QString("type"))
        self.table.setItem(0, 0,self.cell)
        self.table.setCellWidget(0, 1, self.combobox)
        #we create pushbuttons and we call the method when we clic on 
        self.btn_add_row = QtGui.QPushButton('+', self)
        self.connect(self.btn_add_row, QtCore.SIGNAL('clicked()'), self.add_row)
        self.buttonRemove = QtGui.QPushButton('-',self)
        self.connect(self.buttonRemove, QtCore.SIGNAL("clicked()"), self.remove)
        #the dispositon of the table and the butttons

        vbox = QtGui.QVBoxLayout()
        hbox = QtGui.QHBoxLayout()

        hbox.addWidget(self.btn_add_row)
        hbox.addWidget(self.buttonRemove)

        vbox.addWidget(label)
        vbox.addLayout(hbox)
        vbox.addWidget(self.table)
        self.setLayout(vbox)

    def add_row(self):
        self.table.insertRow(self.table.rowCount())
    
    def remove(self):
        self.table.removeRow(self.table.currentRow()) 

    def get_keys(self):
        return list(str(self.table.item(index,0).text()) for index in range(self.table.rowCount())) 

    #Here we take all values from tables
    def getParam(self):
        for index in range(self.table.rowCount()):
            if not self.table.item == None:                
                key = str(self.table.item(index,0).text())
                comment = str(self.table.item(index,0).toolTip())
                if index == 0:
                    yield key, str(self.table.cellWidget(index, 1).currentText()), comment
                elif self.table.item(index,1):
                    if len(str(self.table.item(index,1).text())) != 0 and self.table.item(index,0).textColor() == QtGui.QColor('black'):
                        yield key, str(self.table.item(index, 1).text()), comment

    #Here we put all values in tables   
    def addData(self, cfg):
        for item in cfg:
            if item == 'type':
                box = self.table.cellWidget(0,1)
                value = cfg[item].split(':') 
                if len(value) > 1 :
                    box.setCurrentIndex(box.findText(value[1], QtCore.Qt.MatchFixedString))
                else:
                        box.setCurrentIndex(box.findText(cfg[item], QtCore.Qt.MatchFixedString))
            elif item not in self.get_keys():
                self.add_row()
                row = self.table.rowCount()
                for col in range(self.table.columnCount()):
                    if col == 0:
                        newitem = QtGui.QTableWidgetItem(item)
                        self.table.setItem(row -1, col, newitem)   
                    if col == 1:
                        newitem2 = QtGui.QTableWidgetItem(cfg[item])
                        self.table.setItem(row -1, col, newitem2)
            else:
                index = self.get_keys().index(item)
                self.table.item(index, 1).setText(cfg[item])   

                      
    def addDataConf(self, options):
        keys = self.get_keys()
        newconfigs = dict((option[0], '') for option in options if option[0] not in keys)
        self.addData(newconfigs)

        names = list(option[0] for option in options)

        for index, key in enumerate(self.get_keys()):
            if str(key) in names:
                self.table.item(index, 0).setTextColor(QtGui.QColor('black'))
                self.table.item(index, 0).setToolTip(options[names.index(key)][1])
            elif str(key) == 'type':
                self.table.item(index, 0).setTextColor(QtGui.QColor('black'))
            else:
                self.table.item(index, 0).setTextColor(QtGui.QColor('gray'))

    def add_to_combo(self, items):
        self.combobox.clear()
        self.combobox.addItems(items)
    
#----------------------------------------------------------------------------------------------------
#-----------------------------------------CREATE CONFIG----------------------------------------------
class Conf_Tab(QtGui.QWidget):
    def __init__(self, parent = None):

        super(Conf_Tab,self).__init__()
        #we create 3 tables
        self.Dis = Table(QtGui.QLabel('<strong>Dispatcher :</strong>'))
        self.Inp = Table(QtGui.QLabel('<strong>Input :</strong>'))
        self.Pro = Table(QtGui.QLabel('<strong>Projection :<strong>'))
        self.select = QtGui.QComboBox()
        backends = list(backend.lower() for backend in binoculars.util.get_backends())
        #we add the list of different backends on the select combobox
        self.select.addItems(QtCore.QStringList(backends))
        self.add = QtGui.QPushButton('add')
        self.connect(self.add, QtCore.SIGNAL("clicked()"), self.AddCommand)
        self.scan = QtGui.QLineEdit()
        self.scan.setToolTip('scan selection example: 820 824')

        vbox = QtGui.QVBoxLayout()
        hbox = QtGui.QHBoxLayout() 
        splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
        splitter.addWidget(self.Dis)
        splitter.addWidget(self.Inp)
        splitter.addWidget(self.Pro)
        hbox.addWidget(splitter) 

        commandbox = QtGui.QHBoxLayout() 
        commandbox.addWidget(self.add)
        commandbox.addWidget(self.scan)

        vbox.addWidget(self.select)
        vbox.addLayout(hbox)
        vbox.addLayout(commandbox)

        #the dispositon of all elements of the gui
        #Layout = QtGui.QGridLayout()
        #Layout.addWidget(label1,1,1,1,2)
        #Layout.addWidget(label2,1,0,1,2)
        #Layout.addWidget(label3,1,2,1,2)
        #Layout.addWidget(self.select,0,0)
        #Layout.addWidget(self.Dis,2,1)
        #Layout.addWidget(self.Inp,2,0)
        #Layout.addWidget(self.Pro,2,2) 
        #Layout.addWidget(self.add,3,0)
        #Layout.addWidget(self.scan,3,1)
        self.setLayout(vbox)
        
        #Here we call all methods for selected an ellement on differents combobox 
        self.Dis.add_to_combo(QtCore.QStringList(binoculars.util.get_dispatchers()))
        self.select.activated['QString'].connect(self.DataCombo)
        self.Inp.combobox.activated.connect(self.DataTableInp)
        self.Pro.combobox.activated.connect(self.DataTableInpPro)
        self.Dis.combobox.activated.connect(self.DataTableInpDis) 

    def DataCombo(self,text):
        self.Inp.add_to_combo(QtCore.QStringList(binoculars.util.get_inputs(str(text))))
        self.Pro.add_to_combo(QtCore.QStringList(binoculars.util.get_projections(str(text))))
        self.DataTableInp()
        self.DataTableInpPro()
        self.DataTableInpDis()
      
    def DataTableInp(self):
        backend = str(self.select.currentText())
        inp = binoculars.util.get_input_configkeys(backend, str(self.Inp.combobox.currentText()))
        self.Inp.addDataConf(inp)

    def DataTableInpPro(self):
        backend = str(self.select.currentText())
        proj = binoculars.util.get_projection_configkeys(backend, str(self.Pro.combobox.currentText()))
        self.Pro.addDataConf(proj)

    def DataTableInpDis(self):
        backend = str(self.select.currentText())
        disp = binoculars.util.get_dispatcher_configkeys(str(self.Dis.combobox.currentText()))
        self.Dis.addDataConf(disp)

    #The save method we take all ellements on tables and we put them in this format {0} = {1} #{2}
    def save(self, filename): 
        with open(filename, 'w') as fp:
            fp.write('[dispatcher]\n')
            # cycles over the iterator object
            for key, value, comment in self.Dis.getParam():
                fp.write('{0} = {1} #{2}\n'.format(key, value, comment))
            fp.write('[input]\n')
            for key, value, comment in self.Inp.getParam():
                if key == 'type':
                    value = '{0}:{1}'.format(self.select.currentText(),value)
                fp.write('{0} = {1} #{2}\n'.format(key, value, comment))
            fp.write('[projection]\n')
            for key, value, comment in self.Pro.getParam():
                if key == 'type':
                    value = '{0}:{1}'.format(self.select.currentText(),value)
                fp.write('{0} = {1} #{2}\n'.format(key, value, comment))

    #This method take the name of objects and values for run the script
    def get_configobj(self):

        inInp = {}
        inDis = {}
        inPro = {}

        inDis = dict((key, value) for key, value, comment in self.Dis.getParam())

        for key, value, comment in self.Inp.getParam():
            if key == 'type':
                value = '{0}:{1}'.format(str(self.select.currentText()).strip(),value)
            inInp[key] = value  

        for key, value, comment in self.Pro.getParam():
            if key == 'type':
                value = '{0}:{1}'.format(str(self.select.currentText()).strip(),value)
            inPro[key] = value

        cfg = binoculars.util.ConfigFile('processgui {0}'.format(time.strftime('%d %b %Y %H:%M:%S', time.localtime())))
        setattr(cfg, 'input', inInp)
        setattr(cfg, 'dispatcher', inDis)
        setattr(cfg, 'projection', inPro)
        return cfg

    #This method take elements on a text file or the binocular script and put them on tables
    def read_data(self, filename):
        cfg = binoculars.util.ConfigFile.fromtxtfile(str(filename))    
        input_type = cfg.input['type']
        backend, value = input_type.strip(' ').split(':')
        self.select.setCurrentIndex(self.select.findText(backend, QtCore.Qt.MatchFixedString))
        self.DataCombo(backend)
        self.Dis.addData(cfg.dispatcher)
        self.Inp.addData(cfg.input)
        self.Pro.addData(cfg.projection)

    #we add command on the DockWidget
    def AddCommand(self):
        scan = [str(self.scan.text())]
        cfg = self.get_configobj()
        commandconfig = (scan , cfg)
        self.emit(QtCore.SIGNAL('command'), commandconfig)
        
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    main = Window()
    main.show()

    sys.exit(app.exec_())