1 |
jan |
2411 |
# Copyright (C) 2002, 2004 by Intevation GmbH |
2 |
bh |
208 |
# Authors: |
3 |
jan |
2411 |
# Bernhard Herzog <[email protected]> (2002) |
4 |
|
|
# Jan-Oliver Wagner <[email protected]> (2004) |
5 |
bh |
208 |
# |
6 |
|
|
# This program is free software under the GPL (>=v2) |
7 |
|
|
# Read the file COPYING coming with Thuban for details. |
8 |
|
|
|
9 |
|
|
""" |
10 |
|
|
Extend thuban with a simple tool. |
11 |
|
|
""" |
12 |
|
|
|
13 |
|
|
__version__ = "$Revision$" |
14 |
jan |
2411 |
# $Source$ |
15 |
|
|
# $Id$ |
16 |
bh |
208 |
|
17 |
|
|
# First import some things we need later. |
18 |
|
|
import os |
19 |
|
|
from math import hypot |
20 |
bh |
358 |
from Thuban.UI.command import registry, ToolCommand |
21 |
jan |
2411 |
from Thuban.UI.mainwindow import main_toolbar |
22 |
jan |
1715 |
from Thuban.UI.viewport import Tool |
23 |
bh |
208 |
|
24 |
|
|
|
25 |
|
|
# A tool is a class usually derived from the Tool class. The tool class |
26 |
|
|
# provides some standard methods to handle mouse events. It maintains a |
27 |
|
|
# few instance variables that can be used when processing mouse events: |
28 |
|
|
# self.start is the point where the user started to drag, self.current |
29 |
|
|
# is the current position, self.dragging is true while the user is |
30 |
|
|
# moving the mouse with the (left) button pressed. |
31 |
|
|
|
32 |
|
|
class SimpleTool(Tool): |
33 |
|
|
|
34 |
|
|
"""A Simple Tool""" |
35 |
|
|
|
36 |
|
|
def Name(self): |
37 |
|
|
"""Return the string 'SimpleTool'.""" |
38 |
|
|
# The return value is used to identify tools easily |
39 |
|
|
return "SimpleTool" |
40 |
|
|
|
41 |
|
|
def map_distance(self): |
42 |
|
|
"""Return the distance on the map between the start and current point |
43 |
|
|
""" |
44 |
|
|
# self.view is the canvas window the tool instance is working |
45 |
|
|
# on. Its win_to_proj method computes the coordinates in the |
46 |
|
|
# projected map coordinates for a given point in window |
47 |
|
|
# coordinates |
48 |
|
|
sx, sy = apply(self.view.win_to_proj, self.start) |
49 |
|
|
x, y = apply(self.view.win_to_proj, self.current) |
50 |
|
|
return hypot(x - sx, y - sy) |
51 |
|
|
|
52 |
|
|
def MouseMove(self, event): |
53 |
|
|
"""Called by the canvas window when the mouse moves""" |
54 |
|
|
# The self.dragging flag is true, if the user is currently |
55 |
|
|
# dragging the mouse. Code in the Tool class has already handled |
56 |
|
|
# the button press event to set this flag. |
57 |
|
|
if self.dragging: |
58 |
|
|
# Call the inherited method to update some internal data |
59 |
|
|
# (self.start, etc.) |
60 |
|
|
Tool.MouseMove(self, event) |
61 |
|
|
print "SimpleTool: current distance", self.map_distance() |
62 |
|
|
|
63 |
|
|
def MouseUp(self, event): |
64 |
|
|
if self.dragging: |
65 |
|
|
Tool.MouseUp(self, event) |
66 |
|
|
print "SimpleTool: final distance", self.map_distance() |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
# the function implementing the "SimpleTool" command. Set the tool of |
70 |
|
|
# the canvas to SimpleTool |
71 |
|
|
def simple_tool(context): |
72 |
bh |
223 |
canvas = context.mainwindow.canvas |
73 |
bh |
358 |
canvas.SelectTool(SimpleTool(canvas)) |
74 |
bh |
208 |
|
75 |
|
|
|
76 |
|
|
# Add the command to the registry. A command is represented by a Command |
77 |
|
|
# instance. Here it's instantiated with the the name of the command, |
78 |
|
|
# it's title and the function to call when the command is invoked by the |
79 |
|
|
# user as positional arguments. The name is used internally to identify |
80 |
|
|
# commands. The title is displayed in the menus. |
81 |
|
|
# |
82 |
|
|
# The icon keyword argument is optional and only useful if the command |
83 |
|
|
# is used in a toolbar. It should be the name of an XPM file without the |
84 |
|
|
# .xpm extension which will be automatically appended. We assume here |
85 |
|
|
# that the icon's XPM file is located in the same directory as this |
86 |
|
|
# module. |
87 |
|
|
# |
88 |
bh |
244 |
# The helptext keyword argument is an optional helptext. |
89 |
bh |
208 |
# |
90 |
|
|
# The checked keyword argument is an optional function to determine |
91 |
|
|
# whether the button or menu item should be checked. It's called with |
92 |
|
|
# the context. If the checked argument is not given the button or menu |
93 |
|
|
# item will be a normal command button/item. |
94 |
|
|
|
95 |
|
|
def check_simple_tool(context): |
96 |
|
|
"""Return if the current tool of the context is the simple tool""" |
97 |
|
|
# the CurrentTool() method of the canvas returns the result of the |
98 |
|
|
# tool's Name method so we just have to compare it to "SimpleTool" |
99 |
bh |
223 |
return context.mainwindow.canvas.CurrentTool() == "SimpleTool" |
100 |
bh |
208 |
|
101 |
|
|
iconfile = os.path.abspath(os.path.join(os.path.split(__file__)[0], |
102 |
|
|
"simple_tool")) |
103 |
bh |
358 |
registry.Add(ToolCommand("simple_tool", "Simple Tool", simple_tool, |
104 |
|
|
icon = iconfile, helptext = "Simple Tool", |
105 |
|
|
checked = check_simple_tool)) |
106 |
bh |
208 |
|
107 |
|
|
# Add the command to the toolbar |
108 |
jan |
2411 |
main_toolbar.InsertSeparator() |
109 |
|
|
main_toolbar.InsertItem("simple_tool") |