def __init__(self, pyluggage_config, target_path=None, version_string=None, minimize_when_open=False): QMainWindow.__init__(self) Ui_MainWindow.__init__(self) self.pyluggage_config = pyluggage_config self.version_string = version_string self.setupUi(self) gui_tools.center_widget(self) self.setWindowIcon(QIcon(":/pyluggage/img/cryptoluggage-logo.png")) self.frame_welcome = WelcomeFrame(pyluggage_config=pyluggage_config, version_string=version_string) self.frame_luggage = LuggageFrame(pyluggage_config=pyluggage_config) self.verticalLayout.addWidget(self.frame_welcome) self.verticalLayout.addWidget(self.frame_luggage) self.frame_luggage.hide() self.frame_luggage.help_needed.connect(self._open_help) self.frame_welcome.luggage_opened.connect(self.display_luggage) self.frame_luggage.luggage_closed.connect(self.close_luggage) self.frame_welcome.help_needed.connect(self._open_help) self.setVisible(True) # If available, create a tray icon and a menu if QSystemTrayIcon.isSystemTrayAvailable(): self.tray_icon = TrayIcon.LuggageTrayIcon(parent=self) self.tray_icon.clicked.connect(self.__process_tray_icon_click) self.tray_icon.close_luggage_clicked.connect(self.close_luggage) self.tray_icon.exit_clicked.connect(self.exit_app) self.tray_icon.show() else: self.tray_icon = None if target_path is not None: if not os.path.isfile(target_path): QMessageBox.critical( self, _translate("Luggage does not exist"), _translate("The Luggage at {path} does not exist.\nPlease open manually.".format( path=target_path))) else: self.frame_welcome.open_luggage_query_pass(luggage_path=target_path) else: self.close_luggage() if minimize_when_open: self.__process_tray_icon_click()
class MainWindow(QMainWindow, Ui_MainWindow): # Luggage currently handled by this window (can be None) luggage = None # Frames being handled in this window frame_welcome = None frame_luggage = None frame_config = None # # Signals emitted when the window is shown an hidden (after setup) # window_hidden = QtCore.pyqtSignal() # window_shown = QtCore.pyqtSignal() def __init__(self, pyluggage_config, target_path=None, version_string=None, minimize_when_open=False): QMainWindow.__init__(self) Ui_MainWindow.__init__(self) self.pyluggage_config = pyluggage_config self.version_string = version_string self.setupUi(self) gui_tools.center_widget(self) self.setWindowIcon(QIcon(":/pyluggage/img/cryptoluggage-logo.png")) self.frame_welcome = WelcomeFrame(pyluggage_config=pyluggage_config, version_string=version_string) self.frame_luggage = LuggageFrame(pyluggage_config=pyluggage_config) self.verticalLayout.addWidget(self.frame_welcome) self.verticalLayout.addWidget(self.frame_luggage) self.frame_luggage.hide() self.frame_luggage.help_needed.connect(self._open_help) self.frame_welcome.luggage_opened.connect(self.display_luggage) self.frame_luggage.luggage_closed.connect(self.close_luggage) self.frame_welcome.help_needed.connect(self._open_help) self.setVisible(True) # If available, create a tray icon and a menu if QSystemTrayIcon.isSystemTrayAvailable(): self.tray_icon = TrayIcon.LuggageTrayIcon(parent=self) self.tray_icon.clicked.connect(self.__process_tray_icon_click) self.tray_icon.close_luggage_clicked.connect(self.close_luggage) self.tray_icon.exit_clicked.connect(self.exit_app) self.tray_icon.show() else: self.tray_icon = None if target_path is not None: if not os.path.isfile(target_path): QMessageBox.critical( self, _translate("Luggage does not exist"), _translate("The Luggage at {path} does not exist.\nPlease open manually.".format( path=target_path))) else: self.frame_welcome.open_luggage_query_pass(luggage_path=target_path) else: self.close_luggage() if minimize_when_open: self.__process_tray_icon_click() def display_luggage(self, open_luggage): """Setup the GUI to display an open Luggage """ self.luggage = open_luggage self.frame_luggage.display_luggage(open_luggage) self.frame_welcome.hide() self.frame_luggage.show() if self.tray_icon is not None: self.tray_icon.show_unlocked_icon() self.setFixedSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX) self.resize(self.sizeHint().width(), QDesktopWidget().screen().height()) def close_luggage(self): if self.luggage is not None: self.luggage = None self.frame_luggage.close_luggage() self.frame_luggage.hide() self.frame_welcome.show() self.frame_welcome.update_controls() if self.tray_icon is not None: self.tray_icon.show_locked_icon() self.setFixedSize(self.sizeHint()) def closeEvent(self, QCloseEvent): """Handle the close event to avoid undesired exits """ self.close_luggage() def exit_app(self): # Refuse to exit if a progress is being run for w in QApplication.topLevelWidgets(): if isinstance(w, ProgressDialog): QMessageBox.warning( self, _translate("Cannot exit"), _translate("Cannot exit CryptoLuggage until the current action is completed.")) return sys.exit(0) def _open_help(self): webbrowser.open(self.pyluggage_config.help_url) def __process_tray_icon_click(self): """Process a click in the tray icon """ # Avoid hide/restore when a non-hideable dialog is present for w in QApplication.topLevelWidgets(): if w.isVisible() and isinstance(w, QDialog) and not isinstance(w, HideableQDialog): if be_verbose: print "[MainWindow.__process_tray_icon_click] Non dialog instance " \ "{} was visible. Aborting minimization.".format(w) return main_window_visible = None for w in QApplication.topLevelWidgets(): if isinstance(w, QMainWindow): main_window_visible = w.isVisible() break assert main_window_visible is not None # Hide/restore the QMainWindow and (if present) a HideableQDialog instance for w in QApplication.topLevelWidgets(): # Hide/show window and dialogs if isinstance(w, QMainWindow) or isinstance(w, HideableQDialog): if w.isVisible() != main_window_visible: print "[MainWindow] Warning! This HideableQDialog had different visibility than the main window.\n" \ "Maybe call dialog.deleteLater() after dialog.exec_() when invoked?\n" \ "Calling dialog.deleteLater() now" w.deleteLater() continue w.setVisible(not main_window_visible) # Show/hide the "show/hide CryptoLuggage" menu entry self.tray_icon.set_luggage_shown(self.isVisible())