Mercurial > libervia-backend
annotate frontends/src/quick_frontend/quick_widgets.py @ 1304:1a61b18703c4 frontends_multi_profiles
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 06 Feb 2015 19:05:51 +0100 |
parents | d3ef3894254d |
children | 9512590dc3d7 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
4 # helper class for making a SAT frontend |
811 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) |
0 | 6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
10 # (at your option) any later version. |
0 | 11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
15 # GNU Affero General Public License for more details. |
0 | 16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
0 | 19 |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
20 from sat.core.log import getLogger |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
21 log = getLogger(__name__) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
22 from sat.core import exceptions |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
23 |
1301
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
24 from sat_frontends.quick_frontend.constants import Const as C |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
25 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
26 classes_map = {} |
0 | 27 |
28 | |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
29 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
30 # FIXME: to be removed when an acceptable solution is here |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
31 unicode('') # XXX: unicode doesn't exist in pyjamas |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
32 except (TypeError, AttributeError): # Error raised is not the same depending on pyjsbuild options |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
33 unicode = str |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
34 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
35 |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
36 def register(base_cls, child_cls=None): |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
37 """Register a child class to use by default when a base class is needed |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
38 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
39 @param base_cls: "Quick..." base class (like QuickChat or QuickContact), must inherit from QuickWidget |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
40 @param child_cls: inherited class to use when Quick... class is requested, must inherit from base_cls. |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
41 Can be None if it's the base_cls itself which register |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
42 """ |
1304
1a61b18703c4
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents:
1303
diff
changeset
|
43 # FIXME: we use base_cls.__name__ instead of base_cls directly because pyjamas because |
1a61b18703c4
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents:
1303
diff
changeset
|
44 # in the second case |
1a61b18703c4
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents:
1303
diff
changeset
|
45 classes_map[base_cls.__name__] = child_cls |
1a61b18703c4
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents:
1303
diff
changeset
|
46 |
1a61b18703c4
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents:
1303
diff
changeset
|
47 |
1301
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
48 class WidgetAlreadyExistsError(Exception): |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
49 pass |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
50 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
51 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
52 class QuickWidgetsManager(object): |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
53 """This class is used to manage all the widgets of a frontend |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
54 A widget can be a window, a graphical thing, or someting else depending of the frontend""" |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
55 |
0 | 56 def __init__(self, host): |
57 self.host = host | |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
58 self._widgets = {} |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
59 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
60 def __iter__(self): |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
61 """Iterate throught all widgets""" |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
62 for widget_map in self._widgets.itervalues(): |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
63 for widget in widget_map.itervalues(): |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
64 yield widget |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
65 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
66 def getRealClass(self, class_): |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
67 """Return class registered for given class_ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
68 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
69 @param class_: subclass of QuickWidget |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
70 @return: class actually used to create widget |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
71 """ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
72 try: |
1304
1a61b18703c4
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents:
1303
diff
changeset
|
73 # FIXME: we use base_cls.__name__ instead of base_cls directly because pyjamas bugs |
1a61b18703c4
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents:
1303
diff
changeset
|
74 # in the second case |
1a61b18703c4
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents:
1303
diff
changeset
|
75 cls = classes_map[class_.__name__] |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
76 except KeyError: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
77 cls = class_ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
78 if cls is None: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
79 raise exceptions.InternalError("There is not class registered for {}".format(class_)) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
80 return cls |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
81 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
82 def getWidgets(self, class_): |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
83 """Get all subclassed widgets |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
84 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
85 @param class_: subclass of QuickWidget, same parameter as used in [getOrCreateWidget] |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
86 @return: iterator on widgets |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
87 """ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
88 class_ = self.getRealClass(class_) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
89 try: |
1304
1a61b18703c4
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents:
1303
diff
changeset
|
90 widgets_map = self._widgets[class_.__name__] |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
91 except KeyError: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
92 return iter([]) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
93 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
94 return widgets_map.itervalues() |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
95 |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
96 def getOrCreateWidget(self, class_, target, *args, **kwargs): |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
97 """Get an existing widget or create a new one when necessary |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
98 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
99 If the widget is new, self.host.newWidget will be called with it. |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
100 @param class_(class): class of the widget to create |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
101 @param target: target depending of the widget, usually a JID instance |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
102 @param args(list): optional args to create a new instance of class_ |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
103 @param kwargs(list): optional kwargs to create anew instance of class_ |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
104 if 'profile' key is present, it will be popped and put in 'profiles' |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
105 if there is neither 'profile' nor 'profiles', None will be used for 'profiles' |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
106 if 'on_new_widget' is present it can have the following values: |
1301
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
107 C.WIDGET_NEW [default]: self.host.newWidget will be called on widget creation |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
108 [callable]: this method will be called instead of self.host.newWidget |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
109 None: do nothing |
1301
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
110 if 'on_existing_widget' is present it can have the following values: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
111 C.WIDGET_KEEP [default]: return the existing widget |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
112 C.WIDGET_RAISE: raise WidgetAlreadyExistsError |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
113 C.WIDGET_RECREATE: create a new widget *WITH A NEW HASH* |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
114 if 'force_hash' is present, the hash given in value will be used instead of the one returned by class_.getWidgetHash |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
115 @return: a class_ instance, either new or already existing |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
116 """ |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
117 cls = self.getRealClass(class_) |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
118 |
1301
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
119 ## arguments management ## |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
120 _args = [self.host, target] + list(args) or [] # FIXME: check if it's really necessary to use optional args |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
121 _kwargs = kwargs or {} |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
122 if 'profiles' in _kwargs and 'profile' in _kwargs: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
123 raise ValueError("You can't have 'profile' and 'profiles' keys at the same time") |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
124 try: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
125 _kwargs['profiles'] = _kwargs.pop('profile') |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
126 except KeyError: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
127 if not 'profiles' in _kwargs: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
128 _kwargs['profiles'] = None |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
129 |
1301
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
130 #on_new_widget tell what to do for the new widget creation |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
131 try: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
132 on_new_widget = _kwargs.pop('on_new_widget') |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
133 except KeyError: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
134 on_new_widget = C.WIDGET_NEW |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
135 |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
136 #on_existing_widget tell what to do when the widget already exists |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
137 try: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
138 on_existing_widget = _kwargs.pop('on_existing_widget') |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
139 except KeyError: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
140 on_existing_widget = C.WIDGET_KEEP |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
141 |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
142 ## we get the hash ## |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
143 try: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
144 hash_ = _kwargs.pop('force_hash') |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
145 except KeyError: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
146 hash_ = cls.getWidgetHash(target, _kwargs['profiles']) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
147 |
1304
1a61b18703c4
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents:
1303
diff
changeset
|
148 ## widget creation or retrieval ## |
1a61b18703c4
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents:
1303
diff
changeset
|
149 |
1a61b18703c4
quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents:
1303
diff
changeset
|
150 widgets_map = self._widgets.setdefault(cls.__name__, {}) # we sorts widgets by classes |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
151 if not cls.SINGLE: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
152 widget = None # if the class is not SINGLE, we always create a new widget |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
153 else: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
154 try: |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
155 widget = widgets_map[hash_] |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
156 widget.addTarget(target) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
157 except KeyError: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
158 widget = None |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
159 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
160 if widget is None: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
161 # we need to create a new widget |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
162 log.debug(u"Creating new widget for target {} {}".format(target, cls)) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
163 widget = cls(*_args, **_kwargs) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1265
diff
changeset
|
164 widgets_map[hash_] = widget |
0 | 165 |
1301
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
166 if on_new_widget == C.WIDGET_NEW: |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
167 self.host.newWidget(widget) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
168 elif callable(on_new_widget): |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
169 on_new_widget(widget) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
170 else: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
171 assert on_new_widget is None |
1301
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
172 else: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
173 # the widget already exists |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
174 if on_existing_widget == C.WIDGET_RAISE: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
175 raise WidgetAlreadyExistsError(hash_) |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
176 elif on_existing_widget == C.WIDGET_RECREATE: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
177 # we use getOrCreateWidget to recreate the new widget |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
178 # /!\ we use args and kwargs and not _args and _kwargs because we need the original args |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
179 # we need to get rid of kwargs special options |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
180 new_kwargs = kwargs.copy() |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
181 try: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
182 new_kwargs.pop('force_hash') # FIXME: we use pop instead of del here because pyjamas doesn't raise error on del |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
183 except KeyError: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
184 pass |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
185 else: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
186 raise ValueError("force_hash option can't be used with on_existing_widget=RECREATE") |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
187 |
1301
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
188 # XXX: keep up-to-date if new special kwargs are added (i.e.: delete these keys here) |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
189 new_kwargs['on_new_widget'] = None |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
190 new_kwargs['on_existing_widget'] = C.WIDGET_RAISE |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
191 hash_idx = 1 |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
192 while True: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
193 new_kwargs['force_hash'] = hash_ + "_new_instance_{}".format(hash_idx) |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
194 try: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
195 widget = self.getOrCreateWidget(class_, target, *args, **new_kwargs) |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
196 except WidgetAlreadyExistsError: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
197 hash_idx += 1 |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
198 else: |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
199 log.debug(u"Widget already exists, a new one has been recreated with hash {}".format(new_kwargs['force_hash'])) |
afc57b34c0a3
Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
200 break |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
201 return widget |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
202 |
1303
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
203 def deleteWidget(self, widget_to_delete): |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
204 """Delete a widget |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
205 |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
206 widget's onDelete method will be called before deletion |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
207 """ |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
208 widget_to_delete.onDelete() |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
209 |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
210 for widget_map in self._widgets.itervalues(): |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
211 for hash_, widget in widget_map.iteritems(): |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
212 if widget_to_delete is widget: |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
213 del widget_map[hash_] |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
214 |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
215 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
216 class QuickWidget(object): |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
217 """generic widget base""" |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
218 SINGLE=True # if True, there can be only one widget per target(s) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
219 PROFILES_MULTIPLE=False |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
220 PROFILES_ALLOW_NONE=False |
0 | 221 |
1265
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
222 def __init__(self, host, target, profiles=None): |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
223 """ |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
224 @param host: %(doc_host)s |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
225 @param target: target specific for this widget class |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
226 @param profiles: can be either: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
227 - (unicode): used when widget class manage a unique profile |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
228 - (iterable): some widget class can manage several profiles, several at once can be specified here |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
229 - None: no profile is managed by this widget class (rare) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
230 @raise: ValueError when (iterable) or None is given to profiles for a widget class which manage one unique profile. |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
231 """ |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
232 self.host = host |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
233 self.targets = set() |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
234 self.addTarget(target) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
235 self.profiles = set() |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
236 if isinstance(profiles, basestring): |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
237 self.addProfile(profiles) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
238 elif profiles is None: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
239 if not self.PROFILES_ALLOW_NONE: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
240 raise ValueError("profiles can't have a value of None") |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
241 else: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
242 if not self.PROFILES_MULTIPLE: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
243 raise ValueError("multiple profiles are not allowed") |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
244 for profile in profiles: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
245 self.addProfile(profile) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
246 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
247 @property |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
248 def profile(self): |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
249 assert len(self.profiles) == 1 and not self.PROFILES_MULTIPLE and not self.PROFILES_ALLOW_NONE |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
250 return list(self.profiles)[0] |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
251 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
252 def addTarget(self, target): |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
253 """Add a target if it doesn't already exists |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
254 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
255 @param target: target to add |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
256 """ |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
257 self.targets.add(target) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
258 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
259 def addProfile(self, profile): |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
260 """Add a profile is if doesn't already exists |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
261 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
262 @param profile: profile to add |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
263 """ |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
264 if self.profiles and not self.PROFILES_MULTIPLE: |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
265 raise ValueError("multiple profiles are not allowed") |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
266 self.profiles.add(profile) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
267 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
268 @staticmethod |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
269 def getWidgetHash(target, profiles): |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
270 """Return the hash associated with this target for this widget class |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
271 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
272 some widget classes can manage several target on the same instance |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
273 (e.g.: a chat widget with multiple resources on the same bare jid), |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
274 this method allow to return a hash associated to one or several targets |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
275 to retrieve the good instance. For example, a widget managing JID targets, |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
276 and all resource of the same bare jid would return the bare jid as hash. |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
277 |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
278 @param target: target to check |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
279 @param profiles: profile(s) associated to target, see __init__ docstring |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
280 @return: a hash (can correspond to one or many targets or profiles, depending of widget class) |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
281 """ |
e3a9ea76de35
quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents:
1139
diff
changeset
|
282 return unicode(target) # by defaut, there is one hash for one target |
1303
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
283 |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
284 def onDelete(self): |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
285 """Called when a widget is deleted""" |
d3ef3894254d
quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents:
1301
diff
changeset
|
286 log.debug(u"deleting widget {}".format(self)) # Must be implemented by frontends |