common.py :  » Network » Torrent-Swapper » swapper » Swapper » Dialogs » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Torrent Swapper 
Torrent Swapper » swapper » Swapper » Dialogs » common.py
# Written by Jie Yang
# see LICENSE.txt for license information

import wx
from ABC.GUI.list import ManagedList

def sort_dictlist(dict_list, key, order='increase'):
    
    aux = [(dict_list[i][key], i) for i in xrange(len(dict_list))]
    try:
        aux.sort()
    except UnicodeDecodeError,e:
        # Arno: there are unicode strings and non-unicode strings in the data.
        # One of the non-unicode strings contains data that cannot be
        # decoded into a unicode string for comparison to the other unicode
        # strings by the default 'ascii' codec. See
        # http://downloads.egenix.com/python/Unicode-EPC2002-Talk.pdf
        #
        # This is a legacy problem, as the new code will store everything as
        # unicode in the database. I therefore chose a dirty solution, don't
        # sort 
        pass
    if order == 'decrease' or order == 1:    # 0 - increase, 1 - decrease
        aux.reverse()
    return [dict_list[i] for x, i in aux]


class CommonSwapperList(ManagedList):
    """ 
    0. Give a unique prefix
    1. IDs in rightalign and centeralign must be set in Utility.constants;
    2. Column labels must be set in the language file;
    3. To set default values, modify Utility.utility.setupConfig()
    """
    
    def __init__(self, parent, style, prefix, minid, maxid, exclude = [], rightalign = [], centeralign = []):
        self.parent = parent
        self.utility = parent.utility
        self.prefix = prefix
        ManagedList.__init__(self, parent, style, prefix, minid, maxid, exclude, rightalign, centeralign)
        
        self.data = []
        self.lastcolumnsorted, self.reversesort = self.columns.getSortedColumn()
        self.num = self.getMaxNum()    # max num of lines to show

        self.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.OnRightClick)
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnActivated)
        self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick)
        
        #self.loadList()
                    
    def getMaxNum(self):
        return self.utility.config.Read(self.prefix + "_num", "int")
            
    def OnRightClick(self, event):
        print "right click", self.getSelectedItems()
    
    def getSelectedItems(self):
        item = -1
        itemList = []
        while 1:
            item = self.GetNextItem(item,wx.LIST_NEXT_ALL,wx.LIST_STATE_SELECTED)
            if item == -1:
                break
            else:
                itemList.append(item)
        itemList.sort()
        return itemList
    
    def OnActivated(self, event):
        self.curr_idx = event.m_itemIndex
        #print "actived", self.curr_idx

    def OnColClick(self, event):
        col = event.m_col
        active_columns = self.columns.active
        if col >= len(active_columns) or col < 0:
            return
        else:
            col = active_columns[col][0]    # the real position
        if self.lastcolumnsorted == col:
            self.reversesort = 1 - self.reversesort
        else:
            self.reversesort = 0
        self.lastcolumnsorted = col
        self.columns.writeSortedColumn(self.lastcolumnsorted, self.reversesort)
        self.loadList(reload=False, sorted=True)
        
    def reloadData(self):
        raise

    def getText(self, data, row, col):
        raise
        
    def loadList(self, reload=True, sorted=True):
        active_columns = self.columns.active
        if not active_columns:
            return
        
        if reload:
            self.reloadData()
        
        if sorted:
            key = self.keys[self.lastcolumnsorted]
            self.data = sort_dictlist(self.data, key, self.reversesort)
            
        num = len(self.data)
        if self.num > 0 and self.num < num:
            num = self.num
            
        self.DeleteAllItems() 
        
        first_col = active_columns[0][0]
        for i in xrange(num):
            self.InsertStringItem(i, self.getText(self.data, i, first_col))
            for col,rank in active_columns[1:]:
                txt = self.getText(self.data, i, col)
                self.SetStringItem(i, rank, txt)
        self.Show(True)
    

class MainWindow(wx.Frame):
    def __init__(self,parent,id, title):
        wx.Frame.__init__(self,parent,wx.ID_ANY,title,
                          style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
        self.control = CommonSwapperList(self, wx.Size(500, 100))
        self.Fit()
        self.Show(True)

if __name__ == '__main__':
    app = wx.App()
    frame=MainWindow(None,-1,'Demo')
    app.MainLoop()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.