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