Initial commit
This commit is contained in:
commit
330c41dbe7
274 files changed
+62865
No files matched your search
@@ -0,0 +1,103 @@
|
||||
from datetime import datetime
|
||||
|
||||
from PyQt5 import QtWidgets, QtGui, QtCore
|
||||
from PyQt5.QtCore import *
|
||||
|
||||
from app import person
|
||||
|
||||
|
||||
class ContactUi(QtWidgets.QPushButton):
|
||||
"""
|
||||
联系人类,继承自pyqt的按钮,里面封装了联系人头像等标签
|
||||
"""
|
||||
usernameSingal = pyqtSignal(str)
|
||||
|
||||
def __init__(self, Ui, id=None, rconversation=None):
|
||||
super(ContactUi, self).__init__(Ui)
|
||||
self.contact: person.Contact = person.Contact(rconversation[1])
|
||||
self.init_ui(Ui)
|
||||
self.msgCount = rconversation[0]
|
||||
self.username = rconversation[1]
|
||||
self.conversationTime = rconversation[6]
|
||||
self.msgType = rconversation[7]
|
||||
self.digest = rconversation[8]
|
||||
hasTrunc = rconversation[10]
|
||||
attrflag = rconversation[11]
|
||||
if hasTrunc == 0:
|
||||
if attrflag == 0:
|
||||
self.digest = '[动画表情]'
|
||||
elif attrflag == 67108864:
|
||||
try:
|
||||
remark = data.get_conRemark(rconversation[9])
|
||||
msg = self.digest.split(':')[1].strip('\n').strip()
|
||||
self.digest = f'{remark}:{msg}'
|
||||
except Exception as e:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
self.show_info(id)
|
||||
|
||||
def init_ui(self, Ui):
|
||||
self.layoutWidget = QtWidgets.QWidget(Ui)
|
||||
self.layoutWidget.setObjectName("layoutWidget")
|
||||
self.gridLayout1 = QtWidgets.QGridLayout(self.layoutWidget)
|
||||
self.gridLayout1.setSizeConstraint(QtWidgets.QLayout.SetMaximumSize)
|
||||
self.gridLayout1.setContentsMargins(10, 10, 10, 10)
|
||||
self.gridLayout1.setSpacing(10)
|
||||
self.gridLayout1.setObjectName("gridLayout1")
|
||||
self.label_time = QtWidgets.QLabel(self.layoutWidget)
|
||||
font = QtGui.QFont()
|
||||
font.setFamily("微软雅黑")
|
||||
font.setPointSize(8)
|
||||
self.label_time.setFont(font)
|
||||
self.label_time.setLayoutDirection(QtCore.Qt.RightToLeft)
|
||||
self.label_time.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter)
|
||||
self.label_time.setObjectName("label_time")
|
||||
self.gridLayout1.addWidget(self.label_time, 0, 2, 1, 1)
|
||||
self.label_remark = QtWidgets.QLabel(self.layoutWidget)
|
||||
font = QtGui.QFont()
|
||||
font.setFamily("黑体")
|
||||
font.setPointSize(10)
|
||||
# font.setBold(True)
|
||||
self.label_remark.setFont(font)
|
||||
self.label_remark.setObjectName("label_remark")
|
||||
self.gridLayout1.addWidget(self.label_remark, 0, 1, 1, 1)
|
||||
self.label_msg = QtWidgets.QLabel(self.layoutWidget)
|
||||
font = QtGui.QFont()
|
||||
font.setFamily("微软雅黑")
|
||||
font.setPointSize(8)
|
||||
self.label_msg.setFont(font)
|
||||
self.label_msg.setObjectName("label_msg")
|
||||
self.gridLayout1.addWidget(self.label_msg, 1, 1, 1, 2)
|
||||
self.label_avatar = QtWidgets.QLabel(self.layoutWidget)
|
||||
self.label_avatar.setMinimumSize(QtCore.QSize(60, 60))
|
||||
self.label_avatar.setMaximumSize(QtCore.QSize(60, 60))
|
||||
self.label_avatar.setLayoutDirection(QtCore.Qt.RightToLeft)
|
||||
self.label_avatar.setAutoFillBackground(False)
|
||||
self.label_avatar.setStyleSheet("background-color: #ffffff;")
|
||||
self.label_avatar.setInputMethodHints(QtCore.Qt.ImhNone)
|
||||
self.label_avatar.setFrameShape(QtWidgets.QFrame.NoFrame)
|
||||
self.label_avatar.setFrameShadow(QtWidgets.QFrame.Plain)
|
||||
self.label_avatar.setAlignment(QtCore.Qt.AlignLeading | QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
|
||||
self.label_avatar.setObjectName("label_avatar")
|
||||
self.gridLayout1.addWidget(self.label_avatar, 0, 0, 2, 1)
|
||||
self.gridLayout1.setColumnStretch(0, 1)
|
||||
self.gridLayout1.setColumnStretch(1, 6)
|
||||
self.gridLayout1.setRowStretch(0, 5)
|
||||
self.gridLayout1.setRowStretch(1, 3)
|
||||
self.setLayout(self.gridLayout1)
|
||||
self.setStyleSheet(
|
||||
"QPushButton {background-color: rgb(220,220,220);}"
|
||||
"QPushButton:hover{background-color: rgb(208,208,208);}\n"
|
||||
)
|
||||
|
||||
def show_info(self, id):
|
||||
time = datetime.now().strftime("%m-%d %H:%M")
|
||||
msg = '还没说话'
|
||||
self.label_avatar.setPixmap(self.contact.avatar) # 在label上显示图片
|
||||
self.label_remark.setText(self.contact.conRemark)
|
||||
self.label_msg.setText(self.digest)
|
||||
self.label_time.setText(data.timestamp2str(self.conversationTime)[2:])
|
||||
|
||||
def show_msg(self):
|
||||
self.usernameSingal.emit(self.username)
|
||||
@@ -0,0 +1,303 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Created on 2019年7月26日
|
||||
@author: Irony
|
||||
@site: https://pyqt5.com https://github.com/892768447
|
||||
@email: 892768447@qq.com
|
||||
@file: CustomWidgets.CAvatar
|
||||
@description: 头像
|
||||
"""
|
||||
import os
|
||||
|
||||
from PyQt5.QtCore import QUrl, QRectF, Qt, QSize, QTimer, QPropertyAnimation, \
|
||||
QPointF, pyqtProperty
|
||||
from PyQt5.QtGui import QPixmap, QColor, QPainter, QPainterPath, QMovie
|
||||
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkDiskCache, \
|
||||
QNetworkRequest
|
||||
from PyQt5.QtWidgets import QWidget, qApp
|
||||
|
||||
__Author__ = 'Irony'
|
||||
__Copyright__ = 'Copyright (c) 2019 Irony'
|
||||
__Version__ = 1.0
|
||||
|
||||
|
||||
class CAvatar(QWidget):
|
||||
Circle = 0 # 圆圈
|
||||
Rectangle = 1 # 圆角矩形
|
||||
SizeLarge = QSize(128, 128)
|
||||
SizeMedium = QSize(64, 64)
|
||||
SizeSmall = QSize(32, 32)
|
||||
StartAngle = 0 # 起始旋转角度
|
||||
EndAngle = 360 # 结束旋转角度
|
||||
|
||||
def __init__(self, *args, shape=0, url='', img_bytes=None, cacheDir=False, size=QSize(64, 64), animation=False,
|
||||
**kwargs):
|
||||
super(CAvatar, self).__init__(*args, **kwargs)
|
||||
self.url = ''
|
||||
self._angle = 0 # 角度
|
||||
self.pradius = 0 # 加载进度条半径
|
||||
self.animation = animation # 是否使用动画
|
||||
self._movie = None # 动态图
|
||||
self._pixmap = QPixmap() # 图片对象
|
||||
self.pixmap = QPixmap() # 被绘制的对象
|
||||
self.isGif = url.endswith('.gif')
|
||||
# 进度动画定时器
|
||||
self.loadingTimer = QTimer(self, timeout=self.onLoading)
|
||||
# 旋转动画
|
||||
self.rotateAnimation = QPropertyAnimation(
|
||||
self, b'angle', self, loopCount=1)
|
||||
self.setShape(shape)
|
||||
self.setCacheDir(cacheDir)
|
||||
self.setSize(size)
|
||||
if img_bytes:
|
||||
self.setBytes(img_bytes)
|
||||
|
||||
|
||||
else:
|
||||
self.setUrl(url)
|
||||
|
||||
def paintEvent(self, event):
|
||||
super(CAvatar, self).paintEvent(event)
|
||||
# 画笔
|
||||
painter = QPainter(self)
|
||||
painter.setRenderHint(QPainter.Antialiasing, True)
|
||||
painter.setRenderHint(QPainter.HighQualityAntialiasing, True)
|
||||
painter.setRenderHint(QPainter.SmoothPixmapTransform, True)
|
||||
# 绘制
|
||||
path = QPainterPath()
|
||||
diameter = min(self.width(), self.height())
|
||||
if self.shape == self.Circle:
|
||||
radius = int(diameter / 2)
|
||||
elif self.shape == self.Rectangle:
|
||||
radius = 4
|
||||
halfW = self.width() / 2
|
||||
halfH = self.height() / 2
|
||||
painter.translate(halfW, halfH)
|
||||
path.addRoundedRect(
|
||||
QRectF(-halfW, -halfH, diameter, diameter), radius, radius)
|
||||
painter.setClipPath(path)
|
||||
# 如果是动画效果
|
||||
if self.rotateAnimation.state() == QPropertyAnimation.Running:
|
||||
painter.rotate(self._angle) # 旋转
|
||||
painter.drawPixmap(
|
||||
QPointF(-self.pixmap.width() / 2, -self.pixmap.height() // 2), self.pixmap)
|
||||
else:
|
||||
painter.drawPixmap(-int(halfW), -int(halfH), self.pixmap)
|
||||
# 如果在加载
|
||||
if self.loadingTimer.isActive():
|
||||
diameter = 2 * self.pradius
|
||||
painter.setBrush(
|
||||
QColor(45, 140, 240, int((1 - self.pradius / 10) * 255)))
|
||||
painter.setPen(Qt.NoPen)
|
||||
painter.drawRoundedRect(
|
||||
QRectF(-self.pradius, -self.pradius, diameter, diameter), self.pradius, self.pradius)
|
||||
|
||||
def enterEvent(self, event):
|
||||
"""鼠标进入动画
|
||||
:param event:
|
||||
"""
|
||||
if not (self.animation and not self.isGif):
|
||||
return
|
||||
self.rotateAnimation.stop()
|
||||
cv = self.rotateAnimation.currentValue() or self.StartAngle
|
||||
self.rotateAnimation.setDuration(
|
||||
540 if cv == 0 else int(cv / self.EndAngle * 540))
|
||||
self.rotateAnimation.setStartValue(cv)
|
||||
self.rotateAnimation.setEndValue(self.EndAngle)
|
||||
self.rotateAnimation.start()
|
||||
|
||||
def leaveEvent(self, event):
|
||||
"""鼠标离开动画
|
||||
:param event:
|
||||
"""
|
||||
if not (self.animation and not self.isGif):
|
||||
return
|
||||
self.rotateAnimation.stop()
|
||||
cv = self.rotateAnimation.currentValue() or self.EndAngle
|
||||
self.rotateAnimation.setDuration(int(cv / self.EndAngle * 540))
|
||||
self.rotateAnimation.setStartValue(cv)
|
||||
self.rotateAnimation.setEndValue(self.StartAngle)
|
||||
self.rotateAnimation.start()
|
||||
|
||||
def onLoading(self):
|
||||
"""更新进度动画
|
||||
"""
|
||||
if self.loadingTimer.isActive():
|
||||
if self.pradius > 9:
|
||||
self.pradius = 0
|
||||
self.pradius += 1
|
||||
else:
|
||||
self.pradius = 0
|
||||
self.update()
|
||||
|
||||
def onFinished(self):
|
||||
"""图片下载完成
|
||||
"""
|
||||
self.loadingTimer.stop()
|
||||
self.pradius = 0
|
||||
reply = self.sender()
|
||||
|
||||
if self.isGif:
|
||||
self._movie = QMovie(reply, b'gif', self)
|
||||
if self._movie.isValid():
|
||||
self._movie.frameChanged.connect(self._resizeGifPixmap)
|
||||
self._movie.start()
|
||||
else:
|
||||
data = reply.readAll().data()
|
||||
reply.deleteLater()
|
||||
del reply
|
||||
self._pixmap.loadFromData(data)
|
||||
if self._pixmap.isNull():
|
||||
self._pixmap = QPixmap(self.size())
|
||||
self._pixmap.fill(QColor(204, 204, 204))
|
||||
self._resizePixmap()
|
||||
|
||||
def onError(self, code):
|
||||
"""下载出错了
|
||||
:param code:
|
||||
"""
|
||||
self._pixmap = QPixmap(self.size())
|
||||
self._pixmap.fill(QColor(204, 204, 204))
|
||||
self._resizePixmap()
|
||||
|
||||
def refresh(self):
|
||||
"""强制刷新
|
||||
"""
|
||||
self._get(self.url)
|
||||
|
||||
def isLoading(self):
|
||||
"""判断是否正在加载
|
||||
"""
|
||||
return self.loadingTimer.isActive()
|
||||
|
||||
def setShape(self, shape):
|
||||
"""设置形状
|
||||
:param shape: 0=圆形, 1=圆角矩形
|
||||
"""
|
||||
self.shape = shape
|
||||
|
||||
def setBytes(self, img_bytes):
|
||||
self._pixmap = QPixmap()
|
||||
if isinstance(img_bytes, bytes):
|
||||
if img_bytes[:4] == b'\x89PNG':
|
||||
self._pixmap.loadFromData(img_bytes, format='PNG')
|
||||
else:
|
||||
self._pixmap.loadFromData(img_bytes, format='jfif')
|
||||
elif isinstance(img_bytes, QPixmap):
|
||||
self._pixmap = img_bytes
|
||||
self._resizePixmap()
|
||||
|
||||
def setUrl(self, url):
|
||||
"""设置url,可以是本地路径,也可以是网络地址
|
||||
:param url:
|
||||
"""
|
||||
return
|
||||
self.url = url
|
||||
self._get(url)
|
||||
|
||||
def setCacheDir(self, cacheDir=''):
|
||||
"""设置本地缓存路径
|
||||
:param cacheDir:
|
||||
"""
|
||||
self.cacheDir = cacheDir
|
||||
self._initNetWork()
|
||||
|
||||
def setSize(self, size):
|
||||
"""设置固定尺寸
|
||||
:param size:
|
||||
"""
|
||||
if not isinstance(size, QSize):
|
||||
size = self.SizeMedium
|
||||
self.setMinimumSize(size)
|
||||
self.setMaximumSize(size)
|
||||
self._resizePixmap()
|
||||
|
||||
@pyqtProperty(int)
|
||||
def angle(self):
|
||||
return self._angle
|
||||
|
||||
@angle.setter
|
||||
def angle(self, value):
|
||||
self._angle = value
|
||||
self.update()
|
||||
|
||||
def _resizePixmap(self):
|
||||
"""缩放图片
|
||||
"""
|
||||
if not self._pixmap.isNull():
|
||||
self.pixmap = self._pixmap.scaled(
|
||||
self.width(), self.height(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
|
||||
self.update()
|
||||
|
||||
def _resizeGifPixmap(self, _):
|
||||
"""缩放动画图片
|
||||
"""
|
||||
if self._movie:
|
||||
self.pixmap = self._movie.currentPixmap().scaled(
|
||||
self.width(), self.height(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
|
||||
self.update()
|
||||
|
||||
def _initNetWork(self):
|
||||
"""初始化异步网络库
|
||||
"""
|
||||
if not hasattr(qApp, '_network'):
|
||||
network = QNetworkAccessManager(self.window())
|
||||
setattr(qApp, '_network', network)
|
||||
# 是否需要设置缓存
|
||||
if self.cacheDir and not qApp._network.cache():
|
||||
cache = QNetworkDiskCache(self.window())
|
||||
cache.setCacheDirectory(self.cacheDir)
|
||||
qApp._network.setCache(cache)
|
||||
|
||||
def _get(self, url):
|
||||
"""设置图片或者请求网络图片
|
||||
:param url:
|
||||
"""
|
||||
if not url:
|
||||
self.onError('')
|
||||
return
|
||||
if url.startswith('http') and not self.loadingTimer.isActive():
|
||||
url = QUrl(url)
|
||||
request = QNetworkRequest(url)
|
||||
# request.setHeader(QNetworkRequest.UserAgentHeader, b'CAvatar')
|
||||
# request.setRawHeader(b'Author', b'Irony')
|
||||
request.setAttribute(
|
||||
QNetworkRequest.FollowRedirectsAttribute, True)
|
||||
if qApp._network.cache():
|
||||
request.setAttribute(
|
||||
QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.PreferNetwork)
|
||||
request.setAttribute(
|
||||
QNetworkRequest.CacheSaveControlAttribute, True)
|
||||
reply = qApp._network.get(request)
|
||||
self.pradius = 0
|
||||
self.loadingTimer.start(50) # 显示进度动画
|
||||
reply.finished.connect(self.onFinished)
|
||||
reply.error.connect(self.onError)
|
||||
return
|
||||
self.pradius = 0
|
||||
if os.path.exists(url) and os.path.isfile(url):
|
||||
if self.isGif:
|
||||
self._movie = QMovie(url, parent=self)
|
||||
if self._movie.isValid():
|
||||
self._movie.frameChanged.connect(self._resizeGifPixmap)
|
||||
self._movie.start()
|
||||
else:
|
||||
self._pixmap = QPixmap(url)
|
||||
self._resizePixmap()
|
||||
else:
|
||||
self.onError('')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
w = CAvatar(
|
||||
img_bytes=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x84\x00\x00\x00\x84\x08\x00\x00\x00\x00t/\xdc{\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00\x00\x80\xe8\x00\x00u0\x00\x00\xea`\x00\x00:\x98\x00\x00\x17p\x9c\xbaQ<\x00\x00\x00\x02bKGD\x00\xff\x87\x8f\xcc\xbf\x00\x00\x0e\x8fIDATx\xda\xed\x9b[\x8c]\xd7Y\xc7\xbf\xff\xb7\xd6\xde\xfb\x9c3w\xdf&\xbe\xdf\x9d\xa4\xbe\xc4\x89krmI\xdc\x06\xa5i\xa0$\x95\xa0\x95\x02U\x0bBH(\\\xfa\x80\x10<\xf0\xd6\x07\xfa\x80D\x1e\xa0P\xa2\x02-\xa1)%j\x13\xd2\xe6~O\x9a\xc4J\x9a\xc4\x8e\xeb\xcb8\xf8\x9eq\xec\x8c\xe7~\xf6\xdek}\x7f\x1e\xce\x8c\xc7g<c\x8f\x8dP\x04:K\x9a\x87\xd93k\xed\xdf\xf9\xd6w\xfff0&\x1f\xff\xd2\x8f\x1b\xa0\x05\xd1\x82hA\xb4 Z\x10-\x88\x16D\x0b\xa2\x05\xd1\x82hA\xb4 Z\x10-\x88\x16D\x0b\xe2\xe3\x06hA\\.\x04\xe6\xf0\x1c""\x049\xf5\x90\x14\x10 \x85\x8d\xef.\x05\x02"\x94\xcbZJU\x9b8\x81\x02\x08\x1d\xa9J\x15\x11\xcc\xf0Q\xfc\xecG]&\x81\x88P h\x9c\x00\x8a\x88\xd0DL@\n\x00\x9b;\x04\x84\x00\xe2eb\x14\xe2\x05b\x02\x884d!@C4v\xbe(f\x85\xa0\x88ApY\xf2\x88.\x01HN^(\xa1AT"\x01\x88\x9e\x7f\xe2\xec\xd7\x01\x11\x9a\x9f\x90\xe7\xa5-\x1fI:\x00\x93*a\xe6\xa4\xa4\xa6\x9c\xd0\xceikv\xc5\xa48\x970\x10\x86\te\xe2\xac\xbf9e"\x14\xc6(\x92\xb0b\x89D\x03\x80\x08\x08T\x99i\x11\x01^\x92b\x82%\x08/\xd4\x86\xbd\x11r\xa1\xd5 \x84\xc1\x07"\xee\xdc\xb3ce\xee\xa1\xa6$\xd5$\xab\x7f0\xd0\xb9\x04*\xa22\xa9\xb3s\x81\x88\xe2)4\x99\x80\x9f\xc3\xad\x80b\xa0\xa9\xc5\xf0\xcc\xf3\xd9b\xe6\x95\xe0\x84\xa2F\xb5\xe2\x91\xe7\x97\xfe\xe1\x9a\xc2<\x02\x08a\x93s\x99\xfd:\xdc\x81\xe7\xfb\xe1\xa2\xa9QA\x02\x17e\x10\x08\x84j\xeaC\x91?\xbd\xab\xe2\xe8i".\x98+G\xfaN\x1c\x19\x89\xe2\xactP@\x9bN\x9b\x15\x02\xac}\xeb\x89\x93P\x95\t\xfb\xba\x08D\xe3\xb6,\xd4sG\xab\xf5\xf2\xf03C\x88^D\x85J\x89\xffu\xc4wVM\xc49\x13r\x9a\xdb\x9c\xddD\xd1\xbb\xf5\xdfG\x7f\xcfATJ\xc5\x1cl\x15B\x08\x9f\x1e\xde\xb8\xba\x1b5W\x7f*[\xd2\xbdpu\x1b\x18\xb3\x82\xf9\xeb\x03\\?\xdf\xd2\xa2\xf4<\xdf\xf0/\xe01\xfd\x1d{~\xd8sw%w\x14\xa5\x98^\x84\x82""\x8a\r\xdf\xfc\xce\xf2\xcd\x0b\xdfN+\xe3\x0f\x87\xb8\xee\xbek\xcb\x04\xe3\xb5\xf8\xeeK\xf9\xaaO\xb7\xa7uT\x82*\x85l:\xcd\xfd\xc5,g*\x13\x9cy\x7f\xff\xc2\xe5>:\x05\xe7\xa0\x11\x8d\xaf\x85m\xa1o\xcf\xdb\xc7d(]\xb8\xe2\xc6\x1d\xd7\xd4\x9cYR\x0e~{/v\xdce\x89c\xa4\x93(\x8a\xb9YG\xe9\x87zox\xf9\xe4\xc3\xab\xd6\xa4!\xb1\x8b\xdb\x06H\x81\x18\xc2\x8e\xab\xde\xdd\xd7\x7f\xf4x\xfb\x95_Y0\xbf\xcd\x8fV\x10\x92\xfa\xab\xafiu\xad\xef\xff\xa8\xad\xc3k.N\x8c\xde\xe6\x02\x91X\xfb\xc8\x96\xbb\xef\xef{q%9W\xa7I8qq\xe9\xaa\xba\x7f\xe4\xaf\xd0}CI\xa9w\x94\x92\xe4|M\x92\xc1\x1f\xffp\xa0\\\xbaz\xed-\xcb\x02\xc5I\x9c\x93\x89F\xc9\xdb\xf4\xa6u#oEK"/\xae\x97\x13a\x8aP\xab{\x89\x9ai\x10I\xd2h\xa4\xd7M\xcep\xf0\xe0P\xb1\xf7\x89\x87\xfb\xcdT\xa35\xbd\xf7\x02Q\xb42Z\xe9\xbd\xb3\\e\x19\xa3\x83\\,\x88Lz\xd5<d*\xb44/I+-Q\xf1E\xe5\xd3#\xa3Y]\xdf\xda[\x1d\xd9\xb4\x04f\x829\xea\x84\x88y\xf8\xbb\xb6\xf5V\x0c)\x82\xb9\xa8S/k\xf8\x8e\xa6\x83\x1a\x8f\rYf\xa5\xb7\x93A\xc3\xdb\xfd\x8b6%y\x8a\xd1\x8e\xb1\xc5\xbf\x0b\xe6\xfb\x7f\xe1\xdd\xb5\xf7\\\x91Wcd\xb3f^\x00\x02\x1a\x83\xae\xb2\xd2\xab\x01:\xc10\xf9\xb2Y\x82\tPHQ\xba\x91\x93>\xec\xfa\xe6\xa9E\x7f\xb2\xcd\x19\xd3\xa0\xa5y\xdb\xf7\xd0\x01\xae\xfe\xf2\x1as\x05\xc5ISVq\x81\xeb(\x13\x83\x99\xa51\n`\x93\x12\xa4\xc1\x899\x03g\xf4\x1c\xc1\r?\xb87\xb5\xfdeU\x8f\xc9\xday\xae,3Ww\xce\x87\x03\x7f\xb3/n\xf9\xdaV\x9a+t:\xfd\xec\x1e\x93,\xd5\x99(c\xc6\xd2CCC\x14\xce\xd1D\x83\xe8\xcc1-\xa9\xa3~hl,\xf3y\\w\xe7\x1d\xedCY-\x94\xd5\x10\x8b\x9d\x0f\x1c\xf5\xb7\xfd\xc1<\x12\x92\x80\xd6\x1c\x04fuV\x02OD\x07\x19T\x11\x188q\x1dQ\xd5\x04\xe6\xc1\x99<\x18$\xb4]\xb5\xf2\xe4@\x84\xdf\xf6\xa5\xb5C\xbe\xeaL\xbc!>\xfb\xd0\x01\xd7yko\xae\xa9\x16\x89L\x0b\xa2\x82\xd9\xc6\xd4 \xb32d\x05\x1f\x18\xea\xd9\xbe\x99>LlCd\x1c\x1e\xec\xad\x00\xcef\xda\x86 \xbe\xef\x9f_\tze\xd7\xe9p\xd3\xe7{\x05\x9a\x87\xe7\xff\xb1\xdf\xa5\xd6\xd3\xd9\xbd\xf6\xc6++\x88\xe2%\x9ck\xa3~\xfa\x9b\x01S\x98\x90*c\x99\xaf\x8f<\xf2\x83\xb0\xf9\x97\x95\xa1\xf1\xc9A\xd3\xf2\xfb\x8f\xf7\xddq\xcf\xd5\xa9\x988kd\xb2\x84PI\x15C\x89dt\xc9\x8a\x97\x93\xb0/P\x0f\xad\xee\x8db\x95#\x8f\x7f8?J\xc6\xc1\x93\xaf\xfd\xe4\x86\xdf\\\xee\x94\xe6&\xee\x92\xa2\xc2&\x08\x98\x12A%B`\xa20=\xfe\xd8\x13\xf3v|nY\x14g"0g\xaaq\xffw\xeb\xdd/\x9e\xba\xf7\xc6\x89\x84\xeb\x9c\xfc\x9eP\xf3&\xd9\xce\x9d\x06\xcb\x13\xcf\rW\x88de\xb9\xe8W7V\xbb*\x95\x04\xa3\xfb\x1e~\xe6\xc3\xfb\xd6\xda\x94m@8M\x12\x06(\x9dF:\x13GI\xeb\xbb\xbfs\xb0\xe7\xf77v$\xb9\x06O:3\x04\x05\xaf\x1b\xda\'{~\xb2\xb9\xcdEN\xe6j\x80\x882B\x11\xad|\xf1\xfb\x07T\xb6v\xbdw|\xc1\xe7\xd6\xa9\x14\xd0\xb6\xed\xb7\xb8\xb2f\x92\xc4m\xaf\x1fy\xe7G_\xeb\xd0sL\x94\xc04\xeb(\xb2\xc2\x9bc\x84)\xf3\xddO\xbdT\xbd\xe3\x8b\xf3\x13\xe6ItA\x11|\x84\x037\xfd\xe5\xb1\xbf\xdec;\x87k\xc0\x84;\x07i>\x06u\x8cq\xfc\xcd\x9f\xf6\xf5\xa7\xba\xed\x8f\x16\xbe;\xb8hSn\xc1%%{\xf2\x98Y\t\x14X\xb4\xdf?\xbf\xf6\xf38\x1b:(Nl\x1aD\x02\xc4$OH\x8d\xbb\xdex\xb2\xbe\xfc\xcb\xd7u0\xba`e\xadT\x8d\x12\x1c(l\x93")RD\xe7\x08C#h0\x00.\x8e\x1f\xdb\xb3\xfb\xb5\x81\xaa\xc0_\xb7H\xb6\x92\xa52\x95 RG\xc5\xe2/\x0e/\xLine truncated
|
||||
,
|
||||
url='https://wx.qlogo.cn/mmhead/ver_1/DpDqmvTDORNWfLrMj26YicorEUREffl1G8FapawdKgINVH9g1icudfWesGrH9LqeGAz16z4PmkW9U1KAIM3btWgozZ1GaLF66bdKdxlMdazmibn2hpFeiaa4613dN6HM4Vfk/132')
|
||||
w.show()
|
||||
sys.exit(app.exec_())
|
||||
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Created on 2020年3月13日
|
||||
@author: Irony
|
||||
@site: https://pyqt.site , https://github.com/PyQt5
|
||||
@email: 892768447@qq.com
|
||||
@file: Demo.Lib.QCursorGif
|
||||
@description:
|
||||
"""
|
||||
|
||||
try:
|
||||
from PyQt5.QtCore import QTimer, Qt
|
||||
from PyQt5.QtGui import QCursor, QPixmap
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
except ImportError:
|
||||
from PySide2.QtCore import QTimer, Qt
|
||||
from PySide2.QtGui import QCursor, QPixmap
|
||||
from PySide2.QtWidgets import QApplication
|
||||
from app.resources import resource_rc
|
||||
|
||||
var = resource_rc.qt_resource_name
|
||||
|
||||
|
||||
class QCursorGif:
|
||||
|
||||
def initCursor(self, cursors, parent=None):
|
||||
# 记录默认的光标
|
||||
self._oldCursor = Qt.ArrowCursor
|
||||
self.setOldCursor(parent)
|
||||
# 加载光标图片
|
||||
self._cursorImages = [
|
||||
QCursor(QPixmap(cursor)) for cursor in cursors]
|
||||
self._cursorIndex = 0
|
||||
self._cursorCount = len(self._cursorImages) - 1
|
||||
# 创建刷新定时器
|
||||
self._cursorTimeout = 200
|
||||
self._cursorTimer = QTimer(parent)
|
||||
self._cursorTimer.timeout.connect(self._doBusy)
|
||||
self.num = 0
|
||||
|
||||
def _doBusy(self):
|
||||
if self._cursorIndex > self._cursorCount:
|
||||
self._cursorIndex = 0
|
||||
QApplication.setOverrideCursor(
|
||||
self._cursorImages[self._cursorIndex])
|
||||
self._cursorIndex += 1
|
||||
self.num += 1
|
||||
|
||||
def startBusy(self):
|
||||
# QApplication.setOverrideCursor(Qt.WaitCursor)
|
||||
if not self._cursorTimer.isActive():
|
||||
self._cursorTimer.start(self._cursorTimeout)
|
||||
|
||||
def stopBusy(self):
|
||||
self._cursorTimer.stop()
|
||||
QApplication.restoreOverrideCursor()
|
||||
# 将光标出栈,恢复至原始状态
|
||||
for i in range(self.num):
|
||||
QApplication.restoreOverrideCursor()
|
||||
self.num = 0
|
||||
|
||||
|
||||
def setCursorTimeout(self, timeout):
|
||||
self._cursorTimeout = timeout
|
||||
|
||||
def setOldCursor(self, parent=None):
|
||||
QApplication.overrideCursor()
|
||||
self._oldCursor = (QApplication.overrideCursor() or parent.cursor() or Qt.ArrowCursor or Qt.IBeamCursor) if parent else (
|
||||
QApplication.overrideCursor() or Qt.ArrowCursor)
|
||||
@@ -0,0 +1,2 @@
|
||||
from .contact_info_ui import ContactQListWidgetItem
|
||||
from .scroll_bar import ScrollBar
|
||||
@@ -0,0 +1,301 @@
|
||||
import os.path
|
||||
import subprocess
|
||||
import platform
|
||||
|
||||
from PyQt5 import QtGui
|
||||
from PyQt5.QtCore import QSize, pyqtSignal, Qt, QThread
|
||||
from PyQt5.QtGui import QPainter, QFont, QColor, QPixmap, QPolygon, QFontMetrics
|
||||
from PyQt5.QtWidgets import QWidget, QLabel, QHBoxLayout, QSizePolicy, QVBoxLayout, QSpacerItem, \
|
||||
QScrollArea
|
||||
|
||||
from app.components.scroll_bar import ScrollBar
|
||||
|
||||
|
||||
class MessageType:
|
||||
Text = 1
|
||||
Image = 3
|
||||
|
||||
|
||||
class TextMessage(QLabel):
|
||||
heightSingal = pyqtSignal(int)
|
||||
|
||||
def __init__(self, text, is_send=False, parent=None):
|
||||
if isinstance(text, bytes):
|
||||
text = text.decode('utf-8')
|
||||
super(TextMessage, self).__init__(text, parent)
|
||||
font = QFont('微软雅黑', 12)
|
||||
self.setFont(font)
|
||||
self.setWordWrap(True)
|
||||
self.setMaximumWidth(800)
|
||||
# self.setMinimumWidth(100)
|
||||
self.setMinimumHeight(45)
|
||||
self.setTextInteractionFlags(Qt.TextSelectableByMouse)
|
||||
self.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
|
||||
if is_send:
|
||||
self.setAlignment(Qt.AlignCenter | Qt.AlignRight)
|
||||
self.setStyleSheet(
|
||||
'''
|
||||
background-color:#b2e281;
|
||||
border-radius:10px;
|
||||
padding:10px;
|
||||
'''
|
||||
)
|
||||
else:
|
||||
self.setStyleSheet(
|
||||
'''
|
||||
background-color:white;
|
||||
border-radius:10px;
|
||||
padding:10px;
|
||||
'''
|
||||
)
|
||||
font_metrics = QFontMetrics(font)
|
||||
rect = font_metrics.boundingRect(text)
|
||||
# rect = font_metrics
|
||||
self.setMaximumWidth(rect.width() + 40)
|
||||
|
||||
def paintEvent(self, a0: QtGui.QPaintEvent) -> None:
|
||||
super(TextMessage, self).paintEvent(a0)
|
||||
|
||||
|
||||
class Triangle(QLabel):
|
||||
def __init__(self, Type, is_send=False, position=(0, 0), parent=None):
|
||||
"""
|
||||
|
||||
@param Type:
|
||||
@param is_send:
|
||||
@param position:(x,y)
|
||||
@param parent:
|
||||
"""
|
||||
super().__init__(parent)
|
||||
self.Type = Type
|
||||
self.is_send = is_send
|
||||
self.position = position
|
||||
|
||||
def paintEvent(self, a0: QtGui.QPaintEvent) -> None:
|
||||
|
||||
super(Triangle, self).paintEvent(a0)
|
||||
if self.Type == MessageType.Text:
|
||||
self.setFixedSize(6, 45)
|
||||
painter = QPainter(self)
|
||||
triangle = QPolygon()
|
||||
x, y = self.position
|
||||
if self.is_send:
|
||||
painter.setPen(QColor('#b2e281'))
|
||||
painter.setBrush(QColor('#b2e281'))
|
||||
triangle.setPoints(0, 20+y, 0, 34+y, 6, 27+y)
|
||||
else:
|
||||
painter.setPen(QColor('white'))
|
||||
painter.setBrush(QColor('white'))
|
||||
triangle.setPoints(0, 27+y, 6, 20+y, 6, 34+y)
|
||||
painter.drawPolygon(triangle)
|
||||
|
||||
|
||||
class Notice(QLabel):
|
||||
def __init__(self, text, type_=3, parent=None):
|
||||
super().__init__(text, parent)
|
||||
self.type_ = type_
|
||||
self.setFont(QFont('微软雅黑', 10))
|
||||
self.setWordWrap(True)
|
||||
self.setTextInteractionFlags(Qt.TextSelectableByMouse)
|
||||
self.setAlignment(Qt.AlignCenter)
|
||||
|
||||
|
||||
class Avatar(QLabel):
|
||||
def __init__(self, avatar, parent=None):
|
||||
super().__init__(parent)
|
||||
if isinstance(avatar, str):
|
||||
self.setPixmap(QPixmap(avatar).scaled(45, 45))
|
||||
self.image_path = avatar
|
||||
elif isinstance(avatar, QPixmap):
|
||||
self.setPixmap(avatar.scaled(45, 45))
|
||||
self.setFixedSize(QSize(45, 45))
|
||||
|
||||
|
||||
def open_image_viewer(file_path):
|
||||
system_platform = platform.system()
|
||||
|
||||
if system_platform == "Darwin": # macOS
|
||||
subprocess.run(["open", file_path])
|
||||
elif system_platform == "Windows":
|
||||
subprocess.run(["start", " ", file_path], shell=True)
|
||||
elif system_platform == "Linux":
|
||||
subprocess.run(["xdg-open", file_path])
|
||||
else:
|
||||
print("Unsupported platform")
|
||||
|
||||
|
||||
class OpenImageThread(QThread):
|
||||
def __init__(self, image_path):
|
||||
super().__init__()
|
||||
self.image_path = image_path
|
||||
|
||||
def run(self) -> None:
|
||||
if os.path.exists(self.image_path):
|
||||
open_image_viewer(self.image_path)
|
||||
|
||||
|
||||
class ImageMessage(QLabel):
|
||||
def __init__(self, image, is_send, image_link='', max_width=480, max_height=240, parent=None):
|
||||
"""
|
||||
param:image 图像路径或者QPixmap对象
|
||||
param:image_link='' 点击图像打开的文件路径
|
||||
"""
|
||||
super().__init__(parent)
|
||||
self.image = QLabel(self)
|
||||
self.max_width = max_width
|
||||
self.max_height = max_height
|
||||
# self.setFixedSize(self.max_width,self.max_height)
|
||||
self.setMaximumWidth(self.max_width)
|
||||
self.setMaximumHeight(self.max_height)
|
||||
self.setCursor(Qt.PointingHandCursor)
|
||||
if isinstance(image, str):
|
||||
pixmap = QPixmap(image)
|
||||
self.image_path = image
|
||||
elif isinstance(image, QPixmap):
|
||||
pixmap = image
|
||||
self.set_image(pixmap)
|
||||
if image_link:
|
||||
self.image_path = image_link
|
||||
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
|
||||
|
||||
if is_send:
|
||||
self.setAlignment(Qt.AlignCenter | Qt.AlignRight)
|
||||
# self.setScaledContents(True)
|
||||
|
||||
def set_image(self, pixmap):
|
||||
# 计算调整后的大小
|
||||
adjusted_width = min(pixmap.width(), self.max_width)
|
||||
adjusted_height = min(pixmap.height(), self.max_height)
|
||||
self.setPixmap(pixmap.scaled(adjusted_width, adjusted_height, Qt.KeepAspectRatio))
|
||||
# 调整QLabel的大小以适应图片的宽高,但不超过最大宽高
|
||||
# self.setFixedSize(adjusted_width, adjusted_height)
|
||||
|
||||
def mousePressEvent(self, event):
|
||||
if event.buttons() == Qt.LeftButton: # 左键按下
|
||||
print('打开图像', self.image_path)
|
||||
self.open_image_thread = OpenImageThread(self.image_path)
|
||||
self.open_image_thread.start()
|
||||
|
||||
|
||||
class BubbleMessage(QWidget):
|
||||
def __init__(self, str_content, avatar, Type, is_send=False, display_name=None, parent=None):
|
||||
super().__init__(parent)
|
||||
self.isSend = is_send
|
||||
# self.set
|
||||
self.setStyleSheet(
|
||||
'''
|
||||
border:none;
|
||||
'''
|
||||
)
|
||||
layout = QHBoxLayout()
|
||||
layout.setSpacing(0)
|
||||
layout.setContentsMargins(0, 5, 5, 5)
|
||||
# self.resize(QSize(200, 50))
|
||||
self.avatar = Avatar(avatar)
|
||||
triangle = Triangle(Type, is_send, (0, 0))
|
||||
if Type == MessageType.Text:
|
||||
self.message = TextMessage(str_content, is_send)
|
||||
# self.message.setMaximumWidth(int(self.width() * 0.6))
|
||||
elif Type == MessageType.Image:
|
||||
self.message = ImageMessage(str_content, is_send)
|
||||
else:
|
||||
raise ValueError("未知的消息类型")
|
||||
if display_name:
|
||||
triangle = Triangle(Type, is_send, (0, 10))
|
||||
label_name = QLabel(display_name, self)
|
||||
label_name.setFont(QFont('微软雅黑', 10))
|
||||
if is_send:
|
||||
label_name.setAlignment(Qt.AlignRight)
|
||||
vlayout = QVBoxLayout()
|
||||
vlayout.setSpacing(0)
|
||||
if is_send:
|
||||
vlayout.addWidget(label_name, 0, Qt.AlignTop | Qt.AlignRight)
|
||||
vlayout.addWidget(self.message, 0, Qt.AlignTop | Qt.AlignRight)
|
||||
else:
|
||||
vlayout.addWidget(label_name)
|
||||
vlayout.addWidget(self.message)
|
||||
self.spacerItem = QSpacerItem(45 + 6, 45, QSizePolicy.Expanding, QSizePolicy.Minimum)
|
||||
if is_send:
|
||||
layout.addItem(self.spacerItem)
|
||||
if display_name:
|
||||
layout.addLayout(vlayout, 1)
|
||||
else:
|
||||
layout.addWidget(self.message, 1)
|
||||
layout.addWidget(triangle, 0, Qt.AlignTop | Qt.AlignLeft)
|
||||
layout.addWidget(self.avatar, 0, Qt.AlignTop | Qt.AlignLeft)
|
||||
else:
|
||||
layout.addWidget(self.avatar, 0, Qt.AlignTop | Qt.AlignRight)
|
||||
layout.addWidget(triangle, 0, Qt.AlignTop | Qt.AlignRight)
|
||||
if display_name:
|
||||
layout.addLayout(vlayout, 1)
|
||||
else:
|
||||
layout.addWidget(self.message, 1)
|
||||
layout.addItem(self.spacerItem)
|
||||
self.setLayout(layout)
|
||||
|
||||
|
||||
class ScrollAreaContent(QWidget):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.adjustSize()
|
||||
|
||||
|
||||
class ScrollArea(QScrollArea):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.setWidgetResizable(True)
|
||||
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
||||
self.setStyleSheet(
|
||||
'''
|
||||
border:none;
|
||||
'''
|
||||
)
|
||||
|
||||
|
||||
class ChatWidget(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.resize(500, 200)
|
||||
|
||||
layout = QVBoxLayout()
|
||||
layout.setSpacing(0)
|
||||
self.adjustSize()
|
||||
# 生成滚动区域
|
||||
self.scrollArea = ScrollArea(self)
|
||||
scrollBar = ScrollBar()
|
||||
self.scrollArea.setVerticalScrollBar(scrollBar)
|
||||
# 生成滚动区域的内容部署层部件
|
||||
self.scrollAreaWidgetContents = ScrollAreaContent(self.scrollArea)
|
||||
self.scrollAreaWidgetContents.setMinimumSize(50, 100)
|
||||
# 设置滚动区域的内容部署部件为前面生成的内容部署层部件
|
||||
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
|
||||
layout.addWidget(self.scrollArea)
|
||||
self.layout0 = QVBoxLayout()
|
||||
self.layout0.setSpacing(0)
|
||||
self.scrollAreaWidgetContents.setLayout(self.layout0)
|
||||
self.setLayout(layout)
|
||||
|
||||
def add_message_item(self, bubble_message, index=1):
|
||||
if index:
|
||||
self.layout0.addWidget(bubble_message)
|
||||
else:
|
||||
self.layout0.insertWidget(0, bubble_message)
|
||||
# self.set_scroll_bar_last()
|
||||
|
||||
def set_scroll_bar_last(self):
|
||||
self.scrollArea.verticalScrollBar().setValue(
|
||||
self.scrollArea.verticalScrollBar().maximum()
|
||||
)
|
||||
|
||||
def set_scroll_bar_value(self, val):
|
||||
self.verticalScrollBar().setValue(val)
|
||||
|
||||
def verticalScrollBar(self):
|
||||
return self.scrollArea.verticalScrollBar()
|
||||
|
||||
def update(self) -> None:
|
||||
super().update()
|
||||
self.scrollAreaWidgetContents.adjustSize()
|
||||
self.scrollArea.update()
|
||||
# self.scrollArea.repaint()
|
||||
# self.verticalScrollBar().setMaximum(self.scrollAreaWidgetContents.height())
|
||||
@@ -0,0 +1,78 @@
|
||||
import time
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from PyQt5.QtCore import QTimer, QThread, pyqtSignal, Qt
|
||||
from PyQt5.QtGui import QIcon
|
||||
from PyQt5.QtWidgets import QApplication, QDialog, QCheckBox, QMessageBox, QCalendarWidget, QWidget, QVBoxLayout, \
|
||||
QToolButton
|
||||
|
||||
from app.ui.Icon import Icon
|
||||
|
||||
|
||||
class CalendarDialog(QDialog):
|
||||
selected_date_signal = pyqtSignal(int)
|
||||
|
||||
def __init__(self, date_range=None, parent=None):
|
||||
"""
|
||||
|
||||
@param date_range: tuple[Union[QDate, datetime.date],Union[QDate, datetime.date]] #限定的可选择范围
|
||||
@param parent:
|
||||
"""
|
||||
super().__init__(parent)
|
||||
self.setWindowTitle('选择日期')
|
||||
self.calendar = QCalendarWidget(self)
|
||||
self.calendar.clicked.connect(self.onDateChanged)
|
||||
prev_btn = self.calendar.findChild(QToolButton, "qt_calendar_prevmonth")
|
||||
prev_btn.setIcon(Icon.Arrow_left_Icon)
|
||||
next_btn = self.calendar.findChild(QToolButton, "qt_calendar_nextmonth")
|
||||
next_btn.setIcon(Icon.Arrow_right_Icon)
|
||||
self.date_range = date_range
|
||||
if date_range:
|
||||
self.calendar.setDateRange(*date_range)
|
||||
# 从第一天开始,依次添加日期到列表,直到该月的最后一天
|
||||
current_date = date_range[1]
|
||||
while (current_date + timedelta(days=1)).month == date_range[1].month:
|
||||
current_date += timedelta(days=1)
|
||||
range_format = self.calendar.dateTextFormat(current_date)
|
||||
range_format.setForeground(Qt.gray)
|
||||
self.calendar.setDateTextFormat(current_date, range_format)
|
||||
# 从第一天开始,依次添加日期到列表,直到该月的最后一天
|
||||
current_date = date_range[0]
|
||||
while (current_date - timedelta(days=1)).month == date_range[0].month:
|
||||
current_date -= timedelta(days=1)
|
||||
range_format = self.calendar.dateTextFormat(current_date)
|
||||
range_format.setForeground(Qt.gray)
|
||||
self.calendar.setDateTextFormat(current_date, range_format)
|
||||
layout = QVBoxLayout(self)
|
||||
layout.addWidget(self.calendar)
|
||||
self.setLayout(layout)
|
||||
|
||||
def set_start_date(self):
|
||||
if self.date_range:
|
||||
self.calendar.setCurrentPage(self.date_range[0].year, self.date_range[0].month)
|
||||
def set_end_date(self):
|
||||
if self.date_range:
|
||||
self.calendar.setCurrentPage(self.date_range[1].year, self.date_range[1].month)
|
||||
def onDateChanged(self):
|
||||
# 获取选择的日期
|
||||
selected_date = self.calendar.selectedDate()
|
||||
s_t = time.strptime(selected_date.toString("yyyy-MM-dd"), "%Y-%m-%d") # 返回元祖
|
||||
mkt = int(time.mktime(s_t))
|
||||
timestamp = mkt
|
||||
self.selected_date_signal.emit(timestamp)
|
||||
print("Selected Date:", selected_date.toString("yyyy-MM-dd"), timestamp)
|
||||
self.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
# 设置日期范围
|
||||
start_date = datetime(2024, 1, 5)
|
||||
end_date = datetime(2024, 1, 9)
|
||||
date_range = (start_date.date(), end_date.date())
|
||||
ex = CalendarDialog(date_range=date_range)
|
||||
ex.show()
|
||||
sys.exit(app.exec_())
|
||||
@@ -0,0 +1,104 @@
|
||||
import sys
|
||||
|
||||
from PyQt5.Qt import *
|
||||
from PyQt5.QtCore import *
|
||||
from PyQt5.QtWidgets import *
|
||||
|
||||
from .CAvatar import CAvatar
|
||||
|
||||
Stylesheet = """
|
||||
QWidget{
|
||||
background: rgb(238,244,249);
|
||||
}
|
||||
"""
|
||||
Stylesheet_hover = """
|
||||
QWidget,QLabel{
|
||||
background: rgb(230, 235, 240);
|
||||
}
|
||||
"""
|
||||
Stylesheet_clicked = """
|
||||
QWidget,QLabel{
|
||||
background: rgb(230, 235, 240);
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class QListWidgetItemWidget(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.is_selected = False
|
||||
|
||||
def leaveEvent(self, e): # 鼠标离开label
|
||||
if self.is_selected:
|
||||
return
|
||||
self.setStyleSheet(Stylesheet)
|
||||
|
||||
def enterEvent(self, e): # 鼠标移入label
|
||||
self.setStyleSheet(Stylesheet_hover)
|
||||
|
||||
|
||||
# 自定义的item 继承自QListWidgetItem
|
||||
class ContactQListWidgetItem(QListWidgetItem):
|
||||
def __init__(self, name, url, img_bytes=None):
|
||||
super().__init__()
|
||||
# 自定义item中的widget 用来显示自定义的内容
|
||||
self.widget = QListWidgetItemWidget()
|
||||
# 用来显示name
|
||||
self.nameLabel = QLabel(self.widget)
|
||||
self.nameLabel.setText(name)
|
||||
# 用来显示avator(图像)
|
||||
self.avatorLabel = CAvatar(parent=self.widget, shape=CAvatar.Rectangle, size=QSize(60, 60),
|
||||
url=url, img_bytes=img_bytes)
|
||||
# 设置布局用来对nameLabel和avatorLabel进行布局
|
||||
hbox = QHBoxLayout()
|
||||
hbox.addWidget(self.avatorLabel)
|
||||
hbox.addWidget(self.nameLabel)
|
||||
hbox.addStretch(1)
|
||||
# 设置widget的布局
|
||||
self.widget.setLayout(hbox)
|
||||
self.widget.setStyleSheet(Stylesheet)
|
||||
# 设置自定义的QListWidgetItem的sizeHint,不然无法显示
|
||||
self.setSizeHint(self.widget.sizeHint())
|
||||
|
||||
def select(self):
|
||||
"""
|
||||
设置选择后的事件
|
||||
@return:
|
||||
"""
|
||||
self.widget.is_selected = True
|
||||
self.widget.setStyleSheet(Stylesheet_clicked)
|
||||
|
||||
def dis_select(self):
|
||||
"""
|
||||
设置取消选择的事件
|
||||
@return:
|
||||
"""
|
||||
self.widget.is_selected = False
|
||||
self.widget.setStyleSheet(Stylesheet)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
# 主窗口
|
||||
w = QWidget()
|
||||
w.setWindowTitle("QListWindow")
|
||||
# 新建QListWidget
|
||||
listWidget = QListWidget(w)
|
||||
listWidget.resize(300, 300)
|
||||
|
||||
# 新建两个自定义的QListWidgetItem(customQListWidgetItem)
|
||||
item1 = ContactQListWidgetItem("鲤鱼王", "liyuwang.jpg")
|
||||
item2 = ContactQListWidgetItem("可达鸭", "kedaya.jpg")
|
||||
|
||||
# 在listWidget中加入两个自定义的item
|
||||
listWidget.addItem(item1)
|
||||
listWidget.setItemWidget(item1, item1.widget)
|
||||
listWidget.addItem(item2)
|
||||
listWidget.setItemWidget(item2, item2.widget)
|
||||
|
||||
# 绑定点击槽函数 点击显示对应item中的name
|
||||
listWidget.itemClicked.connect(lambda item: print(item.nameLabel.text()))
|
||||
|
||||
w.show()
|
||||
sys.exit(app.exec_())
|
||||
@@ -0,0 +1,127 @@
|
||||
import sys
|
||||
|
||||
from PyQt5.Qt import *
|
||||
from PyQt5.QtCore import *
|
||||
from PyQt5.QtWidgets import *
|
||||
|
||||
try:
|
||||
from .CAvatar import CAvatar
|
||||
except:
|
||||
from CAvatar import CAvatar
|
||||
|
||||
Stylesheet = """
|
||||
QWidget{
|
||||
background: rgb(238,244,249);
|
||||
}
|
||||
"""
|
||||
Stylesheet_hover = """
|
||||
QWidget,QLabel{
|
||||
background: rgb(230, 235, 240);
|
||||
}
|
||||
"""
|
||||
Stylesheet_clicked = """
|
||||
QWidget,QLabel{
|
||||
background: rgb(230, 235, 240);
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class QListWidgetItemWidget(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.is_selected = False
|
||||
|
||||
def leaveEvent(self, e): # 鼠标离开label
|
||||
if self.is_selected:
|
||||
return
|
||||
self.setStyleSheet(Stylesheet)
|
||||
|
||||
def enterEvent(self, e): # 鼠标移入label
|
||||
self.setStyleSheet(Stylesheet_hover)
|
||||
|
||||
|
||||
# 自定义的item 继承自QListWidgetItem
|
||||
class ContactQListWidgetItem(QListWidgetItem):
|
||||
def __init__(self, name, url, img_bytes=None):
|
||||
super().__init__()
|
||||
self.is_select = False
|
||||
# 自定义item中的widget 用来显示自定义的内容
|
||||
self.widget = QListWidgetItemWidget()
|
||||
# 用来显示name
|
||||
self.nameLabel = QLabel(self.widget)
|
||||
self.nameLabel.setText(name)
|
||||
# 用来显示avator(图像)
|
||||
self.avatorLabel = CAvatar(parent=self.widget, shape=CAvatar.Rectangle, size=QSize(30, 30),
|
||||
url=url, img_bytes=img_bytes)
|
||||
# 设置布局用来对nameLabel和avatorLabel进行布局
|
||||
hbox = QHBoxLayout(self.widget)
|
||||
self.checkBox = QCheckBox(self.widget)
|
||||
self.checkBox.clicked.connect(self.select)
|
||||
hbox.addWidget(self.checkBox)
|
||||
hbox.addWidget(self.avatorLabel)
|
||||
hbox.addWidget(self.nameLabel)
|
||||
hbox.addStretch(1)
|
||||
# 设置widget的布局
|
||||
self.widget.setLayout(hbox)
|
||||
self.widget.setStyleSheet(Stylesheet)
|
||||
# 设置自定义的QListWidgetItem的sizeHint,不然无法显示
|
||||
self.setSizeHint(self.widget.sizeHint())
|
||||
sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
|
||||
sizePolicy.setHorizontalStretch(0)
|
||||
sizePolicy.setVerticalStretch(0)
|
||||
sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth())
|
||||
self.widget.setSizePolicy(sizePolicy)
|
||||
|
||||
def select(self):
|
||||
"""
|
||||
设置选择后的事件
|
||||
@return:
|
||||
"""
|
||||
self.widget.is_selected = True
|
||||
self.is_select = not self.is_select
|
||||
# print('选择',self.is_select)
|
||||
self.checkBox.setChecked(self.is_select)
|
||||
# self.widget.setStyleSheet(Stylesheet_clicked)
|
||||
|
||||
def force_select(self):
|
||||
self.is_select = True
|
||||
self.checkBox.setChecked(self.is_select)
|
||||
|
||||
def force_dis_select(self):
|
||||
self.is_select = False
|
||||
self.checkBox.setChecked(self.is_select)
|
||||
|
||||
def dis_select(self):
|
||||
"""
|
||||
设置取消选择的事件
|
||||
@return:
|
||||
"""
|
||||
self.widget.is_selected = False
|
||||
self.widget.setStyleSheet(Stylesheet)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
# 主窗口
|
||||
w = QWidget()
|
||||
w.setWindowTitle("QListWindow")
|
||||
# 新建QListWidget
|
||||
listWidget = QListWidget(w)
|
||||
listWidget.resize(300, 300)
|
||||
|
||||
# 新建两个自定义的QListWidgetItem(customQListWidgetItem)
|
||||
item1 = ContactQListWidgetItem("鲤鱼王", "liyuwang.jpg")
|
||||
item2 = ContactQListWidgetItem("可达鸭", "kedaya.jpg")
|
||||
|
||||
# 在listWidget中加入两个自定义的item
|
||||
listWidget.addItem(item1)
|
||||
listWidget.setItemWidget(item1, item1.widget)
|
||||
listWidget.addItem(item2)
|
||||
listWidget.setItemWidget(item2, item2.widget)
|
||||
|
||||
# 绑定点击槽函数 点击显示对应item中的name
|
||||
listWidget.itemClicked.connect(lambda item: item.select())
|
||||
|
||||
w.show()
|
||||
sys.exit(app.exec_())
|
||||
@@ -0,0 +1,39 @@
|
||||
from PyQt5 import QtGui
|
||||
from PyQt5.QtCore import *
|
||||
from PyQt5.QtGui import *
|
||||
from PyQt5.QtWidgets import *
|
||||
|
||||
|
||||
class PromptBar(QLabel):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
|
||||
def paintEvent(self, e): # 绘图事件
|
||||
qp = QPainter()
|
||||
qp.begin(self)
|
||||
self.drawRectangles1(qp) # 绘制线条矩形
|
||||
self.drawRectangles2(qp) # 绘制填充矩形
|
||||
self.drawRectangles3(qp) # 绘制线条+填充矩形
|
||||
self.drawRectangles4(qp) # 绘制线条矩形2
|
||||
qp.end()
|
||||
|
||||
def drawRectangles1(self, qp): # 绘制填充矩形
|
||||
qp.setPen(QPen(Qt.black, 2, Qt.SolidLine)) # 颜色、线宽、线性
|
||||
qp.drawRect(*self.data)
|
||||
|
||||
def drawRectangles2(self, qp): # 绘制填充矩形
|
||||
qp.setPen(QPen(Qt.black, 2, Qt.NoPen))
|
||||
qp.setBrush(QColor(200, 0, 0))
|
||||
qp.drawRect(220, 15, 200, 100)
|
||||
|
||||
def drawRectangles3(self, qp): # 绘制线条+填充矩形
|
||||
qp.setPen(QPen(Qt.black, 2, Qt.SolidLine))
|
||||
qp.setBrush(QColor(200, 0, 0))
|
||||
qp.drawRect(430, 15, 200, 100)
|
||||
|
||||
def drawRectangles4(self, qp): # 绘制线条矩形2
|
||||
path = QtGui.QPainterPath()
|
||||
qp.setPen(QPen(Qt.blue, 2, Qt.SolidLine))
|
||||
qp.setBrush(QColor(0, 0, 0, 0)) # 设置画刷颜色透明
|
||||
path.addRect(100, 200, 200, 100)
|
||||
qp.drawPath(path)
|
||||
@@ -0,0 +1,48 @@
|
||||
from PyQt5.QtWidgets import QScrollBar
|
||||
|
||||
|
||||
class ScrollBar(QScrollBar):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.setStyleSheet(
|
||||
'''
|
||||
QScrollBar:vertical {
|
||||
border-width: 0px;
|
||||
border: none;
|
||||
background:rgba(133, 135, 138, 0);
|
||||
width:4px;
|
||||
margin: 0px 0px 0px 0px;
|
||||
}
|
||||
QScrollBar::handle:vertical {
|
||||
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
|
||||
stop: 0 rgb(133, 135, 138), stop: 0.5 rgb(133, 135, 138), stop:1 rgb(133, 135, 138));
|
||||
min-height: 20px;
|
||||
max-height: 20px;
|
||||
margin: 0 0px 0 0px;
|
||||
border-radius: 2px;
|
||||
}
|
||||
QScrollBar::add-line:vertical {
|
||||
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
|
||||
stop: 0 rgba(133, 135, 138, 0), stop: 0.5 rgba(133, 135, 138, 0), stop:1 rgba(133, 135, 138, 0));
|
||||
height: 0px;
|
||||
border: none;
|
||||
subcontrol-position: bottom;
|
||||
subcontrol-origin: margin;
|
||||
}
|
||||
QScrollBar::sub-line:vertical {
|
||||
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
|
||||
stop: 0 rgba(133, 135, 138, 0), stop: 0.5 rgba(133, 135, 138, 0), stop:1 rgba(133, 135, 138, 0));
|
||||
height: 0 px;
|
||||
border: none;
|
||||
subcontrol-position: top;
|
||||
subcontrol-origin: margin;
|
||||
}
|
||||
QScrollBar::sub-page:vertical {
|
||||
background: rgba(133, 135, 138, 0);
|
||||
}
|
||||
|
||||
QScrollBar::add-page:vertical {
|
||||
background: rgba(133, 135, 138, 0);
|
||||
}
|
||||
'''
|
||||
)
|
||||
Reference in new issue
Block a user