#! /usr/bin/env python
# todraw

"""A simple command-based paint program (and later, an interactive shell).
"""

try:
    from Tkinter import *
    from tkFileDialog import *
    from tkMessageBox import *
except:
    import traceback
    traceback.print_exc()
    print "Could not import Tkinter. You may need to do the following:"
    print "  Debian: Install 'python-tk'"
    print "  Gentoo: Add 'tcltk' to python USE flags"
    print "  Fedora: Install 'tkinter'"
    sys.exit()

from PIL import Image, ImageTk
import os
import time

class Application(Frame):
    def __init__(self, master=None, filename=None, width=640, height=480):
        Frame.__init__(self, master, width=width, height=height)

        self.filename = filename
        self.width = width
        self.height = height

        self.bind_all("<q>", self.onQuit)
        self.bind_all("<Escape>", self.onQuit)
        self.lblImage = None
        self.photo = None
        self.pack()
        self.lastmod = self.modTime(self.filename)
        self.setImage(self.filename)
        self.checkRefresh()

    def modTime(self, filename):
        """Return the last modification time the given filename."""
        return os.stat(filename).st_mtime
    
    def onQuit(self, evt):
        print "Quitting todraw"
        sys.exit(0)
    
    def checkRefresh(self):
        """If file was modified, refresh the image."""
        curmod = self.modTime(self.filename)
        if curmod > self.lastmod:
            self.lastmod = curmod
            self.setImage(self.filename)
        self.master.after(1000, self.checkRefresh)

    def setImage(self, filename):
        """Display the given image in the application window.
        """
        start = time.time()
        print "Displaying:", filename
        # Open the image and resize it
        image = Image.open(filename)
        image = image.resize((self.width, self.height))
        self.photo = ImageTk.PhotoImage(image, (self.width, self.height))
        # Create an pack a Label to hold the image
        if self.lblImage:
            self.lblImage.pack_forget()
        self.lblImage = Label(self, image=self.photo)
        self.lblImage.pack()
        print "Took %s to render" % (time.time() - start)



#root = Tk()
def main():
    import sys
    if len(sys.argv) < 2:
        print "Displays and refreshes a given image file"
        print "Usage: todraw FILE"
        exit(1)
    filename = sys.argv[1]
    root.title("todraw")
    #root.withdraw()
    app = Application(root, filename)
    app.mainloop()
    #console()


def console():
    """Open an interactive Python console and import the drawing module."""
    import code
    console = code.InteractiveConsole()
    console.push('from libtovid.render import drawing')
    console.push('from libtovid.render.drawing import *')

    banner = "todraw python shell\n"\
             "Type help(drawing) or help(Drawing) for assistance\n"\
             "Press Ctrl-D to quit"
    console.interact(banner)



### --------------------------------------------------------------------
### Entry point
### --------------------------------------------------------------------

if __name__ == '__main__':
    #main()
    console()
