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