SF.net SVN: fclient: [34] trunk/fclient/fclient_widgets/peer_widget.py
Status: Pre-Alpha
Brought to you by:
jurner
From: <jU...@us...> - 2007-11-02 08:28:16
|
Revision: 34 http://fclient.svn.sourceforge.net/fclient/?rev=34&view=rev Author: jUrner Date: 2007-11-02 01:28:13 -0700 (Fri, 02 Nov 2007) Log Message: ----------- continued implementig peers widget Modified Paths: -------------- trunk/fclient/fclient_widgets/peer_widget.py Modified: trunk/fclient/fclient_widgets/peer_widget.py =================================================================== --- trunk/fclient/fclient_widgets/peer_widget.py 2007-11-02 08:27:25 UTC (rev 33) +++ trunk/fclient/fclient_widgets/peer_widget.py 2007-11-02 08:28:13 UTC (rev 34) @@ -1,4 +1,4 @@ -"""Sketch for a widget handling node peers +"""Sketch for a widget handling peer nodes """ @@ -29,20 +29,28 @@ # #*************************************************************************************************** class PeerWidget(QtGui.QTreeWidget): + """ + """ HeaderIndexStatus = 0 HeaderIndexName = 1 HeaderIndexLastConnected = 2 HeaderIndexNotes = 3 + + ShowPeersOpennet = 1 + ShowPeersDarknet = 2 + ShowPeersAll = ShowPeersOpennet | ShowPeersDarknet def __init__(self, parent, cfg=None): - + """ + """ self._cfg = cfg self._fcpClient = None self._fcpEvents = None self._isCreated = False self._peers = {} # identity --> item + self._showPeersFlags = self.ShowPeersAll QtGui.QWidget.__init__(self, parent) @@ -66,23 +74,49 @@ header.setResizeMode(header.ResizeToContents) header.setStretchLastSection(True) + self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) + self.connect( + self, + QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), + self.handleContextMenu + ) def showEvent(self, event): + """ + """ if not self._isCreated: self._isCreated = True self._cfg.connectFcpNode(self.handleClientConnected) - + ############################################################# + ## + ## handlers for tree events + ## + ############################################################# + def handleContextMenu(self, pt): + """ + """ + pt = self.viewport().mapToGlobal(pt) + m = QtGui.QMenu() + self.populateMenu(m) + m.exec_(pt) + + ############################################################# + ## + ## handlers for Fcp events + ## + ############################################################# def handleClientConnected(self, event, params): + """ + """ self._fcpEvents = ( - #(self._cfg.fcpClient.EventClientConnected, self.handleClientConnected), - (self._cfg.fcpClient.EventClientDisconnected, self.handleClientDisconnected), + (self._cfg.fcpClient.events.ClientDisconnected, self.handleClientDisconnected), - (self._cfg.fcpClient.EventPeer, self.handlePeer), - (self._cfg.fcpClient.EventEndListPeers, self.handleEndListPeers), - (self._cfg.fcpClient.EventPeerRemoved, self.handlePeerRemoved), - (self._cfg.fcpClient.EventUnknownNodeIdentifier, self.handleUnknownNodeIdentifier), + (self._cfg.fcpClient.events.Peer, self.handlePeer), + (self._cfg.fcpClient.events.EndListPeers, self.handleEndListPeers), + (self._cfg.fcpClient.events.PeerRemoved, self.handlePeerRemoved), + (self._cfg.fcpClient.events.UnknownNodeIdentifier, self.handleUnknownNodeIdentifier), ) # take care to not connect twice @@ -95,15 +129,16 @@ self._cfg.fcpClient.listPeers() - def handleClientDisconnected(self, event, params): - pass + """ + """ def handlePeer(self, event, params): - + """ + """ timeLastConnected = params.get('metadata.timeLastConnected', None) - timeLastConnected = self.timeToTimeDelta(timeLastConnected) + timeLastConnected = self.formatTimeDelta(timeLastConnected) identity = params['identity'] item = self._peers.get(identity, None) @@ -117,48 +152,226 @@ ] item = QtGui.QTreeWidgetItem(self, itemStrings) item.setData(0, QtCore.Qt.UserRole, QtCore.QVariant(identity)) - self._peers[identity] = item + self._peers[identity] = (item, params) # update item else: item.settext(self.HeaderIndexLastConnected, timeLastConnected) - - + self._peers[identity] = (item, params) + def handleEndListPeers(self, event, params): - pass + """ + """ - def handlePeerRemoved(self, peer): - pass + """ + """ def handleUnknownNodeIdentifier(self, params): - pass + """ + """ - - - def timeToTimeDelta(self, t): + ####################################################### + ## + ## methods + ## + ####################################################### + def close(self): + """Closes the widget + @note: make shure to call close when done with the widget + """ + # disconnect Fcp events + for event, observer in self._fcpEvents: + if not observer in event: + event -= observer + + + def formatTimeDelta(self, t): + """Formats a Fcp time as time delta relative to to now + @param t: (str) time to format + @return: (str) human readably formated time delta (something like 2.5m or 10.2d) or '' + """ + result = '' try: t = self._cfg.fcpClient.pythonTime(t) - except: - t = self.trUtf8('Unknown') + except: pass else: - if t == 0: - t = self.trUtf8('Never') - else: - t = numbers.format_time_delta(t, time.time()) - return t + if t > 0: + result = numbers.format_time_delta(t, time.time()) + return result - - + def identityFromItem(self, item): + """Returns the peer identity an item is associated to + @param item: treeItem + @return: (str) peer identity + """ v = item.data(0, QtCore.Qt.UserRole) return str(v.toString()) + def peers(self): + """Returns all currently known peers + @return: (dict) identity --> (treeItem, peer) + """ + return self._peers + + def populateMenu(self, menu): + """Populates a menu with peer actions + @return: (list) of all actions added to the menu + """ + actions = [] + + actions.append(ShowPeersMenu(menu, self)) + + return actions + + + def showPeers(self): + """Returns the current ShowPeers* flags set + @return: (int) flags + """ + return self._showPeersFlags + + + def setShowPeers(self, flags): + """Shows or hides peers + @param flags: (int) one or more ShowPeers* flags + @return: always None + """ + self.setUpdatesEnabled(False) + + for item, peer in self.peers().values(): + if peer['opennet'] == self._cfg.fcpClient.FcpTrue: + item.setHidden(not flags & self.ShowPeersOpennet) + else: + item.setHidden(not flags & self.ShowPeersDarknet) + + self.setUpdatesEnabled(True) + self._showPeersFlags = flags + +#************************************************************************************* +# menu actions +#************************************************************************************* +class ShowPeersMenu(QtGui.QMenu): + """ + """ + + def __init__(self, menu, tree): + """ + """ + QtGui.QMenu.__init__(self, menu) + self.setTitle(self.trUtf8('Show peers')) + menu.addMenu(self) + + self.tree = tree + + self.group = QtGui.QActionGroup(self) + self.group.setExclusive(True) + showPeers = ( + ('Darknet', self.trUtf8('Darknet'), ShowPeersDarknetAction), + ('Opennet', self.trUtf8('Opennet'), ShowPeersOpennetAction), + ('OpenAndDarknet', self.trUtf8('Open and darknet'), ShowOpenAndDarknetPeersAction), + ) + + for name, menuText, act in showPeers: + act = act(name, menuText, self.group, tree) + act.setCheckable(True) + self.group.addAction(act) + self.addAction(act) + + self.connect( + self, + QtCore.SIGNAL('aboutToShow()'), + self.handleAboutToShow + ) + + + def handleAboutToShow(self): + """ + """ + acts = { # flag --> actionName + self.tree.ShowPeersAll: 'OpenAndDarknet', + self.tree.ShowPeersOpennet: 'Opennet', + self.tree.ShowPeersDarknet:'Darknet', + } + + flags = self.tree.showPeers() + for flag, actionName in acts.items(): + if flags & flag == flags: + + for act in self.group.actions(): + if str(act.objectName()) == actionName: + act.setChecked(True) + break + else: + raise ValueError('No action [name] found ???') + break + else: + raise ValueError('No action [flag] found ???') + + +class ShowPeersDarknetAction(QtGui.QAction): + """ + """ + + def __init__(self, name, text, parent, tree): + """ + """ + QtGui.QAction.__init__(self, text, parent) + self.setObjectName(name) + + #self.setShortcuts( + # [QtGui.QKeySequence(self.trUtf8('Ctrl+A'))] + # ) + self.tree = tree + self.connect( + self, + QtCore.SIGNAL('triggered()'), + self.__call__ + ) + + + def __call__(self): + """ + """ + flags = self.tree.showPeers() + flags |= self.tree.ShowPeersDarknet + flags &= ~self.tree.ShowPeersOpennet + self.tree.setShowPeers(flags) + + +class ShowPeersOpennetAction(ShowPeersDarknetAction): + """ + """ + + def __call__(self): + """ + """ + flags = self.tree.showPeers() + flags &= ~self.tree.ShowPeersDarknet + flags |= self.tree.ShowPeersOpennet + self.tree.setShowPeers(flags) + + +class ShowOpenAndDarknetPeersAction(ShowPeersDarknetAction): + """ + """ + + def __call__(self): + """ + """ + flags = self.tree.showPeers() + flags |= self.tree.ShowPeersDarknet + flags |= self.tree.ShowPeersOpennet + self.tree.setShowPeers(flags) + + + '''######################################################## Sample Peer message >> Peer @@ -203,7 +416,14 @@ import sys app = QtGui.QApplication(sys.argv) - w = PeerWidget(None) + w = QtGui.QMainWindow() + peers = PeerWidget(None) + w.setCentralWidget(peers) + + m = w.menuBar() + m1 = m.addMenu('Peers') + peers.populateMenu(m1) + w.show() res = app.exec_() sys.exit(res) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |