From 4706ef9b44996c66dfb063c49230c33a9ce8d344 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Thu, 10 Mar 2016 23:04:43 +0300 Subject: [PATCH] context menu added. alias full support, friend removing support --- src/main.py | 24 ++++++++++++++++----- src/mainscreen.py | 40 +++++++++++++++++++++++++++++++++++ src/profile.py | 53 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 107 insertions(+), 10 deletions(-) diff --git a/src/main.py b/src/main.py index 43a2189..de67cfd 100644 --- a/src/main.py +++ b/src/main.py @@ -74,11 +74,11 @@ def main(): ms = MainWindow(tox) ms.show() QtGui.QApplication.setStyle(get_style(settings['theme'])) # set application style - # bootstrap - for data in node_generator(): - tox.bootstrap(*data) - # initializing callbacks - init_callbacks(tox, ms) + + # init thread + init = InitThread(tox, ms) + init.start() + # starting thread for tox iterate mainloop = ToxIterateThread(tox) mainloop.start() @@ -92,6 +92,20 @@ def main(): del tox +class InitThread(QtCore.QThread): + + def __init__(self, tox, ms): + QtCore.QThread.__init__(self) + self.tox, self.ms = tox, ms + + def run(self): + # initializing callbacks + init_callbacks(self.tox, self.ms) + # bootstrap + for data in node_generator(): + self.tox.bootstrap(*data) + + class ToxIterateThread(QtCore.QThread): def __init__(self, tox): diff --git a/src/mainscreen.py b/src/mainscreen.py index 551e3d9..cb6d30e 100644 --- a/src/mainscreen.py +++ b/src/mainscreen.py @@ -181,6 +181,9 @@ class MainWindow(QtGui.QMainWindow): self.friends_list = QtGui.QListWidget(widget) self.friends_list.setGeometry(0, 0, 250, 250) self.friends_list.clicked.connect(self.friend_click) + self.friends_list.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) + self.friends_list.connect(self.friends_list, QtCore.SIGNAL("customContextMenuRequested(QPoint)"), + self.friend_right_click) def setup_right_center(self, widget): self.messages = QtGui.QListWidget(widget) @@ -264,6 +267,43 @@ class MainWindow(QtGui.QMainWindow): text = self.messageEdit.toPlainText() self.profile.send_message(text) +# ----------------------------------------------------------------------------------------------------------------- +# Functions which called when user open context menu in friends list +# ----------------------------------------------------------------------------------------------------------------- + + def friend_right_click(self, pos): + item = self.friends_list.itemAt(pos) + if item is not None: + self.listMenu = QtGui.QMenu() + set_alias_item = self.listMenu.addAction('Set alias') + clear_history_item = self.listMenu.addAction('Clear history') + copy_key_item = self.listMenu.addAction('Copy public key') + remove_item = self.listMenu.addAction('Remove friend') + self.connect(set_alias_item, QtCore.SIGNAL("triggered()"), lambda: self.set_alias(item)) + self.connect(remove_item, QtCore.SIGNAL("triggered()"), lambda: self.remove_friend(item)) + self.connect(copy_key_item, QtCore.SIGNAL("triggered()"), lambda: self.copy_friend_key(item)) + self.connect(clear_history_item, QtCore.SIGNAL("triggered()"), lambda: self.clear_history(item)) + parent_position = self.friends_list.mapToGlobal(QtCore.QPoint(0, 0)) + self.listMenu.move(parent_position + pos) + self.listMenu.show() + + def set_alias(self, item): + num = self.friends_list.indexFromItem(item).row() + self.profile.set_alias(num) + + def remove_friend(self, item): + num = self.friends_list.indexFromItem(item).row() + self.profile.delete_friend(num) + + def copy_friend_key(self, item): + num = self.friends_list.indexFromItem(item).row() + tox_id = self.profile.friend_public_key(num) + clipboard = QtGui.QApplication.clipboard() + clipboard.setText(tox_id) + + def clear_history(self, item): + num = self.friends_list.indexFromItem(item).row() + # ----------------------------------------------------------------------------------------------------------------- # Functions which called when user click somewhere else # ----------------------------------------------------------------------------------------------------------------- diff --git a/src/profile.py b/src/profile.py index 7ec3219..afc36ee 100644 --- a/src/profile.py +++ b/src/profile.py @@ -310,6 +310,14 @@ class Profile(Contact, Singleton): """ if value is None and self._active_friend == -1: # nothing to update return + if value == -1: # all friends were deleted + self.screen.account_name.setText('') + self.screen.account_status.setText('') + self._active_friend = -1 + self.screen.account_avatar.setHidden(True) + self.screen.messages.clear() + self.screen.messageEdit.clear() + return try: if value is not None: self._active_friend = value @@ -388,8 +396,37 @@ class Profile(Contact, Singleton): self.screen.messageEdit.clear() # ----------------------------------------------------------------------------------------------------------------- - # Work with friends (add, remove) + # Work with friends (add, remove, set alias, clear history) # ----------------------------------------------------------------------------------------------------------------- + def set_alias(self, num): + friend = self._friends[num] + name = friend.name.encode('utf-8') + dialog = "Enter new alias for friend {} or leave empty to use friend's name:".format(name) + text, ok = QtGui.QInputDialog.getText(None, 'Set alias', dialog) + if ok: + settings = Settings.get_instance() + aliases = settings['friends_aliases'] + if text: + friend.name = text.encode('utf-8') + try: + index = map(lambda x: x[0], aliases).index(friend.tox_id) + aliases[index] = (friend.tox_id, text) + except: + aliases.append((friend.tox_id, text)) + friend.set_alias(text) + else: # use default name + friend.name = self.tox.friend_get_name(friend.number) + friend.set_alias('') + try: + index = map(lambda x: x[0], aliases).index(friend.tox_id) + del aliases[index] + except: + pass + settings.save() + self.set_active() + + def friend_public_key(self, num): + return self._friends[num].tox_id def create_friend_item(self): """ @@ -442,11 +479,17 @@ class Profile(Contact, Singleton): def delete_friend(self, num): """ Removes friend from contact list - :param num: number of friend + :param num: number of friend in list """ - self.tox.friend_delete(num) - friend = filter(lambda x: x.number == num, self._friends)[0] - del friend + friend = self._friends[num] + self.tox.friend_delete(friend.number) + del self._friends[num] + self.screen.friends_list.takeItem(num) + if num == self._active_friend: # active friend was deleted + if not len(self._friends): # last friend was deleted + self.set_active(-1) + else: + self.set_active(0) def tox_factory(data=None, settings=None):