|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2010 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing specialized tree views. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import Qt |
|
11 from PyQt4.QtGui import QTreeView, QItemSelectionModel |
|
12 |
|
13 class E5TreeView(QTreeView): |
|
14 """ |
|
15 Class implementing a tree view supporting removal of entries. |
|
16 """ |
|
17 def keyPressEvent(self, evt): |
|
18 """ |
|
19 Protected method implementing special key handling. |
|
20 |
|
21 @param evt reference to the event (QKeyEvent) |
|
22 """ |
|
23 if evt.key() in [Qt.Key_Delete, Qt.Key_Backspace] and \ |
|
24 self.model() is not None: |
|
25 self.removeSelected() |
|
26 evt.setAccepted(True) |
|
27 else: |
|
28 QTreeView.keyPressEvent(self, evt) |
|
29 |
|
30 def removeSelected(self): |
|
31 """ |
|
32 Public method to remove the selected entries. |
|
33 """ |
|
34 if self.model() is None or \ |
|
35 self.selectionModel() is None or \ |
|
36 not self.selectionModel().hasSelection(): |
|
37 # no models available or nothing selected |
|
38 return |
|
39 |
|
40 selectedRows = self.selectionModel().selectedRows() |
|
41 for idx in reversed(sorted(selectedRows)): |
|
42 self.model().removeRow(idx.row(), idx.parent()) |
|
43 |
|
44 def removeAll(self): |
|
45 """ |
|
46 Public method to clear the view. |
|
47 """ |
|
48 if self.model() is not None: |
|
49 self.model().removeRows(0, self.model().rowCount(self.rootIndex()), |
|
50 self.rootIndex()) |