|
| 1 | +import sys |
| 2 | +from pathlib import Path |
| 3 | +from PyQt5.QtWidgets import ( |
| 4 | + QApplication, |
| 5 | + QMainWindow, |
| 6 | + QTreeWidget, |
| 7 | + QTreeWidgetItem, |
| 8 | + QVBoxLayout, |
| 9 | + QWidget, |
| 10 | + QLabel, |
| 11 | + QHBoxLayout, |
| 12 | + QDialog, |
| 13 | +) |
| 14 | +from PyQt5.QtWebEngineWidgets import QWebEngineView |
| 15 | +from PyQt5.QtCore import Qt, QUuid, QUrl |
| 16 | +from PyQt5.QtGui import QIcon |
| 17 | + |
| 18 | + |
| 19 | +class FileBrowserWindow(QMainWindow): |
| 20 | + def __init__(self, parent=None): |
| 21 | + super().__init__(parent) |
| 22 | + self.setWindowTitle("Projspec Browser") |
| 23 | + self.setGeometry(100, 100, 950, 600) |
| 24 | + |
| 25 | + # Create tree widget |
| 26 | + self.tree = QTreeWidget(self) |
| 27 | + self.tree.setHeaderLabels(["Name", "Type", "Size"]) |
| 28 | + self.tree.setColumnWidth(0, 400) |
| 29 | + self.tree.setColumnWidth(1, 50) |
| 30 | + |
| 31 | + # Connect signals |
| 32 | + self.tree.itemExpanded.connect(self.on_item_expanded) |
| 33 | + self.tree.currentItemChanged.connect(self.on_item_changed) |
| 34 | + |
| 35 | + self.detail = QWebEngineView(self) |
| 36 | + # self.detail.load(QUrl("https://qt-project.org/")) |
| 37 | + self.detail.setFixedWidth(600) |
| 38 | + |
| 39 | + # Create central widget and layout |
| 40 | + central_widget = QWidget(self) |
| 41 | + self.setCentralWidget(central_widget) |
| 42 | + |
| 43 | + layout = QHBoxLayout(central_widget) |
| 44 | + layout.addWidget(self.tree) |
| 45 | + layout.addWidget(self.detail) |
| 46 | + central_widget.setLayout(layout) |
| 47 | + |
| 48 | + # Status bar |
| 49 | + self.statusBar().showMessage("Ready") |
| 50 | + |
| 51 | + # Populate with home directory |
| 52 | + self.populate_tree() |
| 53 | + |
| 54 | + def populate_tree(self): |
| 55 | + """Populate the tree with the user's home directory""" |
| 56 | + home_path = Path.home() |
| 57 | + root_item = QTreeWidgetItem(self.tree) |
| 58 | + root_item.setText(0, home_path.name or str(home_path)) |
| 59 | + root_item.setText(1, "Folder") |
| 60 | + root_item.setData(0, Qt.ItemDataRole.UserRole, str(home_path)) |
| 61 | + |
| 62 | + # Add a dummy child to make it expandable |
| 63 | + self.add_children(root_item, home_path) |
| 64 | + |
| 65 | + # Expand the root |
| 66 | + root_item.setExpanded(True) |
| 67 | + |
| 68 | + def add_children(self, parent_item, path): |
| 69 | + """Add child items for a directory""" |
| 70 | + try: |
| 71 | + path_obj = Path(path) |
| 72 | + |
| 73 | + # Get all items in directory |
| 74 | + items = sorted( |
| 75 | + path_obj.iterdir(), key=lambda x: (not x.is_dir(), x.name.lower()) |
| 76 | + ) |
| 77 | + |
| 78 | + for item in items: |
| 79 | + # Skip hidden files (optional) |
| 80 | + if item.name.startswith("."): |
| 81 | + continue |
| 82 | + |
| 83 | + child_item = QTreeWidgetItem(parent_item) |
| 84 | + child_item.setText(0, item.name) |
| 85 | + child_item.setData(0, Qt.ItemDataRole.UserRole, str(item)) |
| 86 | + |
| 87 | + if item.is_dir(): |
| 88 | + child_item.setText(1, "Folder") |
| 89 | + child_item.setText(2, "") |
| 90 | + # Add dummy child to make it expandable |
| 91 | + dummy = QTreeWidgetItem(child_item) |
| 92 | + dummy.setText(0, "Loading...") |
| 93 | + else: |
| 94 | + child_item.setText(1, "File") |
| 95 | + try: |
| 96 | + size = item.stat().st_size |
| 97 | + child_item.setText(2, self.format_size(size)) |
| 98 | + except: |
| 99 | + child_item.setText(2, "") |
| 100 | + |
| 101 | + except PermissionError: |
| 102 | + error_item = QTreeWidgetItem(parent_item) |
| 103 | + error_item.setText(0, "Permission Denied") |
| 104 | + error_item.setForeground(0, Qt.GlobalColor.red) |
| 105 | + except Exception as e: |
| 106 | + error_item = QTreeWidgetItem(parent_item) |
| 107 | + error_item.setText(0, f"Error: {str(e)}") |
| 108 | + error_item.setForeground(0, Qt.GlobalColor.red) |
| 109 | + |
| 110 | + def on_item_expanded(self, item): |
| 111 | + """Handle item expansion - load children if not already loaded""" |
| 112 | + # Check if we need to load children (has dummy child) |
| 113 | + if item.childCount() == 1 and item.child(0).text(0) == "Loading...": |
| 114 | + # Remove dummy child |
| 115 | + item.removeChild(item.child(0)) |
| 116 | + |
| 117 | + # Get path from item data |
| 118 | + path = item.data(0, Qt.ItemDataRole.UserRole) |
| 119 | + |
| 120 | + # Add real children |
| 121 | + if path: |
| 122 | + self.add_children(item, path) |
| 123 | + self.statusBar().showMessage(f"Loaded: {path}") |
| 124 | + |
| 125 | + def on_item_changed(self, item): |
| 126 | + import projspec |
| 127 | + |
| 128 | + if item.text(1) == "Folder": |
| 129 | + proj = projspec.Project(item.data(0, Qt.ItemDataRole.UserRole), walk=False) |
| 130 | + if proj.specs: |
| 131 | + print(proj.text_summary()) |
| 132 | + html = f"<!DOCTYPE html><html><body>{proj._repr_html_()}</body></html>" |
| 133 | + self.detail.setHtml(html) |
| 134 | + else: |
| 135 | + self.detail.setHtml("<!DOCTYPE html><html><body></body></html>") |
| 136 | + |
| 137 | + def format_size(self, size): |
| 138 | + """Format file size in human-readable format""" |
| 139 | + for unit in ["B", "KB", "MB", "GB", "TB"]: |
| 140 | + if size < 1024.0: |
| 141 | + return f"{size:.1f} {unit}" |
| 142 | + size /= 1024.0 |
| 143 | + return f"{size:.1f} PB" |
| 144 | + |
| 145 | + |
| 146 | +def main(): |
| 147 | + app = QApplication(sys.argv) |
| 148 | + window = FileBrowserWindow() |
| 149 | + window.show() |
| 150 | + sys.exit(app.exec()) |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + main() |
0 commit comments