
    JeO                        d Z ddlZddlZddlZddlZddl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mZ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dl!m"Z" ddl#m$Z$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1  G d d      Z2 G d d      Z3 G d d      Z4 G d de      Z5 G d dee      Z6 G d de      Z7 G d de      Z8d  Z9e:d!k(  r e9        yy)"zSimple web browser widget    N)PYQT5)QEventQtQUrlSignalSlot)	QFontInfo)	WEBENGINEQWebEnginePageQWebEngineSettingsQWebEngineView)QFrameQHBoxLayoutQLabelQProgressBarQWidget_)SpyderWidgetMixin)DEV)OLD_PYQT)is_text_stringto_text_string)ima)QStylePalette)action2buttoncreate_plugin_layoutcreate_toolbutton)UrlComboBox)FindReplacec                   0    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zy
)WebViewActionszoom_in_actionzoom_out_actionback_actionforward_actionselect_all_actioncopy_actioninspect_actionstop_actionrefresh_actionN)__name__
__module____qualname__ZoomInZoomOutBackForward	SelectAllCopyInspectStopRefresh     8/usr/lib/python3/dist-packages/spyder/widgets/browser.pyr"   r"   '   s/    FGDG#IDGDGr9   r"   c                       e Zd ZdZdZdZdZy)WebViewMenuSectionsmove_sectionselect_sectionzoom_sectionextras_sectionN)r,   r-   r.   MoveSelectZoomExtrasr8   r9   r:   r<   r<   3   s    DFDFr9   r<   c                       e Zd ZdZy)WebViewMenuscontext_menuN)r,   r-   r.   Contextr8   r9   r:   rF   rF   :   s    Gr9   rF   c                   2     e Zd ZdZ ee      Z fdZ xZS )WebPagez
    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                     |t         j                  k(  r| j                  j                  |       yt        t
        |   |||      S )z=
        Overloaded method to handle links ourselves
        F)r   NavigationTypeLinkClickedlinkClickedemitsuperrJ   acceptNavigationRequest)selfurlnavigation_typeisMainFrame	__class__s       r:   rP   zWebPage.acceptNavigationRequestJ   sE     nFFF!!#&Wd;+/ 	/r9   )	r,   r-   r.   __doc__r   r   rM   rP   __classcell__rU   s   @r:   rJ   rJ   @   s     ,K	/ 	/r9   rJ   c                        e Zd ZdZ e       Z	  e       Z	 d fd	Zi fdZ	 	 ddZ	d Z
d Z	 	 ddZddZd	 Zd
 Zd Z e       d        Z e       d        Zd Zd Z e       f fd	Z fdZ fdZ xZS )WebViewz
    Web view.
    c                 (   ||n|}t         rt        | 	  ||       n-t        j                  | |       t	        j                  | |       d| _        d | _        t        r2|rt        |       }nt        |       }| j                  |       d| _        y y )N)class_parentg      ? )r   rO   __init__r   r   zoom_factorrG   r
   rJ   r   setPagesource_text)rQ   parenthandle_linksr\   web_pagerU   s        r:   r^   zWebView.__init__d   s    !-!5v<GV,?##D&1&&t,G "4=)$/LL"!D r9   c                 	   | j                  t        j                        }| j                  t        j                  t        d      | j                  d      |j                  t        j                        }| j                  t        j                        }| j                  t        j                  t        d      | j                  d      |j                  t        j                        }| j                  t        j                        }| j                  t        j                  t        d      |j                  t        j                        }| j                  t        j                        }| j                  t        j                  t        d      |j                  t        j                        }	| j                  t        j                  t        d	      | j                  d
      | j                  t        j                        | _        | j                  t        j                   t        d      | j                  d      | j"                  t        j                        | _        | j                  t        j&                        }
| j                  t        j(                  t        d      |
j                  t        j                        }| j                  t        j*                        }| j                  t        j,                  t        d      | j                  d      |j                  t        j                         | j                  t        j.                        }| j                  t        j.                  t        d      | j                  d      |j                  t        j                         | j1                  t2        j4                        }|| _        ||fD ]$  }| j9                  ||t:        j<                         & ||	fD ]$  }| j9                  ||t:        j>                         & | j                  | j$                  fD ]$  }| j9                  ||t:        j@                         & | j9                  ||t:        jB                         tD        rVtF        sP| jI                         jK                         }|jM                  tN        jP                  d       |jS                  d       y |jS                  d       y )Nr1   previous)nametexticon	triggeredcontextr2   nextz
Select all)rg   rh   rj   rk   r4   zZoom inzoom_inzZoom outzoom_outr5   r7   refreshr6   stop)menusectionTF)*
pageActionr   r1   create_actionr"   r   create_icontriggerr   WidgetWithChildrenShortcutr2   r3   r4   r/   rm   r#   r0   rn   r$   InspectElementr5   Reloadr7   r6   create_menurF   rH   rG   add_item_to_menur<   rA   rB   rC   rD   r   r
   pagesettingssetAttributer   DeveloperExtrasEnabled
setVisible)rQ   optionsoriginal_back_actionr%   original_forward_actionr&   original_select_actionr'   original_copy_actionr(   original_inspect_actionr)   original_refresh_actionoriginal_stop_actionrq   itemr}   s                    r:   setupzWebView.setupx   s1   #~/B/BC(($$6!!*-*2211 ) 
 #'//.2H2H"I++''9!!&)-5511 , 
 "&1I1I!J ..))<,4411	 / 
  $~/B/BC(($$6*2211	 ) 
 #00&&9!!),ll11 1 
  $11'':!!*-mm11  2  
 #'//))#+++''9-5511	 , 
 #'//.2G2G"H''9!!),-5511 	 	
  $~/B/BC$$6!!&)*2211 	 	
  4 45  .1 	D!!+00 " 	 '4 	D!!+22 " 	 (($*>*>? 	D!!+00 " 	 	'.. 	 	
 yyy{++-H!!"4"K"K"&(%%d+%%e,r9   c                     t         st        j                  }nd}|s|t        j                  z  }|r|t        j                  z  }| j                  |t        j                  |            S )z
Find text.r   )r
   r   FindWrapsAroundDocumentFindBackwardFindCaseSensitivelyfindText	FindFlags)rQ   rh   changedforwardcasewordregexpfindflags           r:   	find_textzWebView.find_text   sY     %==HH."="==H."D"DDH}}T>#;#;H#EFFr9   c                 "    | j                         S )z+Return text selected by current text cursor)selectedTextrQ   s    r:   get_selected_textzWebView.get_selected_text   s      ""r9   c                     || _         y)z9Set source text of the page. Callback for QWebEngineView.N)ra   )rQ   ra   s     r:   set_source_textzWebView.set_source_text   s
    &r9   c                    t        |      }|sy|st        j                  |      }|szt        r?| j	                         j                  | j                         t        | j                        }n5t        | j	                         j                         j                               }|rdj                  |      }	 |r%t        j                  |t        j                        }n5t        j                  |t        j                  t        j                  z        }d}|j                  |      D ]  }|dz  }	 |S # t        j                  $ r Y yw xY w)z0Get the number of matches for the searched text.r   z\b{pattern}\b)patternN   )r   reescaper
   r|   toPlainTextr   ra   	mainFrameformatcompile	MULTILINE
IGNORECASEsre_constantserrorfinditer)	rQ   r   ra   r   r   r   regobjnumber_matchesmatchs	            r:   get_number_matcheszWebView.get_number_matches  s    !)ii(G		''(<(<=,T-=-=>,		--/;;=? &--g->G	GR\\:GR\\BMM-IJ __[1 	 EaN	   "" 		s   7AD2 2EEc                    t        |      }| j                         j                         }|j                  |j                  |j
                  |j                  |j                  fD ]"  }|j                  ||j                                $ |*|j                  |j                  |j                                |j                         }|j                  |j                  |       |j                  |j                  |       y N)r	   r|   r}   StandardFont	SerifFontSansSerifFontCursiveFontFantasyFontsetFontFamilyfamily	FixedFont	pixelSizesetFontSizeDefaultFontSizeDefaultFixedFontSize)rQ   font
fixed_fontr}   
fontfamilysizes         r:   set_fontzWebView.set_font$  s    99;'')#00(2D2D#1183G3G#//1 	>J "":t{{}=	> !""8#5#5z7H7H7JK~~X55t<X::DAr9   c                     t        | d      r| j                  | j                         y| j                  | j                         y)zApply zoom factor.setZoomFactorN)hasattrr   r_   setTextSizeMultiplierr   s    r:   apply_zoom_factorzWebView.apply_zoom_factor1  s6    4)t//0 &&t'7'78r9   c                 2    || _         | j                          y)zSet zoom factor.Nr_   r   )rQ   r_   s     r:   set_zoom_factorzWebView.set_zoom_factor:  s    & r9   c                     | j                   S )zReturn zoom factor.)r_   r   s    r:   get_zoom_factorzWebView.get_zoom_factor?  s    r9   c                 `    t        d| j                  dz
        | _        | j                          y)z	Zoom out.皙?N)maxr_   r   r   s    r:   rn   zWebView.zoom_outC  s*     r4#3#3B#67 r9   c                 N    | xj                   dz  c_         | j                          y)zZoom in.r   Nr   r   s    r:   rm   zWebView.zoom_inI  s!     	B r9   c                     dd l }	 |j                  t        | j                         j	                                      y # t
        $ r Y y w xY w)Nr   )
webbrowseropenr   rR   toString
ValueError)rQ   webwindowtyper   s      r:   createWindowzWebView.createWindowP  s<    	OON488:+>+>+@AB 		s   6= 	A	A	c                     | j                   r:| j                   j                  |j                                |j                          y y r   )rG   popup	globalPosaccept)rQ   events     r:   contextMenuEventzWebView.contextMenuEventX  s4    ##EOO$56LLN r9   c                    t         rCt        r| j                  d       t        t        |   ||       t        r&| j                  d       nt        t        |   ||       	 | j                         j                  |        y# t        $ r Y yw xY w)z
        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
        FTN)	r
   r   
setEnabledrO   rZ   setHtml
focusProxyinstallEventFilterAttributeError)rQ   htmlbaseUrlrU   s      r:   r   zWebView.setHtml]  sv     &'4(w7%'4(w7
	 OO006 		s    B   	BBc                     t         |   |       	 | j                         j                  |        y# t        $ r Y yw xY w)zw
        Load url.

        This is reimplemented to install our event filter after the
        url is loaded.
        N)rO   loadr   r   r   )rQ   rR   rU   s     r:   r   zWebView.loadx  s=     	S
	OO006 		s   2 	>>c                 6   | j                         |u rw|j                         t        j                  k(  r| j                  j                          n;|j                         t        j                  k(  r| j                  j                          t        | %  ||      S )z
        Handle events that affect the view.

        All events (e.g. focus in/out) reach the focus proxy, not this
        widget itself. That's why this event filter is necessary.
        )
r   typer   FocusInsig_focus_in_eventrN   FocusOutsig_focus_out_eventrO   eventFilter)rQ   widgetr   rU   s      r:   r   zWebView.eventFilter  sn     ??&zz|v~~-'',,.0((--/w"6511r9   )TN)TTFFF)r]   FFFr   )r,   r-   r.   rV   r   r   r   r^   r   r   r   r   r   r   r   r   r   r   rn   rm   r   r   r   r   r   r   rW   rX   s   @r:   rZ   rZ   V   s       !("(  r-h @E%*G#' @E.3BB9!
  
V! !
 
V! !
 %)F 6"2 2r9   rZ   c                       e Zd 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y)
WebBrowserz
    Web browser widget.
    Nc                 	    t        j                   |       d  _        t         |       _         j                  j                           j                  j                  j                   j                          j                  j                  j                   j                          j                  j                  j                   j                         t         t        j                  d      t!        d       j"                        }t%         j                  j&                        }t%         j                  j(                        }d fd	} |t*        j,                  t        j                  d            } |t*        j.                  t        j                  d             |t*        j0                  t        j                  d	            }	 |t*        j2                  t        j                  d
            }
j5                  d        j                  j6                  j                  fd        j                  j                  j                  fd       t9               j;                  d       j=                           j                  j6                  j                  j>                          j                  j@                  j                  jB                          j                  j                  j                  fd       tE         jG                               }tI                _%         jJ                  jL                  j                   jN                         tP        s/ j                  jR                  j                   jT                         tW                _,         jX                  j[                   j                          jX                  j=                          t         t        j                  d      t!        d       j\                        } jX                  j^                  j                  |j`                         tc               }|	|
||| jJ                  |||fD ]  }|je                  |        |r|je                  |       tg        |      }|je                   j                         |je                   jX                          ji                  |       y )N)rc   homeHome)ri   tiprj   c                 f    t        j                  j                  |       j                  |      S )N)rb   ri   )r   webviewrs   )propri   rQ   s     r:   pageact2btnz(WebBrowser.__init__.<locals>.pageact2btn  s,     ''-dllO Or9   ro   )ri   rp   rf   rl   Fc                  &     j                  d      S )NTr   stop_buttons   r:   <lambda>z%WebBrowser.__init__.<locals>.<lambda>  s    1G1G1M r9   c                  &     j                  d      S )NFr   r   s   r:   r   z%WebBrowser.__init__.<locals>.<lambda>  s    +2H2H2O r9   c                 $    j                         S r   )hide)_stateprogressbars    r:   r   z%WebBrowser.__init__.<locals>.<lambda>  s    9I9I9K r9   findz	Find text)ri   r   toggledr   )5r   r^   home_urlrZ   r   r   loadFinishedconnectload_finishedtitleChangedsetWindowTitle
urlChangedurl_changedr   r   ri   r   go_homer   r$   r#   r   ry   r6   r1   r2   r   loadStartedr   setTextVisibler  showloadProgresssetValuer   	get_labelr   	url_combovalidurl_combo_activatedr
   iconChangedicon_changedr    find_widget
set_editortoggle_find_widgetvisibility_changed
setCheckedr   	addWidgetr   	setLayout)rQ   rb   options_buttonrc   home_buttonzoom_out_buttonzoom_in_buttonr   refresh_buttonprevious_buttonnext_buttonlabelfind_buttonhlayoutr   layoutr  r   s   `               @@r:   r^   zWebBrowser.__init__  s   v&t,?!!))$*<*<=!!))$*=*=>''(8(89'388F3C,-fI26,,@ ((D(DE&t||'B'BC	O %!!(;=!chhv&68%chhz&:<!""&)9; 	u%  (()MN!!))*OP"4(""5)  (()9)9:!!))+*>*>?!!))*KLt~~'($T*$$T%=%=>LL$$,,T->->?&t,##DLL1'388F3C,-kN040G0GI 	++33K4J4JK-&[+dnno~%{KA 	&F f%	&
 n-%g.&))*vr9   c                     t        d      S )zReturn address label textzAddress:r   r   s    r:   r  zWebBrowser.get_label  s    }r9   c                 $    t        |      | _        y)zSet home URLN)r   r  rQ   rh   s     r:   set_home_urlzWebBrowser.set_home_url  s    T
r9   c                 H    | j                  |       | j                  |       y)zSet current URLN)r  go_torQ   rR   s     r:   set_urlzWebBrowser.set_url  s    

3r9   c                 l    t        |      rt        |      }n|}| j                  j                  |       y)zGo to page *address*N)r   r   r   r   )rQ   url_or_textrR   s      r:   r1  zWebBrowser.go_to  s+    +&{#CC#r9   c                 T    | j                   | j                  | j                          yy)zGo to home pageN)r  r3  r   s    r:   r  zWebBrowser.go_home  s#     ==$LL' %r9   c                     t        |      S )z%Convert text address into QUrl object)r   r.  s     r:   text_to_urlzWebBrowser.text_to_url  s    Dzr9   c                     t        | j                  j                               }| j                  | j	                  |             y)z"Load URL from combo box first itemN)r   r  currentTextr1  r8  )rQ   r  rh   s      r:   r  zWebBrowser.url_combo_activated  s0    dnn88:;

4##D)*r9   c                 R    |s%| j                   j                  t        d             y y )NzUnable to load page)r   r   r   )rQ   oks     r:   r	  zWebBrowser.load_finished  s"    LL  #8!9: r9   c                 "    |j                         S )z2Convert QUrl object to displayed text in combo box)r   r2  s     r:   url_to_textzWebBrowser.url_to_text  s    ||~r9   c                 X    | j                   j                  | j                  |             y)z3Displayed URL has changed -> updating URL combo boxN)r  add_textr>  r2  s     r:   r  zWebBrowser.url_changed  s     0 0 56r9   c                     | j                   j                  | j                   j                         | j                  j	                                | j                  | j                  j	                                y r   )r  setItemIconcurrentIndexr   ri   setWindowIconr   s    r:   r  zWebBrowser.icon_changed  sN    ""4>>#>#>#@#'<<#4#4#6	84<<,,./r9   c                 r    |r| j                   j                          y | j                   j                          y r   )r  r  r  )rQ   states     r:   r  zWebBrowser.toggle_find_widget  s)    !!#!!#r9   )NNT)r,   r-   r.   rV   r^   r  r/  r3  r1  r   r  r8  r  r	  r>  r  r  boolr  r8   r9   r:   r   r     sn    FP#
 
V( (
+
;70
 
$Z$ $r9   r   c                   V     e Zd ZdZ ee      Zd fd	Z fdZe	d        Z
ddZ xZS )FrameWebViewz6
    Framed WebView for UI consistency in Spyder.
    c                     t            |       t         ||       _         j                  j                  j                   fd        j                  j                  j                   fd       t               }|j                   j                         |j                  dddd        j                  |        j                          |rtt        r> j                  j                         j                  j                   j                         y  j                  j                  j                   j                         y y )N)rc   r\   c                  (     j                  d      S )NTfocus_apply_stylesheetr   s   r:   r   z'FrameWebView.__init__.<locals>.<lambda>0  s    D***6 r9   c                  (     j                  d      S )NFrL  rN  r   s   r:   r   z'FrameWebView.__init__.<locals>.<lambda>2  s    D***7 r9   r   )rO   r^   rZ   _webviewr   r  r   r   r  setContentsMarginsr   rO  r
   r|   rM   )rQ   rb   rc   r+  rU   s   `   r:   r^   zFrameWebView.__init__'  s     %

 	((006	8))117	9 '!!!Q1-v ""$00889I9IJ))11$2B2BC	 r9   c                     |dk(  rt         |   |      S t        | j                  |      rt	        | j                  |      S t         |   |      S )NrQ  )rO   __getattr__r   rQ  getattr)rQ   rg   rU   s     r:   rT  zFrameWebView.__getattr__@  sJ    :7&t,,4==$'4==$//7&t,,r9   c                     | j                   S r   )rQ  r   s    r:   
web_widgetzFrameWebView.web_widgetI  s    }}r9   c                    |rt         j                  }nt         j                  }t        j                  j                         }|j                  j                  d| ddd       | j                  |j                                y)z0Apply stylesheet according to the current focus.z
1px solid z0px 1px 0px 1pxz0px 0px 1px 0px3px)bordermarginpaddingborderRadiusN)
r   COLOR_ACCENT_3COLOR_BACKGROUND_4	qstylizerstyle
StyleSheetr   	setValuessetStyleSheetr   )rQ   rM  border_colorcsss       r:   rO  zFrameWebView._apply_stylesheetM  sm    (77L(;;Loo((*

~.$%	 	 	
 	3<<>*r9   )T)F)r,   r-   r.   rV   r   r   rM   r^   rT  propertyrW  rO  rW   rX   s   @r:   rI  rI  !  s7     ,KD2-  +r9   rI  c                      ddl m}   | d      }t               }|j                          |j	                  d       |j                          t        j                  |j                                y)zRun web browserr   )qapplication   )	test_timezhttps://www.google.com/N)	spyder.utils.qthelpersri  r   r  r/  r  sysexitexec_)ri  appr   s      r:   testrq  _  sJ    3

#C\F
KKM
12
NNHHSYY[r9   __main__);rV   r   r   rm  qstylizer.styler`  qtpyr   qtpy.QtCorer   r   r   r   r   
qtpy.QtGuir	   qtpy.QtWebEngineWidgetsr
   r   r   r   qtpy.QtWidgetsr   r   r   r   r   spyder.api.translationsr   spyder.api.widgets.mixinsr   spyder.config.baser   spyder.config.guir   spyder.py3compatr   r   spyder.utils.icon_managerr   spyder.utils.paletter   rl  r   r   r   spyder.widgets.comboboxesr   spyder.widgets.findreplacer    r"   r<   rF   rJ   rZ   r   rI  rq  r,   r8   r9   r:   <module>r     s      
  
   6 6  I I M M & 7 " & ; ) .7 7 1 2
	 	  /n /,2n/ 2D
F$ F$R;+6 ;+| zF r9   