def closeEvent(self, event): """Capture close event and hide main window. Hide main window instead of exiting. Only method to quit is from right click menu on system tray icon or view widget. Args: event (QtGui.QCloseEvent): Close event from OS requested exit like ALT+F4, clicking X on window title bar, etc. """ event.ignore() # Store window position and size settings.set_window_pos(self.pos()) settings.set_window_size(self.size()) self.hide()
def clean_up(self): """Perform actions before exiting the application. Following actions are performed before exit: unbind global hot key, save window position and size, submit all changes to model, and close database connection. """ logging.debug('Unbinding global hot key.') self.key_binder.unbind(settings.get_global_hot_key()) logging.debug('Saving window size and position.') settings.set_window_pos(self.pos()) settings.set_window_size(self.size()) settings.sync() logging.debug('Submitting changes to model.') self.main_widget.model_main.submitAll() logging.debug('Closing model.') self.db.close()
def _on_toggle_window(self): """Toggle the main window visibility. If visible, then hide the window. If not visible, then open window based on position settings at: mouse cursor, system tray, or last position. Adjust's the window position based on desktop dimensions to prevent main window going off screen. TODO: Clean up this code as it is long and can most likely be improved. """ win_size = settings.get_window_size() # QSize() # Hide window if visible and leave function if self.isVisible(): # Store window position and size settings.set_window_pos(self.pos()) settings.set_window_size(self.size()) self.hide() else: # Desktop number based on cursor desktop = QtGui.QDesktopWidget() current_screen = desktop.screenNumber(QtGui.QCursor().pos()) logging.debug('Screen #=%s' % current_screen) # Determine global coordinates by summing screen(s) coordinates x_max = 0 y_max = 999999 for screen in range(0, desktop.screenCount()): x_max += desktop.availableGeometry(screen).width() y_screen = desktop.availableGeometry(screen).height() if y_screen < y_max: y_max = y_screen # Minimum x and y screen coordinates x_min, y_min, __, __ = desktop.availableGeometry().getCoords() logging.debug('GlobalScreenRect=(%d,%d,%d,%d)' % (x_min, y_min, x_max, y_max)) # 2: System tray if settings.get_open_window_at() == 2: x = self.tray_icon.geometry().x() y = self.tray_icon.geometry().y() logging.debug('SystemTrayCoords=(%d,%d)' % (x, y)) # 1: Last position elif settings.get_open_window_at() == 1: x = settings.get_window_pos().x() y = settings.get_window_pos().y() logging.debug('LastPositionCoords=(%d,%d)' % (x ,y)) # 0: At mouse cursor else: x = QtGui.QCursor().pos().x() y = QtGui.QCursor().pos().y() logging.debug('CursorCoords=(%d,%d)' % (x, y)) # Readjust window's position if it will be off screen if x < x_min: x = x_min elif x + win_size.width() > x_max: x = x_max - win_size.width() if y < y_min: y = y_min elif y + win_size.height() > y_max: y = y_max - win_size.height() # Clear search box from last interaction if len(self.main_widget.search_box.text()) != 0: self.main_widget.search_box.clear() logging.debug('MainWindowCoords=(%d,%d)' % (x, y)) logging.debug('MainWindowSize=(%d,%d)' % (win_size.width(), win_size.height())) # Reposition and resize the main window self.move(x, y) self.resize(win_size.width(), win_size.height()) self.show() # Open window self.activateWindow() # Bring to front self.main_widget.check_selection()