UI/UserInterface.py

changeset 6625
a67fee7bc09c
parent 6599
419f36a46608
child 6630
bddd12f27a4c
--- a/UI/UserInterface.py	Wed Dec 12 19:43:53 2018 +0100
+++ b/UI/UserInterface.py	Wed Dec 12 19:52:24 2018 +0100
@@ -19,7 +19,7 @@
 
 from PyQt5.QtCore import pyqtSlot, QTimer, QFile, QFileInfo, pyqtSignal, \
     PYQT_VERSION_STR, QDate, QIODevice, qVersion, QProcess, QSize, QUrl, \
-    QObject, Qt
+    QObject, Qt, QUuid, QThread
 from PyQt5.QtGui import QKeySequence, QDesktopServices
 from PyQt5.QtWidgets import QSizePolicy, QWidget, QWhatsThis, QToolBar, \
     QDialog, QSplitter, QApplication, QMenu, QVBoxLayout, QDockWidget, \
@@ -282,6 +282,10 @@
         self.templateViewer = None
         self.numbersViewer = None
         
+        self.__webBrowserProcess = None
+        self.__webBrowserClient = None
+        self.__webBrowserSAName = QUuid.createUuid().toString()[1:-1]
+        
         # Create the main window now so that we can connect QActions to it.
         logging.debug("Creating Layout...")
         self.__createLayout(debugServer)
@@ -572,11 +576,8 @@
         self.__initExternalToolsActions()
         
         # create a dummy help window for shortcuts handling
-        if WEBENGINE_AVAILABLE:
-            from WebBrowser.WebBrowserWindow import WebBrowserWindow
-            self.dummyHelpViewer = \
-                WebBrowserWindow(None, '.', None, 'web_browser', True, True)
-        elif WEBKIT_AVAILABLE:
+        # TODO: remove this in favour of standalone web browser
+        if WEBKIT_AVAILABLE:
             from Helpviewer.HelpWindow import HelpWindow
             self.dummyHelpViewer = \
                 HelpWindow(None, '.', None, 'help viewer', True, True)
@@ -5654,19 +5655,16 @@
             if not homeUrl.scheme():
                 home = QUrl.fromLocalFile(home).toString()
         
-        if WEBENGINE_AVAILABLE or WEBKIT_AVAILABLE:
+        if WEBENGINE_AVAILABLE:
+            self.__launchExternalWebBrowser(home, searchWord=searchWord)
+        
+        elif WEBKIT_AVAILABLE:
+            # TODO: change to use external web browser in single mode
             single = useSingle
-            if WEBENGINE_AVAILABLE:
-                single = single or \
-                    Preferences.getWebBrowser("SingleWebBrowserWindow")
-            elif WEBKIT_AVAILABLE:
+            if WEBKIT_AVAILABLE:
                 single = single or Preferences.getHelp("SingleHelpWindow")
             if not single or self.helpWindow is None:
-                if WEBENGINE_AVAILABLE:
-                    from WebBrowser.WebBrowserWindow import WebBrowserWindow
-                    browser = WebBrowserWindow(home, '.', None, 'web_browser',
-                                               True, searchWord=searchWord)
-                elif WEBKIT_AVAILABLE:
+                if WEBKIT_AVAILABLE:
                     from Helpviewer.HelpWindow import HelpWindow
                     browser = HelpWindow(home, '.', None, 'help viewer', True,
                                          searchWord=searchWord)
@@ -5694,9 +5692,90 @@
             else:
                 self.helpWindow.newTab(home)
                 self.helpWindow.raise_()
+        
         else:
             self.__webBrowser(home)
     
+    def __launchExternalWebBrowser(self, home, searchWord=None):
+        """
+        Private method to start an external web browser and communicate with
+        it.
+        
+        @param home filename of file to be shown or URL to be opened
+        @type str
+        @keyparam searchWord word to search for
+        @type str
+        """
+        from WebBrowser.WebBrowserSingleApplication import \
+            WebBrowserSingleApplicationClient
+        
+        clientArgs = []
+        if searchWord:
+            clientArgs.append("--search={0}".format(searchWord))
+        
+        if self.__webBrowserProcess is None:
+            process = QProcess()
+            args = [
+                os.path.join(
+                    os.path.dirname(__file__), "..", "eric6_browser.py"),
+                "--qthelp",
+                "--single",
+                "--name={0}".format(self.__webBrowserSAName)
+            ]
+            process.start(sys.executable, args)
+            if not process.waitForStarted():
+                E5MessageBox.warning(
+                    self,
+                    self.tr("Start Web Browser"),
+                    self.tr("""The eric6 web browser could not be started."""))
+                return
+            
+            process.finished.connect(self.__webBrowserFinished)
+            self.__webBrowserProcess = process
+            
+            self.__webBrowserClient = WebBrowserSingleApplicationClient(
+                self.__webBrowserSAName)
+            connectCount = 30
+            while connectCount:
+                res = self.__webBrowserClient.connect()
+                if res != 0:
+                    break
+                else:
+                    connectCount -= 1
+                    QThread.msleep(1000)
+            if res <= 0:
+                E5MessageBox.warning(
+                    self,
+                    self.tr("Start Web Browser"),
+                    self.tr("""<p>The eric6 web browser is not started.</p>"""
+                            """<p>Reason: {0}</p>""").format(
+                        self.__webBrowserClient.errstr())
+                )
+                return
+            
+            if home:
+                clientArgs.append(home)
+        else:
+            clientArgs.append("--newtab={0}".format(home))
+        
+        if clientArgs:
+            self.__webBrowserClient.processArgs(clientArgs, disconnect=False)
+    
+    def __webBrowserFinished(self):
+        """
+        Private slot handling the end of the external web browser process.
+        """
+        self.__webBrowserProcess.deleteLater()
+        
+        self.__webBrowserProcess = None
+        self.__webBrowserClient = None
+    
+    def __webBrowserShutdown(self):
+        """
+        Private method to shut down the web browser.
+        """
+        self.__webBrowserClient.processArgs(["--shutdown"], disconnect=False)
+    
     def __helpClosed(self):
         """
         Private slot to handle the helpClosed signal of the help window.
@@ -5743,6 +5822,7 @@
             (boolean)
         @return reference to the help window instance (HelpWindow)
         """
+        # TODO: check if this method is used somewhere
         if WEBENGINE_AVAILABLE or WEBKIT_AVAILABLE:
             if self.helpWindow is None:
                 self.launchHelpViewer("", useSingle=True)
@@ -5937,7 +6017,7 @@
         """
         if self.shortcutsDialog is None:
             from Preferences.ShortcutsDialog import ShortcutsDialog
-            self.shortcutsDialog = ShortcutsDialog(self, 'Shortcuts')
+            self.shortcutsDialog = ShortcutsDialog(self)
         self.shortcutsDialog.populate()
         self.shortcutsDialog.show()
         
@@ -6568,6 +6648,9 @@
             if not self.helpWindow.shutdown():
                 return False
         
+        if self.__webBrowserProcess is not None:
+            self.__webBrowserShutdown()
+        
         if self.irc is not None:
             if not self.irc.shutdown():
                 return False

eric ide

mercurial