annotate cagou/core/utils.py @ 218:30be583dbabc

core (common): new SymbolLabel class to have a clickable area with a symbol and a label next to it.
author Goffi <goffi@goffi.org>
date Sun, 24 Jun 2018 22:09:49 +0200
parents 519b3a29743c
children 9faccd140119
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
196
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr//bin/env python2
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2016-2018 Jérôme Poisson (goffi@goffi.org)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """misc utils/behaviors"""
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from kivy.animation import Animation
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
24
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
25
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 class FilterBehavior(object):
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 """class to handle items filtering with animation"""
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 def __init__(self, *args, **kwargs):
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 super(FilterBehavior, self).__init__(*args, **kwargs)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 self._filter_last = u''
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 self._filter_anim = Animation(width = 0,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 height = 0,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 opacity = 0,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 d = 0.5)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 def do_filter(self, children, text, get_child_text, width_cb, height_cb, continue_tests=None):
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 """filter the children
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 filtered children will have a animation to set width, height and opacity to 0
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 @param children(kivy.uix.widget.Widget): widgets to filter
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 @param text(unicode): filter text (if this text is not present in a child,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 the child is filtered out)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 @param get_child_text(callable): must retrieve child text
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 child is used as sole argument
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 @param width_cb(callable, int, None): method to retrieve width when opened
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 child is used as sole argument, int can be used instead of callable
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 @param height_cb(callable, int, None): method to retrieve height when opened
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 child is used as sole argument, int can be used instead of callable
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 @param continue_tests(list[callable]): list of test to skip the item
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 all callables take child as sole argument.
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 if any of the callable return True, the child is skipped (i.e. not filtered)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 """
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 text = text.strip().lower()
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 filtering = len(text)>len(self._filter_last)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self._filter_last = text
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 for child in self.layout.children:
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 if continue_tests is not None and any([t(child) for t in continue_tests]):
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 continue
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 if text in get_child_text(child).lower():
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self._filter_anim.cancel(child)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 for key, method in (('width', width_cb),
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 ('height', height_cb),
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 ('opacity', lambda c: 1)):
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 try:
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 setattr(child, key, method(child))
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 except TypeError:
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 # method is not a callable, must be an int
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 setattr(child, key, method)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 elif (filtering
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 and child.opacity > 0
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 and not self._filter_anim.have_properties_to_animate(child)):
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self._filter_anim.start(child)