-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlay.py
272 lines (211 loc) · 8.58 KB
/
overlay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
from PyQt4 import QtCore, QtGui
import re
class PMXMessageOverlay(object):
messageFadedOut = QtCore.pyqtSignal()
messageFadedIn = QtCore.pyqtSignal()
'''
Mixin for displaying overlayed messages in a QWidget instance.
Please note that you should:
* Use the mixin on toplevel elements (no QWidgets, but QPlainTextEdit, QWebView, etc.)
* You should call updateMessagePosition at least in resizeEvent of your subclass
'''
def __init__(self):
# Signals
self.messageOverlay = LabelOverlayWidget(text = "", parent = self)
self.messageOverlay.fadedIn.connect(self.messageFadedIn)
self.messageOverlay.fadedOut.connect(self.messageFadedOut)
self.messageOverlay.messageClicked.connect(self.messageClicked)
self.messageOverlay.linkActivated.connect(self.messageLinkActivated)
def messageFadedIn(self):
''' Override '''
#print "Message appeared"
pass
def messageFadedOut(self):
''' Override '''
#print "Message disappeared"
pass
def messageLinkActivated(self, link):
''' Override '''
pass
def showMessage(self, message, timeout = None, icon = None, pos = None ):
self.messageOverlay.setText(message)
self.messageOverlay.updatePosition()
self.messageOverlay.adjustSize()
if unicode(message):
self.messageOverlay.fadeIn()
else:
self.messageOverlay.fadeOut()
def clarMessage(self):
self.messageOverlay.fadeOut()
def messageClicked(self):
self.clarMessage()
def updateMessagePosition(self):
self.messageOverlay.updatePosition()
class LabelOverlayWidget(QtGui.QLabel):
'''
Inner message QLabel.
StyleSheet
Don't use this widget separately, please use PMXMessageOverlay API
'''
fadedOut = QtCore.pyqtSignal()
fadedIn = QtCore.pyqtSignal()
messageClicked = QtCore.pyqtSignal()
STYLESHEET = '''
QLabel, QLabel link {
color: rgb(0, 0, 0);
background-color: rgb(248, 240, 200);
border: 1px solid;
border-color: rgb(173, 114, 47);
border-radius: 5px;
padding: 2px;
}
'''
def __init__(self, text="", parent=None):
super(LabelOverlayWidget, self).__init__(text, parent)
self.paddingLeft = 10
self.paddingBottom = 10
self.timer = QtCore.QTimer(self)
self.timer.setInterval(32)
self.timer.timeout.connect(self.updateOpacity)
self.speed = 0
self.setStyleSheet(self.STYLESHEET)
self.opacity = 0
def setParent(self, parent):
self.updatePosition()
return super(LabelOverlayWidget, self).setParent(parent)
def updatePosition(self):
if hasattr(self.parent(), 'viewport'):
parentRect = self.parent().viewport().rect()
else:
parentRect = self.parent().rect()
if not parentRect:
return
x = parentRect.width() - self.width() - self.paddingLeft
y = parentRect.height() - self.height() - self.paddingBottom
self.setGeometry(x, y, self.width(), self.height())
def resizeEvent(self, event):
super(LabelOverlayWidget, self).resizeEvent(event)
self.updatePosition()
def showEvent(self, event):
self.updatePosition()
return super(LabelOverlayWidget, self).showEvent(event)
def enterEvent(self, event):
""" Mouse hovered the messge """
pass
FULL_THERSHOLD = 0.7
DEFAULT_FADE_SPEED = 0.15
def fadeIn(self, force = False):
self.opacity = 0
self.speed = self.DEFAULT_FADE_SPEED
self.timer.start()
def fadeOut(self, force = False):
self.opacity = self.FULL_THERSHOLD
self.speed = -self.DEFAULT_FADE_SPEED
self.timer.start()
__color = QtGui.QColor(0, 0, 0)
@property
def color(self):
return self.__color
@color.setter
def color(self, value):
assert isinstance(value, QtGui.QColor)
self.__color = value
__backgroundColor = QtGui.QColor(248, 240, 200)
@property
def backgroundColor(self):
return self.__backgroundColor
@backgroundColor.setter
def backgroundColor(self, value):
assert isinstance(value, QtGui.QColor)
self.__backgroundColor = value
self._updateStylesheetAlpha()
__borderColor = QtGui.QColor(173, 114, 47)
@property
def borderColor(self):
return self.__borderColor
@borderColor.setter
def borderColor(self, value):
assert isinstance(value, QtGui.QColor)
self.__borderColor = value
self._updateStylesheetAlpha()
__opacity = 1.0
@property
def opacity(self):
return self.__opacity
@opacity.setter
def opacity(self, value):
assert value <= 1, "%s is not in 0..1 value" % value
if value < 0:
value = 0
self.__opacity = value
self._updateStylesheetAlpha()
def mousePressEvent(self, event):
self.messageClicked.emit()
def updateOpacity(self):
if self.speed > 0:
if self.isHidden():
self.show()
if self.opacity <= self.FULL_THERSHOLD:
self.opacity += self.speed
else:
self.timer.stop()
self.fadedIn.emit()
elif self.speed < 0:
if self.opacity > 0:
self.opacity += self.speed
else:
self.timer.stop()
self.fadedOut.emit()
self.hide()
COLOR_PATTERN = re.compile(r"(?<!-)color:\s*rgba?\([\d\,\s\%\w]*\);?", re.MULTILINE | re.UNICODE)
BACKGROUND_COLOR_PATTERN = re.compile(r"background-color:\s*rgba?\([\d\,\s\%\w]*\);?", re.MULTILINE | re.UNICODE)
BORDER_COLOR_PATTERN = re.compile(r"border-color:\s*rgba?\([\d\,\s\%\w]*\);?", re.MULTILINE | re.UNICODE)
def _updateStylesheetAlpha(self):
styleSheet = unicode(self.styleSheet())
#re.sub(pattern, repl, string, count, flags)
for regex, name, col in ((self.COLOR_PATTERN, 'color', self.color),
(self.BACKGROUND_COLOR_PATTERN, 'background-color',self.backgroundColor),
(self.BORDER_COLOR_PATTERN, 'border-color', self.borderColor)):
repl = '%s: rgba(%d, %d, %d, %d%%);' % (name, col.red(), col.green(), col.blue(), self.opacity * 100.0)
styleSheet = regex.sub(repl, styleSheet)
self.setStyleSheet(styleSheet)
#----------------------------------------------------------------------
# Example usage
#----------------------------------------------------------------------
class ExampleOverlayedText(QtGui.QTextEdit, PMXMessageOverlay):
def __init__(self, parent = None):
QtGui.QTextEdit.__init__(self, parent)
PMXMessageOverlay.__init__(self)
def resizeEvent(self, event):
super(ExampleOverlayedText, self).resizeEvent(event)
self.updateMessagePosition()
def messageLinkActivated(self, link):
QtGui.QMessageBox.information(self, "Link activated", "You just clicked on %s" % link)
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi()
self.lineedit.textChanged.connect(self.showNiceTooltip)
self.plaintext.setPlainText("Please type here...")
def showNiceTooltip(self, msg):
frnd = functools.partial(random.randint, 0, 255)
self.plaintext.messageOverlay.backgroundColor = QtGui.QColor(frnd(), frnd(), frnd())
self.plaintext.messageOverlay.color = QtGui.QColor(frnd(), frnd(), frnd())
self.plaintext.messageOverlay.borderColor = QtGui.QColor(frnd(), frnd(), frnd())
self.plaintext.showMessage(msg)
def setupUi(self):
self.setLayout(QtGui.QVBoxLayout())
self.plaintext = ExampleOverlayedText()
#self.plaintext = QtGui.QTextEdit()
self.layout().addWidget(self.plaintext)
self.lineedit = QtGui.QLineEdit()
self.layout().addWidget(QtGui.QLabel("Test messages here:"))
self.layout().addWidget(self.lineedit)
import random
import functools
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())