U
    '1ec                     @   s0  d Z dZddlmZ ddlmZ ddlmZ ddlT ddl	T ddl
mZ G dd	 d	ZG d
d deejZG dd deZi ZG dd dZG dd deZdd Zdd ZG dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZd"d# Zd$d% Zed&kr,e  d'S )(z3.3.0z-Base class for user-defined graphical widgets    )shapes)	rl_config)colors)*)refc                   @   sF   e Zd ZdZdZdd Zejr&dd Zddd	Z	d
d Z
dddZdS )
PropHolderzBase for property holdersNc                 C   s   | j dk	r| j D ].}|d dkrd|| f }|| j kst|q| j  D ]T\}}d|| f }t| |stt|t| |}||| jjf}|	|sNtd| qNdS )a0  If the _attrMap attribute is not None, this
        checks all expected attributes are present; no
        unwanted attributes are present; and (if a
        checking function is found) checks each
        attribute has a valid value.  Either succeeds
        or raises an informative exception.
        Nr   _z#Unexpected attribute %s found in %szMissing attribute %s from %sz-Invalid value %s for attribute %s in class %s)
_attrMap__dict__keysAssertionErroritemshasattrgetattr	__class____name__validate)selfkeymsgattrZ	metavaluevalueargs r   A/tmp/pip-unpacked-wheel-109iniqw/reportlab/graphics/widgetbase.pyverify   s    	

zPropHolder.verifyc                 C   s   t | || dS )zSBy default we verify.  This could be off
            in some parallel base classes.NZvalidateSetattrr   namer   r   r   r   __setattr__.   s    zPropHolder.__setattr__   c           	      C   s   ddl m} i }| j D ]}|dd dkrt| |}|r||r|j|d}| D ]6\}}|d dkr~||d||f < qX||d||f < qXq|||< q|S )	aJ  Returns a list of all properties which can be edited and
        which are not marked as private. This may include 'child
        widgets' or 'primitive shapes'.  You are free to override
        this and provide alternative implementations; the default
        one simply returns everything without a leading underscore.
        r   )isValidChildr    r   recur[z%s%sz%s.%s)reportlab.lib.validatorsr!   r
   r   r   getPropertiesr   )	r   r#   r!   propsr   	component
childPropsZchildKeyZ
childValuer   r   r   r&   4   s    

zPropHolder.getPropertiesc           
   	   C   s   i }|  D ]h\}}|dd}t|dkr:t| || q|\}}z||| |< W q tk
rr   ||i||< Y qX q|  D ]\}}t| |}	|	| q~dS )a  Permits bulk setting of properties.  These may include
        child objects e.g. "chart.legend.width = 200".

        All assignments will be validated by the object as if they
        were set individually in python code.

        All properties of a top-level object are guaranteed to be
        set before any of the children, which may be helpful to
        widget designers.
        .r    N)r   splitlensetattrKeyErrorr   setProperties)
r   ZpropDictZchildPropDictsr   r   partsZ	childNameZremainsZchildPropDictchildr   r   r   r/   W   s    
zPropHolder.setProperties c                 C   sH   t |   }|  |r$|d }|D ]\}}td|||f  q(dS )zConvenience. Lists them on standard output.  You
        may provide a prefix - mostly helps to generate code
        samples for documentation.
        r*   z	%s%s = %sN)listr&   r   sortprint)r   prefixZpropListr   r   r   r   r   dumpPropertiesv   s    zPropHolder.dumpProperties)r    )r2   )r   
__module____qualname____doc__r	   r   r   ZshapeCheckingr   r&   r/   r7   r   r   r   r   r      s   
#r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )WidgetzBase for all user-defined widgets.  Keep as simple as possible. Does
    not inherit from Shape so that we can rewrite shapes without breaking
    widgets and vice versa.c                 K   s,   |  D ]\}}|| jkrt| || qd S N)r   r
   r-   )r   kwkvr   r   r   _setKeywords   s    
zWidget._setKeywordsc                 C   s   d}t |d S )Nz+draw() must be implemented for each Widget!NotImplementedErrorr   r   r   r   r   draw   s    zWidget.drawc                 C   s   d}t |d S )Nz+demo() must be implemented for each Widget!rA   rC   r   r   r   demo   s    zWidget.democ                 C   s   |   S r<   )rD   r   r   r   r   provideNode   s    zWidget.provideNodec                 C   s   |    S )zGReturn outer boundary as x1,y1,x2,y2.  Can be overridden for efficiency)rD   	getBoundsrF   r   r   r   rH      s    zWidget.getBoundsN)	r   r8   r9   r:   r@   rD   rE   rG   rH   r   r   r   r   r;      s   r;   c                   @   sR   e Zd ZdZeeeddeeddeeddeddddZdddZdd Z	dS )ScaleWidgetz Contents with a scale and offsetzx offsetdesczy offsetscaleNContained drawable elements)xyrL   contentsr         ?c                 C   s>   || _ || _|sg }nt|ttfs*|f}t|| _|| _d S r<   )rN   rO   
isinstancetupler3   rP   rL   )r   rN   rO   rL   rP   r   r   r   __init__   s     
zScaleWidget.__init__c              	   C   s&   t j| jd| jdd| j| j| jfiS )N	transformr   )r   GrouprP   rL   rN   rO   rF   r   r   r   rD      s    zScaleWidget.draw)r   r   rQ   N)
r   r8   r9   r:   AttrMapAttrMapValueisNumberr	   rT   rD   r   r   r   r   rI      s   




	rI   c                   @   s   e Zd Zdd ZdS )
CloneMixinc                 K   s4   |   }|j  |j| j |r0|j| |S r<   )r   r
   clearupdate)r   kwdsnr   r   r   clone   s    
 zCloneMixin.cloneN)r   r8   r9   r_   r   r   r   r   rZ      s   rZ   c                   @   sl   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dddZ
dd Zdd Zdd ZdddZdS )TypedPropertyCollectiona  A container with properties for objects of the same kind.

    This makes it easy to create lists of objects. You initialize
    it with a class of what it is to contain, and that is all you
    can add to it.  You can assign properties to the collection
    as a whole, or to a numeric index within it; if so it creates
    a new child object to hold that data.

    So:
        wedges = TypedPropertyCollection(WedgeProperties)
        wedges.strokeWidth = 2                # applies to all
        wedges.strokeColor = colors.red       # applies to all
        wedges[3].strokeColor = colors.blue   # only to one

    The last line should be taken as a prescription of how to
    create wedge no. 3 if one is needed; no error is raised if
    there are only two data points.

    We try and make sensible use of tuple indices.
        line[(3,x)] is backed by line[(3,)] == line[3] & line
    c                 K   s   |f || j d< i | j d< d S )N_value	_children)r
   )r   ZexampleClassr]   r   r   r   rT      s    z TypedPropertyCollection.__init__c                 C   s   G dd d|t }|S )Nc                   @   s    e Zd Zdd Zedd ZdS )z5TypedPropertyCollection.wKlassFactory.<locals>.WKlassc                 S   sp   z| j jd | |W S    | j}|j}| j}|r\||krNt|| | Y S |d d }q0t|| Y S X d S )Nr   )r   	__bases____getattr__parentrb   __propholder_index__r   )r   r   rf   crN   r   r   r   re      s    zATypedPropertyCollection.wKlassFactory.<locals>.WKlass.__getattr__c                 S   s   |   S r<   )__propholder_parent__rF   r   r   r   rf      s    z<TypedPropertyCollection.wKlassFactory.<locals>.WKlass.parentN)r   r8   r9   re   propertyrf   r   r   r   r   WKlass   s   rk   )rZ   )r   Klassrk   r   r   r   wKlassFactory   s    z%TypedPropertyCollection.wKlassFactoryc              
   C   s   t |ttfrt|n|f}z| j| W S  tk
r   | jj}|tkrRt| }n| | t|< }| }t	t|j
 fddt|j D ]}|j
|= q|j
tt| |d d d || j|< | Y S X d S )Nc                 S   s   | |kS r<   r   )rN   Kr   r   r   <lambda>       z5TypedPropertyCollection.__getitem__.<locals>.<lambda>rc   )ri   rg   )rR   rS   r3   rb   r.   ra   r   _ItemWrapperrm   filterr
   r   r	   r\   dictweakref_ref)r   rN   rl   rk   r1   ir   r   r   __getitem__   s"    
*


z#TypedPropertyCollection.__getitem__c                 C   s"   t |ttfrt|n|f| jkS r<   )rR   rS   r3   rb   )r   r   r   r   r   __contains__  s    z$TypedPropertyCollection.__contains__c                 C   s$   t || jjs td| jjj d S )Nz0This collection can only hold objects of type %s)rR   ra   r   r   r   )r   r   r   r   r   r   __setitem__  s    z#TypedPropertyCollection.__setitem__c                 C   s   t t| j S r<   )r,   r3   rb   r   rF   r   r   r   __len__  s    zTypedPropertyCollection.__len__r    c                 C   s   i }| j j|d D ]\}}||d| < q| j D ]h}| j| j|d}| D ]H\}}t| |rtt| ||krTdt|dkr|n|d |f }|||< qTq6|S )Nr"   z%sz[%s].%sr    r   )ra   r&   r   rb   r   r   r   r,   )r   r#   r'   r   r   idxr)   ZnewKeyr   r   r   r&     s     z%TypedPropertyCollection.getPropertiesc                 K   s8   |  D ]*\}}t|D ]\}}t| | || qqd S r<   )r   	enumerater-   )r   r=   r   r   ru   r?   r   r   r   	setVector  s    z!TypedPropertyCollection.setVectorc                 C   s   t | j|S r<   )r   ra   )r   r   r   r   r   re      s    z#TypedPropertyCollection.__getattr__c                 C   s   t | j||S r<   )r-   ra   r   r   r   r   r   #  s    z#TypedPropertyCollection.__setattr__Nc                 C   s   || krt | | ||S |S r<   )r   )r   r   adefaultr   r   r   	checkAttr&  s    z!TypedPropertyCollection.checkAttr)r    )N)r   r8   r9   r:   rT   rm   rv   rw   rx   ry   r&   r|   re   r   r   r   r   r   r   r`      s   
r`   c                 C   s   t | tr| | S | S )z<return obj if it's not a TypedPropertyCollection else obj[x])rR   r`   )objrN   r   r   r   
tpcGetItem)  s    r   c                 C   s.   t | dsd S | j}t|ts"d S t| tS )Nri   )r   ri   rR   rt   r`   )r   phr   r   r   isWKlass-  s    
 
 r   c                   @   s   e Zd ZdZeeeddeedddeedddedd	ddeed
deedddeeddeeddee	d	Z
dd Zdd ZdS )StylePropertiesa  A container class for attributes used in charts and legends.

    Attributes contained can be those for any graphical element
    (shape?) in the ReportLab graphics package. The idea for this
    container class is to be useful in combination with legends
    and/or the individual appearance of data series in charts.

    A legend could be as simple as a wrapper around a list of style
    properties, where the 'desc' attribute contains a descriptive
    string and the rest could be used by the legend e.g. to draw
    something like a color swatch. The graphical presentation of
    the legend would be its own business, though.

    A chart could be inspecting a legend or, more directly, a list
    of style properties to pick individual attributes that it knows
    about in order to render a particular row of the data. A bar
    chart e.g. could simply use 'strokeColor' and 'fillColor' for
    drawing the bars while a line chart could also use additional
    ones like strokeWidth.
    zwidth of the stroke linerJ   z#Line cap 0=butt, 1=round & 2=squarer    rK   ZadvancedUsagez$Line join 0=miter, 1=round & 2=bevelNz$miter limit control miter line joinszdashing patterns e.g. (1,3)z9level of transparency (alpha) accepts values between 0..1zthe color of the strokezthe filling color)	strokeWidthZstrokeLineCapZstrokeLineJoinZstrokeMiterLimitstrokeDashArrayZstrokeOpacitystrokeColor	fillColorrK   c                 K   s"   |  D ]\}}t| || qdS )z"Initialize with attributes if any.N)r   r-   )r   kwargsr>   r?   r   r   r   rT   V  s    zStyleProperties.__init__c                 C   s   t | || dS )z3Verify attribute name and value, before setting it.Nr   r   r   r   r   r   ]  s    zStyleProperties.__setattr__)r   r8   r9   r:   rW   rX   rY   isListOfNumbersOrNoneisColorOrNoneisStringr	   rT   r   r   r   r   r   r   4  s   



r   c                   @   s   e Zd Zdd Zdd ZdS )
TwoCirclesc                 C   s0   t jdddtjd| _t jdddtjd| _d S )Nd      r   i,  )r   Circler   red
leftCirclerightCirclerF   r   r   r   rT   c  s    zTwoCircles.__init__c                 C   s   t | j| jS r<   )r   rV   r   r   rF   r   r   r   rD   g  s    zTwoCircles.drawN)r   r8   r9   rT   rD   r   r   r   r   r   b  s   r   c                   @   s\   e Zd ZdZeeeeeeeeeeeeeddddZ	dd Z
dd	 Zd
d ZdS )FacezThis draws a face with two eyes.

    It exposes a couple of properties
    to configure itself and hides all other details.
    happysadok)rN   rO   size	skinColoreyeColormoodc                 C   s*   d| _ d| _d| _d | _tj| _d| _d S )N
   P   r   )rN   rO   r   r   r   bluer   r   rF   r   r   r   rT   {  s    zFace.__init__c                 C   s   d S r<   r   rF   r   r   r   rE     s    z	Face.democ                 C   s  | j }t }dddd| j| jg|_|tj|d |d |d | jd |tj|d |d |d t	j
d |tj|d |d |d | jd |tj|d |d |d t	j
d |tj|d |d |d | jd |tj|d |d	 |d
 |d |d	 |d gd d | jdkr,d}n| jdkr>d}nd}|tj|d |d |d |d |d	 |d|  |d
 |d|  gt	jt	j|d d |S )Nr    r         ?r   gffffff?g?g?g?g333333?g?g333333?)pointsr   r   gr   g?gffffff?gQ?)r   r   r   r   )r   r   rV   rN   rO   rU   addr   r   r   whiter   ZPolygonr   Zpinkr   )r   sgoffsetr   r   r   rD     s@    &&&&&&   
 
z	Face.drawN)r   r8   r9   r:   rW   rX   rY   r   OneOfr	   rT   rE   rD   r   r   r   r   r   k  s   	r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TwoFacesc                 C   s,   t  | _d| j_t  | _d| j_d| j_d S )Nr   r   r   )r   faceOner   faceTworN   rF   r   r   r   rT     s
    zTwoFaces.__init__c                 C   s   t | j| jS )zJust return a group)r   rV   r   r   rF   r   r   r   rD     s    zTwoFaces.drawc                 C   s   dS )zQThe default case already looks good enough,
        no implementation needed hereNr   rF   r   r   r   rE     s    zTwoFaces.demoN)r   r8   r9   rT   rD   rE   r   r   r   r   r     s   r   c                   @   sP   e Zd ZdZeejeedddZ	dd Z
dd Zdd
dZdd Zdd Zd	S )Sizerz.Container to show size of all enclosed objectsrM   rJ   )ZBASErP   c                 G   s.   g | _ tj| _tj| _|D ]}| | qd S r<   )rP   r   Zcyanr   Zmagentar   r   )r   elementselemr   r   r   rT     s
    zSizer.__init__c                 C   s4   |r0|t | j kr$tt| j|< t| || dS )zLif name is not None add an attribute pointing to node and add to the attrMapN)r3   r	   r   rX   r!   r-   )r   r   noder   r   r   _addNamedNode  s    zSizer._addNamedNodeNc                 C   s4   |dk	r0t |std| j| | || dS )zAppends non-None child node to the 'contents' attribute. In addition,
        if a name is provided, it is subsequently accessible by name
        Nz1Can only add Shape or UserNode objects to a Group)r!   r   rP   appendr   )r   r   r   r   r   r   r     s    z	Sizer.addc                 C   s6   | j r.g }| j D ]}||  qt|S dS d S )N)r   r   r   r   )rP   r   rH   r   ZgetRectsBounds)r   br   r   r   r   rH     s    

zSizer.getBoundsc                 C   s^   t  }|  \}}}}t j|||| || | j| jd}|| | jD ]}|| qJ|S )N)rN   rO   widthheightr   r   )r   rV   rH   Rectr   r   r   rP   )r   r   x1y1Zx2y2rr   r   r   r   rD     s    

z
Sizer.draw)N)r   r8   r9   r:   rW   r   Z
SolidShaperX   ZisListOfShapesr	   rT   r   r   rH   rD   r   r   r   r   r     s   



r   c                   @   s  e Zd Zeeeddeeddeeddeedddeedddeed	ddeed
ddeeddee	eddee	eddee	eddeedddeedddeedddee
dddeedddeddddeedddddeeeddddddddZdd Zdd  ZdS )!CandleStickPropertieszWidth of a line.rJ   zColor of a line or border.zDash array of a line.zcross line widthr    r   zcross line low valuezcross line high valuezwidth of the box partzfill color of boxzstroke color of boxzDash array of the box.zWidth of the box lines.zlow value of the boxzmiddle box line valuezhigh value of the boxzwhether to show box sideszposition of the candleNz	our chartverticalZ
horizontalzcandle directionr      )ZemptyOKlohi)r   r   r   
crossWidthcrossLocrossHiboxWidthboxFillColorboxStrokeColorboxStrokeDashArrayboxStrokeWidthboxLoboxMidboxHiboxSidespositionchart
candleKindaxesc                 K   s0  | dd| _| dtj| _| dd | _| dd| _| dd | _| dd | _| d	d | _	| d
d | _
| dtj| _| dtj| _| dtj| _| dd | _| dd | _| dd | _| dd| _| dd | _| dd| _| dddg| _| dd }|r"t|ndd | _d S )Nr   r    r   r   r      r   r   r   r   r   r   r   r   r   r   r   Tr   r   r   r   ZcategoryAxisZ	valueAxisr   c                   S   s   d S r<   r   r   r   r   r   ro   '  rp   z0CandleStickProperties.__init__.<locals>.<lambda>)popr   r   Zblackr   r   r   r   r   r   r   NotSetOrZ_not_setr   r   r   r   r   r   r   r   r   r   rt   r   )r   r]   r   r   r   r   rT     s(    zCandleStickProperties.__init__c                    s2  |   }t|| jd }t|dds2t|dfdd}t|| jd }t|ddsht|dfdd}	t j | j| j| j| j	}
|	| j
}|	| j}| j}| j}t| j}t| j}t| j}|	| j}|	| j}|	| j}|| j}| jd|||fk}d||fk} fd	d
}|rXt||t|| }}|rVt||t|| }}|r||ks||kr||||| |
dk	rV|||
d  |||
d  | |||
d  |||
d  | nr|rV||k r ||||| |||
d  |||
d  | ||krV||||| |||
d  |||
d  | |r,||d  }|}|| }|}dkr||||f\}}}} tj||||| jr|nd|||d | js||d|  ||d|  | ||d|  ||d|  | |dk	r,||d|  ||d|  |  jS )zthe symbol interfacer   ZmidScaleNrL   c                    s   | d k	r | S d S r<   r   rN   )_xScaler   r   ro   /  rp   z0CandleStickProperties.__call__.<locals>.<lambda>r    c                    s   | d k	r | S d S r<   r   r   )_yScaler   r   ro   3  rp   c              
      s<   dkr||  } }|| }} t j| |||d d S )Nr   )r   r   r   )r   ZLine)Zx0Zy0r   r   )Gr   r   r   r   r   r   aLineG  s    

z-CandleStickProperties.__call__.<locals>.aLiner   r   )r   r   r   r   )r   r   r   r   rV   r   r   r   r   r   r   r   r   r   r   ZconditionalValuer   r   r   r   r   r   r   r   minmaxr   r   __self__)r   _xZ_y_sizeZ_colorr   ZxAZxScaleZyAZyScaler   r   r   r   r   r   r   r   r   r   r   r   ZhaveBoxZhaveLiner   rN   rO   hwr   )r   r   r   r   r   r   r   r   __call__)  sz     
 








 


*
zCandleStickProperties.__call__)r   r8   r9   rW   rX   rY   r   r   ZisNumberOrNoner   Z	isBooleanr   Z
SequenceOfr   r	   rT   r   r   r   r   r   r     s.   



r   c                  K   s   t tf| S r<   )r`   r   )r]   r   r   r   CandleSticksl  s    r   c                  C   s  ddl m}  t| }tj|_|jtjtjtj	fd t
tt tdd}t }|| ddlm} ||dd t
d	 tdd}t }tj|_d
|_|j|dd t
d |  ||dd t
d | }||dd t
d t
d |  d S )Nr   )WedgePropertiesr   i     )	renderPDFzsample_widget.pdfzA Sample Widgetzsaved sample_widget.pdfr   ZtheFace)r   zdrawing 1 properties:zface.pdfzsaved face.pdfzface_copy.pdfzAn expanded drawingzsaved face_copy.pdfzdrawing 2 properties:)Z#reportlab.graphics.charts.piechartsr   r`   r   r   r   r|   r   Zgreenr   r5   r,   rq   r   ZDrawingr   r   reportlab.graphicsr   Z
drawToFiler   Zyellowr   r   r7   ZexpandUserNodes)r   ZwedgesdZtcr   fZd2r   r   r   testo  s2    
r   __main__N)__version__r:   r   r   Z	reportlabr   Zreportlab.libr   r%   Zreportlab.lib.attrmapweakrefr   rt   r   ZUserNoder;   rI   rq   rZ   r`   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s2   vn.	G9p
