#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright © 2018 Michael J. Hayford
""" Support creation of an iPython console, with rayoptics environment
.. Created on Wed Nov 21 21:48:02 2018
.. codeauthor: Michael J. Hayford
"""
from PySide6.QtGui import QColor
from PySide6 import QtWidgets
from qtconsole.rich_jupyter_widget import RichJupyterWidget
from qtconsole.inprocess import QtInProcessKernelManager
from qtconsole import styles
from IPython.lib import guisupport
import qdarkstyle
from rayoptics.gui.appmanager import ModelInfo
from rayoptics.util import colors
default_template = '''\
QPlainTextEdit, QTextEdit {
background-color: %(bgcolor)s;
background-clip: padding;
color: %(fgcolor)s;
selection-background-color: %(select)s;
}
.inverted {
background-color: %(fgcolor)s;
color: %(bgcolor)s;
}
.error { color: red; }
.in-prompt { color: %(i_color)s; }
.in-prompt-number { font-weight: bold; }
.out-prompt { color: %(o_color)s; }
.out-prompt-number { font-weight: bold; }
'''
[docs]
def create_ipython_console(gui_parent, opt_model, title,
orig_x, orig_y, view_width, view_ht,
add_panel_fcts=None):
""" create a iPython console with a rayoptics environment """
def create_light_or_dark_callback(ipy_console):
# if not hasattr(ipy_console, 'background'):
# ipy_console.background = ipy_console.background()
def l_or_d(is_dark):
accent = colors.accent_colors(is_dark)
prompt_style = {
'i_color': accent['cyan'],
'o_color': accent['orange'],
}
if is_dark:
ipy_console.setStyleSheet(
qdarkstyle.load_stylesheet(qt_api='pyqt5'))
# color_defs = {**styles.get_colors('solarized-dark'),
# **prompt_style }
else:
ipy_console.setStyleSheet('')
# color_defs = {**styles.get_colors('solarized-light'),
# **prompt_style }
# ipy_console.style_sheet = default_template%color_defs
# ipy_console._style_sheet_changed()
return l_or_d
if opt_model:
ro_env = {
'gui_parent': gui_parent,
'app': gui_parent.app_manager,
'opm': opt_model,
'sm': opt_model['seq_model'],
'osp': opt_model['optical_spec'],
'ss': opt_model['specsheet'],
'pm': opt_model['parax_model'],
'em': opt_model['ele_model'],
'pt': opt_model['part_tree'],
'ar': opt_model['analysis_results'],
}
else:
ro_env = {
'gui_parent': gui_parent,
'app': gui_parent.app_manager,
}
ro_setup = 'from rayoptics.environment import *'
# construct the top level widget
widget = QtWidgets.QWidget()
top_layout = QtWidgets.QVBoxLayout(widget)
# set the layout on the widget
widget.setLayout(top_layout)
ipy_console = ConsoleWidget()
top_layout.addWidget(ipy_console)
button_panel_layout = QtWidgets.QHBoxLayout()
top_layout.addLayout(button_panel_layout)
cmd_button_1 = QtWidgets.QPushButton("refresh GUI")
cmd_button_1.clicked.connect(
lambda: ipy_console.execute_command('app.refresh_gui()'))
button_panel_layout.addWidget(cmd_button_1)
cmd_button_2 = QtWidgets.QPushButton("setup opm")
opt_model = opt_model if opt_model else gui_parent.app_manager.model
opm_vars = {
'opm': opt_model,
'sm': opt_model['seq_model'],
'osp': opt_model['optical_spec'],
'ss': opt_model['specsheet'],
'pm': opt_model['parax_model'],
'em': opt_model['ele_model'],
'pt': opt_model['part_tree'],
'ar': opt_model['analysis_results'],
}
cmd_button_2.clicked.connect(
lambda: ipy_console.push_vars(opm_vars))
button_panel_layout.addWidget(cmd_button_2)
cmd_button_3 = QtWidgets.QPushButton("First order data")
cmd_button_3.clicked.connect(
lambda: ipy_console.execute_command("app.model['pm'].first_order_data()"))
button_panel_layout.addWidget(cmd_button_3)
# load the environment
ipy_console.execute_command(ro_setup)
ipy_console.push_vars(ro_env)
mi = ModelInfo(opt_model)
sub_window = gui_parent.add_subwindow(widget, mi)
sub_window.setWindowTitle(title)
sub_window.sync_light_or_dark = create_light_or_dark_callback(ipy_console)
sub_window.setGeometry(orig_x, orig_y, view_width, view_ht)
sub_window.show()