comparison browser_side/panels.py @ 18:795d144fc1d2

moved panels and menu in a separate file
author Goffi <goffi@goffi.org>
date Fri, 15 Apr 2011 15:30:31 +0200
parents
children e8e3704eb97f
comparison
equal deleted inserted replaced
17:c725b702e927 18:795d144fc1d2
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 Libervia: a Salut à Toi frontend
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 import pyjd # this is dummy in pyjs
23 from pyjamas.ui.SimplePanel import SimplePanel
24 from pyjamas.ui.VerticalPanel import VerticalPanel
25 from pyjamas.ui.HorizontalPanel import HorizontalPanel
26 from pyjamas.ui.ScrollPanel import ScrollPanel
27 from pyjamas.ui.HTMLPanel import HTMLPanel
28 from pyjamas.ui.Grid import Grid
29 from pyjamas.ui.MenuBar import MenuBar
30 from pyjamas.ui.MenuItem import MenuItem
31 from pyjamas.ui.Label import Label
32 from pyjamas.ui.DropWidget import DropWidget
33 from pyjamas.ui import HasAlignment
34 from pyjamas import Window
35 from pyjamas import DOM
36
37 from pyjamas.dnd import makeDraggable
38 from pyjamas.ui.DragWidget import DragWidget, DragContainer
39 from tools.jid import JID
40 from datetime import datetime
41
42 class MenuCmd:
43
44 def __init__(self, object, handler):
45 self._object = object
46 self._handler = handler
47
48 def execute(self):
49 handler = getattr(self._object, self._handler)
50 handler()
51
52 class Menu(SimplePanel):
53
54 def __init__(self):
55 SimplePanel.__init__(self)
56
57 menu_general = MenuBar(vertical=True)
58 menu_general.addItem("Properties", MenuCmd(self, "onProperties"))
59
60 menu_games = MenuBar(vertical=True)
61 menu_games.addItem("Tarot", MenuCmd(self, "onTarotGame"))
62 menu_games.addItem("Xiangqi", MenuCmd(self, "onXiangqiGame"))
63
64 menubar = MenuBar(vertical=False)
65 menubar.addItem(MenuItem("General", menu_general))
66 menubar.addItem(MenuItem("Games", True, menu_games))
67 self.add(menubar)
68
69 def onProperties(self):
70 Window.alert("Properties selected")
71
72 def onTarotGame(self):
73 Window.alert("Tarot selected")
74
75 def onXiangqiGame(self):
76 Window.alert("Xiangqi selected")
77
78 class DropCell(DropWidget):
79 """Cell in the middle grid which replace itself with the dropped widget on DnD"""
80
81 def __init__(self):
82 DropWidget.__init__(self)
83
84 def onDragEnter(self, event):
85 print "drag enter"
86 self.addStyleName('dragover')
87 DOM.eventPreventDefault(event)
88
89 def onDragLeave(self, event):
90 print "drag leave"
91 self.removeStyleName('dragover')
92
93 def onDragOver(self, event):
94 DOM.eventPreventDefault(event)
95
96 def _getCellAndRow(self, grid, event):
97 """Return cell and row index where the event is occuring"""
98 cell = grid.getEventTargetCell(event)
99 row = DOM.getParent(cell)
100 return (row.rowIndex, cell.cellIndex)
101
102
103 def onDrop(self, event):
104 print "Empty Panel: onDrop"
105 dt = event.dataTransfer
106 #'text', 'text/plain', and 'Text' are equivalent.
107 try:
108 item = dt.getData("text/plain")
109 item_type = dt.getData("type")
110 print "message: %s" % item
111 print "type: %s" % item_type
112 except:
113 print "no message found"
114 item='&nbsp;'
115 item_type = None
116 DOM.eventPreventDefault(event)
117 if item_type=="GROUP":
118 _mblog = MicroblogPanel(self.host, item)
119 _mblog.setAcceptedGroup(item)
120 elif item_type=="CONTACT":
121 _mblog = MicroblogPanel(self.host, accept_all=True)
122 self.host.mpanels.remove(self)
123 self.host.mpanels.append(_mblog)
124 print "DEBUG"
125 grid = self.getParent()
126 row_idx, cell_idx = self._getCellAndRow(grid, event)
127 self.removeFromParent()
128 grid.setWidget(row_idx, cell_idx, _mblog)
129 print "index:", row_idx, cell_idx
130 #FIXME: delete object ? Check the right way with pyjamas
131 #self.host.middle_panel.changePanel(self.data,self.host.mpanels[0])
132
133
134 class EmptyPanel(DropCell, SimplePanel):
135 """Empty dropable panel"""
136
137 def __init__(self, host):
138 SimplePanel.__init__(self)
139 self.host = host
140 _panel = HTMLPanel("&nbsp;")
141 self.add(_panel)
142 self.setHeight('100%')
143 DropCell.__init__(self)
144
145 class MicroblogEntry(SimplePanel):
146
147 def __init__(self, body, author, timestamp):
148 SimplePanel.__init__(self)
149
150 _datetime = datetime.fromtimestamp(timestamp)
151
152 panel = HTMLPanel("<div class='mb_entry_header'><span class='mb_entry_author'>%(author)s</span> on <span class='mb_entry_timestamp'>%(timestamp)s</span></div><div class='mb_entry_body'>%(body)s</div>" %
153 {"author": author,
154 "timestamp": _datetime,
155 "body": body}
156 )
157 panel.setStyleName('microblogEntry')
158 self.add(panel)
159
160
161 class MicroblogPanel(DropCell, ScrollPanel):
162
163 def __init__(self,host, title='&nbsp;', accept_all=False):
164 """Panel used to show microblog
165 @param title: title of the panel
166 @param accept_all: if true, show every message, without filtering jids"""
167 self.host = host
168 self.accept_all = accept_all
169 title=title.replace('<','&lt;').replace('>','&gt;')
170 self.accepted_groups = []
171 _class = ['mb_panel_header']
172 if title == '&nbsp;':
173 _class.append('empty_header')
174 ScrollPanel.__init__(self)
175 self.vpanel = VerticalPanel()
176 self.vpanel.add(HTMLPanel("<div class='%s'>%s</div>" % (','.join(_class),title)))
177 self.vpanel.setWidth('100%')
178 self.setHeight('100%')
179 self.setStyleName('microblogPanel')
180 self.add(self.vpanel)
181 DropCell.__init__(self)
182
183 def addEntry(self, text, author=None, timestamp=None):
184 """Add an entry to the panel
185 @param text: main text of the entry
186 @param author: who wrote the entry
187 @param date: when the entry was written"""
188 _entry = MicroblogEntry(text, author, timestamp)
189 self.vpanel.insert(_entry,1)
190
191 def setAcceptedGroup(self, group):
192 """Set the group which can be displayed in this panel
193 @param group: string of the group, or list of string
194 """
195 if isinstance(group, list):
196 self.accepted_groups.extend(group)
197 else:
198 self.accepted_groups.append(group)
199
200 def isJidAccepted(self, jid):
201 """Tell if a jid is actepted and show in this panel
202 @param jid: jid
203 @return: True if the jid is accepted"""
204 if self.accept_all:
205 return True
206 for group in self.accepted_groups:
207 if self.host.contactPanel.isContactInGroup(group, jid):
208 return True
209 return False
210
211
212
213 class MiddlePannel(HorizontalPanel):
214
215 def __init__(self, host):
216 self.host=host
217 HorizontalPanel.__init__(self)
218 self._left = self.host.contactPanel
219 self._right = Grid(1,3)
220 self._right.setWidth('100%')
221 self._right.setHeight('100%')
222 self.add(self._left)
223 self.setCellWidth(self._left, "15%")
224 self.add(self._right)
225 self.setCellWidth(self._right, "85%")
226 self.setHeight('100%')
227
228 def changePanel(self, idx, panel):
229 print "panel:",panel
230 print "idx:",idx
231 self._right.setWidget(0,idx,panel)
232
233 class MainPanel(VerticalPanel):
234
235 def __init__(self, host):
236 self.host=host
237 VerticalPanel.__init__(self)
238
239 self.setHorizontalAlignment(HasAlignment.ALIGN_LEFT)
240 self.setVerticalAlignment(HasAlignment.ALIGN_TOP)
241
242 menu = Menu()
243 uni_box = host.uniBox
244 self.middle_panel = MiddlePannel(self.host)
245 self.middle_panel.setWidth('100%')
246
247 self.add(menu)
248 self.add(uni_box)
249 self.add(self.middle_panel)
250
251 self.setCellHeight(menu, "5%")
252 self.setCellHeight(uni_box, "5%")
253 self.setCellVerticalAlignment(uni_box, HasAlignment.ALIGN_CENTER)
254 self.setCellHorizontalAlignment(uni_box, HasAlignment.ALIGN_CENTER)
255 self.setCellHeight(self.middle_panel, "90%")
256 self.setCellWidth(self.middle_panel, "100%")
257
258 self.setWidth("100%")
259 self.setHeight("100%")