def data(self, index, role=Qt.DisplayRole): """Qt Override.""" row = index.row() if not index.isValid() or not (0 <= row < len(self.shortcuts)): return to_qvariant() shortcut = self.shortcuts[row] key = shortcut.key column = index.column() if role == Qt.DisplayRole: if column == CONTEXT: return to_qvariant(shortcut.context) elif column == NAME: color = self.text_color if self._parent == QApplication.focusWidget(): if self.current_index().row() == row: color = self.text_color_highlight else: color = self.text_color text = self.rich_text[row] text = '<p style="color:{0}">{1}</p>'.format(color, text) return to_qvariant(text) elif column == SEQUENCE: text = QKeySequence(key).toString(QKeySequence.NativeText) return to_qvariant(text) elif column == SEARCH_SCORE: # Treating search scores as a table column simplifies the # sorting once a score for a specific string in the finder # has been defined. This column however should always remain # hidden. return to_qvariant(self.scores[row]) elif role == Qt.TextAlignmentRole: return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) return to_qvariant()
def wheelEvent(self, event): """Reimplemented to emit zoom in/out signals when Ctrl is pressed""" # This feature is disabled on MacOS, see spyder-ide/spyder#1510. if sys.platform != 'darwin': if event.modifiers() & Qt.ControlModifier: if hasattr(event, 'angleDelta'): if event.angleDelta().y() < 0: self.zoom_out.emit() elif event.angleDelta().y() > 0: self.zoom_in.emit() elif hasattr(event, 'delta'): if event.delta() < 0: self.zoom_out.emit() elif event.delta() > 0: self.zoom_in.emit() return QPlainTextEdit.wheelEvent(self, event) # Needed to prevent stealing focus when scrolling. # If the current widget with focus is the CompletionWidget, it means # it's being displayed in the editor, so we need to hide it and give # focus back to the editor. If not, we need to leave the focus in # the widget that currently has it. # See spyder-ide/spyder#11502 current_widget = QApplication.focusWidget() if isinstance(current_widget, CompletionWidget): self.hide_completion_widget(focus_to_parent=True) else: self.hide_completion_widget(focus_to_parent=False)
def data(self, index, role=Qt.DisplayRole): """Qt Override.""" row = index.row() if not index.isValid() or not (0 <= row < len(self.shortcuts)): return to_qvariant() shortcut = self.shortcuts[row] key = shortcut.key column = index.column() if role == Qt.DisplayRole: if column == CONTEXT: return to_qvariant(shortcut.context) elif column == NAME: color = self.text_color if self._parent == QApplication.focusWidget(): if self.current_index().row() == row: color = self.text_color_highlight else: color = self.text_color text = self.rich_text[row] text = '<p style="color:{0}">{1}</p>'.format(color, text) return to_qvariant(text) elif column == SEQUENCE: text = QKeySequence(key).toString(QKeySequence.NativeText) return to_qvariant(text) elif column == SEARCH_SCORE: # Treating search scores as a table column simplifies the # sorting once a score for a specific string in the finder # has been defined. This column however should always remain # hidden. return to_qvariant(self.scores[row]) elif role == Qt.TextAlignmentRole: return to_qvariant(int(Qt.AlignHCenter | Qt.AlignVCenter)) return to_qvariant()
def test_change_types_in_varexp(main_window, qtbot): """Test that variable types can't be changed in the Variable Explorer.""" # Create object shell = main_window.ipyconsole.get_current_shellwidget() qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT) with qtbot.waitSignal(shell.executed): shell.execute('a = 10') # Edit object main_window.variableexplorer.visibility_changed(True) nsb = main_window.variableexplorer.get_focus_widget() qtbot.waitUntil(lambda: nsb.editor.model.rowCount() > 0, timeout=EVAL_TIMEOUT) nsb.editor.setFocus() nsb.editor.edit_item() # Try to change types qtbot.keyClicks(QApplication.focusWidget(), "'s'") qtbot.keyClick(QApplication.focusWidget(), Qt.Key_Enter) qtbot.wait(1000) # Assert object remains the same assert shell.get_value('a') == 10
def findinfiles_callback(self): """Find in files callback""" widget = QApplication.focusWidget() text = '' try: if widget.has_selected_text(): text = widget.get_selected_text() except AttributeError: # This is not a text widget deriving from TextEditBaseWidget pass self.findinfiles.set_search_text(text) if text: self.findinfiles.find()
def close_window(self): """Close active dialog or active window.""" parent = QApplication.focusWidget() while parent is not None: if isinstance(parent, QMainWindow): self.close() break if isinstance(parent, QDialog): parent.close() break parent = parent.parent()
def test_change_types_in_varexp(main_window, qtbot): """Test that variable types can't be changed in the Variable Explorer.""" # Create object shell = main_window.ipyconsole.get_current_shellwidget() qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT) with qtbot.waitSignal(shell.executed): shell.execute('a = 10') # Edit object main_window.variableexplorer.visibility_changed(True) nsb = main_window.variableexplorer.get_focus_widget() qtbot.waitUntil(lambda: nsb.editor.model.rowCount() > 0, timeout=EVAL_TIMEOUT) nsb.editor.setFocus() nsb.editor.edit_item() # Try to change types qtbot.keyClicks(QApplication.focusWidget(), "'s'") qtbot.keyClick(QApplication.focusWidget(), Qt.Key_Enter) qtbot.wait(1000) # Assert object remains the same assert shell.get_value('a') == 10
def close_current_dockwidget(self): """Search for the currently focused plugin and close it.""" widget = QApplication.focusWidget() for plugin in self.get_dockable_plugins(): # TODO: remove old API try: # New API if plugin.get_widget().isAncestorOf(widget): plugin.toggle_view_action.setChecked(False) break except AttributeError: # Old API if plugin.isAncestorOf(widget): plugin._toggle_view_action.setChecked(False) break
def view_set_parameter(self, param_name, value): view = self.widget.view() rect = view.getVisualRectParameterProperty(param_name) pos = rect.center() if self.is_multi: pos -= QPoint(rect.width() / 5, 0) else: pos += QPoint(rect.width() / 4, 0) tree = view.treeWidget().viewport() QTest.mouseMove(tree, pos) yield QTest.mouseClick(tree, Qt.LeftButton, Qt.NoModifier, pos) yield editor = QApplication.focusWidget() QTest.keyClicks(editor, str(value)) QTest.keyClick(editor, Qt.Key_Return)
def findinfiles_callback(self): """Find in files callback""" widget = QApplication.focusWidget() if not self.ismaximized: self.dockwidget.setVisible(True) self.dockwidget.raise_() text = '' try: if widget.has_selected_text(): text = widget.get_selected_text() except AttributeError: # This is not a text widget deriving from TextEditBaseWidget pass self.findinfiles.set_search_text(text) if text: self.findinfiles.find()
def view_set_parameter(self, param_name, value): view = self.widget.view() rect = view.getVisualRectParameterProperty(param_name) pos = rect.center() if self.is_multi: pos -= QPoint(rect.width() / 5, 0) else: pos += QPoint(rect.width() / 4, 0) tree = view.treeWidget().viewport() QTest.mouseMove(tree, pos) yield QTest.mouseClick(tree, Qt.LeftButton, Qt.NoModifier, pos) yield editor = QApplication.focusWidget() QTest.keyClicks(editor, str(value)) QTest.keyClick(editor, Qt.Key_Return)
def findinfiles_callback(self): """Find in files callback""" widget = QApplication.focusWidget() if not self.ismaximized: self.dockwidget.setVisible(True) self.dockwidget.raise_() text = '' try: if widget.has_selected_text(): text = widget.get_selected_text() except AttributeError: # This is not a text widget deriving from TextEditBaseWidget pass self.set_search_text(text) if text: self.find()
def find(self): """ Search text in multiple files. Notes ----- Find in files using the currently selected text of the focused widget. """ focus_widget = QApplication.focusWidget() text = '' try: if focus_widget.has_selected_text(): text = focus_widget.get_selected_text() except AttributeError: # This is not a text widget deriving from TextEditBaseWidget pass self.switch_to_plugin() widget = self.get_widget() if text: widget.set_search_text(text) widget.find()
def wheelEvent(self, event): """Reimplemented to emit zoom in/out signals when Ctrl is pressed""" # This feature is disabled on MacOS, see spyder-ide/spyder#1510. if sys.platform != 'darwin': if event.modifiers() & Qt.ControlModifier: if hasattr(event, 'angleDelta'): if event.angleDelta().y() < 0: self.zoom_out.emit() elif event.angleDelta().y() > 0: self.zoom_in.emit() elif hasattr(event, 'delta'): if event.delta() < 0: self.zoom_out.emit() elif event.delta() > 0: self.zoom_in.emit() return QPlainTextEdit.wheelEvent(self, event) # Needed to prevent stealing focus to the find widget when scrolling # See spyder-ide/spyder#11502 current_widget = QApplication.focusWidget() self.hide_completion_widget() self.highlight_current_cell() if current_widget is not None: current_widget.setFocus()
def run_opensbml(self, filenames=None, goto=None, word='', editorwindow=None, processevents=True): """Prompt the user to load a SBML file, translate to antimony, and display in a new window""" editorwindow = None #Used in editor.load processevents = True #Used in editor.load editor = self.main.editor basedir = getcwd() if CONF.get('workingdir', 'editor/open/browse_scriptdir'): c_fname = editor.get_current_filename() if c_fname is not None and c_fname != editor.TEMPFILE_PATH: basedir = os.path.dirname(c_fname) editor.redirect_stdio.emit(False) parent_widget = editor.get_current_editorstack() selectedfilter = '' filters = 'SBML files (*.sbml *.xml);;All files (*.*)' filenames, _selfilter = getopenfilenames(parent_widget, _("Open SBML file"), basedir, filters, selectedfilter=selectedfilter) editor.redirect_stdio.emit(True) if filenames: filenames = [os.path.normpath(fname) for fname in filenames] if CONF.get('workingdir', 'editor/open/auto_set_to_basedir'): directory = os.path.dirname(filenames[0]) editor.emit(Signal("open_dir(QString)"), directory) else: #The file dialog box was closed without selecting a file. return focus_widget = QApplication.focusWidget() if editor.dockwidget and not editor.ismaximized and\ (not editor.dockwidget.isAncestorOf(focus_widget)\ and not isinstance(focus_widget, CodeEditor)): editor.dockwidget.setVisible(True) editor.dockwidget.setFocus() editor.dockwidget.raise_() def _convert(fname): fname = os.path.abspath(encoding.to_unicode_from_fs(fname)) if os.name == 'nt' and len(fname) >= 2 and fname[1] == ':': fname = fname[0].upper() + fname[1:] return fname if hasattr(filenames, 'replaceInStrings'): # This is a QStringList instance (PyQt API #1), converting to list: filenames = list(filenames) if not isinstance(filenames, list): filenames = [_convert(filenames)] else: filenames = [_convert(fname) for fname in list(filenames)] for index, filename in enumerate(filenames): p = re.compile('(.xml$|.sbml$)') pythonfile = p.sub('_antimony.py', filename) if (pythonfile == filename): pythonfile = filename + "_antimony.py" current_editor = editor.set_current_filename( pythonfile, editorwindow) if current_editor is not None: # -- TODO: Do not open an already opened file pass else: # -- Not an existing opened file: if not os.path.isfile(filename): continue # -- current_es = editor.get_current_editorstack(editorwindow) # Creating the editor widget in the first editorstack (the one # that can't be destroyed), then cloning this editor widget in # all other editorstacks: finfo, newname = self.load_and_translate( filename, pythonfile, editor) finfo.path = editor.main.get_spyder_pythonpath() editor._clone_file_everywhere(finfo) current_editor = current_es.set_current_filename(newname) #if (current_editor is not None): # editor.register_widget_shortcuts("Editor", current_editor) current_es.analyze_script() if (current_editor is not None): current_editor.clearFocus() current_editor.setFocus() current_editor.window().raise_() if processevents: QApplication.processEvents()
def run_Import(self, action): """Prompt user to load a COMBINE archive or SED-ML file and translates it""" if action == 'c2p' or action == 'c2pwp' or action == 's2p' or action == 's2pwp': editorwindow = None #Used in editor.load processevents = True #Used in editor.load goto = None word = '' editor = self.main.editor editor0 = editor.get_current_editor() if editor0 is not None: position0 = editor0.get_position('cursor') filename0 = editor.get_current_filename() else: position0, filename0 = None, None # Recent files action raction = editor.sender() if isinstance(raction, QAction): filenames = from_qvariant(raction.data(), to_text_string) if not filenames: basedir = getcwd() if editor.edit_filetypes is None: editor.edit_filetypes = get_edit_filetypes() if editor.edit_filters is None: editor.edit_filters = get_edit_filters() c_fname = editor.get_current_filename() if c_fname is not None and c_fname != editor.TEMPFILE_PATH: basedir = os.path.dirname(c_fname) editor.redirect_stdio.emit(False) parent_widget = editor.get_current_editorstack() if filename0 is not None: selectedfilter = get_filter(editor.edit_filetypes, os.path.splitext(filename0)[1]) else: selectedfilter = '' if action == 'c2p' or action == 'c2pwp': filters = 'Combine archives (*.zip *.omex);;All files (*.*)' filenames, _selfilter = getopenfilenames( parent_widget, _("Open combine archive"), basedir, filters, selectedfilter=selectedfilter) else: filters = 'SED-ML files (*.sedml *.xml);;All files (*.*)' filenames, _selfilter = getopenfilenames( parent_widget, _("Open SED-ML file"), basedir, filters, selectedfilter=selectedfilter) editor.redirect_stdio.emit(True) if filenames: filenames = [ os.path.normpath(fname) for fname in filenames ] else: return focus_widget = QApplication.focusWidget() if editor.dockwidget and\ (not editor.dockwidget.isAncestorOf(focus_widget)\ and not isinstance(focus_widget, CodeEditor)): editor.dockwidget.setVisible(True) editor.dockwidget.setFocus() editor.dockwidget.raise_() def _convert(fname): fname = os.path.abspath(encoding.to_unicode_from_fs(fname)) if os.name == 'nt' and len(fname) >= 2 and fname[1] == ':': fname = fname[0].upper() + fname[1:] return fname if hasattr(filenames, 'replaceInStrings'): # This is a QStringList instance (PyQt API #1), converting to list: filenames = list(filenames) if not isinstance(filenames, list): filenames = [_convert(filenames)] else: filenames = [_convert(fname) for fname in list(filenames)] if isinstance(goto, int): goto = [goto] elif goto is not None and len(goto) != len(filenames): goto = None for index, filename in enumerate(filenames): if action == 'c2p' or action == 'c2pwp': p = re.compile('(.zip$|.omex$)') pythonfile = p.sub('.py', filename) if (pythonfile == filename): pythonfile = filename + ".py" else: p = re.compile('(.xml$|.sedml$)') if action == 's2p': pythonfile = p.sub('_sedml.py', filename) if (pythonfile == filename): pythonfile = filename + "_sedml.py" else: pythonfile = p.sub('_phrasedml.py', filename) if (pythonfile == filename): pythonfile = filename + "_phrasedml.py" current_editor = editor.set_current_filename( pythonfile, editorwindow) if current_editor is None: # -- Not a valid filename: if not os.path.isfile(filename): continue # -- current_es = editor.get_current_editorstack(editorwindow) # Creating the editor widget in the first editorstack (the one # that can't be destroyed), then cloning this editor widget in # all other editorstacks: finfo, newname = self.load_and_translate( filename, pythonfile, editor, action) finfo.path = editor.main.get_spyder_pythonpath() editor._clone_file_everywhere(finfo) current_editor = current_es.set_current_filename(newname) current_es.analyze_script() if goto is not None: # 'word' is assumed to be None as well current_editor.go_to_line(goto[index], word=word) position = current_editor.get_position('cursor') editor.cursor_moved(filename0, position0, filename, position) if (current_editor is not None): current_editor.clearFocus() current_editor.setFocus() current_editor.window().raise_() if processevents: QApplication.processEvents()
def get_focus_client(self): """Return current notebook with focus, if any.""" widget = QApplication.focusWidget() for client in self.get_clients(): if widget is client or widget is client.notebookwidget: return client
def maximize_dockwidget(self, restore=False): """ Maximize current dockwidget. Shortcut: Ctrl+Alt+Shift+M First call: maximize current dockwidget Second call (or restore=True): restore original window layout """ if self._state_before_maximizing is None: if restore: return # Select plugin to maximize self._state_before_maximizing = self.main.saveState( version=WINDOW_STATE_VERSION ) focus_widget = QApplication.focusWidget() for plugin in self.get_dockable_plugins(): plugin.dockwidget.hide() try: # New API if plugin.get_widget().isAncestorOf(focus_widget): self._last_plugin = plugin except Exception: # Old API if plugin.isAncestorOf(focus_widget): self._last_plugin = plugin # Only plugins that have a dockwidget are part of widgetlist, # so last_plugin can be None after the above "for" cycle. # For example, this happens if, after Spyder has started, focus # is set to the Working directory toolbar (which doesn't have # a dockwidget) and then you press the Maximize button if self._last_plugin is None: # Using the Editor as default plugin to maximize self._last_plugin = self.get_plugin(Plugins.Editor) # Maximize last_plugin self._last_plugin.dockwidget.toggleViewAction().setDisabled(True) try: # New API self.main.setCentralWidget(self._last_plugin.get_widget()) except AttributeError: # Old API self.main.setCentralWidget(self._last_plugin) self._last_plugin._ismaximized = True # Workaround to solve an issue with editor's outline explorer: # (otherwise the whole plugin is hidden and so is the outline # explorer and the latter won't be refreshed if not visible) try: # New API self._last_plugin.get_widget().show() self._last_plugin.change_visibility(True) except AttributeError: # Old API self._last_plugin.show() self._last_plugin._visibility_changed(True) if self._last_plugin is self.main.editor: # Automatically show the outline if the editor was maximized: outline_explorer = self.get_plugin(Plugins.OutlineExplorer) self.main.addDockWidget( Qt.RightDockWidgetArea, outline_explorer.dockwidget) outline_explorer.dockwidget.show() else: # Restore original layout (before maximizing current dockwidget) try: # New API self._last_plugin.dockwidget.setWidget( self._last_plugin.get_widget()) except AttributeError: # Old API self._last_plugin.dockwidget.setWidget(self._last_plugin) self._last_plugin.dockwidget.toggleViewAction().setEnabled(True) self.main.setCentralWidget(None) try: # New API self._last_plugin.get_widget().is_maximized = False except AttributeError: # Old API self._last_plugin._ismaximized = False self.main.restoreState( self._state_before_maximizing, version=WINDOW_STATE_VERSION ) self._state_before_maximizing = None try: # New API self._last_plugin.get_widget().get_focus_widget().setFocus() except AttributeError: # Old API self._last_plugin.get_focus_widget().setFocus()
def mousePressEvent(self, event): focused_widget = QApplication.focusWidget() if isinstance(focused_widget, QSpinBox): focused_widget.clearFocus() super(XicamMainWindow, self).mousePressEvent(event)
def run_opensbml(self, filenames=None, goto=None, word='', editorwindow=None, processevents=True): """Prompt the user to load a SBML file, translate to antimony, and display in a new window""" editor = self.main.editor editor0 = editor.get_current_editor() if editor0 is not None: position0 = editor0.get_position('cursor') filename0 = editor.get_current_filename() else: position0, filename0 = None, None if not filenames: # Recent files action action = editor.sender() if isinstance(action, QAction): filenames = from_qvariant(action.data(), to_text_string) if not filenames: basedir = getcwd() if editor.edit_filetypes is None: editor.edit_filetypes = get_edit_filetypes() if editor.edit_filters is None: editor.edit_filters = get_edit_filters() c_fname = editor.get_current_filename() if c_fname is not None and c_fname != editor.TEMPFILE_PATH: basedir = os.path.dirname(c_fname) editor.redirect_stdio.emit(False) parent_widget = editor.get_current_editorstack() if filename0 is not None: selectedfilter = get_filter(editor.edit_filetypes, os.path.splitext(filename0)[1]) else: selectedfilter = '' if not running_under_pytest(): customfilters = 'SBML files (*.sbml *.xml);;All files (*.*)' filenames, _sf = getopenfilenames( parent_widget, _("Open SBML file"), basedir, customfilters, selectedfilter=selectedfilter, options=QFileDialog.HideNameFilterDetails) else: # Use a Qt (i.e. scriptable) dialog for pytest dialog = QFileDialog(parent_widget, _("Open SBML file"), options=QFileDialog.DontUseNativeDialog) if dialog.exec_(): filenames = dialog.selectedFiles() editor.redirect_stdio.emit(True) if filenames: filenames = [os.path.normpath(fname) for fname in filenames] else: return focus_widget = QApplication.focusWidget() if editor.dockwidget and\ (not editor.dockwidget.isAncestorOf(focus_widget)\ and not isinstance(focus_widget, CodeEditor)): editor.dockwidget.setVisible(True) editor.dockwidget.setFocus() editor.dockwidget.raise_() def _convert(fname): fname = os.path.abspath(encoding.to_unicode_from_fs(fname)) if os.name == 'nt' and len(fname) >= 2 and fname[1] == ':': fname = fname[0].upper() + fname[1:] return fname if hasattr(filenames, 'replaceInStrings'): # This is a QStringList instance (PyQt API #1), converting to list: filenames = list(filenames) if not isinstance(filenames, list): filenames = [_convert(filenames)] else: filenames = [_convert(fname) for fname in list(filenames)] if isinstance(goto, int): goto = [goto] elif goto is not None and len(goto) != len(filenames): goto = None for index, filename in enumerate(filenames): p = re.compile('(.xml$|.sbml$)') pythonfile = p.sub('_antimony.py', filename) if (pythonfile == filename): pythonfile = filename + "_antimony.py" # -- Do not open an already opened file current_editor = editor.set_current_filename( pythonfile, editorwindow) if current_editor is None: # -- Not a valid filename: if not os.path.isfile(filename): continue # -- current_es = editor.get_current_editorstack(editorwindow) # Creating the editor widget in the first editorstack (the one # that can't be destroyed), then cloning this editor widget in # all other editorstacks: finfo, newname = self.load_and_translate( filename, pythonfile, editor) finfo.path = editor.main.get_spyder_pythonpath() editor._clone_file_everywhere(finfo) current_editor = current_es.set_current_filename(newname) current_es.analyze_script() if goto is not None: # 'word' is assumed to be None as well current_editor.go_to_line(goto[index], word=word) position = current_editor.get_position('cursor') editor.cursor_moved(filename0, position0, filename, position) if (current_editor is not None): current_editor.clearFocus() current_editor.setFocus() current_editor.window().raise_() if processevents: QApplication.processEvents()
def get_focus_term(self): """Return current terminal with focus, if any.""" widget = QApplication.focusWidget() for term in self.get_terms(): if widget is term: return term