-
-
Save ben-hearn-sb/ab2693db819248efea85ecef355e0887 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PySide2 import QtCore, QtWidgets, QtGui | |
class Tree(QtWidgets.QTreeView): | |
def __init__(self): | |
super(Tree, self).__init__() | |
model = QtWidgets.QFileSystemModel(self) | |
model.setReadOnly(True) | |
skip_exts = ['.wav', '.mp3'] | |
proxy = HideFileTypesProxy(excludes=skip_exts, parent=self) | |
#proxy = ProxyModel(model) | |
#proxy.set_exclusion(('name', '.jpg')) | |
proxy.setDynamicSortFilter(True) | |
proxy.setSourceModel(model) | |
self.setModel(proxy) | |
#root = "/Users/benhearn/Music/tracks_new/2020" | |
root = '/Users/benhearn/Music/tracks_new/2020/_not_micro/' | |
idx = model.setRootPath(root) | |
self.setRootIndex(proxy.mapFromSource(idx)) | |
self._model = model | |
self._proxy = proxy | |
self.resize(600, 400) | |
class HideFileTypesProxy(QtCore.QSortFilterProxyModel): | |
""" | |
A proxy model that excludes files from the view | |
that end with the given extension | |
""" | |
def __init__(self, excludes, *args, **kwargs): | |
super(HideFileTypesProxy, self).__init__(*args, **kwargs) | |
self._excludes = excludes[:] | |
def filterAcceptsRow(self, srcRow, srcParent): | |
idx = self.sourceModel().index(srcRow, 0, srcParent) | |
name = idx.data() | |
# Can do whatever kind of tests you want here, | |
# against the name | |
for exc in self._excludes: | |
if not name.endswith(exc): | |
return False | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment