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