
&cSc           @   s  d  Z  d d l Z d d l m Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 m
 Z
 m Z m Z m Z m Z d d l m Z m Z m Z d d l m Z m Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z d Z d   Z d   Z  d   Z! d   Z" d   Z# d   Z$ d   Z% d   Z& d e j' e f d     YZ( d e) f d     YZ* e* Z+ d   Z, d   Z- d   Z. d   Z/ d Z0 d e j1 f d     YZ2 d e) f d      YZ3 d! e) f d"     YZ4 d# e4 f d$     YZ5 d% e j1 f d&     YZ6 d' e3 f d(     YZ7 d) e2 f d*     YZ8 d+ e j1 f d,     YZ9 d- f  d.     YZ: d/ e j; f d0     YZ< e= d1 k re j> e j?  Z@ e jA e jB d2 d2 d2  jC e jB jD     e(   ZE eE jF d3 d4 d5  eE jG   e jH e@ jI    n  d S(6   s3   
This module is a graph and map visualizing tool.

iN(   t   ogr(   t   Qtt   QtCoret   QtGuit   QtOpenGLt   Qwt5(   t   arrayt   sqrtt   average(   t   randintt   uniform(   t   Ui_Form(   t   cm(   t
   MethodType(   t   SimpleXMLRPCServert   qtc         C   s  | j    } | t j j k r1 |  j d d  nL| t j j k rV |  j d d  n'| t j j k r{ |  j d d  n| t j j k r |  j d d  n | t j j k r |  j	 d  n | t j j
 k r |  j	 d d  n | t j j k s| t j j k rjxn |  j   j   D]D } t | t  r| j d t j   d d t j   d  qqWn t j j |  |  d  S(   Ni    ii   g333333?i   iji,  (   t   keyR   R   t   Key_Upt	   translatet   Key_Downt   Key_Leftt	   Key_Rightt   Key_Plust	   scaleViewt	   Key_Minust	   Key_Spacet	   Key_Entert   scenet   itemst
   isinstancet   Polygont   setPost   qrandR   t   QGraphicsViewt   keyPressEvent(   t   selft   eventR   t   item(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR"      s$    $6c         C   s(   |  j  t j d | j   d   d  S(   Ng       @g      n@(   R   t   matht   powt   delta(   R#   R$   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt
   wheelEvent/   s    c         C   sM   |  j    j | |  j t j d d d d   j   } |  j | |  d  S(   Ni    i   (   t   matrixt   scalet   mapRectR   t   QRectFt   width(   R#   t   scaleFactort   factor(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR   2   s    9c         C   ss   x |  j  D] } | j   q
 Wt } x& |  j  D] } | j   r. t } q. q. W| so |  j |  j  d |  _ n  d  S(   Ni    (   t   nodest   calculateForcest   Falset   advancet   Truet	   killTimert   timerId(   R#   R$   t   nodet
   itemsMoved(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt
   timerEvent8   s    c         C   s&   |  j  s" |  j d d  |  _  n  d  S(   Ni  i   (   R7   t
   startTimer(   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt	   itemMovedG   s    	c         C   s2   d } x |  D] } | | d 7} q Wt  j |  S(   Ni    i   (   R&   R   (   t   at   acct   value(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt	   array_magK   s    c         C   sK   t  |   } |  j   } x, t t |    D] } | | | | | <q+ W| S(   s   normalizes an array(   R@   t   copyt   xranget   len(   R=   t   magt   bt   i(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt
   array_normQ   s
    c         C   s5   t  g  t t |    D] } |  | | | ^ q  S(   N(   t   sumRB   RC   (   R=   RE   RF   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt	   array_dotX   s    t	   MapWindowc           B   s   e  Z d  Z d d  Z d   Z d   Z d d  Z d   Z d   Z	 g  d  Z
 d	   Z d
   Z d   Z i  d  Z g  d  Z d   Z d   Z d   Z d   Z RS(   s$   
    Map and Time-series window
    c         C   s%  t  j j |  d   t  j   |  _ |  j |  j  t j d d  |  _ i  |  _	 i  |  _
 t j t j j t j j t j j t j j t j j t j j t j j t j j t j j t j j t j j t j j t j j g  |  _ |  j   d |  _ d  |  _ |  j j  t  j! j"  t# t$ |  j  |  j _$ t# t% |  j  |  j _% t# t& |  j  |  j _& t' j( j) |  j* t' j+ d  |  j,  t' j( j) |  j* t' j+ d  |  j-  t' j( j) |  j. t' j+ d  |  j/  t' j( j) |  j0 t' j+ d  |  j1  t2 |  d  GHt' j( j) |  j3 t' j+ d	  |  j4  d  S(
   Nt   jeti2   i    s   sliderReleased()s   valueChanged()s
   released()s   splitterMoved()t
   showSumBoxs   stateChanged(int)(5   R   t   QWidgett   __init__t   Nonet   Formt   setupUiR   t   get_cmapRK   t
   timeseriest   arrivalst	   itertoolst   cycleR   t   redt   greent   bluet   cyant   magentat   yellowt   blackt   darkCyant   darkRedt	   darkGreent   darkBluet   darkMagentat
   darkYellowt   colorst   setupQwtPlott   stept   Mt   mapViewt   setViewportUpdateModeR!   t   SmartViewportUpdateR   R"   R)   R   R   t   QObjectt   connectt   horizontalSlidert   SIGNALt   on_horizontalSlider_sliderMovedt    on_horizontalSlider_valueChangedt
   pushButtont   replayt   splittert   centerScenet   hasattrRL   t   on_showSumBox_stateChanged(   R#   t   G(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRN   _   s(    		
		%%%%c         C   s   |  j  j t j j d  |  j  j t j   t j j  t j   |  _	 |  j	 j
 t j d   |  j	 j t j j t j j B |  j	 j t j j  |  j	 j d  |  j	 j |  j   |  j  j   d S(   s.   
        Sets up the time series plot
        t   times   t = i    N(   t   qwtPlott   setAxisTitlet   Qwtt   QwtPlott   xBottomt   insertLegendt	   QwtLegendt   RightLegendt   QwtPlotMarkert   mXt   setLabelt   QwtTextt   setLabelAlignmentR   t
   AlignRightt   AlignTopt   setLineStylet   VLinet	   setXValuet   attacht   replot(   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRe   }   s     c         C   s   |  j  } | j d d    xu | D]m } t j |  j  } | j | j  t j j	 | t j
 d  | j  | j t | j   | j |  q# Wd S(   sa   
        creates check boxes on the node list group box
        pl is a list of polygons
        R   c         S   s   |  j  S(   N(   t   name(   t   p(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   <lambda>   s    s   stateChanged(int)N(   t   nodeListLayoutt   sortR   t	   QCheckBoxt
   scrollAreat   setTextR   R   Rk   Rl   Rn   t   selectt
   setToolTipt   strt   geocodet	   addWidget(   R#   t   plt   lot   nt   cb(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   fill_nodel_list   s    	"t   allc   	      C   s  |  j  j   } | j   d g t |  j   } | d k rz d } xK |  j  j   D]" \ } } t | j    | | <qQ Wn | d k r d } n  t j |  } t	 j
 t	 j	 j  } | j d  | j d  | j |  | j |  j  | j | |  | |  _ |  j j   d S(	   sx   
        Plots a time series which is the sum of all(scope="all") or 
        selected timeseries (scope="set")
        i    R   t   Totalt   Selecteds   Sum of Selectedi   i   N(   RS   t   keysR   RC   t	   iteritemsRH   t   valuesR{   t   QwtPlotCurveR   t   QPent   grayt   setStylet   setWidtht   setPenR   Ry   t   setDatat   globalCurveR   (	   R#   t   scopet   tt   dataR   t   kt   vt   curvet   pen(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   add_global_ts_curve   s$    
		c         C   s   d g t  |  j  } x+ |  j j   D] \ } } | | | | <q& W|  j j   } | j   t j |  } | j t j	 |  j
 j     | j |  j  | j | |  | |  j j | _ d S(   s>   
        Plots a time series curve to the plot window
        i    N(   RC   RS   R   R   R   R{   R   R   R   R   Rd   t   nextR   Ry   R   Rg   t   polyDictR   (   R#   t   gcR   R   R   R   R   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt
   addTsCurve   s    
c      	   C   s  t  d | d |  d | d |  |  _ |  j j |  j j } } |  j j |  j j } } | | d } | | d }	 | | d }
 | | d } t j |  j  |  j _	 |  j j	 j
 | | | |	  |  j j | | | |	  |  j j |  j j	  |  j j |  j j	 j    |  j j |
 |  |  j j t j j  |  j j t j j  |  j j t j j  |  j j t j j  x' |  j j D] } |  j j	 j |  qW|  j |  j j  |  j j d d  |  j j   | } |  j j | |  |  j  j!   r|  j" d  n  d	 S(
   s>   
        Draws the map stored in the shapefile fname.
        t   fnamet   displayt	   namefieldt	   geocfieldg?g       @i,  R   N(#   t   MapRg   t   xmint   ymint   xmaxt   ymaxR   t   QGraphicsSceneRh   R   t   setSceneRectt	   fitInViewt   setScenet   updateSceneRectt	   sceneRectt   centerOnt   setCacheModeR!   t   CacheBackgroundt   setRenderHintt   QPaintert   Antialiasingt   setTransformationAnchort   AnchorUnderMouset   setResizeAnchort   AnchorViewCentert   polyListt   addItemR   t   setMinimumSizeR.   R+   RL   t	   isCheckedR   (   R#   t   filenameR   R   R   R   R   R   t   xxst   yxst   xct   ycR   t   scale_factor(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   drawMap   s2    $c         C   sW  |  j  j d  t |   |  _ d |  j _ t t |  j  |  j _ t t |  j  |  j _ t	 j
 |  j  |  j _ g  | D] } | d | d f ^ qz } t |  j d d  \ } } t |  j d d  \ } } x | D] } t |  j | d | d  }	 |	 j | d | d f   t | | | |  t j t |   d |	 _ |  j j j |	  |  j j |	  q W|  j j |  j _ x | D] }
 t |  j j t |
 d  |  j j t |
 d   } t | | | |  t j t |   d | _ |  j j j |  |  j j |  qW| | |  _ |  _ | | |  _ |  _ |  j    d	 S(
   s   
        Draws a graph in the scene
        nlist: is a lis of nodes in the format(x,y,geocode,name)
        elist: is a list of edges described by tuples of indices to the first list.
        s   Network Viewi    i   i   t   axisi   g      ?g?N(!   t   labelR   t   GraphRg   Rh   R7   R   R<   R:   R   R   R   R   t   mint   maxt   NodeR   R&   R   RC   t   sizeR   t
   insertNodeR1   t   Edget   intt	   arrowSizet
   insertEdgeR   R   R   R   Rt   (   R#   t   nlistt   elistR   t   nposR   R   R   R   R8   t   et   ed(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt	   drawGraph   s0    (171c   
      C   ss  |  j  |  j } } |  j |  j } } | | d } | | d } | | d } | | d } |  j j j t j j	  |  j j j
 | | | |  |  j j | | | |  |  j j |  j j  |  j j |  j j j    |  j j | |  |  j j   | }	 |  j j |	 |	  |  j j t j j  |  j j t j j  |  j j t j j  |  j j t j j  d S(   sJ   
        Centers the scene and fits the specified rectangle to it
        g?g       @N(   R   R   R   R   Rh   R   t   setItemIndexMethodR   R   t   NoIndexR   R   R   R   R   R   R.   R+   R   R!   R   R   R   R   R   R   R   R   (
   R#   R   R   R   R   R   R   R   R   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRt     s$    c         C   s1  t  | j    d k r- t  | j    } n d } x | j   D] \ } } y | | :} Wn( t k
 r } | G| G| | G| GHn X|  j |  } t |  } |  j j j |  r@ t	 j
 t | d d  t | d d  t | d d  t | d d   |  j j | _ |  j j | j   q@ q@ Wd S(   s   
        Paint the polygons with the data from data dict
        datadict is a dictionary of the form {geocode:value,...}
        i   g      ?i    i   i   i   N(   R   R   R   t	   TypeErrorRK   R   Rg   R   t   has_keyR   t   QColort	   fillColort   update(   R#   t   datadictt   normwR   t   valR   t   col(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt	   paintPols3  s    ]c            s   t  |  j |  j      f d   } t j j   t j d  |  j  t j j   t j d  |  j  t j j |  j	 t j d  |    j
   d S(   s?   
        Replay the time series from beggining to end.
        c              s     j    d  S(   N(   t   quit(    (   t   rw(    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   stop_replayO  s    t   drawStept   flashs
   released()N(   t   ReplayWorkerRS   RT   R   Rk   Rl   Rn   R   t   flashBorderst   pushButton_2t   render(   R#   R   (    (   R   sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRr   J  s    """c         C   sG   | |  _  |  j |  |  j j |  |  j j |  | |  j | <d S(   sx   
        Draws one timestep on the map
        step: timestep number
        datadict: dictionary geocode:value
        N(   Rf   R   t	   lcdNumberR   Rm   t   setValueRS   (   R#   Rf   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR   V  s
    	c         C   st   | |  j  | <xG | D]? } t |  } t j j |  j j | _ |  j j | j   q Wt j j	 |  j j | _ d S(   ss   
        Flash the borders to bright green to signal events
        gclist: list of geocodes to be flashed
        N(
   RT   R   R   R   RX   Rg   R   t	   lineColorR   R]   (   R#   Rf   t   gclistR   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR  c  s    c         C   s   |  j  j   d  S(   N(   RP   t   show(   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR  o  s    c         C   s    |  j  j   r |  j   n  d  S(   N(   Rm   t	   isEnabledRo   (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRp   r  s    c         C   s   |  j  j   } |  j j |  | |  _ |  j | |  j |  |  j j |  j  |  j j	 t
 j d |  j   |  j j   d S(   s?   
        Handles updating the display on a slider move
        s   t = %sN(   Rm   R?   R  R   Rf   R   RS   R   R   R   R{   R   Ry   R   (   R#   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRo   v  s    	 c         C   s1   | r |  j    n |  j j   |  j j   d S(   s?   
        Handles adding/remove global timeseries curve
        N(   R   R   t   detachRy   R   (   R#   t   st(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRv     s    N(   t   __name__t
   __module__t   __doc__RO   RN   Re   R   R   R   R   R   Rt   R   Rr   R   R  R  Rp   Ro   Rv   (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRJ   [   s"   				,"						t   BaseBoxc           B   s   e  Z d    Z RS(   c         O   s/   x( | j    D] } |  j | | |  q Wd  S(   N(   R   t   __setattr__(   R#   t   argst   kwargsR   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRN     s    (   R  R  RN   (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s   c          O   sC   t  d k r t |  |   St  d k r2 t |  |   St |  |   Sd  S(   Nt   visualR   (   t   graphic_backendt
   VisualNodet   QtNodet   BaseNode(   R  R  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR     s
    c          O   sC   t  d k r t |  |   St  d k r2 t |  |   St |  |   Sd  S(   NR  R   (   R  t
   VisualEdget   QtEdget   BaseRubberEdge(   R  R  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR     s
    c          O   sC   t  d k r t |  |   St  d k r2 t |  |   St |  |   Sd  S(   NR  R   (   R  t   VisualGrapht   QtGrapht	   BaseGraph(   R  R  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR     s
    c          O   sC   t  d k r t |  |   St  d k r2 t |  |   St |  |   Sd  S(   NR  R   (   R  t	   VisualMapt   QtMapR  (   R  R  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR     s
    g1w7@R  c           B   sN   e  Z e j j d  Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z RS(   i   c         C   sK   t  j j |   | |  _ g  |  _ t j   |  _ |  j d  g  |  _	 d  S(   Ni   (
   R   t   QGraphicsItemRN   t   grapht   edgeListR   t   QPointFt   newPost	   setZValuet	   neighbors(   R#   t   graphWidget(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRN     s    		c         C   s   t  j S(   N(   R   t   Type(   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   type  s    c         C   s   |  j  j |  | j   d  S(   N(   R"  t   appendt   adjust(   R#   t   edge(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   addEdge  s    c         C   s   |  j  S(   N(   R"  (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   edges  s    c         C   sH   |  j    s% |  j    j   |  k r8 |  j   |  _ d  Sd } d } d  S(   Ng        (   R   t   mouseGrabberItemt   posR$  (   R#   t   xvelt   yvel(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR2     s
    %c         C   s   d  S(   N(    (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR4     s    (   R  R  R   R   t   UserTypeR(  RN   R)  R-  R.  R2   R4   (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s   						#R  c           B   s   e  Z d  Z d d d d d d  Z d   Z d   Z d   Z d   Z d	   Z	 d
   Z
 d   Z d   Z d   Z d   Z d   Z RS(   s'   
    The Graph.self.data(start)[5]
    g{Gz?i   s   EpiGrass Viewerc         K   s   | |  _  d | |  _ | |  _ | |  _ | |  _ d |  _ g  |  _ g  |  _ i  |  _	 d |  _
 d |  _ d |  _ d |  _ d |  _ d |  _ t   |  _ d S(   s$   
        Construct a Graph.
        g      ?N(   t   timestept   ratet
   oversamplet   gravityt	   viscosityRO   t   mapR1   R.  R   t
   dragObjectt   clickt   distancet	   timelabelt   rememberFixedt   rememberColorR  R   (   R#   R4  R6  R7  R8  R   t   keywords(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRN      s     														c         C   s?   | |  j  k r; |  j  j |  | |  j | j <|  | _ n  d S(   s/   
        Insert node into the system.
\        N(   R1   R*  R   R   R!  (   R#   R8   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR     s    c         C   s   | |  _  |  | _ d S(   s-   
        Insert map into the system.
        N(   R9  R!  (   R#   t   map_(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt	   insertMap$  s    	c         C   s   t  |  j |  d S(   s?   
        Insert all Nodes in nodelist into the system.
        N(   R9  R   (   R#   t   nodelist(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   insertNodeList+  s    c         C   s&   | |  j  k r" |  j  j |  n  d S(   s.   
        Insert edge into the system.
        N(   R.  R*  (   R#   R,  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR   2  s    c         C   s   t  |  j |  d S(   s?   
        Insert all Edges in edgelist into the system.
        N(   R9  R   (   R#   t   edgelist(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   insertEdgeList9  s    c         C   so   | j  d } g  } xU t |  D]G } x> t | d  D], } | | | f r7 | j | | f  q7 q7 Wq  W| S(   s:   
        Extract edges from the adjacency matrix.
        i    i   (   t   shapeRB   R*  (   R#   R*   t   sizt   elt   ct   l(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   getEdgeFromMatrix?  s    c         C   s   d  S(   N(    (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt
   centerViewM  s    c         C   s-  |  j  |  j } t j d d d  } xt |  j  D] } x1 |  j D]& } | j   | j rE | j   qE qE Wx |  j	 D] } | j
 s |  j r | j |  j  n  |  j r | j |  j  n  | j |  n  | j   | | j 7} | t t |  j	   |  _ qy Wx |  j D] } | j   qWq5 Wd S(   sP   
        Perform one Iteration of the system by advancing one timestep.
        i    N(   R4  R6  R  t   vectort   rangeR.  t   calcSpringForcet   dampingt   calcDampingForceR1   t   fixedR7  t   calcGravityForceR8  t   calcViscosityForcet   calcNewLocationt
   clearForceR0  t   floatRC   t   centerR   (   R#   t	   microstepRY  RF   R,  R8   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR4   P  s&    
				
 c         C   s   d S(   s>   Process the drag and drop interaction from the mouse.
        N(    (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   dispatchDnDi  s    c         C   s   |  j    t j |  j  d S(   sp   Perform one step.  This is a convenience method.
        It actually calls dispatchDnD() and advance().
        N(   R4   Rx   t   sleepR4  (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRf   o  s    
c         C   s   x |  j    q d S(   s[   Start the mainloop, which means that step() is
        called in an infinite loop.
        N(   Rf   (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   mainloopw  s    N(   R  R  R  RO   RN   R   RB  RD  R   RF  RL  RM  R4   R[  Rf   R]  (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s   										t   BaseMapc           B   s)   e  Z d  d d  Z d   Z d   Z RS(   t
   NOME_ZONASt
   ZONA_TRAFEc         C   s   | |  _  | |  _ g  |  _ i  |  _ i  |  _ g  |  _ g  |  _ i  |  _ t j	 j
 |  rj |  j |  n t j	 j d |  } t j	 j
 |  r |  j |  nI t j j d  |  j d  |  j d j | | t j     |  j d   d  S(   Ns   ..s   Map not founds"   Neither {} nor {} were found in {}s   &OK(   R   R   t	   centroidst   centdictt   geomdictR   R   R   t   ost   patht   existst   Readert   joinR   t   QMessageBoxt   informationRO   t   trUtf8t   formatt   getcwd(   R#   R   R   R   t   upfname(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRN     s"    								!c         C   sY  t  j |  } | j d  } d } g  } | j   } x| d k	 r| j   j   } | j   } | j   d k  r| j d  }	 | j	 |  j
  }
 | j |  j  } |	 |  j |
 <|	 j   d k r|	 j   } |  j j |  | j   | j   | j   f |  j |
 <n  g  t |	 j    D] } |	 j |  ^ q!} g  t |	 j    D] } |	 j |  ^ qO} t | |  } | | 7} |  j | |
 |  n  xht | j    D]T} | j |  } x<t | j    D](} | j |  }	 |	 j   d k rt| j	 |  j
  }
 | j |  j  } |	 |  j |
 <|	 j   } |  j j |  | j   | j   | j   f |  j |
 <n  g  t |	 j    D] } |	 j |  ^ q} g  t |	 j    D] } |	 j |  ^ q} t | |  } | | 7} |  j | |
 |  qWqW| j   } q9 W| j   t |  } | j   |  _ t | d d } | |  _ d S(   s0   
        Reads shapefiles vector files.
        i    i   i   R   N(    R    t   Opent   GetLayert   GetNextFeatureRO   t   GetLayerDefnt   GetFieldCountt   GetGeometryReft   GetGeometryCountt   GetFieldAsIntegerR   t   GetFieldAsStringR   Rc  t   GetGeometryTypet   CentroidR   R*  t   GetXt   GetYt   GetZRb  RB   t   GetPointCountt   zipt   dboundt   DestroyR   R   t	   dimensionR   RY  (   R#   R   t   gt   Lt   Nt   tpt   featt   field_countt   geot   g1R   R   t   cenRF   t   xt   yt   lpRJ  t   ringt   cntRY  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRg    sX    +./
+./

c         G   s   d  S(   N(    (   R#   R  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s    (   R  R  RN   Rg  R  (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR^    s   	4R  c           B   s)   e  Z d d  d d  Z d d d  Z RS(   R_  R`  c         C   sA   | |  _  d \ |  _ |  _ |  _ |  _ t j |  | | |  d  S(   Ni   iZ   iLi(   i   iZ   iLi(   R   R   R   R   R   R^  RN   (   R#   R   R   R   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRN     s    	t    c         C   s   t  | | | |  j  } | j |  j k  r3 | j n |  j |  _ | j |  j k  rZ | j n |  j |  _ | j |  j k r | j n |  j |  _ | j |  j k r | j n |  j |  _ |  j j |  | |  j | <| S(   s"   
        Draws a polygon.
        (	   R   R   R   R   R   R   R   R*  R   (   R#   t   polR   R   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s    ''''N(   R  R  RO   RN   R  (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s   R   c           B   so   e  Z d  Z e j j d Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z d	   Z d
   Z RS(   s-   
    Polygons that compose the map on Qt
    i   c         C   s  t  j j |   | |  _ t |  j d d  \ |  _ |  _ t |  j d d  \ |  _	 |  _
 |  j	 |  j d |  j
 |  j d f |  _ |  j	 |  j |  _ |  j
 |  j |  _ | |  _ g  | D] \ } } t j | |  ^ q |  _ t  j |  j  |  _ t j   |  _ t j j |  _ t j j |  _ | |  _ | |  _ |  j t |  j  d |  |  j t j j  |  j  t  j j!  |  j" d  d  S(   NR   i    g       @s    - i   (#   R   R   RN   R   R   R   R   R   R   R   R   RY  R.   t   heightt   plistR   R#  t	   pointListt	   QPolygonFt   polygR$  R   R]   R  R\   R   R   R   R   R   t	   setCursort   PointingHandCursort   setFlagt   ItemIsSelectableR%  (   R#   R  R   R   R'  R  R  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRN     s&    	$$+	.		c         C   s   t  j S(   N(   R   R(  (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR)     s    c         C   s    t  j   } | j |  j  | S(   N(   R   t   QPainterPatht
   addPolygonR  (   R#   Re  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRG    s    c         C   s@   | j  |  j  | j t j |  j d   | j |  j  d  S(   Ni    (   t   setBrushR   R   R   R   R  t   drawPolygonR  (   R#   t   paintert   optiont   widget(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   paint	  s    c         C   s2   | j    } | j   } | j   } |  j   d  S(   N(   t   buttont   scenePosR0  R   (   R#   R$   R  t   sceneposR0  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   mousePressEvent  s    c         C   s&  |  j    r |  j t  |  j j |  j j |  j j |  j  } t j	 t
 | d d  t
 | d d  t
 | d d  t
 | d d   |  _ t j j |  _ |  j j   |  j j j   nT |  j t  t j j |  _ t j j |  _ |  j j |  j |  j  |  j j j   |  j   d S(   s-   
        Toggle selection of polygon
        i    i   i   i   i   N(   t
   isSelectedt   setSelectedR3   R   RK   RS   Rf   R   R   R   R   R   R   R   R]   R  R   R
  Ry   R   R5   RX   t   whiteR   R   R   (   R#   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR     s    )Sc         C   s   |  j    d  S(   N(   R   (   R#   R$   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   mouseReleaseEvent,  s    c         C   s;   | j    } | j   } | j   } |  j j j |  d S(   sK   
        Center the display on the coordinates of the double click
        N(   R  R  R0  R   Rh   R   (   R#   R$   R  R  R0  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   mouseDoubleClickEvent0  s    c         C   s"   t  j |  j |  j |  j |  j  S(   N(   R   R-   R   R   R.   R  (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   boundingRect9  s    (   R  R  R  R   R   R3  R(  RN   R)  RG  R  R  R   R  R  R  (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR     s   									R  c           B   s   e  Z d    Z d   Z RS(   c         K   sA   t  j |   | |  _ g  |  _ i  |  _ d d d d g |  _ d S(   sH   
        Construct a Graph.  to be displayed with Python Visual
        i    N(   R  RN   R   R1   R   t   rect(   R#   R   R@  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRN   ?  s
    			c         C   s  x|  j  D]} | j j   |  j d k  r; | j j   n
 |  j d |  j d <| j j   |  j d k  rz | j j   n
 |  j d |  j d <| j j   |  j d k r | j j   n
 |  j d |  j d <| j j   |  j d k r | j j   n
 |  j d |  j d <q
 W|  j S(   s>   
        Returns the bounding rectangle for the graph
        i    i   i   i   (   R1   R0  R  R  R  (   R#   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   getRectI  s    ???C(   R  R  RN   R  (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR  >  s   	
R  c           B   sh   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z RS(   sO   
    Physical model and visual representation of a node as a mass using Qt
    c         C   sw   t  j |  |  g  |  _ | |  _ | |  _ t j d d d  |  _ d |  _ |  j	 t
 |  j  d |  t |  _ d S(   s#   
        Construct a mass.
        i   i    i   s    - N(   R  RN   R"  R   R   R   R   R   R   R   R   R3   t   selected(   R#   t   graphwR   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRN   Y  s    				c         C   s`  |  j  r t |  _  |  j j j r |  j j j |  j j j |  j j j |  j  } t j	 t
 | d d  t
 | d d  t
 | d d  t
 | d d   |  _ n t j	 d d d  |  _ t j j |  _ |  j j   |  j j j j   n_ t |  _  t j	 d d d  |  _ t j j |  _ |  j j j |  j |  j  |  j j j j   |  j   d  S(   Ni    i   i   i   i   (   R  R3   R!  R   RS   RK   Rf   R   R   R   R   R   R   R   R]   R  R   R
  Ry   R   R5   R  R   R   R   (   R#   R$   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR  h  s    		2V	c         C   s   |  j    d  S(   N(   R   (   R#   R$   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s    c         C   s   t  j S(   N(   R  R(  (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR)    s    c         C   s   |  j  j |  | j   d  S(   N(   R"  R*  R+  (   R#   R,  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR-    s    c         C   s   |  j  S(   N(   R"  (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR.    s    c         C   s,   d } t  j d | d | d | d |  S(   Ng       @ii   (   R   R-   (   R#   R+  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s    c         C   s,   t  j   } | j d d |  j |  j  | S(   Ni(   R   R  t
   addEllipseR   (   R#   Re  (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRG    s    c         C   sd  | j  t j j  | j t j j  | j d d |  j |  j  t j	 d d d  } | j
 t j j @r | j d d  | j d d  | j d t j |  j  j d   | j d t j |  j j d   j d   n/ | j d |  j  | j d |  j j d   | j t j |   | j  t j t j j d   | j d	 d	 |  j |  j  d  S(
   Niii
   i   i   ix   i    i   i(   R   R   R   t   NoPenR  t   darkGrayt   drawEllipseR   R   t   QRadialGradientt   statet   QStylet   State_Sunkent	   setCentert   setFocalPointt
   setColorAtR   R   t   lightt   darkert   QBrushR   R]   (   R#   R  R  R  t   gradient(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s    %1c         C   s+   | t  j j k r n  t  j j |  | |  S(   N(   R   R   t   ItemPositionChanget
   itemChange(   R#   t   changeR?   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s    (   R  R  R  RN   R  R  R)  R-  R.  R  RG  R  R  (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR  U  s   									R  c           B   s|   e  Z e j Z d  e Z e j j d Z	 d   Z
 d   Z d   Z d   Z d   Z d   Z d   Z d	   Z d
   Z RS(   g       @i   c         C   s   t  j j |   d |  _ t j   |  _ t j   |  _ |  j t j	 j
  | |  _ | |  _ |  j j |   |  j j j |  j  |  j j |   |  j j j |  j  |  j   d  S(   Ng      $@(   R   R   RN   R   R   R#  t   sourcePointt	   destPointt   setAcceptedMouseButtonsR   t   NoButtont   sourcet   destR-  R&  R*  R+  (   R#   t
   sourceNodet   destNode(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRN     s    			c         C   s   t  j S(   N(   R  R(  (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR)    s    c         C   s   |  j  S(   N(   R  (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s    c         C   s   | |  _  |  j   d  S(   N(   R  R+  (   R#   R8   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   setSourceNode  s    	c         C   s   |  j  S(   N(   R  (   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s    c         C   s   | |  _  |  j   d  S(   N(   R  R+  (   R#   R8   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   setDestNode  s    	c         C   s   |  j  s |  j r d  St j |  j |  j  d d  |  j |  j d d   } | j   } | d k rj d  St j | j   d | | j   d |  } |  j	   | j
   | |  _ | j   | |  _ d  S(   Ni    g        i
   (   R  R  R   t   QLineFt   mapFromItemt   lengthR#  t   dxt   dyt   prepareGeometryChanget   p1R  t   p2R  (   R#   t   lineR  t
   edgeOffset(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR+    s    6.
c         C   s   |  j  s |  j r t j   Sd } | |  j d } t j |  j t j |  j j   |  j j   |  j j	   |  j j	     j
   j | | | |  S(   Ni   g       @(   R  R  R   R-   R   R  t   QSizeFR  R  R  t
   normalizedt   adjusted(   R#   t   penWidtht   extra(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s    
c   
      C   s  |  j  s |  j r d  St j |  j |  j  } | j   d k rF d  S| j t j	 t j
 j d t j
 j t j
 j t j
 j   | j |  t j | j   | j    } | j   d k r t j | } n  |  j t j t j | t j d  |  j t j | t j d  |  j  } |  j t j t j | t j t j d  |  j t j | t j t j d  |  j  } |  j t j t j | t j d  |  j t j | t j d  |  j  } |  j t j t j | t j t j d  |  j t j | t j t j d  |  j  }	 | j t j
 j  | j t j | j   | | g   | j t j | j   | |	 g   d  S(   Ng        i   i    i   (    R  R  R   R  R  R  R  R   R   R   R   R]   t	   SolidLinet   RoundCapt	   RoundJoint   drawLineR&   t   acosR  R  R  t   TwoPiR#  t   sint   PiR   t   cosR  R  R  R  R  (
   R#   R  R  R  R  t   anglet   sourceArrowP1t   sourceArrowP2t   destArrowP1t   destArrowP2(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s*    :*%1,*%1,%(   R  R  R&   t   piR  R  R   R   R3  R(  RN   R)  R  R  R  R  R+  R  R  (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s   	
								t	   MapServerc           B   s#   e  Z d  Z d d  Z d   Z RS(   s   
    xmlrpc server
    iP  c         C   s@   t  d | f  |  _ d  |  _ d |  _ t j d d  |  _ d  S(   NR  i    RK   i2   (   R   t   serverRO   R9  Rf   R   RR   RK   (   R#   t   porta(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRN     s    		c         C   s   |  j  j   d  S(   N(   R  t   serve_forever(   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   start  s    (   R  R  R  RN   R  (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR    s   R   c           B   s2   e  Z d  d d  Z d   Z d   Z d   Z RS(   g?c         C   sP   t  j j |  |  t  j   |  _ t  j   |  _ | |  _ | |  _ | |  _	 d  S(   N(
   R   t   QThreadRN   t   QMutext   mutext   QWaitConditiont	   conditionRS   RT   t   period(   R#   t   tst   arrR  t   parent(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyRN      s    		c         C   s5   |  j  j   |  j j   |  j  j   |  j   d  S(   N(   R  t   lockR  t   wakeOnet   unlockt   wait(   R#   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   __del__'  s    c         C   s    t  j |  j  } |  j   d  S(   N(   R   t   QMutexLockerR  R  (   R#   t   locker(    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR  ,  s    c         C   s   x t  t |  j   D] } |  j j   |  j t j d  | |  j |  |  j j   |  j	 j
 |  r |  j t j d  | |  j	 |  n  t j |  j  q W|  j j   |  j j |  j  |  j j   d  S(   NR   R   (   RB   RC   RS   R  R  t   emitR   Rn   R  RT   R   Rx   R\  R  R  R  (   R#   R   (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   run/  s    #&N(   R  R  RO   RN   R  R  R  (    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyR     s   		t   __main__i    s   riozonas_LatLong.shpR_  R`  (J   R  R&   t   osgeoR    t	   threadingRU   Rx   Rd  t   syst   PyQt4R   R   R   R   R   R{   t   numpyR   R   R   t   numpy.randomR	   R
   t
   Ui_displayR   t
   matplotlibR   t   typesR   R   R  R"   R)   R   R:   R<   R@   RG   RI   RM   RJ   t   objectR  t   BaseCylinderR   R   R   R   t   rhoR   R  R  R^  R  R   R  R  R  R  R  R   R  t   QApplicationt   argvt   appt   qsrandt   QTimet   secsTot   currentTimeR  R   R  t   exitt   exec_(    (    (    sL   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/dgraph.pyt   <module>   s^   $(								 6				AQX^^.	
