|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2016 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 removeItemRequested = pyqtSignal(QListWidgetItem) |
|
24 |
|
25 def __init__(self, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param parent reference to the parent widget (QWidget) |
|
30 """ |
|
31 super(GreaseMonkeyConfigurationListWidget, self).__init__(parent) |
|
32 |
|
33 self.__delegate = GreaseMonkeyConfigurationListDelegate(self) |
|
34 self.setItemDelegate(self.__delegate) |
|
35 |
|
36 def __containsRemoveIcon(self, pos): |
|
37 """ |
|
38 Private method to check, if the given position is inside the remove |
|
39 icon. |
|
40 |
|
41 @param pos position to check for (QPoint) |
|
42 @return flag indicating success (boolean) |
|
43 """ |
|
44 itm = self.itemAt(pos) |
|
45 if itm is None: |
|
46 return False |
|
47 |
|
48 rect = self.visualItemRect(itm) |
|
49 iconSize = GreaseMonkeyConfigurationListDelegate.RemoveIconSize |
|
50 removeIconXPos = rect.right() - self.__delegate.padding() - iconSize |
|
51 center = rect.height() // 2 + rect.top() |
|
52 removeIconYPos = center - iconSize // 2 |
|
53 |
|
54 removeIconRect = QRect(removeIconXPos, removeIconYPos, |
|
55 iconSize, iconSize) |
|
56 return removeIconRect.contains(pos) |
|
57 |
|
58 def mousePressEvent(self, evt): |
|
59 """ |
|
60 Protected method handling presses of mouse buttons. |
|
61 |
|
62 @param evt mouse press event (QMouseEvent) |
|
63 """ |
|
64 if self.__containsRemoveIcon(evt.pos()): |
|
65 self.removeItemRequested.emit(self.itemAt(evt.pos())) |
|
66 return |
|
67 |
|
68 super(GreaseMonkeyConfigurationListWidget, self).mousePressEvent(evt) |
|
69 |
|
70 def mouseDoubleClickEvent(self, evt): |
|
71 """ |
|
72 Protected method handling mouse double click events. |
|
73 |
|
74 @param evt mouse press event (QMouseEvent) |
|
75 """ |
|
76 if self.__containsRemoveIcon(evt.pos()): |
|
77 self.removeItemRequested.emit(self.itemAt(evt.pos())) |
|
78 return |
|
79 |
|
80 super(GreaseMonkeyConfigurationListWidget, self).mouseDoubleClickEvent( |
|
81 evt) |