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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
|
#-------------------------------------------------------------------------------
#
# Copyright (c) 2007, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
#
# Author: David C. Morrill
# Date: 07/14/2008
#
#-------------------------------------------------------------------------------
""" Traits UI simple, scrubber-based integer or float value editor.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
import wx
from math \
import log10, pow
from traits.api \
import Any, BaseRange, BaseEnum, Str, Float, TraitError, \
on_trait_change
from traitsui.api \
import View, Item, EnumEditor
# FIXME: ScrubberEditor is a proxy class defined here just for backward
# compatibility (represents the editor factory for scrubber editors).
# The class has been moved to traitsui.editors.scrubber_editor
from traitsui.editors.scrubber_editor \
import ScrubberEditor
from traitsui.wx.editor \
import Editor
from pyface.timer.api \
import do_after
from constants \
import ErrorColor
from image_slice \
import paint_parent
from helper \
import disconnect, disconnect_no_id, BufferDC
#-------------------------------------------------------------------------------
# '_ScrubberEditor' class:
#-------------------------------------------------------------------------------
class _ScrubberEditor ( Editor ):
""" Traits UI simple, scrubber-based integer or float value editor.
"""
# The low end of the slider range:
low = Any
# The high end of the slider range:
high = Any
# The smallest allowed increment:
increment = Float
# The current text being displayed:
text = Str
# The mapping to use (only for Enum's):
mapping = Any
#-- Class Variables --------------------------------------------------------
text_styles = {
'left': wx.TE_LEFT,
'center': wx.TE_CENTRE,
'right': wx.TE_RIGHT
}
#---------------------------------------------------------------------------
# Finishes initializing the editor by creating the underlying toolkit
# widget:
#---------------------------------------------------------------------------
def init ( self, parent ):
""" Finishes initializing the editor by creating the underlying toolkit
widget.
"""
factory = self.factory
# Establish the range of the slider:
low_name = high_name = ''
low, high = factory.low, factory.high
if high <= low:
low = high = None
handler = self.object.trait( self.name ).handler
if isinstance( handler, BaseRange ):
low_name, high_name = handler._low_name, handler._high_name
if low_name == '':
low = handler._low
if high_name == '':
high = handler._high
elif isinstance( handler, BaseEnum ):
if handler.name == '':
self.mapping = handler.values
else:
self.sync_value( handler.name, 'mapping', 'from' )
low, high = 0, self.high
# Create the control:
self.control = control = wx.Window( parent, -1,
size = wx.Size( 50, 18 ),
style = wx.FULL_REPAINT_ON_RESIZE |
wx.TAB_TRAVERSAL )
# Set up the painting event handlers:
wx.EVT_ERASE_BACKGROUND( control, self._erase_background )
wx.EVT_PAINT( control, self._on_paint )
wx.EVT_SET_FOCUS( control, self._set_focus )
# Set up mouse event handlers:
wx.EVT_LEAVE_WINDOW( control, self._leave_window )
wx.EVT_ENTER_WINDOW( control, self._enter_window )
wx.EVT_LEFT_DOWN( control, self._left_down )
wx.EVT_LEFT_UP( control, self._left_up )
wx.EVT_MOTION( control, self._motion )
wx.EVT_MOUSEWHEEL( control, self._mouse_wheel )
# Set up the control resize handler:
wx.EVT_SIZE( control, self._resize )
# Set the tooltip:
self._can_set_tooltip = (not self.set_tooltip())
# Save the values we calculated:
self.set( low = low, high = high )
self.sync_value( low_name, 'low', 'from' )
self.sync_value( high_name, 'high', 'from' )
# Force a reset (in case low = high = None, which won't cause a
# notification to fire):
self._reset_scrubber()
#---------------------------------------------------------------------------
# Disposes of the contents of an editor:
#---------------------------------------------------------------------------
def dispose ( self ):
""" Disposes of the contents of an editor.
"""
# Remove all of the wx event handlers:
disconnect_no_id( self.control, wx.EVT_ERASE_BACKGROUND, wx.EVT_PAINT,
wx.EVT_SET_FOCUS, wx.EVT_LEAVE_WINDOW, wx.EVT_ENTER_WINDOW,
wx.EVT_LEFT_DOWN, wx.EVT_LEFT_UP, wx.EVT_MOTION, wx.EVT_MOUSEWHEEL,
wx.EVT_SIZE )
# Disconnect the pop-up text event handlers:
self._disconnect_text()
super( _ScrubberEditor, self ).dispose()
#---------------------------------------------------------------------------
# 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.
"""
self.text = self.string_value( self.value )
self._text_size = None
self._refresh()
self._enum_completed()
#---------------------------------------------------------------------------
# Updates the object when the scrubber value changes:
#---------------------------------------------------------------------------
def update_object ( self, value ):
""" Updates the object when the scrubber value changes.
"""
if self.mapping is not None:
value = self.mapping[ int( value ) ]
if value != self.value:
try:
self.value = value
self.update_editor()
except TraitError:
value = int( value )
if value != self.value:
self.value = value
self.update_editor()
#---------------------------------------------------------------------------
# Handles an error that occurs while setting the object's trait value:
#---------------------------------------------------------------------------
def error ( self, excp ):
""" Handles an error that occurs while setting the object's trait value.
"""
pass
#-- Trait Event Handlers ---------------------------------------------------
def _mapping_changed ( self, mapping ):
""" Handles the Enum mapping being changed.
"""
self.high = len( mapping ) - 1
#-- Private Methods --------------------------------------------------------
@on_trait_change( 'low, high' )
def _reset_scrubber ( self ):
""" Sets the the current tooltip.
"""
low, high = self.low, self.high
if self._can_set_tooltip:
if self.mapping is not None:
tooltip = '[%s]' % (', '.join( self.mapping ))
if len( tooltip ) > 80:
tooltip = ''
elif high is None:
tooltip = ''
if low is not None:
tooltip = '[%g..]' % low
elif low is None:
tooltip = '[..%g]' % high
else:
tooltip = '[%g..%g]' % ( low, high )
self.control.SetToolTipString( tooltip )
# Establish the slider increment:
increment = self.factory.increment
if increment <= 0.0:
if (low is None) or (high is None) or isinstance( low, int ):
increment = 1.0
else:
increment = pow( 10, round( log10( (high - low) / 100.0 ) ) )
self.increment = increment
self.update_editor()
def _get_text_bounds ( self ):
""" Get the window bounds of where the current text should be
displayed.
"""
tdx, tdy, descent, leading = self._get_text_size()
wdx, wdy = self.control.GetClientSizeTuple()
ty = ((wdy - (tdy - descent)) / 2) - 1
alignment = self.factory.alignment
if alignment == 'left':
tx = 0
elif alignment == 'center':
tx = (wdx - tdx) / 2
else:
tx = wdx - tdx
return ( tx, ty, tdx, tdy )
def _get_text_size ( self ):
""" Returns the text size information for the window.
"""
if self._text_size is None:
self._text_size = self.control.GetFullTextExtent(
self.text.strip() or 'M' )
return self._text_size
def _refresh ( self ):
""" Refreshes the contents of the control.
"""
if self.control is not None:
self.control.Refresh()
def _set_scrubber_position ( self, event, delta ):
""" Calculates a new scrubber value for a specified mouse position
change.
"""
clicks = 3
increment = self.increment
if event.ShiftDown():
increment *= 10.0
clicks = 7
elif event.ControlDown():
increment /= 10.0
value = self._value + (delta / clicks) * increment
if self.low is not None:
value = max( value, self.low )
if self.high is not None:
value = min( value, self.high )
self.update_object( value )
def _delayed_click ( self ):
""" Handle a delayed click response.
"""
self._pending = False
def _pop_up_editor ( self ):
""" Pop-up a text control to allow the user to enter a value using
the keyboard.
"""
self.control.SetCursor( wx.StockCursor( wx.CURSOR_ARROW ) )
if self.mapping is not None:
self._pop_up_enum()
else:
self._pop_up_text()
def _pop_up_enum ( self ):
self._ui = self.object.edit_traits(
view = View(
Item( self.name,
id = 'drop_down',
show_label = False,
padding = -4,
editor = EnumEditor( name = 'editor.mapping' ) ),
kind = 'subpanel' ),
parent = self.control,
context = { 'object': self.object, 'editor': self } )
dx, dy = self.control.GetSizeTuple()
drop_down = self._ui.info.drop_down.control
drop_down.SetDimensions( 0, 0, dx, dy )
drop_down.SetFocus()
wx.EVT_KILL_FOCUS( drop_down, self._enum_completed )
def _pop_up_text ( self ):
control = self.control
self._text = text = wx.TextCtrl( control, -1, str( self.value ),
size = control.GetSize(),
style = self.text_styles[ self.factory.alignment ] |
wx.TE_PROCESS_ENTER )
text.SetSelection( -1, -1 )
text.SetFocus()
wx.EVT_TEXT_ENTER( control, text.GetId(), self._text_completed )
wx.EVT_KILL_FOCUS( text, self._text_completed )
wx.EVT_ENTER_WINDOW( text, self._enter_text )
wx.EVT_LEAVE_WINDOW( text, self._leave_text )
wx.EVT_CHAR( text, self._key_entered )
def _destroy_text ( self ):
""" Destroys the current text control.
"""
self._ignore_focus = self._in_text_window
self._disconnect_text()
self.control.DestroyChildren()
self._text = None
def _disconnect_text ( self ):
""" Disconnects the event handlers for the pop up text editor.
"""
if self._text is not None:
disconnect( self._text, wx.EVT_TEXT_ENTER )
disconnect_no_id( self._text, wx.EVT_KILL_FOCUS,
wx.EVT_ENTER_WINDOW, wx.EVT_LEAVE_WINDOW, wx.EVT_CHAR )
def _init_value ( self ):
""" Initializes the current value when the user begins a drag or moves
the mouse wheel.
"""
if self.mapping is not None:
try:
self._value = list( self.mapping ).index( self.value )
except:
self._value = 0
else:
self._value = self.value
#--- wxPython Event Handlers -----------------------------------------------
def _erase_background ( self, event ):
""" Do not erase the background here (do it in the 'on_paint' handler).
"""
pass
def _on_paint ( self, event ):
""" Paint the background using the associated ImageSlice object.
"""
control = self.control
dc = BufferDC( control )
# Draw the background:
factory = self.factory
color = factory.color_
if self._x is not None:
if factory.active_color_ is not None:
color = factory.active_color_
elif self._hover:
if factory.hover_color_ is not None:
color = factory.hover_color_
if color is None:
paint_parent( dc, control )
brush = wx.TRANSPARENT_BRUSH
else:
brush = wx.Brush( color )
color = factory.border_color_
if color is not None:
pen = wx.Pen( color )
else:
pen = wx.TRANSPARENT_PEN
if (pen != wx.TRANSPARENT_PEN) or (brush != wx.TRANSPARENT_BRUSH):
wdx, wdy = control.GetClientSizeTuple()
dc.SetBrush( brush )
dc.SetPen( pen )
dc.DrawRectangle( 0, 0, wdx, wdy )
# Draw the current text value:
dc.SetBackgroundMode( wx.TRANSPARENT )
dc.SetTextForeground( factory.text_color_ )
dc.SetFont( control.GetFont() )
tx, ty, tdx, tdy = self._get_text_bounds()
dc.DrawText( self.text, tx, ty )
# Copy the buffer contents to the display:
dc.copy()
def _resize ( self, event ):
""" Handles the control being resized.
"""
if self._text is not None:
self._text.SetSize( self.control.GetSize() )
def _set_focus ( self, event ):
""" Handle the control getting the keyboard focus.
"""
if ((not self._ignore_focus) and
(self._x is None) and
(self._text is None)):
self._pop_up_editor()
event.Skip()
def _enter_window ( self, event ):
""" Handles the mouse entering the window.
"""
self._hover = True
self.control.SetCursor( wx.StockCursor( wx.CURSOR_HAND ) )
if not self._ignore_focus:
self._ignore_focus = True
self.control.SetFocus()
self._ignore_focus = False
if self._x is not None:
if self.factory.active_color_ != self.factory.color_:
self.control.Refresh()
elif self.factory.hover_color_ != self.factory.color_:
self.control.Refresh()
def _leave_window ( self, event ):
""" Handles the mouse leaving the window.
"""
self._hover = False
if self.factory.hover_color_ != self.factory.color_:
self.control.Refresh()
def _left_down ( self, event ):
""" Handles the left mouse being pressed.
"""
self._x, self._y = event.GetX(), event.GetY()
self._pending = True
self._init_value()
self.control.CaptureMouse()
if self.factory.active_color_ != self.factory.hover_color_:
self.control.Refresh()
do_after( 200, self._delayed_click )
def _left_up ( self, event ):
""" Handles the left mouse button being released.
"""
self.control.ReleaseMouse()
if self._pending:
self._pop_up_editor()
self._x = self._y = self._value = self._pending = None
if self._hover or (self.factory.active_color_ != self.factory.color_):
self.control.Refresh()
def _motion ( self, event ):
""" Handles the mouse moving.
"""
if self._x is not None:
x, y = event.GetX(), event.GetY()
dx = x - self._x
adx = abs( dx )
dy = y - self._y
ady = abs( dy )
if self._pending:
if (adx + ady) < 3:
return
self._pending = False
if adx > ady:
delta = dx
else:
delta = -dy
self._set_scrubber_position( event, delta )
def _mouse_wheel ( self, event ):
""" Handles the mouse wheel moving.
"""
if self._hover:
self._init_value()
clicks = 3
if event.ShiftDown():
clicks = 7
delta = clicks * (event.GetWheelRotation() / event.GetWheelDelta())
self._set_scrubber_position( event, delta )
def _update_value ( self, event ):
""" Updates the object value from the current text control value.
"""
control = event.GetEventObject()
try:
self.update_object( float( control.GetValue() ) )
return True
except TraitError:
control.SetBackgroundColour( ErrorColor )
control.Refresh()
return False
def _enter_text ( self, event ):
""" Handles the mouse entering the pop-up text control.
"""
self._in_text_window = True
def _leave_text ( self, event ):
""" Handles the mouse leaving the pop-up text control.
"""
self._in_text_window = False
def _text_completed ( self, event ):
""" Handles the user pressing the 'Enter' key in the text control.
"""
if self._update_value( event ):
self._destroy_text()
def _enum_completed ( self, event = None ):
""" Handles the Enum drop-down control losing focus.
"""
if self._ui is not None:
self._ignore_focus = True
disconnect_no_id( self._ui.info.drop_down.control,
wx.EVT_KILL_FOCUS )
self._ui.dispose()
del self._ui
def _key_entered ( self, event ):
""" Handles individual key strokes while the text control is active.
"""
key_code = event.GetKeyCode()
if key_code == wx.WXK_ESCAPE:
self._destroy_text()
return
if key_code == wx.WXK_TAB:
if self._update_value( event ):
if event.ShiftDown():
self.control.Navigate( 0 )
else:
self.control.Navigate()
return
event.Skip()
|