def deleteSelectedTool(self): """Delete the currently selected item""" current_row = self.selectedRow() if current_row == -1: # no row selected return tdata = self.tool_model.toolDataFromRow(current_row) tnum = tdata['T'] # should not delete tool if currently loaded in spindle. Warn user if tnum == self.tool_model.stat.tool_in_spindle: box = QMessageBox( QMessageBox.Warning, "Can't delete current tool!", "Tool #{} is currently loaded in the spindle.\n" "Please remove tool from spindle and try again.".format(tnum), QMessageBox.Ok, parent=self) box.show() return False if not self.confirmAction( 'Are you sure you want to delete T{tdata[T]}?\n' '"{tdata[R]}"'.format(tdata=tdata)): return self.tool_model.removeTool(current_row)
def display_warning(self): m = QMessageBox(self.parent()) m.setText( 'You need to restart the server for the changes to take effect') m.setWindowTitle('Warning') m.setIcon(QMessageBox.Information) m.show()
def generate_images(): """ Generate images from `CORE_SETTINGS`. and save them in the developer section of the docs. """ app = get_app() pref = PreferencesDialog() pref.setStyleSheet(get_stylesheet("dark")) pref.show() QTimer.singleShot(1000, pref.close) for idx, (name, field) in enumerate(NapariSettings.__fields__.items()): pref._stack.setCurrentIndex(idx) pixmap = pref.grab() title = field.field_info.title or name pixmap.save(str(IMAGES_PATH / f"preferences-{title.lower()}.png")) box = QMessageBox( QMessageBox.Icon.Question, "Restore Settings", "Are you sure you want to restore default settings?", QMessageBox.RestoreDefaults | QMessageBox.Cancel, pref, ) box.show() def grab(): pixmap = box.grab() pixmap.save(str(IMAGES_PATH / "preferences-reset.png")) box.reject() QTimer.singleShot(300, grab) app.exec_()
def confirm_restore_defaults(self): m = QMessageBox(QMessageBox.Question, "Restore to defaults", "Restore settings to the default state?\n" "You'll need to restart the program.", QMessageBox.Yes | QMessageBox.No, self) m.setDefaultButton(QMessageBox.No) yesButton = m.button(QMessageBox.Yes) yesButton.clicked.connect(self.restore_defaults) m.show()
def show_dialog(parent, title, text, icon): m = QMessageBox(parent) m.setWindowModality(Qt.NonModal) m.setText(text) m.setWindowTitle(title) m.setAttribute(Qt.WA_DeleteOnClose, True) m.setIcon(icon) m.show() center_widget_on_screen(m)
def show_error_message(self, message): """Show error message.""" messageBox = QMessageBox(self) messageBox.setWindowModality(Qt.NonModal) messageBox.setAttribute(Qt.WA_DeleteOnClose) messageBox.setWindowTitle('Render Report Error') messageBox.setText(message) messageBox.setStandardButtons(QMessageBox.Ok) messageBox.show()
def _show_compatibility_message(self, message): """Show a compatibility message.""" messageBox = QMessageBox(self) messageBox.setWindowModality(Qt.NonModal) messageBox.setAttribute(Qt.WA_DeleteOnClose) messageBox.setWindowTitle('Compatibility Check') messageBox.setText(message) messageBox.setStandardButtons(QMessageBox.Ok) messageBox.show()
def show_compatibility_message(self, message): """Show compatibility message.""" messageBox = QMessageBox(self) messageBox.setWindowModality(Qt.NonModal) messageBox.setAttribute(Qt.WA_DeleteOnClose) messageBox.setWindowTitle('Compatibility Check') messageBox.setText(message) messageBox.setStandardButtons(QMessageBox.Ok) messageBox.show()
def about(self): """ Reset message box is based on aboutWindow object For some reason, I did not get time to fix that :return: """ about_message_box = QMessageBox().window() about_message_box.about( self.pushButton, "Info", "Please restart guiscrcpy to reset the settings. " "guiscrcpy will now exit", ) about_message_box.addButton("OK", about_message_box.hide) # noqa: about_message_box.show()
def on_exception(self, exception): """ Called when the `QThread` runs into an exception. Parameters ---------- exception : Exception The Exception that interrupted the `QThread`. """ self.smooth_button.setEnabled(True) self.cancel_button.setEnabled(True) info_box = QMessageBox(parent=self) info_box.setWindowTitle("Smoothing Error") info_box.setIcon(QMessageBox.Critical) info_box.setText(str(exception)) info_box.setStandardButtons(QMessageBox.Ok) info_box.show()
def reset(self): """ Remove configuration files; Reset the mapper and guiscrcpy.json :return: """ self.cfgmgr.reset_config() log("CONFIGURATION FILE REMOVED SUCCESSFULLY") log("RESTART") message_box = QMessageBox().window() message_box.about( self.pushButton, "Info", "Please restart guiscrcpy to reset the settings. " "guiscrcpy will now exit", ) QMessageBox.ButtonRole() message_box.addButton("OK", self.quit_window) # noqa: message_box.show()
def _update_value(self): text = self.value_input.text() try: if self._parameter.type == "LIST_OF_INT": self._parameter.value = list(map(int, text.split(","))) elif self._parameter.type == "LIST_OF_FLOAT": self._parameter.value = list(map(float, text.split(","))) elif self._parameter.type is float: self._parameter.value = float(text) elif self._parameter.type is int: self._parameter.value = int(text) except Exception as e: self._set_text() msg = QMessageBox(self) msg.setIcon(QMessageBox.Critical) msg.setWindowTitle('Setting Error') msg.setText('Illegal Parameter Setting') msg.setInformativeText(f"{text} is illegal for the parameter.<br>" + str(e)) msg.setStandardButtons(QMessageBox.Ok) msg.show()
def report_missing_dependencies(self): """Show a QMessageBox with a list of missing hard dependencies.""" missing_deps = dependencies.missing_dependencies() if missing_deps: InstallerMissingDependencies(missing_deps) # We change '<br>' by '\n', in order to replace the '<' # that appear in our deps by '<' (to not break html # formatting) and finally we restore '<br>' again. missing_deps = (missing_deps.replace('<br>', '\n'). replace('<', '<').replace('\n', '<br>')) message = ( _("<b>You have missing dependencies!</b>" "<br><br><tt>%s</tt><br>" "<b>Please install them to avoid this message.</b>" "<br><br>" "<i>Note</i>: Spyder could work without some of these " "dependencies, however to have a smooth experience when " "using Spyder we <i>strongly</i> recommend you to install " "all the listed missing dependencies.<br><br>" "Failing to install these dependencies might result in bugs." " Please be sure that any found bugs are not the direct " "result of missing dependencies, prior to reporting a new " "issue." ) % missing_deps ) message_box = QMessageBox(self) message_box.setIcon(QMessageBox.Critical) message_box.setAttribute(Qt.WA_DeleteOnClose) message_box.setAttribute(Qt.WA_ShowWithoutActivating) message_box.setStandardButtons(QMessageBox.Ok) message_box.setWindowModality(Qt.NonModal) message_box.setWindowTitle(_('Error')) message_box.setText(message) message_box.show()
class Console(SpyderPluginWidget): """ Console widget """ CONF_SECTION = 'internal_console' focus_changed = Signal() redirect_stdio = Signal(bool) edit_goto = Signal(str, int, str) def __init__(self, parent=None, namespace=None, commands=[], message=None, exitfunc=None, profile=False, multithreaded=False): SpyderPluginWidget.__init__(self, parent) debug_print(" ..internal console: initializing") self.dialog_manager = DialogManager() # Shell light_background = self.get_option('light_background') self.shell = InternalShell(parent, namespace, commands, message, self.get_option('max_line_count'), self.get_plugin_font(), exitfunc, profile, multithreaded, light_background=light_background) self.shell.status.connect(lambda msg: self.show_message.emit(msg, 0)) self.shell.go_to_error.connect(self.go_to_error) self.shell.focus_changed.connect(lambda: self.focus_changed.emit()) # Redirecting some signals: self.shell.redirect_stdio.connect( lambda state: self.redirect_stdio.emit(state)) # Initialize plugin self.initialize_plugin() # Find/replace widget self.find_widget = FindReplace(self) self.find_widget.set_editor(self.shell) self.find_widget.hide() self.register_widget_shortcuts(self.find_widget) # Main layout btn_layout = QHBoxLayout() btn_layout.setAlignment(Qt.AlignLeft) btn_layout.addStretch() btn_layout.addWidget(self.options_button, Qt.AlignRight) layout = create_plugin_layout(btn_layout) layout.addWidget(self.shell) layout.addWidget(self.find_widget) self.setLayout(layout) # Parameters self.shell.toggle_wrap_mode(self.get_option('wrap')) # Accepting drops self.setAcceptDrops(True) # Traceback MessageBox self.msgbox_traceback = None self.error_traceback = "" #------ Private API -------------------------------------------------------- def set_historylog(self, historylog): """Bind historylog instance to this console Not used anymore since v2.0""" historylog.add_history(self.shell.history_filename) self.shell.append_to_history.connect(historylog.append_to_history) def set_help(self, help_plugin): """Bind help instance to this console""" self.shell.help = help_plugin #------ SpyderPluginWidget API --------------------------------------------- def get_plugin_title(self): """Return widget title""" return _('Internal console') def get_focus_widget(self): """ Return the widget to give focus to when this plugin's dockwidget is raised on top-level """ return self.shell def update_font(self): """Update font from Preferences""" font = self.get_plugin_font() self.shell.set_font(font) def closing_plugin(self, cancelable=False): """Perform actions before parent main window is closed""" self.dialog_manager.close_all() self.shell.exit_interpreter() return True def refresh_plugin(self): pass def get_plugin_actions(self): """Return a list of actions related to plugin""" quit_action = create_action(self, _("&Quit"), icon=ima.icon('exit'), tip=_("Quit"), triggered=self.quit) self.register_shortcut(quit_action, "_", "Quit", "Ctrl+Q") run_action = create_action(self, _("&Run..."), None, ima.icon('run_small'), _("Run a Python script"), triggered=self.run_script) environ_action = create_action( self, _("Environment variables..."), icon=ima.icon('environ'), tip=_("Show and edit environment variables" " (for current session)"), triggered=self.show_env) syspath_action = create_action(self, _("Show sys.path contents..."), icon=ima.icon('syspath'), tip=_("Show (read-only) sys.path"), triggered=self.show_syspath) buffer_action = create_action(self, _("Buffer..."), None, tip=_("Set maximum line count"), triggered=self.change_max_line_count) exteditor_action = create_action( self, _("External editor path..."), None, None, _("Set external editor executable path"), triggered=self.change_exteditor) wrap_action = create_action(self, _("Wrap lines"), toggled=self.toggle_wrap_mode) wrap_action.setChecked(self.get_option('wrap')) calltips_action = create_action(self, _("Display balloon tips"), toggled=self.toggle_calltips) calltips_action.setChecked(self.get_option('calltips')) codecompletion_action = create_action( self, _("Automatic code completion"), toggled=self.toggle_codecompletion) codecompletion_action.setChecked( self.get_option('codecompletion/auto')) codecompenter_action = create_action( self, _("Enter key selects completion"), toggled=self.toggle_codecompletion_enter) codecompenter_action.setChecked( self.get_option('codecompletion/enter_key')) option_menu = QMenu(_('Internal console settings'), self) option_menu.setIcon(ima.icon('tooloptions')) add_actions( option_menu, (buffer_action, wrap_action, calltips_action, codecompletion_action, codecompenter_action, exteditor_action)) plugin_actions = [ None, run_action, environ_action, syspath_action, option_menu, MENU_SEPARATOR, quit_action, self.undock_action ] return plugin_actions def register_plugin(self): """Register plugin in Spyder's main window""" self.focus_changed.connect(self.main.plugin_focus_changed) self.main.add_dockwidget(self) # Connecting the following signal once the dockwidget has been created: self.shell.exception_occurred.connect(self.exception_occurred) def exception_occurred(self, text, is_traceback): """Exception ocurred in the internal console. Show a QMessageBox or the internal console to warn the user""" # Skip errors without traceback if not is_traceback and self.msgbox_traceback is None: return if CONF.get('main', 'show_internal_console_if_traceback', False): self.dockwidget.show() self.dockwidget.raise_() else: if self.msgbox_traceback is None: self.msgbox_traceback = QMessageBox( QMessageBox.Critical, _('Error'), _("<b>Spyder has encountered a problem.</b><br>" "Sorry for the inconvenience." "<br><br>" "You can automatically submit this error to our Github " "issues tracker.<br><br>" "<i>Note:</i> You need a Github account for that."), QMessageBox.Ok, parent=self) self.submit_btn = self.msgbox_traceback.addButton( _('Submit to Github'), QMessageBox.YesRole) self.submit_btn.pressed.connect(self.press_submit_btn) self.msgbox_traceback.setWindowModality(Qt.NonModal) self.error_traceback = "" self.msgbox_traceback.show() self.msgbox_traceback.finished.connect(self.close_msg) self.msgbox_traceback.setDetailedText(' ') # open show details (iterate over all buttons and click it) for button in self.msgbox_traceback.buttons(): if (self.msgbox_traceback.buttonRole(button) == QMessageBox.ActionRole): button.click() break self.error_traceback += text self.msgbox_traceback.setDetailedText(self.error_traceback) def close_msg(self): self.msgbox_traceback = None def press_submit_btn(self): self.main.report_issue(self.error_traceback) self.msgbox_traceback = None #------ Public API --------------------------------------------------------- @Slot() def quit(self): """Quit mainwindow""" self.main.close() @Slot() def show_env(self): """Show environment variables""" self.dialog_manager.show(EnvDialog()) @Slot() def show_syspath(self): """Show sys.path""" editor = CollectionsEditor() editor.setup(sys.path, title="sys.path", readonly=True, width=600, icon=ima.icon('syspath')) self.dialog_manager.show(editor) @Slot() def run_script(self, filename=None, silent=False, set_focus=False, args=None): """Run a Python script""" if filename is None: self.shell.interpreter.restore_stds() filename, _selfilter = getopenfilename( self, _("Run Python script"), getcwd_or_home(), _("Python scripts") + " (*.py ; *.pyw ; *.ipy)") self.shell.interpreter.redirect_stds() if filename: os.chdir(osp.dirname(filename)) filename = osp.basename(filename) else: return debug_print(args) filename = osp.abspath(filename) rbs = remove_backslashes command = "runfile('%s', args='%s')" % (rbs(filename), rbs(args)) if set_focus: self.shell.setFocus() if self.dockwidget and not self.ismaximized: self.dockwidget.setVisible(True) self.dockwidget.raise_() self.shell.write(command + '\n') self.shell.run_command(command) def go_to_error(self, text): """Go to error if relevant""" match = get_error_match(to_text_string(text)) if match: fname, lnb = match.groups() self.edit_script(fname, int(lnb)) def edit_script(self, filename=None, goto=-1): """Edit script""" # Called from InternalShell if not hasattr(self, 'main') \ or not hasattr(self.main, 'editor'): self.shell.external_editor(filename, goto) return if filename is not None: self.edit_goto.emit(osp.abspath(filename), goto, '') def execute_lines(self, lines): """Execute lines and give focus to shell""" self.shell.execute_lines(to_text_string(lines)) self.shell.setFocus() @Slot() def change_max_line_count(self): "Change maximum line count" "" mlc, valid = QInputDialog.getInt(self, _('Buffer'), _('Maximum line count'), self.get_option('max_line_count'), 0, 1000000) if valid: self.shell.setMaximumBlockCount(mlc) self.set_option('max_line_count', mlc) @Slot() def change_exteditor(self): """Change external editor path""" path, valid = QInputDialog.getText( self, _('External editor'), _('External editor executable path:'), QLineEdit.Normal, self.get_option('external_editor/path')) if valid: self.set_option('external_editor/path', to_text_string(path)) @Slot(bool) def toggle_wrap_mode(self, checked): """Toggle wrap mode""" self.shell.toggle_wrap_mode(checked) self.set_option('wrap', checked) @Slot(bool) def toggle_calltips(self, checked): """Toggle calltips""" self.shell.set_calltips(checked) self.set_option('calltips', checked) @Slot(bool) def toggle_codecompletion(self, checked): """Toggle automatic code completion""" self.shell.set_codecompletion_auto(checked) self.set_option('codecompletion/auto', checked) @Slot(bool) def toggle_codecompletion_enter(self, checked): """Toggle Enter key for code completion""" self.shell.set_codecompletion_enter(checked) self.set_option('codecompletion/enter_key', checked) #----Drag and drop def dragEnterEvent(self, event): """Reimplement Qt method Inform Qt about the types of data that the widget accepts""" source = event.mimeData() if source.hasUrls(): if mimedata2url(source): event.acceptProposedAction() else: event.ignore() elif source.hasText(): event.acceptProposedAction() def dropEvent(self, event): """Reimplement Qt method Unpack dropped data and handle it""" source = event.mimeData() if source.hasUrls(): pathlist = mimedata2url(source) self.shell.drop_pathlist(pathlist) elif source.hasText(): lines = to_text_string(source.text()) self.shell.set_cursor_position('eof') self.shell.execute_lines(lines) event.acceptProposedAction()
class AbortWindow(QDialog): """ Displays busy message and provides abort button. The class serves SmoothCube, WorkerThread and SelectSmoothing. """ def __init__(self, parent=None): """ init abort or notification ui. Displays while smoothing freezes the application. Allows abort button to be added if needed. """ super(AbortWindow, self).__init__(parent) self.setModal(False) self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint) self.parent = parent self.label_a_1 = QLabel("Executing smoothing algorithm.") self.label_a_2 = QLabel("This may take several minutes.") self.abort_button = QPushButton("Abort") self.abort_button.clicked.connect(self.abort) self.pb = QProgressBar(self) self.pb_counter = 0 self.abort_flag = False self.info_box = None # vbl is short for Vertical Box Layout vbl = QVBoxLayout() vbl.addWidget(self.label_a_1) vbl.addWidget(self.label_a_2) vbl.addWidget(self.pb) vbl.addWidget(self.abort_button) self.setLayout(vbl) self.show() def init_pb(self, start, end): """ Init the progress bar :param start: Start Value :param end: End Value """ self.pb.setRange(start, end) self.pb_counter = start def update_pb(self): """ This function is called in the worker thread to update the progress bar and checks if the local class variable abort_flag is active. If the abort button is clicked, the main thread will set abort_flag to True. The next time the worker thread calls this function, a custom exception is raised terminating the calculation. The exception is handled by the WorkerThread class. :raises: AbortException: terminating smoothing calculation """ if self.abort_flag: raise AbortException("Abort Calculation") self.pb_counter += 1 self.pb.setValue(self.pb_counter) QApplication.processEvents() def abort(self): """Abort calculation""" self.abort_flag = True self.parent.clean_up() def show_error_message(self, message, title, parent=None): self.info_box = QMessageBox(parent=parent) self.info_box.setIcon(QMessageBox.Information) self.info_box.setText(message) self.info_box.setWindowTitle(title) self.info_box.setStandardButtons(QMessageBox.Ok) self.info_box.show() def smoothing_done(self, component_id=None): """Notify user success""" self.hide() if component_id is None: message = "The result has been added as a" \ " new component of the input Data." \ " The new component can be accessed" \ " in the viewer drop-downs." else: message = "The result has been added as" \ " \"{0}\" and can be selected" \ " in the viewer drop-down menu.".format(component_id) self.show_error_message(message, "Success", self) self.clean_up() def print_error(self, exception): """Print error message""" if "signal only works in main thread" in str(exception): message = "Smoothing Failed!\n\n" + "Please update your SpectralCube package" else: message = "Smoothing Failed!\n\n" + str(exception) self.show_error_message(message, "Error", self) self.clean_up() def clean_up(self): self.parent.clean_up() def keyPressEvent(self, e): if e.key() == Qt.Key_Escape: self.abort()
class FileDialog(QDialog): def __init__(self, file_name, job_name, job_number, realization, iteration, parent=None): super(FileDialog, self).__init__(parent) self.setWindowTitle("{} # {} Realization: {} Iteration: {}".format( job_name, job_number, realization, iteration)) try: self._file = open(file_name, "r") except OSError as error: self._mb = QMessageBox( QMessageBox.Critical, "Error opening file", error.strerror, QMessageBox.Ok, self, ) self._mb.finished.connect(self.accept) self._mb.show() return self._view = QPlainTextEdit() self._view.setReadOnly(True) self._view.setWordWrapMode(QTextOption.NoWrap) # for moving the actual slider self._view.verticalScrollBar().sliderMoved.connect(self._update_cursor) # for mouse wheel and keyboard arrows self._view.verticalScrollBar().valueChanged.connect( self._update_cursor) self._view.setFont(QFontDatabase.systemFont(QFontDatabase.FixedFont)) self._follow_mode = False self._init_layout() self._init_thread() self.show() @Slot() def _stop_thread(self): self._thread.quit() self._thread.wait() def _init_layout(self): self.setMinimumWidth(600) self.setMinimumHeight(400) dialog_buttons = QDialogButtonBox(QDialogButtonBox.Ok) dialog_buttons.accepted.connect(self.accept) self._copy_all_button = dialog_buttons.addButton( "Copy All", QDialogButtonBox.ActionRole) self._copy_all_button.clicked.connect(self._copy_all) self._follow_button = dialog_buttons.addButton( "Follow", QDialogButtonBox.ActionRole) self._follow_button.setCheckable(True) self._follow_button.toggled.connect(self._enable_follow_mode) self._enable_follow_mode(self._follow_mode) layout = QVBoxLayout(self) layout.addWidget(self._view) layout.addWidget(dialog_buttons) def _init_thread(self): self._thread = QThread() self._worker = FileUpdateWorker(self._file) self._worker.moveToThread(self._thread) self._worker.read.connect(self._append_text) self._thread.started.connect(self._worker.setup) self._thread.finished.connect(self._worker.stop) self._thread.finished.connect(self._worker.deleteLater) self.finished.connect(self._stop_thread) self._thread.start() def _copy_all(self) -> None: text = self._view.toPlainText() QApplication.clipboard().setText(text, QClipboard.Clipboard) pass def _update_cursor(self, value: int) -> None: if not self._view.textCursor().hasSelection(): block = self._view.document().findBlockByLineNumber(value) cursor = QTextCursor(block) self._view.setTextCursor(cursor) def _enable_follow_mode(self, enable: bool) -> None: if enable: self._view.moveCursor(QTextCursor.End) self._view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self._view.verticalScrollBar().setDisabled(True) self._view.setTextInteractionFlags(Qt.NoTextInteraction) self._follow_mode = True else: self._view.verticalScrollBar().setDisabled(False) self._view.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self._view.setTextInteractionFlags(Qt.TextSelectableByMouse | Qt.TextSelectableByKeyboard) self._follow_mode = False def _append_text(self, text: str) -> None: # Remove trailing newline as appendPlainText adds this if text[-1:] == "\n": text = text[:-1] if self._follow_mode: self._view.moveCursor(QTextCursor.End) self._view.appendPlainText(text)
class FileDialog(QDialog): def __init__(self, file_name, job_name, job_number, realization, iteration, parent=None): super(FileDialog, self).__init__(parent) self.setWindowTitle("{} # {} Realization: {} Iteration: {}" \ .format(job_name, job_number, realization, iteration)) self._file_name = file_name try: self._file = open(file_name, "r") except OSError as error: self._mb = QMessageBox(QMessageBox.Critical, "Error opening file", error.strerror, QMessageBox.Ok, self) self._mb.finished.connect(self.accept) self._mb.show() return self._model = FileModel() self._view = FileView() self._view.setModel(self._model) self._init_layout() self._init_thread() self.show() @Slot() def _stop_thread(self): self._thread.quit() self._thread.wait() def _init_layout(self): self.setMinimumWidth(400) self.setMinimumHeight(200) dialog_buttons = QDialogButtonBox(QDialogButtonBox.Ok) self._follow = dialog_buttons.addButton("Follow", QDialogButtonBox.ActionRole) self._copy_all = dialog_buttons.addButton("Copy All", QDialogButtonBox.ActionRole) dialog_buttons.accepted.connect(self.accept) self._follow.setCheckable(True) self._follow.toggled.connect(self._view.enable_follow_mode) self._copy_all.clicked.connect(self._model.copy_all) layout = QVBoxLayout(self) layout.addWidget(self._view) layout.addWidget(dialog_buttons) def _init_thread(self): self._thread = QThread() self._worker = FileUpdateWorker(self._file) self._worker.moveToThread(self._thread) self._worker.read.connect(self._model.append_text) self._thread.started.connect(self._worker.setup) self._thread.finished.connect(self._worker.stop) self._thread.finished.connect(self._worker.deleteLater) self.finished.connect(self._stop_thread) self._thread.start()
def __init__(self): super(XicamMainWindow, self).__init__() # Set icon self.setWindowIcon(QIcon(QPixmap(str(path("icons/xicam.gif"))))) # Set size and position self.setGeometry(0, 0, 1000, 600) frameGm = self.frameGeometry() screen = QApplication.desktop().screenNumber( QApplication.desktop().cursor().pos()) centerPoint = QApplication.desktop().screenGeometry(screen).center() frameGm.moveCenter(centerPoint) self.move(frameGm.topLeft()) # Init child widgets to None self.topwidget = (self.leftwidget) = (self.rightwidget) = ( self.bottomwidget ) = self.lefttopwidget = self.righttopwidget = self.leftbottomwidget = self.rightbottomwidget = None # Setup appearance self.setWindowTitle("Xi-cam") self.load_style() # Attach an object to restore config when loaded self._config_restorer = ConfigRestorer() # Load plugins pluginmanager.qt_is_safe = True pluginmanager.initialize_types() pluginmanager.collect_plugins() pluginmanager.collect_user_plugins() # Setup center/toolbar/statusbar/progressbar self.pluginmodewidget = pluginModeWidget() self.pluginmodewidget.sigSetStage.connect(self.setStage) self.pluginmodewidget.sigSetGUIPlugin.connect(self.setGUIPlugin) self.addToolBar(self.pluginmodewidget) self.setStatusBar(QStatusBar(self)) msg.progressbar = QProgressBar(self) msg.progressbar.hide() msg.statusbar = self.statusBar() self.statusBar().addPermanentWidget(msg.progressbar) self.setCentralWidget(QStackedWidget()) # NOTE: CentralWidgets are force-deleted when replaced, even if the object is still referenced; # To avoid this, a QStackedWidget is used for the central widget. # Setup menubar menubar = DebuggableMenuBar() self.setMenuBar(menubar) file = QMenu("&File", parent=menubar) plugins = QMenu("&Plugins", parent=menubar) menubar.addMenu(file) file.addAction("Se&ttings", self.showSettings, shortcut=QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_S)) file.addAction("E&xit", self.close) menubar.addMenu(plugins) plugins.addAction("Open User &Plugin Directory", self.openUserPluginDir, shortcut=QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_P)) # Set up help help = QMenu("&Help", parent=menubar) documentation_link = QUrl("https://xi-cam.readthedocs.io/en/latest/") help.addAction("Xi-CAM &Help", lambda: QDesktopServices.openUrl(documentation_link)) slack_link = QUrl("https://nikea.slack.com") help.addAction("Chat on &Slack", lambda: QDesktopServices.openUrl(slack_link)) help.addSeparator() about_title = "About Xi-CAM" version_text = f"""Version: <strong>{version.get_versions()['version']}</strong>""" copyright_text = f"""<small>Copyright (c) 2016, The Regents of the University of California, \ through Lawrence Berkeley National Laboratory \ (subject to receipt of any required approvals from the U.S. Dept. of Energy). \ All rights reserved.</small>""" funding_text = f"""Funding for this research was provided by: \ Lawrence Berkeley National Laboratory (grant No. TReXS LDRD to AH); \ US Department of Energy (award No. Early Career Award to AH; \ contract No. DE-SC0012704; contract No. DE-AC02-06CH11357; \ contract No. DE-AC02-76SF00515; contract No. DE-AC02-05CH11231); \ Center for Advanced Mathematics in Energy Research Applications; \ Light Source Directors Data Solution Task Force Pilot Project.""" about_text = version_text + "<br><br>" + funding_text + "<br><hr>" + copyright_text about_box = QMessageBox(QMessageBox.NoIcon, about_title, about_text) about_box.setTextFormat(Qt.RichText) about_box.setWindowModality(Qt.NonModal) help.addAction("&About Xi-CAM", lambda: about_box.show()) help.addSeparator() help.addAction(QWhatsThis.createAction(help)) menubar.addMenu(help) # Initialize layout with first plugin self._currentGUIPlugin = None self.build_layout() # self._currentGUIPlugin = pluginmanager.getPluginsOfCategory("GUIPlugin")[0] self.populate_layout() # Make F key bindings fkeys = [ Qt.Key_F1, Qt.Key_F2, Qt.Key_F3, Qt.Key_F4, Qt.Key_F5, Qt.Key_F6, Qt.Key_F7, Qt.Key_F8, Qt.Key_F9, Qt.Key_F10, Qt.Key_F11, Qt.Key_F12, ] self.Fshortcuts = [QShortcut(QKeySequence(key), self) for key in fkeys] for i in range(12): self.Fshortcuts[i].activated.connect(partial(self.setStage, i)) self.readSettings() # Wireup default widgets get_default_stage()["left"].sigOpen.connect(self.open) get_default_stage()["left"].sigOpen.connect(print) get_default_stage()["left"].sigPreview.connect( get_default_stage()["lefttop"].preview)
class Console(SpyderPluginWidget): """ Console widget """ CONF_SECTION = 'internal_console' focus_changed = Signal() redirect_stdio = Signal(bool) edit_goto = Signal(str, int, str) def __init__(self, parent=None, namespace=None, commands=[], message=None, exitfunc=None, profile=False, multithreaded=False): SpyderPluginWidget.__init__(self, parent) debug_print(" ..internal console: initializing") self.dialog_manager = DialogManager() # Shell light_background = self.get_option('light_background') self.shell = InternalShell(parent, namespace, commands, message, self.get_option('max_line_count'), self.get_plugin_font(), exitfunc, profile, multithreaded, light_background=light_background) self.shell.status.connect(lambda msg: self.show_message.emit(msg, 0)) self.shell.go_to_error.connect(self.go_to_error) self.shell.focus_changed.connect(lambda: self.focus_changed.emit()) # Redirecting some signals: self.shell.redirect_stdio.connect(lambda state: self.redirect_stdio.emit(state)) # Initialize plugin self.initialize_plugin() # Find/replace widget self.find_widget = FindReplace(self) self.find_widget.set_editor(self.shell) self.find_widget.hide() self.register_widget_shortcuts(self.find_widget) # Main layout layout = QVBoxLayout() layout.addWidget(self.shell) layout.addWidget(self.find_widget) self.setLayout(layout) # Parameters self.shell.toggle_wrap_mode(self.get_option('wrap')) # Accepting drops self.setAcceptDrops(True) # Traceback MessageBox self.msgbox_traceback= None self.error_traceback = "" #------ Private API -------------------------------------------------------- def set_historylog(self, historylog): """Bind historylog instance to this console Not used anymore since v2.0""" historylog.add_history(self.shell.history_filename) self.shell.append_to_history.connect(historylog.append_to_history) def set_help(self, help_plugin): """Bind help instance to this console""" self.shell.help = help_plugin #------ SpyderPluginWidget API --------------------------------------------- def get_plugin_title(self): """Return widget title""" return _('Internal console') def get_focus_widget(self): """ Return the widget to give focus to when this plugin's dockwidget is raised on top-level """ return self.shell def update_font(self): """Update font from Preferences""" font = self.get_plugin_font() self.shell.set_font(font) def closing_plugin(self, cancelable=False): """Perform actions before parent main window is closed""" self.dialog_manager.close_all() self.shell.exit_interpreter() return True def refresh_plugin(self): pass def get_plugin_actions(self): """Return a list of actions related to plugin""" quit_action = create_action(self, _("&Quit"), icon=ima.icon('exit'), tip=_("Quit"), triggered=self.quit) self.register_shortcut(quit_action, "_", "Quit", "Ctrl+Q") run_action = create_action(self, _("&Run..."), None, ima.icon('run_small'), _("Run a Python script"), triggered=self.run_script) environ_action = create_action(self, _("Environment variables..."), icon=ima.icon('environ'), tip=_("Show and edit environment variables" " (for current session)"), triggered=self.show_env) syspath_action = create_action(self, _("Show sys.path contents..."), icon=ima.icon('syspath'), tip=_("Show (read-only) sys.path"), triggered=self.show_syspath) buffer_action = create_action(self, _("Buffer..."), None, tip=_("Set maximum line count"), triggered=self.change_max_line_count) exteditor_action = create_action(self, _("External editor path..."), None, None, _("Set external editor executable path"), triggered=self.change_exteditor) wrap_action = create_action(self, _("Wrap lines"), toggled=self.toggle_wrap_mode) wrap_action.setChecked(self.get_option('wrap')) calltips_action = create_action(self, _("Display balloon tips"), toggled=self.toggle_calltips) calltips_action.setChecked(self.get_option('calltips')) codecompletion_action = create_action(self, _("Automatic code completion"), toggled=self.toggle_codecompletion) codecompletion_action.setChecked(self.get_option('codecompletion/auto')) codecompenter_action = create_action(self, _("Enter key selects completion"), toggled=self.toggle_codecompletion_enter) codecompenter_action.setChecked(self.get_option( 'codecompletion/enter_key')) option_menu = QMenu(_('Internal console settings'), self) option_menu.setIcon(ima.icon('tooloptions')) add_actions(option_menu, (buffer_action, wrap_action, calltips_action, codecompletion_action, codecompenter_action, exteditor_action)) plugin_actions = [None, run_action, environ_action, syspath_action, option_menu, None, quit_action] # Add actions to context menu add_actions(self.shell.menu, plugin_actions) return plugin_actions def register_plugin(self): """Register plugin in Spyder's main window""" self.focus_changed.connect(self.main.plugin_focus_changed) self.main.add_dockwidget(self) # Connecting the following signal once the dockwidget has been created: self.shell.exception_occurred.connect(self.exception_occurred) def exception_occurred(self, text, is_traceback): """Exception ocurred in the internal console. Show a QMessageBox or the internal console to warn the user""" # Skip errors without traceback if not is_traceback and self.msgbox_traceback is None: return if CONF.get('main', 'show_internal_console_if_traceback', False): self.dockwidget.show() self.dockwidget.raise_() else: if self.msgbox_traceback is None: self.msgbox_traceback = QMessageBox( QMessageBox.Critical, _('Error'), _("<b>Spyder has encountered a problem.</b><br>" "Sorry for the inconvenience." "<br><br>" "You can automatically submit this error to our Github " "issues tracker.<br><br>" "<i>Note:</i> You need a Github account for that."), QMessageBox.Ok, parent=self) self.submit_btn = self.msgbox_traceback.addButton( _('Submit to Github'), QMessageBox.YesRole) self.submit_btn.pressed.connect(self.press_submit_btn) self.msgbox_traceback.setWindowModality(Qt.NonModal) self.error_traceback = "" self.msgbox_traceback.show() self.msgbox_traceback.finished.connect(self.close_msg) self.msgbox_traceback.setDetailedText(' ') # open show details (iterate over all buttons and click it) for button in self.msgbox_traceback.buttons(): if (self.msgbox_traceback.buttonRole(button) == QMessageBox.ActionRole): button.click() break self.error_traceback += text self.msgbox_traceback.setDetailedText(self.error_traceback) def close_msg(self): self.msgbox_traceback = None def press_submit_btn(self): self.main.report_issue(self.error_traceback) self.msgbox_traceback = None #------ Public API --------------------------------------------------------- @Slot() def quit(self): """Quit mainwindow""" self.main.close() @Slot() def show_env(self): """Show environment variables""" self.dialog_manager.show(EnvDialog()) @Slot() def show_syspath(self): """Show sys.path""" editor = CollectionsEditor() editor.setup(sys.path, title="sys.path", readonly=True, width=600, icon=ima.icon('syspath')) self.dialog_manager.show(editor) @Slot() def run_script(self, filename=None, silent=False, set_focus=False, args=None): """Run a Python script""" if filename is None: self.shell.interpreter.restore_stds() filename, _selfilter = getopenfilename(self, _("Run Python script"), getcwd(), _("Python scripts")+" (*.py ; *.pyw ; *.ipy)") self.shell.interpreter.redirect_stds() if filename: os.chdir( osp.dirname(filename) ) filename = osp.basename(filename) else: return debug_print(args) filename = osp.abspath(filename) rbs = remove_backslashes command = "runfile('%s', args='%s')" % (rbs(filename), rbs(args)) if set_focus: self.shell.setFocus() if self.dockwidget and not self.ismaximized: self.dockwidget.setVisible(True) self.dockwidget.raise_() self.shell.write(command+'\n') self.shell.run_command(command) def go_to_error(self, text): """Go to error if relevant""" match = get_error_match(to_text_string(text)) if match: fname, lnb = match.groups() self.edit_script(fname, int(lnb)) def edit_script(self, filename=None, goto=-1): """Edit script""" # Called from InternalShell if not hasattr(self, 'main') \ or not hasattr(self.main, 'editor'): self.shell.external_editor(filename, goto) return if filename is not None: self.edit_goto.emit(osp.abspath(filename), goto, '') def execute_lines(self, lines): """Execute lines and give focus to shell""" self.shell.execute_lines(to_text_string(lines)) self.shell.setFocus() @Slot() def change_max_line_count(self): "Change maximum line count""" mlc, valid = QInputDialog.getInt(self, _('Buffer'), _('Maximum line count'), self.get_option('max_line_count'), 0, 1000000) if valid: self.shell.setMaximumBlockCount(mlc) self.set_option('max_line_count', mlc) @Slot() def change_exteditor(self): """Change external editor path""" path, valid = QInputDialog.getText(self, _('External editor'), _('External editor executable path:'), QLineEdit.Normal, self.get_option('external_editor/path')) if valid: self.set_option('external_editor/path', to_text_string(path)) @Slot(bool) def toggle_wrap_mode(self, checked): """Toggle wrap mode""" self.shell.toggle_wrap_mode(checked) self.set_option('wrap', checked) @Slot(bool) def toggle_calltips(self, checked): """Toggle calltips""" self.shell.set_calltips(checked) self.set_option('calltips', checked) @Slot(bool) def toggle_codecompletion(self, checked): """Toggle automatic code completion""" self.shell.set_codecompletion_auto(checked) self.set_option('codecompletion/auto', checked) @Slot(bool) def toggle_codecompletion_enter(self, checked): """Toggle Enter key for code completion""" self.shell.set_codecompletion_enter(checked) self.set_option('codecompletion/enter_key', checked) #----Drag and drop def dragEnterEvent(self, event): """Reimplement Qt method Inform Qt about the types of data that the widget accepts""" source = event.mimeData() if source.hasUrls(): if mimedata2url(source): event.acceptProposedAction() else: event.ignore() elif source.hasText(): event.acceptProposedAction() def dropEvent(self, event): """Reimplement Qt method Unpack dropped data and handle it""" source = event.mimeData() if source.hasUrls(): pathlist = mimedata2url(source) self.shell.drop_pathlist(pathlist) elif source.hasText(): lines = to_text_string(source.text()) self.shell.set_cursor_position('eof') self.shell.execute_lines(lines) event.acceptProposedAction()
class Plotter(QtWidgets.QWidget): def __init__(self,xdata = None,ydata = None,params = None,morethan1 = True): #Accepts a dictionary of plotting data and label data or just arrays of plotting data #Labels should contain structure [xlabel,ylabel,title] super().__init__() self.warning = QMessageBox() self.layout = QtWidgets.QVBoxLayout() self.figure = Figure(figsize = (5,3)) self.canvas = FigureCanvas(self.figure) self.num_sub_plots = None self.compare_methods = False self.turn_on_comparison = QtWidgets.QPushButton('Compare') self.turn_on_comparison.clicked.connect(self.turn_on_compare) ''' self.tlbar = TlBar(self.canvas) self.layout.addWidget(self.tlbar) ''' self.layout.addWidget(self.canvas) self.layout.addWidget(self.turn_on_comparison) self.setLayout(self.layout) #check whether we need to create more than one subplot if morethan1: try: self.num_sub_plots = len(list(xdata.keys())) except: self.warning.setText('Selected multiple plot mode, but have not supplied valid data.\n Plot data must be supplied in a dictionary') self.warning.show() else: self.num_sub_plots = 1 #either way give the data to the plotter. self.xdata = xdata self.ydata = ydata self.params = params self.morethan1 = morethan1 def turn_on_compare(self): if not self.compare_methods: self.compare_methods = True self.turn_on_comparison.setText('Turn Off') else: self.compare_methods = False self.turn_on_comparison.setText('Compare') def load_data(self,xdata,ydata,params,morethan1 = True): self.num_sub_plots = None self.xdata = xdata self.ydata = ydata self.params = params self.morethan1 = morethan1 self.canvas.figure.clf() try: self.num_sub_plots = len(list(self.xdata.keys())) except: self.warning.setText('Selected multiple plot mode, but have not supplied valid data.\n Plot data must be supplied in a dictionary') self.warning.show() self.create_axes() def create_axes(self): if self.num_sub_plots is not None: self.axes = self.canvas.figure.subplots(self.num_sub_plots,1,sharex = True) print(type(self.axes)) def plotIAforaves(self,xdata, ydata,params,compare_xdata = None,compare_ydata = None,compare_labels = None): if xdata is not None: self.num_sub_plots = len(list(xdata.keys())) self.create_axes() for index, key in enumerate(list(ydata.keys())): print(ydata[key]) print(self.figure.axes) self.axes[index].set_xlabel(params[key][0]) self.axes[index].set_ylabel(params[key][1]) self.axes[index].set_yticks([int(np.min(ydata[key])),int(0.5*(np.max(ydata[key])-np.min(ydata[key]))),int(np.max(ydata[key]))]) self.axes[index].set_xticks(np.array([0,int(len(ydata[key])),len(ydata[key])])*30) self.axes[index].plot(xdata[key],ydata[key],c = 'k',alpha = 0.7,label = params[key][2]) if self.compare_methods and compare_xdata is not None and compare_ydata is not None: self.axes[index].plot(compare_xdata[key],compare_ydata[key], c= 'r',alpha = 0.6,label = compare_labels[key]) self.axes[index].legend() self.axes[index].figure.canvas.draw() self.canvas.figure.tight_layout() else: self.warning.setText('Data to plot is invalid!') self.warning.show() def purge_axes(self): self.canvas.figure.clf() self.xdata = None self.ydata = None self.params = None self.morethan1 = None