Continued porting the web browser. QtWebEngine

Sun, 07 Feb 2016 19:36:07 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 07 Feb 2016 19:36:07 +0100
branch
QtWebEngine
changeset 4727
62b50a24fb59
parent 4726
c26e2a2dc0cb
child 4728
0367675d783d

Continued porting the web browser.

- added some editing actions

WebBrowser/WebBrowserView.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserWindow.py file | annotate | diff | comparison | revisions
eric6_browser.py file | annotate | diff | comparison | revisions
--- a/WebBrowser/WebBrowserView.py	Sun Feb 07 18:08:48 2016 +0100
+++ b/WebBrowser/WebBrowserView.py	Sun Feb 07 19:36:07 2016 +0100
@@ -385,6 +385,36 @@
         """
         self.triggerPageAction(QWebEnginePage.Copy)
     
+    def cut(self):
+        """
+        Public slot to cut the selected text.
+        """
+        self.triggerPageAction(QWebEnginePage.Cut)
+    
+    def paste(self):
+        """
+        Public slot to paste text from the clipboard.
+        """
+        self.triggerPageAction(QWebEnginePage.Paste)
+    
+    def undo(self):
+        """
+        Public slot to undo the last edit action.
+        """
+        self.triggerPageAction(QWebEnginePage.Undo)
+    
+    def redo(self):
+        """
+        Public slot to redo the last edit action.
+        """
+        self.triggerPageAction(QWebEnginePage.Redo)
+    
+    def selectAll(self):
+        """
+        Public slot to select all text.
+        """
+        self.triggerPageAction(QWebEnginePage.SelectAll)
+    
     def isForwardAvailable(self):
         """
         Public method to determine, if a forward move in history is possible.
@@ -556,7 +586,8 @@
 ##            menu.addSeparator()
 ##            menu.addAction(
 ##                UI.PixmapCache.getIcon("editCopy.png"),
-##                self.tr("Copy Link to Clipboard"), self.__copyLink)
+##                self.tr("Copy Link to Clipboard"), self.__copyLink)\
+##                .setData(hit.linkUrl())
 ##            menu.addAction(
 ##                UI.PixmapCache.getIcon("mailSend.png"),
 ##                self.tr("Send Link"),
@@ -584,7 +615,7 @@
 ##            menu.addAction(
 ##                UI.PixmapCache.getIcon("editCopy.png"),
 ##                self.tr("Copy Image Location to Clipboard"),
-##                self.__copyLocation).setData(hit.imageUrl().toString())
+##                self.__copyLink).setData(hit.imageUrl())
 ##            menu.addAction(
 ##                UI.PixmapCache.getIcon("mailSend.png"),
 ##                self.tr("Send Image Link"),
@@ -633,7 +664,7 @@
 ##                menu.addAction(
 ##                    UI.PixmapCache.getIcon("editCopy.png"),
 ##                    self.tr("Copy Media Address to Clipboard"),
-##                    self.__copyLocation).setData(videoUrl.toString())
+##                    self.__copyLink).setData(videoUrl)
 ##                menu.addAction(
 ##                    UI.PixmapCache.getIcon("mailSend.png"),
 ##                    self.tr("Send Media Address"), self.__sendLink)\
@@ -725,6 +756,9 @@
 ##            UI.PixmapCache.getIcon("bookmark22.png"),
 ##            self.tr("Bookmark this Page"), self.addBookmark)
         menu.addAction(
+            UI.PixmapCache.getIcon("editCopy.png"),
+            self.tr("Copy Page Link"), self.__copyLink).setData(self.url())
+        menu.addAction(
             UI.PixmapCache.getIcon("mailSend.png"),
             self.tr("Send Page Link"), self.__sendLink).setData(self.url())
         menu.addSeparator()
@@ -741,8 +775,16 @@
         menu.addAction(self.__mw.zoomResetAct)
         menu.addAction(self.__mw.zoomOutAct)
         menu.addSeparator()
+        # TODO: edit actions only for forms
+        menu.addAction(self.__mw.undoAct)
+        menu.addAction(self.__mw.redoAct)
+        menu.addSeparator()
         if self.selectedText():
             menu.addAction(self.__mw.copyAct)
+            menu.addAction(self.__mw.cutAct)
+        menu.addAction(self.__mw.pasteAct)
+        menu.addAction(self.__mw.selectAllAct)
+        if self.selectedText():
             menu.addAction(
                 UI.PixmapCache.getIcon("mailSend.png"),
                 self.tr("Send Text"),
@@ -805,7 +847,7 @@
 ##                           self.__addSearchEngine).setData(element)
 ##            menu.addSeparator()
         
-        # TODO: Web Inspector
+        # TODO: WebInspector
 ##        menu.addAction(
 ##            UI.PixmapCache.getIcon("webInspector.png"),
 ##            self.tr("Web Inspector..."), self.__webInspector)
@@ -867,18 +909,25 @@
             data = data.toString()
         QDesktopServices.openUrl(QUrl("mailto:?body=" + data))
     
+    def __copyLink(self):
+        """
+        Private slot to copy a link to the clipboard.
+        """
+        act = self.sender()
+        data = act.data()
+        if isinstance(data, QUrl) and data.isEmpty():
+            return
+        
+        if isinstance(data, QUrl):
+            data = data.toString()
+        QApplication.clipboard().setText(data)
+    
 ##    def __downloadLink(self):
 ##        """
 ##        Private slot to download a link and save it to disk.
 ##        """
 ##        self.pageAction(QWebPage.DownloadLinkToDisk).trigger()
 ##    
-##    def __copyLink(self):
-##        """
-##        Private slot to copy a link to the clipboard.
-##        """
-##        self.pageAction(QWebPage.CopyLinkToClipboard).trigger()
-##    
 ##    def __downloadImage(self):
 ##        """
 ##        Private slot to download an image and save it to disk.
@@ -891,14 +940,6 @@
 ##        """
 ##        self.pageAction(QWebPage.CopyImageToClipboard).trigger()
     
-    def __copyLocation(self):
-        """
-        Private slot to copy an image or media location to the clipboard.
-        """
-        act = self.sender()
-        url = act.data()
-        QApplication.clipboard().setText(url)
-    
     # TODO: AdBlock
 ##    def __blockImage(self):
 ##        """
--- a/WebBrowser/WebBrowserWindow.py	Sun Feb 07 18:08:48 2016 +0100
+++ b/WebBrowser/WebBrowserWindow.py	Sun Feb 07 19:36:07 2016 +0100
@@ -868,7 +868,82 @@
             self.copyAct.triggered.connect(self.__copy)
         self.__actions.append(self.copyAct)
         
-        # TODO: add more editing actions: Cut, Paste, Undo, Redo, Select All
+        self.cutAct = E5Action(
+            self.tr('Cut'),
+            UI.PixmapCache.getIcon("editCut.png"),
+            self.tr('Cu&t'),
+            QKeySequence(self.tr("Ctrl+X", "Edit|Cut")),
+            0, self, 'webbrowser_edit_cut')
+        self.cutAct.setStatusTip(self.tr('Cut the selected text'))
+        self.cutAct.setWhatsThis(self.tr(
+            """<b>Cut</b>"""
+            """<p>Cut the selected text to the clipboard.</p>"""
+        ))
+        if not self.__initShortcutsOnly:
+            self.cutAct.triggered.connect(self.__cut)
+        self.__actions.append(self.cutAct)
+        
+        self.pasteAct = E5Action(
+            self.tr('Paste'),
+            UI.PixmapCache.getIcon("editPaste.png"),
+            self.tr('&Paste'),
+            QKeySequence(self.tr("Ctrl+V", "Edit|Paste")),
+            0, self, 'webbrowser_edit_paste')
+        self.pasteAct.setStatusTip(self.tr('Paste text from the clipboard'))
+        self.pasteAct.setWhatsThis(self.tr(
+            """<b>Paste</b>"""
+            """<p>Paste some text from the clipboard.</p>"""
+        ))
+        if not self.__initShortcutsOnly:
+            self.pasteAct.triggered.connect(self.__paste)
+        self.__actions.append(self.pasteAct)
+        
+        self.undoAct = E5Action(
+            self.tr('Undo'),
+            UI.PixmapCache.getIcon("editUndo.png"),
+            self.tr('&Undo'),
+            QKeySequence(self.tr("Ctrl+Z", "Edit|Undo")),
+            0, self, 'webbrowser_edit_undo')
+        self.undoAct.setStatusTip(self.tr('Undo the last edit action'))
+        self.undoAct.setWhatsThis(self.tr(
+            """<b>Undo</b>"""
+            """<p>Undo the last edit action.</p>"""
+        ))
+        if not self.__initShortcutsOnly:
+            self.undoAct.triggered.connect(self.__undo)
+        self.__actions.append(self.undoAct)
+        
+        self.redoAct = E5Action(
+            self.tr('Redo'),
+            UI.PixmapCache.getIcon("editRedo.png"),
+            self.tr('&Redo'),
+            QKeySequence(self.tr("Ctrl+Shift+Z", "Edit|Redo")),
+            0, self, 'webbrowser_edit_redo')
+        self.redoAct.setStatusTip(self.tr('Redo the last edit action'))
+        self.redoAct.setWhatsThis(self.tr(
+            """<b>Redo</b>"""
+            """<p>Redo the last edit action.</p>"""
+        ))
+        if not self.__initShortcutsOnly:
+            self.redoAct.triggered.connect(self.__redo)
+        self.__actions.append(self.redoAct)
+        
+        self.selectAllAct = E5Action(
+            self.tr('Select All'),
+            UI.PixmapCache.getIcon("editSelectAll.png"),
+            self.tr('&Select All'),
+            QKeySequence(self.tr("Ctrl+A", "Edit|Select All")),
+            0, self, 'webbrowser_edit_select_all')
+        self.selectAllAct.setStatusTip(self.tr('Select all text'))
+        self.selectAllAct.setWhatsThis(self.tr(
+            """<b>Select All</b>"""
+            """<p>Select all text of the current browser.</p>"""
+        ))
+        if not self.__initShortcutsOnly:
+            self.selectAllAct.triggered.connect(self.__selectAll)
+        self.__actions.append(self.selectAllAct)
+        
+        # TODO: add more editing actions: Select All
         
         self.findAct = E5Action(
             self.tr('Find...'),
@@ -1705,7 +1780,14 @@
         
         menu = mb.addMenu(self.tr('&Edit'))
         menu.setTearOffEnabled(True)
+        menu.addAction(self.undoAct)
+        menu.addAction(self.redoAct)
+        menu.addSeparator()
         menu.addAction(self.copyAct)
+        menu.addAction(self.cutAct)
+        menu.addAction(self.pasteAct)
+        menu.addSeparator()
+        menu.addAction(self.selectAllAct)
         menu.addSeparator()
         menu.addAction(self.findAct)
         menu.addAction(self.findNextAct)
@@ -1869,7 +1951,14 @@
         edittb = self.addToolBar(self.tr("Edit"))
         edittb.setObjectName("EditToolBar")
         edittb.setIconSize(UI.Config.ToolBarIconSize)
+        edittb.addAction(self.undoAct)
+        edittb.addAction(self.redoAct)
+        edittb.addSeparator()
         edittb.addAction(self.copyAct)
+        edittb.addAction(self.cutAct)
+        edittb.addAction(self.pasteAct)
+        edittb.addSeparator()
+        edittb.addAction(self.selectAllAct)
         
         viewtb = self.addToolBar(self.tr("View"))
         viewtb.setObjectName("ViewToolBar")
@@ -2507,6 +2596,36 @@
         Private slot called to handle the copy action.
         """
         self.currentBrowser().copy()
+    
+    def __cut(self):
+        """
+        Private slot called to handle the cut action.
+        """
+        self.currentBrowser().cut()
+    
+    def __paste(self):
+        """
+        Private slot called to handle the paste action.
+        """
+        self.currentBrowser().paste()
+    
+    def __undo(self):
+        """
+        Private slot to handle the undo action.
+        """
+        self.currentBrowser().undo()
+    
+    def __redo(self):
+        """
+        Private slot to handle the redo action.
+        """
+        self.currentBrowser().redo()
+    
+    def __selectAll(self):
+        """
+        Private slot to handle the select all action.
+        """
+        self.currentBrowser().selectAll()
 ##    
 ##    def __privateBrowsing(self):
 ##        """
@@ -3074,8 +3193,8 @@
             item = backItems[index]
             act = QAction(self)
             act.setData(-1 * (index + 1))
-##            icon = HelpWindow.__getWebIcon(item.url())
-##            act.setIcon(icon)
+            icon = HelpWindow.icon(item.url())
+            act.setIcon(icon)
             act.setText(item.title())
             self.backMenu.addAction(act)
         
@@ -3091,8 +3210,8 @@
             item = forwardItems[index]
             act = QAction(self)
             act.setData(index + 1)
-##            icon = HelpWindow.__getWebIcon(item.url())
-##            act.setIcon(icon)
+            icon = HelpWindow.icon(item.url())
+            act.setIcon(icon)
             act.setText(item.title())
             self.forwardMenu.addAction(act)
         
@@ -3291,53 +3410,8 @@
         @param url URL to get icon for (QUrl)
         @return icon for the URL (QIcon)
         """
-        # TODO: implement an icon fetcher
-##        icon = HelpWindow.__getWebIcon(url)
-##        if icon.isNull():
-##            hostUrl = QUrl()
-##            hostUrl.setScheme(url.scheme())
-##            hostUrl.setHost(url.host())
-##            icon = HelpWindow.__getWebIcon(hostUrl)
-##        
-##        if icon.isNull():
-##            pixmap = QWebSettings.webGraphic(
-##                QWebSettings.DefaultFrameIconGraphic)
-##            if pixmap.isNull():
-##                pixmap = UI.PixmapCache.getPixmap("defaultIcon.png")
-##                QWebSettings.setWebGraphic(
-##                    QWebSettings.DefaultFrameIconGraphic, pixmap)
-##            return QIcon(pixmap)
-##        
-        icon = UI.PixmapCache.getIcon("defaultIcon.png")
-        return icon
+        return WebIconProvider.instance().iconForUrl(url)
 
-##    @staticmethod
-##    def __getWebIcon(url):
-##        """
-##        Private static method to fetch the icon for a URL.
-##        
-##        @param url URL to get icon for (QUrl)
-##        @return icon for the URL (QIcon)
-##        """
-##        scheme = url.scheme()
-##        if scheme in ["eric", "about"]:
-##            return UI.PixmapCache.getIcon("ericWeb.png")
-##        elif scheme == "qthelp" and QTHELP_AVAILABLE:
-##            return UI.PixmapCache.getIcon("qthelp.png")
-##        elif scheme == "file":
-##            return UI.PixmapCache.getIcon("fileMisc.png")
-##        elif scheme == "abp":
-##            return UI.PixmapCache.getIcon("adBlockPlus.png")
-##        
-##        icon = QWebSettings.iconForUrl(url)
-##        if icon.isNull():
-##            # try again
-##            QThread.usleep(10)
-##            icon = QWebSettings.iconForUrl(url)
-##        if not icon.isNull():
-##            icon = QIcon(icon.pixmap(22, 22))
-##        return icon
-##        
 ##    @classmethod
 ##    def bookmarksManager(cls):
 ##        """
--- a/eric6_browser.py	Sun Feb 07 18:08:48 2016 +0100
+++ b/eric6_browser.py	Sun Feb 07 19:36:07 2016 +0100
@@ -31,7 +31,7 @@
 import os
 
 # TODO: adjust this when done
-MIN_QT_VERSION = "5.6.0"
+MIN_QT_VERSION = "5.5.0"
 
 from PyQt5.QtCore import qVersion
 if qVersion() < MIN_QT_VERSION:

eric ide

mercurial