
,]c           @   s  d  Z  d d l Z d d l Z d d l Z d d l m Z m Z m Z d d l m	 Z	 m
 Z
 m Z m Z m Z m Z m Z d d l m Z m Z m Z m Z d d l m Z d d l m Z m Z d d l m Z m Z d d	 l m Z m Z m  Z  m! Z! m" Z" d d
 l# m$ Z% d d l& m' Z' d d l( m) Z) d e f d     YZ* d e f d     YZ+ d e f d     YZ, d e	 f d     YZ- d   Z. e/ d k re.   n  d S(   s   Simple web browser widgetiN(   t   QUrlt   Signalt   Slot(   t   QFramet   QHBoxLayoutt   QLabelt   QProgressBart   QMenut   QVBoxLayoutt   QWidget(   t   QWebEnginePaget   QWebEngineSettingst   QWebEngineViewt	   WEBENGINE(   t	   QFontInfo(   t   _t   DEV(   t   is_text_stringt   to_text_string(   t   action2buttont   add_actionst   create_actiont   create_toolbuttont   create_plugin_layout(   t   icon_manager(   t   UrlComboBox(   t   FindReplacet   WebPagec           B   s#   e  Z d  Z e e  Z d   Z RS(   s   
    Web page subclass to manage hyperlinks for WebEngine

    Note: This can't be used for WebKit because the
    acceptNavigationRequest method has a different
    functionality for it.
    c         C   s'   | t  j k r# |  j j |  t St S(   s=   
        Overloaded method to handle links ourselves
        (   R
   t   NavigationTypeLinkClickedt   linkClickedt   emitt   Falset   True(   t   selft   urlt   navigation_typet   isMainFrame(    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   acceptNavigationRequest+   s    (   t   __name__t
   __module__t   __doc__R   R    R   R%   (    (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR   !   s   t   WebViewc           B   s   e  Z d  Z d   Z e e e e e d  Z d   Z d   Z d e e d  Z	 d d  Z d   Z d	   Z d
   Z e   d    Z e   d    Z d   Z d   Z e   d  Z RS(   s   Web viewc         C   s   t  j |  |  d |  _ t |  t d  d t j d  d |  j |  _ t |  t d  d t j d  d |  j	 |  _
 t r t |   } |  j |  d |  _ n  d  S(	   Ng      ?s   Zoom outt   icont   zoom_outt	   triggereds   Zoom int   zoom_int    (   R   t   __init__t   zoom_factorR   R   t   imaR*   R+   t   zoom_out_actionR-   t   zoom_in_actionR   R   t   setPaget   source_text(   R!   t   parentt   web_page(    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR/   7   s    	c         C   s]   t  s t j } n d } | s. | t j B} n  | rD | t j B} n  |  j | t j |   S(   s	   Find texti    (   R   R
   t   FindWrapsAroundDocumentt   FindBackwardt   FindCaseSensitivelyt   findTextt	   FindFlags(   R!   t   textt   changedt   forwardt   caset   wordst   regexpt   findflag(    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt	   find_textE   s    c         C   s
   |  j    S(   s+   Return text selected by current text cursor(   t   selectedText(   R!   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   get_selected_textU   s    c         C   s   | |  _  d S(   s9   Set source text of the page. Callback for QWebEngineView.N(   R5   (   R!   R5   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   set_source_textY   s    R.   c         C   s   t  |  } | s d S| s. t j |  } n  | s t rb |  j   j |  j  t  |  j  } q t  |  j   j   j    } n  y1 | r t j	 |  } n t j	 | t j
  } Wn t j k
 r d SXd } x! | j |  D] } | d 7} q W| S(   s0   Get the number of matches for the searched text.i    Ni   (   R   t   ret   escapeR   t   paget   toPlainTextRG   R5   t	   mainFramet   compilet
   IGNORECASEt   sre_constantst   errort   finditer(   R!   t   patternR5   R@   RB   t   regobjt   number_matchest   match(    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   get_number_matches]   s*    c         C   s   t  |  } |  j   j   } xB | j | j | j | j | j f D] } | j | | j	    qC W| d  k	 r | j | j | j	    n  | j   } | j | j |  | j | j |  d  S(   N(   R   RJ   t   settingst   StandardFontt	   SerifFontt   SansSerifFontt   CursiveFontt   FantasyFontt   setFontFamilyt   familyt   Nonet	   FixedFontt	   pixelSizet   setFontSizet   DefaultFontSizet   DefaultFixedFontSize(   R!   t   fontt
   fixed_fontRW   t
   fontfamilyt   size(    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   set_fontz   s    c         C   s6   t  |  d  r" |  j |  j  n |  j |  j  d S(   s   Apply zoom factort   setZoomFactorN(   t   hasattrRj   R0   t   setTextSizeMultiplier(   R!   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   apply_zoom_factor   s    c         C   s   | |  _  |  j   d S(   s   Set zoom factorN(   R0   Rm   (   R!   R0   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   set_zoom_factor   s    	c         C   s   |  j  S(   s   Return zoom factor(   R0   (   R!   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   get_zoom_factor   s    c         C   s'   t  d |  j d  |  _ |  j   d S(   s   Zoom outg?N(   t   maxR0   Rm   (   R!   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR+      s    c         C   s   |  j  d 7_  |  j   d S(   s   Zoom ing?N(   R0   Rm   (   R!   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR-      s    c         C   s/   d d  l  } | j t |  j   j     d  S(   Ni(   t
   webbrowsert   openR   R"   t   toString(   R!   t   webwindowtypeRq   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   createWindow   s    c         C   s   t  |   } |  j t j  |  j t j  d  |  j t j  |  j t j  d  |  j |  j	 g } t
 r t r |  j   j   } | j t j t  | d  |  j t j  g 7} n  t | |  | j | j    | j   d  S(   N(   R   t
   pageActionR
   t   Backt   ForwardR_   t	   SelectAllt   CopyR3   R2   R   R   RJ   RW   t   setAttributeR   t   DeveloperExtrasEnabledR    t   InspectElementR   t   popupt	   globalPost   accept(   R!   t   eventt   menut   actionsRW   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   contextMenuEvent   s    c         C   sY   t  r< |  j t  t t |   j | |  |  j t  n t t |   j | |  d S(   s   
        Reimplement Qt method to prevent WebEngine to steal focus
        when setting html on the page

        Solution taken from
        https://bugreports.qt.io/browse/QTBUG-52999
        N(   R   t
   setEnabledR   t   superR)   t   setHtmlR    (   R!   t   htmlt   baseUrl(    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR      s
    N(   R&   R'   R(   R/   R    R   RD   RF   RG   RV   R_   Ri   Rm   Rn   Ro   R   R+   R-   Ru   R   R    R   (    (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR)   5   s$   										t
   WebBrowserc           B   s   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z e	   d    Z
 d   Z d   Z d	   Z d
   Z d   Z d   Z e	 e  d    Z RS(   s   
    Web browser widget
    c            s  t  j  |  d   _ t    _  j j j  j   j j	 j  j
   j j j  j  t  d t j d  d t d  d  j } t  j j  } t  j j  }  f d   } | t j  } | t j   | t j  } | t j  }  j t   j j j  f d     j j j  f d    t       j t    j    j j j   j    j j! j   j"   j j j   f d	    t#  j$    }	 t%    _&  j& j' j  j(  t) s j j* j  j+  n  t,    _-  j- j.  j   j- j   t  d t j d
  d t d  d  j/ }
  j- j0 j |
 j1  t2   } x? | | | |
 |	  j& | | |    f D] } | j3 |  qWt4 |  } | j3  j  | j3  j-   j5 |  d  S(   NR*   t   homet   tipt   HomeR,   c            s   t    j j |   d   j S(   NR6   (   R   t   webviewRv   (   t   prop(   R!   (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   <lambda>   s   c              s     j  t  S(   N(   R   R    (    (   t   stop_button(    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR      R.   c              s     j  t  S(   N(   R   R   (    (   R   (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR      R.   c            s
     j    S(   N(   t   hide(   t   _state(   t   progressbar(    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR      R.   t   finds	   Find textt   toggled(6   R	   R/   R_   t   home_urlR)   R   t   loadFinishedt   connectt   load_finishedt   titleChangedt   setWindowTitlet
   urlChangedt   url_changedR   R1   R*   R   t   go_homeR   R2   R3   R
   t   Reloadt   StopRw   Rx   R   R   t   loadStartedR   t   setTextVisibleR   t   showt   loadProgresst   setValueR   t	   get_labelR   t	   url_combot   validt   url_combo_activatedR   t   iconChangedt   icon_changedR   t   find_widgett
   set_editort   toggle_find_widgett   visibility_changedt
   setCheckedR   t	   addWidgetR   t	   setLayout(   R!   R6   t   home_buttont   zoom_out_buttont   zoom_in_buttont   pageact2btnt   refresh_buttont   previous_buttont   next_buttont   labelt   find_buttont   hlayoutt   widgett   layout(    (   R   R!   R   s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR/      s\    	
	c         C   s
   t  d  S(   s   Return address label texts   Address:(   R   (   R!   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR     s    c         C   s   t  |  |  _ d S(   s   Set home URLN(   R    R   (   R!   R=   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   set_home_url  s    c         C   s   |  j  |  |  j |  d S(   s   Set current URLN(   R   t   go_to(   R!   R"   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   set_url  s    c         C   s5   t  |  r t |  } n | } |  j j |  d S(   s   Go to page *address*N(   R   R    R   t   load(   R!   t   url_or_textR"   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR     s    c         C   s&   |  j  d k	 r" |  j |  j   n  d S(   s   Go to home pageN(   R   R_   R   (   R!   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR      s    c         C   s
   t  |  S(   s%   Convert text address into QUrl object(   R    (   R!   R=   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   text_to_url&  s    c         C   s/   t  |  j j    } |  j |  j |   d S(   s"   Load URL from combo box first itemN(   R   R   t   currentTextR   R   (   R!   R   R=   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR   *  s    c         C   s#   | s |  j  j t d   n  d  S(   Ns   Unable to load page(   R   R   R   (   R!   t   ok(    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR   /  s    c         C   s
   | j    S(   s2   Convert QUrl object to displayed text in combo box(   Rs   (   R!   R"   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   url_to_text3  s    c         C   s   |  j  j |  j |   d S(   s3   Displayed URL has changed -> updating URL combo boxN(   R   t   add_textR   (   R!   R"   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR   7  s    c         C   s?   |  j  j |  j  j   |  j j    |  j |  j j    d  S(   N(   R   t   setItemIcont   currentIndexR   R*   t   setWindowIcon(   R!   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR   ;  s    c         C   s'   | r |  j  j   n |  j  j   d  S(   N(   R   R   R   (   R!   t   state(    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR   @  s    N(   R&   R'   R(   R_   R/   R   R   R   R   R   R   R   R   R   R   R   R   t   boolR   (    (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR      s   >										t   FrameWebViewc           B   s;   e  Z d  Z e e  Z d   Z d   Z e d    Z	 RS(   s=   
    Framed QWebEngineView for UI consistency in Spyder.
    c         C   s   t  j |  |  t |   |  _ t   } | j |  j  | j d d d d  |  j |  |  j t  j	 t  j
 B t r |  j j   j j |  j  n |  j j j |  j  d  S(   Ni    (   R   R/   R)   t   _webviewR   R   t   setContentsMarginsR   t   setFrameStylet   StyledPanelt   SunkenR   RJ   R   R   (   R!   R6   R   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR/   N  s    	c         C   s   t  |  j |  S(   N(   t   getattrR   (   R!   t   name(    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   __getattr___  s    c         C   s   |  j  S(   N(   R   (   R!   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt
   web_widgetb  s    (
   R&   R'   R(   R   R    R   R/   R   t   propertyR   (    (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyR   H  s
   		c          C   s`   d d l  m }  |  d d  } t   } | j   | j d  | j   t j | j    d S(   s   Run web browseri(   t   qapplicationt	   test_timei   s   https://www.google.com/N(	   t   spyder.utils.qthelpersR   R   R   R   R   t   syst   exitt   exec_(   R   t   appR   (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   testg  s    	

t   __main__(0   R(   RH   RO   R   t   qtpy.QtCoreR    R   R   t   qtpy.QtWidgetsR   R   R   R   R   R   R	   t   qtpy.QtWebEngineWidgetsR
   R   R   R   t
   qtpy.QtGuiR   t   spyder.config.baseR   R   t   spyder.py3compatR   R   R   R   R   R   R   R   t   spyder.utilsR   R1   t   spyder.widgets.comboboxesR   t   spyder.widgets.findreplaceR   R   R)   R   R   R   R&   (    (    (    s5   lib/python2.7/site-packages/spyder/widgets/browser.pyt   <module>   s(   4"(	