1313import webbrowser
1414
1515from PyQt5 .QtCore import pyqtSlot , QUrl , QLocale , QTranslator
16- from PyQt5 .QtGui import QColor
16+ from PyQt5 .QtGui import QColor , QCursor
1717from PyQt5 .QtWebKit import QWebSettings
1818from PyQt5 .QtWebKitWidgets import QWebPage
19- from PyQt5 .QtWidgets import QApplication
19+ from PyQt5 .QtWidgets import QApplication , QMenu , QAction
2020
2121from Utils import Constants
22- from Utils .CommonUtil import Signals , Setting , AppLog
22+ from Utils .CommonUtil import Signals , Setting , AppLog , openFolder
2323from Utils .GradientUtils import GradientUtils
2424from Utils .NetworkAccessManager import NetworkAccessManager
2525from Utils .ThemeManager import ThemeManager
@@ -86,6 +86,7 @@ def _initSignals(self):
8686 Signals .showReadmed .connect (self .renderReadme )
8787 Signals .urlLoaded .connect (self .onUrlLoaded )
8888 Signals .runExampled .connect (self ._runFile )
89+ Signals .cloneFinished .connect (lambda : self ._showNotice ('更新例子完成' ))
8990 Signals .cloneFinished .connect (self .treeViewCatalogs .initCatalog )
9091 Signals .cloneFinished .connect (self .renderReadme )
9192 Signals .progressStoped .connect (self .widgetCatalogs .stop )
@@ -103,6 +104,20 @@ def _initLanguage(self):
103104
104105 def _initWebView (self ):
105106 """初始化网页"""
107+ # 右键菜单
108+ self ._webviewMenu = QMenu (self .tr ('Menu' ), self .webViewContent )
109+ self ._webviewactRun = QAction (
110+ self .tr ('Run' ), self ._webviewMenu , triggered = self ._doActRun )
111+ self ._webviewactView = QAction (
112+ self .tr ('View' ), self ._webviewMenu , triggered = self ._doActView )
113+ self ._webviewactFolder = QAction (
114+ self .tr ('Open' ), self ._webviewMenu , triggered = self ._doActOpen )
115+ self ._webviewMenu .addAction (self ._webviewactRun )
116+ self ._webviewMenu .addAction (self ._webviewactView )
117+ self ._webviewMenu .addAction (self ._webviewactFolder )
118+
119+ self .webViewContent .customContextMenuRequested .connect (
120+ self ._showWebMenu )
106121 settings = self .webViewContent .settings ()
107122 # 设置默认编码
108123 settings .setDefaultTextEncoding ('UTF-8' )
@@ -119,6 +134,75 @@ def _initWebView(self):
119134 self .webViewContent .load (QUrl .fromLocalFile (
120135 os .path .abspath (Constants .HomeFile )))
121136
137+ def _doActRun (self ):
138+ """右键菜单运行代码
139+ """
140+ path = self .sender ().data ()
141+ Signals .runExampled .emit (path )
142+
143+ def _doActView (self ):
144+ """右键菜单查看代码
145+ """
146+ path = self .sender ().data ()
147+ try :
148+ code = open (path , 'rb' ).read ().decode (errors = 'ignore' )
149+ Signals .showCoded .emit (code )
150+ except Exception as e :
151+ AppLog .warn (str (e ))
152+
153+ def _doActOpen (self ):
154+ """右键菜单打开文件夹
155+ """
156+ path = self .sender ().data ()
157+ openFolder (path )
158+
159+ def _showWebMenu (self , pos ):
160+ """显示网页右键菜单
161+ :param pos: 点击位置
162+ """
163+ hit = self .webViewContent .page ().currentFrame ().hitTestContent (pos )
164+ url = hit .linkUrl ()
165+ if url .isValid ():
166+ path = url .toLocalFile ().strip ().replace ('\\ ' , '/' )
167+ names = path .split ('Markdown/' )
168+ if len (names ) == 1 :
169+ return
170+ path = os .path .abspath (os .path .join (
171+ Constants .DirCurrent , names [1 ]))
172+ AppLog .debug ('path: {}' .format (path ))
173+ AppLog .debug ('isdir: {}' .format (os .path .isdir (path )))
174+ self ._webviewactRun .setData (path )
175+ self ._webviewactView .setData (path )
176+ self ._webviewactFolder .setData (path )
177+ if os .path .exists (path ) and os .path .isdir (path ):
178+ self ._webviewactRun .setVisible (False )
179+ self ._webviewactView .setVisible (False )
180+ self ._webviewactFolder .setVisible (True )
181+ elif os .path .exists (path ) and os .path .isfile (path ):
182+ self ._webviewactRun .setVisible (True )
183+ self ._webviewactView .setVisible (True )
184+ self ._webviewactFolder .setVisible (True )
185+ self ._webviewMenu .exec_ (QCursor .pos ())
186+
187+ def _showNotice (self , message , timeout = 2000 ):
188+ """底部显示提示
189+ :param message: 提示消息
190+ """
191+ if hasattr (self , '_tip' ):
192+ self ._tip ._hideTimer .stop ()
193+ self ._tip .close ()
194+ self ._tip .deleteLater ()
195+ del self ._tip
196+ self ._tip = ToolTip ()
197+ self ._tip .setText (message )
198+ self ._tip .show ()
199+ self ._tip .move (
200+ self .pos ().x () + int ((self .width () - self ._tip .width ()) / 2 ),
201+ self .pos ().y () + self .height () - 60 ,
202+ )
203+ self ._tip ._hideTimer .timeout .connect (self ._tip .close )
204+ self ._tip ._hideTimer .start (timeout )
205+
122206 @pyqtSlot ()
123207 def on_buttonSkin_clicked (self ):
124208 """选择主题样式
0 commit comments