# get Tkinter
from Tkinter import *
import Tkinter
import tkFileDialog
import webbrowser

from ContentWindow import *

class MainFrameDraw:
    
    saved = "" # the filename used for saving an LED configuration
    
    #the URLs to important project-related information in the about page
    project = "http://teaching.up.edu/srdesign/AY13/PortlandCityGrill/index.php"
    src = "https://code.google.com/p/senior-design-lightbox/"
    
    def new(self):
        """This function is called when the user selects 'New' in the file menu.
           It is used to clear the LED grid in the GUI as well as the device by
           setting all the LED duty cycles to 0"""

        self.saved = ""
        self.content.setToZero() # set actual LEDs off
        root.title("LightBox Application")
        self.content.ledGrid.clearValues() # now reset labels/numbers in the GUI

    def load(self):
        """This function is called when the user selects 'Open' in the file menu.
           It is used to open a text file with defined LED names and values and 
           load them into the GUI"""

        self.fileOpt = options = {}
        options['defaultextension'] = '.txt'
        options['filetypes'] = [('All files', '.*'), ('Text files', '.txt')]
        filename = tkFileDialog.askopenfilename(**self.fileOpt)
        if filename:
            print filename
            self.saved = filename
            root.title("LightBox Application - " + filename)
            self.content.ledGrid.loadGrid(filename)
        

    def save(self):
        """This function is called when the user selects 'Save' in the file menu.
           It is used to save the current LED configuration in the GUI to the currently
           loaded text file"""

        if self.saved == "" or self.saved is  None:
            self.saveAs()
        # if we have a file already loaded/saved before,
        # just save again using that name.
        else:
            root.title("LightBox Application - " + self.saved)
            self.content.ledGrid.saveGrid(self.saved) 

    def saveAs(self):
        """This function is called when the user selects 'Save As' in the file menu.
           It is used to save the current LED configuration in the GUI to a specified
           text file"""

        self.fileOpt = options = {}
        options['defaultextension'] = '.txt'
        options['filetypes'] = [('All files', '.*'), ('Text files', '.txt')]
        options['initialfile'] = self.saved
        filename = tkFileDialog.asksaveasfilename(**self.fileOpt)
        if filename:
            print filename
            self.saved = filename
            root.title("LightBox Application - " + filename) 
            self.content.ledGrid.saveGrid(filename)
        
        
    # __init__: the initializing function
    #       sets up the MainFrame widget
    #       to be the main GUI functionality
    #   parameters:
    #       self: the required hidden parameter
    #       master: the widget containing this widget
    def __init__(self, master):

        # set up the menubar along the top of the application
        # with file and help menus
        menubar = Menu(master)

        # place a ContentWindow widget in the frame below the menu
        self.content = ContentWindowDraw(master)
        
        # file menu
        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label="New", command=self.new)
        filemenu.add_command(label="Open", command=self.load)
        filemenu.add_command(label="Save", command=self.save)
        filemenu.add_command(label="Save as...", command=self.saveAs)
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=self.content.quitProgram)
        menubar.add_cascade(label="File", menu=filemenu)

        # preset menu
        presetMenu = Menu(menubar, tearoff=0)
        
        # The goal is to insert presets in a paired manner, so the name always
        # corresponds to the correct file. 
        presetNames = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet", "White" ]
        
        # adding presets to the preset menu
        presetMenu.add_command(label=presetNames[0], command=lambda: self.loadRedPreset())
        presetMenu.add_command(label=presetNames[1], command=lambda: self.loadOrangePreset())
        presetMenu.add_command(label=presetNames[2], command=lambda: self.loadYellowPreset())
        presetMenu.add_command(label=presetNames[3], command=lambda: self.loadGreenPreset())
        presetMenu.add_command(label=presetNames[4], command=lambda: self.loadBluePreset())
        presetMenu.add_command(label=presetNames[5], command=lambda: self.loadIndigoPreset())
        presetMenu.add_command(label=presetNames[6], command=lambda: self.loadVioletPreset())
        presetMenu.add_command(label=presetNames[7], command=lambda: self.loadWhitePreset())

        menubar.add_cascade(label="Presets", menu=presetMenu)

        # help menu
        helpmenu = Menu(menubar, tearoff=0)
        helpmenu.add_command(label="About Project LightBox", command=self.drawAbout)
        menubar.add_cascade(label="Help", menu=helpmenu)
        
        # actually placing the menubar on the GUI
        master.config(menu=menubar)

    # redrawMainFrame: redraws the ContentWindow widget
    #   parameters:
    #       self: the required hidden parameter
    #       squareNum: the number of the square to be redrawn
    #       naming: the name to be drawn on the LED
    #       setting: the setting to be drawn on the LED
    def redrawMainFrame(self, squareNum, naming, setting):
        self.content.redrawContent(squareNum, naming, setting)


    def drawAbout(self):
        """This function sets up the About window within the 'Help' tab in the menu bar at the
           top of the application"""

        top = Toplevel(master=None, pady=20, padx=40, height=100, width=100)
        top.title("About Project LightBox")
        
        # this about message is a modified version of what is on the front 
        # page of our senior design website. 
        aboutMsg = """
    This is Project LightBox. 
    
    It was designed and built by Team Portland City Grill. 
    
    The LightBox is an enclosure with programmable LEDs inside. 
    The RGB lightsshine through a plate, similar to the manner that white light 
    shines through plates used to view X-ray negatives. Team PCG delivered 
    the hardware and the software for this project at University of Portland's Founder's Day, 2013.

    
    Team Members:
    Benjamin Co (Fall Team Lead)
    Garrett Gustafson (Spring Team Lead)
    Wendelin Mueller
    Julia Weimar-East
        
    Faculty Advisers:
    Primary: Andrew M. Nuxoll, PhD
    Secondary: Tanya L. Crenshaw, PhD
    
    Client:
    Kevin Cantrell, PhD: Chemistry Department Chair, University of Portland
    
    Industry Adviser:
    Andy McConnell
    """
        
        info = Message(top, text=aboutMsg)
        info.pack()
        
        close = Button(top, text="Close", command=top.destroy, padx=20) # close button
        # button to link to our senior design project page
        website = Button(top, text="Visit Website", command=self.visitProject, padx = 20)
        # button to link to our project's source code 
        code = Button(top, text="View Source Code", command=self.visitSrc, padx=20)
        
        # placing the buttons
        website.pack(side=LEFT)
        code.pack(side=LEFT)
        close.pack(side=RIGHT)
        
    def visitProject(self):
        """this function opens a web browser for our website"""

        webbrowser.open_new_tab(self.project)
        
    def visitSrc(self):
        """this function opens a web browser for our source code at Google code"""

        webbrowser.open_new_tab(self.src)

    #functions for loading the LED grid in the GUI with preset values
    def loadRedPreset(self):
        """this function is called when 'Red' is selected from the 'Presets' tab in the top
           menu bar and sets the GUI LED grid to a red configuration"""
        self.content.ledGrid.loadPreset("Red")

    def loadOrangePreset(self):
        """this function is called when 'Orange' is selected from the 'Presets' tab in the top
           menu bar and sets the GUI LED grid to a orange configuration"""

        self.content.ledGrid.loadPreset("Orange")

    def loadYellowPreset(self):
        """this function is called when 'Yellow' is selected from the 'Presets' tab in the top
           menu bar and sets the GUI LED grid to a yellow configuration"""

        self.content.ledGrid.loadPreset("Yellow")

    def loadGreenPreset(self):
        """this function is called when 'Green' is selected from the 'Presets' tab in the top
           menu bar and sets the GUI LED grid to a green configuration"""

        self.content.ledGrid.loadPreset("Green")

    def loadBluePreset(self):
        """this function is called when 'Blue' is selected from the 'Presets' tab in the top
           menu bar and sets the GUI LED grid to a blue configuration"""

        self.content.ledGrid.loadPreset("Blue")

    def loadIndigoPreset(self):
        """this function is called when 'Indigo' is selected from the 'Presets' tab in the top
           menu bar and sets the GUI LED grid to a indigo configuration"""

        self.content.ledGrid.loadPreset("Indigo")

    def loadVioletPreset(self):
        """this function is called when 'Violet' is selected from the 'Presets' tab in the top
           menu bar and sets the GUI LED grid to a violet configuration"""

        self.content.ledGrid.loadPreset("Violet")
            
    def loadWhitePreset(self):
        """this function is called when 'White' is selected from the 'Presets' tab in the top
           menu bar and sets the GUI LED grid to a white configuration"""

        self.content.ledGrid.loadPreset("White")


#make the GUI appear and run
root = Tkinter.Tk()
root.title("LightBox Application")

        
thismain = MainFrameDraw(root)
root.mainloop()
