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
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in 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!
""" Pyface 'FeatureBar' support.
Defines the 'FeatureBar' class which displays and allows the user to
interact with a set of DockWindowFeatures for a specified DockControl.
"""
import wx
from traits.api import HasPrivateTraits, Instance, Bool, Event
from pyface.wx.drag_and_drop import PythonDropTarget, PythonDropSource
from pyface.ui_traits import TraitsUIColor as Color
from .dock_sizer import DockControl, FEATURE_EXTERNAL_DRAG
from .ifeature_tool import IFeatureTool
# -------------------------------------------------------------------------------
# 'FeatureBar' class:
# -------------------------------------------------------------------------------
class FeatureBar(HasPrivateTraits):
# ---------------------------------------------------------------------------
# Trait definitions:
# ---------------------------------------------------------------------------
# The wx.Window which is the parent for the FeatureBar:
parent = Instance(wx.Window)
# The DockControl whose features are being displayed:
dock_control = Instance(DockControl)
# The wx.Window being used for the FeatureBar:
control = Instance(wx.Window)
# Event posted when the user has completed using the FeatureBar:
completed = Event()
# The background color for the FeatureBar:
bg_color = Color(0xDBEEF7, allow_none=True)
# The border color for the FeatureBar:
border_color = Color(0x2583AF, allow_none=True)
# Should the feature bar display horizontally (or vertically)?
horizontal = Bool(True)
# ---------------------------------------------------------------------------
# Hides the feature bar:
# ---------------------------------------------------------------------------
def hide(self):
""" Hides the feature bar.
"""
if self.control is not None:
self.control.Hide()
# ---------------------------------------------------------------------------
# Shows the feature bar:
# ---------------------------------------------------------------------------
def show(self):
""" Shows the feature bar.
"""
# Make sure all prerequisites are met:
dock_control, parent = self.dock_control, self.parent
if (dock_control is None) or (parent is None):
return
# Create the actual control (if needed):
control = self.control
if control is None:
self.control = control = wx.Frame(
None, -1, "", style=wx.BORDER_NONE
)
# Set up the 'erase background' event handler:
control.Bind(wx.EVT_ERASE_BACKGROUND, self._erase_background)
# Set up the 'paint' event handler:
control.Bind(wx.EVT_PAINT, self._paint)
# Set up mouse event handlers:
control.Bind(wx.EVT_LEFT_DOWN, self._left_down)
control.Bind(wx.EVT_LEFT_UP, self._left_up)
control.Bind(wx.EVT_RIGHT_DOWN, self._right_down)
control.Bind(wx.EVT_RIGHT_UP, self._right_up)
control.Bind(wx.EVT_MOTION, self._mouse_move)
control.Bind(wx.EVT_ENTER_WINDOW, self._mouse_enter)
control.SetDropTarget(PythonDropTarget(self))
# Calculate the best size and position for the feature bar:
size = wx.Size(32, 32)
width = height = 0
horizontal = self.horizontal
for feature in dock_control.active_features:
bitmap = feature.bitmap
if bitmap is not None:
if horizontal:
width += bitmap.GetWidth() + 3
height = max(height, bitmap.GetHeight())
else:
width = max(width, bitmap.GetWidth())
height += bitmap.GetHeight() + 3
if width > 0:
if horizontal:
size = wx.Size(width + 5, height + 8)
else:
size = wx.Size(width + 8, height + 5)
control.SetSize(size)
px, py = parent.GetScreenPosition()
fx, fy = dock_control.feature_popup_position
control.SetPosition(wx.Point(px + fx, py + fy))
control.Show()
# -- Window Event Handlers --------------------------------------------------
# ---------------------------------------------------------------------------
# Handles repainting the window:
# ---------------------------------------------------------------------------
def _paint(self, event):
""" Handles repainting the window.
"""
window = self.control
dx, dy = window.GetSize().Get()
dc = wx.PaintDC(window)
# Draw the feature container:
bg_color = self.bg_color
border_color = self.border_color
if (bg_color is not None) or (border_color is not None):
if border_color is None:
dc.SetPen(wx.TRANSPARENT_PEN)
else:
dc.SetPen(wx.Pen(border_color, 1, wx.SOLID))
if bg_color is None:
dc.SetBrush(wx.TRANSPARENT_PEN)
else:
dc.SetBrush(wx.Brush(bg_color, wx.SOLID))
dc.DrawRectangle(0, 0, dx, dy)
# Draw the feature icons:
if self.horizontal:
x = 4
for feature in self.dock_control.active_features:
bitmap = feature.bitmap
if bitmap is not None:
dc.DrawBitmap(bitmap, x, 4, True)
x += bitmap.GetWidth() + 3
else:
y = 4
for feature in self.dock_control.active_features:
bitmap = feature.bitmap
if bitmap is not None:
dc.DrawBitmap(bitmap, 4, y, True)
y += bitmap.GetHeight() + 3
# ---------------------------------------------------------------------------
# Handles erasing the window background:
# ---------------------------------------------------------------------------
def _erase_background(self, event):
""" Handles erasing the window background.
"""
pass
# ---------------------------------------------------------------------------
# Handles the left mouse button being pressed:
# ---------------------------------------------------------------------------
def _left_down(self, event):
""" Handles the left mouse button being pressed.
"""
self._feature = self._feature_at(event)
self._dragging = False
self._xy = (event.GetX(), event.GetY())
# self.control.CaptureMouse()
# ---------------------------------------------------------------------------
# Handles the left mouse button being released:
# ---------------------------------------------------------------------------
def _left_up(self, event):
""" Handles the left mouse button being released.
"""
# self.control.ReleaseMouse()
self._dragging = None
feature, self._feature = self._feature, None
if feature is not None:
if feature is self._feature_at(event):
self.control.ReleaseMouse()
self.completed = True
feature._set_event(event)
feature.click()
# ---------------------------------------------------------------------------
# Handles the right mouse button being pressed:
# ---------------------------------------------------------------------------
def _right_down(self, event):
""" Handles the right mouse button being pressed.
"""
self._feature = self._feature_at(event)
self._dragging = False
self._xy = (event.GetX(), event.GetY())
# self.control.CaptureMouse()
# ---------------------------------------------------------------------------
# Handles the right mouse button being released:
# ---------------------------------------------------------------------------
def _right_up(self, event):
""" Handles the right mouse button being released.
"""
# self.control.ReleaseMouse()
self._dragging = None
feature, self._feature = self._feature, None
if feature is not None:
if feature is self._feature_at(event):
self.control.ReleaseMouse()
self.completed = True
feature._set_event(event)
feature.right_click()
# ---------------------------------------------------------------------------
# Handles the mouse moving over the window:
# ---------------------------------------------------------------------------
def _mouse_move(self, event):
""" Handles the mouse moving over the window.
"""
# Update tooltips if no mouse button is currently pressed:
if self._dragging is None:
feature = self._feature_at(event)
if feature is not self._tooltip_feature:
self._tooltip_feature = feature
tooltip = ""
if feature is not None:
tooltip = feature.tooltip
wx.ToolTip.Enable(False)
wx.ToolTip.Enable(True)
self.control.SetToolTip(wx.ToolTip(tooltip))
# Check to see if the mouse has left the window, and mark it
# completed if it has:
x, y = event.GetX(), event.GetY()
dx, dy = self.control.GetSize().Get()
if (x < 0) or (y < 0) or (x >= dx) or (y >= dy):
self.control.ReleaseMouse()
self._tooltip_feature = None
self.completed = True
return
# Check to see if we are in 'drag mode' yet:
if not self._dragging:
x, y = self._xy
if (abs(x - event.GetX()) + abs(y - event.GetY())) < 3:
return
self._dragging = True
# Check to see if user is trying to drag a 'feature':
feature = self._feature
if feature is not None:
feature._set_event(event)
prefix = button = ""
if event.RightIsDown():
button = "right_"
if event.ControlDown():
prefix = "control_"
elif event.AltDown():
prefix = "alt_"
elif event.ShiftDown():
prefix = "shift_"
object = getattr(feature, "%s%sdrag" % (prefix, button))()
if object is not None:
self.control.ReleaseMouse()
self._feature = None
self.completed = True
self.dock_control.pre_drag_all(object)
PythonDropSource(self.control, object)
self.dock_control.post_drag_all()
self._dragging = None
# ---------------------------------------------------------------------------
# Handles the mouse entering the window:
# ---------------------------------------------------------------------------
def _mouse_enter(self, event):
""" Handles the mouse entering the window.
"""
self.control.CaptureMouse()
# -- Drag and drop event handlers: ----------------------------------------------
# ---------------------------------------------------------------------------
# Handles a Python object being dropped on the control:
# ---------------------------------------------------------------------------
def wx_dropped_on(self, x, y, data, drag_result):
""" Handles a Python object being dropped on the window.
"""
# Determine what, if any, feature the object was dropped on:
feature = self._can_drop_on_feature(x, y, data)
# Indicate use of the feature bar is complete:
self.completed = True
# Reset any drag state information:
self.dock_control.post_drag(FEATURE_EXTERNAL_DRAG)
# Check to see if the data was dropped on a feature or not:
if feature is not None:
if isinstance(data, IFeatureTool):
# Handle an object implementing IFeatureTool being dropped:
dock_control = feature.dock_control
data.feature_dropped_on_dock_control(dock_control)
data.feature_dropped_on(dock_control.object)
else:
# Handle a normal object being dropped:
wx, wy = self.control.GetScreenPosition()
feature.trait_set(x=wx + x, y=wy + y)
feature.drop(data)
return drag_result
return wx.DragNone
# ---------------------------------------------------------------------------
# Handles a Python object being dragged over the control:
# ---------------------------------------------------------------------------
def wx_drag_over(self, x, y, data, drag_result):
""" Handles a Python object being dragged over the control.
"""
# Handle the case of dragging a normal object over a 'feature':
if self._can_drop_on_feature(x, y, data) is not None:
return drag_result
return wx.DragNone
# ---------------------------------------------------------------------------
# Handles a dragged Python object leaving the window:
# ---------------------------------------------------------------------------
def wx_drag_leave(self, data):
""" Handles a dragged Python object leaving the window.
"""
# Indicate use of the feature bar is complete:
self.completed = True
# Reset any drag state information:
self.dock_control.post_drag(FEATURE_EXTERNAL_DRAG)
# -- Private Methods --------------------------------------------------------
# ---------------------------------------------------------------------------
# Returns a feature that the pointer is over and which can accept the
# specified data:
# ---------------------------------------------------------------------------
def _can_drop_on_feature(self, x, y, data):
""" Returns a feature that the pointer is over and which can accept the
specified data.
"""
feature = self._feature_at(FakeEvent(x, y))
if (feature is not None) and feature.can_drop(data):
return feature
return None
# ---------------------------------------------------------------------------
# Returns the DockWindowFeature (if any) at a specified window position:
# ---------------------------------------------------------------------------
def _feature_at(self, event):
""" Returns the DockWindowFeature (if any) at a specified window
position.
"""
if self.horizontal:
x = 4
for feature in self.dock_control.active_features:
bitmap = feature.bitmap
if bitmap is not None:
bdx = bitmap.GetWidth()
if self._is_in(event, x, 4, bdx, bitmap.GetHeight()):
return feature
x += bdx + 3
else:
y = 4
for feature in self.dock_control.active_features:
bitmap = feature.bitmap
if bitmap is not None:
bdy = bitmap.GetHeight()
if self._is_in(event, 4, y, bitmap.GetWidth(), bdy):
return feature
y += bdy + 3
return None
# ---------------------------------------------------------------------------
# Returns whether or not an event is within a specified bounds:
# ---------------------------------------------------------------------------
def _is_in(self, event, x, y, dx, dy):
""" Returns whether or not an event is within a specified bounds.
"""
return (x <= event.GetX() < (x + dx)) and (
y <= event.GetY() < (y + dy)
)
# -------------------------------------------------------------------------------
# 'FakeEvent' class:
# -------------------------------------------------------------------------------
class FakeEvent(object):
def __init__(self, x, y):
self.x, self.y = x, y
def GetX(self):
return self.x
def GetY(self):
return self.y
|