# 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


class Dimensioning_Selection_prototype:
    'these selection classes are for purposes of recording a drawing view selection for later updates'
    def __init__( self, svg_KWs, svg_element, viewInfo, **extraKWs ):
        self.init_for_svg_KWs( svg_KWs, svg_element, **extraKWs )
        #info for updating selection after its drawing view has been updated
        self.svg_element_tag = svg_element.tag
        self.viewInfo = viewInfo #view bounds etc ...
    def init_for_svg_KWs( self, svg_KWs, svg_element ):
        raise(NotImplementedError,'needs to overwritten depending upon the selection type')
    def svg_fun_args( self, args ):
        raise(NotImplementedError,'needs to overwritten depending upon the selection type')


class PointSelection( Dimensioning_Selection_prototype ):
    def init_for_svg_KWs( self, svg_KWs, svg_element, condensed_args = False  ):
        self.x = svg_KWs['x']
        self.y = svg_KWs['y']
        self.condensed_args = condensed_args
    def xy(self):
        return self.x, self.y
    def svg_fun_args( self, args ):
        if self.condensed_args:
            args.append( [self.x, self.y] )
        else:
            args.extend( [self.x, self.y] )
    def updateValues( self, doc ):
        if not self.viewInfo.changed( doc ):
            return False

# 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



class Dimensioning_Selection_prototype:
    'these selection classes are for purposes of recording a drawing view selection for later updates'
    def __init__( self, svg_KWs, svg_element, viewInfo, **extraKWs ):
        self.init_for_svg_KWs( svg_KWs, svg_element, **extraKWs )
        #info for updating selection after its drawing view has been updated
        self.svg_element_tag = svg_element.tag
        self.viewInfo = viewInfo #view bounds etc ...
    def init_for_svg_KWs( self, svg_KWs, svg_element ):
        raise(NotImplementedError,'needs to overwritten depending upon the selection type')
    def svg_fun_args( self, args ):
        raise(NotImplementedError,'needs to overwritten depending upon the selection type')


class PointSelection( Dimensioning_Selection_prototype ):
    def init_for_svg_KWs( self, svg_KWs, svg_element, condensed_args = False  ):
        self.x = svg_KWs['x']
        self.y = svg_KWs['y']
        self.condensed_args = condensed_args
    def xy(self):
        return self.x, self.y
    def svg_fun_args( self, args ):
        if self.condensed_args:
            args.append( [self.x, self.y] )
        else:
            args.extend( [self.x, self.y] )
    def updateValues( self, doc ):
        if not self.viewInfo.changed( doc ):
            return False

        debugPrint(3,'PointSelection: drawing %s has changed, updating values' % self.viewInfo.name )