<s> """<STR_LIT>""" <EOL> from traits . has_traits import HasTraits <EOL> from traits . trait_types import List , Int , Str <EOL> from traitsui . item import Item <EOL> from traitsui . view import View <EOL> from traitsui . editors . list_str_editor import ListStrEditor <EOL> from traitsui . tests . _tools import ( <EOL> press_ok_button , skip_if_not_qt4 , skip_if_not_wx , <EOL> store_exceptions_on_all_threads ) <EOL> class ListStrEditorWithSelectedIndex ( HasTraits ) : <EOL> values = List ( Str ( ) ) <EOL> selected_index = Int ( ) <EOL> selected_indices = List ( Int ( ) ) <EOL> selected = Str ( ) <EOL> single_select_view = View ( <EOL> Item ( '<STR_LIT>' , <EOL> show_label = False , <EOL> editor = ListStrEditor ( <EOL> selected_index = '<STR_LIT>' , <EOL> editable = False ) , <EOL> ) , <EOL> buttons = [ '<STR_LIT:OK>' ] ) <EOL> multi_select_view = View ( <EOL> Item ( '<STR_LIT>' , <EOL> show_label = False , <EOL> editor = ListStrEditor ( <EOL> multi_select = True , <EOL> selected_index = '<STR_LIT>' , <EOL> editable = False ) , <EOL> ) , <EOL> buttons = [ '<STR_LIT:OK>' ] ) <EOL> single_select_item_view = View ( <EOL> Item ( '<STR_LIT>' , <EOL> show_label = False , <EOL> editor = ListStrEditor ( <EOL> selected = '<STR_LIT>' , <EOL> editable = False ) , <EOL> ) , <EOL> buttons = [ '<STR_LIT:OK>' ] ) <EOL> def get_selected ( control ) : <EOL> """<STR_LIT>""" <EOL> import wx <EOL> selected = [ ] <EOL> item = - <NUM_LIT:1> <EOL> while True : <EOL> item = control . GetNextItem ( item , wx . LIST_NEXT_ALL , <EOL> wx . LIST_STATE_SELECTED ) <EOL> if item == - <NUM_LIT:1> : <EOL> break ; <EOL> selected . append ( item ) <EOL> return selected <EOL> @ skip_if_not_wx <EOL> def test_wx_list_str_selected_index ( ) : <EOL> with store_exceptions_on_all_threads ( ) : <EOL> obj = ListStrEditorWithSelectedIndex ( <EOL> values = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> selected_index = <NUM_LIT:1> ) <EOL> ui = obj . edit_traits ( view = single_select_view ) <EOL> liststrctrl = ui . control . FindWindowByName ( '<STR_LIT>' ) <EOL> selected_1 = get_selected ( liststrctrl ) <EOL> obj . selected_index = <NUM_LIT:0> <EOL> selected_2 = get_selected ( liststrctrl ) <EOL> press_ok_button ( ui ) <EOL> assert selected_1 == [ <NUM_LIT:1> ] <EOL> assert selected_2 == [ <NUM_LIT:0> ] <EOL> @ skip_if_not_wx <EOL> def test_wx_list_str_multi_selected_index ( ) : <EOL> with store_exceptions_on_all_threads ( ) : <EOL> obj = ListStrEditorWithSelectedIndex ( <EOL> values = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> selected_indices = [ <NUM_LIT:1> ] ) <EOL> ui = obj . edit_traits ( view = multi_select_view ) <EOL> liststrctrl = ui . control . FindWindowByName ( '<STR_LIT>' ) <EOL> selected_1 = get_selected ( liststrctrl ) <EOL> obj . selected_indices = [ <NUM_LIT:0> ] <EOL> selected_2 = get_selected ( liststrctrl ) <EOL> press_ok_button ( ui ) <EOL> assert selected_1 == [ <NUM_LIT:1> ] <EOL> assert selected_2 == [ <NUM_LIT:0> ] <EOL> @ skip_if_not_qt4 <EOL> def test_selection_listener_disconnected ( ) : <EOL> """<STR_LIT>""" <EOL> from pyface . api import GUI <EOL> from pyface . qt . QtGui import QApplication , QItemSelectionModel <EOL> from pyface . ui . qt4 . util . event_loop_helper import EventLoopHelper <EOL> from pyface . ui . qt4 . util . testing import event_loop <EOL> obj = ListStrEditorWithSelectedIndex ( values = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> with store_exceptions_on_all_threads ( ) : <EOL> qt_app = QApplication . instance ( ) <EOL> if qt_app is None : <EOL> qt_app = QApplication ( [ ] ) <EOL> helper = EventLoopHelper ( gui = GUI ( ) , qt_app = qt_app ) <EOL> ui = obj . edit_traits ( view = single_select_item_view ) <EOL> with helper . delete_widget ( ui . control ) : <EOL> press_ok_button ( ui ) <EOL> ui = obj . edit_traits ( view = single_select_item_view ) <EOL> with event_loop ( ) : <EOL> editor = ui . get_editors ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> list_view = editor . list_view <EOL> mi = editor . model . index ( <NUM_LIT:1> ) <EOL> list_view . selectionModel ( ) . select ( mi , QItemSelectionModel . ClearAndSelect ) <EOL> obj . selected = '<STR_LIT>' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> editor = ListStrEditorWithSelectedIndex ( <EOL> values = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> selected_index = <NUM_LIT:1> ) <EOL> editor . configure_traits ( view = single_select_view ) </s>
