Skip to content

Commit

Permalink
Merge from 5.x: PR #16091
Browse files Browse the repository at this point in the history
Fixes #15698
Fixes #15320
  • Loading branch information
ccordoba12 committed Jul 28, 2021
2 parents c0a3697 + 12eebb3 commit df5acb3
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 72 deletions.
2 changes: 1 addition & 1 deletion spyder/plugins/editor/widgets/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import unicodedata

# Third party imports
import qstylizer
import qstylizer.style
from qtpy.compat import getsavefilename
from qtpy.QtCore import (QByteArray, QFileInfo, QPoint, QSize, Qt, QTimer,
Signal, Slot)
Expand Down
128 changes: 77 additions & 51 deletions spyder/plugins/variableexplorer/widgets/arrayeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,25 +599,8 @@ def __init__(self, parent, data, readonly=False,
ylabels=ylabels, readonly=readonly, parent=self)
self.view = ArrayView(self, self.model, data.dtype, data.shape)

btn_layout = QHBoxLayout()
btn_layout.setAlignment(Qt.AlignLeft)
btn = QPushButton(_( "Format"))
# disable format button for int type
btn.setEnabled(is_float(data.dtype))
btn_layout.addWidget(btn)
btn.clicked.connect(self.change_format)
btn = QPushButton(_( "Resize"))
btn_layout.addWidget(btn)
btn.clicked.connect(self.view.resize_to_contents)
bgcolor = QCheckBox(_( 'Background color'))
bgcolor.setChecked(self.model.bgcolor_enabled)
bgcolor.setEnabled(self.model.bgcolor_enabled)
bgcolor.stateChanged.connect(self.model.bgcolor)
btn_layout.addWidget(bgcolor)

layout = QVBoxLayout()
layout.addWidget(self.view)
layout.addLayout(btn_layout)
self.setLayout(layout)

def accept_changes(self):
Expand Down Expand Up @@ -651,7 +634,7 @@ def change_format(self):
class ArrayEditor(BaseDialog):
"""Array Editor Dialog"""
def __init__(self, parent=None):
QDialog.__init__(self, parent)
super().__init__(parent)

# Destroying the C++ object right after closing the dialog box,
# otherwise it may be garbage-collected in another QThread
Expand Down Expand Up @@ -711,7 +694,6 @@ def setup_and_check(self, data, title='', readonly=False,

self.layout = QGridLayout()
self.setLayout(self.layout)
self.setWindowIcon(ima.icon('arredit'))
if title:
title = to_text_string(title) + " - " + _("NumPy object array")
else:
Expand All @@ -720,7 +702,7 @@ def setup_and_check(self, data, title='', readonly=False,
title += ' (' + _('read only') + ')'
self.setWindowTitle(title)

# Stack widget
# ---- Stack widget
self.stack = QStackedWidget(self)
if is_record_array:
for name in data.dtype.names:
Expand All @@ -735,22 +717,34 @@ def setup_and_check(self, data, title='', readonly=False,
self.stack.addWidget(ArrayEditorWidget(self, data.mask, readonly,
xlabels, ylabels))
elif data.ndim == 3:
pass
# We create here the necessary widgets for current_dim_changed to
# work. The rest are created below.
# QSpinBox
self.index_spin = QSpinBox(self, keyboardTracking=False)
self.index_spin.valueChanged.connect(self.change_active_widget)

# Labels
self.shape_label = QLabel()
self.slicing_label = QLabel()

# Set the widget to display when launched
self.current_dim_changed(self.last_dim)
else:
self.stack.addWidget(ArrayEditorWidget(self, data, readonly,
xlabels, ylabels))

self.arraywidget = self.stack.currentWidget()
if self.arraywidget:
self.arraywidget.model.dataChanged.connect(
self.save_and_close_enable)
self.arraywidget.model.dataChanged.connect(self.save_and_close_enable)
self.stack.currentChanged.connect(self.current_widget_changed)
self.layout.addWidget(self.stack, 1, 0)

# Buttons configuration
btn_layout = QHBoxLayout()
# ---- Top row of buttons
btn_layout_top = None
if is_record_array or is_masked_array or data.ndim == 3:
btn_layout_top = QHBoxLayout()

if is_record_array:
btn_layout.addWidget(QLabel(_("Record array fields:")))
btn_layout_top.addWidget(QLabel(_("Record array fields:")))
names = []
for name in data.dtype.names:
field = data.dtype.fields[name]
Expand All @@ -763,57 +757,88 @@ def setup_and_check(self, data, title='', readonly=False,
names.append(text)
else:
names = [_('Masked data'), _('Data'), _('Mask')]

if data.ndim == 3:
# QSpinBox
self.index_spin = QSpinBox(self, keyboardTracking=False)
self.index_spin.valueChanged.connect(self.change_active_widget)
# QComboBox
names = [str(i) for i in range(3)]
ra_combo = QComboBox(self)
ra_combo.addItems(names)
ra_combo.currentIndexChanged.connect(self.current_dim_changed)

# Adding the widgets to layout
label = QLabel(_("Axis:"))
btn_layout.addWidget(label)
btn_layout.addWidget(ra_combo)
self.shape_label = QLabel()
btn_layout.addWidget(self.shape_label)
btn_layout_top.addWidget(label)
btn_layout_top.addWidget(ra_combo)
btn_layout_top.addWidget(self.shape_label)

label = QLabel(_("Index:"))
btn_layout.addWidget(label)
btn_layout.addWidget(self.index_spin)
self.slicing_label = QLabel()
btn_layout.addWidget(self.slicing_label)
# set the widget to display when launched
self.current_dim_changed(self.last_dim)
btn_layout_top.addWidget(label)
btn_layout_top.addWidget(self.index_spin)

btn_layout_top.addWidget(self.slicing_label)
else:
ra_combo = QComboBox(self)
ra_combo.currentIndexChanged.connect(self.stack.setCurrentIndex)
ra_combo.addItems(names)
btn_layout.addWidget(ra_combo)
btn_layout_top.addWidget(ra_combo)

if is_masked_array:
label = QLabel(_(
"<u>Warning</u>: changes are applied separately"))
label.setToolTip(_("For performance reasons, changes applied "\
"to masked array won't be reflected in "\
label = QLabel(
_("<u>Warning</u>: Changes are applied separately")
)
label.setToolTip(_("For performance reasons, changes applied "
"to masked arrays won't be reflected in "
"array's data (and vice-versa)."))
btn_layout.addWidget(label)
btn_layout_top.addWidget(label)

btn_layout_top.addStretch()

btn_layout.addStretch()
# ---- Bottom row of buttons
btn_layout_bottom = QHBoxLayout()

btn_format = QPushButton(_("Format"))
# disable format button for int type
btn_format.setEnabled(is_float(self.arraywidget.data.dtype))
btn_layout_bottom.addWidget(btn_format)
btn_format.clicked.connect(lambda: self.arraywidget.change_format())

btn_resize = QPushButton(_("Resize"))
btn_layout_bottom.addWidget(btn_resize)
btn_resize.clicked.connect(
lambda: self.arraywidget.view.resize_to_contents())

self.bgcolor = QCheckBox(_('Background color'))
self.bgcolor.setEnabled(self.arraywidget.model.bgcolor_enabled)
self.bgcolor.setChecked(self.arraywidget.model.bgcolor_enabled)
self.bgcolor.stateChanged.connect(
lambda state: self.arraywidget.model.bgcolor(state))
btn_layout_bottom.addWidget(self.bgcolor)

btn_layout_bottom.addStretch()

if not readonly:
self.btn_save_and_close = QPushButton(_('Save and Close'))
self.btn_save_and_close.setDisabled(True)
self.btn_save_and_close.clicked.connect(self.accept)
btn_layout.addWidget(self.btn_save_and_close)
btn_layout_bottom.addWidget(self.btn_save_and_close)

self.btn_close = QPushButton(_('Close'))
self.btn_close.setAutoDefault(True)
self.btn_close.setDefault(True)
self.btn_close.clicked.connect(self.reject)
btn_layout.addWidget(self.btn_close)
self.layout.addLayout(btn_layout, 2, 0)
btn_layout_bottom.addWidget(self.btn_close)

# ---- Final layout
btn_layout_bottom.setContentsMargins(4, 4, 4, 4)
if btn_layout_top is not None:
btn_layout_top.setContentsMargins(4, 4, 4, 4)
self.layout.addLayout(btn_layout_top, 2, 0)
self.layout.addLayout(btn_layout_bottom, 3, 0)
else:
self.layout.addLayout(btn_layout_bottom, 2, 0)

self.setMinimumSize(400, 300)
# Set minimum size
self.setMinimumSize(500, 300)

# Make the dialog act as a window
self.setWindowFlags(Qt.Window)
Expand All @@ -831,6 +856,7 @@ def save_and_close_enable(self, left_top, bottom_right):
def current_widget_changed(self, index):
self.arraywidget = self.stack.widget(index)
self.arraywidget.model.dataChanged.connect(self.save_and_close_enable)
self.bgcolor.setChecked(self.arraywidget.model.bgcolor_enabled)

def change_active_widget(self, index):
"""
Expand Down
12 changes: 12 additions & 0 deletions spyder/plugins/variableexplorer/widgets/basedialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,21 @@

# Third party imports
from qtpy.QtWidgets import QDialog
import qstylizer.style


class BaseDialog(QDialog):

def __init__(self, parent=None):
QDialog.__init__(self, parent)

# Set style of all QPushButton's inside the dialog.
css = qstylizer.style.StyleSheet()
css.QPushButton.setValues(
padding='3px 15px 3px 15px',
)
self.setStyleSheet(css.toString())

def set_dynamic_width_and_height(self, screen_geometry, width_ratio=0.5,
height_ratio=0.5):
"""
Expand Down
26 changes: 14 additions & 12 deletions spyder/plugins/variableexplorer/widgets/dataframeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ def headerData(self, section, orientation, role):
"""Get the information to put in the header."""
if role == Qt.TextAlignmentRole:
if orientation == Qt.Horizontal:
return Qt.AlignCenter | Qt.AlignBottom
return Qt.AlignCenter
else:
return Qt.AlignRight | Qt.AlignVCenter
if role != Qt.DisplayRole and role != Qt.ToolTipRole:
Expand Down Expand Up @@ -841,7 +841,7 @@ def headerData(self, section, orientation, role):
"""
if role == Qt.TextAlignmentRole:
if orientation == Qt.Horizontal:
return Qt.AlignCenter | Qt.AlignBottom
return Qt.AlignCenter
else:
return Qt.AlignRight | Qt.AlignVCenter
if role != Qt.DisplayRole and role != Qt.ToolTipRole:
Expand Down Expand Up @@ -889,7 +889,8 @@ class DataFrameEditor(BaseDialog, SpyderConfigurationAccessor):
CONF_SECTION = 'variable_explorer'

def __init__(self, parent=None):
QDialog.__init__(self, parent)
super().__init__(parent)

# Destroying the C++ object right after closing the dialog box,
# otherwise it may be garbage-collected in another QThread
# (e.g. the editor's analysis thread in Spyder), thus leading to
Expand All @@ -909,9 +910,8 @@ def setup_and_check(self, data, title=''):

self.layout = QGridLayout()
self.layout.setSpacing(0)
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setContentsMargins(20, 20, 20, 0)
self.setLayout(self.layout)
self.setWindowIcon(ima.icon('arredit'))
if title:
title = to_text_string(title) + " - %s" % data.__class__.__name__
else:
Expand Down Expand Up @@ -959,14 +959,16 @@ def setup_and_check(self, data, title=''):
# Make the dialog act as a window
self.setWindowFlags(Qt.Window)
btn_layout = QHBoxLayout()
btn_layout.setSpacing(5)

btn = QPushButton(_("Format"))
btn_format = QPushButton(_("Format"))
# disable format button for int type
btn_layout.addWidget(btn)
btn.clicked.connect(self.change_format)
btn = QPushButton(_('Resize'))
btn_layout.addWidget(btn)
btn.clicked.connect(self.resize_to_contents)
btn_layout.addWidget(btn_format)
btn_format.clicked.connect(self.change_format)

btn_resize = QPushButton(_('Resize'))
btn_layout.addWidget(btn_resize)
btn_resize.clicked.connect(self.resize_to_contents)

bgcolor = QCheckBox(_('Background color'))
bgcolor.setChecked(self.dataModel.bgcolor_enabled)
Expand Down Expand Up @@ -994,7 +996,7 @@ def setup_and_check(self, data, title=''):
self.btn_close.clicked.connect(self.reject)
btn_layout.addWidget(self.btn_close)

btn_layout.setContentsMargins(4, 4, 4, 4)
btn_layout.setContentsMargins(0, 16, 0, 16)
self.layout.addLayout(btn_layout, 4, 0, 1, 2)
self.setModel(self.dataModel)
self.resizeColumnsToContents()
Expand Down
4 changes: 2 additions & 2 deletions spyder/plugins/variableexplorer/widgets/importwizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class ImportWizard(BaseDialog):
"""Text data import wizard"""
def __init__(self, parent, text,
title=None, icon=None, contents_title=None, varname=None):
QDialog.__init__(self, parent)
super().__init__(parent)

# Destroying the C++ object right after closing the dialog box,
# otherwise it may be garbage-collected in another QThread
Expand Down Expand Up @@ -600,7 +600,7 @@ def _get_table_data(self):
data = self._simplify_shape(
self.table_widget.get_data())
if self.table_widget.array_btn.isChecked():
return array(data)
return np.array(data)
elif (pd.read_csv is not FakeObject and
self.table_widget.df_btn.isChecked()):
info = self.table_widget.pd_info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(self,
:param reset: If true the persistent settings, such as column widths,
are reset.
"""
QDialog.__init__(self, parent=parent)
super().__init__(parent)
self.setAttribute(Qt.WA_DeleteOnClose)

# Options
Expand Down Expand Up @@ -290,6 +290,8 @@ def _setup_views(self):

# Save and close buttons
btn_layout = QHBoxLayout()
btn_layout.setContentsMargins(4, 8, 8, 16)
btn_layout.setSpacing(5)
btn_layout.addStretch()

if not self.readonly:
Expand All @@ -303,7 +305,7 @@ def _setup_views(self):
self.btn_close.setDefault(True)
self.btn_close.clicked.connect(self.reject)
btn_layout.addWidget(self.btn_close)
v_group_layout.addLayout(btn_layout)
layout.addLayout(btn_layout)

# Splitter parameters
self.central_splitter.setCollapsible(0, False)
Expand Down
2 changes: 1 addition & 1 deletion spyder/plugins/variableexplorer/widgets/texteditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class TextEditor(BaseDialog):
"""Array Editor Dialog"""
def __init__(self, text, title='', font=None, parent=None, readonly=False):
QDialog.__init__(self, parent)
super().__init__(parent)

# Destroying the C++ object right after closing the dialog box,
# otherwise it may be garbage-collected in another QThread
Expand Down
11 changes: 10 additions & 1 deletion spyder/utils/stylesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

# Third-party imports
import qdarkstyle
import qstylizer
from qstylizer.parser import parse as parse_stylesheet
import qstylizer.style

# Local imports
from spyder.config.gui import OLD_PYQT
Expand Down Expand Up @@ -172,6 +172,15 @@ def _customize_stylesheet(self):
backgroundColor=color
)

# Adjust padding of QPushButton's in QDialog's
css["QDialog QPushButton"].setValues(
padding='3px 15px 3px 15px',
)

css["QDialogButtonBox QPushButton:!default"].setValues(
padding='3px 0px 3px 0px',
)


APP_STYLESHEET = AppStylesheet()

Expand Down
Loading

0 comments on commit df5acb3

Please sign in to comment.