from PySide import QtGui, QtCore
from unipath import Path

from dialogs import OpenDialog
from editor import Editor
from preview import Preview
from tree import Tree


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        
        splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
        self.tree =  Tree()
        self.editor = Editor()
        self.preview = Preview()
        self.setCentralWidget(splitter)        
        splitter.addWidget(self.tree)
        splitter.addWidget(self.editor)
        splitter.addWidget(self.preview)

        self.setWindowTitle("RST Previewer")
        self.showMaximized()
        
        self.setupActions()

    def setupActions(self):
        """
            Set up the top menu actions and keyboard shortcuts.
        """
        self.fileMenu = self.menuBar().addMenu("&File")

        self.openAction = QtGui.QAction(
                                QtGui.QIcon(":/images/open.png"), 
                                "&Open File", 
                                self, 
                                shortcut="Ctrl+O",
                                statusTip="Open File", 
                                triggered=self.openFile
                            )
        self.fileMenu.addAction(self.openAction)

        self.openFolderAction = QtGui.QAction(
                                QtGui.QIcon(":/images/open.png"), 
                                "Open &Folder", 
                                self, 
                                shortcut="Ctrl+F",
                                statusTip="Open Folder", 
                                triggered=self.openFolder
                            )
        self.fileMenu.addAction(self.openFolderAction)

    def openFile(self, path=None):
        """ 
            Ask the user to open a file via the Open File dialog.
            Then open it in the tree, editor, and HTML preview windows.
        """
        if not path:
            dialog = OpenDialog()
            dialog.set_folders_only(False)
            path = dialog.getOpenFileName(self, "Open File",
                    '', "ReStructuredText Files (*.rst)")
    
        if path:
            inFile = QtCore.QFile(path[0])
            if inFile.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text):
                text = inFile.readAll()
    
                try:
                    # Python v3.
                    text = str(text, encoding='ascii')
                except TypeError:
                    # Python v2.
                    text = str(text)
    
                self.editor.setPlainText(text)
                
                # Load the directory containing the file into the tree.
                file_path = Path(path[0])
                tree_dir = file_path.parent.absolute()
                self.tree.load_from_path(tree_dir)
                
                # Load corresponding HTML file from pre-built Sphinx docs
                file_stem = str(file_path.stem)
                html_str = "_build/html/{0}.html".format(file_stem)
                output_html_path = Path(file_path.parent, html_str).absolute()
                self.preview.load_html(output_html_path)
    
    def openFolder(self, path=None):
        """ 
            Ask the user to open a folder (directory) via 
            the Open Folder dialog. Then open it in the tree, 
            editor, and HTML preview windows.
        """
        if not path:
            dialog = OpenDialog()
            dialog.set_folders_only(True)
            path = dialog.getExistingDirectory(self, "Open Folder", '')
        
        if path:                
            # Load the folder into the tree.
            self.tree.load_from_path(path)
            
            # TODO: Load index.rst in the editor
            #       or the first .rst file
            
            # TODO: Load index.html in the preview pane
            #       or the first .html file

@@ -1403,32 +1403,36 @@
                 
+    
 QtGui.QIcon(%22:/i
@@ -1442,32 +1442,36 @@
 es/open.png%22), %0A
+    
                 
@@ -1527,32 +1527,36 @@
                 
+    
 self, %0A         
@@ -1570,32 +1570,36 @@
                 
+   
+ 
 shortcut=%22Ctrl+F
@@ -1601,16 +1601,20 @@
 trl+F%22,%0A
+    
         
@@ -1687,32 +1687,36 @@
                 
+   
+ 
 triggered=self.o
@@ -1725,16 +1725,20 @@
 nFolder%0A
+    
         
@@ -1809,24 +1809,462 @@
 olderAction)
+%0A        %0A        self.quitAction = QtGui.QAction(%0A                            QtGui.QIcon(':/images/save.png'), %0A                            %22&Quit RST Previewer%22, %0A                            self,%0A                            shortcut=%22Ctrl+Q%22,%0A                            statusTip=%22Quit RST Previewer%22,%0A                            triggered=self.close%0A                        )        %0A        self.fileMenu.addAction(self.quitAction)
 %0A%0A    def op

Add quit action (not working)