Skip to content

Commit 38bcb74

Browse files
committed
Move load_file() from Editor plugin to mainwindow.py
1 parent 6b3365b commit 38bcb74

File tree

3 files changed

+103
-77
lines changed

3 files changed

+103
-77
lines changed

Diff for: spyder/api/plugins/new_api.py

+10
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,16 @@ def open_file(self, filename):
968968
"""
969969
raise NotImplementedError
970970

971+
def get_current_filename(self):
972+
"""
973+
Get the currently displayed file name.
974+
975+
This applies to plugins that can handle files in a QTabWiget, like
976+
the Editor or spyder-notebook.
977+
"""
978+
return None
979+
980+
971981
class SpyderDockablePlugin(SpyderPluginV2):
972982
"""
973983
A Spyder plugin to enhance functionality with a dockable widget.

Diff for: spyder/app/mainwindow.py

+87-3
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@
4343
#==============================================================================
4444
# Third-party imports
4545
#==============================================================================
46-
from qtpy.QtCore import (QCoreApplication, Qt, QTimer, Signal, Slot,
46+
from qtpy.compat import getopenfilenames
47+
from qtpy.QtCore import (QCoreApplication, Qt, QDir, QTimer, Signal, Slot,
4748
qInstallMessageHandler)
4849
from qtpy.QtGui import QColor, QKeySequence
4950
from qtpy.QtWidgets import (
50-
QApplication, QMainWindow, QMessageBox, QShortcut, QTabBar)
51+
QApplication, QFileDialog, QMainWindow, QMessageBox, QShortcut, QTabBar)
5152

5253
# Avoid a "Cannot mix incompatible Qt library" error on Windows platforms
5354
from qtpy import QtSvg # analysis:ignore
@@ -83,7 +84,8 @@
8384
from spyder.config.gui import is_dark_font_color
8485
from spyder.config.main import OPEN_FILES_PORT
8586
from spyder.config.manager import CONF
86-
from spyder.config.utils import IMPORT_EXT
87+
from spyder.config.utils import (get_edit_filetypes, get_edit_filters,
88+
get_filter, IMPORT_EXT)
8789
from spyder.py3compat import to_text_string
8890
from spyder.utils import encoding, programs
8991
from spyder.utils.icon_manager import ima
@@ -871,6 +873,10 @@ def post_visible_setup(self):
871873
self.get_plugin(Plugins.Application).on_mainwindow_visible()
872874
QApplication.processEvents()
873875

876+
# For load_files()
877+
self.edit_filetypes = None
878+
self.edit_filters = None
879+
874880
# Server to maintain just one Spyder instance and open files in it if
875881
# the user tries to start other instances with
876882
# $ spyder foo.py
@@ -1170,6 +1176,83 @@ def redirect_internalshell_stdio(self, state):
11701176
else:
11711177
console.restore_stds()
11721178

1179+
@Slot()
1180+
def load_files(self):
1181+
"""Load files into Spyder using an 'Open files' dialog."""
1182+
basedir = getcwd_or_home()
1183+
if self.edit_filetypes is None:
1184+
self.edit_filetypes = get_edit_filetypes()
1185+
if self.edit_filters is None:
1186+
self.edit_filters = get_edit_filters()
1187+
1188+
# Try to get the current filename from plugin that has focus
1189+
c_fname = None
1190+
for plugin in (self.widgetlist + self.thirdparty_plugins):
1191+
if plugin.get_widget().isAncestorOf(self.last_focused_widget):
1192+
c_fname = plugin.get_current_filename()
1193+
break
1194+
1195+
# If we couldn't get it from them, then use the Editor
1196+
editor = self.get_plugin(Plugins.Editor, error=False)
1197+
if c_fname is None and editor:
1198+
c_fname = editor.get_current_filename()
1199+
1200+
# Switch basedir to the current filename one
1201+
if editor and c_fname != editor.get_widget().TEMPFILE_PATH:
1202+
basedir = osp.dirname(c_fname)
1203+
1204+
# Select filter for the dialog according to c_fname extension
1205+
if c_fname is not None:
1206+
selectedfilter = get_filter(self.edit_filetypes,
1207+
osp.splitext(c_fname)[1])
1208+
else:
1209+
selectedfilter = ''
1210+
1211+
# Open file dialog
1212+
self.redirect_internalshell_stdio(False)
1213+
if not running_under_pytest():
1214+
# See: spyder-ide/spyder#3291
1215+
if sys.platform == 'darwin':
1216+
dialog = QFileDialog(
1217+
parent=self,
1218+
caption=_("Open file"),
1219+
directory=basedir,
1220+
)
1221+
dialog.setNameFilters(self.edit_filters.split(';;'))
1222+
dialog.setOption(QFileDialog.HideNameFilterDetails, True)
1223+
dialog.setFilter(QDir.AllDirs | QDir.Files | QDir.Drives
1224+
| QDir.Hidden)
1225+
dialog.setFileMode(QFileDialog.ExistingFiles)
1226+
1227+
if dialog.exec_():
1228+
filenames = dialog.selectedFiles()
1229+
else:
1230+
filenames, _sf = getopenfilenames(
1231+
self,
1232+
_("Open file"),
1233+
basedir,
1234+
self.edit_filters,
1235+
selectedfilter=selectedfilter,
1236+
options=QFileDialog.HideNameFilterDetails,
1237+
)
1238+
else:
1239+
# Use a Qt (i.e. scriptable) dialog for pytest
1240+
dialog = QFileDialog(self, _("Open file"),
1241+
options=QFileDialog.DontUseNativeDialog)
1242+
if dialog.exec_():
1243+
filenames = dialog.selectedFiles()
1244+
self.redirect_internalshell_stdio(True)
1245+
1246+
# Normalize returned filenames
1247+
if filenames:
1248+
filenames = [osp.normpath(fname) for fname in filenames]
1249+
else:
1250+
return
1251+
1252+
# Open filenames
1253+
for fname in filenames:
1254+
self.open_file(fname)
1255+
11731256
def open_file(self, fname, external=False):
11741257
"""
11751258
Open fname with the appropriate plugin.
@@ -1192,6 +1275,7 @@ def open_file(self, fname, external=False):
11921275
plugin = self.get_plugin(plugin_name)
11931276
if ext in plugin.FILE_EXTENSIONS:
11941277
plugin.open_file(fname)
1278+
return
11951279

11961280
# Open file with the editor, the variable explorer or with
11971281
# an external program

Diff for: spyder/plugins/editor/widgets/main_widget.py

+6-74
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,18 @@
2424
import uuid
2525

2626
# Third party imports
27-
from qtpy.compat import from_qvariant, getopenfilenames, to_qvariant
28-
from qtpy.QtCore import QByteArray, Qt, Signal, Slot, QDir
27+
from qtpy.compat import from_qvariant, to_qvariant
28+
from qtpy.QtCore import QByteArray, Qt, Signal, Slot
2929
from qtpy.QtGui import QTextCursor
3030
from qtpy.QtPrintSupport import QAbstractPrintDialog, QPrintDialog, QPrinter
3131
from qtpy.QtWidgets import (QAction, QActionGroup, QApplication, QDialog,
32-
QFileDialog, QInputDialog, QSplitter,
33-
QVBoxLayout, QWidget)
32+
QInputDialog, QSplitter, QVBoxLayout, QWidget)
3433

3534
# Local imports
3635
from spyder.api.config.decorators import on_conf_change
3736
from spyder.api.plugins import Plugins
3837
from spyder.api.widgets.main_widget import PluginMainWidget
39-
from spyder.config.base import _, get_conf_path, running_under_pytest
40-
from spyder.config.utils import (get_edit_filetypes, get_edit_filters,
41-
get_filter)
38+
from spyder.config.base import _, get_conf_path
4239
from spyder.plugins.editor.api.panel import Panel
4340
from spyder.py3compat import qbytearray_to_str, to_text_string
4441
from spyder.utils import encoding, programs, sourcecode
@@ -391,7 +388,7 @@ def setup(self):
391388
text=_("&Open..."),
392389
icon=self.create_icon('fileopen'),
393390
tip=_("Open file"),
394-
triggered=self.load,
391+
triggered=self._plugin.main.load_files,
395392
context=Qt.WidgetShortcut,
396393
register_shortcut=True
397394
)
@@ -1612,7 +1609,7 @@ def register_editorstack(self, editorstack):
16121609
editorstack.text_changed_at.connect(self.text_changed_at)
16131610
editorstack.current_file_changed.connect(self.current_file_changed)
16141611
editorstack.plugin_load.connect(self.load)
1615-
editorstack.plugin_load[()].connect(self.load)
1612+
editorstack.plugin_load[()].connect(self._plugin.main.load_files)
16161613
editorstack.edit_goto.connect(self.load)
16171614
editorstack.sig_save_as.connect(self.save_as)
16181615
editorstack.sig_prev_edit_pos.connect(self.go_to_last_edit_location)
@@ -2177,77 +2174,12 @@ def load(self, filenames=None, goto=None, word='',
21772174
except (AttributeError, RuntimeError):
21782175
pass
21792176

2180-
editor0 = self.get_current_editor()
2181-
if editor0 is not None:
2182-
filename0 = self.get_current_filename()
2183-
else:
2184-
filename0 = None
2185-
21862177
if not filenames:
21872178
# Recent files action
21882179
action = self.sender()
21892180
if isinstance(action, QAction):
21902181
filenames = from_qvariant(action.data(), to_text_string)
21912182

2192-
if not filenames:
2193-
basedir = getcwd_or_home()
2194-
if self.edit_filetypes is None:
2195-
self.edit_filetypes = get_edit_filetypes()
2196-
if self.edit_filters is None:
2197-
self.edit_filters = get_edit_filters()
2198-
2199-
c_fname = self.get_current_filename()
2200-
if c_fname is not None and c_fname != self.TEMPFILE_PATH:
2201-
basedir = osp.dirname(c_fname)
2202-
2203-
self.sig_redirect_stdio_requested.emit(False)
2204-
parent_widget = self.get_current_editorstack()
2205-
if filename0 is not None:
2206-
selectedfilter = get_filter(self.edit_filetypes,
2207-
osp.splitext(filename0)[1])
2208-
else:
2209-
selectedfilter = ''
2210-
2211-
if not running_under_pytest():
2212-
# See: spyder-ide/spyder#3291
2213-
if sys.platform == 'darwin':
2214-
dialog = QFileDialog(
2215-
parent=parent_widget,
2216-
caption=_("Open file"),
2217-
directory=basedir,
2218-
)
2219-
dialog.setNameFilters(self.edit_filters.split(';;'))
2220-
dialog.setOption(QFileDialog.HideNameFilterDetails, True)
2221-
dialog.setFilter(QDir.AllDirs | QDir.Files | QDir.Drives
2222-
| QDir.Hidden)
2223-
dialog.setFileMode(QFileDialog.ExistingFiles)
2224-
2225-
if dialog.exec_():
2226-
filenames = dialog.selectedFiles()
2227-
else:
2228-
filenames, _sf = getopenfilenames(
2229-
parent_widget,
2230-
_("Open file"),
2231-
basedir,
2232-
self.edit_filters,
2233-
selectedfilter=selectedfilter,
2234-
options=QFileDialog.HideNameFilterDetails,
2235-
)
2236-
else:
2237-
# Use a Qt (i.e. scriptable) dialog for pytest
2238-
dialog = QFileDialog(parent_widget, _("Open file"),
2239-
options=QFileDialog.DontUseNativeDialog)
2240-
if dialog.exec_():
2241-
filenames = dialog.selectedFiles()
2242-
2243-
self.sig_redirect_stdio_requested.emit(True)
2244-
2245-
if filenames:
2246-
filenames = [osp.normpath(fname) for fname in filenames]
2247-
else:
2248-
self.__ignore_cursor_history = cursor_history_state
2249-
return
2250-
22512183
focus_widget = QApplication.focusWidget()
22522184
if self.editorwindows and not self.dockwidget.isVisible():
22532185
# We override the editorwindow variable to force a focus on

0 commit comments

Comments
 (0)