forked from y8tireu/My-Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile-Manager.py
275 lines (237 loc) · 10.4 KB
/
File-Manager.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
# file_manager.py
import sys
import os
import shutil
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QFileSystemModel, QMessageBox,
QInputDialog, QMenu, QTreeView, QListView, QSplitter,
QToolBar, QAction, QStatusBar, QWidget, QVBoxLayout, QLineEdit,
QLabel
)
from PyQt5.QtCore import Qt, QDir, QModelIndex, QSize
from PyQt5.QtGui import QIcon, QPixmap
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setWindowTitle("Advanced File Manager")
MainWindow.resize(1024, 768)
self.centralwidget = QWidget(MainWindow)
self.verticalLayout = QVBoxLayout(self.centralwidget)
# Toolbar
self.toolBar = QToolBar(MainWindow)
MainWindow.addToolBar(Qt.TopToolBarArea, self.toolBar)
# Actions
self.actionNew_Folder = QAction(QIcon.fromTheme("folder-new"), "New Folder", MainWindow)
self.actionDelete = QAction(QIcon.fromTheme("edit-delete"), "Delete", MainWindow)
self.actionRefresh = QAction(QIcon.fromTheme("view-refresh"), "Refresh", MainWindow)
self.toolBar.addAction(self.actionNew_Folder)
self.toolBar.addAction(self.actionDelete)
self.toolBar.addAction(self.actionRefresh)
# Search Bar
self.searchLineEdit = QLineEdit()
self.searchLineEdit.setPlaceholderText("Search...")
self.toolBar.addWidget(self.searchLineEdit)
# Splitter
self.splitter = QSplitter(Qt.Horizontal)
self.verticalLayout.addWidget(self.splitter)
# Directory Tree
self.treeView = QTreeView()
self.treeView.setHeaderHidden(True)
self.splitter.addWidget(self.treeView)
# Right Pane Splitter
self.rightSplitter = QSplitter(Qt.Vertical)
self.splitter.addWidget(self.rightSplitter)
# File List
self.listView = QListView()
self.rightSplitter.addWidget(self.listView)
# Preview Pane
self.previewLabel = QLabel("File Preview")
self.previewLabel.setAlignment(Qt.AlignCenter)
self.rightSplitter.addWidget(self.previewLabel)
# Status Bar
self.statusbar = QStatusBar(MainWindow)
MainWindow.setStatusBar(self.statusbar)
MainWindow.setCentralWidget(self.centralwidget)
class FileManager(QMainWindow, Ui_MainWindow):
def __init__(self):
super(FileManager, self).__init__()
self.setupUi(self)
self.initUI()
def initUI(self):
# Set up the file system model
self.dir_model = QFileSystemModel()
self.dir_model.setRootPath('/')
self.dir_model.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs)
self.file_model = QFileSystemModel()
self.file_model.setRootPath('/')
# Include directories in the file model
self.file_model.setFilter(QDir.NoDotAndDotDot | QDir.AllEntries)
# Configure the tree view (directory tree)
self.treeView.setModel(self.dir_model)
self.treeView.setRootIndex(self.dir_model.index('/'))
self.treeView.clicked.connect(self.on_tree_view_clicked)
# Configure the list view (file list)
self.listView.setModel(self.file_model)
self.listView.setRootIndex(self.file_model.index('/'))
self.listView.doubleClicked.connect(self.on_file_double_clicked)
self.listView.clicked.connect(self.on_file_clicked)
# Connect toolbar actions
self.actionNew_Folder.triggered.connect(self.create_new_folder)
self.actionDelete.triggered.connect(self.delete_item)
self.actionRefresh.triggered.connect(self.refresh_views)
# Context menu for list view
self.listView.setContextMenuPolicy(Qt.CustomContextMenu)
self.listView.customContextMenuRequested.connect(self.open_context_menu)
# Search functionality
self.searchLineEdit.returnPressed.connect(self.search_files)
# Drag-and-drop support
self.listView.setDragEnabled(True)
self.listView.setAcceptDrops(True)
self.listView.setDefaultDropAction(Qt.MoveAction)
self.listView.setDropIndicatorShown(True)
# Load styles
self.setStyleSheet("""
QMainWindow {
background-color: #2b2b2b;
}
QTreeView, QListView {
background-color: #3c3f41;
color: #dcdcdc;
selection-background-color: #606366;
}
QToolBar {
background-color: #3c3f41;
}
QStatusBar {
background-color: #3c3f41;
color: #dcdcdc;
}
QLineEdit {
background-color: #2b2b2b;
color: #dcdcdc;
border: 1px solid #606366;
border-radius: 4px;
padding: 4px;
}
QLabel {
color: #dcdcdc;
}
""")
# Initial status bar message
self.statusbar.showMessage("Welcome to Advanced File Manager", 5000)
def on_tree_view_clicked(self, index):
# Update list view based on selected directory
dir_path = self.dir_model.fileInfo(index).absoluteFilePath()
self.listView.setRootIndex(self.file_model.setRootPath(dir_path))
self.statusbar.showMessage(f"Current Directory: {dir_path}")
def on_file_double_clicked(self, index):
# Handle file opening or navigation
file_path = self.file_model.fileInfo(index).absoluteFilePath()
if os.path.isdir(file_path):
# Open directory in list view
self.listView.setRootIndex(self.file_model.setRootPath(file_path))
self.statusbar.showMessage(f"Current Directory: {file_path}")
else:
# Open file with default application
os.system(f'xdg-open "{file_path}"')
def on_file_clicked(self, index):
# Preview file content or image
file_path = self.file_model.fileInfo(index).absoluteFilePath()
if os.path.isfile(file_path):
if file_path.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
pixmap = QPixmap(file_path)
self.previewLabel.setPixmap(pixmap.scaled(
self.previewLabel.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
elif file_path.lower().endswith(('.txt', '.py', '.md', '.log', '.ini', '.cfg')):
try:
with open(file_path, 'r') as f:
content = f.read()
self.previewLabel.setText(content)
except Exception as e:
self.previewLabel.setText(f"Could not open file:\n{e}")
else:
self.previewLabel.setText("No preview available.")
else:
self.previewLabel.setText("")
def create_new_folder(self):
index = self.treeView.currentIndex()
dir_path = self.dir_model.fileInfo(index).absoluteFilePath()
folder_name, ok = QInputDialog.getText(self, "New Folder", "Folder Name:")
if ok and folder_name:
new_folder_path = os.path.join(dir_path, folder_name)
try:
os.mkdir(new_folder_path)
self.refresh_views()
except Exception as e:
QMessageBox.critical(self, "Error", f"Could not create folder:\n{e}")
def delete_item(self):
index = self.listView.currentIndex()
file_path = self.file_model.fileInfo(index).absoluteFilePath()
reply = QMessageBox.question(
self, "Delete", f"Are you sure you want to delete '{file_path}'?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No
)
if reply == QMessageBox.Yes:
try:
if os.path.isdir(file_path):
shutil.rmtree(file_path)
else:
os.remove(file_path)
self.refresh_views()
except Exception as e:
QMessageBox.critical(self, "Error", f"Could not delete item:\n{e}")
def refresh_views(self):
self.dir_model.refresh()
self.file_model.refresh()
self.statusbar.showMessage("Views refreshed", 2000)
def open_context_menu(self, position):
index = self.listView.indexAt(position)
if not index.isValid():
return
menu = QMenu()
open_action = menu.addAction("Open")
delete_action = menu.addAction("Delete")
rename_action = menu.addAction("Rename")
permissions_action = menu.addAction("Permissions")
action = menu.exec_(self.listView.mapToGlobal(position))
if action == open_action:
self.on_file_double_clicked(index)
elif action == delete_action:
self.delete_item()
elif action == rename_action:
self.rename_item(index)
elif action == permissions_action:
self.manage_permissions(index)
def rename_item(self, index):
file_path = self.file_model.fileInfo(index).absoluteFilePath()
new_name, ok = QInputDialog.getText(self, "Rename", "New Name:")
if ok and new_name:
new_path = os.path.join(os.path.dirname(file_path), new_name)
try:
os.rename(file_path, new_path)
self.refresh_views()
except Exception as e:
QMessageBox.critical(self, "Error", f"Could not rename item:\n{e}")
def search_files(self):
search_query = self.searchLineEdit.text().lower()
root_index = self.listView.rootIndex()
for i in range(self.file_model.rowCount(root_index)):
index = self.file_model.index(i, 0, root_index)
file_name = self.file_model.fileName(index).lower()
self.listView.setRowHidden(i, root_index, search_query not in file_name)
def manage_permissions(self, index):
file_path = self.file_model.fileInfo(index).absoluteFilePath()
permissions = oct(os.stat(file_path).st_mode)[-3:]
new_permissions, ok = QInputDialog.getText(
self, "Permissions", f"Current permissions: {permissions}\nEnter new permissions (e.g., 755):"
)
if ok and new_permissions:
try:
os.chmod(file_path, int(new_permissions, 8))
QMessageBox.information(self, "Success", "Permissions updated successfully.")
except Exception as e:
QMessageBox.critical(self, "Error", f"Could not change permissions:\n{e}")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = FileManager()
window.show()
sys.exit(app.exec_())