profile + callbacks update. status now supported

This commit is contained in:
ingvar1995 2016-02-29 18:40:49 +03:00
parent 2441d2b690
commit 79cae779e7
3 changed files with 75 additions and 61 deletions

View file

@ -37,7 +37,12 @@ def self_connection_status(st, tox_link):
"""
def wrapped(tox, connection, user_data):
print 'Connection status: ', str(connection)
invoke_in_main_thread(st.repaint)
profile = Profile.get_instance()
if profile.status is None:
status = tox_link.self_get_status()
invoke_in_main_thread(profile.set_status, status)
elif connection == TOX_CONNECTION['NONE']:
invoke_in_main_thread(profile.set_status, None)
return wrapped
@ -47,7 +52,7 @@ def friend_status(tox, friend_num, new_status, user_data):
"""
print "Friend's #{} status changed! New status: ".format(friend_num, new_status)
profile = Profile.get_instance()
friend = filter(lambda x: x.number == friend_num, profile.friends)[0]
friend = profile.get_friend_by_number(friend_num)
invoke_in_main_thread(friend.set_status, new_status)
@ -57,11 +62,11 @@ def friend_connection_status(tox, friend_num, new_status, user_data):
"""
print "Friend #{} connected! Friend's status: ".format(friend_num, new_status)
profile = Profile.get_instance()
friend = filter(lambda x: x.number == friend_num, profile.friends)[0]
friend = profile.get_friend_by_number(friend_num)
if new_status == TOX_CONNECTION['NONE']:
invoke_in_main_thread(friend.set_status, None)
elif friend.status is None:
invoke_in_main_thread(friend.set_status, TOX_USER_STATUS['NONE'])
#elif friend.status is None:
# invoke_in_main_thread(friend.set_status, TOX_USER_STATUS['NONE'])
def friend_name(window):
@ -72,7 +77,7 @@ def friend_name(window):
"""
def wrapped(tox, friend_num, name, size, user_data):
profile = Profile.get_instance()
friend = filter(lambda x: x.number == friend_num, profile.friends)[0]
friend = profile.get_friend_by_number(friend_num)
print 'New name: ', str(friend_num), str(name)
invoke_in_main_thread(friend.set_name, name)
invoke_in_main_thread(window.update_active_friend)
@ -87,7 +92,7 @@ def friend_status_message(window):
"""
def wrapped(tox, friend_num, status_message, size, user_data):
profile = Profile.get_instance()
friend = filter(lambda x: x.number == friend_num, profile.friends)[0]
friend = profile.get_friend_by_number(friend_num)
invoke_in_main_thread(friend.set_status_message, status_message)
print 'User #{} has new status: {}'.format(friend_num, status_message)
invoke_in_main_thread(window.update_active_friend)
@ -101,9 +106,10 @@ def friend_message(window):
"""
def wrapped(tox, friend_number, message_type, message, size, user_data):
print 'Message: ', message.decode('utf8')
if not window.isActiveWindow() and Settings()['notifications']:
tray_notification('Message', message.decode('utf8'))
profile = Profile.get_instance()
if not window.isActiveWindow() and Settings()['notifications']:
friend = profile.get_friend_by_number(friend_number)
tray_notification(friend.name, message.decode('utf8'))
invoke_in_main_thread(profile.new_message, friend_number, message_type, message)
return wrapped

View file

@ -66,6 +66,12 @@ class ContactItem(QtGui.QListWidget):
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(200, 5, 16, 16))
self.connection_status.setMinimumSize(QtCore.QSize(32, 32))
self.connection_status.setMaximumSize(QtCore.QSize(32, 32))
self.connection_status.setBaseSize(QtCore.QSize(32, 32))
self.connection_status.setObjectName("connection_status")
class StatusCircle(QtGui.QWidget):
@ -73,7 +79,7 @@ class StatusCircle(QtGui.QWidget):
def __init__(self, parent):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(0, 0, 32, 32)
self.tox = parent.tox
self.data = None
def mouseReleaseEvent(self, event):
pass
@ -82,20 +88,17 @@ class StatusCircle(QtGui.QWidget):
paint = QtGui.QPainter()
paint.begin(self)
paint.setRenderHint(QtGui.QPainter.Antialiasing)
#paint.setBrush(QtCore.Qt.white)
#paint.drawRect(event.rect())
k = 16
rad_x = rad_y = 10
if not self.tox.self_get_connection_status():
color = QtCore.Qt.black
rad_x = rad_y = 5
if self.data is None:
color = QtCore.Qt.transparent
else:
status = self.tox.self_get_status()
if status == TOX_USER_STATUS['NONE']:
color = QtCore.Qt.green
elif status == TOX_USER_STATUS['AWAY']:
if self.data == TOX_USER_STATUS['NONE']:
color = QtCore.Qt.darkGreen
elif self.data == TOX_USER_STATUS['AWAY']:
color = QtCore.Qt.yellow
else: # self.status == TOX_USER_STATUS['BUSY']:
color = QtCore.Qt.red
else: # self.data == TOX_USER_STATUS['BUSY']:
color = QtCore.Qt.darkRed
paint.setPen(color)
center = QtCore.QPoint(k, k)
@ -212,34 +215,34 @@ class MainWindow(QtGui.QMainWindow):
Form.setMinimumSize(QtCore.QSize(250, 100))
Form.setMaximumSize(QtCore.QSize(250, 100))
Form.setBaseSize(QtCore.QSize(250, 100))
self.graphicsView = QtGui.QGraphicsView(Form)
self.graphicsView.setGeometry(QtCore.QRect(10, 20, 64, 64))
self.graphicsView.setMinimumSize(QtCore.QSize(64, 64))
self.graphicsView.setMaximumSize(QtCore.QSize(64, 64))
self.graphicsView.setBaseSize(QtCore.QSize(64, 64))
self.graphicsView.setObjectName("graphicsView")
self.name = QtGui.QLabel(Form)
self.name.setGeometry(QtCore.QRect(80, 30, 191, 25))
Form.graphicsView = QtGui.QGraphicsView(Form)
Form.graphicsView.setGeometry(QtCore.QRect(10, 20, 64, 64))
Form.graphicsView.setMinimumSize(QtCore.QSize(64, 64))
Form.graphicsView.setMaximumSize(QtCore.QSize(64, 64))
Form.graphicsView.setBaseSize(QtCore.QSize(64, 64))
Form.graphicsView.setObjectName("graphicsView")
self.name = Form.name = QtGui.QLabel(Form)
Form.name.setGeometry(QtCore.QRect(80, 30, 191, 25))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(16)
font.setBold(True)
self.name.setFont(font)
self.name.setObjectName("name")
self.status_message = QtGui.QLabel(Form)
self.status_message.setGeometry(QtCore.QRect(80, 60, 191, 17))
Form.name.setFont(font)
Form.name.setObjectName("name")
self.status_message = Form.status_message = QtGui.QLabel(Form)
Form.status_message.setGeometry(QtCore.QRect(80, 60, 191, 17))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(14)
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(200, 34, 64, 64))
self.connection_status.setMinimumSize(QtCore.QSize(32, 32))
self.connection_status.setMaximumSize(QtCore.QSize(32, 32))
self.connection_status.setBaseSize(QtCore.QSize(32, 32))
self.connection_status.setObjectName("connection_status")
Form.status_message.setFont(font)
Form.status_message.setObjectName("status_message")
self.connection_status = Form.connection_status = StatusCircle(self)
Form.connection_status.setGeometry(QtCore.QRect(200, 34, 64, 64))
Form.connection_status.setMinimumSize(QtCore.QSize(32, 32))
Form.connection_status.setMaximumSize(QtCore.QSize(32, 32))
Form.connection_status.setBaseSize(QtCore.QSize(32, 32))
Form.connection_status.setObjectName("connection_status")
def setup_right_top(self, Form):
Form.setObjectName("Form")
@ -316,21 +319,15 @@ class MainWindow(QtGui.QMainWindow):
main.setLayout(grid)
self.setCentralWidget(main)
self.setup_menu(self)
self.setup_info_from_tox()
def mouseReleaseEvent(self, event):
# TODO: add reaction on mouse click
pass
self.profile.change_status()
# if self.connection_status.status != TOX_USER_CONNECTION_STATUS['OFFLINE']:
# self.connection_status.status += 1
# self.connection_status.status %= TOX_USER_CONNECTION_STATUS['OFFLINE']
# self.tox.self_set_status(self.connection_status.status)
# self.connection_status.repaint()
def setup_info_from_tox(self):
self.name.setText(self.profile.name)
self.status_message.setText(self.profile.status_message)
# -----------------------------------------------------------------------------------------------------------------
# Functions which called when user click in menu
# -----------------------------------------------------------------------------------------------------------------

View file

@ -86,8 +86,8 @@ class Contact(object):
return self._status
def set_status(self, value):
# TODO: status repaint
self._status = value
self._widget.connection_status.data = self._status = value
self._widget.connection_status.repaint()
status = property(get_status, set_status)
@ -142,13 +142,15 @@ class Profile(Contact):
self._status_message = tox.self_get_status_message()
self._status = None
data = tox.self_get_friend_list()
self.friends, num, self._active_friend = [], 0, -1
self._friends, num, self._active_friend = [], 0, -1
for i in data:
name = tox.friend_get_name(i) or tox.friend_get_public_key(i)
status_message = tox.friend_get_status_message(i)
self.friends.append(Friend(i, name, status_message, widgets[num]))
self._friends.append(Friend(i, name, status_message, widgets[num]))
num += 1
Profile._instance = self
self.set_name(tox.self_get_name())
self.set_status_message(tox.self_get_status_message())
@staticmethod
def get_instance():
@ -158,11 +160,17 @@ class Profile(Contact):
if self._status is not None:
self._status += 1
self._status %= 3
self.tox.self_set_status(self._status)
self._widget.connection_status.data = self._status
self._widget.connection_status.repaint()
def filtration(self, show_online=True, filter_str=''):
for friend in self.friends:
for friend in self._friends:
friend.visibility = (friend.status is not None or not show_online) and (filter_str in friend.name)
def get_friend_by_number(self, num):
return filter(lambda x: x.number == num, self._friends)[0]
# -----------------------------------------------------------------------------------------------------------------
# Work with active friend
# -----------------------------------------------------------------------------------------------------------------
@ -172,7 +180,7 @@ class Profile(Contact):
def set_active(self, value):
try:
visible_friends = filter(lambda elem: elem[1].visibility, enumerate(self.friends))
visible_friends = filter(lambda elem: elem[1].visibility, enumerate(self._friends))
self._active_friend = visible_friends[value][0]
self._messages.clear()
self._messages.repaint()
@ -183,17 +191,20 @@ class Profile(Contact):
active_friend = property(get_active, set_active)
def get_active_friend_data(self):
friend = self.friends[self._active_friend]
return friend.name, friend.status_message
if self._active_friend != -1:
friend = self._friends[self._active_friend]
return friend.name, friend.status_message
else:
return '', ''
def get_active_number(self):
return self.friends[self._active_friend].number
return self._friends[self._active_friend].number
def get_active_name(self):
return self.friends[self._active_friend].name
return self._friends[self._active_friend].name
def is_active_online(self):
return self._active_friend + 1 and self.friends[self._active_friend].status is not None
return self._active_friend + 1 and self._friends[self._active_friend].status is not None
# -----------------------------------------------------------------------------------------------------------------
# Private messages
@ -210,7 +221,7 @@ class Profile(Contact):
self._messages.scrollToBottom()
self._messages.repaint()
else:
friend = filter(lambda x: x.number == id, self.friends)[0]
friend = filter(lambda x: x.number == id, self._friends)[0]
friend.set_messages(True)
def send_message(self, text):
@ -238,7 +249,7 @@ class Profile(Contact):
def delete_friend(self, num):
self.tox.friend_delete(num)
friend = filter(lambda x: x.number == num, self.friends)[0]
friend = filter(lambda x: x.number == num, self._friends)[0]
del friend