def __init__(self, parent=None): super().__init__(parent) self.myInfo = UserInfo(0) # 自己的用户信息 self.friendInfo = UserInfo(1) # 群信息 self.initUI() # UI设置
def __init__(self, parent=None): super().__init__(parent) self.myInfo = UserInfo(0) # 自己的用户信息 self.friendInfo = UserInfo(0) # 好友信息 self.my_host = "" self.friend_host = "" self.server = TcpS() self.server.sendFileName[str].connect(self.getFileName) self.initUI() # UI设置
def __init__(self): super().__init__() self.myInfo = UserInfo(0) # 自己的用户信息 self.host = "" # 自己的ip地址 self.chatFrameList = [] # 已经启动显示的聊天面板 self.myInfoGui = None # 界面基本设置 self.setAttribute(Qt.WA_DeleteOnClose, True) self.setAttribute(Qt.WA_QuitOnClose, True) # 设置标题和大小 self.setWindowTitle(self.tr("i Chat")) # 自适应屏幕大小 screen = QApplication.desktop().availableGeometry() self.resize(screen.width() / 5 * 3, screen.height() / 3 * 2) self.setMinimumSize(self.size()) # 移动到屏幕中央 frame = self.frameGeometry() frame.moveCenter(QApplication.desktop().availableGeometry().center()) self.move(frame.topLeft()) # UI设置 self.initUI()
def __init__(self, parent=None): super().__init__(parent) self.setStyleSheet( 'border:10px groove white;border-radius:10px;padding:0px 0px') self.myInfo = UserInfo(0) # 自己的用户信息 self.addFriendGui = None # 添加好友的界面 self.setFriendNameGui = None # 修改备注的界面 self.createGroupGui = None # 创建群的界面 self.addGroupGui = None # 加入群的界面 self.friendInfoGui = None # 查看好友详情界面 font = QFont("微软雅黑", 12, 10) # 添加好友动作 self.addFriendAction = QAction(self) self.addFriendAction.setText(self.tr("添加好友")) self.addFriendAction.setFont(font) self.addFriendAction.triggered.connect(self.addFriendActionTriggered) # 删除好友动作 self.delFriendAction = QAction(self) self.delFriendAction.setText(self.tr("删除好友")) self.delFriendAction.setFont(font) self.delFriendAction.triggered.connect(self.delFriendActionTriggered) # 修改备注操作 self.setFriendNameAction = QAction(self) self.setFriendNameAction.setText(self.tr("修改好友备注")) self.setFriendNameAction.setFont(font) self.setFriendNameAction.triggered.connect( self.setFriendNameActionTriggered) # 创建群操作 self.createGroupAction = QAction(self) self.createGroupAction.setText(self.tr("创建群")) self.createGroupAction.setFont(font) self.createGroupAction.triggered.connect( self.createGroupActionTriggered) # 解散群操作 self.delGroupAction = QAction(self) self.delGroupAction.setText(self.tr("解散该群")) self.delGroupAction.setFont(font) self.delGroupAction.triggered.connect(self.delGroupActionTriggered) # 加群操作 self.addGroupAction = QAction(self) self.addGroupAction.setText(self.tr("加入群")) self.addGroupAction.setFont(font) self.addGroupAction.triggered.connect(self.addGroupActionTriggered) # 退群操作 self.quitGroupAction = QAction(self) self.quitGroupAction.setText(self.tr("退出此群")) self.quitGroupAction.setFont(font) self.quitGroupAction.triggered.connect(self.quitGroupActionTriggered) # 查看好友信息操作 self.seeFriendInfoAction = QAction(self) self.seeFriendInfoAction.setText(self.tr("查看该用户详情")) self.seeFriendInfoAction.setFont(font) self.seeFriendInfoAction.triggered.connect( self.seeFriendInfoActionTriggered) self.setFont(QFont("微软雅黑", 16)) # UI设置 self.itemClicked.connect( self.friendItemClicked) # 把自己的itemClicked信号发送到外面去
def init(self): self.myInfo = UserInfo(0) # 自己的用户信息 # 这个信号是用来在调用了对外的添加信号和信号槽的函数之后, # 把传进来的gui参数传给真正用来连接信号和信号槽的函数的, # 此举旨在让这个连接动作也在另一个线程 self.addSignalSlotForClassSignal.connect( self.addSignalSlotForClassSlot) # 和服务器的socket连接 self.connectToServer = TcpSocket() self.connectToServer.connectToHost(self.srv_host, self.srv_port) self.is_connected = self.connectToServer.waitForConnected( 3000) # 判断连接服务器是否成功 self.connectToServer.getMsgSignal.connect(self.getMsgFromServer)
def seeFriendInfoActionTriggered(self): # 获取当前选中好友项 selectedItemList = self.selectedItems() # 如果有的话 if len(selectedItemList) != 0: friend_id = selectedItemList[0].data(Qt.UserRole + FriendInfoIndex.id) friend_name = selectedItemList[0].data(Qt.UserRole + FriendInfoIndex.name) friend_nick_name = selectedItemList[0].data( Qt.UserRole + FriendInfoIndex.nick_name) # friend_phone = selectedItemList[0].data(Qt.UserRole + FriendInfoIndex.phone) friend_mail = selectedItemList[0].data(Qt.UserRole + FriendInfoIndex.mail) friend_gender = selectedItemList[0].data(Qt.UserRole + FriendInfoIndex.gender) friend_age = selectedItemList[0].data(Qt.UserRole + FriendInfoIndex.age) friend_birthday = selectedItemList[0].data( Qt.UserRole + FriendInfoIndex.birthday) friend_vocation = selectedItemList[0].data( Qt.UserRole + FriendInfoIndex.vocation) friend_signature = selectedItemList[0].data( Qt.UserRole + FriendInfoIndex.signature) friend_register_time = selectedItemList[0].data( Qt.UserRole + FriendInfoIndex.register_time) friend_mark_name = selectedItemList[0].data( Qt.UserRole + FriendInfoIndex.mark_name) friend_info = UserInfo(0, uid=friend_id, name=friend_name, mark_name=friend_mark_name, nick_name=friend_nick_name, mail=friend_mail, gender=friend_gender, age=friend_age, birthday=friend_birthday, vocation=friend_vocation, signature=friend_signature, register_time=friend_register_time) if self.friendInfoGui is None: self.friendInfoGui = FriendInfoGui(friend_info) self.friendInfoGui.closeSignal.connect( self.closeFriendInfoGuiSlot) # 关闭好友信息界面 self.friendInfoGui.show()
def __init__(self, my_info=UserInfo(0), parent=None): super().__init__(parent) self.myInfo = my_info # 自己的用户信息 # 界面基本设置 self.setAttribute(Qt.WA_DeleteOnClose, True) self.setAttribute(Qt.WA_QuitOnClose, False) # 设置标题 self.setWindowTitle(self.tr("个人信息")) self.setFixedSize(743, 579) self.initUI() # UI设置 # 移动到屏幕中央 self.resize(self.sizeHint()) rect = self.frameGeometry() rect.moveCenter(QApplication.desktop().availableGeometry().center()) self.move(rect.topLeft())
def getMsgFromServer(self, msg): print(msg) # 输出调试信息 # 获取消息列表 msgList = msg.split(' ') # 登陆成功 if msgList[0] == "LoginSuccess": self.loginSuccessSignal.emit() # 登陆失败 elif msgList[0] == "LoginFailed": self.loginFailedSignal.emit() # 重复登陆 elif msgList[0] == "LoginRepeat": self.loginRepeatSignal.emit() # 注册成功 elif msgList[0] == "RegisterSuccess": self.registerSuccessSignal.emit(msgList[1]) # 注册失败 elif msgList[0] == "RegisterFail": self.registerSuccessSignal.emit(None) # 自己的用户信息 elif msgList[0] == "MyInfo": # 获取自己的用户信息 self.myInfo.id = msgList[1] self.host = msgList[2] my_info_dict = eval(' '.join(msgList[3:])) self.myInfo.name = my_info_dict['name'] self.myInfo.nick_name = my_info_dict['nick_name'] self.myInfo.age = my_info_dict['age'] self.myInfo.birthday = my_info_dict['birthday'] self.myInfo.gender = my_info_dict['gender_id'] self.myInfo.mail = my_info_dict['email'] self.myInfo.phone = my_info_dict['telephone'] self.myInfo.register_time = my_info_dict['register_time'] self.myInfo.signature = my_info_dict['signature'] self.myInfo.vocation = my_info_dict['vocation'] self.getMyInfoSignal.emit(self.myInfo) # 传递自己的用户信息 self.getMyHostSignal.emit(self.host) # 传递自己的ip地址 # 获取聊天消息 elif msgList[0] == "Message": friendID = msgList[1] # 获取发送方,即好友id # 弹出包头及无关信息 msgList.reverse() msgList.pop() msgList.pop() msgList.pop() msgList.reverse() self.getMsgSignal.emit([friendID, " ".join(msgList)]) # 传递聊天消息 # 获取群消息 elif msgList[0] == "GroupMessage": sender_name = msgList[1] # 获取发送方用户名 group_name = msgList[2] # 获取群名 # 弹出包头及无关信息 msgList.reverse() msgList.pop() msgList.pop() msgList.pop() msgList.pop() msgList.reverse() self.getGroupMsgSignal.emit( [group_name, sender_name, " ".join(msgList)]) # 传递聊天消息 # 添加好友请求返回没有这个用户 elif msgList[0] == "NoThisUser": self.noThisUserSignal.emit(msgList[1]) # 修改个人信息是否成功 elif msgList[0] == "UpdateMyInfo": self.updateMyInfoSignal.emit(msgList[1]) # 获得好友ip地址 elif msgList[0] == "GetFriendHost": self.getFriendHostSignal.emit(msgList[1]) elif msgList[0] == "RecvFile": self.recvFileSignal.emit(msgList) elif msgList[0] == "RefuseRecvFile": self.refuseRecvFileSignal.emit(msgList) # 好友列表 else: # msgList[0] == "FriendList": msgList = msg.split('~~~~~~') friendInfoList = [] # 好友及群列表 # 整理好友列表的id和用户名好人备注封装成用户信息对象做成一个列表加到要发送的列表中,并且发送 for friendInfo in msgList: if friendInfo != "FriendList": friendInfoStrList = friendInfo.split('~`~') if friendInfoStrList[0] == "Group": friendInfoList.append( UserInfo(1, friendInfoStrList[1], friendInfoStrList[2], friendInfoStrList[3])) else: friend_info_dict = eval(friendInfoStrList[3]) friendInfoList.append( UserInfo(0, uid=friendInfoStrList[0], name=friendInfoStrList[1], nick_name=friend_info_dict['nick_name'], mail=friend_info_dict['email'], signature=friend_info_dict['signature'], gender=friend_info_dict['gender_id'], birthday=friend_info_dict['birthday'], vocation=friend_info_dict['vocation'], register_time=friend_info_dict[ 'register_time'], age=friend_info_dict['age'], mark_name=friendInfoStrList[2])) self.getFriendListSignal.emit(friendInfoList)
def friendListWidgetItemClicked(self, item): if item.data(Qt.UserRole + FriendInfoIndex.obj_type) == 0: # 如果是好友 # 判定是否相应的聊天面板是否已经打开 for chatFrame in self.chatFrameList: # 如果打开了 if chatFrame.friendInfo.id == item.data(Qt.UserRole + FriendInfoIndex.id): self.getFriendHostSignal.connect( chatFrame.getFriendHostSlot) # 获取好友ip地址 self.getFriendHostRequestSignal.emit( item.data(Qt.UserRole + FriendInfoIndex.id)) self.refuseRecvFileSignal.connect( chatFrame.refuseRecvFileSlot) chatFrame.raise_() # 放到最前面 return # 如果没有打开 # 打开 chatFrame = ChatFrame(self.widget) chatFrame.sendMsgSignal.connect(self.sendMsgSignal) # 发送聊天消息 chatFrame.sendFileRequestSignal.connect( self.sendFileRequestSignal) # 发送文件请求 self.getMsgSignal.connect(chatFrame.getMsgSlot) # 获取聊天消息 self.getFriendHostSignal.connect( chatFrame.getFriendHostSlot) # 获取好友ip地址 self.refuseRecvFileSignal.connect(chatFrame.refuseRecvFileSlot) # 更新聊天界面的自己的用户信息和该好友的用户信息 chatFrame.myInfo = self.myInfo chatFrame.my_host = self.host chatFrame.friendInfo = UserInfo( 0, uid=item.data(Qt.UserRole + FriendInfoIndex.id), name=item.data(Qt.UserRole + FriendInfoIndex.name), mark_name=item.data(Qt.UserRole + FriendInfoIndex.mark_name)) self.getFriendHostRequestSignal.emit( item.data(Qt.UserRole + FriendInfoIndex.id)) chatFrame.resize(self.widget.size()) f_msg_filename = './cache/' + '-'.join([ 'f', self.myInfo.id, item.data(Qt.UserRole + FriendInfoIndex.id) ]) + '.txt' try: f_file = open(f_msg_filename, 'r', encoding="utf8") chatFrame.recvTextEdit.append(''.join(f_file.readlines())) except OSError as err: print(err) f_file = open(f_msg_filename, 'x', encoding="utf8") f_file.close() else: f_file.close() chatFrame.show() self.chatFrameList.append(chatFrame) # 加入聊天面板列表 elif item.data(Qt.UserRole + FriendInfoIndex.obj_type) == 1: # 如果是群 # 判定是否相应的聊天面板是否已经打开 for chatFrame in self.chatFrameList: # 如果打开了 if chatFrame.friendInfo.nick_name == item.data( Qt.UserRole + FriendInfoIndex.nick_name): chatFrame.raise_() # 放到最前面 return # 如果没有打开 # 打开 chatFrame = GroupFrame(self.widget) chatFrame.sendGroupMsgSignal.connect( self.sendGroupMsgSignal) # 发送聊天消息 self.getGroupMsgSignal.connect(chatFrame.getGroupMsgSlot) # 获取聊天消息 # 更新聊天界面的自己的用户信息和该好友的用户信息 chatFrame.myInfo = self.myInfo chatFrame.friendInfo = UserInfo( 1, item.data(Qt.UserRole + FriendInfoIndex.id), item.data(Qt.UserRole + FriendInfoIndex.name), item.data(Qt.UserRole + FriendInfoIndex.nick_name)) chatFrame.resize(self.widget.size()) g_msg_filename = './cache/' + '-'.join([ 'g', self.myInfo.id, item.data(Qt.UserRole + FriendInfoIndex.nick_name) ]) + '.txt' try: g_file = open(g_msg_filename, 'r', encoding="utf8") chatFrame.recvTextEdit.append(''.join(g_file.readlines())) except OSError as err: print(err) g_file = open(g_msg_filename, 'x', encoding="utf8") g_file.close() else: g_file.close() chatFrame.show() self.chatFrameList.append(chatFrame) # 加入聊天面板列表