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
|
#!/usr/bin/env python
#/*##########################################################################
# Copyright (C) 2004-2014 V.A. Sole, European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# 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.
#
#############################################################################*/
__author__ = "V.A. Sole - ESRF Data Analysis"
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
import sys
import os
from PyMca5.PyMcaGui import PyMcaQt as qt
from PyMca5.PyMcaGui import IconDict
from PyMca5 import PyMcaDirs
from PyMca5.PyMcaGui import PyMcaFileDialogs
PyMcaDirs.nativeFileDialogs = False
QTVERSION = qt.qVersion()
HDF5SUPPORT = True
from PyMca5.PyMcaGui.pymca import QDataSource
class SimpleFitBatchParameters(qt.QWidget):
def __init__(self, parent=None, file_browser=True):
qt.QWidget.__init__(self, parent)
self.setWindowTitle("PyMca Simple Fit Batch GUI")
self.setWindowIcon(qt.QIcon(qt.QPixmap(IconDict['gioconda16'])))
self.mainLayout = qt.QGridLayout(self)
self.mainLayout.setContentsMargins(2, 2, 2, 2)
self.mainLayout.setSpacing(2)
self._inputDir = None
self._outputDir = None
self._lastInputFileFilter = None
self._fileList = []
self._build(file_browser)
def _build(self, file_browser):
row = 0
if file_browser:
self._listLabel = qt.QLabel(self)
self._listLabel.setText("Input File list:")
self._listView = qt.QTextEdit(self)
self._listView.setMaximumHeight(30*self._listLabel.sizeHint().height())
self._listButton = qt.QPushButton(self)
self._listButton.setText('Browse')
self._listButton.setAutoDefault(False)
self._listButton.clicked.connect(self.browseList)
self.mainLayout.addWidget(self._listLabel, 0, 0, qt.Qt.AlignTop|qt.Qt.AlignLeft)
self.mainLayout.addWidget(self._listView, 0, 1)
self.mainLayout.addWidget(self._listButton, 0, 2, qt.Qt.AlignTop|qt.Qt.AlignRight)
row += 1
#options
labels = ['Fit Configuration File:', 'Output Directory:']
row0 = 0
for label in labels:
l = qt.QLabel(self)
l.setText(label)
line = qt.QLineEdit(self)
b = qt.QPushButton(self)
b.setText('Browse')
self.mainLayout.addWidget(l, row+row0, 0)
self.mainLayout.addWidget(line, row+row0, 1)
self.mainLayout.addWidget(b, row+row0, 2)
if row0 == 0:
self._fitConfigurationLine = line
self._fitConfigurationButton = b
else:
self._outputDirectoryLine = line
self._outputDirectoryButton = b
row0 += 1
row += row0
self._outputDirectoryButton.clicked.connect( \
self.browseOutputDirectory)
self._fitConfigurationButton.clicked.connect( \
self.browseFitConfiguration)
def browseList(self):
if self._inputDir is None:
self._inputDir = PyMcaDirs.inputDir
elif os.path.exists(self._inputDir):
PyMcaDirs.inputDir = self._inputDir
filetypes = ["Mca Files (*.mca)",
"Edf Files (*.edf)"]
if HDF5SUPPORT:
filetypes.append("HDF5 Files(*.nxs *.h5 *.hdf)")
filetypes.append("SPEC Files (*.spec)")
filetypes.append("SPEC Files (*.dat)")
filetypes.append("All files (*)")
message = "Open a set of files"
mode = "OPEN"
getfilter = True
currentfilter = self._lastInputFileFilter
fileList, fileFilter = PyMcaFileDialogs.getFileList(self,
filetypelist=filetypes,
message=message,
mode=mode,
getfilter=getfilter,
single=False,
currentfilter=currentfilter)
if not len(fileList):
return
else:
self._lastInputFileFilter = fileFilter
self._inputDir = os.path.dirname(fileList[0])
if (QTVERSION < '4.2.0') or (not len(self._fileList)):
self.setFileList(fileList)
self.raise_()
return
msg = qt.QMessageBox()
msg.setWindowTitle("Append or replace")
msg.setIcon(qt.QMessageBox.Information)
msg.setText("Do you want to delete current file list?")
msg.setStandardButtons(qt.QMessageBox.Yes|qt.QMessageBox.No)
answer=msg.exec_()
if answer == qt.QMessageBox.Yes:
append = False
else:
append = True
self.setFileList(fileList, append=append)
self.raise_()
def browseFitConfiguration(self):
if self._inputDir is None:
self._inputDir = PyMcaDirs.inputDir
elif os.path.exists(self._inputDir):
PyMcaDirs.inputDir = self._inputDir
filetypes = ["Configuration Files (*.cfg)"]
if self._inputDir is None:
self._inputDir = PyMcaDirs.inputDir
elif os.path.exists(self._inputDir):
PyMcaDirs.inputDir = self._inputDir
message = "Select a Simple Fit Configuration File"
mode = "OPEN"
getfilter = False
currentfilter = None #self._lastInputFileFilter
fileList = PyMcaFileDialogs.getFileList(self,
filetypelist=filetypes,
message=message,
mode=mode,
getfilter=getfilter,
single=True,
currentfilter=currentfilter)
if not len(fileList):
return
self._inputDir = os.path.dirname(fileList[0])
self.setFitConfigurationFile(fileList[0])
self.raise_()
def browseOutputDirectory(self):
if self._outputDir is None:
self._outputDir = PyMcaDirs.outputDir
elif os.path.exists(self._outputDir):
PyMcaDirs.inputDir = self._outputDir
message = "Select a Simple Fit Configuration File"
mode = "OPEN"
fileList = PyMcaFileDialogs.getExistingDirectory(self,
message=message,
mode=mode)
if not len(fileList):
return
if type(fileList) != type([]):
fileList = [fileList]
self._outputDir = os.path.dirname(fileList[0])
self.setOutputDirectory(fileList[0])
self.raise_()
def setFileList(self, filelist, append=False):
if filelist is None:
filelist = []
if not append:
self._fileList = []
self._listView.clear()
text = ""
for ffile in self._fileList:
text += ffile + "\n"
for ffile in filelist:
if ffile not in self._fileList:
self._fileList.append(ffile)
text += ffile + "\n"
self._listView.insertPlainText(text)
sourceType = QDataSource.getSourceType(self._fileList[0])
dataSourceClass = QDataSource.source_types[sourceType]
dataSourceWidget = QDataSource.source_widgets[sourceType]
self._dataSource = dataSourceClass(self._fileList[0])
self._dataWidget = dataSourceWidget()
self._dataWidget.setDataSource(self._dataSource)
self._dataWidget.sigAddSelection.connect(self.printSelection)
self._dataWidget.show()
def setFitConfigurationFile(self, fname):
self._fitConfigurationLine.setText(fname)
def setOutputDirectory(self, fname):
self._outputDirectoryLine.setText(fname)
def printSelection(self, ddict):
print("Received = ", ddict)
def getParameters(self):
ddict = {}
ddict['selection'] = {}
ddict['selection']['x'] = None
ddict['selection']['y'] = None
ddict['selection']['m'] = None
ddict['filelist'] = self._fileList
ddict['outputdir'] = str(self._outputDirectoryLine.text())
ddict['fitconfiguration'] = str(self._fitConfigurationLine.text())
return ddict
class SimpleFitBatchGui(qt.QWidget):
def __init__(self, parent=None, stack=False, actions=True):
qt.QWidget.__init__(self, parent)
if stack in [None, False]:
file_browser = False
else:
file_browser = True
self.mainLayout = qt.QVBoxLayout(self)
self.mainLayout.setContentsMargins(2, 2, 2, 2)
self.mainLayout.setSpacing(2)
self.parametersWidget = SimpleFitBatchParameters(self)
self.getParameters = self.parametersWidget.getParameters
self.mainLayout.addWidget(self.parametersWidget)
if actions:
self.actionsBox = qt.QWidget(self)
self.actionsBox.mainLayout = qt.QHBoxLayout(self.actionsBox)
self.actionsBox.mainLayout.setContentsMargins(2, 2, 2, 2)
self.actionsBox.mainLayout.setSpacing(2)
self.closeButton = qt.QPushButton(self.actionsBox)
self.closeButton.setText("Close")
self.startButton = qt.QPushButton(self.actionsBox)
self.startButton.setText("Start")
self.actionsBox.mainLayout.addWidget(qt.HorizontalSpacer(self.actionsBox))
self.actionsBox.mainLayout.addWidget(self.closeButton)
self.actionsBox.mainLayout.addWidget(qt.HorizontalSpacer(self.actionsBox))
self.actionsBox.mainLayout.addWidget(self.startButton)
self.actionsBox.mainLayout.addWidget(qt.HorizontalSpacer(self.actionsBox))
self.mainLayout.addWidget(self.actionsBox)
self.closeButton.clicked.connect(self.close)
if __name__ == "__main__":
app = qt.QApplication([])
w = SimpleFitBatchGui()
w.show()
app.exec_()
|