Skip to content

Commit

Permalink
Merge from 5.x: PR #16041
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed Jul 19, 2021
2 parents 8641c44 + 08f5f1c commit 16aaf90
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 118 deletions.
125 changes: 12 additions & 113 deletions spyder/app/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from qtpy.compat import from_qvariant
from qtpy.QtCore import (QCoreApplication, Qt, QTimer, Signal, Slot,
qInstallMessageHandler)
from qtpy.QtGui import QColor, QIcon, QKeySequence
from qtpy.QtGui import QColor, QKeySequence
from qtpy.QtWidgets import (QAction, QApplication, QMainWindow, QMenu,
QMessageBox, QShortcut, QStyleFactory, QCheckBox)

Expand All @@ -69,15 +69,15 @@
#==============================================================================
from spyder import __version__
from spyder import dependencies
from spyder.app.utils import (create_splash_screen, delete_lsp_log_files,
qt_message_handler, set_links_color,
setup_logging, set_opengl_implementation, Spy)
from spyder.app.utils import (
create_application, create_splash_screen, create_window,
delete_lsp_log_files, qt_message_handler, set_links_color, setup_logging,
set_opengl_implementation)
from spyder.api.plugin_registration.registry import PLUGIN_REGISTRY
from spyder.config.base import (_, DEV, get_conf_path, get_debug_level,
get_home_dir, get_module_source_path,
get_safe_mode, is_pynsist, running_in_mac_app,
is_pynsist, running_in_mac_app,
running_under_pytest, STDERR)
from spyder.utils.image_path_manager import get_image_path
from spyder.config.gui import is_dark_font_color
from spyder.config.main import OPEN_FILES_PORT
from spyder.config.manager import CONF
Expand Down Expand Up @@ -960,10 +960,8 @@ def setup(self):
# Menus
# TODO: Remove when all menus are migrated to use the Main Menu Plugin
logger.info("Creating Menus...")
from spyder.api.widgets.menus import SpyderMenu
from spyder.plugins.mainmenu.api import (
ApplicationMenus, HelpMenuSections, ToolsMenuSections,
FileMenuSections)
ApplicationMenus, ToolsMenuSections, FileMenuSections)
mainmenu = self.mainmenu
self.edit_menu = mainmenu.get_application_menu("edit_menu")
self.search_menu = mainmenu.get_application_menu("search_menu")
Expand Down Expand Up @@ -2008,107 +2006,6 @@ def _test_setting_opengl(self, option):
return QCoreApplication.testAttribute(Qt.AA_UseOpenGLES)


#==============================================================================
# Utilities for the 'main' function below
#==============================================================================
def create_application():
"""Create application and patch sys.exit."""
# Our QApplication
app = qapplication()

# --- Set application icon
app_icon = QIcon(get_image_path("spyder"))
app.setWindowIcon(app_icon)

# Required for correct icon on GNOME/Wayland:
if hasattr(app, 'setDesktopFileName'):
app.setDesktopFileName('spyder')

#----Monkey patching QApplication
class FakeQApplication(QApplication):
"""Spyder's fake QApplication"""
def __init__(self, args):
self = app # analysis:ignore
@staticmethod
def exec_():
"""Do nothing because the Qt mainloop is already running"""
pass
from qtpy import QtWidgets
QtWidgets.QApplication = FakeQApplication

# ----Monkey patching sys.exit
def fake_sys_exit(arg=[]):
pass
sys.exit = fake_sys_exit

# ----Monkey patching sys.excepthook to avoid crashes in PyQt 5.5+
def spy_excepthook(type_, value, tback):
sys.__excepthook__(type_, value, tback)
sys.excepthook = spy_excepthook

# Removing arguments from sys.argv as in standard Python interpreter
sys.argv = ['']

return app


def create_window(app, splash, options, args):
"""
Create and show Spyder's main window and start QApplication event loop.
"""
# Main window
main = MainWindow(splash, options)
try:
main.setup()
except BaseException:
if main.console is not None:
try:
main.console.exit_interpreter()
except BaseException:
pass
raise

main.pre_visible_setup()
main.show()
main.post_visible_setup()

if main.console:
namespace = CONF.get('internal_console', 'namespace', {})
main.console.start_interpreter(namespace)
main.console.set_namespace_item('spy', Spy(app=app, window=main))

# Propagate current configurations to all configuration observers
CONF.notify_all_observers()

# Don't show icons in menus for Mac
if sys.platform == 'darwin':
QCoreApplication.setAttribute(Qt.AA_DontShowIconsInMenus, True)

# Open external files with our Mac app
if running_in_mac_app():
app.sig_open_external_file.connect(main.open_external_file)
app._has_started = True
if hasattr(app, '_pending_file_open'):
if args:
args = app._pending_file_open + args
else:
args = app._pending_file_open


# Open external files passed as args
if args:
for a in args:
main.open_external_file(a)

# To give focus again to the last focused widget after restoring
# the window
app.focusChanged.connect(main.change_last_focused_widget)

if not running_under_pytest():
app.exec_()
return main


#==============================================================================
# Main
#==============================================================================
Expand All @@ -2121,7 +2018,7 @@ def main(options, args):
set_opengl_implementation(option)

app = create_application()
window = create_window(app, None, options, None)
window = create_window(MainWindow, app, None, options, None)
return window

# **** Handle hide_console option ****
Expand Down Expand Up @@ -2207,9 +2104,11 @@ def main(options, args):
import faulthandler
with open(faulthandler_file, 'w') as f:
faulthandler.enable(file=f)
mainwindow = create_window(app, splash, options, args)
mainwindow = create_window(
MainWindow, app, splash, options, args
)
else:
mainwindow = create_window(app, splash, options, args)
mainwindow = create_window(MainWindow, app, splash, options, args)
except FontError:
QMessageBox.information(None, "Spyder",
"Spyder was unable to load the <i>Spyder 3</i> "
Expand Down
125 changes: 120 additions & 5 deletions spyder/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
# Third-party imports
import psutil
from qtpy.QtCore import QCoreApplication, Qt
from qtpy.QtGui import QColor, QPalette, QPixmap
from qtpy.QtWidgets import QSplashScreen
from qtpy.QtGui import QColor, QIcon, QPalette, QPixmap
from qtpy.QtWidgets import QApplication, QSplashScreen

# Local imports
from spyder.config.base import (DEV, get_conf_path, get_debug_level,
running_under_pytest)
from spyder.config.base import (
DEV, get_conf_path, get_debug_level, running_in_mac_app,
running_under_pytest)
from spyder.config.manager import CONF
from spyder.utils.image_path_manager import get_image_path
from spyder.utils.qthelpers import file_uri
from spyder.utils.qthelpers import file_uri, qapplication
from spyder.utils.external.dafsa.dafsa import DAFSA
from spyder.utils.stylesheet import QStylePalette

Expand Down Expand Up @@ -200,3 +202,116 @@ def set_links_color(app):
app_palette = app.palette()
app_palette.setColor(QPalette.Normal, QPalette.Link, qcolor)
app.setPalette(app_palette)


def create_application():
"""Create application and patch sys.exit."""
# Our QApplication
app = qapplication()

# --- Set application icon
app_icon = QIcon(get_image_path("spyder"))
app.setWindowIcon(app_icon)

# Required for correct icon on GNOME/Wayland:
if hasattr(app, 'setDesktopFileName'):
app.setDesktopFileName('spyder')

# ---- Monkey patching QApplication
class FakeQApplication(QApplication):
"""Spyder's fake QApplication"""
def __init__(self, args):
self = app # analysis:ignore

@staticmethod
def exec_():
"""Do nothing because the Qt mainloop is already running"""
pass

from qtpy import QtWidgets
QtWidgets.QApplication = FakeQApplication

# ---- Monkey patching sys.exit
def fake_sys_exit(arg=[]):
pass
sys.exit = fake_sys_exit

# ---- Monkey patching sys.excepthook to avoid crashes in PyQt 5.5+
def spy_excepthook(type_, value, tback):
sys.__excepthook__(type_, value, tback)
sys.excepthook = spy_excepthook

# Removing arguments from sys.argv as in standard Python interpreter
sys.argv = ['']

return app


def create_window(WindowClass, app, splash, options, args):
"""
Create and show Spyder's main window and start QApplication event loop.
Parameters
----------
WindowClass: QMainWindow
Subclass to instantiate the Window.
app: QApplication
Instance to start the application.
splash: QSplashScreen
Splash screen instamce.
options: argparse.Namespace
Command line options passed to Spyder
args: list
List of file names passed to the Spyder executable in the
command line.
"""
# Main window
main = WindowClass(splash, options)
try:
main.setup()
except BaseException:
if main.console is not None:
try:
main.console.exit_interpreter()
except BaseException:
pass
raise

main.pre_visible_setup()
main.show()
main.post_visible_setup()

if main.console:
namespace = CONF.get('internal_console', 'namespace', {})
main.console.start_interpreter(namespace)
main.console.set_namespace_item('spy', Spy(app=app, window=main))

# Propagate current configurations to all configuration observers
CONF.notify_all_observers()

# Don't show icons in menus for Mac
if sys.platform == 'darwin':
QCoreApplication.setAttribute(Qt.AA_DontShowIconsInMenus, True)

# Open external files with our Mac app
if running_in_mac_app():
app.sig_open_external_file.connect(main.open_external_file)
app._has_started = True
if hasattr(app, '_pending_file_open'):
if args:
args = app._pending_file_open + args
else:
args = app._pending_file_open

# Open external files passed as args
if args:
for a in args:
main.open_external_file(a)

# To give focus again to the last focused widget after restoring
# the window
app.focusChanged.connect(main.change_last_focused_widget)

if not running_under_pytest():
app.exec_()
return main

0 comments on commit 16aaf90

Please sign in to comment.