Results 1 to 4 of 4

Thread: Custom file browser - grouping files

  1. #1
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Custom file browser - grouping files

    I'm trying to create a custom image file browser. Eventually I want it to have an image preview for the selected image and other things specific to an image browser.

    However, I first want to be able to group files together. I want to take a list of files such as these:

    foo.0001.png
    foo.0002.png
    foo.0003.png
    foo.0004.png
    And display them like this

    foo.[0001-0004].png
    The builtin QFileDialog will display them as distinct files whereas I want to have an option to display them grouped.

    Is it even possible to do this by subclassing QDirModel? Because, I think that would be the nicest solution but it doesn't seem trivial. Or can this be done using a proxy model?

    I can certainly write my own model from scratch (even keep it simple and ignore size and date information) but if its possible to do it with a QDirModel and/or a proxy model that would be great

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom file browser - grouping files

    Might be you can subclass QDirModel and override the function QDirModel::sort . Hope it might work

  3. #3
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: Custom file browser - grouping files

    Update.

    Subclassing QDirModel is harder than one might think so I just created a model to mimic a QDirModel... Downsides: read only, and for the time being only presents one column "name". But thats only because its hard to determine size/date/user/etc for a group of files. I just tossed this in to a QListView and it works pretty darn well for a file open/save dialog

    Qt Code:
    1. class QFileRangeModel(QtGui.QStringListModel):
    2. """A simple model that emulates a QDirModel to provide file grouping by numeric ranges"""
    3. def __init__(self, directory=None, parent=None):
    4. QtGui.QStringListModel.__init__(self)
    5. if not directory:
    6. directory = os.getcwd()
    7.  
    8. self._icon_provider = QdFileIconProvider()
    9. self._directory = directory
    10.  
    11. self._hide_dot = True
    12. self._hide_dotdot = False
    13. self._show_hidden = False
    14.  
    15. self._name_filters = []
    16.  
    17. self._file_info = {}
    18.  
    19. self.setHeaderData(0, Qt.Horizontal, QtCore.QVariant("Name"), Qt.DisplayRole)
    20. self.setDirectory(self._directory)
    21.  
    22. def setDirectory(self, directory):
    23. """Sets the current directory being viewed on this model"""
    24. self._directory = directory
    25. self.refresh()
    26.  
    27. #
    28. # Methods to mimic the QDirModel API
    29. #
    30.  
    31. def data(self, index, role):
    32. """Overridden method to allow file icons to be drawn in views"""
    33. # same as Qt.DecorationRole
    34. if role == QtGui.QDirModel.FileIconRole:
    35. return QtCore.QVariant(self.fileIcon(index))
    36. return QtGui.QStringListModel.data(self, index, role)
    37.  
    38. def flags(self, index):
    39. return Qt.ItemIsSelectable | Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled | Qt.ItemIsEnabled
    40.  
    41. def setFilter(self, filters):
    42. """Interface method from QDirModel to allow filtering out certain results"""
    43. if QtCore.QDir.NoDotAndDotDot & filters:
    44. self._hide_dot = True
    45. self._hide_dotdot = True
    46. return
    47.  
    48. def setNameFilter(self, nameFilter):
    49. """Name filter (usually on extention) to filter out files"""
    50. nameFilter = str(nameFilter)
    51. match = re.match(".+\((?P<exts>.+\)", nameFilter)
    52. if match:
    53. return
    54. self._name_filters = [nameFilter]
    55. self.refresh()
    56.  
    57. def setIconProvider(self, icon_provider):
    58. self._icon_provider = icon_provider
    59.  
    60. def iconProvider(self):
    61. return self._icon_provider
    62.  
    63. def fileIcon(self, index):
    64. """Returns the icon for the given file/directory index"""
    65. if not index.isValid:
    66. return self._icon_provider.icon(QtGui.QFileIconProvider.Computer)
    67. return self._icon_provider.icon(self._file_info[str(self.fileName(index))])
    68.  
    69. def fileName(self, index):
    70. """Returns the basename of the file"""
    71. return self.data(index, Qt.DisplayRole).toPyObject()
    72.  
    73. def filePath(self, index):
    74. """Returns the full path to the file"""
    75. name = self.data(index, Qt.DisplayRole)
    76. return os.path.join(self._directory, str(name.toPyObject()))
    77.  
    78. def isDir(self, index):
    79. """Checks if the given index is a file/dir"""
    80. return os.path.isdir(self.filePath(index))
    81.  
    82. def refresh(self):
    83. """Reloads the model with a fresh call"""
    84. cmd = "foo_ls --groupded"
    85. output = os.popen(cmd).read()
    86.  
    87. self._file_info = {}
    88.  
    89. files = []
    90. for line in output:
    91. name = line.strip()
    92. if name == "." and self._hide_dot:
    93. continue
    94. elif name == ".." and self._hide_dotdot:
    95. continue
    96. elif name.startswith(".") and not self._show_hidden:
    97. continue
    98. self._file_info[name] = QtCore.QFileInfo(os.path.join(self._directory, name))
    99. files.append(name)
    100.  
    101. self.setStringList(self.customSort(files))
    102.  
    103. def customSort(self, entries, order=Qt.AscendingOrder):
    104. """Sorts the results in a manner similar to a QDirModel"""
    105. dirs = []
    106. files = []
    107. indexes = {}
    108. for name, info in self._file_info.iteritems():
    109. indexes[entries.index(name)] = name
    110. if info.isDir():
    111. dirs.append(name)
    112. continue
    113. files.append(name)
    114.  
    115. reverseSort = order == Qt.DescendingOrder
    116. files.sort(reverse=reverseSort)
    117. dirs.sort(reverse=reverseSort)
    118.  
    119. correctOrder = []
    120. if reverseSort:
    121. correctOrder = files + dirs
    122. else:
    123. correctOrder = dirs+files
    124.  
    125. return correctOrder
    126.  
    127. def sort(self, column, order):
    128. """Overriden method that calls our own customSort"""
    129. stringlist = self.stringList()
    130. correctOrder = self.customSort(list(stringlist), order)
    131. self.setStringList(correctOrder)
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to chezifresh for this useful post:

    giowck (25th September 2009)

  5. #4

    Default Re: Custom file browser - grouping files

    thanks for posting this !

    wil give it a try tonight

Similar Threads

  1. Eclipse, Moc Files, Custom Build Steps
    By gmat4321 in forum Qt Programming
    Replies: 4
    Last Post: 6th August 2010, 06:25
  2. Extracting files from a zip file
    By Althor in forum Qt Programming
    Replies: 4
    Last Post: 11th March 2009, 11:39
  3. Replies: 5
    Last Post: 22nd September 2006, 09:04
  4. Replies: 11
    Last Post: 4th July 2006, 16:09
  5. Opening swf file in the default browser
    By munna in forum Qt Programming
    Replies: 16
    Last Post: 5th May 2006, 10:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.