toxygen/src/list_items.py

236 lines
9.2 KiB
Python
Raw Normal View History

from toxcore_enums_and_consts import *
from PySide import QtGui, QtCore
import profile
from file_transfers import TOX_FILE_TRANSFER_STATE
2016-03-29 14:54:58 +02:00
from util import curr_directory, convert_time
from messages import FILE_TRANSFER_MESSAGE_STATUS
2016-03-04 18:52:52 +01:00
class MessageEdit(QtGui.QTextEdit):
2016-03-24 19:52:27 +01:00
2016-03-05 13:44:52 +01:00
def __init__(self, text, width, parent=None):
2016-03-04 18:52:52 +01:00
super(MessageEdit, self).__init__(parent)
2016-03-15 18:05:19 +01:00
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
2016-03-04 18:52:52 +01:00
self.setWordWrapMode(QtGui.QTextOption.WrapAtWordBoundaryOrAnywhere)
self.document().setTextWidth(width)
2016-03-04 18:52:52 +01:00
self.setPlainText(text)
font = QtGui.QFont()
font.setFamily("Times New Roman")
2016-03-11 12:37:45 +01:00
font.setPixelSize(14)
2016-03-04 18:52:52 +01:00
font.setBold(False)
self.setFont(font)
self.setFixedHeight(self.document().size().height())
2016-03-04 18:52:52 +01:00
self.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse | QtCore.Qt.LinksAccessibleByMouse)
2016-03-24 19:52:27 +01:00
class MessageItem(QtGui.QWidget):
"""
Message in messages list
"""
def __init__(self, text, time, user='', message_type=TOX_MESSAGE_TYPE['NORMAL'], parent=None):
2016-03-24 19:52:27 +01:00
QtGui.QWidget.__init__(self, parent)
self.name = QtGui.QLabel(self)
2016-03-15 21:54:01 +01:00
self.name.setGeometry(QtCore.QRect(0, 2, 95, 20))
self.name.setTextFormat(QtCore.Qt.PlainText)
font = QtGui.QFont()
font.setFamily("Times New Roman")
2016-03-15 21:35:15 +01:00
font.setPointSize(11)
font.setBold(True)
self.name.setFont(font)
self.name.setObjectName("name")
2016-03-22 10:50:18 +01:00
self.name.setText(user if len(user) <= 14 else user[:11] + '...')
self.time = QtGui.QLabel(self)
self.time.setGeometry(QtCore.QRect(parent.width() - 50, 0, 50, 25))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(False)
self.time.setFont(font)
self.time.setObjectName("time")
self.time.setText(time)
self.message = MessageEdit(text, parent.width() - 150, self)
self.message.setGeometry(QtCore.QRect(100, 0, parent.width() - 150, self.message.height()))
self.setFixedHeight(self.message.height())
if message_type == TOX_MESSAGE_TYPE['ACTION']:
self.name.setStyleSheet("QLabel { color: #4169E1; }")
2016-03-29 14:54:58 +02:00
self.message.setStyleSheet("QTextEdit { color: #4169E1; }")
2016-03-04 18:52:52 +01:00
else:
if text[0] == '>':
2016-03-29 16:11:30 +02:00
self.message.setStyleSheet("QTextEdit { color: green; }")
2016-03-04 18:52:52 +01:00
if text[-1] == '<':
2016-03-29 16:11:30 +02:00
self.message.setStyleSheet("QTextEdit { color: red; }")
2016-03-04 18:52:52 +01:00
2016-03-24 19:52:27 +01:00
class ContactItem(QtGui.QWidget):
"""
Contact in friends list
"""
def __init__(self, parent=None):
2016-03-24 19:52:27 +01:00
QtGui.QWidget.__init__(self, parent)
self.setBaseSize(QtCore.QSize(250, 70))
self.avatar_label = QtGui.QLabel(self)
self.avatar_label.setGeometry(QtCore.QRect(3, 3, 64, 64))
self.avatar_label.setScaledContents(True)
self.name = QtGui.QLabel(self)
self.name.setGeometry(QtCore.QRect(70, 10, 170, 25))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(12)
font.setBold(True)
self.name.setFont(font)
self.name.setObjectName("name")
self.status_message = QtGui.QLabel(self)
self.status_message.setGeometry(QtCore.QRect(70, 30, 180, 20))
font.setPointSize(10)
font.setBold(False)
self.status_message.setFont(font)
self.status_message.setObjectName("status_message")
self.connection_status = StatusCircle(self)
self.connection_status.setGeometry(QtCore.QRect(218, 5, 32, 32))
self.connection_status.setObjectName("connection_status")
class StatusCircle(QtGui.QWidget):
"""
Connection status
"""
def __init__(self, parent):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(0, 0, 32, 32)
self.data = None
self.messages = False
def paintEvent(self, event):
paint = QtGui.QPainter()
paint.begin(self)
paint.setRenderHint(QtGui.QPainter.Antialiasing)
k = 16
rad_x = rad_y = 5
if self.data is None:
color = QtCore.Qt.transparent
else:
if self.data == TOX_USER_STATUS['NONE']:
color = QtGui.QColor(50, 205, 50)
elif self.data == TOX_USER_STATUS['AWAY']:
color = QtGui.QColor(255, 200, 50)
else: # self.data == TOX_USER_STATUS['BUSY']:
color = QtGui.QColor(255, 50, 0)
paint.setPen(color)
center = QtCore.QPoint(k, k)
paint.setBrush(color)
paint.drawEllipse(center, rad_x, rad_y)
if self.messages:
if color == QtCore.Qt.transparent:
color = QtCore.Qt.darkRed
paint.setBrush(QtCore.Qt.transparent)
paint.setPen(color)
paint.drawEllipse(center, rad_x + 3, rad_y + 3)
paint.end()
2016-03-18 22:28:53 +01:00
2016-03-28 23:07:42 +02:00
class FileTransferItem(QtGui.QListWidget):
2016-03-24 19:52:27 +01:00
2016-03-29 14:54:58 +02:00
def __init__(self, file_name, size, time, user, friend_number, file_number, state, parent=None):
2016-03-28 23:07:42 +02:00
QtGui.QListWidget.__init__(self, parent)
self.resize(QtCore.QSize(600, 50))
2016-03-29 14:54:58 +02:00
if state != FILE_TRANSFER_MESSAGE_STATUS['CANCELLED']:
self.setStyleSheet('QWidget { background-color: green; }')
else:
self.setStyleSheet('QWidget { background-color: #B40404; }')
2016-03-18 22:28:53 +01:00
self.name = QtGui.QLabel(self)
2016-03-29 14:54:58 +02:00
self.name.setGeometry(QtCore.QRect(1, 15, 95, 20))
2016-03-18 22:28:53 +01:00
self.name.setTextFormat(QtCore.Qt.PlainText)
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(11)
font.setBold(True)
self.name.setFont(font)
self.name.setObjectName("name")
2016-03-22 10:55:49 +01:00
self.name.setText(user if len(user) <= 14 else user[:11] + '...')
self.name.setStyleSheet('QLabel { color: black; }')
2016-03-18 22:28:53 +01:00
self.time = QtGui.QLabel(self)
2016-03-29 14:54:58 +02:00
self.time.setGeometry(QtCore.QRect(550, 2, 50, 46))
2016-03-18 22:28:53 +01:00
font.setPointSize(10)
font.setBold(False)
self.time.setFont(font)
self.time.setObjectName("time")
2016-03-29 14:54:58 +02:00
self.time.setText(convert_time(time))
self.time.setStyleSheet('QLabel { color: black; }')
2016-03-18 22:28:53 +01:00
self.cancel = QtGui.QPushButton(self)
2016-03-29 14:54:58 +02:00
self.cancel.setGeometry(QtCore.QRect(500, 2, 46, 46))
2016-03-21 15:37:56 +01:00
pixmap = QtGui.QPixmap(curr_directory() + '/images/decline.png')
icon = QtGui.QIcon(pixmap)
self.cancel.setIcon(icon)
2016-03-22 22:21:14 +01:00
self.cancel.setIconSize(QtCore.QSize(30, 30))
2016-03-29 14:54:58 +02:00
self.cancel.setVisible(state > 1)
self.cancel.clicked.connect(lambda: self.cancel_transfer(friend_number, file_number))
2016-03-18 22:28:53 +01:00
self.accept = QtGui.QPushButton(self)
2016-03-29 14:54:58 +02:00
self.accept.setGeometry(QtCore.QRect(450, 2, 46, 46))
2016-03-21 15:37:56 +01:00
pixmap = QtGui.QPixmap(curr_directory() + '/images/accept.png')
icon = QtGui.QIcon(pixmap)
self.accept.setIcon(icon)
2016-03-22 22:21:14 +01:00
self.accept.setIconSize(QtCore.QSize(30, 30))
2016-03-21 19:53:02 +01:00
self.accept.clicked.connect(lambda: self.accept_transfer(friend_number, file_number, size))
2016-03-29 14:54:58 +02:00
self.accept.setVisible(state == FILE_TRANSFER_MESSAGE_STATUS['INCOMING_NOT_STARTED'])
self.pb = QtGui.QProgressBar(self)
self.pb.setGeometry(QtCore.QRect(100, 15, 100, 20))
self.pb.setValue(0)
2016-03-29 14:54:58 +02:00
if state < 2:
self.pb.setVisible(False)
2016-03-19 12:41:01 +01:00
self.file_name = QtGui.QLabel(self)
2016-03-29 14:54:58 +02:00
self.file_name.setGeometry(QtCore.QRect(210, 2, 230, 46))
font.setPointSize(12)
self.file_name.setFont(font)
self.file_name.setObjectName("time")
file_size = size / 1024
if not file_size:
2016-03-25 14:45:27 +01:00
file_size = '{}B'.format(size)
elif file_size >= 1024:
file_size = '{}MB'.format(file_size / 1024)
else:
file_size = '{}KB'.format(file_size)
2016-03-22 10:55:49 +01:00
file_data = u'{} {}'.format(file_size, file_name)
self.file_name.setText(file_data if len(file_data) <= 27 else file_data[:24] + '...')
self.file_name.setStyleSheet('QLabel { color: black; }')
self.saved_name = file_name
def cancel_transfer(self, friend_number, file_number):
pr = profile.Profile.get_instance()
pr.cancel_transfer(friend_number, file_number)
2016-03-22 19:00:42 +01:00
self.setStyleSheet('QListWidget { background-color: #B40404; }')
self.cancel.setVisible(False)
self.accept.setVisible(False)
self.pb.setVisible(False)
2016-03-21 19:53:02 +01:00
def accept_transfer(self, friend_number, file_number, size):
directory = QtGui.QFileDialog.getExistingDirectory()
if directory:
pr = profile.Profile.get_instance()
2016-03-21 19:53:02 +01:00
pr.accept_transfer(self, directory + '/' + self.saved_name, friend_number, file_number, size)
self.accept.setVisible(False)
2016-03-21 18:19:13 +01:00
@QtCore.Slot(int, float)
def update(self, state, progress):
self.pb.setValue(int(progress * 100))
if state == TOX_FILE_TRANSFER_STATE['CANCELED']:
2016-03-22 19:00:42 +01:00
self.setStyleSheet('QListWidget { background-color: #B40404; }')
self.cancel.setVisible(False)
self.accept.setVisible(False)
self.pb.setVisible(False)
2016-03-21 18:19:13 +01:00
elif state == TOX_FILE_TRANSFER_STATE['FINISHED']:
self.pb.setVisible(False)
self.cancel.setVisible(False)