Esempio n. 1
0
 def data(self, index: QModelIndex, role: int):
     """Modification des datas"""
     if role == Qt.DecorationRole:
         info = self.fileInfo(index)
         if info.isFile():
             fichier = UserFile(info.filePath())
             return fichier.pixMap()
     return QFileSystemModel.data(self, index, role)
Esempio n. 2
0
class AbstractPathCompleter(AbstractCompleter):
    """Base class for PathCompleter and GlobCompleter
    """

    mustBeLoaded = True

    # global object. Reused by all completers
    _fsModel = QFileSystemModel()

    _ERROR = 'error'
    _HEADER = 'currentDir'
    _STATUS = 'status'
    _DIRECTORY = 'directory'
    _FILE = 'file'

    def __init__(self, text):
        self._originalText = text
        self._dirs = []
        self._files = []
        self._error = None
        self._status = None
        """hlamer: my first approach is making self._model static member of class. But, sometimes it
        returns incorrect icons. I really can't understand when and why.
        When it is private member of instance, it seems it works
        """
        self._model = None  # can't construct in the construtor, must be constructed in GUI thread

    @staticmethod
    def _filterHidden(paths):
        """Remove hidden and ignored files from the list
        """
        return [
            path for path in paths
            if not os.path.basename(path).startswith('.')
            and not core.fileFilter().regExp().match(path)
        ]

    def _classifyRowIndex(self, row):
        """Get list item type and index by it's row
        """

        if self._error:
            assert row == 0
            return (self._ERROR, 0)

        if row == 0:
            return (self._HEADER, 0)

        row -= 1
        if self._status:
            if row == 0:
                return (self._STATUS, 0)
            row -= 1

        if row in range(len(self._dirs)):
            return (self._DIRECTORY, row)
        row -= len(self._dirs)

        if row in range(len(self._files)):
            return (self._FILE, row)

        assert False

    def _formatHeader(self, text):
        """Format current directory for show it in the list of completions
        """
        return '<font style="background-color: %s; color: %s">%s</font>' % \
            (QApplication.instance().palette().color(QPalette.Window).name(),
             QApplication.instance().palette().color(QPalette.WindowText).name(),
             htmlEscape(text))

    def rowCount(self):
        """Row count in the list of completions
        """
        if self._error:
            return 1
        else:
            count = 1  # current directory
            if self._status:
                count += 1
            count += len(self._dirs)
            count += len(self._files)
            return count

    def _iconForPath(self, path):
        """Get icon for file or directory path. Uses QFileSystemModel
        """
        if self._model is None:
            self._model = QFileSystemModel()

        index = self._model.index(path)
        return self._model.data(index, Qt.DecorationRole)

    def text(self, row, column):
        """Item text in the list of completions
        """
        rowType, index = self._classifyRowIndex(row)
        if rowType == self._ERROR:
            return '<font color=red>%s</font>' % htmlEscape(self._error)
        elif rowType == self._HEADER:
            return self._formatHeader(self._headerText())
        elif rowType == self._STATUS:
            return '<i>%s</i>' % htmlEscape(self._status)
        elif rowType == self._DIRECTORY:
            return self._formatPath(self._dirs[index], True)
        elif rowType == self._FILE:
            return self._formatPath(self._files[index], False)

    def icon(self, row, column):
        """Item icon in the list of completions
        """
        rowType, index = self._classifyRowIndex(row)
        if rowType == self._ERROR:
            return QApplication.instance().style().standardIcon(
                QStyle.SP_MessageBoxCritical)
        elif rowType == self._HEADER:
            return None
        elif rowType == self._STATUS:
            return None
        elif rowType == self._DIRECTORY:
            return self._iconForPath(self._dirs[index])
        elif rowType == self._FILE:
            return self._iconForPath(self._files[index])

    def isSelectable(self, row, column):
        rowType, index = self._classifyRowIndex(row)
        return rowType in (self._DIRECTORY, self._FILE)

    def getFullText(self, row):
        """User clicked a row. Get inline completion for this row
        """
        row -= 1  # skip current directory
        if row in range(len(self._dirs)):
            return self._dirs[row] + '/'
        else:
            row -= len(self._dirs)  # skip dirs
            if row in range(len(self._files)):
                return self._files[row]

        return None
Esempio n. 3
0
class AbstractPathCompleter(AbstractCompleter):
    """Base class for PathCompleter and GlobCompleter
    """

    mustBeLoaded = True

    # global object. Reused by all completers
    _fsModel = QFileSystemModel()

    _ERROR = 'error'
    _HEADER = 'currentDir'
    _STATUS = 'status'
    _DIRECTORY = 'directory'
    _FILE = 'file'

    def __init__(self, text):
        self._originalText = text
        self._dirs = []
        self._files = []
        self._error = None
        self._status = None

        """hlamer: my first approach is making self._model static member of class. But, sometimes it
        returns incorrect icons. I really can't understand when and why.
        When it is private member of instance, it seems it works
        """
        self._model = None  # can't construct in the construtor, must be constructed in GUI thread

    @staticmethod
    def _filterHidden(paths):
        """Remove hidden and ignored files from the list
        """
        return [path for path in paths
                if not os.path.basename(path).startswith('.') and
                not core.fileFilter().regExp().match(path)]

    def _classifyRowIndex(self, row):
        """Get list item type and index by it's row
        """

        if self._error:
            assert row == 0
            return (self._ERROR, 0)

        if row == 0:
            return (self._HEADER, 0)

        row -= 1
        if self._status:
            if row == 0:
                return (self._STATUS, 0)
            row -= 1

        if row in range(len(self._dirs)):
            return (self._DIRECTORY, row)
        row -= len(self._dirs)

        if row in range(len(self._files)):
            return (self._FILE, row)

        assert False

    def _formatHeader(self, text):
        """Format current directory for show it in the list of completions
        """
        return '<font style="background-color: %s; color: %s">%s</font>' % \
            (QApplication.instance().palette().color(QPalette.Window).name(),
             QApplication.instance().palette().color(QPalette.WindowText).name(),
             htmlEscape(text))

    def rowCount(self):
        """Row count in the list of completions
        """
        if self._error:
            return 1
        else:
            count = 1  # current directory
            if self._status:
                count += 1
            count += len(self._dirs)
            count += len(self._files)
            return count

    def _iconForPath(self, path):
        """Get icon for file or directory path. Uses QFileSystemModel
        """
        if self._model is None:
            self._model = QFileSystemModel()

        index = self._model.index(path)
        return self._model.data(index, Qt.DecorationRole)

    def text(self, row, column):
        """Item text in the list of completions
        """
        rowType, index = self._classifyRowIndex(row)
        if rowType == self._ERROR:
            return '<font color=red>%s</font>' % htmlEscape(self._error)
        elif rowType == self._HEADER:
            return self._formatHeader(self._headerText())
        elif rowType == self._STATUS:
            return '<i>%s</i>' % htmlEscape(self._status)
        elif rowType == self._DIRECTORY:
            return self._formatPath(self._dirs[index], True)
        elif rowType == self._FILE:
            return self._formatPath(self._files[index], False)

    def icon(self, row, column):
        """Item icon in the list of completions
        """
        rowType, index = self._classifyRowIndex(row)
        if rowType == self._ERROR:
            return QApplication.instance().style().standardIcon(QStyle.SP_MessageBoxCritical)
        elif rowType == self._HEADER:
            return None
        elif rowType == self._STATUS:
            return None
        elif rowType == self._DIRECTORY:
            return self._iconForPath(self._dirs[index])
        elif rowType == self._FILE:
            return self._iconForPath(self._files[index])

    def isSelectable(self, row, column):
        rowType, index = self._classifyRowIndex(row)
        return rowType in (self._DIRECTORY, self._FILE)

    def getFullText(self, row):
        """User clicked a row. Get inline completion for this row
        """
        row -= 1  # skip current directory
        if row in range(len(self._dirs)):
            return self._dirs[row] + '/'
        else:
            row -= len(self._dirs)  # skip dirs
            if row in range(len(self._files)):
                return self._files[row]

        return None
Esempio n. 4
0
 def data(self, index, role):
     if role == Qt.ToolTipRole:
         return self.filePath(index)
     else:
         return QFileSystemModel.data(self, index, role)
 def data(self, index, role=Qt.DisplayRole):
     if role != Qt.CheckStateRole:
         return QFileSystemModel.data(self, index, role)
     else:
         if index.column() == 0:
             return self.checkState(index)