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