/usr/share/pyshared/PyMca/PyMcaFileDialogs.py is in pymca 4.5.0-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import PyMcaQt as qt
import PyMcaDirs
import os
QTVERSION = qt.qVersion()
def getExistingDirectory(parent=None, message=None, mode=None):
if message is None:
message = "Please select a directory"
if mode is None:
mode = "OPEN"
else:
mode = mode.upper()
if mode == "OPEN":
wdir = PyMcaDirs.inputDir
else:
wdir = PyMcaDirs.outputDir
if PyMcaDirs.nativeFileDialogs:
outdir=str(qt.QFileDialog.getExistingDirectory(parent,
message,
wdir))
else:
outfile = qt.QFileDialog(parent)
outfile.setWindowTitle("Output Directory Selection")
outfile.setModal(1)
outfile.setDirectory(wdir)
outfile.setFileMode(outfile.DirectoryOnly)
ret = outfile.exec_()
if ret:
outdir=str(outfile.selectedFiles()[0])
else:
outdir = ""
outfile.close()
del outfile
if len(outdir):
if mode == "OPEN":
PyMcaDirs.inputDir = os.path.dirname(outdir)
if PyMcaDirs.outputDir is None:
PyMcaDirs.outputDir = os.path.dirname(outdir)
else:
PyMcaDirs.outputDir = os.path.dirname(outdir)
if PyMcaDirs.inputDir is None:
PyMcaDirs.inputDir = os.path.dirname(outdir)
return outdir
def getFileList(parent=None, filetypelist=None, message=None,
mode=None, getfilter=None, single=False, currentfilter=None):
if filetypelist is None:
fileTypeList = ['All Files (*)']
else:
fileTypeList = filetypelist
if currentfilter is None:
currentfilter = filetypelist[0]
if message is None:
if single:
message = "Please select one file"
else:
message = "Please select one or more files"
if mode is None:
mode = "OPEN"
else:
mode = mode.upper()
if mode == "OPEN":
wdir = PyMcaDirs.inputDir
else:
wdir = PyMcaDirs.outputDir
if getfilter is None:
getfilter = False
if getfilter:
if QTVERSION < '4.5.1':
native_possible = False
else:
native_possible = True
else:
native_possible = True
filterused = None
if native_possible and PyMcaDirs.nativeFileDialogs:
filetypes = currentfilter+"\n"
for filetype in fileTypeList:
if filetype != currentfilter:
filetypes += filetype+"\n"
if getfilter:
if mode == "OPEN":
if single:
filelist, filterused = qt.QFileDialog.getOpenFileNameAndFilter(parent,
message,
wdir,
filetypes)
filelist =[filelist]
else:
filelist, filterused = qt.QFileDialog.getOpenFileNamesAndFilter(parent,
message,
wdir,
filetypes)
filterused = str(filterused)
else:
filelist = qt.QFileDialog.getSaveFileNameAndFilter(parent,
message,
wdir,
filetypes)
if len(filelist[0]):
filterused = str(filelist[1])
filelist=[filelist[0]]
else:
filelist = []
else:
if mode == "OPEN":
if single:
filelist = [qt.QFileDialog.getOpenFileName(parent,
message,
wdir,
filetypes)]
else:
filelist = qt.QFileDialog.getOpenFileNames(parent,
message,
wdir,
filetypes)
else:
filelist = qt.QFileDialog.getSaveFileName(parent,
message,
wdir,
filetypes)
filelist = str(filelist)
if len(filelist):
filelist = [filelist]
else:
filelist = []
if not len(filelist):
if getfilter:
return [], filterused
else:
return []
else:
sample = str(filelist[0])
for filetype in fileTypeList:
ftype = filetype.replace("(", "")
ftype = ftype.replace(")", "")
extensions = ftype.split()[2:]
for extension in extensions:
if sample.endswith(extension[-3:]):
filterused = filetype
break
else:
fdialog = qt.QFileDialog(parent)
fdialog.setModal(True)
fdialog.setWindowTitle(message)
if hasattr(qt, "QStringList"):
strlist = qt.QStringList()
else:
strlist = []
strlist.append(currentfilter)
for filetype in fileTypeList:
if filetype != currentfilter:
strlist.append(filetype)
fdialog.setFilters(strlist)
if mode == "OPEN":
fdialog.setFileMode(fdialog.ExistingFiles)
else:
fdialog.setAcceptMode(fdialog.AcceptSave)
fdialog.setFileMode(fdialog.AnyFile)
fdialog.setDirectory(wdir)
if QTVERSION > '4.3.0':
history = fdialog.history()
if len(history) > 6:
fdialog.setHistory(history[-6:])
ret = fdialog.exec_()
if ret != qt.QDialog.Accepted:
fdialog.close()
del fdialog
if getfilter:
return [], filterused
else:
return []
else:
filelist = fdialog.selectedFiles()
if single:
filelist = [filelist[0]]
filterused = str(fdialog.selectedFilter())
if mode != "OPEN":
if "." in filterused:
extension = filterused.replace(")", "")
if "(" in extension:
extension = extension.split("(")[-1]
extensionList = extension.split()
txt = str(filelist[0])
for extension in extensionList:
extension = extension.split(".")[-1]
if extension != "*":
txt = str(filelist[0])
if txt.endswith(extension):
break
else:
txt = txt+"."+extension
filelist[0] = txt
fdialog.close()
del fdialog
filelist = [str(x) for x in filelist]
if not(len(filelist)):
return []
if mode == "OPEN":
PyMcaDirs.inputDir = os.path.dirname(filelist[0])
if PyMcaDirs.outputDir is None:
PyMcaDirs.outputDir = os.path.dirname(filelist[0])
else:
PyMcaDirs.outputDir = os.path.dirname(filelist[0])
if PyMcaDirs.inputDir is None:
PyMcaDirs.inputDir = os.path.dirname(filelist[0])
#do not sort file list
#filelist.sort()
if getfilter:
return filelist, filterused
else:
return filelist
if __name__ == "__main__":
app = qt.QApplication([])
fileTypeList = ['PNG Files (*.png *.jpg)']
print(getExistingDirectory())
PyMcaDirs.nativeFileDialogs = False
print(getExistingDirectory())
PyMcaDirs.nativeFileDialogs = True
print(getFileList(None, fileTypeList,"Please select a file", "SAVE", True, single=True))
PyMcaDirs.nativeFileDialogs = False
print(getFileList(None, fileTypeList,"Please select files", "LOAD", getfilter=False, single=False))
#app.exec_()
|