changeset 405:84ff5c917064

widgets: implemented ordering in ContactItem and CagouWidget: ordering of slides in collection_carousel were using a specific key during the sort, this has been replaced by an ordering implemented directly into the classes. Ordering has also been implemented in ContactItem to have items appearing in a consistent order notably for opened chats.
author Goffi <goffi@goffi.org>
date Wed, 12 Feb 2020 20:02:58 +0100
parents f7476818f9fb
children 03554ad70846
files cagou/core/cagou_widget.py cagou/core/common.py cagou/core/widgets_handler.py
diffstat 3 files changed, 19 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/cagou_widget.py	Wed Feb 12 20:02:58 2020 +0100
+++ b/cagou/core/cagou_widget.py	Wed Feb 12 20:02:58 2020 +0100
@@ -18,6 +18,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+from functools import total_ordering
 from sat.core import log as logging
 from sat.core import exceptions
 from kivy.uix.behaviors import ButtonBehavior
@@ -83,6 +84,7 @@
         menu.ExtraSideMenu().show()
 
 
+@total_ordering
 class CagouWidget(BoxLayout):
     main_container = properties.ObjectProperty(None)
     header_input = properties.ObjectProperty(None)
@@ -118,6 +120,17 @@
             )
             self.header_box.add_widget(self.header_input)
 
+    def __lt__(self, other):
+        # XXX: sorting is notably used when collection_carousel is set
+        try:
+            target = str(self.target)
+        except AttributeError:
+            target = str(list(self.targets)[0])
+            other_target = str(list(other.targets)[0])
+        else:
+            other_target = str(other.target)
+        return target < other_target
+
     @property
     def screen_manager(self):
         if ((not self.global_screen_manager
--- a/cagou/core/common.py	Wed Feb 12 20:02:58 2020 +0100
+++ b/cagou/core/common.py	Wed Feb 12 20:02:58 2020 +0100
@@ -19,7 +19,7 @@
 """common simple widgets"""
 
 import json
-from functools import partial
+from functools import partial, total_ordering
 from sat.core.i18n import _
 from sat.core import log as logging
 from kivy.uix.widget import Widget
@@ -54,7 +54,7 @@
 class NotifLabel(Label):
     pass
 
-
+@total_ordering
 class ContactItem(BoxLayout):
     """An item from ContactList
 
@@ -86,6 +86,9 @@
                 self.avatar_layout.remove_widget(self.badge)
                 self.badge = None
 
+    def __lt__(self, other):
+        return self.jid < other.jid
+
 
 class ContactButton(ButtonBehavior, ContactItem):
     pass
--- a/cagou/core/widgets_handler.py	Wed Feb 12 20:02:58 2020 +0100
+++ b/cagou/core/widgets_handler.py	Wed Feb 12 20:02:58 2020 +0100
@@ -497,17 +497,6 @@
                 continue
             yield w
 
-    def widgets_sort(self, widget):
-        """method used as key to sort the widgets
-
-        order of the widgets when changing slide is affected
-        @param widget(QuickWidget): widget to sort
-        @return: a value which will be used for sorting
-        """
-        try:
-            return str(widget.target).lower()
-        except AttributeError:
-            return str(list(widget.targets)[0]).lower()
 
     def _updateHiddenSlides(self):
         """adjust carousel slides according to visible widgets"""
@@ -522,7 +511,7 @@
         # we ignore current_slide as it may not be visible yet (e.g. if an other
         # screen is shown
         hidden = list(self.hiddenList(visible_list, ignore=current_slide))
-        slides_sorted =  sorted(set(hidden + [current_slide]), key=self.widgets_sort)
+        slides_sorted =  sorted(set(hidden + [current_slide]))
         to_remove = set(self.carousel.slides).difference({current_slide})
         for w in to_remove:
             self.carousel.remove_widget(w)