From 7282e205400daf8bbcd045f2ec0e7b9017614b5b Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Fri, 4 Mar 2016 20:52:52 +0300 Subject: [PATCH] ui update: messages #1 --- src/callbacks.py | 4 ++-- src/list_items.py | 51 +++++++++++++++++++++++++++++++++++++++++++---- src/profile.py | 14 +++++++------ 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/callbacks.py b/src/callbacks.py index ee89b62..371dea7 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -50,7 +50,7 @@ def friend_status(tox, friend_num, new_status, user_data): """ Check friend's status (none, busy, away) """ - print "Friend's #{} status changed! New status: ".format(friend_num, new_status) + print "Friend's #{} status changed! New status: {}".format(friend_num, new_status) profile = Profile.get_instance() friend = profile.get_friend_by_number(friend_num) invoke_in_main_thread(friend.set_status, new_status) @@ -60,7 +60,7 @@ def friend_connection_status(tox, friend_num, new_status, user_data): """ Check friend's connection status (offline, udp, tcp) """ - print "Friend #{} connected! Friend's status: ".format(friend_num, new_status) + print "Friend #{} connected! Friend's status: {}".format(friend_num, new_status) profile = Profile.get_instance() friend = profile.get_friend_by_number(friend_num) if new_status == TOX_CONNECTION['NONE']: diff --git a/src/list_items.py b/src/list_items.py index a5ee49e..f059be5 100644 --- a/src/list_items.py +++ b/src/list_items.py @@ -1,5 +1,34 @@ from toxcore_enums_and_consts import * from PySide import QtGui, QtCore +# TODO: remove some hardcoded values + + +class MessageEdit(QtGui.QPlainTextEdit): + def __init__(self, text='', parent=None): + super(MessageEdit, self).__init__(parent) + + self.setWordWrapMode(QtGui.QTextOption.WrapAtWordBoundaryOrAnywhere) + self.setPlainText(text) + self.document().setTextWidth(parent.width() - 100) + font = QtGui.QFont() + font.setFamily("Times New Roman") + font.setPointSize(10) + font.setBold(False) + + self.setFont(font) + lines = prev = 0 + try: + for elem in xrange(self.document().blockCount()): + pos = self.document().findBlockByLineNumber(elem).position() + lines += (pos - prev) // 55 + 1 + prev = pos + except: + print 'updateSize failed' + print 'lines ', lines + self.setFixedHeight(lines * 17) + self.setMinimumHeight(lines * 17) + self.setMaximumHeight(lines * 17) + self.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse | QtCore.Qt.LinksAccessibleByMouse) class MessageItem(QtGui.QListWidget): @@ -29,14 +58,28 @@ class MessageItem(QtGui.QListWidget): self.time.setObjectName("time") self.time.setText(time) - self.message = QtGui.QPlainTextEdit(self) - self.message.setGeometry(QtCore.QRect(50, 0, 400, 50)) - self.message.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse | QtCore.Qt.LinksAccessibleByMouse) - self.message.setPlainText(text) + self.message = MessageEdit(text, self) + print parent.width() + print self.message.height() + self.message.setGeometry(QtCore.QRect(50, 0, parent.width() - 100, self.message.height())) + self.h = self.message.height() + self.message.setFrameShape(QtGui.QFrame.NoFrame) + self.time.setFrameShape(QtGui.QFrame.NoFrame) + self.name.setFrameShape(QtGui.QFrame.NoFrame) + self.setFrameShape(QtGui.QFrame.NoFrame) if message_type == TOX_MESSAGE_TYPE['ACTION']: self.name.setStyleSheet("QLabel { color: blue; }") self.message.setStyleSheet("QPlainTextEdit { color: blue; }") + else: + if text[0] == '>': + self.message.setStyleSheet("QPlainTextEdit { color: green; }") + if text[-1] == '<': + self.message.setStyleSheet("QPlainTextEdit { color: red; }") + + def getHeight(self): + return max(self.h, 30) + class ContactItem(QtGui.QListWidget): diff --git a/src/profile.py b/src/profile.py index 2d24996..3d550c1 100644 --- a/src/profile.py +++ b/src/profile.py @@ -225,9 +225,10 @@ class Profile(Contact): def new_message(self, id, message_type, message): if id == self._active_friend: # add message to list user_name = Profile.get_instance().get_active_name() - item = MessageItem(message.decode('utf-8'), curr_time(), user_name, message_type) + item = MessageItem(message.decode('utf-8'), curr_time(), user_name, message_type, self._messages) elem = QtGui.QListWidgetItem(self._messages) - elem.setSizeHint(QtCore.QSize(500, 100)) + print 'item height', item.height() + elem.setSizeHint(QtCore.QSize(500, item.getHeight())) self._messages.addItem(elem) self._messages.setItemWidget(elem, item) self._messages.scrollToBottom() @@ -238,15 +239,16 @@ class Profile(Contact): def send_message(self, text): if self.is_active_online() and text: - if text.startswith('/me'): + if text.startswith('/me '): message_type = TOX_MESSAGE_TYPE['ACTION'] - text = text[3:] + text = text[4:] else: message_type = TOX_MESSAGE_TYPE['NORMAL'] self.tox.friend_send_message(self._active_friend, message_type, text.encode('utf-8')) - item = MessageItem(text, curr_time(), self._name, message_type) + item = MessageItem(text, curr_time(), self._name, message_type, self._messages) elem = QtGui.QListWidgetItem(self._messages) - elem.setSizeHint(QtCore.QSize(500, 100)) + print 'item height', item.height() + elem.setSizeHint(QtCore.QSize(500, item.getHeight())) self._messages.addItem(elem) self._messages.setItemWidget(elem, item) self._messages.scrollToBottom()