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