|
9 | 9 | import utils |
10 | 10 |
|
11 | 11 | from PySide import QtGui, QtCore |
| 12 | +from PySide.QtCore import Qt |
| 13 | + |
| 14 | +class FileItem(QtGui.QTreeWidgetItem): |
| 15 | + def __init__(self, parent=None, path=None): |
| 16 | + super(FileItem, self).__init__(parent) |
| 17 | + self.path = path |
| 18 | + |
| 19 | +class TreeBrowser(QtGui.QWidget): |
| 20 | + def __init__(self, directory=None, checked_files=None, |
| 21 | + whitelist=None, blacklist=None, parent=None): |
| 22 | + super(TreeBrowser, self).__init__(parent=parent) |
| 23 | + self.root = QtGui.QTreeWidget() |
| 24 | + self.root.setHeaderLabel('Included files') |
| 25 | + self.root.itemChanged.connect(self.item_changed) |
| 26 | + self.files = {} |
| 27 | + |
| 28 | + self.paths = [] |
| 29 | + |
| 30 | + layout = QtGui.QVBoxLayout() |
| 31 | + layout.addWidget(self.root) |
| 32 | + self.setLayout(layout) |
| 33 | + |
| 34 | + self.watcher = QtCore.QFileSystemWatcher() |
| 35 | + self.watcher.directoryChanged.connect(self.directory_changed) |
| 36 | + self.watcher.fileChanged.connect(self.file_changed) |
| 37 | + |
| 38 | + self.directoryChanged = self.watcher.directoryChanged |
| 39 | + self.fileChanged = self.watcher.fileChanged |
| 40 | + |
| 41 | + self.init(directory, checked_files, whitelist, blacklist) |
| 42 | + |
| 43 | + def init(self, directory=None, checked_files=None, |
| 44 | + whitelist=None, blacklist=None): |
| 45 | + |
| 46 | + if directory: |
| 47 | + self.directory = directory + os.sep |
| 48 | + else: |
| 49 | + self.directory = directory |
| 50 | + |
| 51 | + self.checked_files = checked_files or [] |
| 52 | + |
| 53 | + self.whitelist = whitelist or [] |
| 54 | + self.blacklist = blacklist or [] |
| 55 | + |
| 56 | + self.watcher.removePaths(self.watcher.files()) |
| 57 | + |
| 58 | + self.files = {} |
| 59 | + |
| 60 | + self.root.clear() |
| 61 | + |
| 62 | + self.generate_directory_widget() |
| 63 | + |
| 64 | + def file_changed(self, path): |
| 65 | + print(path) |
| 66 | + pass |
| 67 | + |
| 68 | + def directory_changed(self, path): |
| 69 | + print(path) |
| 70 | + pass |
| 71 | + |
| 72 | + def get_abs_file_list(self): |
| 73 | + return [os.path.join(self.directory, path) for path in self.files.keys()] |
| 74 | + |
| 75 | + def get_checked_files(self): |
| 76 | + pass |
| 77 | + |
| 78 | + def item_changed(self, item, column): |
| 79 | + self.files[item.path] = item.checkState(column) |
| 80 | + |
| 81 | + def generate_directory_widget(self): |
| 82 | + if self.directory is None: |
| 83 | + return |
| 84 | + |
| 85 | + parent_map = {'': self.root} |
| 86 | + |
| 87 | + for root, dirs, files in os.walk(self.directory): |
| 88 | + for directory in dirs: |
| 89 | + |
| 90 | + proj_path = root.replace(self.directory, '') |
| 91 | + |
| 92 | + parent = parent_map[proj_path] |
| 93 | + |
| 94 | + path = os.path.join(proj_path, directory) |
| 95 | + |
| 96 | + checked = Qt.Unchecked |
| 97 | + |
| 98 | + for checked_file in self.checked_files: |
| 99 | + match = re.match('^'+checked_file, path) |
| 100 | + if match: |
| 101 | + checked = Qt.Checked |
| 102 | + |
| 103 | + child = FileItem(parent, path) |
| 104 | + child.setFlags(child.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable) |
| 105 | + child.setText(0, directory) |
| 106 | + child.setCheckState(0, checked) |
| 107 | + |
| 108 | + self.files[path] = checked |
| 109 | + |
| 110 | + parent_map[path] = child |
| 111 | + |
| 112 | + for file in files: |
| 113 | + proj_path = root.replace(self.directory, '') |
| 114 | + |
| 115 | + parent = parent_map[proj_path] |
| 116 | + |
| 117 | + path = os.path.join(proj_path, file) |
| 118 | + |
| 119 | + checked = Qt.Unchecked |
| 120 | + |
| 121 | + for checked_file in self.checked_files: |
| 122 | + match = re.match(checked_file, path) |
| 123 | + if match: |
| 124 | + checked = Qt.Checked |
| 125 | + |
| 126 | + child = FileItem(parent, path) |
| 127 | + child.setFlags(child.flags() | Qt.ItemIsUserCheckable) |
| 128 | + child.setText(0, file) |
| 129 | + child.setCheckState(0, checked) |
| 130 | + |
| 131 | + self.files[path] = checked |
| 132 | + |
| 133 | + self.watcher.addPaths(self.get_abs_file_list()) |
| 134 | + |
12 | 135 |
|
13 | 136 | class ExistingProjectDialog(QtGui.QDialog): |
14 | 137 | def __init__(self, recent_projects, directory_callback, parent=None): |
@@ -93,6 +216,7 @@ def project_clicked(self, _): |
93 | 216 | def cancelled(self): |
94 | 217 | self.close() |
95 | 218 |
|
| 219 | + |
96 | 220 | class Validator(QtGui.QRegExpValidator): |
97 | 221 | def __init__(self, regex, action, parent=None): |
98 | 222 | self.exp = regex |
|
0 commit comments