-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuperManCheck_FileLineEdit.py
293 lines (276 loc) · 11.8 KB
/
SuperManCheck_FileLineEdit.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
285
286
287
288
289
290
291
292
293
'''
file_input v1.0
FileLineEdit:
* a LineEdit with built-in Import/Export/Browse
'''
# python 2,3 support unicode function
try:
UNICODE_EXISTS = bool(type(unicode))
except NameError:
unicode = lambda s: str(s)
try:
from PySide import QtGui, QtCore
import PySide.QtGui as QtWidgets
print("PySide Try")
qtMode = 0
except ImportError:
try:
from PySide2 import QtCore, QtGui, QtWidgets
print("PySide2 Try")
qtMode = 2
except ImportError:
try:
from PyQt4 import QtGui,QtCore
import PyQt4.QtGui as QtWidgets
import sip
qtMode = 1
print("PyQt4 Try")
except ImportError:
from PyQt5 import QtGui,QtCore,QtWidgets
import sip
qtMode = 3
print("PyQt5 Try")
import os,sys
import subprocess
class FileLineEdit(QtWidgets.QWidget):
def __init__(self, parent=None,btn_list=[],type='',pathType='',ext='',label='',import_func='',export_func=''):
QtWidgets.QWidget.__init__(self,parent)
# memo
self.parent=parent
self.pathType = pathType
self.ext = ext
self.import_func=import_func
self.export_func=export_func
self.memoData={}
self.memoData['last_import']=''
self.memoData['last_export']=''
self.memoData['last_browse']=''
# UI
self.uiList={}
self.uiList['main_layout']=QtWidgets.QHBoxLayout();
self.uiList['main_layout'].setContentsMargins(0, 0, 0, 0)
self.setLayout(self.uiList['main_layout'])
self.uiList['main_label'] = QtWidgets.QLabel(label)
self.uiList['main_layout'].addWidget(self.uiList['main_label'])
self.uiList['main_input'] = QtWidgets.QLineEdit()
self.uiList['main_layout'].addWidget(self.uiList['main_input'])
if len(btn_list)==0:
btn_list = ['browse','show','clear','import','export']
for t in btn_list:
self.uiList['{0}_btn'.format(t)] = QtWidgets.QPushButton(t.title())
self.uiList['{0}_btn'.format(t)].setMaximumWidth(50)
self.uiList['main_layout'].addWidget(self.uiList['{0}_btn'.format(t)])
if type=='':
if t in ['import','export']:
self.uiList['{0}_btn'.format(t)].setVisible(1)
else:
if t=='browse':
self.uiList['{0}_btn'.format(t)].setVisible(1)
self.qui_policy('main_input',5,3)
# hide ui
# connect UI
self.Establish_Connections()
def Establish_Connections(self):
for ui_name in self.uiList.keys():
prefix = ui_name.rsplit('_', 1)[0]
if ui_name.endswith('_btn'):
if hasattr(self, prefix+"_action"):
self.uiList[ui_name].clicked.connect(getattr(self, prefix+"_action"))
# drop support
self.uiList['main_input'].installEventFilter(self)
# the main window event filter function
def eventFilter(self, object, event):
if event.type() == QtCore.QEvent.DragEnter:
data = event.mimeData()
urls = data.urls()
if object is self.uiList['main_input'] and (urls and urls[0].scheme() == 'file'):
event.acceptProposedAction()
return 1
elif event.type() == QtCore.QEvent.Drop:
data = event.mimeData()
urls = data.urls()
if object is self.uiList['main_input'] and (urls and urls[0].scheme() == 'file'):
filePath = unicode(urls[0].path())[1:]
if self.pathType=='folder':
if os.path.isdir(filePath):
self.uiList['main_input'].setText(os.path.normpath(filePath))
else:
self.quickMsg('Require Folder as Input')
else:
self.uiList['main_input'].setText(os.path.normpath(filePath))
return 1
return 0
def setText(self,txt):
self.uiList['main_input'].setText(txt)
def text(self):
return unicode(self.uiList['main_input'].text())
def setLabel(self,txt):
self.uiList['main_label'].setText(txt)
def label(self):
return unicode(self.uiList['main_label'].text())
def setReadOnly(self,state):
self.uiList['main_input'].setReadOnly(state)
def setImportFunc(self, import_func):
self.import_func = import_func
def setExportFunc(self, export_func):
self.export_func = export_func
def browse_action(self):
if self.pathType == 'folder':
tmp_path= self.quickFolderAsk()
if tmp_path == '':
return
self.setText(tmp_path)
else:
file= self.quickFileAsk('import',ext=self.ext)
if file == "":
return
self.setText(file)
def show_action(self):
file_path = self.text()
if file_path is not None and os.path.exists(file_path):
self.openFolder(file_path)
def clear_action(self):
self.uiList['main_input'].setText('')
def export_action(self):
filePath_input = self.uiList['main_input']
file = unicode(filePath_input.text())
if file == "":
file= self.quickFileAsk('export',self.ext)
if file == "":
return
# update ui
filePath_input.setText(file)
# export process
if self.parent is not None:
if self.export_func !='' and hasattr(self.parent, self.export_func):
getattr(self.parent,self.export_func)(file)
self.quickInfo("File: '"+file+"' creation finished.")
def import_action(self):
filePath_input = self.uiList['main_input']
file=unicode(filePath_input.text())
if file == "":
file= self.quickFileAsk('import',self.ext)
if file == "":
return
# check exists
if not os.path.exists(file):
self.quickMsg('File not exists.')
return
# update ui
filePath_input.setText(file)
# import process
if self.parent is not None:
if self.import_func !='' and hasattr(self.parent, self.import_func):
getattr(self.parent,self.import_func)(file)
self.quickInfo("File: '"+file+"' loading finished.")
# support functions
def openFolder(self, folderPath):
if os.path.isfile(folderPath):
folderPath = os.path.dirname(folderPath)
if os.path.isdir(folderPath):
cmd_list = None
if sys.platform == 'darwin':
cmd_list = ['open', '--', folderPath]
elif sys.platform == 'linux2':
cmd_list = ['xdg-open', '--', folderPath]
elif sys.platform in ['win32','win64']:
cmd_list = ['explorer', folderPath.replace('/','\\')]
if cmd_list != None:
try:
subprocess.check_call(cmd_list)
except subprocess.CalledProcessError:
pass # handle errors in the called executable
except OSError:
pass # executable not found
def quickMsg(self,msg):
tmpMsg = QtWidgets.QMessageBox() # for simple msg that no need for translation
tmpMsg.setWindowTitle("Info")
tmpMsg.setText(msg)
tmpMsg.addButton("OK",QtWidgets.QMessageBox.YesRole)
tmpMsg.exec_()
def quickInfo(self, info, force=0):
if hasattr( self.window(), "quickInfo") and force == 0:
self.window().statusBar().showMessage(info)
def quickFolderAsk(self,dir=None):
if dir == None:
dir = self.memoData['last_browse']
if self.parent is not None and hasattr(self.parent, 'memoData'):
dir = self.parent.memoData['last_browse']
return unicode(QtWidgets.QFileDialog.getExistingDirectory(self, "Select Directory",dir))
def quickFileAsk(self, type, ext=None, dir=None):
# standalone version parent access option
if ext == None:
ext = "RAW data (*.json);;RAW binary data (*.dat);;Format Txt (*{0});;AllFiles (*.*)".format(self.fileType)
elif isinstance(ext, (str,unicode)):
if ';;' not in ext:
if ext == '':
ext = 'AllFiles (*.*)'
else:
ext = self.extFormat(ext) + ';;AllFiles (*.*)'
elif isinstance(ext, (tuple,list)):
if len(ext) > 0 and isinstance(ext[0], (tuple,list)):
tmp_list = [self.extFormat(x) for x in ext]
tmp_list.append('AllFiles (*.*)')
ext = ';;'.join(tmp_list)
else:
ext = ';;'.join([self.extFormat(x) for x in ext].append('AllFiles(*.*)'))
elif isinstance(ext, dict):
tmp_list = [self.extFormat(x) for x in ext.items()]
tmp_list.append('AllFiles (*.*)')
ext = ';;'.join(tmp_list)
else:
ext = "AllFiles (*.*)"
file = ''
if type == 'export':
if dir == None:
dir = self.memoData['last_export']
if self.parent is not None and hasattr(self.parent, 'memoData'):
dir = self.parent.memoData['last_export']
file = QtWidgets.QFileDialog.getSaveFileName(self, "Save File",dir,ext)
elif type == 'import':
if dir == None:
dir = self.memoData['last_import']
if self.parent is not None and hasattr(self.parent, 'memoData'):
dir = self.parent.memoData['last_import']
file = QtWidgets.QFileDialog.getOpenFileName(self, "Open File",dir,ext)
if isinstance(file, (list, tuple)):
file = file[0] # for deal with pyside case
else:
file = unicode(file) # for deal with pyqt case
# save last dir in memoData
if file != '':
if type == 'export':
self.memoData['last_export'] = os.path.dirname(file) #QFileInfo().path()
if self.parent is not None and hasattr(self.parent, 'memoData'):
self.parent.memoData['last_export'] = os.path.dirname(file)
elif type == 'import':
self.memoData['last_import'] = os.path.dirname(file)
if self.parent is not None and hasattr(self.parent, 'memoData'):
self.parent.memoData['last_import'] = os.path.dirname(file)
return file
def extFormat(self, ext):
if isinstance(ext, (tuple,list)):
ext = '{0} (*.{1})'.format(ext[1],ext[0])
else:
if ext.startswith('.'):
ext = ext[1:]
ext = '{0} (*.{0})'.format(ext)
return ext
def qui_policy(self, ui_list, w, h):
# reference value
policyList = (
QtWidgets.QSizePolicy.Fixed,
QtWidgets.QSizePolicy.Minimum,
QtWidgets.QSizePolicy.Maximum,
QtWidgets.QSizePolicy.Preferred,
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.MinimumExpanding,
QtWidgets.QSizePolicy.Ignored,
)
# 0 = fixed; 1 > min; 2 < max; 3 = prefered; 4 = <expanding>; 5 = expanding> Aggresive; 6=4 ignored size input
if not isinstance(ui_list, (list, tuple)):
ui_list = [ui_list]
for each_ui in ui_list:
if isinstance(each_ui, str):
each_ui = self.uiList[each_ui]
each_ui.setSizePolicy(policyList[w],policyList[h])