"""A simple model that emulates a QDirModel to provide file grouping by numeric ranges"""
def __init__(self, directory=None, parent=None):
if not directory:
directory = os.getcwd()
self._icon_provider = QdFileIconProvider()
self._directory = directory
self._hide_dot = True
self._hide_dotdot = False
self._show_hidden = False
self._name_filters = []
self._file_info = {}
self.
setHeaderData(0, Qt.
Horizontal, QtCore.
QVariant("Name"), Qt.
DisplayRole) self.setDirectory(self._directory)
def setDirectory(self, directory):
"""Sets the current directory being viewed on this model"""
self._directory = directory
self.refresh()
#
# Methods to mimic the QDirModel API
#
def data(self, index, role):
"""Overridden method to allow file icons to be drawn in views"""
# same as Qt.DecorationRole
return QtCore.
QVariant(self.
fileIcon(index
))
def flags(self, index):
return Qt.ItemIsSelectable | Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled | Qt.ItemIsEnabled
def setFilter(self, filters):
"""Interface method from QDirModel to allow filtering out certain results"""
if QtCore.
QDir.
NoDotAndDotDot & filters
: self._hide_dot = True
self._hide_dotdot = True
return
def setNameFilter(self, nameFilter):
"""Name filter (usually on extention) to filter out files"""
nameFilter = str(nameFilter)
match = re.match(".+\((?P<exts>.+\)", nameFilter)
if match:
return
self._name_filters = [nameFilter]
self.refresh()
def setIconProvider(self, icon_provider):
self._icon_provider = icon_provider
def iconProvider(self):
return self._icon_provider
def fileIcon(self, index):
"""Returns the icon for the given file/directory index"""
if not index.isValid:
return self._icon_provider.icon(self._file_info[str(self.fileName(index))])
def fileName(self, index):
"""Returns the basename of the file"""
return self.data(index, Qt.DisplayRole).toPyObject()
def filePath(self, index):
"""Returns the full path to the file"""
name = self.data(index, Qt.DisplayRole)
return os.path.join(self._directory, str(name.toPyObject()))
def isDir(self, index):
"""Checks if the given index is a file/dir"""
return os.path.isdir(self.filePath(index))
def refresh(self):
"""Reloads the model with a fresh call"""
cmd = "foo_ls --groupded"
output = os.popen(cmd).read()
self._file_info = {}
files = []
for line in output:
name = line.strip()
if name == "." and self._hide_dot:
continue
elif name == ".." and self._hide_dotdot:
continue
elif name.startswith(".") and not self._show_hidden:
continue
self._file_info
[name
] = QtCore.
QFileInfo(os.
path.
join(self._directory, name
)) files.append(name)
self.setStringList(self.customSort(files))
def customSort(self, entries, order=Qt.AscendingOrder):
"""Sorts the results in a manner similar to a QDirModel"""
dirs = []
files = []
indexes = {}
for name, info in self._file_info.iteritems():
indexes[entries.index(name)] = name
if info.isDir():
dirs.append(name)
continue
files.append(name)
reverseSort = order == Qt.DescendingOrder
files.sort(reverse=reverseSort)
dirs.sort(reverse=reverseSort)
correctOrder = []
if reverseSort:
correctOrder = files + dirs
else:
correctOrder = dirs+files
return correctOrder
def sort(self, column, order):
"""Overriden method that calls our own customSort"""
stringlist = self.stringList()
correctOrder = self.customSort(list(stringlist), order)
self.setStringList(correctOrder)
class QFileRangeModel(QtGui.QStringListModel):
"""A simple model that emulates a QDirModel to provide file grouping by numeric ranges"""
def __init__(self, directory=None, parent=None):
QtGui.QStringListModel.__init__(self)
if not directory:
directory = os.getcwd()
self._icon_provider = QdFileIconProvider()
self._directory = directory
self._hide_dot = True
self._hide_dotdot = False
self._show_hidden = False
self._name_filters = []
self._file_info = {}
self.setHeaderData(0, Qt.Horizontal, QtCore.QVariant("Name"), Qt.DisplayRole)
self.setDirectory(self._directory)
def setDirectory(self, directory):
"""Sets the current directory being viewed on this model"""
self._directory = directory
self.refresh()
#
# Methods to mimic the QDirModel API
#
def data(self, index, role):
"""Overridden method to allow file icons to be drawn in views"""
# same as Qt.DecorationRole
if role == QtGui.QDirModel.FileIconRole:
return QtCore.QVariant(self.fileIcon(index))
return QtGui.QStringListModel.data(self, index, role)
def flags(self, index):
return Qt.ItemIsSelectable | Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled | Qt.ItemIsEnabled
def setFilter(self, filters):
"""Interface method from QDirModel to allow filtering out certain results"""
if QtCore.QDir.NoDotAndDotDot & filters:
self._hide_dot = True
self._hide_dotdot = True
return
def setNameFilter(self, nameFilter):
"""Name filter (usually on extention) to filter out files"""
nameFilter = str(nameFilter)
match = re.match(".+\((?P<exts>.+\)", nameFilter)
if match:
return
self._name_filters = [nameFilter]
self.refresh()
def setIconProvider(self, icon_provider):
self._icon_provider = icon_provider
def iconProvider(self):
return self._icon_provider
def fileIcon(self, index):
"""Returns the icon for the given file/directory index"""
if not index.isValid:
return self._icon_provider.icon(QtGui.QFileIconProvider.Computer)
return self._icon_provider.icon(self._file_info[str(self.fileName(index))])
def fileName(self, index):
"""Returns the basename of the file"""
return self.data(index, Qt.DisplayRole).toPyObject()
def filePath(self, index):
"""Returns the full path to the file"""
name = self.data(index, Qt.DisplayRole)
return os.path.join(self._directory, str(name.toPyObject()))
def isDir(self, index):
"""Checks if the given index is a file/dir"""
return os.path.isdir(self.filePath(index))
def refresh(self):
"""Reloads the model with a fresh call"""
cmd = "foo_ls --groupded"
output = os.popen(cmd).read()
self._file_info = {}
files = []
for line in output:
name = line.strip()
if name == "." and self._hide_dot:
continue
elif name == ".." and self._hide_dotdot:
continue
elif name.startswith(".") and not self._show_hidden:
continue
self._file_info[name] = QtCore.QFileInfo(os.path.join(self._directory, name))
files.append(name)
self.setStringList(self.customSort(files))
def customSort(self, entries, order=Qt.AscendingOrder):
"""Sorts the results in a manner similar to a QDirModel"""
dirs = []
files = []
indexes = {}
for name, info in self._file_info.iteritems():
indexes[entries.index(name)] = name
if info.isDir():
dirs.append(name)
continue
files.append(name)
reverseSort = order == Qt.DescendingOrder
files.sort(reverse=reverseSort)
dirs.sort(reverse=reverseSort)
correctOrder = []
if reverseSort:
correctOrder = files + dirs
else:
correctOrder = dirs+files
return correctOrder
def sort(self, column, order):
"""Overriden method that calls our own customSort"""
stringlist = self.stringList()
correctOrder = self.customSort(list(stringlist), order)
self.setStringList(correctOrder)
To copy to clipboard, switch view to plain text mode
Bookmarks