Fix for context menu position

This commit is contained in:
Silvan Calarco 2019-04-02 19:51:25 +02:00
parent 93a7dcc4a1
commit 2d44c33365

View File

@ -9,23 +9,28 @@ import os
import gettext import gettext
import libproxy import libproxy
from PyQt5.QtCore import pyqtSignal from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QIcon from PyQt5.QtGui import QIcon, QCursor
from PyQt5.QtWidgets import QSystemTrayIcon, QApplication, QMenu, QMainWindow, QMessageBox, QWidget from PyQt5.QtWidgets import QSystemTrayIcon, QApplication, QMenu, QMainWindow,\
from PyQt5 import QtCore QMessageBox, QWidget
from PyQt5.QtCore import QTimer, QProcess
class SystemTrayIcon(QSystemTrayIcon): class SystemTrayIcon(QSystemTrayIcon):
context_menu = None
def __init__(self, icon, parent=None): def __init__(self, icon, parent=None):
global w, menu, contatore, srpm, lista, NetsrpmsArg, nOre, desktop_session QSystemTrayIcon.__init__(self, icon, parent)
global w, contatore, srpm, lista, NetsrpmsArg, nOre, desktop_session
nOre = 2 nOre = 2
srpm = ["skype", "flashplugin", "win32codecs", "msttcorefonts", "b43-firmware", "sun-java7", "sun-java8", "virtualbox-extension-pack", "chromium-pepper-flash", "spotify"] srpm = [
"skype", "flashplugin", "win32codecs", "msttcorefonts", "b43-firmware",
"sun-java7", "sun-java8", "virtualbox-extension-pack", "chromium-pepper-flash",
"spotify"
]
NetsrpmsArg = "-c" NetsrpmsArg = "-c"
lista = [] lista = []
contatore = 0 contatore = 0
w = parent w = parent
QSystemTrayIcon.__init__(self, icon, parent)
menu = QMenu(parent) menu = QMenu(parent)
try: try:
@ -39,22 +44,24 @@ class SystemTrayIcon(QSystemTrayIcon):
desktop_session = 'kde' desktop_session = 'kde'
# refresh packages cache timer # refresh packages cache timer
global refreshPackagesTimer, refreshPackagesCacheProcess, checkSRPMSUpdatesTimer, SRPMUpdateProcess, networkOnlineProcess global refreshPackagesTimer, refreshPackagesCacheProcess,\
refreshPackagesTimer = QtCore.QTimer(self) checkSRPMSUpdatesTimer, SRPMUpdateProcess, networkOnlineProcess
checkSRPMSUpdatesTimer = QtCore.QTimer(self) refreshPackagesTimer = QTimer(self)
refreshPackagesCacheProcess = QtCore.QProcess(self) checkSRPMSUpdatesTimer = QTimer(self)
SRPMUpdateProcess = QtCore.QProcess(self) refreshPackagesCacheProcess = QProcess(self)
networkOnlineProcess = QtCore.QProcess(self) SRPMUpdateProcess = QProcess(self)
networkOnlineProcess = QProcess(self)
refreshPackagesTimer.timeout.connect(self.refreshPackagesCache) refreshPackagesTimer.timeout.connect(self.refreshPackagesCache)
refreshPackagesCacheProcess.finished.connect(self.restartRefreshPackagesTimer) refreshPackagesCacheProcess.finished.connect(self.restartRefreshPackagesTimer)
checkSRPMSUpdatesTimer.timeout.connect(self.SRPMSCheckUpgradeList) checkSRPMSUpdatesTimer.timeout.connect(self.SRPMSCheckUpgradeList)
# software menu # software menu
global softwareManagerProcess, softwareCheckUpgradesProcess, softwareBaseInstallsProcess, dcopServerProcess global softwareManagerProcess, softwareCheckUpgradesProcess,\
softwareManagerProcess = QtCore.QProcess(self) softwareBaseInstallsProcess, dcopServerProcess
softwareCheckUpgradesProcess = QtCore.QProcess(self) softwareManagerProcess = QProcess(self)
softwareBaseInstallsProcess = QtCore.QProcess(self) softwareCheckUpgradesProcess = QProcess(self)
dcopServerProcess = QtCore.QProcess(self) softwareBaseInstallsProcess = QProcess(self)
dcopServerProcess = QProcess(self)
softwareMenu = menu.addMenu(QIcon.fromTheme("applications-system"),_("Software")) softwareMenu = menu.addMenu(QIcon.fromTheme("applications-system"),_("Software"))
softwareManageAction = softwareMenu.addAction(QIcon.fromTheme("applications-other"),_("Add/Remove software packages...")) softwareManageAction = softwareMenu.addAction(QIcon.fromTheme("applications-other"),_("Add/Remove software packages..."))
@ -67,13 +74,15 @@ class SystemTrayIcon(QSystemTrayIcon):
softwareBaseInstallsAction.triggered.connect(self.softwareBaseInstalls) softwareBaseInstallsAction.triggered.connect(self.softwareBaseInstalls)
# network menu # network menu
global networkFirewallDisableProcess, networkFirewallEnableProcess, networkFirewallConfigureProcess, networkProxyProcess global networkFirewallDisableProcess, networkFirewallEnableProcess,\
networkFirewallDisableProcess = QtCore.QProcess(self) networkFirewallConfigureProcess, networkProxyProcess
networkFirewallEnableProcess = QtCore.QProcess(self) networkFirewallDisableProcess = QProcess(self)
networkFirewallConfigureProcess = QtCore.QProcess(self) networkFirewallEnableProcess = QProcess(self)
networkProxyProcess = QtCore.QProcess(self) networkFirewallConfigureProcess = QProcess(self)
networkProxyProcess = QProcess(self)
networkMenu = menu.addMenu(QIcon.fromTheme("preferences-system-network"),_("Network")) networkMenu = menu.addMenu(QIcon.fromTheme("preferences-system-network"),
_("Network"))
networkFirewallMenu = networkMenu.addMenu(QIcon.fromTheme("security-medium"),_("Firewall")) networkFirewallMenu = networkMenu.addMenu(QIcon.fromTheme("security-medium"),_("Firewall"))
networkFirewallDisableAction = networkFirewallMenu.addAction(QIcon.fromTheme("security-low"),_("Disable")) networkFirewallDisableAction = networkFirewallMenu.addAction(QIcon.fromTheme("security-low"),_("Disable"))
networkFirewallDisableAction.triggered.connect(self.networkFirewallDisable) networkFirewallDisableAction.triggered.connect(self.networkFirewallDisable)
@ -87,15 +96,15 @@ class SystemTrayIcon(QSystemTrayIcon):
networkProxyAction.triggered.connect(self.networkProxy) networkProxyAction.triggered.connect(self.networkProxy)
SRPMUpdateProcess.finished.connect(self.SRPMSCheckUpgrade) SRPMUpdateProcess.finished.connect(self.SRPMSCheckUpgrade)
# about box # about box
aboutAction = menu.addSeparator() aboutAction = menu.addSeparator()
aboutAction = menu.addAction(QIcon.fromTheme("help-about"),_("About")) aboutAction = menu.addAction(QIcon.fromTheme("help-about"),_("About"))
aboutAction.triggered.connect(self.about) aboutAction.triggered.connect(self.about)
self.context_menu = menu
self.setToolTip(_("openmamba control center")) self.setToolTip(_("openmamba control center"))
self.activated.connect(self.iconActivated) self.activated.connect(self.iconActivated)
self.setContextMenu(menu)
self.show()
# the first time refresh packages cache after 5 minutes # the first time refresh packages cache after 5 minutes
refreshPackagesTimer.start( 5 * 60000 ) refreshPackagesTimer.start( 5 * 60000 )
@ -104,7 +113,7 @@ class SystemTrayIcon(QSystemTrayIcon):
def setProxyForSmart(self): def setProxyForSmart(self):
setProxyProcess = QtCore.QProcess(self) setProxyProcess = QProcess(self)
pf = libproxy.ProxyFactory() pf = libproxy.ProxyFactory()
for protocol in ["http","https","ftp"]: for protocol in ["http","https","ftp"]:
proxies = pf.getProxies("%s://openmamba.org" % protocol) proxies = pf.getProxies("%s://openmamba.org" % protocol)
@ -117,8 +126,8 @@ class SystemTrayIcon(QSystemTrayIcon):
break break
def iconActivated(self, reason): def iconActivated(self, reason):
if reason in (QSystemTrayIcon.Trigger, QSystemTrayIcon.DoubleClick): if reason in (QSystemTrayIcon.Trigger, QSystemTrayIcon.DoubleClick, QSystemTrayIcon.Context):
menu.popup(self.geometry().center()) self.contextMenu().popup(QCursor.pos())
def about(self): def about(self):
global version global version
@ -185,7 +194,7 @@ class SystemTrayIcon(QSystemTrayIcon):
networkFirewallDisableProcess.start("sudo",['/usr/sbin/ufw','disable']) networkFirewallDisableProcess.start("sudo",['/usr/sbin/ufw','disable'])
def networkFirewallDisableMessage(self,status): def networkFirewallDisableMessage(self,status):
if (status == QtCore.QProcess.NormalExit): if (status == QProcess.NormalExit):
self.showMessage("Firewall", _("Firewall disabled in current session")) self.showMessage("Firewall", _("Firewall disabled in current session"))
else: else:
self.showMessage("Firewall", _("Could not disable firewall")) self.showMessage("Firewall", _("Could not disable firewall"))
@ -194,7 +203,7 @@ class SystemTrayIcon(QSystemTrayIcon):
networkFirewallEnableProcess.start("sudo",['/usr/sbin/ufw','enable']) networkFirewallEnableProcess.start("sudo",['/usr/sbin/ufw','enable'])
def networkFirewallEnableMessage(self,status): def networkFirewallEnableMessage(self,status):
if (status == QtCore.QProcess.NormalExit): if (status == QProcess.NormalExit):
self.showMessage("Firewall", _("Firewall enabled in current session")) self.showMessage("Firewall", _("Firewall enabled in current session"))
else: else:
self.showMessage("Firewall", _("Could not enable firewall")) self.showMessage("Firewall", _("Could not enable firewall"))
@ -262,17 +271,14 @@ class SystemTrayIcon(QSystemTrayIcon):
def main(): def main():
app = QApplication(sys.argv) app = QApplication(sys.argv)
if (app.isSessionRestored()): if app.isSessionRestored():
app.exit(1) app.exit(1)
else: else:
app.setQuitOnLastWindowClosed(0) app.setQuitOnLastWindowClosed(0)
gettext.install('mambatray', '/usr/share/locale') gettext.install('mambatray', '/usr/share/locale')
w = QWidget() trayIcon = SystemTrayIcon(
w.setWindowIcon(QIcon.fromTheme("mambatray",QIcon("/usr/share/icons/hicolor/64x64/apps/mambatray.png"))) QIcon.fromTheme("mambatray",QIcon("/usr/share/icons/hicolor/32x32/apps/mambatray.png"))
trayIcon = SystemTrayIcon(QIcon.fromTheme("mambatray", QIcon("/usr/share/icons/hicolor/32x32/apps/mambatray.png")), w) )
trayIcon.show()
trayIcon.setContextMenu(menu)
trayIcon.setVisible(True)
sys.exit(app.exec_()) sys.exit(app.exec_())