filtering fixed

This commit is contained in:
ingvar1995 2018-07-29 11:16:03 +03:00
parent 10a77960dc
commit f38df24947
4 changed files with 139 additions and 44 deletions

View file

@ -25,7 +25,7 @@ class ContactsManager(ToxSave):
self._sorting = settings['sorting'] self._sorting = settings['sorting']
self._filter_string = '' self._filter_string = ''
self._friend_item_height = 40 if settings['compact_mode'] else 70 self._friend_item_height = 40 if settings['compact_mode'] else 70
#screen.online_contacts.setCurrentIndex(int(self._sorting)) screen.contacts_filter.setCurrentIndex(int(self._sorting))
self._history = history self._history = history
self._load_contacts() self._load_contacts()
@ -163,50 +163,55 @@ class ContactsManager(ToxSave):
def filtration_and_sorting(self, sorting=0, filter_str=''): def filtration_and_sorting(self, sorting=0, filter_str=''):
""" """
Filtration of friends list Filtration of friends list
:param sorting: 0 - no sorting, 1 - online only, 2 - online first, 4 - by name :param sorting: 0 - no sorting, 1 - online only, 2 - online first, 3 - by name,
4 - online and by name, 5 - online first and by name
:param filter_str: show contacts which name contains this substring :param filter_str: show contacts which name contains this substring
""" """
# TODO: simplify?
filter_str = filter_str.lower() filter_str = filter_str.lower()
number = self.get_active_number() contact = self.get_curr_contact()
is_friend = self.is_active_a_friend()
if sorting > 1: if sorting > 5 or sorting < 0:
if sorting & 2: sorting = 0
self._contacts = sorted(self._contacts, key=lambda x: int(x.status is not None), reverse=True)
if sorting & 4: if sorting in (1, 2, 4, 5): # online first
if not sorting & 2: self._contacts = sorted(self._contacts, key=lambda x: int(x.status is not None), reverse=True)
self._contacts = sorted(self._contacts, key=lambda x: x.name.lower()) sort_by_name = sorting in (4, 5)
else: # save results of prev sorting # save results of previous sorting
online_friends = filter(lambda x: x.status is not None, self._contacts) online_friends = filter(lambda x: x.status is not None, self._contacts)
count = len(list(online_friends)) online_friends_count = len(list(online_friends))
part1 = self._contacts[:count] part1 = self._contacts[:online_friends_count]
part2 = self._contacts[count:] part2 = self._contacts[online_friends_count:]
part1 = sorted(part1, key=lambda x: x.name.lower()) key_lambda = lambda x: x.name.lower() if sort_by_name else x.number
part2 = sorted(part2, key=lambda x: x.name.lower()) part1 = sorted(part1, key=key_lambda)
self._contacts = part1 + part2 part2 = sorted(part2, key=key_lambda)
else: # sort by number self._contacts = part1 + part2
online_friends = filter(lambda x: x.status is not None, self._contacts) elif sorting == 0:
count = len(list(online_friends)) self._contacts = sorted(self._contacts, key=lambda x: x.number)
part1 = self._contacts[:count] else:
part2 = self._contacts[count:] self._contacts = sorted(self._contacts, key=lambda x: x.name.lower())
part1 = sorted(part1, key=lambda x: x.number)
part2 = sorted(part2, key=lambda x: x.number) # change item widgets
self._contacts = part1 + part2 for index, contact in enumerate(self._contacts):
for index, contact in enumerate(self._contacts): list_item = self._screen.friends_list.item(index)
list_item = self._screen.friends_list.item(index) item_widget = self._screen.friends_list.itemWidget(list_item)
item_widget = self._screen.friends_list.itemWidget(list_item) contact.set_widget(item_widget)
contact.set_widget(item_widget)
for index, friend in enumerate(self._contacts): for index, friend in enumerate(self._contacts):
friend.visibility = (friend.status is not None or not (sorting & 1)) and (filter_str in friend.name.lower()) filtered_by_name = filter_str in friend.name.lower()
friend.visibility = (friend.status is not None or sorting not in (1, 4)) and filtered_by_name
# show friend even if it's hidden when there any unread messages/actions
friend.visibility = friend.visibility or friend.messages or friend.actions friend.visibility = friend.visibility or friend.messages or friend.actions
if friend.visibility: 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: else:
self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250, 0)) self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250, 0))
# save soring results
self._sorting, self._filter_string = sorting, filter_str self._sorting, self._filter_string = sorting, filter_str
self._settings['sorting'] = self._sorting self._settings['sorting'] = self._sorting
self._settings.save() self._settings.save()
self.set_active_by_number_and_type(number, is_friend) # update active contact
index = self._contacts.index(contact)
self.set_active(index)
def update_filtration(self): def update_filtration(self):
""" """

View file

@ -166,6 +166,8 @@ class MainWindow(QtWidgets.QMainWindow):
self.audioSettings.setText(util_ui.tr("Audio")) self.audioSettings.setText(util_ui.tr("Audio"))
self.videoSettings.setText(util_ui.tr("Video")) self.videoSettings.setText(util_ui.tr("Video"))
self.updateSettings.setText(util_ui.tr("Updates")) self.updateSettings.setText(util_ui.tr("Updates"))
self.importPlugin.setText(util_ui.tr("Import plugin"))
self.reloadPlugins.setText(util_ui.tr("Reload plugins"))
self.searchLineEdit.setPlaceholderText(util_ui.tr("Search")) self.searchLineEdit.setPlaceholderText(util_ui.tr("Search"))
self.sendMessageButton.setToolTip(util_ui.tr("Send message")) self.sendMessageButton.setToolTip(util_ui.tr("Send message"))
@ -178,12 +180,6 @@ class MainWindow(QtWidgets.QMainWindow):
self.contactsFilterComboBox.addItem(util_ui.tr("Online and by name")) self.contactsFilterComboBox.addItem(util_ui.tr("Online and by name"))
self.contactsFilterComboBox.addItem(util_ui.tr("Online first and by name")) self.contactsFilterComboBox.addItem(util_ui.tr("Online first and by name"))
ind = self._settings['sorting']
d = {0: 0, 1: 1, 2: 2, 3: 4, 4: 3, 1 | 4: 4, 2 | 4: 5}
self.contactsFilterComboBox.setCurrentIndex(d[ind])
self.importPlugin.setText(util_ui.tr("Import plugin"))
self.reloadPlugins.setText(util_ui.tr("Reload plugins"))
def setup_right_bottom(self, Form): def setup_right_bottom(self, Form):
Form.resize(650, 60) Form.resize(650, 60)
self.messageEdit = MessageArea(Form, self) self.messageEdit = MessageArea(Form, self)
@ -238,7 +234,7 @@ class MainWindow(QtWidgets.QMainWindow):
self.avatar_label = left_column.avatarLabel self.avatar_label = left_column.avatarLabel
self.searchLineEdit = left_column.searchLineEdit self.searchLineEdit = left_column.searchLineEdit
self.contactsFilterComboBox = left_column.contactsFilterComboBox self.contacts_filter = self.contactsFilterComboBox = left_column.contactsFilterComboBox
self.groupInvitesPushButton = left_column.groupInvitesPushButton self.groupInvitesPushButton = left_column.groupInvitesPushButton
@ -691,9 +687,9 @@ class MainWindow(QtWidgets.QMainWindow):
super().mouseReleaseEvent(event) super().mouseReleaseEvent(event)
def _filtering(self): def _filtering(self):
ind = self.contactsFilterComboBox.currentIndex() index = self.contactsFilterComboBox.currentIndex()
d = {0: 0, 1: 1, 2: 2, 3: 4, 4: 1 | 4, 5: 2 | 4} search_text = self.searchLineEdit.text()
self._contacts_manager.filtration_and_sorting(d[ind], self.searchLineEdit.text()) self._contacts_manager.filtration_and_sorting(index, search_text)
def show_search_field(self): def show_search_field(self):
if hasattr(self, 'search_field') and self.search_field.isVisible(): if hasattr(self, 'search_field') and self.search_field.isVisible():

View file

@ -57,7 +57,7 @@
<rect> <rect>
<x>40</x> <x>40</x>
<y>50</y> <y>50</y>
<width>23</width> <width>20</width>
<height>23</height> <height>23</height>
</rect> </rect>
</property> </property>

View file

@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>270</width>
<height>500</height>
</rect>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QLabel" name="avatarLabel">
<property name="geometry">
<rect>
<x>5</x>
<y>5</y>
<width>64</width>
<height>64</height>
</rect>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLineEdit" name="searchLineEdit">
<property name="geometry">
<rect>
<x>0</x>
<y>75</y>
<width>150</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QComboBox" name="contactsFilterComboBox">
<property name="geometry">
<rect>
<x>150</x>
<y>75</y>
<width>120</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="searchLabel">
<property name="geometry">
<rect>
<x>0</x>
<y>77</y>
<width>20</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QListWidget" name="friendsListWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>100</y>
<width>270</width>
<height>400</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="groupInvitesPushButton">
<property name="geometry">
<rect>
<x>0</x>
<y>100</y>
<width>270</width>
<height>30</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>