Skip to content

Commit

Permalink
Update bubble_message.py
Browse files Browse the repository at this point in the history
  • Loading branch information
LC044 authored Nov 17, 2023
1 parent b306415 commit 142a260
Showing 1 changed file with 142 additions and 30 deletions.
172 changes: 142 additions & 30 deletions app/components/bubble_message.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from PIL import Image
from PyQt5 import QtGui
from PyQt5.QtGui import QPainter, QFont, QColor, QPixmap, QPolygon
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QSizePolicy, QVBoxLayout, QSpacerItem
from PyQt5.QtCore import QSize, pyqtSignal, Qt, QRectF, QPoint
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QSizePolicy, QVBoxLayout, QSpacerItem, \
QScrollArea, QScrollBar
from PyQt5.QtCore import QSize, pyqtSignal, Qt, QThread


class TextMessage(QLabel):
Expand Down Expand Up @@ -72,6 +73,16 @@ def paintEvent(self, a0: QtGui.QPaintEvent) -> None:
painter.drawPolygon(triangle)


class Notice(QLabel):
def __init__(self, text, type_=3, parent=None):
super().__init__(text, parent)
self.type_ = type_
self.setFont(QFont('微软雅黑', 12))
self.setWordWrap(True)
self.setTextInteractionFlags(Qt.TextSelectableByMouse)
self.setAlignment(Qt.AlignCenter)


class Avatar(QLabel):
def __init__(self, avatar, parent=None):
super().__init__(parent)
Expand All @@ -84,6 +95,16 @@ def __init__(self, avatar, parent=None):
self.setMaximumHeight(45)


class OpenImageThread(QThread):
def __init__(self, image_path):
super().__init__()
self.image_path = image_path

def run(self) -> None:
image = Image.open(self.image_path)
image.show()


class ImageMessage(QLabel):
def __init__(self, avatar, parent=None):
super().__init__(parent)
Expand All @@ -96,30 +117,28 @@ def __init__(self, avatar, parent=None):
self.setPixmap(avatar)
self.setMaximumWidth(480)
self.setMaximumHeight(720)
# self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
# self.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
# self.setScaledContents(True)

# def paintEvent(self, a0) -> None:
# painter = QPainter(self)
# painter.begin(self)
# # self.setPixmap()
# painter.end()

def mousePressEvent(self, event):
if event.buttons() == Qt.LeftButton: # 左键按下
image = Image.open(self.image_path)
image.show()
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, 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.avatar = Avatar(avatar)
triangle = Triangle(Type,is_send)
triangle = Triangle(Type, is_send)
if Type == 3:
self.message = TextMessage(str_content, is_send)
else:
Expand All @@ -133,48 +152,141 @@ def __init__(self, str_content, avatar, Type, is_send=False, parent=None):
border-left: 5px transparent;
border-radius:10px;
'''
self.spacerItem = QSpacerItem(65, 65, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.spacerItem = QSpacerItem(45 + 6, 45, QSizePolicy.Expanding, QSizePolicy.Minimum)
if is_send:
layout.addItem(self.spacerItem)
layout.addWidget(self.message, 10)
layout.addWidget(triangle,0,Qt.AlignTop | Qt.AlignLeft)
layout.addWidget(self.message, 1)
layout.addWidget(triangle, 0, Qt.AlignTop | Qt.AlignLeft)
layout.addWidget(self.avatar, 0, Qt.AlignTop | Qt.AlignLeft)
layout.setStretch(0, 1)
else:
layout.addWidget(self.avatar, 0, Qt.AlignTop | Qt.AlignRight)
layout.addWidget(triangle, 0, Qt.AlignTop | Qt.AlignRight)
layout.addWidget(self.message, 10)
layout.addWidget(self.message, 1)
layout.addItem(self.spacerItem)
layout.setStretch(3, 1)
self.setLayout(layout)


class ScrollAreaContent(QWidget):
def __init__(self, parent=None):
super().__init__(parent)

def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
# print(self.width(),self.height())
self.setMinimumSize(self.width(), self.height())


class ScrollArea(QScrollArea):
def __init__(self, parent=None):
super().__init__(parent)
self.setWidgetResizable(True)
self.setStyleSheet(
'''
border:none;
'''
)

def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
# return
self.widget().setMinimumSize(self.width(), self.widget().height())
self.widget().setMaximumSize(self.width(), self.widget().height())
self.widget().resize(QSize(self.width(), self.widget().height()))


#

class ScrollBar(QScrollBar):
def __init__(self):
super().__init__()
self.setStyleSheet(
'''
QScrollBar:vertical {
border-width: 0px;
border: none;
background:rgba(64, 65, 79, 0);
width:5px;
margin: 0px 0px 0px 0px;
}
QScrollBar::handle:vertical {
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
stop: 0 #DDDDDD, stop: 0.5 #DDDDDD, stop:1 #aaaaff);
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(64, 65, 79, 0), stop: 0.5 rgba(64, 65, 79, 0), stop:1 rgba(64, 65, 79, 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(64, 65, 79, 0), stop: 0.5 rgba(64, 65, 79, 0), stop:1 rgba(64, 65, 79, 0));
height: 0 px;
border: none;
subcontrol-position: top;
subcontrol-origin: margin;
}
QScrollBar::sub-page:vertical {
background: rgba(64, 65, 79, 0);
}
QScrollBar::add-page:vertical {
background: rgba(64, 65, 79, 0);
}
'''
)


class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.resize(500, 200)
txt = '''在工具中单击边缘可以添加黑点,单击可以删掉黑点,拖动可以调整黑点长度。勾选等选项可以查看内容、缩放等区域右侧可预览不同拉伸情况下的效果,拖动可以调整预览的拉伸比例'''
avatar = 'Data/head.jpg'
bubble_message = BubbleMessage(txt, avatar, Type=3, is_send=False)
layout = QVBoxLayout()
layout.setSpacing(0)

# 生成滚动区域
self.scrollArea = ScrollArea()
self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
scrollBar = ScrollBar()
self.scrollArea.setVerticalScrollBar(scrollBar)
# self.scrollArea.setGeometry(QRect(9, 9, 261, 211))
# 生成滚动区域的内容部署层部件
self.scrollAreaWidgetContents = ScrollAreaContent()
self.scrollAreaWidgetContents.setMinimumSize(50, 100)
# 设置滚动区域的内容部署部件为前面生成的内容部署层部件
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
layout.addWidget(self.scrollArea)
layout0 = QVBoxLayout()
layout0.setSpacing(0)
self.scrollArea.setLayout(layout0)
self.scrollAreaWidgetContents.setLayout(layout0)

time = Notice("2023-11-17 15:44")
layout0.addWidget(time)
txt = "你说啥"
avatar_2 = 'Data/fg1.png'
bubble_message1 = BubbleMessage(txt, avatar_2, Type=3, is_send=True)
layout.addWidget(bubble_message)
layout.addWidget(bubble_message1)
layout0.addWidget(bubble_message)
layout0.addWidget(bubble_message1)

bubble_message2 = BubbleMessage('', avatar_2, Type=3, is_send=True)
layout.addWidget(bubble_message2)
layout0.addWidget(bubble_message2)
txt = "我啥都没说"
avatar0 = 'Data/fg1.png'
bubble_message1 = BubbleMessage('Data/fg1.png', avatar, Type=1, is_send=False)
layout.addWidget(bubble_message1)
self.spacerItem = QSpacerItem(65, 65, QSizePolicy.Minimum, QSizePolicy.Expanding)
layout.addItem(self.spacerItem)
# layout.setStretch(0, 1)
layout0.addWidget(bubble_message1)
self.spacerItem = QSpacerItem(10, 10, QSizePolicy.Minimum, QSizePolicy.Expanding)
layout0.addItem(self.spacerItem)
layout.setStretch(0, 1)
self.setLayout(layout)

# def resizeEvent(self, a0) -> None:
# return


if __name__ == '__main__':
app = QApplication([])
Expand Down

0 comments on commit 142a260

Please sign in to comment.