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

DragTool mouse down event handling #268

Open
thorsant opened this issue Mar 24, 2015 · 8 comments
Open

DragTool mouse down event handling #268

thorsant opened this issue Mar 24, 2015 · 8 comments

Comments

@thorsant
Copy link

When trying to use DragTool, dragging will not occur unless I also implement my own normal_left_down function:

def normal_left_down(self, event):
    event.handled = True

Unless I implement this method, the left_down attribute of the subsequent mouse move events will be False, and dragging will not be handled correctly by the DragTool. Is it correct that I have to implement this extra handling to make it work? And what is the dependency of the MouseEvent.left_down attribute to previously handled events?

Also the DragTool does not use the modifier keys for anything.

@corranwebster
Copy link
Contributor

The DragTool in Chaco is just an alias for the one in Enable (https://github.com/enthought/enable/blob/master/enable/tools/drag_tool.py) so any issues with it are probably better raised there.

That said, the DragTool does already handle catching the mouse down, mouse move and mouse up events for you, but does nothing with them. If you want to create subclass of DragTool that does something useful, you need to write implementations for some or all of the is_draggable, drag_start, dragging, drag_cancel and drag_end methods. (See https://github.com/enthought/enable/blob/master/enable/tools/drag_tool.py#L58).

You may find that there are already tools in Chaco and Enable which do what you want, or close to what you want, (eg. for the drag operation to move an object, use MoveTool https://github.com/enthought/chaco/blob/master/chaco/tools/move_tool.py, or the Enable version https://github.com/enthought/enable/blob/master/enable/tools/move_tool.py; for drag to resize, use the ResizeTool https://github.com/enthought/enable/blob/master/enable/tools/resize_tool.py; and for dragging to change an arbitrary trait, there's the ValueDragTool and the AttributeDragTool https://github.com/enthought/enable/blob/master/enable/tools/value_drag_tool.py).

@thorsant
Copy link
Author

Hi,

Thanks for you swift response.

I have implemented all methods, is_draggable, drag_start, dragging, drag_cancel and drag_end, but, none of these gets called unless I handle the normal_left_down event as described above. You say DragTool handles the mouse down events, but it does not seem to handle them correctly. For the tool to call drag_start, the attribute left_down in the mouse_move event must be True(Line 143-145), but this attribute is only True if the left_down event is handled as described in my original post (or consumed by DragTool). Should the left_down event be consumed? And the modifier keys dont seem to be used.

@corranwebster
Copy link
Contributor

The DragTool captures the mouse events at a lower level, so you shouldn't need to use normal_left_down. It does this so you can choose which button to drag with via the drag_button trait (which defaults to "left" and which I assume you haven't over-ridden).

Can you provide a snippet of code that illustrates your problem?

@thorsant
Copy link
Author

Hi

I think I have figured it out, but I still need some help. I compared our code to one of the examples, and we are using a PanTool, with drag_button=left, thus the PanTool takes the event. Removing the PanTool made it work as it should. I then enabled the PanTool again and tried setting the modifier_key of the DragTool to 'alt' so I can use them together, but this does not work, the PanTool takes the mouse down event anyhow

I have a image plot, to which I append a PanTool. Then I plot several line plots on top of the image plot, and attach my LineMoveTool. I also have a ZoomTool with drag_button=right

Any ideas to how this can be made to work?

@thorsant
Copy link
Author

I tried setting the drag_button of the PanTool in the example (https://github.com/enthought/chaco/blob/master/examples/demo/edit_line.py) to right, and playing around with the order of the two tools, but it seems like the PanTool takes the event anyhow. Setting the modifier_key of the DragTool makes no difference

@corranwebster
Copy link
Contributor

The modifier key trait isn't hooked up by default - and it probably should be (but there are issues with the current implementation enthought/enable#90). For an example of how to handle modifier keys in your subclass, see the ValueDragTool above.

As far as https://github.com/enthought/chaco/blob/master/examples/demo/edit_line.py goes, it is working for me as expected: the PanTool's drag button is set to right and I can drag either the points or pan the plot as expected.

In terms of adding it to your plot, I would add your tool first, then the PanTool to the list of tools if you intend to share the same mouse button, since the PanTool will consume the event if it matches.

I feel like I am missing some important information related to your LineMoveTool. Would you care to post it (or a relevant subset)?

@thorsant
Copy link
Author

I get the edit_line.py example to work correctly, but if I set drag_button to left in the PanTool, the pan tool always handles the event, no matter what order I add them the plot.

My LineMoveTool is very similar to the tool in edit_line.py.

In the ValueDragTool, the tool consumes the drag button down event in case the drag button is clicked and the tool says it is draggable, while DragTool never consumes this event. Why the difference?

I'll try to restructure my overlays and tools, and see what I can get from the ValueDragTool. Maybe I'll just have to implement my own tool from bottom up to get the functionality I am seeking. What I want is: If I click on one of the lines in the plot, my MoveLineTool should be invoked, but if I click and dont hit any lines, I would like the PanTool to be invoked. I guess my LineMoveTool then has to consume the event in the same way as the ValueDragTool does?

@thorsant
Copy link
Author

I now see that component sends the event to all tools, even though one tool has consumed the event, i.e. set handled to True. But if a line is clicked, I can set my LineMoveTool to the active tool in the component, then this tool will have priority over events. Should I just set the _active_tool attribute in the component or is there a more proper way of doing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants