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
|
#/*##########################################################################
# Copyright (C) 2004-2022 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF.
#
# 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"
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
import sys
import os
import logging
_logger = logging.getLogger(__name__)
from PyMca5.PyMcaGui import PyMcaQt as qt
QTVERSION = qt.qVersion()
from PyMca5.PyMcaGui.plotting import PyMca_Icons as icons
from PyMca5.PyMcaIO import spswrap as sps
from PyMca5 import PyMcaDirs
from PyMca5.PyMcaGui.io import PyMcaFileDialogs
BLISS = False
if sys.version_info > (3, 5):
try:
from PyMca5.PyMcaCore import RedisTools
BLISS = True
except:
_logger.info("Bliss data file direct support not available")
class QSourceSelector(qt.QWidget):
sigSourceSelectorSignal = qt.pyqtSignal(object)
def __init__(self, parent=None, filetypelist=None, pluginsIcon=False):
qt.QWidget.__init__(self, parent)
self.mainLayout= qt.QVBoxLayout(self)
self.mainLayout.setContentsMargins(0, 0, 0, 0)
self.mainLayout.setSpacing(0)
if filetypelist is None:
self.fileTypeList = ["Spec Files (*mca)",
"Spec Files (*dat)",
"Spec Files (*spec)",
"SPE Files (*SPE)",
"EDF Files (*edf)",
"EDF Files (*ccd)",
"CSV Files (*csv)",
"All Files (*)"]
else:
self.fileTypeList = filetypelist
self.lastFileFilter = self.fileTypeList[0]
# --- file combo/open/close
self.lastInputDir = PyMcaDirs.inputDir
self.fileWidget= qt.QWidget(self)
fileWidgetLayout= qt.QHBoxLayout(self.fileWidget)
fileWidgetLayout.setContentsMargins(0, 0, 0, 0)
fileWidgetLayout.setSpacing(0)
self.fileCombo = qt.QComboBox(self.fileWidget)
self.fileCombo.setEditable(0)
self.mapCombo= {}
openButton= qt.QPushButton(self.fileWidget)
self.openIcon = qt.QIcon(qt.QPixmap(icons.IconDict["fileopen"]))
self.closeIcon = qt.QIcon(qt.QPixmap(icons.IconDict["close"]))
self.reloadIcon = qt.QIcon(qt.QPixmap(icons.IconDict["reload"]))
if BLISS:
self.specIcon = qt.QIcon(qt.QPixmap(icons.IconDict["bliss"]))
else:
self.specIcon = qt.QIcon(qt.QPixmap(icons.IconDict["spec"]))
openButton.setIcon(self.openIcon)
openButton.setSizePolicy(qt.QSizePolicy(qt.QSizePolicy.Fixed, qt.QSizePolicy.Minimum))
openButton.setToolTip("Open new file data source")
closeButton= qt.QPushButton(self.fileWidget)
closeButton.setIcon(self.closeIcon)
closeButton.setToolTip("Close current data source")
refreshButton= qt.QPushButton(self.fileWidget)
refreshButton.setIcon(self.reloadIcon)
refreshButton.setToolTip("Refresh data source")
specButton= qt.QPushButton(self.fileWidget)
specButton.setIcon(self.specIcon)
if BLISS:
specButton.setToolTip("Open data acquisition source")
else:
specButton.setToolTip("Open new shared memory source")
closeButton.setSizePolicy(qt.QSizePolicy(qt.QSizePolicy.Fixed, qt.QSizePolicy.Minimum))
specButton.setSizePolicy(qt.QSizePolicy(qt.QSizePolicy.Fixed, qt.QSizePolicy.Minimum))
refreshButton.setSizePolicy(qt.QSizePolicy(qt.QSizePolicy.Fixed, qt.QSizePolicy.Minimum))
openButton.clicked.connect(self._openFileSlot)
closeButton.clicked.connect(self.closeFile)
refreshButton.clicked.connect(self._reload)
specButton.clicked.connect(self.openBlissOrSpec)
if hasattr(self.fileCombo, "textActivated"):
self.fileCombo.textActivated[str].connect(self._fileSelection)
else:
_logger.debug("Using deprecated signal")
self.fileCombo.activated[str].connect(self._fileSelection)
fileWidgetLayout.addWidget(self.fileCombo)
fileWidgetLayout.addWidget(openButton)
fileWidgetLayout.addWidget(closeButton)
fileWidgetLayout.addWidget(specButton)
if sys.platform == "win32":specButton.hide()
fileWidgetLayout.addWidget(refreshButton)
self.specButton = specButton
if pluginsIcon:
self.pluginsButton = qt.QPushButton(self.fileWidget)
self.pluginsButton.setIcon(qt.QIcon(qt.QPixmap(icons.IconDict["plugin"])))
self.pluginsButton.setToolTip("Plugin handling")
self.pluginsButton.setSizePolicy(qt.QSizePolicy(qt.QSizePolicy.Fixed, qt.QSizePolicy.Minimum))
fileWidgetLayout.addWidget(self.pluginsButton)
self.mainLayout.addWidget(self.fileWidget)
def _reload(self):
_logger.debug("_reload called")
qstring = self.fileCombo.currentText()
if not len(qstring):
return
key = qt.safe_str(qstring)
ddict = {}
ddict["event"] = "SourceReloaded"
ddict["combokey"] = key
ddict["sourcelist"] = self.mapCombo[key] * 1
self.sigSourceSelectorSignal.emit(ddict)
def _openFileSlot(self):
self.openFile(None, None)
def openSource(self, sourcename, specsession=None):
if specsession is None:
if sourcename in sps.getspeclist():
specsession=True
elif BLISS and sourcename in RedisTools.get_sessions_list():
specsession = "bliss"
else:
specsession=False
self.openFile(sourcename, specsession=specsession)
def openFile(self, filename=None, justloaded=None, specsession=False):
_logger.debug("openfile = %s", filename)
staticDialog = False
if specsession == "bliss":
specsession = False
session = filename
node = RedisTools.get_node(session)
if not node:
txt = "No REDIS information retrieved from session %s" % \
session
raise IOError(txt)
filename = RedisTools.get_session_filename(node)
if not len(filename):
txt = "Cannot retrieve last output filename from session %s" % \
session
raise IOError(txt)
if not os.path.exists(filename):
txt = "Last output file <%s> does not exist" % filename
raise IOError(txt)
filename = [filename]
key = os.path.basename(filename[0])
try:
self._emitSourceSelectedOrReloaded(filename, key)
except:
_logger.error("Problem opening %s" % filename[0])
key = "%s" % session
self._emitSourceSelectedOrReloaded([session], key)
return
if not specsession:
if justloaded is None:
justloaded = True
if filename is None:
if self.lastInputDir is not None:
if not os.path.exists(self.lastInputDir):
self.lastInputDir = None
wdir = self.lastInputDir
filelist, fileFilter = PyMcaFileDialogs.getFileList(self,
filetypelist=self.fileTypeList,
message="Open a new source file",
currentdir=wdir,
mode="OPEN",
getfilter=True,
single=False,
currentfilter=self.lastFileFilter)
if not len(filelist):
return
if not len(filelist[0]):
return
filename=[]
for f in filelist:
filename.append(qt.safe_str(f))
if not len(filename):
return
if len(filename):
self.lastInputDir = os.path.dirname(filename[0])
PyMcaDirs.inputDir = os.path.dirname(filename[0])
self.lastFileFilter = fileFilter
justloaded = True
if justloaded:
if type(filename) != type([]):
filename = [filename]
if not os.path.exists(filename[0]):
if '%' not in filename[0]:
raise IOError("File %s does not exist" % filename[0])
#check if it is a stack
if len(filename) > 1:
key = "STACK from %s to %s" % (filename[0], filename[-1])
else:
key = os.path.basename(filename[0])
else:
key = filename
if key not in sps.getspeclist():
qt.QMessageBox.critical(self,
"SPS Error",
"No shared memory source named %s" % key)
return
self._emitSourceSelectedOrReloaded(filename, key)
def _emitSourceSelectedOrReloaded(self, filename, key):
ddict = {}
ddict["event"] = "NewSourceSelected"
if key in self.mapCombo.keys():
if self.mapCombo[key] == filename:
#Reloaded event
ddict["event"] = "SourceReloaded"
else:
i = 0
while key in self.mapCombo.keys():
key += "_%d" % i
ddict["combokey"] = key
ddict["sourcelist"] = filename
self.mapCombo[key] = filename
if ddict["event"] =="NewSourceSelected":
nitems = self.fileCombo.count()
self.fileCombo.insertItem(nitems, key)
self.fileCombo.setCurrentIndex(nitems)
else:
if hasattr(qt, "QString"):
nitem = self.fileCombo.findText(qt.QString(key))
else:
nitem = self.fileCombo.findText(key)
self.fileCombo.setCurrentIndex(nitem)
self.sigSourceSelectorSignal.emit(ddict)
def closeFile(self):
_logger.debug("closeFile called")
#get current combobox key
qstring = self.fileCombo.currentText()
if not len(qstring):
return
key = qt.safe_str(qstring)
ddict = {}
ddict["event"] = "SourceClosed"
ddict["combokey"] = key
ddict["sourcelist"] = self.mapCombo[key] * 1
if hasattr(qt, "QString"):
nitem = self.fileCombo.findText(qt.QString(key))
else:
nitem = self.fileCombo.findText(key)
self.fileCombo.removeItem(nitem)
del self.mapCombo[key]
self.sigSourceSelectorSignal.emit(ddict)
def openBlissOrSpec(self):
if not BLISS:
return self.openSpec()
sessionList = RedisTools.get_sessions_list()
if not len(sessionList):
return self.openSpec()
activeList = []
for session in sessionList:
node = RedisTools.get_node(session)
if node:
activeList.append(session)
if not len(activeList):
_logger.info("Bliss sessions found but no info in REDIS")
return self.openSpec()
sessionList = activeList
menu = qt.QMenu()
for session in sessionList:
if hasattr(qt, "QString"):
menu.addAction(qt.QString(session),
lambda i=session:self.openFile(i, specsession="bliss"))
else:
menu.addAction(session,
lambda i=session:self.openFile(i, specsession="bliss"))
menu.exec(self.cursor().pos())
def openSpec(self):
speclist = sps.getspeclist()
if not len(speclist):
qt.QMessageBox.information(self,
"No SPEC Shared Memory or Bliss session in REDIS Found",
"No shared memory source available")
return
menu = qt.QMenu()
for spec in speclist:
if hasattr(qt, "QString"):
menu.addAction(qt.QString(spec),
lambda i=spec:self.openFile(i, specsession=True))
else:
menu.addAction(spec,
lambda i=spec:self.openFile(i, specsession=True))
menu.exec(self.cursor().pos())
def _fileSelection(self, qstring):
_logger.debug("file selected %s", qstring)
key = str(qstring)
ddict = {}
ddict["event"] = "SourceSelected"
ddict["combokey"] = key
ddict["sourcelist"] = self.mapCombo[key]
self.sigSourceSelectorSignal.emit(ddict)
def test():
a = qt.QApplication(sys.argv)
#new access
from PyMca5.PyMcaGui.pymca import QDataSource
w= QSourceSelector()
def mySlot(ddict):
print(ddict)
if ddict["event"] == "NewSourceSelected":
d = QDataSource.QDataSource(ddict["sourcelist"][0])
w.specfileWidget.setDataSource(d)
w.sigSourceSelectorSignal.connect(mySlot)
a.lastWindowClosed.connect(a.quit)
w.show()
a.exec()
if __name__=="__main__":
test()
|