-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilebrowser.py
716 lines (544 loc) · 24.2 KB
/
filebrowser.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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
import os
from PyQt5.QtWidgets import (QTreeView, QFileSystemModel, QSizePolicy,
QAction, QMenu, QMessageBox)
from PyQt5.Qt import (QDir, QSizePolicy, QPalette, Qt, QFont)
from PyQt5.Qsci import QsciAPIs
from PyQt5 import Qsci
from PyQt5.Qt import Qt
from codeeditor import CodeEditor
from dialog import EnterDialog
from runthread import RunThread
from configuration import Configuration
from widgets import MessageBox
import shutil
class FileBrowser(QTreeView):
def __init__(self, parent=None, textPad=None, notebook=None,
codeView=None):
super().__init__()
self.path = self.checkPath(os.getcwd())
self.filename = None
self.text = None
self.initItems()
self.textPad = textPad
self.notebook = notebook
self.codeView = codeView
self.mainWindow = parent
self.index = None
self.copySourceFilePath = None # copy / paste items
self.copySourceFileName = None
self.isCopyFileFolder = None
self.setStyleSheet(
"""
border: 5 px;
background-color: lightblue;
color: red;
alternate-background-color: #063970;
selection-background-color: #3b5784;
""")
# Contextmenu
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.openMenu)
self.popMenu = QMenu()
self.popMenu.setStyleSheet(
"""
color: #063970;
background-color: #2c2c2c;
selection-background-color: #3b5784;
alternate-background-color: #063970;
"""
)
infoAction = QAction('Info', self)
infoAction.triggered.connect(self.onInfo)
createFolderAction = QAction('Create New Folder', self)
createFolderAction.triggered.connect(self.onCreateNewFolder)
copyAction = QAction('Copy Item', self)
copyAction.triggered.connect(self.onCopy)
pasteAction = QAction('Paste Item', self)
pasteAction.triggered.connect(self.onPaste)
renameAction = QAction('Rename Item', self)
renameAction.triggered.connect(self.onRename)
deleteAction = QAction('Delete Item', self)
deleteAction.triggered.connect(self.onDelete)
terminalAction = QAction('Open Terminal', self)
terminalAction.triggered.connect(self.onTerminal)
self.popMenu.addAction(infoAction)
self.popMenu.addSeparator()
self.popMenu.addAction(createFolderAction)
self.popMenu.addSeparator()
self.popMenu.addAction(copyAction)
self.popMenu.addAction(pasteAction)
self.popMenu.addAction(renameAction)
self.popMenu.addSeparator()
self.popMenu.addAction(deleteAction)
self.popMenu.addSeparator()
self.popMenu.addAction(terminalAction)
def openMenu(self, position):
# -> open ContextMenu
self.popMenu.exec_(self.mapToGlobal(position))
def onInfo(self):
if not self.index:
return
index = self.index
indexItem = self.model.index(index.row(), 0, index.parent())
fileName, filePath, fileDir, fileInfo = self.getFileInformation()
bundle = fileInfo.isBundle() # bool
dir = fileInfo.isDir() # bool
file = fileInfo.isFile() # bool
executable = fileInfo.isExecutable() # bool
readable = fileInfo.isReadable() # bool
writable = fileInfo.isWritable() # bool
created = fileInfo.created() # QDateTime
#modified = fileInfo.lastModified() # QDateTime
owner = fileInfo.owner() # QString
size = fileInfo.size() # QInt
s = format(size, ',d')
text = ''
text += 'Type:\t'
if bundle:
text += 'Bundle\n\n'
if dir:
text += 'Path\n\n'
if file:
text += 'File\n\n'
if readable:
text += 'read:\tyes\n'
else:
text += 'read:\tno\n'
if writable:
text += 'write:\tyes\n'
else:
text += 'write:\tno\n'
if executable:
text += 'exec:\tyes\n\n'
else:
text += 'exec:\tno\n\n'
text += 'size:\t' + str(s) + ' bytes' + '\n'
text += 'owner:\t' + owner + '\n'
text += 'created:\t' + str(created.date().day()) + '.' + \
str(created.date().month()) + '.' + \
str(created.date().year()) + ' ' + \
created.time().toString() + '\n'
q = MessageBox(QMessageBox.Information, fileName,
text, QMessageBox.NoButton)
q.exec_()
def onCreateNewFolder(self):
if not self.index:
return
else:
index = self.index
fileName, filePath, fileDir, fileInfo = self.getFileInformation()
path = os.getcwd() + '/'
path = self.checkPath(path)
index = self.index
fileName, filePath, fileDir, fileInfo = self.getFileInformation()
dialog = EnterDialog(self.mainWindow, fileName, \
filePath, fileDir, fileInfo, False, path)
dialog.exec_()
# return
def onCopy(self):
if not self.index:
return
fileName, filePath, fileDir, fileInfo = self.getFileInformation()
if fileName == '..':
self.clearSelection()
self.mainWindow.statusBar.showMessage("can't copy this item !", 3000)
self.copySourceFilePath = None
self.copySourceFileName = None
self.isCopyFileFolder = None
return
if fileDir:
self.copySourceFilePath = filePath
self.copySourceFileName = fileName
self.isCopyFileFolder = True
self.mainWindow.statusBar.showMessage('Path: <' + \
self.copySourceFileName + \
'> marked', 3000)
else:
self.copySourceFilePath = filePath
self.copySourceFileName = fileName
self.mainWindow.statusBar.showMessage('File: <' + \
self.copySourceFileName + \
'> marked', 3000)
def onPaste(self):
if not self.index:
return
if not self.copySourceFilePath:
self.mainWindow.statusBar.showMessage('nothing marked !', 3000)
return
if not self.copySourceFileName:
self.mainWindow.statusBar.showMessage('nothing marked !', 3000)
return
fileName, filePath, fileDir, fileInfo = self.getFileInformation()
rootPath = os.getcwd()
rootPath = self.checkPath(rootPath)
fileList = os.listdir(self.path) # File list at current path
if fileName == '..':
# clicked on '..'
rootPath = os.getcwd()
rootPath = self.checkPath(rootPath)
if fileDir and self.isCopyFileFolder:
# copy path to another path
if fileName == '..':
path = os.getcwd()
path = self.checkPath(path)
else:
path = filePath
newPath = path + '/' + self.copySourceFileName
fileList = os.listdir(path)
if self.copySourceFileName in fileList:
q = MessageBox(QMessageBox.Warning, "Error",
"Another path with the same name already exists.",
QMessageBox.NoButton)
q.exec_()
self.resetMarkedItems(True)
return
if self.copySourceFilePath in newPath:
q = MessageBox(QMessageBox.Critical, 'Error',
'Name of path already exists in new path',
QMessageBox.NoButton)
q.exec_()
self.resetMarkedItems(True)
return
try:
os.mkdir(newPath)
self.copytree(self.copySourceFilePath, newPath)
self.mainWindow.statusBar.showMessage('Done !', 3000)
except Exception as e:
q = MessageBox(QMessageBox.Critical, "Error",
str(e), QMessageBox.NoButton)
q.exec_()
self.resetMarkedItems(True)
return
elif fileDir and not self.isCopyFileFolder:
# copy file to path
if fileName == '..':
path = os.getcwd()
path = self.checkPath(path)
else:
path = filePath
fileList = os.listdir(path) # File list at current path
if self.copySourceFileName in fileList:
result = MessageBox(QMessageBox.Warning, "Warning",
"File already exists.\n" +
"Continue ?", QMessageBox.Yes | QMessageBox.No)
if (result.exec_() == QMessageBox.Yes):
try:
shutil.copy(self.copySourceFilePath, path)
self.mainWindow.statusBar.showMessage('Done !', 3000)
except Exception as e:
q = MessageBox(QMessageBox.Critical, "Error",
str(e), QMessageBox.NoButton)
q.exec_()
self.resetMarkedItems(True)
return
else:
self.resetMarkedItems()
return
else:
try:
shutil.copy(self.copySourceFilePath, path)
self.mainWindow.statusBar.showMessage('Done !', 3000)
except Exception as e:
q = MessageBox(QMessageBox.Critical, "Error",
str(e), QMessageBox.NoButton)
q.exec_()
self.resetMarkedItems(True)
return
elif not fileDir and self.isCopyFileFolder:
# copy path to selected file -> correct this user input ...
newPath = rootPath + '/' + self.copySourceFileName
if self.copySourceFileName in fileList:
q = MessageBox(QMessageBox.Information, "Error",
"Another path with the same name already exists.",
QMessageBox.NoButton)
q.exec_()
self.resetMarkedItems(True)
return
try:
os.mkdir(newPath)
self.copytree(self.copySourceFilePath, newPath)
self.mainWindow.statusBar.showMessage('Done !', 3000)
except Exception as e:
q = MessageBox(QMessageBox.Critical, "Error",
str(e), QMessageBox.NoButton)
q.exec_()
self.resetMarkedItems(True)
return
elif not fileDir and not self.isCopyFileFolder:
# user copy file to another file -> correct this input
fileList = os.listdir(rootPath) # File list in current path
if self.copySourceFileName in fileList:
result = MessageBox(QMessageBox.Warning, "Warning",
"File with this name already exists !" +
"Continue ?", QMessageBox.Yes | QMessageBox.No)
if (result.exec_() == QMessageBox.Yes):
try:
shutil.copy(self.copySourceFilePath, rootPath)
self.mainWindow.statusBar.showMessage('Done !', 3000)
except Exception as e:
q = MessageBox(QMessageBox.Critical, "Error",
str(e), QMessageBox.NoButton)
q.exec_()
self.resetMarkedItems(True)
return
else:
self.resetMarkedItems(True)
return
else:
try:
shutil.copy(self.copySourceFilePath, rootPath)
self.mainWindow.statusBar.showMessage('Done !', 3000)
except Exception as e:
q = MessageBox(QMessageBox.Critical, "Error",
str(e), QMessageBox.Ok)
q.exec_()
self.resetMarkedItems(True)
return
self.resetMarkedItems()
def resetMarkedItems(self, showMessage=False):
# reset marked items
self.copySourceFilePath = None
self.copySourceFileName = None
self.isCopyFileFolder = None
if showMessage:
self.mainWindow.statusBar.showMessage('Mark removed !', 3000)
# copy path with subfolder
# thanks to stackoverflow.com ..... !
def copytree(self, src, dst, symlinks=False, ignore=None):
if not os.path.exists(dst):
os.makedirs(dst)
if not os.path.exists(src):
os.makedirs(src)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
self.copytree(s, d, symlinks, ignore)
else:
if not os.path.exists(d) or os.stat(s).st_mtime - os.stat(d).st_mtime > 1:
shutil.copy2(s, d)
def onRename(self):
if not self.index:
return
fileName, filePath, fileDir, fileInfo = self.getFileInformation()
dialog = EnterDialog(self.mainWindow, fileName, filePath, fileDir, fileInfo, True)
dialog.exec_()
def onDelete(self):
if not self.index:
return
fileName, filePath, fileDir, fileInfo = self.getFileInformation()
if fileDir:
if fileName == '..':
self.mainWindow.statusBar.showMessage('can not delete this item !', 3000)
return
else:
result = MessageBox(QMessageBox.Warning, 'Delete directory', '<b>' + filePath + '</b>' + " ?",
QMessageBox.Yes | QMessageBox.No)
if (result.exec_() == QMessageBox.Yes):
try:
shutil.rmtree(filePath)
self.mainWindow.statusBar.showMessage('Done !', 3000)
self.resetMarkedItems()
except Exception as e:
q = MessageBox(QMessageBox.Critical, "Error", str(e), QMessageBox.NoButton)
q.exec_()
self.resetMarkedItems(True)
else:
return
else:
pathList = filePath.split('/')[:-1]
path = ''
for elem in pathList:
path += elem + '/'
file = filePath.split('/')[-1]
result = MessageBox(QMessageBox.Warning, 'Delete file', path + "<b>" + file + "</b>" + " ?",
QMessageBox.Yes | QMessageBox.No)
if (result.exec_() == QMessageBox.Yes):
try:
os.remove(filePath)
self.mainWindow.statusBar.showMessage('Done !', 3000)
except Exception as e:
q = MessageBox(QMessageBox.Critical, "Error", str(e), QMessageBox.NoButton)
q.exec_()
self.resetMarkedItems(True)
def onTerminal(self):
c = Configuration()
system = c.getSystem()
command = c.getTerminal(system)
thread = RunThread(command)
thread.start()
def initItems(self):
font = QFont()
font.setPixelSize(16)
self.prepareModel(os.getcwd())
self.setToolTip(os.getcwd())
# prepare drag and drop
self.setDragEnabled(False)
sizePolicy = QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)
self.setSizePolicy(sizePolicy)
self.setAutoExpandDelay(2)
self.setAlternatingRowColors(False)
self.setAnimated(False)
self.setIndentation(20)
self.setSortingEnabled(False)
self.setRootIsDecorated(False)
self.setPalette(QPalette(Qt.blue))
self.verticalScrollBar().setStyleSheet(
"""border: 20px solid blue;
background-color: darkgreen;
alternate-background-color: #063970;""")
self.horizontalScrollBar().setStyleSheet(
"""border: 20px solid blue;
background-color: darkgreen;
alternate-background-color: #063970;""")
self.setFont(font)
# signals
self.doubleClicked.connect(self.onDoubleClicked)
self.clicked.connect(self.onClicked)
self.pressed.connect(self.onClicked)
#self.entered.connect(self.onEntered)
self.columnMoved()
# that hides the size, file type and last modified colomns
self.setHeaderHidden(True)
self.hideColumn(1)
self.hideColumn(2)
self.hideColumn(3)
self.resize(400, 400)
def prepareModel(self, path):
self.model = QFileSystemModel()
self.model.setRootPath(path)
#model.setFilter(QDir.AllDirs |QDir.NoDotAndDotDot | QDir.AllEntries)
self.model.setFilter(QDir.Files | QDir.AllDirs | QDir.NoDot | QDir.Hidden)
#self.model.setNameFilters(self.filter)
self.model.rootPathChanged.connect(self.onRootPathChanged)
self.fsindex = self.model.setRootPath(path)
self.setModel(self.model)
self.setRootIndex(self.fsindex)
def checkPath(self, path):
if '\\' in path:
path = path.replace('\\', '/')
return path
def getFileInformation(self):
index = self.index
indexItem = self.model.index(index.row(), 0, index.parent())
fileName = self.model.fileName(indexItem)
filePath = self.model.filePath(indexItem)
fileDir = self.model.isDir(indexItem)
fileInfo = self.model.fileInfo(indexItem)
fileName = self.checkPath(fileName)
filePath = self.checkPath(filePath)
return(fileName, filePath, fileDir, fileInfo)
def onClicked(self, index):
self.index = index #.... index des FileSystemModels
indexItem = self.model.index(index.row(), 0, index.parent())
fileName, filePath, fileDir, fileInfo = self.getFileInformation()
self.setToolTip(filePath)
if fileDir:
self.path = self.checkPath(os.getcwd())
self.filename = None
else:
self.filename = filePath
self.path = self.checkPath(os.getcwd())
#print('self.filename: ', self.filename)
#print('self.path: ', self.path)
def refresh(self, dir=None):
if not dir:
dir = self.checkPath(os.getcwd())
else:
dir = dir
self.model.setRootPath(dir)
if self.rootIsDecorated:
self.setRootIsDecorated(False)
self.clearSelection()
def onDoubleClicked(self, index):
self.index = index #.... wie oben ... def onClicked(...):
indexItem = self.model.index(index.row(), 0, index.parent())
fileName, filePath, fileDir, fileInfo = self.getFileInformation()
if fileDir:
filePath = self.checkPath(filePath)
try:
os.chdir(filePath)
except Exception as e:
self.mainWindow.statusBar.showMessage(str(e), 3000)
self.path = self.checkPath(os.getcwd())
self.model.setRootPath(filePath)
if self.rootIsDecorated:
self.setRootIsDecorated(False)
else:
self.filename = filePath
try:
with open(self.filename, 'r') as f:
self.text = f.read()
except Exception as e:
self.mainWindow.statusBar.showMessage(str(e), 3000)
self.filename = None
return
if self.textPad:
if not self.textPad.filename:
editor = CodeEditor(self.mainWindow)
editor.filename = filePath
self.notebook.newTab(editor)
x = self.notebook.count() # number of tabs
index = x - 1
self.notebook.setCurrentIndex(index)
tabName = os.path.basename(editor.filename)
self.textPad = editor
self.textPad.setText(self.text)
self.notebook.setTabText(x, tabName)
else:
editor = CodeEditor(self.mainWindow)
editor.filename = filePath
tabName = os.path.basename(editor.filename)
self.notebook.newTab(editor)
x = self.notebook.count() # number of tabs
index = x - 1
self.notebook.setCurrentIndex(index)
self.textPad = editor
self.textPad.setText(self.text)
self.notebook.setTabText(x, tabName)
if not self.textPad:
editor = CodeEditor(self.mainWindow)
editor.filename = None
self.notebook.newTab(editor)
x = self.notebook.count()
index = x - 1
self.notebook.setCurrentIndex(index)
self.textPad = editor
# make codeView
codeViewList = self.codeView.makeDictForCodeView(self.text)
self.codeView.updateCodeView(codeViewList)
# update textPad Autocomplete
autocomplete = Qsci.QsciAPIs(self.textPad.lexer)
self.textPad.autocomplete = autocomplete
self.textPad.setPythonAutocomplete()
self.clearSelection()
self.textPad.updateAutoComplete()
# remove the '*' when opening new file ...
self.removeStarAtOpen()
def removeStarAtOpen(self):
notebook = self.mainWindow.notebook
textPad = notebook.currentWidget()
index = notebook.currentIndex()
if textPad.filename:
fname = os.path.basename(textPad.filename)
notebook.setTabText(index, fname)
def onRootPathChanged(self):
self.setModel(None)
self.setModel(self.model)
self.fsindex = self.model.setRootPath(QDir.currentPath())
self.setRootIndex(self.fsindex)
sizePolicy = QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)
self.setSizePolicy(sizePolicy)
self.setAutoExpandDelay(2)
self.setAlternatingRowColors(False)
self.setAnimated(True)
self.setIndentation(20)
self.setSortingEnabled(False)
self.setRootIsDecorated(False)
self.setHeaderHidden(True)
self.hideColumn(1)
self.hideColumn(2)
self.hideColumn(3)
self.setToolTip(QDir.currentPath())
self.path = os.getcwd()
self.path = self.checkPath(self.path)