message edit rewritten. history bug fixed. ui improvements

This commit is contained in:
ingvar1995 2016-03-25 16:24:38 +03:00
parent 50825c238c
commit 1d5d9d6e47
5 changed files with 13 additions and 29 deletions

View File

@ -40,7 +40,7 @@ class History(object):
' id INTEGER PRIMARY KEY,' ' id INTEGER PRIMARY KEY,'
' message TEXT,' ' message TEXT,'
' owner INTEGER,' ' owner INTEGER,'
' unix_time INTEGER,' ' unix_time REAL,'
' message_type INTEGER' ' message_type INTEGER'
')') ')')
db.commit() db.commit()

View File

@ -5,33 +5,21 @@ from file_transfers import TOX_FILE_TRANSFER_STATE
from util import curr_directory from util import curr_directory
class MessageEdit(QtGui.QPlainTextEdit): class MessageEdit(QtGui.QTextEdit):
def __init__(self, text, width, parent=None): def __init__(self, text, width, parent=None):
super(MessageEdit, self).__init__(parent) super(MessageEdit, self).__init__(parent)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setWordWrapMode(QtGui.QTextOption.WrapAtWordBoundaryOrAnywhere) self.setWordWrapMode(QtGui.QTextOption.WrapAtWordBoundaryOrAnywhere)
self.document().setTextWidth(width)
self.setPlainText(text) self.setPlainText(text)
self.document().setTextWidth(parent.width() - 100)
font = QtGui.QFont() font = QtGui.QFont()
font.setFamily("Times New Roman") font.setFamily("Times New Roman")
font.setPixelSize(14) font.setPixelSize(14)
font.setBold(False) font.setBold(False)
self.setFont(font) self.setFont(font)
lines = 0 self.setFixedHeight(self.document().size().height())
fm = QtGui.QFontMetrics(font)
try:
for elem in xrange(self.document().blockCount()):
block = self.document().findBlockByLineNumber(elem)
line_width = fm.width(block.text())
print 'Width: ', line_width
lines += line_width / float(width) + 1
except:
print 'updateSize failed'
print 'lines ', lines
size = int(lines + 0.5) * 21
self.setFixedHeight(max(size, 25))
self.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse | QtCore.Qt.LinksAccessibleByMouse) self.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse | QtCore.Qt.LinksAccessibleByMouse)
@ -64,9 +52,7 @@ class MessageItem(QtGui.QWidget):
self.message = MessageEdit(text, parent.width() - 150, self) self.message = MessageEdit(text, parent.width() - 150, self)
self.message.setGeometry(QtCore.QRect(100, 0, parent.width() - 150, self.message.height())) self.message.setGeometry(QtCore.QRect(100, 0, parent.width() - 150, self.message.height()))
self.h = self.message.height() self.setFixedHeight(self.message.height())
print 'self.h ', self.h
self.setFixedHeight(self.getHeight())
if message_type == TOX_MESSAGE_TYPE['ACTION']: if message_type == TOX_MESSAGE_TYPE['ACTION']:
self.name.setStyleSheet("QLabel { color: #4169E1; }") self.name.setStyleSheet("QLabel { color: #4169E1; }")
@ -77,9 +63,6 @@ class MessageItem(QtGui.QWidget):
if text[-1] == '<': if text[-1] == '<':
self.message.setStyleSheet("QPlainTextEdit { color: red; }") self.message.setStyleSheet("QPlainTextEdit { color: red; }")
def getHeight(self):
return max(self.h, 25)
class ContactItem(QtGui.QWidget): class ContactItem(QtGui.QWidget):
""" """
@ -205,7 +188,7 @@ class FileTransferItem(QtGui.QWidget):
self.file_name.setObjectName("time") self.file_name.setObjectName("time")
file_size = size / 1024 file_size = size / 1024
if not file_size: if not file_size:
file_size = '<1KB' file_size = '<{}B'.format(size)
elif file_size >= 1024: elif file_size >= 1024:
file_size = '{}MB'.format(file_size / 1024) file_size = '{}MB'.format(file_size / 1024)
else: else:

View File

@ -211,6 +211,7 @@ class MainWindow(QtGui.QMainWindow):
def setup_right_center(self, widget): def setup_right_center(self, widget):
self.messages = QtGui.QListWidget(widget) self.messages = QtGui.QListWidget(widget)
self.messages.setGeometry(0, 0, 620, 250) self.messages.setGeometry(0, 0, 620, 250)
self.messages.setObjectName("messages")
def load(pos): def load(pos):
if not pos: if not pos:

View File

@ -219,7 +219,6 @@ class Friend(Contact):
:param first_time: friend became active, load first part of messages :param first_time: friend became active, load first part of messages
:return: list of loaded messages :return: list of loaded messages
""" """
# TODO: fix bug with message order
if (first_time and self._history_loaded) or (not hasattr(self, '_message_getter')): if (first_time and self._history_loaded) or (not hasattr(self, '_message_getter')):
return return
data = self._message_getter.get(42) data = self._message_getter.get(42)
@ -510,7 +509,7 @@ class Profile(Contact, Singleton):
self._messages.scrollToBottom() self._messages.scrollToBottom()
self._friends[self._active_friend].append_message((message.decode('utf-8'), self._friends[self._active_friend].append_message((message.decode('utf-8'),
MESSAGE_OWNER['FRIEND'], MESSAGE_OWNER['FRIEND'],
int(time.time()), time.time(),
message_type)) message_type))
else: else:
friend = filter(lambda x: x.number == friend_num, self._friends)[0] friend = filter(lambda x: x.number == friend_num, self._friends)[0]
@ -538,7 +537,7 @@ class Profile(Contact, Singleton):
self._messages.scrollToBottom() self._messages.scrollToBottom()
friend.append_message((text, friend.append_message((text,
MESSAGE_OWNER['ME'], MESSAGE_OWNER['ME'],
int(time.time()), time.time(),
message_type)) message_type))
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
@ -580,6 +579,7 @@ class Profile(Contact, Singleton):
data = friend.load_corr(False) data = friend.load_corr(False)
if not data: if not data:
return return
data.reverse()
for message in data: for message in data:
self.create_message_item(message[0], self.create_message_item(message[0],
convert_time(message[2]), convert_time(message[2]),
@ -609,7 +609,7 @@ class Profile(Contact, Singleton):
def create_message_item(self, text, time, name, message_type, append=True): def create_message_item(self, text, time, name, message_type, append=True):
item = MessageItem(text, time, name, message_type, self._messages) item = MessageItem(text, time, name, message_type, self._messages)
elem = QtGui.QListWidgetItem() elem = QtGui.QListWidgetItem()
elem.setSizeHint(QtCore.QSize(600, item.getHeight())) elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height()))
if append: if append:
self._messages.addItem(elem) self._messages.addItem(elem)
else: else:

View File

@ -1252,9 +1252,9 @@ QPushButton:hover
background-color: #4A4949; background-color: #4A4949;
} }
QListWidget QListWidgetItem:selected #messages:item:selected
{ {
background-color: #4A4949; background-color: transparent;
} }
#friends_list:item:selected #friends_list:item:selected