eric6/WebBrowser/Download/DownloadModel.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7923
91e843545d9a
child 8143
2c730d5fd177
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

# -*- coding: utf-8 -*-

# Copyright (c) 2010 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the download model.
"""

from PyQt5.QtCore import Qt, QAbstractListModel, QModelIndex, QMimeData, QUrl


class DownloadModel(QAbstractListModel):
    """
    Class implementing the download model.
    """
    def __init__(self, manager, parent=None):
        """
        Constructor
        
        @param manager reference to the download manager
        @type DownloadManager
        @param parent reference to the parent object
        @type QObject
        """
        super(DownloadModel, self).__init__(parent)
        
        self.__manager = manager
    
    def data(self, index, role):
        """
        Public method to get data from the model.
        
        @param index index to get data for
        @type QModelIndex
        @param role role of the data to retrieve
        @type int
        @return requested data
        @rtype any
        """
        if index.row() < 0 or index.row() >= self.rowCount(index.parent()):
            return None
        
        if role == Qt.ToolTipRole:
            if (
                self.__manager.downloads()[index.row()]
                .downloadedSuccessfully()
            ):
                return self.__manager.downloads()[index.row()].getInfoData()
        
        return None
    
    def rowCount(self, parent=None):
        """
        Public method to get the number of rows of the model.
        
        @param parent parent index
        @type QModelIndex
        @return number of rows
        @rtype int
        """
        if parent is None:
            parent = QModelIndex()
        
        if parent.isValid():
            return 0
        else:
            return self.__manager.downloadsCount()
    
    def removeRows(self, row, count, parent=None):
        """
        Public method to remove downloads from the model.
        
        @param row row of the first download to remove
        @type int
        @param count number of downloads to remove
        @type int
        @param parent index of the parent download node
        @type QModelIndex
        @return flag indicating successful removal
        @rtype bool
        """
        if parent is None:
            parent = QModelIndex()
        
        if parent.isValid():
            return False
        
        if row < 0 or count <= 0 or row + count > self.rowCount(parent):
            return False
        
        lastRow = row + count - 1
        for i in range(lastRow, row - 1, -1):
            if not self.__manager.downloads()[i].downloading():
                self.beginRemoveRows(parent, i, i)
                del self.__manager.downloads()[i]
                self.endRemoveRows()
        self.__manager.changeOccurred()
        return True
    
    def flags(self, index):
        """
        Public method to get flags for an item.
        
        @param index index of the node cell
        @type QModelIndex
        @return flags
        @rtype Qt.ItemFlags
        """
        if index.row() < 0 or index.row() >= self.rowCount(index.parent()):
            return Qt.NoItemFlags
        
        defaultFlags = QAbstractListModel.flags(self, index)
        
        itm = self.__manager.downloads()[index.row()]
        if itm.downloadedSuccessfully():
            return defaultFlags | Qt.ItemIsDragEnabled
        
        return defaultFlags
    
    def mimeData(self, indexes):
        """
        Public method to return the mime data.
        
        @param indexes list of indexes
        @type QModelIndexList
        @return mime data
        @rtype QMimeData
        """
        mimeData = QMimeData()
        urls = []
        for index in indexes:
            if index.isValid():
                itm = self.__manager.downloads()[index.row()]
                urls.append(QUrl.fromLocalFile(itm.absoluteFilePath()))
        mimeData.setUrls(urls)
        return mimeData

eric ide

mercurial