|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a statusbar icon tracking the network status. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot, pyqtSignal |
|
13 from PyQt5.QtNetwork import QNetworkConfigurationManager |
|
14 from PyQt5.QtWidgets import QLabel |
|
15 |
|
16 import UI.PixmapCache |
|
17 import Preferences |
|
18 |
|
19 |
|
20 class E5NetworkIcon(QLabel): |
|
21 """ |
|
22 Class implementing a statusbar icon tracking the network status. |
|
23 |
|
24 @signal onlineStateChanged(online) emitted to indicate a change of the |
|
25 network state |
|
26 """ |
|
27 onlineStateChanged = pyqtSignal(bool) |
|
28 |
|
29 def __init__(self, parent=None): |
|
30 """ |
|
31 Constructor |
|
32 |
|
33 @param parent reference to the parent widget |
|
34 @type QWidget |
|
35 """ |
|
36 super(E5NetworkIcon, self).__init__(parent) |
|
37 |
|
38 if Preferences.getUI("DynamicOnlineCheck"): |
|
39 self.__networkManager = QNetworkConfigurationManager(self) |
|
40 self.__online = self.__networkManager.isOnline() |
|
41 self.__onlineStateChanged(self.__online) |
|
42 |
|
43 self.__networkManager.onlineStateChanged.connect( |
|
44 self.__onlineStateChanged) |
|
45 else: |
|
46 self.__online = True |
|
47 self.__onlineStateChanged(self.__online) |
|
48 |
|
49 @pyqtSlot(bool) |
|
50 def __onlineStateChanged(self, online): |
|
51 """ |
|
52 Private slot handling online state changes. |
|
53 |
|
54 @param online flag indicating the online status |
|
55 @type bool |
|
56 """ |
|
57 if online: |
|
58 self.setPixmap(UI.PixmapCache.getPixmap("network-online.png")) |
|
59 else: |
|
60 self.setPixmap(UI.PixmapCache.getPixmap("network-offline.png")) |
|
61 |
|
62 tooltip = self.tr("<p>Shows the network status<br/><br/>" |
|
63 "<b>Network:</b> {0}</p>") |
|
64 |
|
65 if online: |
|
66 tooltip = tooltip.format(self.tr("Connected")) |
|
67 else: |
|
68 tooltip = tooltip.format(self.tr("Offline")) |
|
69 |
|
70 self.setToolTip(tooltip) |
|
71 |
|
72 if online != self.__online: |
|
73 self.__online = online |
|
74 self.onlineStateChanged.emit(online) |
|
75 |
|
76 def isOnline(self): |
|
77 """ |
|
78 Public method to get the online state. |
|
79 |
|
80 @return online state |
|
81 @rtype bool |
|
82 """ |
|
83 return self.__online |