contacts sorting

This commit is contained in:
ingvar1995 2016-10-30 18:13:12 +03:00
parent e598d027eb
commit 202c5a14a5
7 changed files with 77 additions and 24 deletions

View file

@ -24,10 +24,7 @@ class BaseContact:
self._name, self._status_message = name, status_message
self._status, self._widget = None, widget
self._tox_id = tox_id
if self._widget is not None:
self._widget.name.setText(name)
self._widget.status_message.setText(status_message)
self.load_avatar()
self.init_widget()
# -----------------------------------------------------------------------------------------------------------------
# Name - current name or alias of user
@ -112,3 +109,10 @@ class BaseContact:
def get_pixmap(self):
return self._widget.avatar_label.pixmap()
def init_widget(self):
if self._widget is not None:
self._widget.name.setText(self._name)
self._widget.status_message.setText(self._status_message)
self._widget.connection_status.update(self._status)
self.load_avatar()

View file

@ -186,6 +186,10 @@ class Contact(basecontact.BaseContact):
visibility = property(get_visibility, set_visibility)
def set_widget(self, widget):
self._widget = widget
self.init_widget()
# -----------------------------------------------------------------------------------------------------------------
# Unread messages and other actions from friend
# -----------------------------------------------------------------------------------------------------------------

View file

@ -3,7 +3,7 @@
from menu import *
from profile import *
from list_items import *
from widgets import MultilineEdit, LineEdit
from widgets import MultilineEdit, LineEdit, ComboBox
import plugin_support
from mainscreen_widgets import *
import settings
@ -123,7 +123,13 @@ class MainWindow(QtGui.QMainWindow, Singleton):
self.online_contacts.clear()
self.online_contacts.addItem(QtGui.QApplication.translate("MainWindow", "All", None, QtGui.QApplication.UnicodeUTF8))
self.online_contacts.addItem(QtGui.QApplication.translate("MainWindow", "Online", None, QtGui.QApplication.UnicodeUTF8))
self.online_contacts.setCurrentIndex(int(Settings.get_instance()['show_online_friends']))
self.online_contacts.addItem(QtGui.QApplication.translate("MainWindow", "Online first", None, QtGui.QApplication.UnicodeUTF8))
self.online_contacts.addItem(QtGui.QApplication.translate("MainWindow", "Name", None, QtGui.QApplication.UnicodeUTF8))
self.online_contacts.addItem(QtGui.QApplication.translate("MainWindow", "Online and by name", None, QtGui.QApplication.UnicodeUTF8))
self.online_contacts.addItem(QtGui.QApplication.translate("MainWindow", "Online first and by name", None, QtGui.QApplication.UnicodeUTF8))
ind = Settings.get_instance()['sorting']
d = {0: 0, 1: 1, 2: 2, 3: 4, 1 | 4: 4, 2 | 4: 5}
self.online_contacts.setCurrentIndex(d[ind])
self.importPlugin.setText(QtGui.QApplication.translate("MainWindow", "Import plugin", None, QtGui.QApplication.UnicodeUTF8))
def setup_right_bottom(self, Form):
@ -171,7 +177,7 @@ class MainWindow(QtGui.QMainWindow, Singleton):
self.contact_name.setObjectName("contact_name")
self.contact_name.textChanged.connect(self.filtering)
self.online_contacts = QtGui.QComboBox(Form)
self.online_contacts = ComboBox(Form)
self.online_contacts.setGeometry(QtCore.QRect(150, 0, 120, 25))
self.online_contacts.activated[int].connect(lambda x: self.filtering())
self.search_label.raise_()
@ -257,6 +263,8 @@ class MainWindow(QtGui.QMainWindow, Singleton):
self.friends_list.connect(self.friends_list, QtCore.SIGNAL("customContextMenuRequested(QPoint)"),
self.friend_right_click)
self.friends_list.setVerticalScrollMode(QtGui.QAbstractItemView.ScrollPerPixel)
self.friends_list.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.friends_list.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
def setup_right_center(self, widget):
self.messages = QtGui.QListWidget(widget)
@ -512,9 +520,9 @@ class MainWindow(QtGui.QMainWindow, Singleton):
if self.profile.active_friend + 1:
self.sticker = StickerWindow(self)
self.sticker.setGeometry(QtCore.QRect(self.x() if Settings.get_instance()['mirror_mode'] else 270 + self.x(),
self.y() + self.height() - 200,
self.sticker.width(),
self.sticker.height()))
self.y() + self.height() - 200,
self.sticker.width(),
self.sticker.height()))
self.sticker.show()
def active_call(self):
@ -663,4 +671,6 @@ class MainWindow(QtGui.QMainWindow, Singleton):
super(MainWindow, self).mouseReleaseEvent(event)
def filtering(self):
self.profile.filtration(self.online_contacts.currentIndex() == 1, self.contact_name.text())
ind = self.online_contacts.currentIndex()
d = {0: 0, 1: 1, 2: 2, 3: 4, 4: 1 | 4, 5: 2 | 4}
self.profile.filtration_and_sorting(d[ind], self.contact_name.text())

View file

@ -365,7 +365,7 @@ class WelcomeScreen(CenteredWidget):
None, QtGui.QApplication.UnicodeUTF8)
elif num == 6:
text = QtGui.QApplication.translate('WelcomeScreen',
'New in Toxygen v0.2.4:<br>File transfers update<br>Autoreconnection<br>Improvements<br>Bug fixes',
'New in Toxygen v0.2.6:<br>Updater<br>Better contact sorting<br>Plugins improvements',
None, QtGui.QApplication.UnicodeUTF8)
elif num == 7:
text = QtGui.QApplication.translate('WelcomeScreen',

View file

@ -43,13 +43,13 @@ class Profile(basecontact.BaseContact, Singleton):
self._load_history = True
self._factory = items_factory.ItemsFactory(self._screen.friends_list, self._messages)
settings = Settings.get_instance()
self._show_online = settings['show_online_friends']
self._sorting = settings['sorting']
self._show_avatars = settings['show_avatars']
self._filter_string = ''
self._friend_item_height = 40 if settings['compact_mode'] else 70
self._paused_file_transfers = dict(settings['paused_file_transfers'])
# key - file id, value: [path, friend number, is incoming, start position]
screen.online_contacts.setCurrentIndex(int(self._show_online))
screen.online_contacts.setCurrentIndex(int(self._sorting))
aliases = settings['friends_aliases']
data = tox.self_get_friend_list()
self._history = History(tox.self_get_public_key()) # connection to db
@ -69,7 +69,7 @@ class Profile(basecontact.BaseContact, Singleton):
friend = Friend(message_getter, i, name, status_message, item, tox_id)
friend.set_alias(alias)
self._contacts.append(friend)
self.filtration(self._show_online)
self.filtration_and_sorting(self._sorting)
# -----------------------------------------------------------------------------------------------------------------
# Edit current user's data
@ -119,31 +119,59 @@ class Profile(basecontact.BaseContact, Singleton):
# Filtration
# -----------------------------------------------------------------------------------------------------------------
def filtration(self, show_online=True, filter_str=''):
def filtration_and_sorting(self, sorting=0, filter_str=''):
"""
Filtration of friends list
:param show_online: show online only contacts
:param sorting: 0 - no sort, 1 - online only, 2 - online first, 4 - by name
:param filter_str: show contacts which name contains this substring
"""
filter_str = filter_str.lower()
settings = Settings.get_instance()
if sorting > 1:
if sorting & 2:
self._contacts = sorted(self._contacts, key=lambda x: int(x.status is not None), reverse=True)
if sorting & 4:
if not sorting & 2:
self._contacts = sorted(self._contacts, key=lambda x: x.name)
else: # save results of prev sorting
online_friends = filter(lambda x: x.status is not None, self._contacts)
count = len(list(online_friends))
part1 = self._contacts[:count]
part2 = self._contacts[count:]
part1 = sorted(part1, key=lambda x: x.name)
part2 = sorted(part2, key=lambda x: x.name)
self._contacts = part1 + part2
else: # sort by number
online_friends = filter(lambda x: x.status is not None, self._contacts)
count = len(list(online_friends))
part1 = self._contacts[:count]
part2 = self._contacts[count:]
part1 = sorted(part1, key=lambda x: x.number)
part2 = sorted(part2, key=lambda x: x.number)
self._contacts = part1 + part2
self._screen.friends_list.clear()
for contact in self._contacts:
contact.set_widget(self.create_friend_item())
for index, friend in enumerate(self._contacts):
friend.visibility = (friend.status is not None or not show_online) and (filter_str in friend.name.lower())
friend.visibility = (friend.status is not None or not (sorting & 1)) and (filter_str in friend.name.lower())
friend.visibility = friend.visibility or friend.messages or friend.actions
if friend.visibility:
self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250,
self._friend_item_height))
self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250, self._friend_item_height))
else:
self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250, 0))
self._show_online, self._filter_string = show_online, filter_str
settings['show_online_friends'] = self._show_online
self._sorting, self._filter_string = sorting, filter_str
settings['sorting'] = self._sorting
settings.save()
def update_filtration(self):
"""
Update list of contacts when 1 of friends change connection status
"""
self.filtration(self._show_online, self._filter_string)
self.filtration_and_sorting(self._sorting, self._filter_string)
# -----------------------------------------------------------------------------------------------------------------
# Friend getters
# -----------------------------------------------------------------------------------------------------------------
def get_friend_by_number(self, num):
return list(filter(lambda x: x.number == num, self._contacts))[0]

View file

@ -121,7 +121,7 @@ class Settings(dict, Singleton):
'allow_inline': True,
'allow_auto_accept': True,
'auto_accept_path': None,
'show_online_friends': False,
'sorting': 0,
'auto_accept_from_friends': [],
'paused_file_transfers': {},
'resend_files': True,

View file

@ -15,6 +15,13 @@ class DataLabel(QtGui.QLabel):
super().setText(text)
class ComboBox(QtGui.QComboBox):
def __init__(self, *args):
super().__init__(*args)
self.view().setSizePolicy(QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Expanding)
class CenteredWidget(QtGui.QWidget):
def __init__(self):