from qtpyvcp.widgets.form_widgets.main_window import VCPMainWindow
#from gladevcp.persistence import IniFile
import persistence.py
import getiniinfo.py

# Setup logging
from qtpyvcp.utilities import logger
LOG = logger.getLogger('qtpyvcp.' + __name__)

class MyMainWindow(VCPMainWindow):
    """Main window class for the VCP."""
    def __init__(self, *args, **kwargs):
        super(MyMainWindow, self).__init__(*args, **kwargs)
        self.offsetButtonGroup.buttonClicked.connect(self.offsetHandleKeys)
        self.numberGroup.buttonClicked.connect(self.numKeyboard)
        self.numkey_backspaceButton.clicked.connect(self.numberKeyBackspace)
        self.wr_len.clicked.connect(self.WriteLength)
        self.wr_wid.clicked.connect(self.WriteWidth)
        self.wr_len_q.clicked.connect(self.WriteLQuantity)
        self.wr_wid_q.clicked.connect(self.WriteWQuantity)
        self.wr_depth.clicked.connect(self.WriteStoneDepth)
        self.wr_z_step.clicked.connect(self.WriteZStepMove)
        self.mdiButtonGroup.buttonClicked.connect(self.mdiHandleKeys)
        self.mdi_backspace.clicked.connect(self.mdiBackspace)
        self.wr_feed.clicked.connect(self.WriteCutFeed)
		
		
    # add any custom methods here

    def offsetHandleKeys(self, button):
		char = str(button.text())
		text = self.offsetLabel.text() or '0'
		if text != '0':
			text += char
		else:
			text = char
		self.offsetLabel.setText(text)
    
    def numKeyboard(self, button):
		text = self.displayLabel.text() # copy the label text to the variable
		if len(text) > 0: # if there is something in the label
			text += button.text() # add the button text to the text variable
		else: # if the label is empty
			text = button.text() # assign the button text to the text variable
		self.displayLabel.setText(text) # set the text in label
		
    def numberKeyBackspace(self,button):
		text = self.displayLabel.text()[:-1] # assign all but the last char to text
		self.displayLabel.setText(text)
		
    def WriteLength(self,button):
		if len(self.displayLabel.text()) >0:
			text = int(self.displayLabel.text())
			if text > 0:
				self.stone_length.setValue(text)
			else:
				self.stone_length.setValue(1000)
			self.displayLabel.clear()
				
    def WriteWidth(self,button):
		if len(self.displayLabel.text()) >0:
			text = int(self.displayLabel.text())
			if text > 0:
				self.stone_width.setValue(text)
			else:
				self.stone_width.setValue(1000)
			self.displayLabel.clear()

    def WriteLQuantity(self,button):
		if len(self.displayLabel.text()) >0:
			text = int(self.displayLabel.text())
			if text > 0:
				self.stone_len_pcs.setValue(text)
			else:
				self.stone_len_pcs.setValue(1)
			self.displayLabel.clear()

    def WriteWQuantity(self,button):
		if len(self.displayLabel.text()) >0:
			text = int(self.displayLabel.text())
			if text > 0:
				self.stone_wid_pcs.setValue(text)
			else:
				self.stone_wid_pcs.setValue(1)
			self.displayLabel.clear()

    def WriteStoneDepth(self,button):
		if len(self.displayLabel.text()) >0:
			text = int(self.displayLabel.text())
			if text > 0:
				self.stone_depth.setValue(text)
			else:
				self.stone_depth.setValue(1)
			self.displayLabel.clear()

    def WriteZStepMove(self,button):
		if len(self.displayLabel.text()) >0:
			text = int(self.displayLabel.text())
			if text > 0:
				self.stone_cutstep.setValue(text)
			else:
				self.sstone_cutstep.setValue(1)
			self.displayLabel.clear()

    def WriteCutFeed(self,button):
		if len(self.displayLabel.text()) >0:
			text = int(self.displayLabel.text())
			if text > 0:
				self.cut_feed.setValue(text)
			else:
				self.cut_feed.setValue(100)
			self.displayLabel.clear()

    def mdiHandleKeys(self, button):
		char = str(button.text())
		text = self.mdiEntry.text() or '0'
		if text != '0':
			text += char
		else:
			text = char
		self.mdiEntry.setText(text)

    def mdiBackspace(self,button):
		text = self.mdiEntry.text()[:-1] # assign all but the last char to text
		self.mdiEntry.setText(text)

