Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manually set the text format to plain text in error #1546

Merged
merged 7 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions traitsui/qt4/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"""


from pyface.qt import QtGui
from pyface.qt import QtCore, QtGui

from traits.api import HasTraits, Instance, Str, Callable

Expand Down Expand Up @@ -78,9 +78,16 @@ def error(self, excp):
else:
control = self.control

QtGui.QMessageBox.information(
control, self.description + " value error", str(excp)
message_box = QtGui.QMessageBox(
QtGui.QMessageBox.Information,
self.description + " value error",
str(excp),
buttons=QtGui.QMessageBox.Ok,
parent=control
)
message_box.setTextFormat(QtCore.Qt.PlainText)
message_box.setEscapeButton(QtGui.QMessageBox.Ok)
message_box.exec_()

def set_tooltip_text(self, control, text):
""" Sets the tooltip for a specified control.
Expand Down
65 changes: 63 additions & 2 deletions traitsui/tests/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,33 @@

import unittest

from traits.api import Any, Bool, Event, Float, HasTraits, Int, List, Undefined
from pyface.toolkit import toolkit_object
from traits.api import (
Any, Bool, Event, Float, HasTraits, Int, List, Range, Undefined
)
from traits.trait_base import xgetattr

from traitsui.context_value import ContextValue, CVFloat, CVInt
from traitsui.editor import Editor
from traitsui.editor_factory import EditorFactory
from traitsui.handler import default_handler
from traitsui.ui import UI
from traitsui.testing.api import (
KeyClick, KeySequence, Textbox, UITester
)
from traitsui.tests._tools import (
BaseTestMixin, GuiTestAssistant, no_gui_test_assistant
BaseTestMixin,
GuiTestAssistant,
is_mac_os,
no_gui_test_assistant,
requires_toolkit,
ToolkitName,
)

ModalDialogTester = toolkit_object(
"util.modal_dialog_tester:ModalDialogTester"
)
no_modal_dialog_tester = ModalDialogTester.__name__ == "Unimplemented"


class FakeControl(HasTraits):
Expand Down Expand Up @@ -917,3 +933,48 @@ def test_sync_value_both(self):

with self.assertTraitDoesNotChange(user_object, "user_auxiliary"):
editor.auxiliary_value = 14

# regression test for enthought/traitsui#1543
@requires_toolkit([ToolkitName.qt])
@unittest.skipIf(no_modal_dialog_tester, "ModalDialogTester unavailable")
@unittest.skipIf(is_mac_os, "There is a separate issue on OSX")
aaronayres35 marked this conversation as resolved.
Show resolved Hide resolved
def test_editor_error_msg(self):
from pyface.qt import QtGui

class Foo(HasTraits):
x = Range(low=0.0, high=1.0, value=0.5, exclude_low=True)

foo = Foo()
tester = UITester()
with tester.create_ui(foo) as ui:

x_range = tester.find_by_name(ui, "x")
x_range_textbox = x_range.locate(Textbox())

for _ in range(3):
x_range_textbox.perform(KeyClick('Backspace'))

x_range_textbox.perform(KeySequence('0.0'))

def trigger_error():
x_range_textbox.perform(KeyClick('Enter'))

def check_and_close(mdtester):
try:
with mdtester.capture_error():
self.assertTrue(
mdtester.has_widget(
text="The 'x' trait of a Foo instance must be "
"0.0 < a floating point number <= 1.0, "
"but a value of 0.0 <class 'float'> was "
"specified.",
type_=QtGui.QMessageBox,
)
)
finally:
mdtester.close(accept=True)
self.assertTrue(mdtester.dialog_was_opened)

mdtester = ModalDialogTester(trigger_error)
mdtester.open_and_run(check_and_close)
self.assertTrue(mdtester.dialog_was_opened)