Monday, 31 May 2010

Filesystem TreeView with Checkboxes

This post isn't about Gereqi specifically but more to do with something I made during recent development. Currently I am trying to copy the Configuration dialog of Amarok to make Gereqi a bit more user friendly.

Amarok's filesystem view looks like this:

Unfortunately, in PyQt4 (or Qt4) there's no easy way to have those checkBoxes so it would like this:

With some clever c++ hackery made by a member of the qtcentre forums I was able to translate that into PyQt4 despite not knowing c++ and some of Qt4 specific features such as QSet, which I still haven't the foggiest about.

This PyQt4 code, for future reference and to help some poor soul dealing with the lack of PyQt4 examples and documentation on the internet, looks exactly like this (one day I might properly explain or comment it:

from PyQt4.QtCore import QDirModel

class MyQDirModel(QDirModel):
checked = []

def data(self, index, role = Qt.DisplayRole):
if index.isValid() and (index.column() == 0) and (role == Qt.CheckStateRole):
# the item is checked only if we have stored its path
if self.filePath(index) in MyQDirModel.checked:
return Qt.Checked
return Qt.Unchecked

return, index, role)

def flags(self, index):
if index.column() == 0: # make the first column checkable
return QDirModel.flags(self, index) | Qt.ItemIsUserCheckable
return QDirModel.flags(self, index)

def setData(self, index, value, role = Qt.EditRole):
if index.isValid() and (index.column() == 0) and role == Qt.CheckStateRole:
# store checked paths, remove unchecked paths
if (value == Qt.Checked):
return True
return True

return QDirModel.setData(self, index, value, role);

Simply put the above creates a "new" QDirModel Class with some custom methods as the default ones don't allow for this behaviour. Just swap where you were going to QDirModel with MyQDirModel.

The result of all this? A filesystem view the same as Amarok's

So that's one of the thing's I've been recently working on for version 0.4.0. Other things include directory watching, equaliser and sessioning (your Ui settings are saved to be loaded on restart).

There's more to come for 0.4.0 so keep your eye's peeled.

No comments:

Post a Comment