⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list.py

📁 ABC-win32-v3.1 一个P2P软源代码
💻 PY
📖 第 1 页 / 共 2 页
字号:
            width = item[self.WIDTH]
            changed = self.utility.config.Write(self.list.prefix + str(colid) + "_width", width)
            if changed:
                overallchange = True

        # APPLY on-the-fly
        if overallchange:
            self.utility.config.Flush()
            self.utility.lang.flush()
            self.list.updateColumns()


################################################################
#
# Class: ColumnManager
#
# Keep track of the settings for order and appearance of
# columns in a ManagedList
#
################################################################
class ColumnManager:
    def __init__(self, list):
        self.utility = list.utility
        
        self.minid = list.minid
        self.maxid = list.maxid
        self.prefix = list.prefix
        
        self.exclude = list.exclude

        self.active = []
        
        self.getColumnData()

    # Method used to compare two elements of self.active
    def compareRank(self, a, b):
        if a[1] < b[1]:
            return -1
        if a[1] > b[1]:
            return 1
        else:
            return 0
        
    def getNumCol(self):
        return len(self.active)
        
    def getRankfromID(self, colid):
        return self.utility.config.Read(self.prefix + str(colid) + "_rank", "int")

    def getIDfromRank(self, rankid):
        return self.active[rankid][0]
    
    def getTextfromRank(self, rankid):
        colid = self.active[rankid][0]
        return self.utility.lang.get(self.prefix + str(colid) + "_text")
    
    def getValuefromRank(self, rankid):
        colid = self.active[rankid][0]
        return self.utility.config.Read(self.prefix + str(colid) + "_width", "int")

    def getColumnData(self):
        self.active = []

        # Get the list of active columns
        for colid in range(self.minid, self.maxid):
            rank = self.utility.config.Read(self.prefix + str(colid) + "_rank", "int")
            
            if (rank < 0 or colid in self.exclude):
                self.utility.config.Write(self.prefix + str(colid) + "_rank", -1)
            elif (rank > -1):
                self.active.append([colid, rank])
                
        # Sort the columns by rank
        self.active.sort(self.compareRank)
        
        # Make sure that the columns are in an order that makes sense
        # (i.e.: if we have a config with IDs of 4, 99, 2000 then
        #        we'll convert that to 0, 1, 2)
        for i in range(len(self.active)):
            colid = self.active[i][0]
            rank = i
            self.active[i][1] = rank
            self.utility.config.Write(self.prefix + str(colid) + "_rank", rank)
        self.utility.config.Flush()


################################################################
#
# Class: ManagedList
#
# An extension of wx.ListCtrl that keeps track of column
# settings in a unified manner.
#
################################################################       
class ManagedList(wx.ListCtrl):
    def __init__(self, parent, style, prefix, minid, maxid, exclude = [], rightalign = [], centeralign = []):        
        wx.ListCtrl.__init__(self, parent, -1, style = style)
        
        self.parent = parent
        
        self.prefix = prefix
        self.minid = minid
        self.maxid = maxid
        self.exclude = exclude
        
        self.rightalign = rightalign
        self.centeralign = centeralign
        
        self.utility = parent.utility
        
        self.columns = ColumnManager(self)
        
        self.Bind(wx.EVT_LIST_COL_END_DRAG, self.OnResizeColumn)
        self.Bind(wx.EVT_LIST_COL_RIGHT_CLICK, self.OnColRightClick)
        
        self.loadColumns()
        
        self.loadFont()
        
        # Add to the list of ManagedList objects
        self.utility.lists[self] = True
        
    def loadFont(self):
        # Get font information
        fontinfo = self.utility.config.Read('listfont', "bencode-fontinfo")
        font = self.utility.getFontFromInfo(fontinfo)

        # Only change if we've gotten an acceptable font
        if font.Ok():
            # Jump to the top of the list
            self.EnsureVisible(0)
            # Change the font
            self.SetFont(font)
            # Refresh to make the change visable
            self.Refresh()

    def loadColumns(self):
        # Delete Old Columns (if they exist)
        #################################################
        numcol = self.GetColumnCount()
        for i in range(numcol):
            self.DeleteColumn(0)

        # Read status display
        ####################################
        
        for rank in range(self.columns.getNumCol()):
            colid = self.columns.getIDfromRank(rank)
            if colid in self.rightalign:
                style = wx.LIST_FORMAT_RIGHT
            elif colid in self.centeralign:
                style = wx.LIST_FORMAT_CENTER
            else:
                style = wx.LIST_FORMAT_LEFT
            text = self.columns.getTextfromRank(rank)
            width = self.columns.getValuefromRank(rank)
            # Don't allow a column to have a size of 0
            if width == 0:
                width = -1
            self.InsertColumn(rank, text, style, width)
        
        # Save the width of the column that was just resized
    def OnResizeColumn(self, event):
        if self.utility.config.Read('savecolumnwidth', "boolean"):
            rank = event.GetColumn()
            width = self.GetColumnWidth(rank)
            colid = self.columns.getIDfromRank(rank)
            self.utility.config.Write(self.prefix + str(colid) + "_width", width)
            self.utility.config.Flush()
            
    # Create a list of columns that are active/inactive
    def OnColRightClick(self, event):
        if not hasattr(self, "columnpopup"):
            self.makeColumnPopup()
            
        # Check off columns for all that are currently active
        for colid in range(self.minid, self.maxid):
            if colid in self.exclude:
                continue
            
            if self.utility.config.Read(self.prefix + str(colid) + "_rank", "int") > -1:
                self.columnpopup.Check(777 + colid, True)
            else:
                self.columnpopup.Check(777 + colid, False)
        
        self.lastcolumnselected = event.GetColumn()
               
        self.PopupMenu(self.columnpopup, event.GetPosition())
        
    def makeColumnPopup(self):
        self.columnpopup = wx.Menu()
        
        for colid in range(self.minid, self.maxid):
            if colid in self.exclude:
                continue
            
            text = self.utility.lang.get(self.prefix + str(colid) + '_text')
            self.columnpopup.Append(777 + colid, text, text, wx.ITEM_CHECK)
            
        self.columnpopup.AppendSeparator()
        
        self.utility.makePopup(self.columnpopup, self.onColumnDialog, 'more')

        startid = 777 + self.minid
        endid = 777 + (self.maxid - 1)

        self.Bind(wx.EVT_MENU, self.onSelectColumn, id=startid, id2=endid)
        
    def onColumnDialog(self, event = None):
        dialog = ColumnsDialog(self)
        dialog.ShowModal()
        dialog.Destroy()

    def onSelectColumn(self, event):
        eventid = event.GetId()
        colid = eventid - 777
        oldrank = self.utility.config.Read(self.prefix + str(colid) + "_rank", "int")

        if oldrank > -1:
            # Column was deselected, don't show it now
            # (update ranks for the rest of the columns that appeared after it)
            for i in range (self.minid, self.maxid):
                temprank = self.utility.config.Read(self.prefix + str(i) + "_rank", "int")
                if (i == colid):
                    self.utility.config.Write(self.prefix + str(i) + "_rank", -1)
                elif (temprank > oldrank):
                    self.utility.config.Write(self.prefix + str(i) + "_rank", temprank - 1)
                else:
                    self.utility.config.Write(self.prefix + str(i) + "_rank", temprank)
        else:
            # Column was selected, need to show it
            # Put it after the closest column
            if hasattr(self, 'lastcolumnselected'):
                newrank = self.lastcolumnselected + 1
            # (just tack it on the end of the display)
            else:
                newrank = self.GetColumnCount()
            
            for i in range (self.minid, self.maxid):
                temprank = self.utility.config.Read(self.prefix + str(i) + "_rank", "int")
                if (i == colid):
                    self.utility.config.Write(self.prefix + str(i) + "_rank", newrank)
                elif (temprank >= newrank):
                    self.utility.config.Write(self.prefix + str(i) + "_rank", temprank + 1)
                else:
                    self.utility.config.Write(self.prefix + str(i) + "_rank", temprank)
        self.utility.config.Flush()
        
        # Write changes to the config file and refresh the display
        self.updateColumns()
    
    def updateColumns(self):
        self.columns.getColumnData()
        self.loadColumns()
        
        self.parent.updateColumns(force = True)
    
    def getSelected(self, firstitemonly = False, reverse = False):
        selected = []
        index = self.GetNextItem(-1, wx.LIST_NEXT_ALL, wx.LIST_STATE_SELECTED)
        while index != -1:
            selected.append(index)
            if (firstitemonly):
                return selected
            index = self.GetNextItem(index, wx.LIST_NEXT_ALL, wx.LIST_STATE_SELECTED)
        selected.sort()
        if reverse:
            selected.reverse()
        return selected
        
    def selectAll(self):
        for index in range(self.GetItemCount()):
            self.Select(index)
    
    def invertSelection(self):
        for index in range(self.GetItemCount()):
            self.SetItemState(index, 4 - self.GetItemState(index, wx.LIST_STATE_SELECTED), wx.LIST_STATE_SELECTED)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -