聊天消息自适应

This commit is contained in:
shuaikangzhou 2023-11-18 13:25:56 +08:00
parent c72b67f2a0
commit a248146e83
4 changed files with 108 additions and 113 deletions

View File

@ -4,13 +4,11 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="84e65474-7da9-466d-baf3-cc88dde3ffdd" name="变更" comment="修改聊天记录显示顺序">
<list default="true" id="84e65474-7da9-466d-baf3-cc88dde3ffdd" name="变更" comment="聊天记录从后往前显示">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/DataBase/micro_msg.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/DataBase/micro_msg.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/DataBase/msg.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/DataBase/msg.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/components/bubble_message.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/components/bubble_message.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/ui_pc/chat/chat_info.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/ui_pc/chat/chat_info.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/ui_pc/chat/chat_window.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/ui_pc/chat/chat_window.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/ui_pc/contact/contact_window.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/ui_pc/contact/contact_window.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -128,27 +126,6 @@
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="chat_info" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="WeChatMsg" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/app/ui_pc/chat" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/app/ui_pc/chat/chat_info.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="main_pc" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="WeChatMsg" />
<option name="INTERPRETER_OPTIONS" value="" />
@ -254,13 +231,34 @@
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="test" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="WeChatMsg" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/app/components" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/app/components/test.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Python.main_pc" />
<item itemvalue="Python.bubble_message" />
<item itemvalue="Python.person" />
<item itemvalue="Python.chat_info" />
<item itemvalue="Python.test" />
<item itemvalue="Python.msg" />
<item itemvalue="Python.person" />
</list>
</recent_temporary>
</component>
@ -276,13 +274,6 @@
<option name="presentableId" value="Default" />
<updated>1672848140146</updated>
</task>
<task id="LOCAL-00024" summary="readme">
<created>1684599688133</created>
<option name="number" value="00024" />
<option name="presentableId" value="LOCAL-00024" />
<option name="project" value="LOCAL" />
<updated>1684599688133</updated>
</task>
<task id="LOCAL-00025" summary="readme">
<created>1684600483941</created>
<option name="number" value="00025" />
@ -619,7 +610,14 @@
<option name="project" value="LOCAL" />
<updated>1700236795662</updated>
</task>
<option name="localTasksCounter" value="73" />
<task id="LOCAL-00073" summary="聊天记录从后往前显示">
<created>1700279520681</created>
<option name="number" value="00073" />
<option name="presentableId" value="LOCAL-00073" />
<option name="project" value="LOCAL" />
<updated>1700279520681</updated>
</task>
<option name="localTasksCounter" value="74" />
<servers />
</component>
<component name="UnknownFeatures">
@ -655,7 +653,6 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="修改导入路径方便打包成exe" />
<MESSAGE value="修改部分UI" />
<MESSAGE value="上传html模板" />
<MESSAGE value="增加几个图标" />
@ -680,7 +677,8 @@
<MESSAGE value="更改头像显示" />
<MESSAGE value="将自己的wxid设置为配置文件" />
<MESSAGE value="修改聊天记录显示顺序" />
<option name="LAST_COMMIT_MESSAGE" value="修改聊天记录显示顺序" />
<MESSAGE value="聊天记录从后往前显示" />
<option name="LAST_COMMIT_MESSAGE" value="聊天记录从后往前显示" />
<option name="OPTIMIZE_IMPORTS_BEFORE_PROJECT_COMMIT" value="true" />
<option name="REFORMAT_BEFORE_PROJECT_COMMIT" value="true" />
</component>

View File

@ -2,6 +2,7 @@ import os.path
import re
import sqlite3
import threading
from pprint import pprint
DB = []
cursor = []
@ -56,22 +57,21 @@ def get_messages(username_):
return result
def get_message_by_num(username_, n):
def get_message_by_num(username_, local_id):
sql = '''
select localId,TalkerId,Type,SubType,IsSender,CreateTime,Status,StrContent,strftime('%Y-%m-%d %H:%M:%S',CreateTime,'unixepoch','localtime') as StrTime
from MSG
where StrTalker=?
order by CreateTime desc
limit 100
where StrTalker = ? and localId < ?
order by CreateTime desc
limit 30
'''
result = []
try:
lock.acquire(True)
for cur in cursor:
cur = cursor[-1]
cur.execute(sql, [username_])
cur.execute(sql, [username_, local_id])
result_ = cur.fetchall()
result_.reverse()
result += result_
return result_
finally:
@ -93,5 +93,8 @@ if __name__ == '__main__':
# result = get_messages(username)
# pprint(result)
# pprint(len(result))
result = get_message_by_num('wxid_0o18ef858vnu22', 0)
result = get_message_by_num('wxid_0o18ef858vnu22', 9999999)
print(result)
print(result[-1][0])
local_id = result[-1][0]
pprint(get_message_by_num('wxid_0o18ef858vnu22', local_id))

View File

@ -140,6 +140,7 @@ class BubbleMessage(QWidget):
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)
if Type == 1:
@ -173,35 +174,20 @@ class BubbleMessage(QWidget):
class ScrollAreaContent(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# self.setStyleSheet(
# '''
# background-color:rgb(127,127,127);
# '''
# )
def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
# print(self.width(),self.height())
self.setMinimumSize(self.width(), self.height())
self.adjustSize()
class ScrollArea(QScrollArea):
def __init__(self, parent=None):
super().__init__(parent)
self.setWidgetResizable(True)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
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):
@ -254,57 +240,41 @@ class ChatWidget(QWidget):
def __init__(self):
super().__init__()
self.resize(500, 200)
txt = '''在工具中单击边缘可以添加黑点,单击可以删掉黑点,拖动可以调整黑点长度。勾选等选项可以查看内容、缩放等区域右侧可预览不同拉伸情况下的效果,拖动可以调整预览的拉伸比例'''
avatar = './app/data/icons/default_avatar.svg'
bubble_message = BubbleMessage(txt, avatar, Type=1, is_send=False)
layout = QVBoxLayout()
layout.setSpacing(0)
self.adjustSize()
# 生成滚动区域
self.scrollArea = ScrollArea()
self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scrollArea = ScrollArea(self)
scrollBar = ScrollBar()
self.scrollArea.setVerticalScrollBar(scrollBar)
# self.scrollArea.setGeometry(QRect(9, 9, 261, 211))
# 生成滚动区域的内容部署层部件
self.scrollAreaWidgetContents = ScrollAreaContent()
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.layout0.addWidget(bubble_message)
# self.scrollArea.setLayout(layout0)
self.scrollAreaWidgetContents.setLayout(self.layout0)
time = Notice("2023-11-17 15:44")
# self.layout0.addWidget(time)
# txt = "你说啥"
# avatar_2 = '../data/icons/default_avatar.svg'
# bubble_message1 = BubbleMessage(txt, avatar_2, Type=1, is_send=True)
#
# self.layout0.addWidget(bubble_message1)
#
# bubble_message2 = BubbleMessage('', avatar_2, Type=1, is_send=True)
# self.layout0.addWidget(bubble_message2)
# txt = "我啥都没说"
# avatar0 = 'Data/fg1.png'
bubble_message1 = BubbleMessage("D:\Project\Python\PyQt-master\QLabel\Data\\fg1.png", avatar, Type=3,
is_send=False)
self.layout0.addWidget(bubble_message1)
self.spacerItem = QSpacerItem(10, 10, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.layout0.addItem(self.spacerItem)
# layout.setStretch(0, 1)
self.setLayout(layout)
def add_message_item(self, bubble_message):
self.layout0.addWidget(bubble_message)
def add_message_item(self, bubble_message, index=1):
if index:
self.layout0.addWidget(bubble_message)
else:
self.layout0.insertWidget(0, bubble_message)
def set_scroll_bar_last(self):
self.scrollArea.verticalScrollBar().setValue(self.scrollArea.widget().height())
def set_scroll_bar_value(self, val):
self.verticalScrollBar().setValue(val)
def verticalScrollBar(self):
return self.scrollArea.verticalScrollBar()
class Test(QWidget):
def __init__(self):
@ -318,6 +288,12 @@ class Test(QWidget):
Type=1,
)
w1.add_message_item(bm1)
for i in range(10):
txt = '''在工具中单击边缘可以添加黑点,单击可以删掉黑点,拖动可以调整黑点长度。勾选等选项可以查看内容、缩放等区域右侧可预览不同拉伸情况下的效果,拖动可以调整预览的拉伸比例'''
avatar = '../data/icons/default_avatar.svg'
bubble_message = BubbleMessage(txt, avatar, Type=1, is_send=False)
print(bubble_message.height(), '高度')
w1.add_message_item(bubble_message, 0)
w2 = QLabel("nihao")
layout.addWidget(w1)
layout.addWidget(w2)

View File

@ -1,14 +1,15 @@
from PyQt5.QtCore import QThread, pyqtSignal, Qt
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout
from app.DataBase import msg
from app.components.bubble_message import BubbleMessage, ScrollBar, ScrollArea, ScrollAreaContent, ChatWidget
from app.components.bubble_message import BubbleMessage, ChatWidget
from app.person import MePC
class ChatInfo(QWidget):
def __init__(self, contact, parent=None):
super().__init__(parent)
self.last_pos = 0
self.contact = contact
self.init_ui()
@ -25,22 +26,8 @@ class ChatInfo(QWidget):
self.vBoxLayout.addLayout(self.hBoxLayout)
self.chat_window = ChatWidget()
self.chat_window.scrollArea.verticalScrollBar().valueChanged.connect(self.verticalScrollBar)
self.vBoxLayout.addWidget(self.chat_window)
return
self.scrollArea = ScrollArea()
self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
scrollBar = ScrollBar()
self.scrollArea.setVerticalScrollBar(scrollBar)
self.scrollAreaWidgetContents = ScrollAreaContent()
self.scrollAreaWidgetContents.setMinimumSize(200, 400)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.vBoxLayout.addWidget(self.scrollArea)
self.scroolAreaLayout = QVBoxLayout()
self.scroolAreaLayout.setSpacing(0)
self.scrollAreaWidgetContents.setLayout(self.scroolAreaLayout)
def show_chats(self):
self.show_chat_thread = ShowChatThread(self.contact)
@ -52,7 +39,32 @@ class ChatInfo(QWidget):
# self.spacerItem = QSpacerItem(10, 10, QSizePolicy.Minimum, QSizePolicy.Expanding)
# self.scroolAreaLayout.addItem(self.spacerItem)
self.setLayout(self.vBoxLayout)
self.chat_window.set_scroll_bar_last()
self.setScrollBarPos()
def verticalScrollBar(self, pos):
"""
滚动条到0之后自动更新聊天记录
:param pos:
:return:
"""
# print(pos)
if pos > 0:
return
self.last_pos = self.chat_window.verticalScrollBar().maximum()
print('记录当前滚动条位置:', self.last_pos)
self.show_chat_thread.start()
def setScrollBarPos(self):
"""
将滚动条位置设置为上次看到的地方
:param pos:
:return:
"""
print('上次滚动条位置', self.last_pos)
pos = self.chat_window.verticalScrollBar().maximum() - self.last_pos
print('当前滚动条位置:', pos)
self.chat_window.set_scroll_bar_value(pos)
def show_chat(self, message):
try:
@ -62,15 +74,16 @@ class ChatInfo(QWidget):
avatar = MePC().avatar if is_send else self.contact.avatar
if type_ == 1:
str_content = message[7]
str_time = message[8]
bubble_message = BubbleMessage(
str_content,
str_time + ' ' + str_content,
avatar,
type_,
is_send
)
# print(str_content)
# self.scroolAreaLayout.addWidget(bubble_message)
self.chat_window.add_message_item(bubble_message)
self.chat_window.add_message_item(bubble_message, 0)
except:
print(message)
@ -78,14 +91,19 @@ class ChatInfo(QWidget):
class ShowChatThread(QThread):
showSingal = pyqtSignal(tuple)
finishSingal = pyqtSignal(int)
msg_id = 0
# heightSingal = pyqtSignal(int)
def __init__(self, contact):
super().__init__()
self.last_message_id = 9999999
self.wxid = contact.wxid
def run(self) -> None:
messages = msg.get_message_by_num(self.wxid, 0)
messages = msg.get_message_by_num(self.wxid, self.last_message_id)
if messages:
self.last_message_id = messages[-1][0]
for message in messages:
self.showSingal.emit(message)
self.msg_id += 1
self.finishSingal.emit(1)