# Path: importerUtils.py
# def logInfo(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkLogging", False)): _log("logInfo",    Console.PrintMessage, msg, args)
# 
# def getIconPath(fileName):
# 	return os.path.join(os.path.dirname(__file__), "Resources", "icons", fileName)
# 
# def getTableValue(table, col, row):
# 	try:
# 		return table.get(getCellRef(col, row))
# 	except:
# 		return None
# 
# def setTableValue(table, col, row, val):
# 	if (type(val) == str):
# 		table.set(getCellRef(col, row), val)
# 	else:
# 		if ((sys.version_info.major <= 2) and (type(val) == unicode)):
# 			table.set(getCellRef(col, row), "%s" %(val.encode("utf8")))
# 		else:
# 			table.set(getCellRef(col, row), str(val))
# 
# def logInfo(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkLogging", False)): _log("logInfo",    Console.PrintMessage, msg, args)
# 
# def logWarning(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkWarning", False)): _log("logWarning", Console.PrintWarning, msg, args)
# 
# def logError(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkError", True)):    _log("logError",   Console.PrintError,   msg, args)
# 
# def getCellRef(col, row):
# 	if (isString(col)):
# 		return u"%s%d" %(col, row)
# 	return u"%s%d" %(int2col(col), row)
# 
# def setTableValue(table, col, row, val):
# 	if (type(val) == str):
# 		table.set(getCellRef(col, row), val)
# 	else:
# 		if ((sys.version_info.major <= 2) and (type(val) == unicode)):
# 			table.set(getCellRef(col, row), "%s" %(val.encode("utf8")))
# 		else:
# 			table.set(getCellRef(col, row), str(val))
# 
# def calcAliasname(name):
# 	alias = name.replace(':', '_')
# 	if (IS_CELL_REF.search(name)):
# 		return '%s_' %(alias)
# 	# 45, 48..57, 65..90, 97..122
# 	result = ''
# 	for c in alias:
# 		i = ord(c)
# 		if (i == 45) or (i > 47 and i < 58) or (i > 64 and i < 91) or (i > 96 and i < 123):
# 			result += c
# 		else:
# 			result += '_'
# 	if (result[0] == '_'):
# 		return 'd' + result
# 	return result
# 
# Path: importerClasses.py
# class ParameterTableModel(TableModel):
# 	def __init__(self, parent, mylist, *args):
# 		TableModel.__init__(self, parent, mylist, ['Variant', 'Source', 'Property', 'Parameter', 'Value', 'Units'], *args)
# 	def flags(self, index):
# 		if (index.column() == 0):
# 			return Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsUserCheckable
# 		if (index.column() in [1, 2, 5]): # make object's name and property and unit column read only!
# 			return Qt.ItemIsEnabled
# 		return Qt.ItemIsEnabled |Qt.ItemIsEditable
# 
# class VariantTableModel(TableModel):
# 	def __init__(self, parent, values, *args):
# 		TableModel.__init__(self, parent, values[1:], values[0], *args)
# 	def flags(self, index):
# 		return Qt.ItemIsEnabled | Qt.ItemIsEditable
# 
# Path: importerConstants.py
# DIR_X  = VEC(1, 0, 0)
# 
# DIR_Y  = VEC(0, 1, 0)
# 
# DIR_Z  = VEC(0, 0, 1)


import os, re, sys, Part, FreeCAD, FreeCADGui
from importerUtils   import logInfo, getIconPath, getTableValue, setTableValue, logInfo, logWarning, logError, getCellRef, setTableValue, calcAliasname
from FreeCAD         import Vector as VEC
from importerClasses import ParameterTableModel, VariantTableModel
from math            import degrees, radians, pi, sqrt, cos, sin, atan
from PySide.QtCore   import *
from PySide.QtGui    import *
from importerConstants import DIR_X, DIR_Y, DIR_Z
			row += 1
			header = getTableValue(table, 'A', row)

		return headers

	def _updateMapping_(self, fp):
		if (fp.Values is None): return False
		if (fp.Parameters is None): return False

		fp.Mapping.clear()
		FreeCAD.ActiveDocument.recompute()
		parameter  = self._getHeadersByRow_(fp.Parameters)
		for col in range(1, len(fp.Values[0])):
			hdr = fp.Values[0][col]
			cell = parameter[hdr]
			fp.Mapping[col] = cell

		return True

	def _updateVariant_(self, fp):
		try:
			if (not self._updateMapping_(fp)):
				return False
			r = fp.Rows[fp.Variant]
			FreeCAD.Console.PrintMessage("Set parameters according to variant '%s' (row %d):\n" %(fp.Variant, r))
			for col in fp.Mapping:
				prm = fp.Values[0][col]
				val = fp.Values[r][col]
				if (hasattr(val, 'Value')):
					val = val.Value

# Path: importerUtils.py
# def logInfo(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkLogging", False)): _log("logInfo",    Console.PrintMessage, msg, args)
# 
# def getIconPath(fileName):
# 	return os.path.join(os.path.dirname(__file__), "Resources", "icons", fileName)
# 
# def getTableValue(table, col, row):
# 	try:
# 		return table.get(getCellRef(col, row))
# 	except:
# 		return None
# 
# def setTableValue(table, col, row, val):
# 	if (type(val) == str):
# 		table.set(getCellRef(col, row), val)
# 	else:
# 		if ((sys.version_info.major <= 2) and (type(val) == unicode)):
# 			table.set(getCellRef(col, row), "%s" %(val.encode("utf8")))
# 		else:
# 			table.set(getCellRef(col, row), str(val))
# 
# def logInfo(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkLogging", False)): _log("logInfo",    Console.PrintMessage, msg, args)
# 
# def logWarning(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkWarning", False)): _log("logWarning", Console.PrintWarning, msg, args)
# 
# def logError(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkError", True)):    _log("logError",   Console.PrintError,   msg, args)
# 
# def getCellRef(col, row):
# 	if (isString(col)):
# 		return u"%s%d" %(col, row)
# 	return u"%s%d" %(int2col(col), row)
# 
# def setTableValue(table, col, row, val):
# 	if (type(val) == str):
# 		table.set(getCellRef(col, row), val)
# 	else:
# 		if ((sys.version_info.major <= 2) and (type(val) == unicode)):
# 			table.set(getCellRef(col, row), "%s" %(val.encode("utf8")))
# 		else:
# 			table.set(getCellRef(col, row), str(val))
# 
# def calcAliasname(name):
# 	alias = name.replace(':', '_')
# 	if (IS_CELL_REF.search(name)):
# 		return '%s_' %(alias)
# 	# 45, 48..57, 65..90, 97..122
# 	result = ''
# 	for c in alias:
# 		i = ord(c)
# 		if (i == 45) or (i > 47 and i < 58) or (i > 64 and i < 91) or (i > 96 and i < 123):
# 			result += c
# 		else:
# 			result += '_'
# 	if (result[0] == '_'):
# 		return 'd' + result
# 	return result
# 
# Path: importerClasses.py
# class ParameterTableModel(TableModel):
# 	def __init__(self, parent, mylist, *args):
# 		TableModel.__init__(self, parent, mylist, ['Variant', 'Source', 'Property', 'Parameter', 'Value', 'Units'], *args)
# 	def flags(self, index):
# 		if (index.column() == 0):
# 			return Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsUserCheckable
# 		if (index.column() in [1, 2, 5]): # make object's name and property and unit column read only!
# 			return Qt.ItemIsEnabled
# 		return Qt.ItemIsEnabled |Qt.ItemIsEditable
# 
# class VariantTableModel(TableModel):
# 	def __init__(self, parent, values, *args):
# 		TableModel.__init__(self, parent, values[1:], values[0], *args)
# 	def flags(self, index):
# 		return Qt.ItemIsEnabled | Qt.ItemIsEditable
# 
# Path: importerConstants.py
# DIR_X  = VEC(1, 0, 0)
# 
# DIR_Y  = VEC(0, 1, 0)
# 
# DIR_Z  = VEC(0, 0, 1)


# Path: InventorViewProviders.py
import os, re, sys, Part, FreeCAD, FreeCADGui
from importerUtils   import logInfo, getIconPath, getTableValue, setTableValue, logInfo, logWarning, logError, getCellRef, setTableValue, calcAliasname
from FreeCAD         import Vector as VEC
from importerClasses import ParameterTableModel, VariantTableModel
from math            import degrees, radians, pi, sqrt, cos, sin, atan
from PySide.QtCore   import *
from PySide.QtGui    import *
from importerConstants import DIR_X, DIR_Y, DIR_Z

			row += 1
			header = getTableValue(table, 'A', row)

		return headers

	def _updateMapping_(self, fp):
		if (fp.Values is None): return False
		if (fp.Parameters is None): return False

		fp.Mapping.clear()
		FreeCAD.ActiveDocument.recompute()
		parameter  = self._getHeadersByRow_(fp.Parameters)
		for col in range(1, len(fp.Values[0])):
			hdr = fp.Values[0][col]
			cell = parameter[hdr]
			fp.Mapping[col] = cell

		return True

	def _updateVariant_(self, fp):
		try:
			if (not self._updateMapping_(fp)):
				return False
			r = fp.Rows[fp.Variant]
			FreeCAD.Console.PrintMessage("Set parameters according to variant '%s' (row %d):\n" %(fp.Variant, r))
			for col in fp.Mapping:
				prm = fp.Values[0][col]
				val = fp.Values[r][col]
				if (hasattr(val, 'Value')):
					val = val.Value

				setTableValue(fp.Parameters, 'B', fp.Mapping[col], val)