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