This repository has been archived by the owner on Jul 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathTest_NinePatchLabel.py
255 lines (215 loc) · 6.95 KB
/
Test_NinePatchLabel.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2015年10月22日
@author: Irony."[讽刺]
@email: 892768447@qq.com
@description:
'''
from __future__ import unicode_literals
import os
from os.path import dirname, abspath
import random
import sys
# 解决2.x到3.x的字符串问题
# 父级目录
sys.path.insert(0, dirname(dirname(abspath(sys.argv[0]))))
sys.path.insert(0, dirname(dirname(abspath(__file__))))
from PyQt5.QtCore import QMetaObject, pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QListWidget, \
QPushButton, QHBoxLayout, QListWidgetItem, QTextEdit
from uilib.widgets.NinePatchLabel import NinePatchLabel
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2015 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
SCROLLBARSTYLE = """
/*QScrollBar Style*/
QScrollBar:vertical {
background: transparent;
width: 10px;
margin: 0px 0px 0px 0px;
padding-top: 12px;
padding-bottom: 12px;
}
QScrollBar:horizontal {
background: transparent;
height: 10px;
margin: 0px 0px 0px 0px;
padding-left: 12px;
padding-right: 12px;
}
QScrollBar:vertical:hover,QScrollBar:horizontal:hover {
background: rgba(0, 0, 0, 30);
border-radius: 5px;
}
QScrollBar::handle:vertical {
background: rgba(0, 0, 0, 50);
width: 10px;
border-radius: 5px;
border: none;
}
QScrollBar::handle:horizontal {
background: rgba(0, 0, 0, 50);
height: 10px;
border-radius: 5px;
border: none;
}
QScrollBar::handle:vertical:hover,QScrollBar::handle:horizontal:hover {
background: rgba(0, 0, 0, 100);
}
QScrollBar::add-page:vertical {
width: 10px;
background: transparent;
}
QScrollBar::add-page:horizontal {
height: 10px;
background: transparent;
}
QScrollBar::sub-page:vertical {
width: 10px;
background: transparent;
}
QScrollBar::sub-page:horizontal {
height: 10px;
background: transparent;
}
QScrollBar::sub-line:vertical {
height: 12px;
width: 10px;
background: transparent;
subcontrol-position: top;
}
QScrollBar::sub-line:horizontal {
height: 10px;
width: 12px;
background: transparent;
subcontrol-position: left;
}
QScrollBar::up-arrow:vertical {
image: url(../uilib/img/scrollbar_arrowup_normal.png);
}
QScrollBar::left-arrow:horizontal {
image: url(../uilib/img/scrollbar_arrowleft_normal.png);
}
QScrollBar::up-arrow:vertical:hover {
image: url(../uilib/img/scrollbar_arrowup_down.png);
}
QScrollBar::left-arrow:horizontal:hover {
image: url(../uilib/img/scrollbar_arrowleft_down.png);
}
QScrollBar::up-arrow:vertical:pressed {
image: url(../uilib/img/scrollbar_arrowup_highlight.png);
}
QScrollBar::left-arrow:horizontal:pressed {
image: url(../uilib/img/scrollbar_arrowleft_highlight.png);
}
QScrollBar::add-line:vertical {
height: 12px;
width: 10px;
background: transparent;
subcontrol-position: bottom;
}
QScrollBar::add-line:horizontal {
height: 10px;
width: 12px;
background: transparent;
subcontrol-position: right;
}
QScrollBar::down-arrow:vertical {
image: url(../uilib/img/scrollbar_arrowdown_normal.png);
}
QScrollBar::right-arrow:horizontal {
image: url(../uilib/img/scrollbar_arrowright_normal.png);
}
QScrollBar::down-arrow:vertical:hover {
image: url(../uilib/img/scrollbar_arrowdown_down.png);
}
QScrollBar::right-arrow:horizontal:hover {
image: url(../uilib/img/scrollbar_arrowright_down.png);
}
QScrollBar::down-arrow:vertical:pressed {
image: url(../uilib/img/scrollbar_arrowdown_highlight.png);
}
QScrollBar::right-arrow:horizontal:pressed {
image: url(../uilib/img/scrollbar_arrowright_highlight.png);
}
"""
class Item(NinePatchLabel):
def __init__(self, parent = None, text = "", norImage = None, preImage = None):
super(Item, self).__init__(parent, text, norImage, preImage)
self.setStyleSheet("margin: 30px;")
class TestNinePatchLabel(QWidget):
skin_aio_friend_bubble_nor = "skin_aio_friend_bubble_nor.9.png"
skin_aio_friend_bubble_pressed = "skin_aio_friend_bubble_pressed.9.png"
skin_aio_user_bubble_nor = "skin_aio_user_bubble_nor.9.png"
skin_aio_user_bubble_pressed = "skin_aio_user_bubble_pressed.9.png"
def __init__(self):
super(TestNinePatchLabel, self).__init__()
self.resize(400, 600)
self.listWidget = QListWidget(self)
self.listWidget.setObjectName("listWidget")
self.listWidget.setStyleSheet("""
QListWidget::item:selected {
background: rgba(0,0,0,0);
}
QListWidget::item:hover {
background: rgba(0,0,0,0);
}
""")
self.textEdit = QTextEdit(self)
self.textEdit.setMaximumHeight(100)
self.textEdit.setObjectName("textEdit")
self.sendBtn = QPushButton("发送", self)
self.sendBtn.setObjectName("sendBtn")
hlayout = QHBoxLayout()
hlayout.setContentsMargins(0, 0, 0, 0)
hlayout.setSpacing(0)
hlayout.addWidget(self.textEdit)
hlayout.addWidget(self.sendBtn)
vlayout = QVBoxLayout(self)
vlayout.setContentsMargins(0, 0, 0, 0)
vlayout.setSpacing(0)
vlayout.addWidget(self.listWidget)
vlayout.addItem(hlayout)
QMetaObject.connectSlotsByName(self) # 通过objectname注册信号
self.init()
self.setStyleSheet(SCROLLBARSTYLE)
def init(self):
'''加载气泡数组'''
paths = os.listdir("..\\bubble")
self.bubble = [
(
os.path.join(os.path.join("..\\bubble", path), self.skin_aio_friend_bubble_nor),
os.path.join(os.path.join("..\\bubble", path), self.skin_aio_friend_bubble_pressed),
os.path.join(os.path.join("..\\bubble", path), self.skin_aio_user_bubble_nor),
os.path.join(os.path.join("..\\bubble", path), self.skin_aio_user_bubble_pressed)
)
for path in paths
]
@pyqtSlot()
def on_sendBtn_clicked(self):
self.send()
def send(self):
'''发送消息'''
text = self.textEdit.toPlainText().strip()
if not text:
return
fn , fp, un, up = random.choice(self.bubble) # 从中随机一个
# 我说
item = QListWidgetItem()
self.listWidget.addItem(item)
pItem = Item(None, "我说: " + text, un, up)
item.setSizeHint(pItem.size()) # 把item的大小变得和label一样
self.listWidget.setItemWidget(item, pItem)
# 对方说
item = QListWidgetItem()
self.listWidget.addItem(item)
pItem = Item(None, "她说: " + text, fn, fp)
item.setSizeHint(pItem.size()) # 把item的大小变得和label一样
self.listWidget.setItemWidget(item, pItem)
self.listWidget.scrollTo(self.listWidget.indexFromItem(item))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = TestNinePatchLabel()
window.show()
sys.exit(app.exec_())