1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
#------------------------------------------------------------------------------
# Copyright (c) 2007, Riverbank Computing Limited
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD license.
# However, when used with the GPL version of PyQt the additional terms described in the PyQt GPL exception also apply
#
# Author: Riverbank Computing Limited
#------------------------------------------------------------------------------
""" Defines the set editors for the PyQt user interface toolkit.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
from pyface.qt import QtCore, QtGui
# FIXME: ToolkitEditorFactory is a proxy class defined here just for backward
# compatibility. The class has been moved to the
# traitsui.editors.set_editor file.
from traitsui.editors.set_editor \
import ToolkitEditorFactory
from traitsui.helper \
import enum_values_changed
from editor \
import Editor
from traits.api \
import Instance, Property
#-------------------------------------------------------------------------------
# 'SimpleEditor' class:
#-------------------------------------------------------------------------------
class SimpleEditor ( Editor ):
""" Simple style of editor for sets.
The editor displays two list boxes, with buttons for moving the selected
items from left to right, or vice versa. If **can_move_all** on the factory
is True, then buttons are displayed for moving all the items to one box
or the other. If the set is ordered, buttons are displayed for moving the
selected item up or down in right-side list box.
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# The top level QLayout for the editor:
root_layout = Instance(QtGui.QLayout)
# Current set of enumeration names:
names = Property
# Current mapping from names to values:
mapping = Property
# Current inverse mapping from values to names:
inverse_mapping = Property
# Is set editor scrollable? This value overrides the default.
scrollable = True
#---------------------------------------------------------------------------
# Finishes initializing the editor by creating the underlying toolkit
# widget:
#---------------------------------------------------------------------------
def init ( self, parent ):
""" Finishes initializing the editor by creating the underlying toolkit
widget.
"""
self.control = QtGui.QWidget()
self.root_layout = QtGui.QGridLayout(self.control)
self.root_layout.setContentsMargins(0, 0, 0, 0)
factory = self.factory
if factory.name != '':
self._object, self._name, self._value = \
self.parse_extended_name( factory.name )
self.values_changed()
self._object.on_trait_change( self._values_changed,
self._name, dispatch = 'ui' )
else:
factory.on_trait_change( self.update_editor, 'values_modified',
dispatch = 'ui' )
blayout = QtGui.QVBoxLayout()
self._unused = self._create_listbox(0, self._on_unused, self._on_use,
factory.left_column_title)
self._use_all = self._unuse_all = self._up = self._down = None
if factory.can_move_all:
self._use_all = self._create_button('>>', blayout,
self._on_use_all)
self._use = self._create_button('>', blayout, self._on_use)
self._unuse = self._create_button('<', blayout, self._on_unuse)
if factory.can_move_all:
self._unuse_all = self._create_button('<<', blayout,
self._on_unuse_all)
if factory.ordered:
self._up = self._create_button('Move Up', blayout, self._on_up)
self._down = self._create_button('Move Down', blayout,
self._on_down)
self.root_layout.addLayout(blayout, 1, 1, QtCore.Qt.AlignCenter)
self._used = self._create_listbox(2, self._on_value, self._on_unuse,
factory.right_column_title)
self.context_object.on_trait_change( self.update_editor,
self.extended_name + '_items?', dispatch = 'ui' )
#---------------------------------------------------------------------------
# Gets the current set of enumeration names:
#---------------------------------------------------------------------------
def _get_names ( self ):
""" Gets the current set of enumeration names.
"""
if self._object is None:
return self.factory._names
return self._names
#---------------------------------------------------------------------------
# Gets the current mapping:
#---------------------------------------------------------------------------
def _get_mapping ( self ):
""" Gets the current mapping.
"""
if self._object is None:
return self.factory._mapping
return self._mapping
#---------------------------------------------------------------------------
# Gets the current inverse mapping:
#---------------------------------------------------------------------------
def _get_inverse_mapping ( self ):
""" Gets the current inverse mapping.
"""
if self._object is None:
return self.factory._inverse_mapping
return self._inverse_mapping
#---------------------------------------------------------------------------
# Creates a list box:
#---------------------------------------------------------------------------
def _create_listbox(self, col, handler1, handler2, title):
"""Creates a list box.
"""
# Add the column title in emphasized text:
title_widget = QtGui.QLabel(title)
font = QtGui.QFont(title_widget.font())
font.setBold(True)
font.setPointSize(font.pointSize() + 1)
title_widget.setFont(font)
self.root_layout.addWidget(title_widget, 0, col, QtCore.Qt.AlignLeft)
# Create the list box and add it to the column:
list = QtGui.QListWidget()
list.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.root_layout.addWidget(list, 1, col)
list.connect(list,
QtCore.SIGNAL('itemClicked(QListWidgetItem *)'), handler1)
list.connect(list,
QtCore.SIGNAL('itemDoubleClicked(QListWidgetItem *)'), handler2)
return list
#---------------------------------------------------------------------------
# Creates a button:
#---------------------------------------------------------------------------
def _create_button(self, label, layout, handler):
""" Creates a button.
"""
button = QtGui.QPushButton(label)
button.connect(button, QtCore.SIGNAL('clicked()'), handler)
layout.addWidget(button)
return button
#---------------------------------------------------------------------------
# Recomputes the cached data based on the underlying enumeration model:
#---------------------------------------------------------------------------
def values_changed ( self ):
""" Recomputes the cached data based on the underlying enumeration model.
"""
self._names, self._mapping, self._inverse_mapping = \
enum_values_changed( self._value(), self.string_value )
#---------------------------------------------------------------------------
# Handles the underlying object model's enumeration set being changed:
#---------------------------------------------------------------------------
def _values_changed ( self ):
""" Handles the underlying object model's enumeration set being changed.
"""
self.values_changed()
self.update_editor()
#---------------------------------------------------------------------------
# Updates the editor when the object trait changes external to the editor:
#---------------------------------------------------------------------------
def update_editor ( self ):
""" Updates the editor when the object trait changes externally to the
editor.
"""
# Check for any items having been deleted from the enumeration that are
# still present in the object value:
mapping = self.inverse_mapping.copy()
values = [ v for v in self.value if v in mapping ]
if len( values ) < len( self.value ):
self.value = values
# Get a list of the selected items in the right box:
used = self._used
used_labels = self._get_selected_strings( used )
# Get a list of the selected items in the left box:
unused = self._unused
unused_labels = self._get_selected_strings( unused )
# Empty list boxes in preparation for rebuilding from current values:
used.clear()
unused.clear()
# Ensure right list box is kept alphabetized unless insertion
# order is relevant:
if not self.factory.ordered:
values = values[:]
values.sort()
# Rebuild the right listbox:
used_selections = []
for i, value in enumerate( values ):
label = mapping[ value ]
used.addItem(label)
del mapping[ value ]
if label in used_labels:
used_selections.append(i)
# Rebuild the left listbox:
unused_selections = []
unused_items = mapping.values()
unused_items.sort()
mapping = self.mapping
self._unused_items = [ mapping[ ui ] for ui in unused_items ]
for i, unused_item in enumerate( unused_items ):
unused.addItem(unused_item)
if unused_item in unused_labels:
unused_selections.append( i )
# If nothing is selected, default selection should be top of left box,
# or of right box if left box is empty:
if (len( used_selections ) == 0) and (len( unused_selections ) == 0):
if unused.count() == 0:
used_selections.append(0)
else:
unused_selections.append(0)
used_count = used.count()
for i in used_selections:
if i < used_count:
used.item(i).setSelected(True)
unused_count = unused.count()
for i in unused_selections:
if i < unused_count:
unused.item(i).setSelected(True)
self._check_up_down()
self._check_left_right()
#---------------------------------------------------------------------------
# Disposes of the contents of an editor:
#---------------------------------------------------------------------------
def dispose ( self ):
""" Disposes of the contents of an editor.
"""
if self._object is not None:
self._object.on_trait_change( self._values_changed,
self._name, remove = True )
else:
self.factory.on_trait_change( self.update_editor,
'values_modified', remove = True )
self.context_object.on_trait_change( self.update_editor,
self.extended_name + '_items?', remove = True )
super( SimpleEditor, self ).dispose()
#---------------------------------------------------------------------------
# Returns the editor's control for indicating error status:
#---------------------------------------------------------------------------
def get_error_control ( self ):
""" Returns the editor's control for indicating error status.
"""
return [ self._unused, self._used ]
#---------------------------------------------------------------------------
# Event handlers:
#---------------------------------------------------------------------------
def _on_value(self):
if not self.factory.ordered:
self._unused.clearSelection()
self._check_left_right()
self._check_up_down()
def _on_unused(self):
if not self.factory.ordered:
self._used.clearSelection()
self._check_left_right()
self._check_up_down()
def _on_use(self):
self._unused_items, self.value = self._transfer_items( self._unused,
self._used, self._unused_items, self.value )
def _on_unuse(self):
self.value, self._unused_items = self._transfer_items( self._used,
self._unused, self.value, self._unused_items )
def _on_use_all(self):
self._unused_items, self.value = self._transfer_all( self._unused,
self._used, self._unused_items, self.value )
def _on_unuse_all(self):
self.value, self._unused_items = self._transfer_all( self._used,
self._unused, self.value, self._unused_items )
def _on_up(self):
self._move_item(-1)
def _on_down(self):
self._move_item(1)
#---------------------------------------------------------------------------
# Private methods:
#---------------------------------------------------------------------------
#---------------------------------------------------------------------------
# Transfers all items from one list to another:
#---------------------------------------------------------------------------
def _transfer_all ( self, list_from, list_to, values_from, values_to ):
""" Transfers all items from one list to another.
"""
values_from = values_from[:]
values_to = values_to[:]
list_from.clearSelection()
while list_from.count() > 0:
index_to = list_to.count()
list_from.item(0).setSelected(True)
list_to.insertItems(index_to, self._get_selected_strings(list_from))
list_from.takeItem(0)
values_to.append(values_from[0])
del values_from[0]
list_to.item(0).setSelected(True)
self._check_left_right()
self._check_up_down()
return ( values_from, values_to )
#---------------------------------------------------------------------------
# Transfers the selected item from one list to another:
#---------------------------------------------------------------------------
def _transfer_items ( self, list_from, list_to, values_from, values_to ):
""" Transfers the selected item from one list to another.
"""
values_from = values_from[:]
values_to = values_to[:]
index_from = max( self._get_first_selection( list_from ), 0 )
index_to = max( self._get_first_selection( list_to ), 0 )
list_to.clearSelection()
# Get the list of strings in the "from" box to be moved:
selected_list = self._get_selected_strings( list_from )
# fixme: I don't know why I have to reverse the list to get
# correct behavior from the ordered list box. Investigate -- LP
selected_list.reverse()
list_to.insertItems(index_to, selected_list)
# Delete the transferred items from the left box:
items_from = list_from.selectedItems()
for i in range(len(items_from) - 1, -1, -1):
list_from.takeItem(list_from.row(items_from[i]))
del items_from
# Delete the transferred items from the "unused" value list:
for item_label in selected_list:
val_index_from = values_from.index( self.mapping[ item_label ] )
values_to.insert( index_to, values_from[ val_index_from ] )
del values_from[ val_index_from ]
# If right list is ordered, keep moved items selected:
if self.factory.ordered:
items = list_to.findItems(item_label,
QtCore.Qt.MatchFixedString|QtCore.Qt.MatchCaseSensitive)
if items:
items[0].setSelected(True)
# Reset the selection in the left box:
count = list_from.count()
if count > 0:
if index_from >= count:
index_from = count - 1
list_from.item(index_from).setSelected(True)
self._check_left_right()
self._check_up_down()
return ( values_from, values_to )
#---------------------------------------------------------------------------
# Moves an item up or down with the 'used' list:
#---------------------------------------------------------------------------
def _move_item ( self, direction ):
""" Moves an item up or down within the "used" list.
"""
# Move the item up/down within the list:
listbox = self._used
index_from = self._get_first_selection(listbox)
index_to = index_from + direction
label = listbox.takeItem(index_from).text()
listbox.insertItem(index_to, label)
listbox.item(index_to).setSelected(True)
# Enable the up/down buttons appropriately:
self._check_up_down()
# Move the item up/down within the editor's trait value:
value = self.value
if direction < 0:
index = index_to
values = [ value[ index_from ], value[ index_to ] ]
else:
index = index_from
values = [ value[ index_to ], value[ index_from ] ]
self.value = value[ : index ] + values + value[ index + 2: ]
#---------------------------------------------------------------------------
# Sets the proper enable state for the up and down buttons:
#---------------------------------------------------------------------------
def _check_up_down ( self ):
""" Sets the proper enabled state for the up and down buttons.
"""
if self.factory.ordered:
selected = self._used.selectedItems()
self._up.setEnabled(len(selected) == 1 and
selected[0] is not self._used.item(0))
self._down.setEnabled(len(selected) == 1 and
selected[0] is not self._used.item(self._used.count() - 1))
#---------------------------------------------------------------------------
# Sets the proper enable state for the left and right buttons:
#---------------------------------------------------------------------------
def _check_left_right(self):
""" Sets the proper enabled state for the left and right buttons.
"""
self._use.setEnabled(self._unused.count() > 0 and
self._get_first_selection(self._unused) >= 0)
self._unuse.setEnabled(self._used.count() > 0 and
self._get_first_selection(self._used) >= 0)
if self.factory.can_move_all:
self._use_all.setEnabled(self._unused.count() > 0 and
self._get_first_selection(self._unused) >= 0)
self._unuse_all.setEnabled(self._used.count() > 0 and
self._get_first_selection(self._used) >= 0)
#---------------------------------------------------------------------------
# Returns a list of the selected strings in the listbox
#---------------------------------------------------------------------------
def _get_selected_strings(self, listbox):
""" Returns a list of the selected strings in the given *listbox*.
"""
return [unicode(itm.text()) for itm in listbox.selectedItems()]
#---------------------------------------------------------------------------
# Returns the index of the first (or only) selected item.
#---------------------------------------------------------------------------
def _get_first_selection ( self, listbox ):
""" Returns the index of the first (or only) selected item.
"""
select_list = listbox.selectedItems()
if len(select_list) == 0:
return -1
return listbox.row(select_list[0])
|