Ejemplo n.º 1
0
class FileBrowser(QMainWindow):
    """Example file browsing widget. Based of the C++ example."""
    def __init__(self, parent=None, flags=Qt.Widget):
        super(FileBrowser, self).__init__(parent, flags)

        self.gallery = QDocumentGallery(self)
        self.fileSystemModel = QFileSystemModel(self)

        self.rootPath = QDesktopServices.storageLocation(QDesktopServices.HomeLocation)
        self.fileSystemModel.setRootPath(self.rootPath)

        self.view = QListView()
        self.view.setModel(self.fileSystemModel)
        self.view.activated.connect(self.activated)

        self.setCentralWidget(self.view)

        self.menuBar().addAction(self.tr("Documents"), self.browseDocuments)
        self.menuBar().addAction(self.tr("Audio"), self.browseAudio)
        self.menuBar().addAction(self.tr("Images"), self.browseImages)
        self.menuBar().addAction(self.tr("Videos"), self.browseVideos)

        self.browseDocuments()

    def activated(self, index):
        fileInfo = self.fileSystemModel.fileInfo(index)

        if fileInfo.isDir() and fileInfo.fileName() != '.':
            if fileInfo.fileName() == '..':
                parent = self.view.rootIndex().parent()

                fileInfo = self.fileSystemModel.fileInfo(parent)

                if fileInfo.absoluteFilePath() == self.rootPath:
                    self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs)

                self.view.setRootIndex(parent)

            else:
                self.fileSystemModel.setFilter(QDir.AllEntries | QDir.AllDirs)
                self.view.setRootIndex(index)

            self.setWindowTitle(fileInfo.fileName())
        else:
            if fileInfo.fileName() == '.':
                fileInfo = self.fileSystemModel.fileInfo(self.view.rootIndex())

            widget = DocumentPropertiesWidget(fileInfo, self.gallery, self)
            widget.setWindowFlags(self.window().windowFlags() | Qt.Dialog)
            widget.setAttribute(Qt.WA_DeleteOnClose)
            widget.setWindowModality(Qt.WindowModal)
            widget.show()

    def browseAudio(self):
        self.rootPath = QDesktopServices.storageLocation(QDesktopServices.MusicLocation)
        self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs)
        self.view.setRootIndex(self.fileSystemModel.index(self.rootPath))
        self.setWindowTitle(self.tr("Audio"))

    def browseDocuments(self):
        self.rootPath = QDesktopServices.storageLocation(QDesktopServices.DocumentsLocation)
        self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs)
        self.view.setRootIndex(self.fileSystemModel.index(self.rootPath))
        self.setWindowTitle(self.tr("Documents"))

    def browseImages(self):
        self.rootPath = QDesktopServices.storageLocation(QDesktopServices.PicturesLocation)
        self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs)
        self.view.setRootIndex(self.fileSystemModel.index(self.rootPath))
        self.setWindowTitle(self.tr("Images"))

    def browseVideos(self):
        self.rootPath = QDesktopServices.storageLocation(QDesktopServices.MoviesLocation)
        self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs)
        self.view.setRootIndex(self.fileSystemModel.index(self.rootPath))
        self.setWindowTitle(self.tr("Videos"))
Ejemplo n.º 2
0
class FileBrowser(QMainWindow):
    """Example file browsing widget. Based of the C++ example."""
    def __init__(self, parent=None, flags=Qt.Widget):
        super(FileBrowser, self).__init__(parent, flags)

        self.gallery = QDocumentGallery(self)
        self.fileSystemModel = QFileSystemModel(self)

        self.rootPath = QDesktopServices.storageLocation(
            QDesktopServices.HomeLocation)
        self.fileSystemModel.setRootPath(self.rootPath)

        self.view = QListView()
        self.view.setModel(self.fileSystemModel)
        self.view.activated.connect(self.activated)

        self.setCentralWidget(self.view)

        self.menuBar().addAction(self.tr("Documents"), self.browseDocuments)
        self.menuBar().addAction(self.tr("Audio"), self.browseAudio)
        self.menuBar().addAction(self.tr("Images"), self.browseImages)
        self.menuBar().addAction(self.tr("Videos"), self.browseVideos)

        self.browseDocuments()

    def activated(self, index):
        fileInfo = self.fileSystemModel.fileInfo(index)

        if fileInfo.isDir() and fileInfo.fileName() != '.':
            if fileInfo.fileName() == '..':
                parent = self.view.rootIndex().parent()

                fileInfo = self.fileSystemModel.fileInfo(parent)

                if fileInfo.absoluteFilePath() == self.rootPath:
                    self.fileSystemModel.setFilter(QDir.AllEntries
                                                   | QDir.NoDotAndDotDot
                                                   | QDir.AllDirs)

                self.view.setRootIndex(parent)

            else:
                self.fileSystemModel.setFilter(QDir.AllEntries | QDir.AllDirs)
                self.view.setRootIndex(index)

            self.setWindowTitle(fileInfo.fileName())
        else:
            if fileInfo.fileName() == '.':
                fileInfo = self.fileSystemModel.fileInfo(self.view.rootIndex())

            widget = DocumentPropertiesWidget(fileInfo, self.gallery, self)
            widget.setWindowFlags(self.window().windowFlags() | Qt.Dialog)
            widget.setAttribute(Qt.WA_DeleteOnClose)
            widget.setWindowModality(Qt.WindowModal)
            widget.show()

    def browseAudio(self):
        self.rootPath = QDesktopServices.storageLocation(
            QDesktopServices.MusicLocation)
        self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot
                                       | QDir.AllDirs)
        self.view.setRootIndex(self.fileSystemModel.index(self.rootPath))
        self.setWindowTitle(self.tr("Audio"))

    def browseDocuments(self):
        self.rootPath = QDesktopServices.storageLocation(
            QDesktopServices.DocumentsLocation)
        self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot
                                       | QDir.AllDirs)
        self.view.setRootIndex(self.fileSystemModel.index(self.rootPath))
        self.setWindowTitle(self.tr("Documents"))

    def browseImages(self):
        self.rootPath = QDesktopServices.storageLocation(
            QDesktopServices.PicturesLocation)
        self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot
                                       | QDir.AllDirs)
        self.view.setRootIndex(self.fileSystemModel.index(self.rootPath))
        self.setWindowTitle(self.tr("Images"))

    def browseVideos(self):
        self.rootPath = QDesktopServices.storageLocation(
            QDesktopServices.MoviesLocation)
        self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot
                                       | QDir.AllDirs)
        self.view.setRootIndex(self.fileSystemModel.index(self.rootPath))
        self.setWindowTitle(self.tr("Videos"))