E5Gui/E5TableView.py

Fri, 31 Dec 2010 15:49:50 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 31 Dec 2010 15:49:50 +0100
changeset 791
9ec2ac20e54e
parent 55
b5c84934de9c
child 945
8cd4d08fa9f6
child 1510
e75ecf2bd9dd
permissions
-rw-r--r--

Updated copyright notice.

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

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

"""
Module implementing specialized table views. 
"""

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QTableView, QItemSelectionModel

class E5TableView(QTableView):
    """
    Class implementing a table 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_Delete, Qt.Key_Backspace] and \
           self.model() is not None:
            self.removeSelected()
            evt.setAccepted(True)
        else:
            QTableView.keyPressEvent(self, evt)
    
    def removeSelected(self):
        """
        Public method to remove the selected entries.
        """
        if self.model() is None or self.selectionModel() is None:
            # no models available
            return
        
        row = 0
        selectedRows = self.selectionModel().selectedRows()
        for selectedRow in reversed(selectedRows):
            row = selectedRow.row()
            self.model().removeRow(row, self.rootIndex())
        
        idx = self.model().index(row, 0, self.rootIndex())
        if not idx.isValid():
            idx = self.model().index(row - 1, 0, self.rootIndex())
        self.selectionModel().select(idx, 
            QItemSelectionModel.SelectCurrent | QItemSelectionModel.Rows)
        self.setCurrentIndex(idx)
    
    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