class ActivityStatus(QMainWindow): """ Main application window, responsible for application lifecycle """ def __init__(self): QMainWindow.__init__(self, None, Qt.FramelessWindowHint) self.__settings = Settings() self.__activityManager = ActivityManager(self.__settings) self.__trayIcon = QSystemTrayIcon(self) self.__managerController = ActivityManagerControl(self, self.__activityManager) self.__appMenu = QMenu(self) self.__closeAction = QAction(self.tr('Close'), self) self.__appIcon = resources.getIcon('pomidor.png') self._configureActions() self._configureMenu() self._setupTrayIcon() self._configureMainWindow() self._setupEventHooks() logging.debug('Application started') def _setupTrayIcon(self): """ Set initial image on tray icon """ self.__trayIcon.setIcon(self.__appIcon) self.__trayIcon.show() self.__trayIcon.activated.connect(self._trayIconClicked) self.__trayIcon.setContextMenu(self.__appMenu) def _configureMainWindow(self): """Configure main window contents""" self.setCentralWidget(self.__managerController) self.setWindowIcon(self.__appIcon) def _setupEventHooks(self): """Connect to event hooks provided by the activity manager""" self.__activityManager.activityStarted += self._hideMainWindow self.__activityManager.workActivityEnded += self._notifyActivityEnding self.__activityManager.breakActivityEnded += self._notifyActivityEnding self.__activityManager.activityTimeChanged += self._showRemainingTime def _configureMenu(self): """Configure application menu, add all actions and separators""" self.__appMenu.addActions(self.__managerController.actionList) self.__appMenu.addSeparator() self.__appMenu.addAction(self.__closeAction) def _configureActions(self): """Configure actions of the main controller""" self.__closeAction.triggered.connect(_closeApplication) def _trayIconClicked(self, reason): """ Process the click on the tray icon @param reason: how the icon was clicked """ logging.debug('Tray icon clicked') if reason == QSystemTrayIcon.Trigger: if self.isVisible(): self._hideMainWindow() else: self._showMainWindw() def closeEvent(self, event): """ Prevent main window from closing by clicking on close button @param event: the event, which controls the operation @type event: QCloseEvent """ event.ignore() self._hideMainWindow() def _hideMainWindow(self, _=''): """Hide main window from the screen""" logging.debug('Main window is hidden') self.setVisible(False) def _showMainWindw(self): """Show main window near-by to the system tray icon""" logging.debug('Main window is shown') self.setVisible(True) trayIconGeometry = self.__trayIcon.geometry() screenGeometry = QApplication.desktop().screenGeometry(trayIconGeometry.topLeft()) self.move(_calculateWindowPosition(screenGeometry, trayIconGeometry, self.width(), self.height())) def _notifyActivityEnding(self): """Invoke activity ending action""" logging.debug('Notifying user about action ending') process = Process(target=_executeAction, args=(self.__settings.endActivityAction,)) process.start() self.__trayIcon.setIcon(self.__appIcon) def _showRemainingTime(self, seconds): """ Show remaining time to the user @param seconds: remaining time @type seconds: int """ if seconds > 60: time = int(seconds / 60) else: time = seconds text = "{0:02d}".format(time) basePixelMap = resources.getPixelMap('pomidor.png') painter = QPainter(basePixelMap) painter.setFont(QFont("PT Sans", 64, QFont.Bold)) painter.setPen(QPen(QColor(0, 0, 0))) painter.drawText(basePixelMap.rect(), Qt.AlignCenter, text) painter.setFont(QFont("PT Sans", 58, QFont.Bold)) painter.setPen(QPen(QColor(240, 240, 240))) painter.drawText(basePixelMap.rect(), Qt.AlignCenter, text) painter.end() self.__trayIcon.setIcon(QIcon(basePixelMap))
#refactor def _quit(): app.quit() qDebug('bye!') def show_window(thereason): if thereason == QSystemTrayIcon.ActivationReason.Trigger: if main_window.isVisible() == True: main_window.hide() main_window.show() #create the QApplication instance before any other widget app = QApplication(sys.argv) app.setQuitOnLastWindowClosed(False) ico = QIcon(os.path.join(os.getcwd(),'yhn.png')) systrayico = QSystemTrayIcon(ico) systrayico.activated.connect(show_window) systrayico.show() x_pos = systrayico.geometry().left() y_pos = app.desktop().availableGeometry(app.desktop().primaryScreen()).topLeft().y() main_window = MainWindow(x_pos, y_pos, 420, 500) sys.exit(app.exec_())