7 """ |
7 """ |
8 Module implementing the web browser using QWebEngineView. |
8 Module implementing the web browser using QWebEngineView. |
9 """ |
9 """ |
10 |
10 |
11 import os |
11 import os |
|
12 import functools |
12 |
13 |
13 from PyQt5.QtCore import ( |
14 from PyQt5.QtCore import ( |
14 pyqtSignal, pyqtSlot, Qt, QUrl, QFileInfo, QTimer, QEvent, QPoint, |
15 pyqtSignal, pyqtSlot, Qt, QUrl, QFileInfo, QTimer, QEvent, QPoint, |
15 QPointF, QDateTime, QStandardPaths, QByteArray, QIODevice, QDataStream |
16 QPointF, QDateTime, QStandardPaths, QByteArray, QIODevice, QDataStream |
16 ) |
17 ) |
614 |
615 |
615 for suggestion in contextMenuData.spellCheckerSuggestions(): |
616 for suggestion in contextMenuData.spellCheckerSuggestions(): |
616 act = menu.addAction(suggestion) |
617 act = menu.addAction(suggestion) |
617 act.setFont(boldFont) |
618 act.setFont(boldFont) |
618 act.triggered.connect( |
619 act.triggered.connect( |
619 lambda: self.__replaceMisspelledWord)(act) |
620 functools.partial(self.__replaceMisspelledWord, act)) |
620 |
621 |
621 if not bool(menu.actions()): |
622 if not bool(menu.actions()): |
622 menu.addAction(self.tr("No suggestions")).setEnabled(False) |
623 menu.addAction(self.tr("No suggestions")).setEnabled(False) |
623 |
624 |
624 menu.addSeparator() |
625 menu.addSeparator() |
781 searchEngineName.lower())), |
782 searchEngineName.lower())), |
782 self.tr("Search image in {0}").format(searchEngineName)) |
783 self.tr("Search image in {0}").format(searchEngineName)) |
783 act.setData(engine.getSearchQuery( |
784 act.setData(engine.getSearchQuery( |
784 hitTest.imageUrl(), searchEngineName)) |
785 hitTest.imageUrl(), searchEngineName)) |
785 act.triggered.connect( |
786 act.triggered.connect( |
786 lambda: self.__searchImage(act)) |
787 functools.partial(self.__searchImage, act)) |
787 |
788 |
788 menu.addSeparator() |
789 menu.addSeparator() |
789 act = menu.addAction( |
790 act = menu.addAction( |
790 UI.PixmapCache.getIcon("adBlockPlus"), |
791 UI.PixmapCache.getIcon("adBlockPlus"), |
791 self.tr("Block Image")) |
792 self.tr("Block Image")) |
898 language = languages[0] |
899 language = languages[0] |
899 langCode = language.split("[")[1][:2] |
900 langCode = language.split("[")[1][:2] |
900 googleTranslatorUrl = QUrl( |
901 googleTranslatorUrl = QUrl( |
901 "http://translate.google.com/#auto/{0}/{1}".format( |
902 "http://translate.google.com/#auto/{0}/{1}".format( |
902 langCode, self.selectedText())) |
903 langCode, self.selectedText())) |
903 act = menu.addAction( |
904 act1 = menu.addAction( |
904 UI.PixmapCache.getIcon("translate"), |
905 UI.PixmapCache.getIcon("translate"), |
905 self.tr("Google Translate")) |
906 self.tr("Google Translate")) |
906 act.setData(googleTranslatorUrl) |
907 act1.setData(googleTranslatorUrl) |
907 act.triggered.connect( |
908 act1.triggered.connect( |
908 lambda: self.__openLinkInNewTab(act)) |
909 lambda: self.__openLinkInNewTab(act1)) |
909 wiktionaryUrl = QUrl( |
910 wiktionaryUrl = QUrl( |
910 "http://{0}.wiktionary.org/wiki/Special:Search?search={1}" |
911 "http://{0}.wiktionary.org/wiki/Special:Search?search={1}" |
911 .format(langCode, self.selectedText())) |
912 .format(langCode, self.selectedText())) |
912 act = menu.addAction( |
913 act2 = menu.addAction( |
913 UI.PixmapCache.getIcon("wikipedia"), |
914 UI.PixmapCache.getIcon("wikipedia"), |
914 self.tr("Dictionary")) |
915 self.tr("Dictionary")) |
915 act.setData(wiktionaryUrl) |
916 act2.setData(wiktionaryUrl) |
916 act.triggered.connect( |
917 act2.triggered.connect( |
917 lambda: self.__openLinkInNewTab(act)) |
918 lambda: self.__openLinkInNewTab(act2)) |
918 menu.addSeparator() |
919 menu.addSeparator() |
919 |
920 |
920 guessedUrl = QUrl.fromUserInput(self.selectedText().strip()) |
921 guessedUrl = QUrl.fromUserInput(self.selectedText().strip()) |
921 if self.__isUrlValid(guessedUrl): |
922 if self.__isUrlValid(guessedUrl): |
922 act = menu.addAction(self.tr("Go to web address")) |
923 act3 = menu.addAction(self.tr("Go to web address")) |
923 act.setData(guessedUrl) |
924 act3.setData(guessedUrl) |
924 act.triggered.connect( |
925 act3.triggered.connect( |
925 lambda: self.__openLinkInNewTab(act)) |
926 lambda: self.__openLinkInNewTab(act3)) |
926 |
927 |
927 def __createPageContextMenu(self, menu): |
928 def __createPageContextMenu(self, menu): |
928 """ |
929 """ |
929 Private method to populate the basic context menu. |
930 Private method to populate the basic context menu. |
930 |
931 |
1002 menu.addSeparator() |
1003 menu.addSeparator() |
1003 |
1004 |
1004 w3url = QUrl.fromEncoded( |
1005 w3url = QUrl.fromEncoded( |
1005 b"http://validator.w3.org/check?uri=" + |
1006 b"http://validator.w3.org/check?uri=" + |
1006 QUrl.toPercentEncoding(bytes(self.url().toEncoded()).decode())) |
1007 QUrl.toPercentEncoding(bytes(self.url().toEncoded()).decode())) |
1007 act = menu.addAction( |
1008 act1 = menu.addAction( |
1008 UI.PixmapCache.getIcon("w3"), |
1009 UI.PixmapCache.getIcon("w3"), |
1009 self.tr("Validate Page")) |
1010 self.tr("Validate Page")) |
1010 act.setData(w3url) |
1011 act1.setData(w3url) |
1011 act.triggered.connect( |
1012 act1.triggered.connect( |
1012 lambda: self.__openLinkInNewTab(act)) |
1013 lambda: self.__openLinkInNewTab(act1)) |
1013 |
1014 |
1014 from .WebBrowserLanguagesDialog import WebBrowserLanguagesDialog |
1015 from .WebBrowserLanguagesDialog import WebBrowserLanguagesDialog |
1015 languages = Preferences.toList( |
1016 languages = Preferences.toList( |
1016 Preferences.Prefs.settings.value( |
1017 Preferences.Prefs.settings.value( |
1017 "WebBrowser/AcceptLanguages", |
1018 "WebBrowser/AcceptLanguages", |
1023 b"http://translate.google.com/translate?sl=auto&tl=" + |
1024 b"http://translate.google.com/translate?sl=auto&tl=" + |
1024 langCode.encode() + |
1025 langCode.encode() + |
1025 b"&u=" + |
1026 b"&u=" + |
1026 QUrl.toPercentEncoding( |
1027 QUrl.toPercentEncoding( |
1027 bytes(self.url().toEncoded()).decode())) |
1028 bytes(self.url().toEncoded()).decode())) |
1028 act = menu.addAction( |
1029 act2 = menu.addAction( |
1029 UI.PixmapCache.getIcon("translate"), |
1030 UI.PixmapCache.getIcon("translate"), |
1030 self.tr("Google Translate")) |
1031 self.tr("Google Translate")) |
1031 act.setData(googleTranslatorUrl) |
1032 act2.setData(googleTranslatorUrl) |
1032 act.triggered.connect( |
1033 act2.triggered.connect( |
1033 lambda: self.__openLinkInNewTab(act)) |
1034 lambda: self.__openLinkInNewTab(act2)) |
1034 |
1035 |
1035 def __checkForForm(self, act, pos): |
1036 def __checkForForm(self, act, pos): |
1036 """ |
1037 """ |
1037 Private method to check the given position for an open search form. |
1038 Private method to check the given position for an open search form. |
1038 |
1039 |