# Path: drawingDimensioning/core.py
# def debugPrint( level, msg ):
#     if level <= debugPrint.level:
#         App.Console.PrintMessage(msg + '\n')
# 
# Path: drawingDimensioning/svgLib.py
# class SvgTextParser:
#     def __init__(self, xml):
#         p_header_end = xml.find('>') 
#         self.header = xml[:p_header_end]
#         try:
#             self.text = unicode(xml[ p_header_end+1:-len('</text>') ],'utf8')
#         except TypeError:
#             self.text = unicode(xml[ p_header_end+1:-len('</text>') ])
#         #import FreeCAD
#         #FreeCAD.Console.PrintMessage(self.text)
#         self.parms = {}
#         h = self.header
#         p = h.find('=')
#         while p > -1:
#             i = p-1
#             key = ''
#             while key == '' or h[i] != ' ':
#                 if h[i] != ' ':
#                     key = h[i] + key
#                 i = i - 1
#             p1 = h.find('"', p)
#             p2 = h.find('"', p1+1)
#             self.parms[key] = h[p1+1:p2]
#             p = self.header.find('=', p+1)
#         self.x = float(self.parms['x'])
#         self.y = float(self.parms['y'])
#         self.font_family = self.parms.get('font-family', 'inherit')
#         self.font_size = self.parms.get('font-size','inherit')
#         if 'style' in self.parms: #for backwards compadiability
#             self.font_size = self.parms['style'][len('font-size:'):]
#         self.fill = self.parms.get('fill','rgb(0,0,0)')
#         self.transform = self.parms.get('transform')
#         if self.transform != None:
#             t = self.transform
#             if 'rotate(' in t:
#                 self.rotation = float(t[t.find('rotate(')+len('rotate('):].split()[0])
#             else:
#                 self.rotation = 0
#         else:
#             self.rotation = 0
#         self.text_anchor = self.parms.get('text-anchor','inherit')
#     def toXML(self):
#         XML = '''<text x="%f" y="%f" font-family="%s" font-size="%s" fill="%s" text-anchor="%s" %s >%s</text>'''  % (  self.x, self.y, self.font_family, self.font_size,  self.fill, self.text_anchor, 'transform="rotate(%f %f,%f)"' % (self.rotation,self.x,self.y) if self.rotation != 0 else '', self.text )
#         return XML
#     def convertUnits(self,value):
#         '''http://www.w3.org/TR/SVG/coords.html#Units
#         units do not seem to have on font size though:
#         8cm == 8pt == 8blah?'''
#         i = 0
#         while i < len(value) and value[i] in '0123456789.':
#             i = i + 1
#         v = value[:i]
#         factor = 1.0
#         return float(v)*factor
# 
#     def textRect(self):
#         sizeInPoints = self.convertUnits(self.font_size.strip())
#         font = QtGui.QFont(self.font_family, sizeInPoints)
#         fm = QtGui.QFontMetrics(font)
#         return fm.boundingRect(self.text)
# 
#     def width(self):
#         'return width of untransformed text'
#         return self.textRect().width() *0.63
#     def height(self):
#         'height of untransformed text'
#         return self.textRect().height() *0.6
# 
#     def __unicode__(self):
#         return u'<svgLib_dd.SvgTextParser family="%s" font_size="%s" fill="%s" rotation="%f" text="%s">' % (self.font_family, self.font_size, self.fill, self.rotation, self.text )
# 
#     def __repr__(self):
#         return u'<svgLib_dd.SvgTextParser family="%s" font_size="%s" fill="%s" rotation="%f">' % (self.font_family, self.font_size, self.fill, self.rotation )


import pickle
import base64
from numpy.linalg import norm
from numpy import log
from .core import debugPrint
from .svgLib import SvgTextParser
        from .recomputeDimensions import SvgElements
        from .recomputeDimensions import SvgElements
        from .recomputeDimensions import SvgElements
        from .recomputeDimensions import SvgElements
        dy = (y2-y1)*10**-6
        x = self.point.x
        y = self.point.y
        args.append( [ x-dx,y-dy,x+dx,y+dy ] )
    def updateValues( self, doc ):
        changed = self.point.updateValues( doc ) or self.parellel_line.updateValues( doc )  
        self.viewInfo = self.point.viewInfo
        return changed


class TwoLineSelection( Dimensioning_Selection_prototype ):
    def __init__(self, output_mode='combined_center'):
        self.lines = []
        self.output_mode = output_mode
    def addLine( self, svg_KWs, svg_element, viewInfo ):
        self.lines.append( LineSelection( svg_KWs, svg_element, viewInfo ) )
        self.viewInfo = viewInfo
    def svg_fun_args( self, args ):
        if self.output_mode == 'combined_center':
            x1, y1, x2, y2 = self.lines[0].drawing_coordinates()
            x3, y3, x4, y4 = self.lines[1].drawing_coordinates()
            args.append( [0.25*(x1 + x2 + x3 + x4), 0.25*(y1 + y2 + y3 + y4)] )
        else:
            raise(NotImplementedError, "output_mode %s not implemented" % self.output_mode)
    def updateValues( self, doc ):
        changed = any([ line.updateValues( doc ) for line in self.lines ])
        self.viewInfo = self.lines[0].viewInfo
        return changed

        

# Path: drawingDimensioning/core.py
# def debugPrint( level, msg ):
#     if level <= debugPrint.level:
#         App.Console.PrintMessage(msg + '\n')
# 
# Path: drawingDimensioning/svgLib.py
# class SvgTextParser:
#     def __init__(self, xml):
#         p_header_end = xml.find('>') 
#         self.header = xml[:p_header_end]
#         try:
#             self.text = unicode(xml[ p_header_end+1:-len('</text>') ],'utf8')
#         except TypeError:
#             self.text = unicode(xml[ p_header_end+1:-len('</text>') ])
#         #import FreeCAD
#         #FreeCAD.Console.PrintMessage(self.text)
#         self.parms = {}
#         h = self.header
#         p = h.find('=')
#         while p > -1:
#             i = p-1
#             key = ''
#             while key == '' or h[i] != ' ':
#                 if h[i] != ' ':
#                     key = h[i] + key
#                 i = i - 1
#             p1 = h.find('"', p)
#             p2 = h.find('"', p1+1)
#             self.parms[key] = h[p1+1:p2]
#             p = self.header.find('=', p+1)
#         self.x = float(self.parms['x'])
#         self.y = float(self.parms['y'])
#         self.font_family = self.parms.get('font-family', 'inherit')
#         self.font_size = self.parms.get('font-size','inherit')
#         if 'style' in self.parms: #for backwards compadiability
#             self.font_size = self.parms['style'][len('font-size:'):]
#         self.fill = self.parms.get('fill','rgb(0,0,0)')
#         self.transform = self.parms.get('transform')
#         if self.transform != None:
#             t = self.transform
#             if 'rotate(' in t:
#                 self.rotation = float(t[t.find('rotate(')+len('rotate('):].split()[0])
#             else:
#                 self.rotation = 0
#         else:
#             self.rotation = 0
#         self.text_anchor = self.parms.get('text-anchor','inherit')
#     def toXML(self):
#         XML = '''<text x="%f" y="%f" font-family="%s" font-size="%s" fill="%s" text-anchor="%s" %s >%s</text>'''  % (  self.x, self.y, self.font_family, self.font_size,  self.fill, self.text_anchor, 'transform="rotate(%f %f,%f)"' % (self.rotation,self.x,self.y) if self.rotation != 0 else '', self.text )
#         return XML
#     def convertUnits(self,value):
#         '''http://www.w3.org/TR/SVG/coords.html#Units
#         units do not seem to have on font size though:
#         8cm == 8pt == 8blah?'''
#         i = 0
#         while i < len(value) and value[i] in '0123456789.':
#             i = i + 1
#         v = value[:i]
#         factor = 1.0
#         return float(v)*factor
# 
#     def textRect(self):
#         sizeInPoints = self.convertUnits(self.font_size.strip())
#         font = QtGui.QFont(self.font_family, sizeInPoints)
#         fm = QtGui.QFontMetrics(font)
#         return fm.boundingRect(self.text)
# 
#     def width(self):
#         'return width of untransformed text'
#         return self.textRect().width() *0.63
#     def height(self):
#         'height of untransformed text'
#         return self.textRect().height() *0.6
# 
#     def __unicode__(self):
#         return u'<svgLib_dd.SvgTextParser family="%s" font_size="%s" fill="%s" rotation="%f" text="%s">' % (self.font_family, self.font_size, self.fill, self.rotation, self.text )
# 
#     def __repr__(self):
#         return u'<svgLib_dd.SvgTextParser family="%s" font_size="%s" fill="%s" rotation="%f">' % (self.font_family, self.font_size, self.fill, self.rotation )


# Path: drawingDimensioning/proxies.py
import pickle
import base64
from numpy.linalg import norm
from numpy import log
from .core import debugPrint
from .svgLib import SvgTextParser
        from .recomputeDimensions import SvgElements
        from .recomputeDimensions import SvgElements
        from .recomputeDimensions import SvgElements
        from .recomputeDimensions import SvgElements

        dy = (y2-y1)*10**-6
        x = self.point.x
        y = self.point.y
        args.append( [ x-dx,y-dy,x+dx,y+dy ] )
    def updateValues( self, doc ):
        changed = self.point.updateValues( doc ) or self.parellel_line.updateValues( doc )  
        self.viewInfo = self.point.viewInfo
        return changed


class TwoLineSelection( Dimensioning_Selection_prototype ):
    def __init__(self, output_mode='combined_center'):
        self.lines = []
        self.output_mode = output_mode
    def addLine( self, svg_KWs, svg_element, viewInfo ):
        self.lines.append( LineSelection( svg_KWs, svg_element, viewInfo ) )
        self.viewInfo = viewInfo
    def svg_fun_args( self, args ):
        if self.output_mode == 'combined_center':
            x1, y1, x2, y2 = self.lines[0].drawing_coordinates()
            x3, y3, x4, y4 = self.lines[1].drawing_coordinates()
            args.append( [0.25*(x1 + x2 + x3 + x4), 0.25*(y1 + y2 + y3 + y4)] )
        else:
            raise(NotImplementedError, "output_mode %s not implemented" % self.output_mode)
    def updateValues( self, doc ):
        changed = any([ line.updateValues( doc ) for line in self.lines ])
        self.viewInfo = self.lines[0].viewInfo
        return changed

        

class PlacementClick: