recent_files.py :  » GUI » Kodos » kodos-2.4.9 » modules » 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 » GUI » Kodos 
Kodos » kodos 2.4.9 » modules » recent_files.py

from util import getHomeDirectory
import os
import string
from qt import *
import xpm

MAX_SIZE = 50 # max number of files to retain

class RecentFiles:
    def __init__(self, parent, numShown=5, debug=None):
        self.parent = parent
        self.numShown = int(numShown)
        self.debug = debug
        self.filename = getHomeDirectory() + os.sep + ".kodos" + os.sep + "recent_files"
        self.__recent_files = []
        self.__indecies = []
        self.load()


    def load(self):
        try:
            fp = open(self.filename, "r")
            self.__recent_files = map(string.strip, fp.readlines())
        except Exception, e:
            #print "Warning: ", str(e)
            return
        
        if self.debug: print "recent_files:", self.__recent_files
        self.addToMenu()


    def save(self):
        # truncate list if necessary
        self.__recent_files = self.__recent_files[:MAX_SIZE]
        try:
            fp = open(self.filename, "w")
            for f in self.__recent_files:
                fp.write("%s\n" % f)
            fp.close()
        except Exception, e:
            print "Could not save recent file list", str(e)
            

    def add(self, filename):
        try:
            self.__recent_files.remove(filename)
        except:
            pass

        self.__recent_files.insert(0, filename)
        self.save()
        self.addToMenu()


    def clearMenu(self):
        # clear each menu entry...
        for idx in self.__indecies:
            self.parent.fileMenu.removeItem(idx)

        # clear list of menu entry indecies
        self.__indecies = []

        
    def addToMenu(self, clear=1):
        if clear: self.clearMenu()

        # add applicable items to menu
        num = min(self.numShown, len(self.__recent_files))
        for i in range(num):
            filename = self.__recent_files[i]
            idx = self.parent.fileMenu.insertItem(
                QIconSet(QPixmap(xpm.newIcon)),
                filename)

            self.__indecies.insert(0, idx)
        

    def setNumShown(self, numShown):
        ns = int(numShown)
        if ns == self.numShown: return

        # clear menu of size X then add entries of size Y
        self.clearMenu()
        self.numShown = ns
        self.addToMenu(0)


    def isRecentFile(self, menuid):
        return menuid in self.__indecies


    def move(self, filename, menuid):
        # fix me....
        menu = self.parent.fileMenu
        idx = menu.indexOf(self.__indecies[0])
        menu.removeItem(menuid)
        menu.insertItem(QIconSet(QPixmap(xpm.newIcon)),
                        filename,
                        -1,
                        idx)
        try:
            self.__recent_files.remove(filename)
        except:
            pass
        self.__indecies.insert(0, filename)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.