diff --git a/spyder/widgets/variableexplorer/arrayeditor.py b/spyder/widgets/variableexplorer/arrayeditor.py index a937247132b..0e9c68bca3f 100644 --- a/spyder/widgets/variableexplorer/arrayeditor.py +++ b/spyder/widgets/variableexplorer/arrayeditor.py @@ -20,7 +20,7 @@ from qtpy.compat import from_qvariant, to_qvariant from qtpy.QtCore import (QAbstractTableModel, QItemSelection, QItemSelectionRange, QModelIndex, Qt, Slot) -from qtpy.QtGui import QColor, QCursor, QDoubleValidator +from qtpy.QtGui import QColor, QCursor, QDoubleValidator, QKeySequence from qtpy.QtWidgets import (QAbstractItemDelegate, QApplication, QCheckBox, QComboBox, QDialog, QDialogButtonBox, QGridLayout, QHBoxLayout, QInputDialog, QItemDelegate, QLabel, @@ -492,7 +492,7 @@ def _sel_to_text(self, cell_range): output = io.StringIO() try: np.savetxt(output, _data[row_min:row_max+1, col_min:col_max+1], - delimiter='\t') + delimiter='\t', fmt=self.model().get_format()) except: QMessageBox.warning(self, _("Warning"), _("It was not possible to copy values for " diff --git a/spyder/widgets/variableexplorer/tests/test_arrayeditor.py b/spyder/widgets/variableexplorer/tests/test_arrayeditor.py index f0cf65dea7d..88dd3897110 100644 --- a/spyder/widgets/variableexplorer/tests/test_arrayeditor.py +++ b/spyder/widgets/variableexplorer/tests/test_arrayeditor.py @@ -14,9 +14,9 @@ import numpy as np from numpy.testing import assert_array_equal import pytest +from qtpy.QtCore import Qt # Local imports -from spyder.utils.qthelpers import qapplication from spyder.widgets.variableexplorer.arrayeditor import ArrayEditor @@ -28,9 +28,32 @@ def launch_arrayeditor(data, title="", xlabels=None, ylabels=None): dlg.accept() # trigger slot connected to OK button return dlg.get_value() +def setup_arrayeditor(qbot, data, title="", xlabels=None, ylabels=None): + """Setups an arrayeditor.""" + dlg = ArrayEditor() + dlg.setup_and_check(data, title, xlabels=xlabels, ylabels=ylabels) + dlg.show() + qbot.addWidget(dlg) + return dlg # --- Tests # ----------------------------------------------------------------------------- +def test_arrayeditor_format(qtbot): + """Changes the format of the array and validates its selected content.""" + arr = np.array([1, 2, 3], dtype=np.float32) + dlg = setup_arrayeditor(qtbot, arr, "test array float32") + qtbot.keyClick(dlg.arraywidget.view, Qt.Key_Down, modifier=Qt.ShiftModifier) + qtbot.keyClick(dlg.arraywidget.view, Qt.Key_Down, modifier=Qt.ShiftModifier) + contents = dlg.arraywidget.view._sel_to_text(dlg.arraywidget.view.selectedIndexes()) + assert contents == "1.000\n2.000\n" + dlg.arraywidget.view.model().set_format("%.18e") + assert dlg.arraywidget.view.model().get_format() == "%.18e" + qtbot.keyClick(dlg.arraywidget.view, Qt.Key_Down, modifier=Qt.ShiftModifier) + qtbot.keyClick(dlg.arraywidget.view, Qt.Key_Down, modifier=Qt.ShiftModifier) + contents = dlg.arraywidget.view._sel_to_text(dlg.arraywidget.view.selectedIndexes()) + assert contents == "1.000000000000000000e+00\n2.000000000000000000e+00\n" + + def test_arrayeditor_with_string_array(qtbot): arr = np.array(["kjrekrjkejr"]) assert arr == launch_arrayeditor(arr, "string array")