Hi again!

Op den Dunnersdag 25 November 2010 Klock 21:15:19 hett Gionata Boccalini 
schreven:
> Ok, don't worry, I absolutely don't want to waste your time!! But what
> do you mean with
> 
> > In
> > general, your code could (and should) be much more concise.

OK, I had a look again, your code is not much too complex actually.

Anyhow, there's potential for optimization IMHO:

- [duplicate/complex code] remove all QTimer and resizeColumnToContents stuff, 
instead use self.__fileView.header().setResizeMode(0, 
QtGui.QHeaderView.ResizeToContents) *

- for the minimal example for this list, the updateSelection() stuff is 
unnecessary (the smaller the example, the more likely you'll get answers)

- [consistency] rename __treeView to __dirView

- [simplicity] instead of connecting to clicked/expanded on the left, what 
about currentChanged?

- [duplicate code] in any case, don't setup the filemodel's root in __init__, 
but make sure that the connected signals are emitted - in my case, I would 
call setCurrentIndex - in order to let updateFiles() to the job

- [duplicate code] setup the treeviews with your visual preferences only once

- maybe disable expansion in the __fileView (and decoration)

Attached you find a refactored version of your example.

Then, it is easy to solve your problem by just re-creating the model in every 
updateFiles() step.  I am attaching that, too.

* I just realized that manual resizing might still be desirable.  Anyhow, the 
timer solution looks quite hacky to my eyes.

HTH,
  Hans
import sys

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QDir, Qt, QFileInfo
from PyQt4.QtGui import QSplitter


class FileBrowser(QSplitter):
    def __init__(self, parent = None):
        super(FileBrowser, self).__init__(parent)

        # Directory tree and model
        self.__dirModel = QtGui.QFileSystemModel()
        self.__dirModel.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot)

        self.__dirView = self._setupTreeView()
        self.__dirView.setModel(self.__dirModel)
        self.__dirView.setRootIndex(
            self.__dirModel.setRootPath(QDir.rootPath()))
        self.__dirView.hideColumn(1)
        self.__dirView.hideColumn(2)

        self.__dirView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
        self.__dirView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        self.__dirView.selectionModel().currentChanged.connect(self.updateFiles)

        # File tree and model
        self.__fileModel = QtGui.QFileSystemModel()
        self.__fileModel.setFilter(QDir.Files)

        self.__fileView = self._setupTreeView()
        self.__fileView.setModel(self.__fileModel)
        self.__fileView.setRootIsDecorated(False)
        self.__fileView.setItemsExpandable(False)

        # Splitter layout
        self.setOrientation(Qt.Horizontal)
        self.addWidget(self.__dirView)
        self.addWidget(self.__fileView)

        # Default sorting and start location (homePath)
        self.__dirView.sortByColumn(0, Qt.AscendingOrder)
        self.__fileView.sortByColumn(2, Qt.AscendingOrder)
        self.__dirView.setCurrentIndex(
            self.__dirModel.index(QDir.homePath()))


    def _setupTreeView(self):
        result = QtGui.QTreeView(self)
        result.setSortingEnabled(True)
        result.setAlternatingRowColors(True)
        result.setAnimated(True)
        result.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)
        return result


    @QtCore.pyqtSlot('QModelIndex')
    def updateFiles(self,  index):
        dirPath = self.__dirModel.filePath(index)
        self.__fileModel.setRootPath(dirPath)
        self.__fileView.setRootIndex(self.__fileModel.index(dirPath))



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    MainWindowClass = QtGui.QMainWindow()
    fileb = FileBrowser()
    fileb.resize(1000, 600)
    fileb.show()
    sys.exit(app.exec_())
import sys

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QDir, Qt, QFileInfo
from PyQt4.QtGui import QSplitter


class FileBrowser(QSplitter):
    def __init__(self, parent = None):
        super(FileBrowser, self).__init__(parent)

        # Directory tree and model
        self.__dirModel = QtGui.QFileSystemModel()
        self.__dirModel.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot)

        self.__dirView = self._setupTreeView()
        self.__dirView.setModel(self.__dirModel)
        self.__dirView.setRootIndex(
            self.__dirModel.setRootPath(QDir.rootPath()))
        self.__dirView.hideColumn(1)
        self.__dirView.hideColumn(2)

        self.__dirView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
        self.__dirView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        self.__dirView.selectionModel().currentChanged.connect(self.updateFiles)

        # File tree
        self.__fileView = self._setupTreeView()
        self.__fileView.setRootIsDecorated(False)
        self.__fileView.setItemsExpandable(False)

        # Splitter layout
        self.setOrientation(Qt.Horizontal)
        self.addWidget(self.__dirView)
        self.addWidget(self.__fileView)

        # Default sorting and start location (homePath)
        self.__dirView.sortByColumn(0, Qt.AscendingOrder)
        self.__fileView.sortByColumn(2, Qt.AscendingOrder)
        self.__dirView.setCurrentIndex(
            self.__dirModel.index(QDir.homePath()))


    def _setupTreeView(self):
        result = QtGui.QTreeView(self)
        result.setSortingEnabled(True)
        result.setAlternatingRowColors(True)
        result.setAnimated(True)
        result.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)
        return result


    @QtCore.pyqtSlot('QModelIndex')
    def updateFiles(self,  index):
        dirPath = self.__dirModel.filePath(index)
        self.__fileModel = QtGui.QFileSystemModel()
        self.__fileModel.setFilter(QDir.Files)
        self.__fileModel.setRootPath(dirPath)
        self.__fileView.setModel(self.__fileModel)
        self.__fileView.setRootIndex(self.__fileModel.index(dirPath))



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    MainWindowClass = QtGui.QMainWindow()
    fileb = FileBrowser()
    fileb.resize(1000, 600)
    fileb.show()
    sys.exit(app.exec_())
_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to