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): def wrapped(tox, connection, user_data):
print 'Connection status: ', str(connection) 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 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) print "Friend's #{} status changed! New status: ".format(friend_num, new_status)
profile = Profile.get_instance() 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) 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) print "Friend #{} connected! Friend's status: ".format(friend_num, new_status)
profile = Profile.get_instance() 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']: if new_status == TOX_CONNECTION['NONE']:
invoke_in_main_thread(friend.set_status, None) invoke_in_main_thread(friend.set_status, None)
elif friend.status is None: #elif friend.status is None:
invoke_in_main_thread(friend.set_status, TOX_USER_STATUS['NONE']) # invoke_in_main_thread(friend.set_status, TOX_USER_STATUS['NONE'])
def friend_name(window): def friend_name(window):
@ -72,7 +77,7 @@ def friend_name(window):
""" """
def wrapped(tox, friend_num, name, size, user_data): def wrapped(tox, friend_num, name, size, user_data):
profile = Profile.get_instance() 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) print 'New name: ', str(friend_num), str(name)
invoke_in_main_thread(friend.set_name, name) invoke_in_main_thread(friend.set_name, name)
invoke_in_main_thread(window.update_active_friend) 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): def wrapped(tox, friend_num, status_message, size, user_data):
profile = Profile.get_instance() 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) invoke_in_main_thread(friend.set_status_message, status_message)
print 'User #{} has new status: {}'.format(friend_num, status_message) print 'User #{} has new status: {}'.format(friend_num, status_message)
invoke_in_main_thread(window.update_active_friend) 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): def wrapped(tox, friend_number, message_type, message, size, user_data):
print 'Message: ', message.decode('utf8') print 'Message: ', message.decode('utf8')
if not window.isActiveWindow() and Settings()['notifications']:
tray_notification('Message', message.decode('utf8'))
profile = Profile.get_instance() 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) invoke_in_main_thread(profile.new_message, friend_number, message_type, message)
return wrapped return wrapped

View File

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

View File

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