|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a special list widget for GreaseMonkey scripts. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSignal, QRect |
|
11 from PyQt6.QtWidgets import QListWidget, QListWidgetItem |
|
12 |
|
13 from .GreaseMonkeyConfigurationListDelegate import ( |
|
14 GreaseMonkeyConfigurationListDelegate |
|
15 ) |
|
16 |
|
17 |
|
18 class GreaseMonkeyConfigurationListWidget(QListWidget): |
|
19 """ |
|
20 Class implementing a special list widget for GreaseMonkey scripts. |
|
21 |
|
22 @signal removeItemRequested(item) emitted to indicate an item removal |
|
23 request (QListWidgetItem) |
|
24 """ |
|
25 removeItemRequested = pyqtSignal(QListWidgetItem) |
|
26 |
|
27 def __init__(self, parent=None): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param parent reference to the parent widget (QWidget) |
|
32 """ |
|
33 super().__init__(parent) |
|
34 |
|
35 self.__delegate = GreaseMonkeyConfigurationListDelegate(self) |
|
36 self.setItemDelegate(self.__delegate) |
|
37 |
|
38 def __containsRemoveIcon(self, pos): |
|
39 """ |
|
40 Private method to check, if the given position is inside the remove |
|
41 icon. |
|
42 |
|
43 @param pos position to check for (QPoint) |
|
44 @return flag indicating success (boolean) |
|
45 """ |
|
46 itm = self.itemAt(pos) |
|
47 if itm is None: |
|
48 return False |
|
49 |
|
50 rect = self.visualItemRect(itm) |
|
51 iconSize = GreaseMonkeyConfigurationListDelegate.RemoveIconSize |
|
52 removeIconXPos = rect.right() - self.__delegate.padding() - iconSize |
|
53 center = rect.height() // 2 + rect.top() |
|
54 removeIconYPos = center - iconSize // 2 |
|
55 |
|
56 removeIconRect = QRect(removeIconXPos, removeIconYPos, |
|
57 iconSize, iconSize) |
|
58 return removeIconRect.contains(pos) |
|
59 |
|
60 def mousePressEvent(self, evt): |
|
61 """ |
|
62 Protected method handling presses of mouse buttons. |
|
63 |
|
64 @param evt mouse press event (QMouseEvent) |
|
65 """ |
|
66 if self.__containsRemoveIcon(evt.position().toPoint()): |
|
67 self.removeItemRequested.emit( |
|
68 self.itemAt(evt.position().toPoint())) |
|
69 return |
|
70 |
|
71 super().mousePressEvent(evt) |
|
72 |
|
73 def mouseDoubleClickEvent(self, evt): |
|
74 """ |
|
75 Protected method handling mouse double click events. |
|
76 |
|
77 @param evt mouse press event (QMouseEvent) |
|
78 """ |
|
79 if self.__containsRemoveIcon(evt.position().toPoint()): |
|
80 self.removeItemRequested.emit( |
|
81 self.itemAt(evt.position().toPoint())) |
|
82 return |
|
83 |
|
84 super().mouseDoubleClickEvent( |
|
85 evt) |