
    JeH                     l    d Z ddlZddlmZmZ ddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ  G d d	      Zy)
z
Layout Plugin API.
    N)QRectFQt)QGridLayoutQPlainTextEditQWidget)SpyderAPIError)PLUGIN_REGISTRY)_c                   Z    e Zd ZdZdZ	 d Zd Zd Zd Zdddd	g fd
Z	d Z
d ZddZd Zy)BaseGridLayoutTypea  
    A base layout type to create custom layouts for Spyder panes.

    The API for this plugin is a subset of a QGridLayout, so the same
    concepts, like row, column, spans and stretches apply.

    Notes
    -----
    See: https://doc.qt.io/qt-5/qgridlayout.html
    Nc                     || _         g | _        g | _        i | _        i | _        d| _        d | _        g | _        d| _        d| _	        g | _
        y )NFr   )plugin_areas_area_rects_column_stretchs_row_stretchs_default_added_default_area_visible_areas_rows_cols_plugin_ids)selfparent_plugins     ;/usr/lib/python3/dist-packages/spyder/plugins/layout/api.py__init__zBaseGridLayoutType.__init__'   sV    # "#! 

    c           	         g | _         | j                  t        d      | j                          | j                  st        d      g }d}| j                  D ]  }|j                  |d          |d   r|| _        | j                   j                  |d          |r|d   dk(  r|d	   dk(  rt        d
      |d   dk(  r
|d	   dk(  rd}t        |d         t        |d         k  rt        dj                  | j                         |d   |d                t        | j                         st        d      t        |      st        d      |j                  d      dk7  rt        d      |st        d      | j                          y)z:
        Check the current layout is a valid one.
        Nz-A Layout must define an `ID` class attribute!z(A Layout must define add least one area!Fdefaultvisiblerowr   columnz1Multiple areas defined their row and column as 0!Thidden_plugin_ids
plugin_idszAt least 1 hidden plugin id is not being specified in the area plugin ids list!
 SpyderLayout: {}
 hidden_plugin_ids: {}
plugin_ids: {}z!At least 1 area must be `visible`zNo area is the `default`!   z!Only 1 area can be the `default`!z51 area needs to be specified with row 0 and column 0!)r   IDr   get_namer   appendr   setformatanycount_check_area)r   default_areasarea_zero_zeroareas       r   _check_layout_validityz)BaseGridLayoutType._check_layout_validity6   s    !77?  ". / / 	 {{ !KLLKK 	AD  i1I%)"&&tI7$u+"2tH~7J$GI I E{aDNa$7!%t/01Sl9K5LL$% &,VDMMO,01D,E,0,>&@	A A	A0 4&&' !DEE =! !<== t$) !DEE  GI I 	r   c                 8   g | _         | j                  dz   }g }d}t        | j                        D ]8  \  }}t	               }|j                  |d   |z          |j                  |d   |d   z   |z
         |j                  ||d   z
  |z
         |j                  ||d   z
  |d   z
  |z          ||_	        |d   |_
        |j                  |       t	               }|j                  |d          |j                  |d   |d   z          |j                  ||d   z
         |j                  ||d   z
  |d   z
         ||_	        |d   |_
        | j                   j                  |       ; |D ]c  }|D ]\  }	|j                  |	j                  k7  s|j                  |	      s/t        dj                  |j                  |	j                               e d	}
g }g }t        | j                         D ]  \  }}|j                  |j                                |j                  |j!                                t#        |j%                         |j'                         z        }|
|z  }
|| j                  |   d
<    |
t)        |      t)        |      z  k7  rt        d      y)z
        Check if the current layout added areas cover the entire rectangle.

        Rectangle given by the extreme points for the added areas.
        r%   g-C6?r"   col_spanr!   row_spanr$   z:Area with plugins {0} is overlapping area with plugins {1}r   r0   zpAreas are not covering the entire section!
Either an area is missing or col_span/row_span are not correctly set!N)r   r   	enumerater   r   setLeftsetRightsetTop	setBottomindexr$   r(   
intersectsr   r*   toprightabswidthheightmax)r   r@   area_float_rectsdeltar:   r0   rectfrectrect_1rect_2
total_areatopsrightss                r   r-   zBaseGridLayoutType._check_areau   s    a$T[[1 	*KE4 HEMM$x.501NN4>D,<<uDELL$u+-56OOFT%[04
3CCeKLEK#L1E##E* 8DLLh(MM$x.4
+;;<KKe,-NN6DK/$z2BBCDJ"<0DO##D)+	*0 ' 	JF* J<<6<</((0,//5vf6G6G6<6G6G0IJ JJ	J 
$T%5%56 	.KE4KK
#MM$**,'tzz|dkkm34D$J)-DKKv&	. VSY.. %  /r   c                     t        d      )z
        Return the layout localized name.

        Returns
        -------
        str
            Localized name of the layout.

        Notes
        -----
        This is a method to be able to update localization without a restart.
        z)A layout must define a `get_name` method!)NotImplementedError)r   s    r   r'   zBaseGridLayoutType.get_name   s     ""MNNr   r%   FTc	                 n   | j                   r|rt        d      | xj                  |z  c_        t        || j                        | _        t        || j
                        | _        || _         d| j                  |<   d| j                  |<   | j                  j                  t        ||||||||             y)a	  
        Add a new area and `plugin_ids` that will populate it to the layout.

        The area will start at row, column spanning row_pan rows and
        column_span columns.

        Parameters
        ----------
        plugin_ids: list
            List of plugin ids that will be in the area
        row: int
            Initial row where the area starts
        column: int
            Initial column where the area starts
        row_span: int, optional
            Number of rows that the area covers
        col_span: int, optional
            Number of columns the area covers
        default: bool, optiona
            Defines an area as the default one, i.e all other plugins that where
            not passed in the `plugins_ids` will be added to the default area.
            By default is False.
        visible: bool, optional
            Defines if the area is visible when setting up the layout.
            Default is True.

        Notes
        -----
        See: https://doc.qt.io/qt-5/qgridlayout.html
        z,A default location has already been defined!r%   )r$   r!   r"   r4   r3   r   r    r#   N)r   r   r   rA   r   r   r   r   r   r(   dict)	r   r$   r!   r"   r4   r3   r   r    r#   s	            r   add_areazBaseGridLayoutType.add_area   s    N 7  ", - - 	J&djj)
,
%()f%"#3%!!"3		
r   c                 "    || j                   |<   y)a  
        Set the factor of column to stretch.

        The stretch factor is relative to the other columns in this grid.
        Columns with a higher stretch factor take more of the available space.

        Parameters
        ----------
        column: int
            The column number. The first column is number 0.
        stretch: int
            Column stretch factor.

        Notes
        -----
        See: https://doc.qt.io/qt-5/qgridlayout.html
        N)r   )r   r"   stretchs      r   set_column_stretchz%BaseGridLayoutType.set_column_stretch  s    $ )0f%r   c                 "    || j                   |<   y)a  
        Set the factor of row to stretch.

        The stretch factor is relative to the other rows in this grid.
        Rows with a higher stretch factor take more of the available space.

        Parameters
        ----------
        row: int
            The row number. The first row is number 0.
        stretch: int
            Row stretch factor.

        Notes
        -----
        See: https://doc.qt.io/qt-5/qgridlayout.html
        N)r   )r   r!   rQ   s      r   set_row_stretchz"BaseGridLayoutType.set_row_stretch  s    $ #*3r   c           	         ddl m}  |       }t               }t               }| j                  D ]  }t               }|j                  d       |j                  dj                  |d                |d   s|r!|j                  ||d   |d   |d	   |d
          |d   r|j                  d       |d   r|j                  d        | j                  j                         D ]  \  }}	|j                  ||	        | j                  j                         D ]  \  }
}	|j                  |
|	        |j!                  |       |j#                          |j%                          y)zI
        Show the layout with placeholder texts using a QWidget.
        r   )qapplicationT
r$   r    r!   r"   r4   r3   r   z+QPlainTextEdit {background-color: #ff0000;}z+QPlainTextEdit {background-color: #eeeeee;}N)spyder.utils.qthelpersrV   r   r   r   r   setReadOnlysetPlainTextjoin	addWidgetsetStyleSheetr   itemssetRowStretchr   setColumnStretch	setLayoutshowMaximizedexec_)r   show_hidden_areasrV   appwidgetlayoutr0   labelr!   rQ   cols              r   preview_layoutz!BaseGridLayoutType.preview_layout+  s_    	8nKK 	CD"$Ed#tyyl);<=I"3  KN$$ I##AC 	?##AC'	C, !..446 	/LC  g.	/ !11779 	2LC##C1	2 	 		r   c           	      4   g }g }|D ]  }|j                  |j                         |j                  t        j                  v r5|j                  j                         r|j                  |j                         |j                  d        t        j                  | j                        }t        t        | j                        t        |      z        }|dxx   |z  cc<   |dxx   |z  cc<   | j                  D cg c]  }|d   r|n| }	}i }
|	D ]m  }|}|d   d   }|j                  |d      }|s#|j                  }||
|d   |d   f<   |d	   |_        |d
   |_        |d   |_        |j                  |d          o g }t$        j&                  }t)        d| j*                  dz         D ]  }d}t)        d| j,                  dz         D ]b  }||f}||
v s||
|   }n.|j                  d|
|   j                  z  |||
|   |f       |
|   }|j/                  t$        j0                  ||       d  t$        j2                  }t)        d| j,                  dz         D ]a  }d}t)        d| j*                  dz         D ]A  }||f}||
v s||
|   }|j                  d|
|   j                  z  |||
|   |f       |
|   }C c t5        |d       }|D ]  \  }}}}}|j7                  |||        g }|	D ]  }|d   }|j                  |d   d   d      }|s$|d   dd }|d   }|D ]  }|j                  |d      }|s||v r t9        |d      r|j                  ||f       =| j:                  j=                  ||       ||vr|j                  |       o|j                  d        |d   s|j                  j?                          |j                  jA                           |D ]M  \  }}| j:                  jC                  |      s| j:                  j=                  ||       j                  d       O g }g }|
jE                         D ]b  \  }}| jF                  jE                         D ]@  \  }}|d   |k(  s|j                   dk(  s|j                  |       |j                  |       B d g }g } |
jE                         D ]b  \  }}| jH                  jE                         D ]@  \  }}|d   |k(  s|j"                  dk(  s|j                  |       | j                  |       B d |jK                          |jM                  ||t$        j&                         |jM                  || t$        j2                         |D ]  }|j                  |d      }|s|jO                  d       |j                  j?                          |jP                  jS                  d       |jO                  d       |j                  jT                  }!|!s|!jW                  d        yc c}w )z
        Set the given mainwindow layout.

        First validate the current layout definition, then clear the mainwindow
        current layout and finally calculate and set the new layout.
        Fr$   r#   r   r   )errorr!   r"   r0   r3   r4   r    r%   Nc                     | d   | d   fS )Nr   r%    )xs    r   <lambda>z;BaseGridLayoutType.set_main_window_layout.<locals>.<lambda>  s    1qt r   )keyTABIFYT),r(   NAMEr	   external_plugins
dockwidget	isVisibletoggle_viewcopydeepcopyr   listr)   r   r   
get_pluginr0   r3   r4   r   
Horizontalranger   r   addDockWidgetLeftDockWidgetAreaVerticalsortedsplitDockWidgethasattrr   tabify_pluginsshowraise_tabify_pluginr^   r   r   rb   resizeDocksblockSignalstoggle_view_action
setCheckeddock_tabbarsetCurrentIndex)"r   main_windowdockable_pluginsall_plugin_idsexternal_plugins_to_showr   patched_default_areaunassgined_plugin_idsr0   patched_areasdockscurrent_area	plugin_iddocklayout_data	directionr!   ri   rq   sorted_datafirstsecondplugins_to_tabifyarea_visiblebase_pluginr$   r#   current_plugincolumn_dockscolumn_stretchesrQ   	row_docksrow_stretchesr   s"                                     r   set_main_window_layoutz)BaseGridLayoutType.set_main_window_layoutT  s     $&  ' 
	&F!!&++. ???%%//1(//<u%
	&  $}}T-?-?@ $  !C$77!9\*.CC*015JJ1 % %)O =% %
 ! 
	4DL$\215I ++IU+CF((GK|E*L,BCD L	 $Z 0 $Z 0""4	?3
	4  MM	DJJN+ 	@CDQ

Q/ @Cj%<|$Sz#**uSz. !"3Z&	()  %Sz----tY@@	@( KK	DJJN+ 	*CDQ

Q/ *Cj%<|$Sz#**uSz. !"3Z&	()  %Sz*	*$ [.DE3> 	B/D#ufi''vyA	B ! 	4D	?L%00\"1%U 1 4K!,/3
$()<$=!!+ BI%0%;%;! &< &0N%%)>> ' A-44!/ =? !KK66 +^=(0AA . : :< H . : :5 AB" 	?**//1**1135	4< $5 	.FK;;,,V4**;?&&u-	.
  	5IC $ 5 5 ; ; = 5Wq6S=T]]a%7 ''-$++G45	5 	 	2IC $ 2 2 8 8 : 2Wq6S=T]]a%7$$T*!((12	2 	!!#.>N	="++F 2 	3I ++IU+CF##D)!!&&())44T:##E* %//;;//2	3y%s   :X)F)__name__
__module____qualname____doc__r&   r   r1   r-   r'   rO   rR   rT   rj   r   rn   r   r   r   r      sU    	 
B2=~;~O* #%<
|0(*('Rm3r   r   )r   rx   qtpy.QtCorer   r   qtpy.QtWidgetsr   r   r   spyder.api.exceptionsr   'spyder.api.plugin_registration.registryr	   spyder.api.translationsr
   r   rn   r   r   <module>r      s1   
  # ? ? 1 C %i3 i3r   