user now can read incoming messages

This commit is contained in:
ingvar1995 2016-02-26 21:54:15 +03:00
parent b592ca3f00
commit a1ff425f4c
4 changed files with 93 additions and 14 deletions

View file

@ -1,7 +1,9 @@
from PySide import QtCore
from notifications import *
from settings import Settings
# TODO: add all callbacks (replace test callbacks and use wrappers)
from profile import Profile
# TODO: add all callbacks (remove test callbacks and use wrappers)
# NOTE: don't forget to call repaint
class InvokeEvent(QtCore.QEvent):
@ -52,6 +54,8 @@ def friend_message(window):
print 'Message: ', message.decode('utf8')
if not window.isActiveWindow() and Settings()['notifications']:
tray_notification('Message', message.decode('utf8'))
profile = Profile.getInstance()
invoke_in_main_thread(profile.newMessage, friend_number, message_type, message)
return wrapped

View file

@ -71,7 +71,7 @@ def main():
tox = tox_factory(data, settings)
ms = MainWindow(tox)
ms.setup_info_from_tox()
#ms.setup_info_from_tox()
ms.show()
# bootstrap
for data in node_generator():

View file

@ -5,6 +5,37 @@ from PySide import QtGui, QtCore
from menu import *
from profile import Profile
from toxcore_enums_and_consts import *
from util import curr_time
class MessageItem(QtGui.QListWidget):
def __init__(self, text, time, user='', parent=None):
QtGui.QListWidget.__init__(self, parent)
self.setBaseSize(QtCore.QSize(250, 250))
self.name = QtGui.QLabel(self)
self.name.setGeometry(QtCore.QRect(0, 0, 50, 25))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(12)
font.setBold(True)
self.name.setFont(font)
self.name.setObjectName("name")
self.name.setText(user)
self.time = QtGui.QLabel(self)
self.time.setGeometry(QtCore.QRect(250, 0, 30, 20))
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 = QtGui.QPlainTextEdit(self)
self.message.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse | QtCore.Qt.LinksAccessibleByMouse)
self.message.setPlainText(text)
class ContactItem(QtGui.QListWidget):
@ -231,7 +262,7 @@ class MainWindow(QtGui.QMainWindow):
self.callButton.setText(QtGui.QApplication.translate("Form", "Start call", None, QtGui.QApplication.UnicodeUTF8))
QtCore.QMetaObject.connectSlotsByName(Form)
def setup_left_center(self, widget):
def setup_left_center(self, widget, profile_widget):
# widget.setFixedWidth(250)
# widget.setMinimumSize(QtCore.QSize(250, 500))
# widget.setMaximumSize(QtCore.QSize(250, 500))
@ -248,9 +279,13 @@ class MainWindow(QtGui.QMainWindow):
self.friends_list.addItem(elem)
self.friends_list.setItemWidget(elem, item)
widgets.append(item)
self.profile = Profile(self.tox, widgets, None)
self.profile = Profile(self.tox, widgets, profile_widget, self.messages)
self.friends_list.clicked.connect(self.friend_click)
def setup_right_center(self, widget):
self.messages = QtGui.QListWidget(widget)
self.messages.setGeometry(0, 0, 250, 300)
def initUI(self):
self.setMinimumSize(800, 400)
self.setGeometry(400, 400, 800, 400)
@ -263,22 +298,27 @@ class MainWindow(QtGui.QMainWindow):
name = QtGui.QWidget()
self.setup_left_top(name)
grid.addWidget(name, 0, 0)
messages = QtGui.QWidget()
self.setup_right_center(messages)
grid.addWidget(messages, 1, 1)
info = QtGui.QWidget()
self.setup_right_top(info)
grid.addWidget(info, 0, 1)
message = QtGui.QWidget()
self.setup_right_bottom(message)
grid.addWidget(message, 2, 1)
message_buttons = QtGui.QWidget()
self.setup_right_bottom(message_buttons)
grid.addWidget(message_buttons, 2, 1)
main_list = QtGui.QWidget()
self.setup_left_center(main_list)
self.setup_left_center(main_list, name)
grid.addWidget(main_list, 1, 0)
grid.setColumnMinimumWidth(1, 500)
grid.setColumnMinimumWidth(0, 250)
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
# if self.connection_status.status != TOX_USER_CONNECTION_STATUS['OFFLINE']:
# self.connection_status.status += 1
@ -288,8 +328,8 @@ class MainWindow(QtGui.QMainWindow):
def setup_info_from_tox(self):
# TODO: remove - use Profile()
self.name.setText(self.tox.self_get_name())
self.status_message.setText(self.tox.self_get_status_message())
self.name.setText(self.profile.name)
self.status_message.setText(self.profile.status_message)
# -----------------------------------------------------------------------------------------------------------------
# Functions which called when user click in menu

View file

@ -1,9 +1,11 @@
import mainscreen
from settings import Settings
from PySide import QtCore, QtGui
import os
from tox import Tox
from util import Singleton
from toxcore_enums_and_consts import *
from ctypes import *
from util import curr_time
class ProfileHelper(object):
@ -75,7 +77,7 @@ class Contact(object):
def setStatusMessage(self, value):
self._status_message = value
self._widget.status.setText(value)
self._widget.status_message.setText(value)
status_message = property(getStatusMessage, setStatusMessage)
@ -83,6 +85,7 @@ class Contact(object):
return self._status
def setStatus(self, value):
# TODO: status repaint
self._status = value
status = property(getStatus, setStatus)
@ -109,16 +112,21 @@ class Friend(Contact):
return self._number
number = property(getNumber)
# TODO: check if setNumber needed
class Profile(Contact, Singleton):
class Profile(Contact):
"""
Profile of current toxygen user. Contains friends list, tox instance
"""
# TODO: add slices
def __init__(self, tox, widgets, widget):
def __init__(self, tox, widgets, widget, messages_list):
self._widget = widget
self._messages = messages_list
self.tox = tox
self._name = tox.self_get_name()
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
for i in data:
@ -126,6 +134,11 @@ class Profile(Contact, Singleton):
status_message = tox.friend_get_status_message(i)
self.friends.append(Friend(name, status_message, i, widgets[num]))
num += 1
Profile._instance = self
@staticmethod
def getInstance():
return Profile._instance
def getActive(self):
return self._active_friend
@ -139,6 +152,9 @@ class Profile(Contact, Singleton):
def getActiveNumber(self):
return self.friends[self._active_friend].getNumber()
def getActiveName(self):
return self.friends[self._active_friend].getName()
def isActiveOnline(self):
if not self._active_friend + 1: # no active friend
return False
@ -148,6 +164,25 @@ class Profile(Contact, Singleton):
status = self.friends[self._active_friend].getStatus()
return status is not None
def newMessage(self, id, message_type, message):
if id == self._active_friend: # add message to list
user_name = Profile.getInstance().getActiveName()
item = mainscreen.MessageItem(message, curr_time(), user_name)
elem = QtGui.QListWidgetItem(self._messages)
print item.sizeHint()
elem.setSizeHint(QtCore.QSize(250, 50))
self._messages.addItem(elem)
self._messages.setItemWidget(elem, item)
self._messages.repaint()
else:
friend = filter(lambda x: x.getNumber() == id, self.friends)[0]
friend.setMessages(True)
def changeStatus(self):
if self._status is not None:
self._status += 1
self._status %= 3
def tox_factory(data=None, settings=None):
"""