identicons basic support

This commit is contained in:
ingvar1995 2018-05-16 20:25:21 +03:00
parent f3aa0aeda3
commit c0a143c817
9 changed files with 36 additions and 14 deletions

View file

@ -10,7 +10,7 @@ version = main.__version__ + '.0'
if system() == 'Windows':
MODULES = ['PyQt5', 'PyAudio', 'numpy', 'opencv-python']
MODULES = ['PyQt5', 'PyAudio', 'numpy', 'opencv-python', 'pydenticon']
else:
MODULES = []
try:
@ -29,6 +29,10 @@ else:
import cv2
except ImportError:
MODULES.append('opencv-python')
try:
import pydenticon
except ImportError:
MODULES.append('pydenticon')
class InstallScript(install):

View file

@ -3,6 +3,7 @@ from PyQt5 import QtCore, QtGui
from wrapper.toxcore_enums_and_consts import TOX_PUBLIC_KEY_SIZE
import utils.util as util
import common.event as event
import contacts.common as common
class BaseContact:
@ -119,14 +120,17 @@ class BaseContact:
self._widget.avatar_label.repaint()
self._avatar_changed_event(avatar_path)
def reset_avatar(self):
def reset_avatar(self, generate_new):
avatar_path = self.get_avatar_path()
if os.path.isfile(avatar_path):
if os.path.isfile(avatar_path) and not avatar_path == self._get_default_avatar_path():
os.remove(avatar_path)
if generate_new:
self.set_avatar(common.generate_avatar(self.tox_id))
else:
self.load_avatar()
def set_avatar(self, avatar):
avatar_path = self.get_avatar_path()
avatar_path = self.get_contact_avatar_path()
with open(avatar_path, 'wb') as f:
f.write(avatar)
self.load_avatar()
@ -137,7 +141,7 @@ class BaseContact:
def get_avatar_path(self):
avatar_path = self.get_contact_avatar_path()
if not os.path.isfile(avatar_path) or not os.path.getsize(avatar_path): # load default image
avatar_path = util.join_path(util.get_images_directory(), self._get_default_avatar_name())
avatar_path = self._get_default_avatar_path()
return avatar_path
@ -166,5 +170,5 @@ class BaseContact:
# -----------------------------------------------------------------------------------------------------------------
@staticmethod
def _get_default_avatar_name():
return 'avatar.png'
def _get_default_avatar_path():
return util.join_path(util.get_images_directory(), 'avatar.png')

View file

@ -1,3 +1,4 @@
from pydenticon import Generator
class BaseTypingNotificationHandler:
@ -22,3 +23,17 @@ class FriendTypingNotificationHandler(BaseTypingNotificationHandler):
BaseTypingNotificationHandler.DEFAULT_HANDLER = BaseTypingNotificationHandler()
def generate_avatar(tox_id):
foreground = ["rgb(45,79,255)",
"rgb(254,180,44)",
"rgb(226,121,234)",
"rgb(30,179,253)",
"rgb(232,77,65)",
"rgb(49,203,115)",
"rgb(141,69,170)"]
generator = Generator(5, 5, foreground=foreground, background="rgba(42,42,42,0)")
identicon = generator.generate(tox_id, 220, 220, padding=(10, 10, 10, 10))
return identicon

View file

@ -295,6 +295,7 @@ class ContactsManager:
self._history.add_friend_to_db(tox_id)
friend = self._contact_provider.get_friend_by_public_key(tox_id)
self._contacts.append(friend)
friend.reset_avatar()
def block_user(self, tox_id):
"""

View file

@ -136,8 +136,8 @@ class Profile(basecontact.BaseContact):
self._call.stop()
del self._call
def reset_avatar(self):
super().reset_avatar()
def reset_avatar(self, generate_new):
super().reset_avatar(generate_new)
for friend in filter(lambda x: x.status is not None, self._contacts):
self.send_avatar(friend.number)

View file

@ -316,9 +316,6 @@ class ReceiveAvatar(ReceiveTransfer):
elif not size:
self.send_control(TOX_FILE_CONTROL['CANCEL'])
self._file.close()
if exists(path):
remove(path)
self._file.close()
remove(full_path)
elif exists(path):
hash = self.get_file_id()

View file

@ -271,7 +271,7 @@ class FileTransfersHandler:
self._file_transfers[(friend_number, file_number)] = ra
ra.set_transfer_finished_handler(self.transfer_finished)
else:
friend.load_avatar()
friend.reset_avatar(self._settings['identicons'])
# -----------------------------------------------------------------------------------------------------------------
# Private methods

View file

@ -245,7 +245,7 @@ class ProfileSettings(CenteredWidget):
self.tox_id.setText(self._profile.new_nospam())
def reset_avatar(self):
self._profile.reset_avatar()
self._profile.reset_avatar(self._settings['identicons'])
def set_avatar(self):
choose = util_ui.tr("Choose avatar")

View file

@ -140,6 +140,7 @@ class Settings(dict):
'unread_color': 'red',
'save_unsent_only': False,
'compact_mode': False,
'identicons': True,
'show_welcome_screen': True,
'close_to_tray': False,
'font': 'Times New Roman',