eric7/E5Gui/EricTreeView.py

Sat, 22 May 2021 18:51:46 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 22 May 2021 18:51:46 +0200
branch
eric7
changeset 8356
68ec9c3d4de5
parent 8318
eric7/E5Gui/E5TreeView.py@962bce857696
permissions
-rw-r--r--

Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.

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

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

"""
Module implementing specialized tree views.
"""

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QTreeView


class EricTreeView(QTreeView):
    """
    Class implementing a tree view supporting removal of entries.
    """
    def keyPressEvent(self, evt):
        """
        Protected method implementing special key handling.
        
        @param evt reference to the event (QKeyEvent)
        """
        if (
            evt.key() in [Qt.Key.Key_Delete, Qt.Key.Key_Backspace] and
            self.model() is not None
        ):
            self.removeSelected()
            evt.setAccepted(True)
        else:
            super().keyPressEvent(evt)
    
    def removeSelected(self):
        """
        Public method to remove the selected entries.
        """
        if (
            self.model() is None or
            self.selectionModel() is None or
            not self.selectionModel().hasSelection()
        ):
            # no models available or nothing selected
            return
        
        selectedRows = self.selectionModel().selectedRows()
        for idx in reversed(sorted(selectedRows)):
            self.model().removeRow(idx.row(), idx.parent())
    
    def removeAll(self):
        """
        Public method to clear the view.
        """
        if self.model() is not None:
            self.model().removeRows(0, self.model().rowCount(self.rootIndex()),
                                    self.rootIndex())

eric ide

mercurial