eric6/E5Network/E5NetworkIcon.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
diff -r f99d60d6b59b -r 2602857055c5 eric6/E5Network/E5NetworkIcon.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eric6/E5Network/E5NetworkIcon.py	Sun Apr 14 15:09:21 2019 +0200
@@ -0,0 +1,83 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2015 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a statusbar icon tracking the network status.
+"""
+
+from __future__ import unicode_literals
+
+from PyQt5.QtCore import pyqtSlot, pyqtSignal
+from PyQt5.QtNetwork import QNetworkConfigurationManager
+from PyQt5.QtWidgets import QLabel
+
+import UI.PixmapCache
+import Preferences
+
+
+class E5NetworkIcon(QLabel):
+    """
+    Class implementing a statusbar icon tracking the network status.
+    
+    @signal onlineStateChanged(online) emitted to indicate a change of the
+        network state
+    """
+    onlineStateChanged = pyqtSignal(bool)
+    
+    def __init__(self, parent=None):
+        """
+        Constructor
+        
+        @param parent reference to the parent widget
+        @type QWidget
+        """
+        super(E5NetworkIcon, self).__init__(parent)
+        
+        if Preferences.getUI("DynamicOnlineCheck"):
+            self.__networkManager = QNetworkConfigurationManager(self)
+            self.__online = self.__networkManager.isOnline()
+            self.__onlineStateChanged(self.__online)
+            
+            self.__networkManager.onlineStateChanged.connect(
+                self.__onlineStateChanged)
+        else:
+            self.__online = True
+            self.__onlineStateChanged(self.__online)
+    
+    @pyqtSlot(bool)
+    def __onlineStateChanged(self, online):
+        """
+        Private slot handling online state changes.
+        
+        @param online flag indicating the online status
+        @type bool
+        """
+        if online:
+            self.setPixmap(UI.PixmapCache.getPixmap("network-online.png"))
+        else:
+            self.setPixmap(UI.PixmapCache.getPixmap("network-offline.png"))
+        
+        tooltip = self.tr("<p>Shows the network status<br/><br/>"
+                          "<b>Network:</b> {0}</p>")
+        
+        if online:
+            tooltip = tooltip.format(self.tr("Connected"))
+        else:
+            tooltip = tooltip.format(self.tr("Offline"))
+        
+        self.setToolTip(tooltip)
+        
+        if online != self.__online:
+            self.__online = online
+            self.onlineStateChanged.emit(online)
+    
+    def isOnline(self):
+        """
+        Public method to get the online state.
+        
+        @return online state
+        @rtype bool
+        """
+        return self.__online

eric ide

mercurial