# -*- coding: utf8 -*-
import sys
from PyQt4 import QtCore, QtGui

from tpiconv_qt import Ui_MainWindow
from tpiconv_about_qt import About_Dialog
from FileFactory import *

class AboutDialog(QtGui.QDialog):
	def __init__(self, parent=None):
		QtGui.QWidget.__init__(self, parent)
		self.ui = About_Dialog()
		self.ui.setupUi(self)

class MyForm(QtGui.QMainWindow):
	def __init__(self, parent=None):
		QtGui.QWidget.__init__(self, parent)
		self.ui = Ui_MainWindow()
		self.ui.setupUi(self)
		self._connectSlots()
		
	def _prepareMenu(self, ext):
		ext = ext.lower()
		self.ui.action_FP.setEnabled(True)
		self.ui.action_APN.setEnabled(True)
		self.ui.action_ZI.setEnabled(True)
		if ext == 'fp':
			self.ui.action_FP.setEnabled(False)
			return True
		elif ext == 'apn':
			self.ui.action_APN.setEnabled(False)
			return True
		elif ext == 'zi':
			self.ui.action_ZI.setEnabled(False)
			return True
		return False
	def _connectSlots(self):
		print "connect"
		QtCore.QObject.connect(self.ui.actionOtw_rz,QtCore.SIGNAL("activated()"),self._OtworzClicked)
		QtCore.QObject.connect(self.ui.actionZapisz,QtCore.SIGNAL("activated()"),self._ZapiszClicked)
		QtCore.QObject.connect(self.ui.actionO_programie,QtCore.SIGNAL("activated()"),self._OprogramieClicked)
		QtCore.QObject.connect(self.ui.action_FP,QtCore.SIGNAL("activated()"),self._FPClicked)
		QtCore.QObject.connect(self.ui.action_ZI,QtCore.SIGNAL("activated()"),self._ZIClicked)
		QtCore.QObject.connect(self.ui.action_APN,QtCore.SIGNAL("activated()"),self._APNClicked)
		QtCore.QObject.connect(self.ui.actionTransformata_Walsha,QtCore.SIGNAL("activated()"),self._WTClicked)
		QtCore.QObject.connect(self.ui.actionSzybka_transformata_walsha,QtCore.SIGNAL("activated()"),self._FWTClicked)		
	
	def _OtworzClicked(self):
		fn = QtGui.QFileDialog.getOpenFileName( )
		ext = str(fn).split('.')[-1]
		if not self._prepareMenu(ext):
			# ERROR: zly typ pliku
			pass
		f = open(str(fn))
		self.ui.textEdit.setPlainText("".join(f.readlines()))
		self.ui.textEdit_2.setPlainText("")
		factory = FileFactory()
		self.tmp = factory.get_parser(ext)
		self.content = self.tmp.load_file(str(fn))
		
		
	def _ZapiszClicked(self):
		print "zapisz"
	
	def _OprogramieClicked(self):
		about = AboutDialog(self)
		about.showNormal()
	
	def _FPClicked(self):
		conv = FunctionConverter()
		self.converted = conv.convert("Funkcja Przełączająca", self.tmp.convert_to_FP())
		content = self.converted.parse_out()
		self.ui.textEdit_2.setPlainText(content)
	
	def _APNClicked(self):
		conv = FunctionConverter()
		self.converted = conv.convert("Algebraiczna Postać Normalna", self.tmp.convert_to_FP())
		content = self.converted.parse_out()
		self.ui.textEdit_2.setPlainText(content)
	
	def _ZIClicked(self):
		conv = FunctionConverter()
		self.converted = conv.convert("Zbiór Implikantów", self.tmp.convert_to_FP())
		content = self.converted.parse_out()
		self.ui.textEdit_2.setPlainText(content)
	
	def _WTClicked(self):
		checker = FunctionChecker()
		self.converted = checker.check("Transformata Walsha", self.tmp.convert_to_FP())
		content = self.converted.parse_out()
		self.ui.textEdit_2.setPlainText(content)
		
	def _FWTClicked(self):
		checker = FunctionChecker()
		self.converted = checker.check("Szybka transformata Walsha", self.tmp.convert_to_FP())
		content = self.converted.parse_out()
		self.ui.textEdit_2.setPlainText(content)
	
		
if __name__ == "__main__":
	app = QtGui.QApplication(sys.argv)
	myapp = MyForm()
	myapp.show()
	sys.exit(app.exec_())

