#!/usr/bin/python
import os, sys, PyQt4, getopt
from PyQt4 import QtCore, QtGui


from OptionsGUI import OptionsGUI
from GenSyntax import *
from ActionSyntax import *
from YamlData import *
from GetPotData import *
from InputFileTreeWidget import *
from InputFileTextbox import *

from readInputFile import readInputFile, GPNode

try:
  _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
  _fromUtf8 = lambda s: s

class InputFileWidget(QtGui.QWidget):
  def __init__(self, app_path, options, win_parent=None):
    QtGui.QWidget.__init__(self, win_parent)
    self.yaml_data = YamlData(app_path, options)
    self.action_syntax = ActionSyntax(app_path)

    # Start with an input file template if this application has one
    input_file_template_name = os.path.dirname(app_path) + '/input_template'
    self.input_file_template_root_node = None
    if os.path.isfile(input_file_template_name):
      self.input_file_template_root_node = readInputFile(input_file_template_name)
      self.input_file_template_getpot_data = GetPotData(self.input_file_template_root_node, self)
    else: # If they haven't specified their own template... let's use a default one:
      input_file_template_name = os.path.dirname(sys.argv[0]) + '/input_template'
      self.input_file_template_root_node = readInputFile(input_file_template_name)
      self.input_file_template_getpot_data = GetPotData(self.input_file_template_root_node, self)
      
    self.input_file_root_node = None

    self.constructed_data = {}
    self.initUI()
    if options.input_file:
      abs_input_file = os.path.abspath(options.input_file)
      if os.path.isfile(abs_input_file):
        self.openInputFile(abs_input_file)
      else:
        msgBox = QMessageBox()
        msgBox.setText("Warning: Input file, " + options.input_file + ", not found!")
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.setDefaultButton(QMessageBox.Ok)
        msgBox.exec_()    
    
  def initUI(self):
    # Just a holder so the edit param_widget can go in where we want
    self.edit_param_layout_spot = QtGui.QVBoxLayout()
    
    self.tree_widget_layout = QtGui.QVBoxLayout()
    self.layoutH = QtGui.QHBoxLayout()
    self.layout_with_textbox = QtGui.QHBoxLayout()

    self.input_file_textbox = InputFileTextbox(self)
    self.tree_widget = InputFileTreeWidget(self)
    
    self.tree_widget_layout.addWidget(self.tree_widget)
    self.init_buttons(self.layoutH)
    self.tree_widget_layout.addLayout(self.layoutH)
    self.layout_with_textbox.addLayout(self.tree_widget_layout)
    self.layout_with_textbox.addLayout(self.edit_param_layout_spot)
    self.layout_with_textbox.addLayout(self.input_file_textbox.getLayout())
    self.setLayout(self.layout_with_textbox)

  def init_buttons(self, layout):
    self.buttonOpen = QtGui.QPushButton("Open")
    self.buttonSave = QtGui.QPushButton("Save")
    self.buttonClear = QtGui.QPushButton("Clear")
    QtCore.QObject.connect(self.buttonOpen, QtCore.SIGNAL("clicked()"), self.click_open)
    QtCore.QObject.connect(self.buttonSave, QtCore.SIGNAL("clicked()"), self.click_save)
    QtCore.QObject.connect(self.buttonClear, QtCore.SIGNAL("clicked()"), self.click_clear)
    layout.addWidget(self.buttonOpen)
    layout.addWidget(self.buttonSave)
    layout.addWidget(self.buttonClear)
    
  def openInputFile(self, file_name):
    if file_name and file_name != '':
      progress = QtGui.QProgressDialog("Reading Input File...", "Abort", 0, 100, self)
      progress.setWindowModality(Qt.WindowModal)
      counter = 0

      counter+=1
      progress.setValue(counter)

      # Clear the tree
      self.tree_widget.clear()

      counter+=1
      progress.setValue(counter)

      self.tree_widget.addHardPathsToTree()

      counter+=1
      progress.setValue(counter)

      os.chdir(os.path.dirname(str(file_name)))

      counter+=1
      progress.setValue(counter)

      self.input_file_root_node = readInputFile(file_name)
      self.input_file_getpot_data = GetPotData(self.input_file_root_node, self)

      counter+=1
      progress.setValue(counter)
      
      main_sections = self.input_file_root_node.children

      self.tree_widget.loadData(counter, progress, main_sections)

      counter+=1
      progress.setValue(counter)

  def click_open(self):
    file_name = QtGui.QFileDialog.getOpenFileName(self, "Open Input File", "~/", "Input Files (*.i)")
    if file_name:
      self.tree_widget.clear()
      self.addHardPathsToTree()
      self.openInputFile(file_name)
    
  def click_clear(self):
    msgBox = QMessageBox()
    msgBox.setText("Clear Tree?")
    msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
    msgBox.setDefaultButton(QMessageBox.No)
    ret = msgBox.exec_()
    if ret == QMessageBox.Yes:
      self.tree_widget.clear()
      self.addHardPathsToTree()


  def click_save(self):
    file_name = QtGui.QFileDialog.getSaveFileName(self, "Save Input File", "~/", "Input Files (*.i)")

    if file_name != '':
      file = open(file_name,'w')
      output_string = self.buildInputString()
      file.write(output_string)
      os.chdir(os.path.dirname(str(file_name)))    

@@ -4220,59 +4220,8 @@
 s)%0A%0A
-      counter+=1%0A      progress.setValue(counter)%0A%0A
   de

fix progress bar